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 // prints coverage results ( tab-delimited )
40 void Visit(const PileupPosition& pileupData) {
41 *m_out << m_references[pileupData.RefId].RefName << "\t"
42 << pileupData.Position << "\t"
43 << pileupData.PileupAlignments.size() << endl;
47 RefVector m_references;
51 } // namespace BamTools
53 // ---------------------------------------------
54 // CoverageSettings implementation
56 struct CoverageTool::CoverageSettings {
63 string InputBamFilename;
64 string OutputFilename;
67 CoverageSettings(void)
69 , HasOutputFile(false)
70 , InputBamFilename(Options::StandardIn())
71 , OutputFilename(Options::StandardOut())
75 // ---------------------------------------------
76 // CoverageToolPrivate implementation
78 struct CoverageTool::CoverageToolPrivate {
82 CoverageToolPrivate(CoverageTool::CoverageSettings* settings);
83 ~CoverageToolPrivate(void);
91 CoverageTool::CoverageSettings* m_settings;
93 RefVector m_references;
96 CoverageTool::CoverageToolPrivate::CoverageToolPrivate(CoverageTool::CoverageSettings* settings)
97 : m_settings(settings)
98 , m_out(cout.rdbuf()) // default output to cout
101 CoverageTool::CoverageToolPrivate::~CoverageToolPrivate(void) { }
103 bool CoverageTool::CoverageToolPrivate::Run(void) {
105 // if output filename given
107 if ( m_settings->HasOutputFile ) {
109 // open output file stream
110 outFile.open(m_settings->OutputFilename.c_str());
112 cerr << "Could not open " << m_settings->OutputFilename << " for output." << endl;
116 // set m_out to file's streambuf
117 m_out.rdbuf(outFile.rdbuf());
120 //open our BAM reader
122 if (!reader.Open(m_settings->InputBamFilename)) {
123 cerr << "Could not open " << m_settings->InputBamFilename << " for reading." << endl;
126 m_references = reader.GetReferenceData();
128 // set up our output 'visitor'
129 CoverageVisitor* cv = new CoverageVisitor(m_references, &m_out);
131 // set up pileup engine with 'visitor'
133 pileup.AddVisitor(cv);
135 // process input data
137 while ( reader.GetNextAlignment(al) )
138 pileup.AddAlignment(al);
142 if ( m_settings->HasOutputFile ) outFile.close();
150 // ---------------------------------------------
151 // CoverageTool implementation
153 CoverageTool::CoverageTool(void)
155 , m_settings(new CoverageSettings)
158 // set program details
159 Options::SetProgramInfo("bamtools coverage", "prints coverage data for a single BAM file", "[-in <filename>] [-out <filename>]");
162 OptionGroup* IO_Opts = Options::CreateOptionGroup("Input & Output");
163 Options::AddValueOption("-in", "BAM filename", "the input BAM file", "", m_settings->HasInputFile, m_settings->InputBamFilename, IO_Opts, Options::StandardIn());
164 Options::AddValueOption("-out", "filename", "the output file", "", m_settings->HasOutputFile, m_settings->OutputFilename, IO_Opts, Options::StandardOut());
167 CoverageTool::~CoverageTool(void) {
175 int CoverageTool::Help(void) {
176 Options::DisplayHelp();
180 int CoverageTool::Run(int argc, char* argv[]) {
182 // parse command line arguments
183 Options::Parse(argc, argv, 1);
185 // run internal ConvertTool implementation, return success/fail
186 m_impl = new CoverageToolPrivate(m_settings);