]> git.donarmstrong.com Git - bamtools.git/blobdiff - src/toolkit/bamtools_convert.cpp
Reviewed BAM to YAML conversion contribution (thank you Chase Miller). Reorganized...
[bamtools.git] / src / toolkit / bamtools_convert.cpp
index f68207378788f3a1fcc7312c48e30f1334101005..b236b63dda0ce2eb20cc70c66d4e7a231269d523 100644 (file)
@@ -3,7 +3,7 @@
 // Marth Lab, Department of Biology, Boston College
 // All rights reserved.
 // ---------------------------------------------------------------------------
-// Last modified: 23 September 2010
+// Last modified: 4 October 2010
 // ---------------------------------------------------------------------------
 // Converts between BAM and a number of other formats
 // ***************************************************************************
@@ -34,10 +34,10 @@ namespace BamTools {
     static const string FORMAT_FASTA    = "fasta";
     static const string FORMAT_FASTQ    = "fastq";
     static const string FORMAT_JSON     = "json";
-    static const string FORMAT_YAML     = "yaml";
     static const string FORMAT_SAM      = "sam";
     static const string FORMAT_PILEUP   = "pileup";
     static const string FORMAT_WIGGLE   = "wig";
+    static const string FORMAT_YAML     = "yaml";
 
     // other constants
     static const unsigned int FASTA_LINE_MAX = 50;
@@ -130,9 +130,9 @@ struct ConvertTool::ConvertToolPrivate {
         void PrintFasta(const BamAlignment& a);
         void PrintFastq(const BamAlignment& a);
         void PrintJson(const BamAlignment& a);
-        void PrintYaml(const BamAlignment& a);
         void PrintSam(const BamAlignment& a);
         void PrintWiggle(const BamAlignment& a);
+       void PrintYaml(const BamAlignment& a);
         
         // special case - uses the PileupEngine
         bool RunPileupConversion(BamMultiReader* reader);
@@ -162,7 +162,21 @@ bool ConvertTool::ConvertToolPrivate::Run(void) {
     
     // open input files
     BamMultiReader reader;
-    reader.Open(m_settings->InputFiles);
+    if ( !m_settings->HasInput ) { // don't attempt to open index for stdin
+        if ( !reader.Open(m_settings->InputFiles, false) ) {
+            cerr << "Could not open input files" << endl;
+            return false;
+        }
+    } else {
+        if ( !reader.Open(m_settings->InputFiles, true) ) {
+            if ( !reader.Open(m_settings->InputFiles, false) ) {
+                cerr << "Could not open input files" << endl;
+                return false;
+            } else {
+                cerr << "Opened reader without index file, jumping is disabled." << endl;
+            }
+        }
+    }
     m_references = reader.GetReferenceData();
 
     // set region if specified
@@ -216,9 +230,9 @@ bool ConvertTool::ConvertToolPrivate::Run(void) {
         else if ( m_settings->Format == FORMAT_FASTA )    pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintFasta;
         else if ( m_settings->Format == FORMAT_FASTQ )    pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintFastq;
         else if ( m_settings->Format == FORMAT_JSON )     pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintJson;
-        else if ( m_settings->Format == FORMAT_YAML )     pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintYaml;
         else if ( m_settings->Format == FORMAT_SAM )      pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintSam;
         else if ( m_settings->Format == FORMAT_WIGGLE )   pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintWiggle;
+        else if ( m_settings->Format == FORMAT_YAML )     pFunction = &BamTools::ConvertTool::ConvertToolPrivate::PrintYaml;
         else { 
             cerr << "Unrecognized format: " << m_settings->Format << endl;
             cerr << "Please see help|README (?) for details on supported formats " << endl;
@@ -466,7 +480,10 @@ void ConvertTool::ConvertToolPrivate::PrintJson(const BamAlignment& a) {
                 case('H') : 
                     m_out << "\""; 
                     while (tagData[index]) {
-                        m_out << tagData[index];
+                        if (tagData[index] == '\"')
+                            m_out << "\\\""; // escape for json
+                        else
+                            m_out << tagData[index];
                         ++index;
                     }
                     m_out << "\""; 
@@ -484,48 +501,6 @@ void ConvertTool::ConvertToolPrivate::PrintJson(const BamAlignment& a) {
     m_out << "}" << endl;
 }
 
-
-// Print BamAlignment in YAML format
-void ConvertTool::ConvertToolPrivate::PrintYaml(const BamAlignment& a) {
-
-
-        // write alignment name
-        m_out << "---" << endl;
-        m_out << a.Name << ":" << endl;
-
-        // write alignment data
-        m_out << "   " << "AlndBases: "  <<  a.AlignedBases << endl;
-        m_out << "   " << "Qualities: "     <<  a.Qualities << endl;
-        m_out << "   " << "Name: "          <<  a.Name << endl;
-        m_out << "   " << "Length: "        <<  a.Length << endl;
-        m_out << "   " << "TagData: "       <<  a.TagData << endl;
-        m_out << "   " << "RefID: "         <<  a.RefID << endl;
-        m_out << "   " << "RefName: "       <<  m_references[a.RefID].RefName << endl;
-        m_out << "   " << "Position: "      <<  a.Position << endl;
-        m_out << "   " << "Bin: "           <<  a.Bin << endl;
-        m_out << "   " << "MapQuality: "    <<  a.MapQuality << endl;
-        m_out << "   " << "AlignmentFlag: " <<  a.AlignmentFlag << endl;
-        m_out << "   " << "MateRefID: "     <<  a.MateRefID << endl;
-        m_out << "   " << "MatePosition: "  <<  a.MatePosition << endl;
-        m_out << "   " << "InsertSize: "    <<  a.InsertSize << endl;
-
-       // Write Cigar data
-       const vector<CigarOp>& cigarData = a.CigarData;
-       if ( !cigarData.empty() ) {
-          m_out << "   " <<  "Cigar: ";
-          vector<CigarOp>::const_iterator cigarBegin = cigarData.begin();
-          vector<CigarOp>::const_iterator cigarIter = cigarBegin;
-          vector<CigarOp>::const_iterator cigarEnd  = cigarData.end();
-          for ( ; cigarIter != cigarEnd; ++cigarIter ) {
-              const CigarOp& op = (*cigarIter);
-              m_out << op.Length << op.Type;
-          }
-       m_out << endl;
-       }
-
-}
-
-
 // print BamAlignment in SAM format
 void ConvertTool::ConvertToolPrivate::PrintSam(const BamAlignment& a) {
   
@@ -656,6 +631,44 @@ void ConvertTool::ConvertToolPrivate::PrintWiggle(const BamAlignment& a) {
     ; 
 }
 
+// Print BamAlignment in YAML format
+void ConvertTool::ConvertToolPrivate::PrintYaml(const BamAlignment& a) {
+
+    // write alignment name
+    m_out << "---" << endl;
+    m_out << a.Name << ":" << endl;
+
+    // write alignment data
+    m_out << "   " << "AlndBases: "     <<  a.AlignedBases << endl;
+    m_out << "   " << "Qualities: "     <<  a.Qualities << endl;
+    m_out << "   " << "Name: "          <<  a.Name << endl;
+    m_out << "   " << "Length: "        <<  a.Length << endl;
+    m_out << "   " << "TagData: "       <<  a.TagData << endl;
+    m_out << "   " << "RefID: "         <<  a.RefID << endl;
+    m_out << "   " << "RefName: "       <<  m_references[a.RefID].RefName << endl;
+    m_out << "   " << "Position: "      <<  a.Position << endl;
+    m_out << "   " << "Bin: "           <<  a.Bin << endl;
+    m_out << "   " << "MapQuality: "    <<  a.MapQuality << endl;
+    m_out << "   " << "AlignmentFlag: " <<  a.AlignmentFlag << endl;
+    m_out << "   " << "MateRefID: "     <<  a.MateRefID << endl;
+    m_out << "   " << "MatePosition: "  <<  a.MatePosition << endl;
+    m_out << "   " << "InsertSize: "    <<  a.InsertSize << endl;
+
+    // write Cigar data
+    const vector<CigarOp>& cigarData = a.CigarData;
+    if ( !cigarData.empty() ) {
+       m_out << "   " <<  "Cigar: ";
+       vector<CigarOp>::const_iterator cigarBegin = cigarData.begin();
+       vector<CigarOp>::const_iterator cigarIter = cigarBegin;
+       vector<CigarOp>::const_iterator cigarEnd  = cigarData.end();
+       for ( ; cigarIter != cigarEnd; ++cigarIter ) {
+           const CigarOp& op = (*cigarIter);
+           m_out << op.Length << op.Type;
+       }
+       m_out << endl;
+    }
+}
+
 bool ConvertTool::ConvertToolPrivate::RunPileupConversion(BamMultiReader* reader) {
   
     // check for valid BamMultiReader