// 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)
// ***************************************************************************
#include <utils/bamtools_utilities.h>
using namespace BamTools;
+#include <fstream>
#include <iostream>
#include <string>
#include <vector>
// flags
bool HasInput;
+ bool HasInputFilelist;
bool HasRegion;
// filenames
vector<string> InputFiles;
+ string InputFilelist;
string Region;
// constructor
CountSettings(void)
: HasInput(false)
+ , HasInputFilelist(false)
, HasRegion(false)
{ }
};
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;
// 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
// 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
, 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) {