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: 23 March 2011
7 // ---------------------------------------------------------------------------
8 // Prints alignment count for BAM file(s)
9 // ***************************************************************************
11 #include "bamtools_count.h"
13 #include <api/BamMultiReader.h>
14 #include <utils/bamtools_options.h>
15 #include <utils/bamtools_utilities.h>
16 using namespace BamTools;
23 // ---------------------------------------------
24 // CountSettings implementation
26 struct CountTool::CountSettings {
33 vector<string> InputFiles;
43 // ---------------------------------------------
44 // CountTool implementation
46 CountTool::CountTool(void)
48 , m_settings(new CountSettings)
50 // set program details
51 Options::SetProgramInfo("bamtools count", "prints number of alignments in BAM file(s)", "[-in <filename> -in <filename> ...] [-region <REGION>]");
54 OptionGroup* IO_Opts = Options::CreateOptionGroup("Input & Output");
55 Options::AddValueOption("-in", "BAM filename", "the input BAM file(s)", "", m_settings->HasInput, m_settings->InputFiles, IO_Opts, Options::StandardIn());
56 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);
59 CountTool::~CountTool(void) {
64 int CountTool::Help(void) {
65 Options::DisplayHelp();
69 int CountTool::Run(int argc, char* argv[]) {
71 // parse command line arguments
72 Options::Parse(argc, argv, 1);
74 // if no '-in' args supplied, default to stdin
75 if ( !m_settings->HasInput )
76 m_settings->InputFiles.push_back(Options::StandardIn());
78 // open reader without index
79 BamMultiReader reader;
80 if ( !reader.Open(m_settings->InputFiles) ) {
81 cerr << "bamtools count ERROR: could not open input BAM file(s)... Aborting." << endl;
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 find index files
103 reader.LocateIndexes();
105 // if index data available for all BAM files, we can use SetRegion
106 if ( reader.IsIndexLoaded() ) {
108 // attempt to set region on reader
109 if ( !reader.SetRegion(region.LeftRefID, region.LeftPosition, region.RightRefID, region.RightPosition) ) {
110 cerr << "bamtools count ERROR: set region failed. Check that REGION describes a valid range" << endl;
115 // everything checks out, just iterate through specified region, counting alignments
116 while ( reader.GetNextAlignmentCore(al) )
120 // no index data available, we have to iterate through until we
121 // find overlapping alignments
123 while ( reader.GetNextAlignmentCore(al) ) {
124 if ( (al.RefID >= region.LeftRefID) && ( (al.Position + al.Length) >= region.LeftPosition ) &&
125 (al.RefID <= region.RightRefID) && ( al.Position <= region.RightPosition) )
133 // error parsing REGION string
135 cerr << "bamtools count ERROR: could not parse REGION - " << m_settings->Region << endl;
136 cerr << "Check that REGION is in valid format (see documentation) and that the coordinates are valid"
144 cout << alignmentCount << endl;