]> git.donarmstrong.com Git - bamtools.git/blob - bamtools.cpp
Complete prior commit
[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 <fstream>
13 #include <string>
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(string cmdname) {
23     cerr << "usage: " << cmdname << " <command> [options]" << endl
24          << "actions:" << 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;
30 }
31
32
33 void BamMerge(string outputFilename, vector<string> filenames) {
34
35     BamMultiReader reader;
36
37     reader.Open(filenames, false); // opens the files without checking for indexes
38
39     string mergedHeader = reader.GetHeaderText();
40
41     RefVector references = reader.GetReferenceData();
42
43     // open BamWriter
44     BamWriter writer;
45     writer.Open( outputFilename.c_str(), mergedHeader, references);
46
47     BamAlignment bAlignment;
48     while (reader.GetNextAlignment(bAlignment)) {
49         // write to output file
50         writer.SaveAlignment(bAlignment);
51     }
52
53     // close output file
54     writer.Close();
55     // close input files
56     reader.Close();
57
58 }
59
60 void BamCreateIndex(const char* inputFilename) {
61
62         // open our BAM reader
63         BamReader reader;
64         reader.Open(inputFilename);
65
66     // create index file
67     reader.CreateIndex();
68
69         // close our file
70         reader.Close();
71
72 }
73
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;
80         cout << endl;
81 }
82
83 void BamDump(vector<string> files) {
84
85         BamMultiReader reader;
86         reader.Open(files);
87         
88         BamAlignment bAlignment;
89         while (reader.GetNextAlignment(bAlignment)) {
90             PrintAlignment(bAlignment);
91         }
92
93         reader.Close();
94
95 }
96
97 void BamDumpHeader(vector<string> files) {
98
99         BamMultiReader reader;
100         reader.Open(files, false);
101         
102     cout << reader.GetHeaderText() << endl;
103
104         reader.Close();
105
106 }
107
108
109 int main(int argc, char* argv[]) {
110
111         // validate argument count
112         if( argc < 3 ) {
113         usageSummary(argv[0]);
114                 exit(1);
115         }
116
117     string command = argv[1];
118     
119     if (command == "index") {
120
121         BamCreateIndex(argv[2]);
122
123     } else if (command == "merge") {
124
125         vector<string> files;
126         string outputFile = argv[2];
127
128         // check if our output exists, and exit if so
129         ifstream output(outputFile.c_str());
130         if (output.good()) {
131             cerr << "ERROR: output file " << outputFile << " exists, exiting." << endl;
132             exit(1);
133         } else {
134             output.close();
135         }
136
137         for (int i = 3; i<argc; ++i) {
138             files.push_back(argv[i]);
139         }
140         BamMerge(outputFile, files);
141         
142     } else if (command == "dump") {
143         
144         vector<string> files;
145         for (int i = 2; i<argc; ++i) {
146             files.push_back(argv[i]);
147         }
148         BamDump(files);
149
150     } else if (command == "header") {
151
152         vector<string> files;
153         for (int i = 2; i<argc; ++i) {
154             files.push_back(argv[i]);
155         }
156         BamDumpHeader(files);
157
158     }
159
160
161         return 0;
162 }