1 // ***************************************************************************
2 // bamtools_count.cpp (c) 2010 Derek Barnett, Erik Garrison
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 3 September 2010
7 // ---------------------------------------------------------------------------
8 // Prints alignment count for BAM file(s)
9 // ***************************************************************************
15 #include "bamtools_count.h"
16 #include "bamtools_options.h"
17 #include "bamtools_utilities.h"
18 #include "BamReader.h"
19 #include "BamMultiReader.h"
22 using namespace BamTools;
24 // ---------------------------------------------
25 // CountSettings implementation
27 struct CountTool::CountSettings {
34 vector<string> InputFiles;
44 // ---------------------------------------------
45 // CountTool implementation
47 CountTool::CountTool(void)
49 , m_settings(new CountSettings)
51 // set program details
52 Options::SetProgramInfo("bamtools count", "prints alignment counts for a BAM file", "[-in <filename> -in <filename> ...] [-region <REGION>]");
55 OptionGroup* IO_Opts = Options::CreateOptionGroup("Input & Output");
56 Options::AddValueOption("-in", "BAM filename", "the input BAM file(s)", "", m_settings->HasInput, m_settings->InputFiles, IO_Opts, Options::StandardIn());
58 OptionGroup* FilterOpts = Options::CreateOptionGroup("Filters");
59 Options::AddValueOption("-region", "REGION", "genomic region. Index file is required and is read automatically if it exists as <filename>.bai or <filename>.bti. See \'bamtools help index\' for more details on creating one", "", m_settings->HasRegion, m_settings->Region, FilterOpts);
62 CountTool::~CountTool(void) {
67 int CountTool::Help(void) {
68 Options::DisplayHelp();
72 int CountTool::Run(int argc, char* argv[]) {
74 // parse command line arguments
75 Options::Parse(argc, argv, 1);
77 // if no '-in' args supplied, default to stdin
78 if ( !m_settings->HasInput )
79 m_settings->InputFiles.push_back(Options::StandardIn());
81 // open reader without index
82 BamMultiReader reader;
83 reader.Open(m_settings->InputFiles, false, true);
87 int alignmentCount(0);
89 // if no region specified, count entire file
90 if ( !m_settings->HasRegion ) {
91 while ( reader.GetNextAlignmentCore(al) )
95 // otherwise attempt to use region as constraint
98 // if region string parses OK
100 if ( Utilities::ParseRegionString(m_settings->Region, reader, region) ) {
102 // attempt to re-open reader with index files
104 bool openedOK = reader.Open(m_settings->InputFiles, true, true );
108 cerr << "ERROR: Could not open input BAM file(s)... Aborting." << endl;
112 // if index data available, we can use SetRegion
113 if ( reader.IsIndexLoaded() ) {
115 // attempt to use SetRegion(), if failed report error
116 if ( !reader.SetRegion(region.LeftRefID, region.LeftPosition, region.RightRefID, region.RightPosition) ) {
117 cerr << "ERROR: Region requested, but could not set BamReader region to REGION: " << m_settings->Region << " Aborting." << endl;
122 // everything checks out, just iterate through specified region, counting alignments
123 while ( reader.GetNextAlignmentCore(al) )
127 // no index data available, we have to iterate through until we
128 // find overlapping alignments
130 while( reader.GetNextAlignmentCore(al) ) {
131 if ( (al.RefID >= region.LeftRefID) && ( (al.Position + al.Length) >= region.LeftPosition ) &&
132 (al.RefID <= region.RightRefID) && ( al.Position <= region.RightPosition) )
140 // error parsing REGION string
142 cerr << "ERROR: Could not parse REGION - " << m_settings->Region << endl;
143 cerr << "Be sure REGION is in valid format (see README) and that coordinates are valid for selected references" << endl;
150 cout << alignmentCount << endl;