// Marth Lab, Department of Biology, Boston College
// All rights reserved.
// ---------------------------------------------------------------------------
-// Last modified: 22 July 2010
+// Last modified: 21 March 2011
// ---------------------------------------------------------------------------
// Prints general alignment statistics for BAM file(s).
// ***************************************************************************
+#include "bamtools_stats.h"
+
+#include <api/BamMultiReader.h>
+#include <utils/bamtools_options.h>
+using namespace BamTools;
+
#include <cmath>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <vector>
-
-#include "bamtools_stats.h"
-#include "bamtools_options.h"
-#include "BamMultiReader.h"
using namespace std;
-using namespace BamTools;
// ---------------------------------------------
// StatsSettings implementation
StatsTool::StatsToolPrivate::~StatsToolPrivate(void) { }
-bool StatsTool::StatsToolPrivate::CalculateMedian(vector<int>& data, double& median) { // median is double in case of even data size, need to return average of middle 2 elements
+// median is of type double because in the case of even number of data elements,
+// we need to return the average of middle 2 elements
+bool StatsTool::StatsToolPrivate::CalculateMedian(vector<int>& data, double& median) {
- // check that data exists
+ // skip if data empty
if ( data.empty() ) return false;
-
- size_t dataSize = data.size();
- size_t middleIndex = dataSize / 2;
-
+
+ // find middle element
+ size_t middleIndex = data.size() / 2;
vector<int>::iterator target = data.begin() + middleIndex;
nth_element(data.begin(), target, data.end());
// odd number of elements
- if ( (dataSize % 2) != 0) {
+ if ( (data.size() % 2) != 0) {
median = (double)(*target);
return true;
}
bool StatsTool::StatsToolPrivate::Run() {
- // opens the BAM files without checking for indexes
+ // open the BAM files
BamMultiReader reader;
- if ( !reader.Open(settings->InputFiles, false, true) ) {
- cerr << "Could not open input BAM file(s)... quitting." << endl;
+ if ( !reader.Open(settings->InputFiles) ) {
+ cerr << "bamtools stats ERROR: could not open input BAM file(s)... Aborting." << endl;
reader.Close();
return false;
}
- // plow through file, keeping track of stats
+ // plow through alignments, keeping track of stats
BamAlignment al;
- while ( reader.GetNextAlignmentCore(al) ) {
+ while ( reader.GetNextAlignmentCore(al) )
ProcessAlignment(al);
- }
+ reader.Close();
- // print stats
+ // print stats & exit
PrintStats();
-
- // clean and exit
- reader.Close();
return true;
}
, m_impl(0)
{
// set program details
- Options::SetProgramInfo("bamtools stats", "prints general alignment statistics", "[-in <filename> -in <filename> ... ]");
+ Options::SetProgramInfo("bamtools stats", "prints general alignment statistics", "[-in <filename> -in <filename> ...] [statsOptions]");
// set up options
OptionGroup* IO_Opts = Options::CreateOptionGroup("Input & Output");
// parse command line arguments
Options::Parse(argc, argv, 1);
-
+
// set to default input if none provided
if ( !m_settings->HasInput )
m_settings->InputFiles.push_back(Options::StandardIn());
- // run internal SortTool implementation, return success/fail
+ // run internal StatsTool implementation, return success/fail
m_impl = new StatsToolPrivate(m_settings);
if ( m_impl->Run() ) return 0;