]> git.donarmstrong.com Git - bamtools.git/blobdiff - src/toolkit/bamtools_count.cpp
merge master 2.3.0
[bamtools.git] / src / toolkit / bamtools_count.cpp
index 7244f241361cbba1603f3f1d93b0b1732dc1e9d4..5a7c0a7e2f31a9ce15c180d84e46584f8cf430f7 100644 (file)
@@ -2,7 +2,7 @@
 // bamtools_count.cpp (c) 2010 Derek Barnett, Erik Garrison
 // Marth Lab, Department of Biology, Boston College
 // ---------------------------------------------------------------------------
-// Last modified: 7 April 2011
+// Last modified: 10 December 2012
 // ---------------------------------------------------------------------------
 // Prints alignment count for BAM file(s)
 // ***************************************************************************
@@ -15,6 +15,7 @@
 #include <utils/bamtools_utilities.h>
 using namespace BamTools;
 
+#include <fstream>
 #include <iostream>
 #include <string>
 #include <vector>
@@ -27,15 +28,18 @@ struct CountTool::CountSettings {
 
     // flags
     bool HasInput;
+    bool HasInputFilelist;
     bool HasRegion;
 
     // filenames
     vector<string> InputFiles;
+    string InputFilelist;
     string Region;
     
     // constructor
     CountSettings(void)
         : HasInput(false)
+        , HasInputFilelist(false)
         , HasRegion(false)
     { }  
 }; 
@@ -62,31 +66,25 @@ struct CountTool::CountToolPrivate {
         CountTool::CountSettings* m_settings;
 };
 
-static
-void printAlignments(const vector<BamAlignment>& alignments) {
-
-    vector<BamAlignment>::const_iterator alIter = alignments.begin();
-    vector<BamAlignment>::const_iterator alEnd  = alignments.end();
-    for ( ; alIter != alEnd; ++alIter ) {
-        const BamAlignment& a = (*alIter);
+bool CountTool::CountToolPrivate::Run(void) {
 
-        cerr << a.Name
-             << "\t" << a.RefID << ":" << a.Position;
+    // set to default input if none provided
+    if ( !m_settings->HasInput && !m_settings->HasInputFilelist )
+        m_settings->InputFiles.push_back(Options::StandardIn());
 
-        int aqValue;
-        bool hasTag = a.GetTag("Aq", aqValue);
-        cerr << "\tAq=";
-        if ( hasTag ) cerr << aqValue;
-        else cerr << "?";
-        cerr << endl;
-    }
-}
+    // add files in the filelist to the input file list
+    if ( m_settings->HasInputFilelist ) {
 
-bool CountTool::CountToolPrivate::Run(void) {
+        ifstream filelist(m_settings->InputFilelist.c_str(), ios::in);
+        if ( !filelist.is_open() ) {
+            cerr << "bamtools count ERROR: could not open input BAM file list... Aborting." << endl;
+            return false;
+        }
 
-    // if no '-in' args supplied, default to stdin
-    if ( !m_settings->HasInput )
-        m_settings->InputFiles.push_back(Options::StandardIn());
+        string line;
+        while ( getline(filelist, line) )
+            m_settings->InputFiles.push_back(line);
+    }
 
     // open reader without index
     BamMultiReader reader;
@@ -101,74 +99,8 @@ bool CountTool::CountToolPrivate::Run(void) {
 
     // if no region specified, count entire file
     if ( !m_settings->HasRegion ) {
-
-
-        vector<BamAlignment> alignments;
-        while ( reader.GetNextAlignment(al) ) {
+        while ( reader.GetNextAlignmentCore(al) )
             ++alignmentCount;
-
-            if ( alignments.size() < 100 )
-                alignments.push_back(al);
-        }
-
-        using namespace BamTools::Algorithms;
-
-        cerr << endl
-             << "------------------------------" << endl
-             << "Unsorted Alignments" << endl
-             << "------------------------------" << endl
-             << endl;
-        std::stable_sort(alignments.begin(), alignments.end(), Sort::Unsorted());
-        printAlignments(alignments);
-        cerr << "------------------------------" << endl
-             << endl;
-
-        cerr << endl
-             << "------------------------------" << endl
-             << "Sorted Alignments (by name)" << endl
-             << "------------------------------" << endl
-             << endl;
-//        std::sort(alignments.begin(), alignments.end(), Sort::ByName());
-        Algorithms::SortAlignments(alignments, Sort::ByName());
-        printAlignments(alignments);
-        cerr << endl
-             << "------------------------------" << endl
-             << endl;
-
-        cerr << endl
-             << "------------------------------" << endl
-             << "Sorted Alignments (by tag Aq)" << endl
-             << "------------------------------" << endl
-             << endl;
-//        std::sort(alignments.begin(), alignments.end(), Sort::ByTag<int>("Aq"));
-        Algorithms::SortAlignments(alignments, Sort::ByTag<int>("Aq"));
-        printAlignments(alignments);
-        cerr << endl
-             << "------------------------------" << endl
-             << endl;
-
-        cerr << endl
-             << "------------------------------" << endl
-             << "Sorted Alignments (by tag Aq) desc" << endl
-             << "------------------------------" << endl
-             << endl;
-        std::sort(alignments.begin(), alignments.end(), Sort::ByTag<int>("Aq", Sort::DescendingOrder));
-        printAlignments(alignments);
-        cerr << endl
-             << "------------------------------" << endl
-             << endl;
-
-
-
-
-//        // ########################################
-//        // original
-//        // ########################################
-//
-//        while ( reader.GetNextAlignmentCore(al) )
-//            ++alignmentCount;
-//
-//        //#########################################
     }
 
     // otherwise attempt to use region as constraint
@@ -184,27 +116,16 @@ bool CountTool::CountToolPrivate::Run(void) {
             // if index data available for all BAM files, we can use SetRegion
             if ( reader.HasIndexes() ) {
 
-                vector<BamAlignment> alignments;
-                using namespace BamTools::Algorithms;
-
-                alignments = GetSortedRegion(reader, region, Sort::ByName() );
-                printAlignments(alignments);
-
-                cerr << "################################" << endl;
-
-                alignments = GetSortedRegion(reader, region, Sort::ByTag<int>("Aq"));
-                printAlignments(alignments);
-
-//                // attempt to set region on reader
-//                if ( !reader.SetRegion(region.LeftRefID, region.LeftPosition, region.RightRefID, region.RightPosition) ) {
-//                    cerr << "bamtools count ERROR: set region failed. Check that REGION describes a valid range" << endl;
-//                    reader.Close();
-//                    return false;
-//                }
+                // attempt to set region on reader
+                if ( !reader.SetRegion(region.LeftRefID, region.LeftPosition, region.RightRefID, region.RightPosition) ) {
+                    cerr << "bamtools count ERROR: set region failed. Check that REGION describes a valid range" << endl;
+                    reader.Close();
+                    return false;
+                }
 
-//                // everything checks out, just iterate through specified region, counting alignments
-//                while ( reader.GetNextAlignmentCore(al) )
-//                    ++alignmentCount;
+                // everything checks out, just iterate through specified region, counting alignments
+                while ( reader.GetNextAlignmentCore(al) )
+                    ++alignmentCount;
             }
 
             // no index data available, we have to iterate through until we
@@ -247,12 +168,16 @@ CountTool::CountTool(void)
     , m_impl(0)
 { 
     // set program details
-    Options::SetProgramInfo("bamtools count", "prints number of alignments in BAM file(s)", "[-in <filename> -in <filename> ...] [-region <REGION>]");
+    Options::SetProgramInfo("bamtools count", "prints number of alignments in BAM file(s)",
+                            "[-in <filename> -in <filename> ... | -list <filelist>] [-region <REGION>]");
     
     // set up options 
     OptionGroup* IO_Opts = Options::CreateOptionGroup("Input & Output");
     Options::AddValueOption("-in",     "BAM filename", "the input BAM file(s)", "", m_settings->HasInput,  m_settings->InputFiles, IO_Opts, Options::StandardIn());
-    Options::AddValueOption("-region", "REGION",       "genomic region. Index file is recommended for better performance, and is used automatically if it exists. See \'bamtools help index\' for more details on creating one", "", m_settings->HasRegion, m_settings->Region, IO_Opts);
+    Options::AddValueOption("-list",   "filename", "the input BAM file list, one line per file", "", m_settings->HasInputFilelist,  m_settings->InputFilelist, IO_Opts);
+    Options::AddValueOption("-region", "REGION",
+                            "genomic region. Index file is recommended for better performance, and is used automatically if it exists. See \'bamtools help index\' for more details on creating one",
+                            "", m_settings->HasRegion, m_settings->Region, IO_Opts);
 }
 
 CountTool::~CountTool(void) {