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", "", m_settings->HasInputBamFilename, m_settings->InputBamFilename, IO_Opts, Options::StandardIn());
61 Options::AddValueOption("-in", "BAM filename", "the input BAM file(s)", "", m_settings->HasInput, m_settings->InputFiles, IO_Opts);
62 //Options::AddValueOption("-index", "BAM index filename", "the BAM index file", "", m_settings->HasBamIndexFilename, m_settings->BamIndexFilename, IO_Opts);
64 OptionGroup* FilterOpts = Options::CreateOptionGroup("Filters");
65 Options::AddValueOption("-region", "REGION", "genomic region. Index file is recommended for better performance, and is read automatically if it exists as <filename>.bai. See \'bamtools help index\' for more details on creating one", "", m_settings->HasRegion, m_settings->Region, FilterOpts);
68 CountTool::~CountTool(void) {
73 int CountTool::Help(void) {
74 Options::DisplayHelp();
78 int CountTool::Run(int argc, char* argv[]) {
80 // parse command line arguments
81 Options::Parse(argc, argv, 1);
83 BamMultiReader reader;
84 reader.Open(m_settings->InputFiles, false, true);
87 int alignmentCount(0);
89 // set up error handling
90 ostringstream errorStream("");
91 bool foundError(false);
93 // if no region specified, count entire file
94 if ( !m_settings->HasRegion ) {
96 while ( reader.GetNextAlignmentCore(al) )
100 // more complicated - region specified
104 if ( Utilities::ParseRegionString(m_settings->Region, reader, region) ) {
106 // check if there are index files *.bai corresponding to the input files
107 bool hasIndexes = true;
108 for (vector<string>::const_iterator f = m_settings->InputFiles.begin(); f != m_settings->InputFiles.end(); ++f) {
109 if (!Utilities::FileExists(*f + ".bai")) {
110 errorStream << "could not find index file " << *f + ".bai"
111 << ", parsing whole file(s) to get alignment counts for target region" << endl;
116 // has index, we can jump directly to
119 // this is kind of a hack...?
120 BamMultiReader reader;
121 reader.Open(m_settings->InputFiles, true, true);
123 if ( !reader.SetRegion(region.LeftRefID, region.LeftPosition, region.RightRefID, region.RightPosition) ) {
125 errorStream << "Could not set BamReader region to REGION: " << m_settings->Region << endl;
128 while ( reader.GetNextAlignmentCore(al) )
134 // read through sequentially, until first overlapping read is found
136 bool alignmentFound(false);
137 //reader.Open(m_settings->InputFiles, false, true);
138 while( reader.GetNextAlignmentCore(al) ) {
139 if ( (al.RefID == region.LeftRefID) && ( (al.Position + al.Length) >= region.LeftPosition ) ) {
140 alignmentFound = true;
145 // if overlapping alignment found (not EOF), increment counter
146 if ( alignmentFound ) ++ alignmentCount;
149 // -----------------------------
150 // count alignments until stop hit
154 errorStream << "Could not parse REGION: " << m_settings->Region << endl;
155 errorStream << "Be sure REGION is in valid format (see README) and that coordinates are valid for selected references" << endl;
159 // print errors OR results
161 cerr << errorStream.str() << endl;
163 cout << alignmentCount << endl;
167 return (int)foundError;