1 // ***************************************************************************
2 // bamtools_count.cpp (c) 2010 Derek Barnett, Erik Garrison
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 7 April 2011
6 // ---------------------------------------------------------------------------
7 // Prints alignment count for BAM file(s)
8 // ***************************************************************************
10 #include "bamtools_count.h"
12 #include <api/BamMultiReader.h>
13 #include <utils/bamtools_options.h>
14 #include <utils/bamtools_utilities.h>
15 using namespace BamTools;
22 // ---------------------------------------------
23 // CountSettings implementation
25 struct CountTool::CountSettings {
32 vector<string> InputFiles;
42 // ---------------------------------------------
43 // CountToolPrivate implementation
45 struct CountTool::CountToolPrivate {
49 CountToolPrivate(CountTool::CountSettings* settings)
50 : m_settings(settings)
53 ~CountToolPrivate(void) { }
61 CountTool::CountSettings* m_settings;
64 bool CountTool::CountToolPrivate::Run(void) {
66 // if no '-in' args supplied, default to stdin
67 if ( !m_settings->HasInput )
68 m_settings->InputFiles.push_back(Options::StandardIn());
70 // open reader without index
71 BamMultiReader reader;
72 if ( !reader.Open(m_settings->InputFiles) ) {
73 cerr << "bamtools count ERROR: could not open input BAM file(s)... Aborting." << endl;
79 int alignmentCount(0);
81 // if no region specified, count entire file
82 if ( !m_settings->HasRegion ) {
83 while ( reader.GetNextAlignmentCore(al) )
87 // otherwise attempt to use region as constraint
90 // if region string parses OK
92 if ( Utilities::ParseRegionString(m_settings->Region, reader, region) ) {
94 // attempt to find index files
95 reader.LocateIndexes();
97 // if index data available for all BAM files, we can use SetRegion
98 if ( reader.IsIndexLoaded() ) {
100 // attempt to set region on reader
101 if ( !reader.SetRegion(region.LeftRefID, region.LeftPosition, region.RightRefID, region.RightPosition) ) {
102 cerr << "bamtools count ERROR: set region failed. Check that REGION describes a valid range" << endl;
107 // everything checks out, just iterate through specified region, counting alignments
108 while ( reader.GetNextAlignmentCore(al) )
112 // no index data available, we have to iterate through until we
113 // find overlapping alignments
115 while ( reader.GetNextAlignmentCore(al) ) {
116 if ( (al.RefID >= region.LeftRefID) && ( (al.Position + al.Length) >= region.LeftPosition ) &&
117 (al.RefID <= region.RightRefID) && ( al.Position <= region.RightPosition) )
125 // error parsing REGION string
127 cerr << "bamtools count ERROR: could not parse REGION - " << m_settings->Region << endl;
128 cerr << "Check that REGION is in valid format (see documentation) and that the coordinates are valid"
136 cout << alignmentCount << endl;
143 // ---------------------------------------------
144 // CountTool implementation
146 CountTool::CountTool(void)
148 , m_settings(new CountSettings)
151 // set program details
152 Options::SetProgramInfo("bamtools count", "prints number of alignments in BAM file(s)", "[-in <filename> -in <filename> ...] [-region <REGION>]");
155 OptionGroup* IO_Opts = Options::CreateOptionGroup("Input & Output");
156 Options::AddValueOption("-in", "BAM filename", "the input BAM file(s)", "", m_settings->HasInput, m_settings->InputFiles, IO_Opts, Options::StandardIn());
157 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);
160 CountTool::~CountTool(void) {
169 int CountTool::Help(void) {
170 Options::DisplayHelp();
174 int CountTool::Run(int argc, char* argv[]) {
176 // parse command line arguments
177 Options::Parse(argc, argv, 1);
179 // initialize CountTool with settings
180 m_impl = new CountToolPrivate(m_settings);
182 // run CountTool, return success/fail