From 2fa1d42264e20cc4188fb6958ac6d576f8d3b24b Mon Sep 17 00:00:00 2001
From: Birte Kehr <birte.kehr@bihealth.de>
Date: Thu, 25 Aug 2022 19:37:23 +0200
Subject: [PATCH] Adding back an option to suppress printing the
 POPINS_SAMPLE_INFO file.

---
 src/argument_parsing.h      |  9 ++++++-
 src/popins2_crop_unmapped.h | 54 +++++++++++++++++--------------------
 2 files changed, 32 insertions(+), 31 deletions(-)

diff --git a/src/argument_parsing.h b/src/argument_parsing.h
index 2579147..0114cf4 100644
--- a/src/argument_parsing.h
+++ b/src/argument_parsing.h
@@ -38,6 +38,8 @@ struct CropUnmappedOptions {
 
     float alignment_score_factor;
 
+    bool printSampleInfo;
+
     CropUnmappedOptions () :
         matesBam("mates.bam"),
         pe1("paired.1.fastq"),
@@ -46,7 +48,8 @@ struct CropUnmappedOptions {
         prefix("."),
         sampleID(""),
         humanSeqs(maxValue<int>()),
-        alignment_score_factor(0.67f)
+        alignment_score_factor(0.67f),
+        printSampleInfo(true)
     {}
 };
 
@@ -251,6 +254,8 @@ bool getOptionValues(CropUnmappedOptions & options, ArgumentParser const & parse
         getOptionValue(options.humanSeqs, parser, "filter");
     if (isSet(parser, "alignment-score-factor"))
         getOptionValue(options.alignment_score_factor, parser, "alignment-score-factor");
+    if (isSet(parser, "noSampleInfo"))
+        options.printSampleInfo = false;
 
     return true;
 }
@@ -569,6 +574,7 @@ void setupParser(ArgumentParser & parser, CropUnmappedOptions & options){
     addOption(parser, ArgParseOption("pe2", "paired2", "File name for the second reads file.", ArgParseArgument::INPUT_FILE, "FASTQ FILE"));
     addOption(parser, ArgParseOption("se", "single", "File name for the single reads file.", ArgParseArgument::INPUT_FILE, "FASTQ FILE"));
     addOption(parser, ArgParseOption("m", "mates", "File name for the aligned mates BAM file.", ArgParseArgument::OUTPUT_FILE, "BAM FILE"));
+    addOption(parser, ArgParseOption("i", "noSampleInfo", "Do not print sample information to POPINS_SAMPLE_INFO file in sample directory."));
 
     addSection(parser, "Algorithm options");
     addOption(parser, ArgParseOption("a", "adapters", "Enable adapter removal for Illumina reads. Default: \\fIno adapter removal\\fP.", ArgParseArgument::STRING, "STR"));
@@ -589,6 +595,7 @@ void setupParser(ArgumentParser & parser, CropUnmappedOptions & options){
     setDefaultValue(parser, "prefix", "\'.\'");
     setDefaultValue(parser, "sample", "retrieval from BAM file header");
     setDefaultValue(parser, "alignment-score-factor", options.alignment_score_factor);
+    setDefaultValue(parser, "noSampleInfo", "false");
 
     setMinValue(parser, "alignment-score-factor", "0.0");
     setMaxValue(parser, "alignment-score-factor", "1.0");
diff --git a/src/popins2_crop_unmapped.h b/src/popins2_crop_unmapped.h
index 4d64020..ea24a52 100644
--- a/src/popins2_crop_unmapped.h
+++ b/src/popins2_crop_unmapped.h
@@ -79,44 +79,38 @@ int popins2_crop_unmapped(int argc, char const ** argv){
     CharString fastqSingle = getFileName(workingDirectory, options.se);
     Triple<CharString> fastqFiles = Triple<CharString>(fastqFirst, fastqSecond, fastqSingle);
 
+    msg.str("");
+    msg << "Cropping unmapped reads from " << options.mappingFile;
+    printStatus(msg);
 
+    // Crop unmapped reads and reads with unreliable mappings from the input bam file.
+    if (options.adapters == "HiSeqX")
+    {
+        if (crop_unmapped(info.avg_cov, fastqFiles, matesBam, options.mappingFile, options.humanSeqs, HiSeqXAdapters(), as_factor) != 0)
+            return 7;
+    }
+    else if (options.adapters == "HiSeq")
+    {
+        if (crop_unmapped(info.avg_cov, fastqFiles, matesBam, options.mappingFile, options.humanSeqs, HiSeqAdapters(), as_factor) != 0)
+            return 7;
+    }
+    else
+    {
+        if (crop_unmapped(info.avg_cov, fastqFiles, matesBam, options.mappingFile, options.humanSeqs, NoAdapters(), as_factor) != 0)
+            return 7;
+    }
 
-    // check if files already exits
-    std::fstream stream(toCString(fastqFirst));
-    if (!stream.is_open())
+    if (options.printSampleInfo)
     {
+        CharString sampleInfoFile = getFileName(workingDirectory, "POPINS_SAMPLE_INFO");
+        writeSampleInfo(info, sampleInfoFile);
+
         msg.str("");
-        msg << "Cropping unmapped reads from " << options.mappingFile;
+        msg << "Sample info written to \'" << sampleInfoFile << "\'.";
         printStatus(msg);
-
-        // Crop unmapped reads and reads with unreliable mappings from the input bam file.
-        if (options.adapters == "HiSeqX")
-        {
-            if (crop_unmapped(info.avg_cov, fastqFiles, matesBam, options.mappingFile, options.humanSeqs, HiSeqXAdapters(), as_factor) != 0)
-                return 7;
-        }
-        else if (options.adapters == "HiSeq")
-        {
-            if (crop_unmapped(info.avg_cov, fastqFiles, matesBam, options.mappingFile, options.humanSeqs, HiSeqAdapters(), as_factor) != 0)
-                return 7;
-        }
-        else
-        {
-            if (crop_unmapped(info.avg_cov, fastqFiles, matesBam, options.mappingFile, options.humanSeqs, NoAdapters(), as_factor) != 0)
-                return 7;
-        }
-
-	    CharString sampleInfoFile = getFileName(workingDirectory, "POPINS_SAMPLE_INFO");
-	    writeSampleInfo(info, sampleInfoFile);
-
-	    msg.str("");
-	    msg << "Sample info written to \'" << sampleInfoFile << "\'.";
-	    printStatus(msg);
-      
     }
 
     return res;
-
 }
 
 #endif // #ifndef POPINS2_CROP_UNMAPPED_H_
-- 
GitLab