]> git.donarmstrong.com Git - bamtools.git/blob - bamtools.cpp
json output
[bamtools.git] / bamtools.cpp
1 // ***************************************************************************
2 // bamtools.cpp (c) 2010 Derek Barnett, Erik Garrison
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 2 June 2010
7 // ---------------------------------------------------------------------------
8 // Integrates a number of BamTools functionalities into a single executable.
9 // ***************************************************************************
10
11 // Std C/C++ includes
12 #include <iostream>
13
14 // BamTools includes
15 #include "bamtools_convert.h"
16 #include "bamtools_count.h"
17 #include "bamtools_coverage.h"
18 #include "bamtools_filter.h"
19 #include "bamtools_header.h"
20 #include "bamtools_index.h"
21 #include "bamtools_merge.h"
22 #include "bamtools_sam.h"
23 #include "bamtools_sort.h"
24 #include "bamtools_stats.h"
25
26 using namespace std;
27 using namespace BamTools;
28
29 // ------------------------------------------
30 // bamtools subtool names
31 static const string CONVERT  = "convert";
32 static const string COUNT    = "count";
33 static const string COVERAGE = "coverage";
34 static const string FILTER   = "filter";
35 static const string HEADER   = "header";
36 static const string INDEX    = "index";
37 static const string MERGE    = "merge";
38 static const string SAM      = "sam";
39 static const string SORT     = "sort";
40 static const string STATS    = "stats";
41
42 // ------------------------------------------
43 // bamtools help/version names
44 static const string HELP       = "help";
45 static const string LONG_HELP  = "--help";
46 static const string SHORT_HELP = "-h";
47
48 static const string VERSION       = "version";
49 static const string LONG_VERSION  = "--version";
50 static const string SHORT_VERSION = "-v";
51
52 // ------------------------------------------
53 // Print help info
54 int Help(int argc, char* argv[]) {
55   
56     // 'bamtools help COMMAND'
57     if (argc > 2) {
58         
59         AbstractTool* tool(0);
60         if ( argv[2] == CONVERT )  tool = new ConvertTool;
61         if ( argv[2] == COUNT )    tool = new CountTool;
62         if ( argv[2] == COVERAGE ) tool = new CoverageTool;
63         if ( argv[2] == FILTER )   tool = new FilterTool;
64         if ( argv[2] == HEADER )   tool = new HeaderTool;
65         if ( argv[2] == INDEX )    tool = new IndexTool;
66         if ( argv[2] == MERGE )    tool = new MergeTool;
67         if ( argv[2] == SAM )      tool = new SamTool;
68         if ( argv[2] == SORT )     tool = new SortTool;
69         if ( argv[2] == STATS )    tool = new StatsTool;
70         
71         // if tool known, print its help screen
72         if ( tool ) return tool->Help();
73     }
74
75     // either 'bamtools help' or unrecognized argument after 'help'
76     cerr << endl;
77     cerr << "usage: bamtools [--help] COMMAND [ARGS]" << endl;
78     cerr << endl;
79     cerr << "Available bamtools commands:" << endl;
80     cerr << "\tconvert   Converts between BAM and a number of other formats" << endl;
81     cerr << "\tcount     Prints number of alignments in BAM file" << endl;
82     cerr << "\tcoverage  Prints coverage statistics from the input BAM file" << endl;
83     cerr << "\tfilter    Filters BAM file(s) by user-specified criteria" << endl;
84     cerr << "\theader    Prints BAM header information" << endl;
85     cerr << "\tindex     Generates index for BAM file" << endl;
86     cerr << "\tmerge     Merge multiple BAM files into single file" << endl;
87     cerr << "\tsam       Prints the BAM file in SAM (text) format" << endl;
88     cerr << "\tsort      Sorts the BAM file according to some criteria" << endl;
89     cerr << "\tstats     Prints some basic statistics from the input BAM file" << endl;
90     cerr << endl;
91     cerr << "See 'bamtools help COMMAND' for more information on a specific command." << endl;
92     cerr << endl;
93     return 0;
94 }
95
96 // ------------------------------------------
97 // Print version info
98 int Version(void) {
99     cout << endl;
100     cout << "bamtools v0.8.xx" << endl;
101     cout << "Part of BamTools API and toolkit" << endl;
102     cout << "Primary authors: Derek Barnett, Erik Garrison, Michael Stromberg" << endl;
103     cout << "(c) 2009-2010 Marth Lab, Biology Dept., Boston College" << endl;
104     cout << endl;
105     return 0;
106 }
107
108 // ------------------------------------------
109 // toolkit entry point
110 int main(int argc, char* argv[]) {
111
112     // just 'bamtools'
113     if ( (argc == 1) ) return Help(argc, argv);
114     
115     // 'bamtools help', 'bamtools --help', or 'bamtools -h'
116     if ( (argv[1] == HELP) || (argv[1] == LONG_HELP) || (argv[1] == SHORT_HELP) ) return Help(argc, argv); 
117     
118     // 'bamtools version', 'bamtools --version', or 'bamtools -v'
119     if ( (argv[1] == VERSION) || (argv[1] == LONG_VERSION) || (argv[1] == SHORT_VERSION) ) return Version(); 
120         
121     // determine desired sub-tool
122     AbstractTool* tool(0);
123     if ( argv[1] == CONVERT )  tool = new ConvertTool;
124     if ( argv[1] == COUNT )    tool = new CountTool;
125     if ( argv[1] == COVERAGE ) tool = new CoverageTool;
126     if ( argv[1] == FILTER )   tool = new FilterTool;
127     if ( argv[1] == HEADER )   tool = new HeaderTool;
128     if ( argv[1] == INDEX )    tool = new IndexTool;
129     if ( argv[1] == MERGE )    tool = new MergeTool;
130     if ( argv[1] == SAM )      tool = new SamTool;
131     if ( argv[1] == SORT )     tool = new SortTool;
132     if ( argv[1] == STATS )    tool = new StatsTool;
133     
134     // if found, run tool
135     if ( tool ) return tool->Run(argc, argv);
136     // no match found, show help
137     else return Help(argc, argv); 
138 }