1 // ***************************************************************************
2 // bamtools.cpp (c) 2010 Erik Garrison
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Integrates a number of BamTools functionalities into a single executable.
7 // ***************************************************************************
13 #include <boost/algorithm/string.hpp>
17 #include "BamReader.h"
18 #include "BamWriter.h"
19 #include "BamMultiReader.h"
20 using namespace BamTools;
23 cerr << "usage: bamtools <command> [options]" << endl
25 << " index <bam file>" << endl
26 << " merge <merged BAM file> [<BAM file> <BAM file> ...]" << endl
27 << " dump [<BAM file> <BAM file> ...]" << endl;
28 //<< " trim <input BAM file> <input BAM index file> <output BAM file> <reference name> <leftBound> <rightBound>" << endl;
32 void BamMerge(string outputFilename, vector<string> filenames) {
34 BamMultiReader reader;
36 reader.Open(filenames);
38 string mergedHeader = reader.GetHeaderText();
40 RefVector references = reader.GetReferenceData();
44 writer.Open( outputFilename.c_str(), mergedHeader, references);
46 BamAlignment bAlignment;
47 while (reader.GetNextAlignment(bAlignment)) {
48 // write to output file
49 writer.SaveAlignment(bAlignment);
59 void BamCreateIndex(const char* inputFilename) {
61 // open our BAM reader
63 reader.Open(inputFilename);
73 // Spit out basic BamAlignment data
74 void PrintAlignment(const BamAlignment& alignment) {
75 cout << "---------------------------------" << endl;
76 cout << "Name: " << alignment.Name << endl;
77 cout << "Aligned to: " << alignment.RefID;
78 cout << ":" << alignment.Position << endl;
82 void BamDump(vector<string> files) {
84 BamMultiReader reader;
87 BamAlignment bAlignment;
88 while (reader.GetNextAlignment(bAlignment)) {
89 PrintAlignment(bAlignment);
97 int main(int argc, char* argv[]) {
99 // validate argument count
105 string command = argv[1];
107 if (command == "index") {
108 BamCreateIndex(argv[2]);
109 } else if (command == "merge") {
110 vector<string> files;
111 string outputFile = argv[2];
112 for (int i = 3; i<argc; ++i) {
113 files.push_back(argv[i]);
115 BamMerge(outputFile, files);
116 } else if (command == "dump") {
117 vector<string> files;
118 for (int i = 2; i<argc; ++i) {
119 files.push_back(argv[i]);