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: 18 May 2011 (DB)
7 // ---------------------------------------------------------------------------
8 // Integrates a number of BamTools functionalities into a single executable.
9 // ***************************************************************************
11 #include "bamtools_convert.h"
12 #include "bamtools_count.h"
13 #include "bamtools_coverage.h"
14 #include "bamtools_filter.h"
15 #include "bamtools_header.h"
16 #include "bamtools_index.h"
17 #include "bamtools_merge.h"
18 #include "bamtools_random.h"
19 #include "bamtools_resolve.h"
20 #include "bamtools_revert.h"
21 #include "bamtools_sort.h"
22 #include "bamtools_split.h"
23 #include "bamtools_stats.h"
24 #include "bamtools_version.h"
30 using namespace BamTools;
33 // bamtools subtool names
34 static const string CONVERT = "convert";
35 static const string COUNT = "count";
36 static const string COVERAGE = "coverage";
37 static const string FILTER = "filter";
38 static const string HEADER = "header";
39 static const string INDEX = "index";
40 static const string MERGE = "merge";
41 static const string RANDOM = "random";
42 static const string RESOLVE = "resolve";
43 static const string REVERT = "revert";
44 static const string SORT = "sort";
45 static const string SPLIT = "split";
46 static const string STATS = "stats";
48 // bamtools help/version constants
49 static const string HELP = "help";
50 static const string LONG_HELP = "--help";
51 static const string SHORT_HELP = "-h";
52 static const string VERSION = "version";
53 static const string LONG_VERSION = "--version";
54 static const string SHORT_VERSION = "-v";
56 // determine if string is a help constant
57 static bool IsHelp(char* str) {
58 return ( str == HELP ||
63 // determine if string is a version constant
64 static bool IsVersion(char* str) {
65 return ( str == VERSION ||
66 str == LONG_VERSION ||
67 str == SHORT_VERSION );
70 // subtool factory method
71 AbstractTool* CreateTool(const string& arg) {
73 // determine tool type based on arg
74 if ( arg == CONVERT ) return new ConvertTool;
75 if ( arg == COUNT ) return new CountTool;
76 if ( arg == COVERAGE ) return new CoverageTool;
77 if ( arg == FILTER ) return new FilterTool;
78 if ( arg == HEADER ) return new HeaderTool;
79 if ( arg == INDEX ) return new IndexTool;
80 if ( arg == MERGE ) return new MergeTool;
81 if ( arg == RANDOM ) return new RandomTool;
82 if ( arg == RESOLVE ) return new ResolveTool;
83 if ( arg == REVERT ) return new RevertTool;
84 if ( arg == SORT ) return new SortTool;
85 if ( arg == SPLIT ) return new SplitTool;
86 if ( arg == STATS ) return new StatsTool;
93 int Help(int argc, char* argv[]) {
95 // check for 'bamtools help COMMAND' to print tool-specific help message
98 // determine desired sub-tool
99 AbstractTool* tool = CreateTool( argv[2] );
101 // if tool known, print its help screen
102 if ( tool ) return tool->Help();
105 // print general BamTools help message
107 cerr << "usage: bamtools [--help] COMMAND [ARGS]" << endl;
109 cerr << "Available bamtools commands:" << endl;
110 cerr << "\tconvert Converts between BAM and a number of other formats" << endl;
111 cerr << "\tcount Prints number of alignments in BAM file(s)" << endl;
112 cerr << "\tcoverage Prints coverage statistics from the input BAM file" << endl;
113 cerr << "\tfilter Filters BAM file(s) by user-specified criteria" << endl;
114 cerr << "\theader Prints BAM header information" << endl;
115 cerr << "\tindex Generates index for BAM file" << endl;
116 cerr << "\tmerge Merge multiple BAM files into single file" << endl;
117 cerr << "\trandom Select random alignments from existing BAM file(s), intended more as a testing tool." << endl;
118 cerr << "\tresolve Resolves paired-end reads (marking the IsProperPair flag as needed)" << endl;
119 cerr << "\trevert Removes duplicate marks and restores original base qualities" << endl;
120 cerr << "\tsort Sorts the BAM file according to some criteria" << endl;
121 cerr << "\tsplit Splits a BAM file on user-specified property, creating a new BAM output file for each value found" << endl;
122 cerr << "\tstats Prints some basic statistics from input BAM file(s)" << endl;
124 cerr << "See 'bamtools help COMMAND' for more information on a specific command." << endl;
129 // print version info
132 stringstream versionStream("");
133 versionStream << BAMTOOLS_VERSION_MAJOR << "."
134 << BAMTOOLS_VERSION_MINOR << "."
135 << BAMTOOLS_VERSION_BUILD;
138 cout << "bamtools " << versionStream.str() << endl;
139 cout << "Part of BamTools API and toolkit" << endl;
140 cout << "Primary authors: Derek Barnett, Erik Garrison, Michael Stromberg" << endl;
141 cout << "(c) 2009-2011 Marth Lab, Biology Dept., Boston College" << endl;
146 // toolkit entry point
147 int main(int argc, char* argv[]) {
150 if ( (argc == 1) ) return Help(argc, argv);
152 // 'bamtools help', 'bamtools --help', or 'bamtools -h'
153 if ( IsHelp(argv[1]) ) return Help(argc, argv);
155 // 'bamtools version', 'bamtools --version', or 'bamtools -v'
156 if ( IsVersion(argv[1]) ) return Version();
158 // determine desired sub-tool, run if found
159 AbstractTool* tool = CreateTool( argv[1] );
160 if ( tool ) return tool->Run(argc, argv);
162 // no tool matched, show help
163 return Help(argc, argv);