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: 25 October 2010
7 // ---------------------------------------------------------------------------
8 // Integrates a number of BamTools functionalities into a single executable.
9 // ***************************************************************************
11 // stringify version information
12 #define BAMTOOLS_VER1_(x) #x
13 #define BAMTOOLS_VER_(x) BAMTOOLS_VER1_(x)
14 #define BAMTOOLS_VERSION BAMTOOLS_VER_(BT_VERSION)
20 #include "bamtools_convert.h"
21 #include "bamtools_count.h"
22 #include "bamtools_coverage.h"
23 #include "bamtools_filter.h"
24 #include "bamtools_header.h"
25 #include "bamtools_index.h"
26 #include "bamtools_merge.h"
27 #include "bamtools_random.h"
28 #include "bamtools_sort.h"
29 #include "bamtools_split.h"
30 #include "bamtools_stats.h"
32 using namespace BamTools;
34 // bamtools subtool names
35 static const string CONVERT = "convert";
36 static const string COUNT = "count";
37 static const string COVERAGE = "coverage";
38 static const string FILTER = "filter";
39 static const string HEADER = "header";
40 static const string INDEX = "index";
41 static const string MERGE = "merge";
42 static const string RANDOM = "random";
43 static const string SORT = "sort";
44 static const string SPLIT = "split";
45 static const string STATS = "stats";
47 // bamtools help/version constants
48 static const string HELP = "help";
49 static const string LONG_HELP = "--help";
50 static const string SHORT_HELP = "-h";
51 static const string VERSION = "version";
52 static const string LONG_VERSION = "--version";
53 static const string SHORT_VERSION = "-v";
55 // determine if string is a help constant
56 static bool IsHelp(char* str) {
57 return ( str == HELP ||
62 // determine if string is a version constant
63 static bool IsVersion(char* str) {
64 return ( str == VERSION ||
65 str == LONG_VERSION ||
66 str == SHORT_VERSION );
69 // subtool factory method
70 AbstractTool* CreateTool(const string& arg) {
72 // determine tool type based on arg
73 if ( arg == CONVERT ) return new ConvertTool;
74 if ( arg == COUNT ) return new CountTool;
75 if ( arg == COVERAGE ) return new CoverageTool;
76 if ( arg == FILTER ) return new FilterTool;
77 if ( arg == HEADER ) return new HeaderTool;
78 if ( arg == INDEX ) return new IndexTool;
79 if ( arg == MERGE ) return new MergeTool;
80 if ( arg == RANDOM ) return new RandomTool;
81 if ( arg == SORT ) return new SortTool;
82 if ( arg == SPLIT ) return new SplitTool;
83 if ( arg == STATS ) return new StatsTool;
90 int Help(int argc, char* argv[]) {
92 // check for 'bamtools help COMMAND' to print tool-specific help message
95 // determine desired sub-tool
96 AbstractTool* tool = CreateTool( argv[2] );
98 // if tool known, print its help screen
99 if ( tool ) return tool->Help();
102 // print general BamTools help message
104 cerr << "usage: bamtools [--help] COMMAND [ARGS]" << endl;
106 cerr << "Available bamtools commands:" << endl;
107 cerr << "\tconvert Converts between BAM and a number of other formats" << endl;
108 cerr << "\tcount Prints number of alignments in BAM file(s)" << endl;
109 cerr << "\tcoverage Prints coverage statistics from the input BAM file" << endl;
110 cerr << "\tfilter Filters BAM file(s) by user-specified criteria" << endl;
111 cerr << "\theader Prints BAM header information" << endl;
112 cerr << "\tindex Generates index for BAM file" << endl;
113 cerr << "\tmerge Merge multiple BAM files into single file" << endl;
114 cerr << "\trandom Select random alignments from existing BAM file(s)" << endl;
115 cerr << "\tsort Sorts the BAM file according to some criteria" << endl;
116 cerr << "\tsplit Splits a BAM file on user-specified property, creating a new BAM output file for each value found" << endl;
117 cerr << "\tstats Prints some basic statistics from input BAM file(s)" << endl;
119 cerr << "See 'bamtools help COMMAND' for more information on a specific command." << endl;
124 // print version info
127 cout << "bamtools " << BAMTOOLS_VERSION << endl;
128 cout << "Part of BamTools API and toolkit" << endl;
129 cout << "Primary authors: Derek Barnett, Erik Garrison, Michael Stromberg" << endl;
130 cout << "(c) 2009-2010 Marth Lab, Biology Dept., Boston College" << endl;
135 // toolkit entry point
136 int main(int argc, char* argv[]) {
139 if ( (argc == 1) ) return Help(argc, argv);
141 // 'bamtools help', 'bamtools --help', or 'bamtools -h'
142 if ( IsHelp(argv[1]) ) return Help(argc, argv);
144 // 'bamtools version', 'bamtools --version', or 'bamtools -v'
145 if ( IsVersion(argv[1]) ) return Version();
147 // determine desired sub-tool
148 AbstractTool* tool = CreateTool( argv[1] );
150 // if found, run tool... otherwise show help
151 if ( tool ) return tool->Run(argc, argv);
152 else return Help(argc, argv);