+// ---------------------------------------------
+// CoverageToolPrivate implementation
+
+struct CoverageTool::CoverageToolPrivate {
+
+ // ctor & dtor
+ public:
+ CoverageToolPrivate(CoverageTool::CoverageSettings* settings);
+ ~CoverageToolPrivate(void);
+
+ // interface
+ public:
+ bool Run(void);
+
+ // data members
+ private:
+ CoverageTool::CoverageSettings* m_settings;
+ ostream m_out;
+ RefVector m_references;
+};
+
+CoverageTool::CoverageToolPrivate::CoverageToolPrivate(CoverageTool::CoverageSettings* settings)
+ : m_settings(settings)
+ , m_out(cout.rdbuf()) // default output to cout
+{ }
+
+CoverageTool::CoverageToolPrivate::~CoverageToolPrivate(void) { }
+
+bool CoverageTool::CoverageToolPrivate::Run(void) {
+
+ // if output filename given
+ ofstream outFile;
+ if ( m_settings->HasOutputFile ) {
+
+ // open output file stream
+ outFile.open(m_settings->OutputFilename.c_str());
+ if ( !outFile ) {
+ cerr << "bamtools coverage ERROR: could not open " << m_settings->OutputFilename
+ << " for output" << endl;
+ return false;
+ }
+
+ // set m_out to file's streambuf
+ m_out.rdbuf(outFile.rdbuf());
+ }
+
+ //open our BAM reader
+ BamReader reader;
+ if ( !reader.Open(m_settings->InputBamFilename) ) {
+ cerr << "bamtools coverage ERROR: could not open input BAM file: " << m_settings->InputBamFilename << endl;
+ return false;
+ }
+
+ // retrieve references
+ m_references = reader.GetReferenceData();
+
+ // set up our output 'visitor'
+ CoverageVisitor* cv = new CoverageVisitor(m_references, &m_out);
+
+ // set up pileup engine with 'visitor'
+ PileupEngine pileup;
+ pileup.AddVisitor(cv);
+
+ // process input data
+ BamAlignment al;
+ while ( reader.GetNextAlignment(al) )
+ pileup.AddAlignment(al);
+
+ // clean up
+ reader.Close();
+ if ( m_settings->HasOutputFile )
+ outFile.close();
+ delete cv;
+ cv = 0;
+
+ // return success
+ return true;
+}
+