]> git.donarmstrong.com Git - bamtools.git/blob - bamtools.cpp
e4d7769abf5100e56e810d84ec300ddc06fe5c97
[bamtools.git] / bamtools.cpp
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 // ***************************************************************************
8
9 // Std C/C++ includes
10 #include <cstdlib>
11 #include <iostream>
12 #include <string>
13 #include <boost/algorithm/string.hpp>
14 using namespace std;
15
16 // BamTools includes
17 #include "BamReader.h"
18 #include "BamWriter.h"
19 #include "BamMultiReader.h"
20 using namespace BamTools;
21
22 void usageSummary() {
23     cerr << "usage: bamtools <command> [options]" << endl
24          << "actions:" << 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;
29 }
30
31
32 void BamMerge(string outputFilename, vector<string> filenames) {
33
34     BamMultiReader reader;
35
36     reader.Open(filenames);
37
38     string mergedHeader = reader.GetHeaderText();
39
40     RefVector references = reader.GetReferenceData();
41
42     // open BamWriter
43     BamWriter writer;
44     writer.Open( outputFilename.c_str(), mergedHeader, references);
45
46     BamAlignment bAlignment;
47     while (reader.GetNextAlignment(bAlignment)) {
48         // write to output file
49         writer.SaveAlignment(bAlignment);
50     }
51
52     // close output file
53     writer.Close();
54     // close input files
55     reader.Close();
56
57 }
58
59 void BamCreateIndex(const char* inputFilename) {
60
61         // open our BAM reader
62         BamReader reader;
63         reader.Open(inputFilename);
64
65     // create index file
66     reader.CreateIndex();
67
68         // close our file
69         reader.Close();
70
71 }
72
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;
79         cout << endl;
80 }
81
82 void BamDump(vector<string> files) {
83
84         BamMultiReader reader;
85         reader.Open(files);
86         
87         BamAlignment bAlignment;
88         while (reader.GetNextAlignment(bAlignment)) {
89             PrintAlignment(bAlignment);
90         }
91
92         reader.Close();
93
94 }
95
96
97 int main(int argc, char* argv[]) {
98
99         // validate argument count
100         if( argc < 2 ) {
101         usageSummary();
102                 exit(1);
103         }
104
105     string command = argv[1];
106     
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]);
114         }
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]);
120         }
121         BamDump(files);
122     }
123
124
125         return 0;
126 }