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 // ***************************************************************************
17 #include "BamReader.h"
18 #include "BamWriter.h"
19 #include "BamMultiReader.h"
20 using namespace BamTools;
22 void usageSummary(string cmdname) {
23 cerr << "usage: " << cmdname << " <command> [options]" << endl
25 << " index <BAM file> # generates BAM index <BAM file>.bai" << endl
26 << " merge <merged BAM file> [<BAM file> <BAM file> ...] # merges BAM files into a single file" << endl
27 << " dump [<BAM file> <BAM file> ...] # dumps alignment summaries to stdout" << endl
28 << " header [<BAM file> <BAM file> ...] # prints header, or unified header for BAM file or files" << endl;
29 //<< " trim <input BAM file> <input BAM index file> <output BAM file> <reference name> <leftBound> <rightBound>" << endl;
33 void BamMerge(string outputFilename, vector<string> filenames) {
35 BamMultiReader reader;
37 reader.Open(filenames, false); // opens the files without checking for indexes
39 string mergedHeader = reader.GetHeaderText();
41 RefVector references = reader.GetReferenceData();
45 writer.Open( outputFilename.c_str(), mergedHeader, references);
47 BamAlignment bAlignment;
48 while (reader.GetNextAlignment(bAlignment)) {
49 // write to output file
50 writer.SaveAlignment(bAlignment);
60 void BamCreateIndex(const char* inputFilename) {
62 // open our BAM reader
64 reader.Open(inputFilename);
74 // Spit out basic BamAlignment data
75 void PrintAlignment(const BamAlignment& alignment) {
76 cout << "---------------------------------" << endl;
77 cout << "Name: " << alignment.Name << endl;
78 cout << "Aligned to: " << alignment.RefID;
79 cout << ":" << alignment.Position << endl;
83 void BamDump(vector<string> files) {
85 BamMultiReader reader;
88 BamAlignment bAlignment;
89 while (reader.GetNextAlignment(bAlignment)) {
90 PrintAlignment(bAlignment);
97 void BamDumpHeader(vector<string> files) {
99 BamMultiReader reader;
100 reader.Open(files, false);
102 cout << reader.GetHeaderText() << endl;
109 int main(int argc, char* argv[]) {
111 // validate argument count
113 usageSummary(argv[0]);
117 string command = argv[1];
119 if (command == "index") {
121 BamCreateIndex(argv[2]);
123 } else if (command == "merge") {
125 vector<string> files;
126 string outputFile = argv[2];
128 // check if our output exists, and exit if so
129 ifstream output(outputFile.c_str());
131 cerr << "ERROR: output file " << outputFile << " exists, exiting." << endl;
137 for (int i = 3; i<argc; ++i) {
138 files.push_back(argv[i]);
140 BamMerge(outputFile, files);
142 } else if (command == "dump") {
144 vector<string> files;
145 for (int i = 2; i<argc; ++i) {
146 files.push_back(argv[i]);
150 } else if (command == "header") {
152 vector<string> files;
153 for (int i = 2; i<argc; ++i) {
154 files.push_back(argv[i]);
156 BamDumpHeader(files);