1 // ***************************************************************************
2 // bamtools_coverage.cpp (c) 2010 Derek Barnett, Erik Garrison
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 16 September 2010
7 // ---------------------------------------------------------------------------
8 // Prints coverage data for a single BAM file
9 // ***************************************************************************
15 #include "bamtools_coverage.h"
16 #include "bamtools_options.h"
17 #include "bamtools_pileup_engine.h"
18 #include "BamReader.h"
20 using namespace BamTools;
24 // ---------------------------------------------
25 // CoverageVisitor implementation
27 class CoverageVisitor : public PileupVisitor {
30 CoverageVisitor(const RefVector& references, ostream* out)
32 , m_references(references)
35 ~CoverageVisitor(void) { }
37 // PileupVisitor interface implementation
39 // void Visit(const int& refId, const int& position, const vector<BamAlignment>& alignments) {
40 void Visit(const PileupPosition& pileupData) {
41 // -----------------------------------------
42 // print coverage results ( tab-delimited )
43 // <refName> <0-based pos> <number of alleles>
44 // *m_out << m_references[refId].RefName << "\t" << position << "\t" << alignments.size() << endl;
45 *m_out << m_references[pileupData.RefId].RefName << "\t"
46 << pileupData.Position << "\t"
47 << pileupData.PileupAlignments.size() << endl;
51 RefVector m_references;
55 } // namespace BamTools
57 // ---------------------------------------------
58 // CoverageSettings implementation
60 struct CoverageTool::CoverageSettings {
67 string InputBamFilename;
68 string OutputFilename;
71 CoverageSettings(void)
73 , HasOutputFile(false)
74 , InputBamFilename(Options::StandardIn())
75 , OutputFilename(Options::StandardOut())
79 // ---------------------------------------------
80 // CoverageToolPrivate implementation
82 struct CoverageTool::CoverageToolPrivate {
86 CoverageToolPrivate(CoverageTool::CoverageSettings* settings);
87 ~CoverageToolPrivate(void);
95 CoverageTool::CoverageSettings* m_settings;
97 RefVector m_references;
100 CoverageTool::CoverageToolPrivate::CoverageToolPrivate(CoverageTool::CoverageSettings* settings)
101 : m_settings(settings)
102 , m_out(cout.rdbuf()) // default output to cout
105 CoverageTool::CoverageToolPrivate::~CoverageToolPrivate(void) { }
107 bool CoverageTool::CoverageToolPrivate::Run(void) {
109 // if output filename given
111 if ( m_settings->HasOutputFile ) {
113 // open output file stream
114 outFile.open(m_settings->OutputFilename.c_str());
116 cerr << "Could not open " << m_settings->OutputFilename << " for output." << endl;
120 // set m_out to file's streambuf
121 m_out.rdbuf(outFile.rdbuf());
124 //open our BAM reader
126 if (!reader.Open(m_settings->InputBamFilename)) {
127 cerr << "Could not open " << m_settings->InputBamFilename << " for reading." << endl;
130 m_references = reader.GetReferenceData();
132 // set up our output 'visitor'
133 CoverageVisitor* cv = new CoverageVisitor(m_references, &m_out);
135 // set up pileup engine with 'visitor'
137 pileup.AddVisitor(cv);
139 // process input data
141 while ( reader.GetNextAlignment(al) )
142 pileup.AddAlignment(al);
146 if ( m_settings->HasOutputFile ) outFile.close();
154 // ---------------------------------------------
155 // CoverageTool implementation
157 CoverageTool::CoverageTool(void)
159 , m_settings(new CoverageSettings)
162 // set program details
163 Options::SetProgramInfo("bamtools coverage", "prints coverage data for a single BAM file", "[-in <filename>] [-out <filename>]");
166 OptionGroup* IO_Opts = Options::CreateOptionGroup("Input & Output");
167 Options::AddValueOption("-in", "BAM filename", "the input BAM file", "", m_settings->HasInputFile, m_settings->InputBamFilename, IO_Opts, Options::StandardIn());
168 Options::AddValueOption("-out", "filename", "the output file", "", m_settings->HasOutputFile, m_settings->OutputFilename, IO_Opts, Options::StandardOut());
171 CoverageTool::~CoverageTool(void) {
179 int CoverageTool::Help(void) {
180 Options::DisplayHelp();
184 int CoverageTool::Run(int argc, char* argv[]) {
186 // parse command line arguments
187 Options::Parse(argc, argv, 1);
189 // run internal ConvertTool implementation, return success/fail
190 m_impl = new CoverageToolPrivate(m_settings);