]> git.donarmstrong.com Git - bamtools.git/blobdiff - src/toolkit/bamtools_revert.cpp
Major update to BamTools version 1.0
[bamtools.git] / src / toolkit / bamtools_revert.cpp
index 0a6c08ac822dee535087ef9b45eb81c66bc57c95..a9da67e519611a517dc458223186943735e4f7f5 100644 (file)
@@ -3,20 +3,22 @@
 // Marth Lab, Department of Biology, Boston College
 // All rights reserved.
 // ---------------------------------------------------------------------------
-// Last modified: 5 December 2010
+// Last modified: 21 March 2011
 // ---------------------------------------------------------------------------
 // Prints general alignment statistics for BAM file(s).
 // ***************************************************************************
 
+#include "bamtools_revert.h"
+
+#include <api/BamReader.h>
+#include <api/BamWriter.h>
+#include <utils/bamtools_options.h>
+#include <utils/bamtools_utilities.h>
+using namespace BamTools;
+
 #include <iostream>
 #include <string>
-#include "bamtools_revert.h"
-#include "bamtools_options.h"
-#include "bamtools_utilities.h"
-#include "BamReader.h"
-#include "BamWriter.h"
 using namespace std;
-using namespace BamTools;
 
 // ---------------------------------------------
 // RevertSettings implementation
@@ -37,12 +39,12 @@ struct RevertTool::RevertSettings {
     // constructor
     RevertSettings(void)
         : HasInput(false)
-       , HasOutput(false)
-       , IsForceCompression(false)
-       , IsKeepDuplicateFlag(false)
-       , IsKeepQualities(false)
-       , InputFilename(Options::StandardIn())
-       , OutputFilename(Options::StandardOut())
+        , HasOutput(false)
+        , IsForceCompression(false)
+        , IsKeepDuplicateFlag(false)
+        , IsKeepQualities(false)
+        , InputFilename(Options::StandardIn())
+        , OutputFilename(Options::StandardOut())
     { }
 };  
 
@@ -62,12 +64,12 @@ struct RevertTool::RevertToolPrivate {
         
     // internal methods
     private:
-       void RevertAlignment(BamAlignment& al);
+        void RevertAlignment(BamAlignment& al);
         
     // data members
     private:
         RevertTool::RevertSettings* m_settings;
-       string m_OQ;
+        string m_OQ;
 };
 
 RevertTool::RevertToolPrivate::RevertToolPrivate(RevertTool::RevertSettings* settings)
@@ -84,16 +86,16 @@ void RevertTool::RevertToolPrivate::RevertAlignment(BamAlignment& al) {
 
     // replace Qualities with OQ, if requested
     if ( !m_settings->IsKeepQualities ) {
-       string originalQualities;
-       if ( al.GetTag(m_OQ, originalQualities) ) {
-           al.Qualities = originalQualities;
-           al.RemoveTag(m_OQ);
-       }
+        string originalQualities;
+        if ( al.GetTag(m_OQ, originalQualities) ) {
+            al.Qualities = originalQualities;
+            al.RemoveTag(m_OQ);
+        }
     }
 
     // clear duplicate flag, if requested
     if ( !m_settings->IsKeepDuplicateFlag )
-       al.SetIsDuplicate(false);
+        al.SetIsDuplicate(false);
 }
 
 bool RevertTool::RevertToolPrivate::Run(void) {
@@ -101,7 +103,8 @@ bool RevertTool::RevertToolPrivate::Run(void) {
     // opens the BAM file without checking for indexes
     BamReader reader;
     if ( !reader.Open(m_settings->InputFilename) ) {
-       cerr << "Could not open input BAM file... quitting." << endl;
+        cerr << "bamtools revert ERROR: could not open " << m_settings->InputFilename
+             << " for reading... Aborting." << endl;
         return false;
     }
 
@@ -109,18 +112,26 @@ bool RevertTool::RevertToolPrivate::Run(void) {
     const string& headerText = reader.GetHeaderText();
     const RefVector& references = reader.GetReferenceData();
     
-    // open writer
+    // determine compression mode for BamWriter
+    bool writeUncompressed = ( m_settings->OutputFilename == Options::StandardOut() &&
+                              !m_settings->IsForceCompression );
+    BamWriter::CompressionMode compressionMode = BamWriter::Compressed;
+    if ( writeUncompressed ) compressionMode = BamWriter::Uncompressed;
+
+    // open BamWriter
     BamWriter writer;
-    bool writeUncompressed = ( m_settings->OutputFilename == Options::StandardOut() && !m_settings->IsForceCompression );
-    if ( !writer.Open(m_settings->OutputFilename, headerText, references, writeUncompressed) ) {
-        cerr << "Could not open " << m_settings->OutputFilename << " for writing." << endl;
+    writer.SetCompressionMode(compressionMode);
+    if ( !writer.Open(m_settings->OutputFilename, headerText, references) ) {
+        cerr << "bamtools revert ERROR: could not open " << m_settings->OutputFilename
+             << " for writing... Aborting." << endl;
+        reader.Close();
         return false;
     }
 
     // plow through file, reverting alignments
     BamAlignment al;
     while ( reader.GetNextAlignment(al) ) {
-       RevertAlignment(al);
+        RevertAlignment(al);
         writer.SaveAlignment(al);
     }
     
@@ -139,7 +150,7 @@ RevertTool::RevertTool(void)
     , m_impl(0)
 {
     // set program details
-    Options::SetProgramInfo("bamtools revert", "removes duplicate marks and restores original (non-recalibrated) base qualities", "[-in <filename> ... ]");
+    Options::SetProgramInfo("bamtools revert", "removes duplicate marks and restores original (non-recalibrated) base qualities", "[-in <filename> -in <filename> ...] [-out <filename> | [-forceCompression]] [revertOptions]");
     
     // set up options 
     OptionGroup* IO_Opts = Options::CreateOptionGroup("Input & Output");