]> git.donarmstrong.com Git - bamtools.git/blob - src/toolkit/bamtools.cpp
modified/clarified some help & usage messages
[bamtools.git] / src / toolkit / 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: 12 October 2010
7 // ---------------------------------------------------------------------------
8 // Integrates a number of BamTools functionalities into a single executable.
9 // ***************************************************************************
10
11 #include <iostream>
12 #include "bamtools_convert.h"
13 #include "bamtools_count.h"
14 #include "bamtools_coverage.h"
15 #include "bamtools_filter.h"
16 #include "bamtools_header.h"
17 #include "bamtools_index.h"
18 #include "bamtools_merge.h"
19 #include "bamtools_random.h"
20 #include "bamtools_sort.h"
21 #include "bamtools_split.h"
22 #include "bamtools_stats.h"
23 using namespace std;
24 using namespace BamTools;
25
26 // ------------------------------------------
27 // bamtools subtool names
28
29 static const string CONVERT  = "convert";
30 static const string COUNT    = "count";
31 static const string COVERAGE = "coverage";
32 static const string FILTER   = "filter";
33 static const string HEADER   = "header";
34 static const string INDEX    = "index";
35 static const string MERGE    = "merge";
36 static const string RANDOM   = "random";
37 static const string SORT     = "sort";
38 static const string SPLIT    = "split";
39 static const string STATS    = "stats";
40
41 // ------------------------------------------
42 // bamtools help/version names
43
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 static bool IsHelp(char* str) {
53     return ( str == HELP ||
54              str == LONG_HELP ||
55              str == SHORT_HELP );
56 }
57
58 static bool IsVersion(char* str) {
59     return ( str == VERSION ||
60              str == LONG_VERSION ||
61              str == SHORT_VERSION );
62 }
63
64 // ------------------------------------------
65 // Subtool factory method
66 AbstractTool* CreateTool(const string& arg) {
67   
68     // determine tool type based on arg
69     if ( arg == CONVERT )  return new ConvertTool;
70     if ( arg == COUNT )    return new CountTool;
71     if ( arg == COVERAGE ) return new CoverageTool;
72     if ( arg == FILTER )   return new FilterTool;
73     if ( arg == HEADER )   return new HeaderTool;
74     if ( arg == INDEX )    return new IndexTool;
75     if ( arg == MERGE )    return new MergeTool;
76     if ( arg == RANDOM )   return new RandomTool;
77     if ( arg == SORT )     return new SortTool;
78     if ( arg == SPLIT )    return new SplitTool;
79     if ( arg == STATS )    return new StatsTool;
80
81     // unknown arg
82     return 0;
83 }
84
85 // ------------------------------------------
86 // Print help info
87 int Help(int argc, char* argv[]) {
88   
89     // check for 'bamtools help COMMAND' to print tool-specific help message
90     if (argc > 2) {
91         
92         // determine desired sub-tool
93         AbstractTool* tool = CreateTool( argv[2] );
94
95         // if tool known, print its help screen
96         if ( tool ) return tool->Help();
97     }
98
99     // print general BamTools help message
100     cerr << endl;
101     cerr << "usage: bamtools [--help] COMMAND [ARGS]" << endl;
102     cerr << endl;
103     cerr << "Available bamtools commands:" << endl;
104     cerr << "\tconvert         Converts between BAM and a number of other formats" << endl;
105     cerr << "\tcount           Prints number of alignments in BAM file(s)" << endl;
106     cerr << "\tcoverage        Prints coverage statistics from the input BAM file" << endl;    
107     cerr << "\tfilter          Filters BAM file(s) by user-specified criteria" << endl;
108     cerr << "\theader          Prints BAM header information" << endl;
109     cerr << "\tindex           Generates index for BAM file" << endl;
110     cerr << "\tmerge           Merge multiple BAM files into single file" << endl;
111     cerr << "\trandom          Select random alignments from existing BAM file(s)" << endl;
112     cerr << "\tsort            Sorts the BAM file according to some criteria" << endl;
113     cerr << "\tsplit           Splits a BAM file on user-specified property, creating a new BAM output file for each value found" << endl;
114     cerr << "\tstats           Prints some basic statistics from input BAM file(s)" << endl;
115     cerr << endl;
116     cerr << "See 'bamtools help COMMAND' for more information on a specific command." << endl;
117     cerr << endl;
118     return 0;
119 }
120
121 // ------------------------------------------
122 // Print version info
123 int Version(void) {
124     cout << endl;
125     cout << "bamtools v0.8.xx" << endl;
126     cout << "Part of BamTools API and toolkit" << endl;
127     cout << "Primary authors: Derek Barnett, Erik Garrison, Michael Stromberg" << endl;
128     cout << "(c) 2009-2010 Marth Lab, Biology Dept., Boston College" << endl;
129     cout << endl;
130     return 0;
131 }
132
133 // ------------------------------------------
134 // toolkit entry point
135 int main(int argc, char* argv[]) {
136
137     // just 'bamtools'
138     if ( (argc == 1) ) return Help(argc, argv);
139     
140     // 'bamtools help', 'bamtools --help', or 'bamtools -h'
141     if ( IsHelp(argv[1]) ) return Help(argc, argv); 
142     
143     // 'bamtools version', 'bamtools --version', or 'bamtools -v'
144     if ( IsVersion(argv[1]) ) return Version(); 
145         
146     // determine desired sub-tool
147     AbstractTool* tool = CreateTool( argv[1] );
148     
149     // if found, run tool... otherwise show help
150     if ( tool ) return tool->Run(argc, argv);
151     else return Help(argc, argv); 
152 }