]> git.donarmstrong.com Git - bamtools.git/commitdiff
bamtools executable
authorErik Garrison <erik.garrison@bc.edu>
Fri, 21 May 2010 20:53:26 +0000 (16:53 -0400)
committerErik Garrison <erik.garrison@bc.edu>
Fri, 21 May 2010 20:53:26 +0000 (16:53 -0400)
Merge a number of useful tools into a single executable.

Also in this commit, addition of verification that reference sequences
are identical among readers opened by the BamMultiReader.  Without this
check the behavior of the MultiReader is undefined.

bamtools.cpp [new file with mode: 0644]

diff --git a/bamtools.cpp b/bamtools.cpp
new file mode 100644 (file)
index 0000000..e4d7769
--- /dev/null
@@ -0,0 +1,126 @@
+// ***************************************************************************
+// bamtools.cpp (c) 2010 Erik Garrison
+// Marth Lab, Department of Biology, Boston College
+// All rights reserved.
+// ---------------------------------------------------------------------------
+// Integrates a number of BamTools functionalities into a single executable.
+// ***************************************************************************
+
+// Std C/C++ includes
+#include <cstdlib>
+#include <iostream>
+#include <string>
+#include <boost/algorithm/string.hpp>
+using namespace std;
+
+// BamTools includes
+#include "BamReader.h"
+#include "BamWriter.h"
+#include "BamMultiReader.h"
+using namespace BamTools;
+
+void usageSummary() {
+    cerr << "usage: bamtools <command> [options]" << endl
+         << "actions:" << endl
+         << "    index <bam file>" << endl
+         << "    merge <merged BAM file> [<BAM file> <BAM file> ...]" << endl
+         << "    dump [<BAM file> <BAM file> ...]" << endl;
+         //<< "    trim <input BAM file> <input BAM index file> <output BAM file> <reference name> <leftBound> <rightBound>" << endl;
+}
+
+
+void BamMerge(string outputFilename, vector<string> filenames) {
+
+    BamMultiReader reader;
+
+    reader.Open(filenames);
+
+    string mergedHeader = reader.GetHeaderText();
+
+    RefVector references = reader.GetReferenceData();
+
+    // open BamWriter
+    BamWriter writer;
+    writer.Open( outputFilename.c_str(), mergedHeader, references);
+
+    BamAlignment bAlignment;
+    while (reader.GetNextAlignment(bAlignment)) {
+        // write to output file
+        writer.SaveAlignment(bAlignment);
+    }
+
+    // close output file
+    writer.Close();
+    // close input files
+    reader.Close();
+
+}
+
+void BamCreateIndex(const char* inputFilename) {
+
+       // open our BAM reader
+       BamReader reader;
+       reader.Open(inputFilename);
+
+    // create index file
+    reader.CreateIndex();
+
+       // close our file
+       reader.Close();
+
+}
+
+// Spit out basic BamAlignment data 
+void PrintAlignment(const BamAlignment& alignment) {
+       cout << "---------------------------------" << endl;
+       cout << "Name: "       << alignment.Name << endl;
+       cout << "Aligned to: " << alignment.RefID;
+       cout << ":"            << alignment.Position << endl;
+        cout << endl;
+}
+
+void BamDump(vector<string> files) {
+
+       BamMultiReader reader;
+       reader.Open(files);
+       
+       BamAlignment bAlignment;
+       while (reader.GetNextAlignment(bAlignment)) {
+            PrintAlignment(bAlignment);
+       }
+
+       reader.Close();
+
+}
+
+
+int main(int argc, char* argv[]) {
+
+       // validate argument count
+       if( argc < 2 ) {
+        usageSummary();
+               exit(1);
+       }
+
+    string command = argv[1];
+    
+    if (command == "index") {
+        BamCreateIndex(argv[2]);
+    } else if (command == "merge") {
+        vector<string> files;
+        string outputFile = argv[2];
+        for (int i = 3; i<argc; ++i) {
+            files.push_back(argv[i]);
+        }
+        BamMerge(outputFile, files);
+    } else if (command == "dump") {
+        vector<string> files;
+        for (int i = 2; i<argc; ++i) {
+            files.push_back(argv[i]);
+        }
+        BamDump(files);
+    }
+
+
+       return 0;
+}