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: 2 June 2010
7 // ---------------------------------------------------------------------------
8 // Prints alignment count for BAM file
10 // ** Expand to multiple??
12 // ***************************************************************************
19 #include "bamtools_count.h"
20 #include "bamtools_options.h"
21 #include "bamtools_utilities.h"
22 #include "BamReader.h"
23 #include "BamMultiReader.h"
26 using namespace BamTools;
28 // ---------------------------------------------
29 // CountSettings implementation
31 struct CountTool::CountSettings {
38 vector<string> InputFiles;
48 // ---------------------------------------------
49 // CountTool implementation
51 CountTool::CountTool(void)
53 , m_settings(new CountSettings)
55 // set program details
56 Options::SetProgramInfo("bamtools count", "prints alignment counts for a BAM file", "-in <filename> [-region <REGION>]");
59 OptionGroup* IO_Opts = Options::CreateOptionGroup("Input & Output");
60 Options::AddValueOption("-in", "BAM filename", "the input BAM file(s)", "", m_settings->HasInput, m_settings->InputFiles, IO_Opts);
61 //Options::AddValueOption("-index", "BAM index filename", "the BAM index file", "", m_settings->HasBamIndexFilename, m_settings->BamIndexFilename, IO_Opts);
63 OptionGroup* FilterOpts = Options::CreateOptionGroup("Filters");
64 Options::AddValueOption("-region", "REGION", "genomic region. Index file is recommended for better performance, 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);
67 CountTool::~CountTool(void) {
72 int CountTool::Help(void) {
73 Options::DisplayHelp();
77 int CountTool::Run(int argc, char* argv[]) {
79 // parse command line arguments
80 Options::Parse(argc, argv, 1);
82 if ( !m_settings->HasInput )
83 m_settings->InputFiles.push_back(Options::StandardIn());
85 BamMultiReader reader;
86 reader.Open(m_settings->InputFiles, false, true);
89 int alignmentCount(0);
91 // set up error handling
92 ostringstream errorStream("");
93 bool foundError(false);
95 // if no region specified, count entire file
96 if ( !m_settings->HasRegion ) {
98 while ( reader.GetNextAlignmentCore(al) )
102 // more complicated - region specified
106 if ( Utilities::ParseRegionString(m_settings->Region, reader, region) ) {
108 // check if there are index files *.bai/*.bti corresponding to the input files
109 bool hasDefaultIndex = false;
110 bool hasBamtoolsIndex = false;
111 bool hasNoIndex = false;
112 int defaultIndexCount = 0;
113 int bamtoolsIndexCount = 0;
114 for (vector<string>::const_iterator f = m_settings->InputFiles.begin(); f != m_settings->InputFiles.end(); ++f) {
116 if ( Utilities::FileExists(*f + ".bai") ) {
117 hasDefaultIndex = true;
121 if ( Utilities::FileExists(*f + ".bti") ) {
122 hasBamtoolsIndex = true;
123 ++bamtoolsIndexCount;
126 if ( !hasDefaultIndex && !hasBamtoolsIndex ) {
128 cerr << "*WARNING - could not find index file for " << *f
129 << ", parsing whole file(s) to get alignment counts for target region"
130 << " (could be slow)" << endl;
135 // determine if index file types are heterogeneous
136 bool hasDifferentIndexTypes = false;
137 if ( defaultIndexCount > 0 && bamtoolsIndexCount > 0 ) {
138 hasDifferentIndexTypes = true;
139 cerr << "*WARNING - different index file formats found"
140 << ", parsing whole file(s) to get alignment counts for target region"
141 << " (could be slow)" << endl;
144 // if any input file has no index, or if input files use different index formats
145 // can't use BamMultiReader to jump directly (**for now**)
146 if ( hasNoIndex || hasDifferentIndexTypes ) {
148 // read through sequentially, counting all overlapping reads
150 while( reader.GetNextAlignmentCore(al) ) {
151 if ( (al.RefID >= region.LeftRefID) && ( (al.Position + al.Length) >= region.LeftPosition ) &&
152 (al.RefID <= region.RightRefID) && ( al.Position <= region.RightPosition) )
159 // has index file for each input file (and same format)
162 // this is kind of a hack...?
163 BamMultiReader reader;
164 reader.Open(m_settings->InputFiles, true, true, hasDefaultIndex );
166 if ( !reader.SetRegion(region.LeftRefID, region.LeftPosition, region.RightRefID, region.RightPosition) ) {
168 errorStream << "Could not set BamReader region to REGION: " << m_settings->Region << endl;
171 while ( reader.GetNextAlignmentCore(al) )
178 errorStream << "Could not parse REGION: " << m_settings->Region << endl;
179 errorStream << "Be sure REGION is in valid format (see README) and that coordinates are valid for selected references" << endl;
183 // print errors OR results
185 cerr << errorStream.str() << endl;
187 cout << alignmentCount << endl;
191 return (int)foundError;