]> git.donarmstrong.com Git - bamtools.git/blob - src/toolkit/bamtools.cpp
Major update to BamTools version 1.0
[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: 21 March 2011 (DB)
7 // ---------------------------------------------------------------------------
8 // Integrates a number of BamTools functionalities into a single executable.
9 // ***************************************************************************
10
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_revert.h"
20 #include "bamtools_sort.h"
21 #include "bamtools_split.h"
22 #include "bamtools_stats.h"
23 #include "bamtools_version.h"
24 #include <cstdio>
25 #include <cstdlib>
26 #include <iostream>
27 #include <sstream>
28 #include <string>
29 using namespace BamTools;
30 using namespace std;
31
32 // bamtools subtool names
33 static const string CONVERT  = "convert";
34 static const string COUNT    = "count";
35 static const string COVERAGE = "coverage";
36 static const string FILTER   = "filter";
37 static const string HEADER   = "header";
38 static const string INDEX    = "index";
39 static const string MERGE    = "merge";
40 static const string RANDOM   = "random";
41 static const string REVERT   = "revert";
42 static const string SORT     = "sort";
43 static const string SPLIT    = "split";
44 static const string STATS    = "stats";
45
46 // bamtools help/version constants
47 static const string HELP          = "help";
48 static const string LONG_HELP     = "--help";
49 static const string SHORT_HELP    = "-h";
50 static const string VERSION       = "version";
51 static const string LONG_VERSION  = "--version";
52 static const string SHORT_VERSION = "-v";
53
54 // determine if string is a help constant
55 static bool IsHelp(char* str) {
56     return ( str == HELP ||
57              str == LONG_HELP ||
58              str == SHORT_HELP );
59 }
60
61 // determine if string is a version constant
62 static bool IsVersion(char* str) {
63     return ( str == VERSION ||
64              str == LONG_VERSION ||
65              str == SHORT_VERSION );
66 }
67
68 // subtool factory method
69 AbstractTool* CreateTool(const string& arg) {
70   
71     // determine tool type based on arg
72     if ( arg == CONVERT )  return new ConvertTool;
73     if ( arg == COUNT )    return new CountTool;
74     if ( arg == COVERAGE ) return new CoverageTool;
75     if ( arg == FILTER )   return new FilterTool;
76     if ( arg == HEADER )   return new HeaderTool;
77     if ( arg == INDEX )    return new IndexTool;
78     if ( arg == MERGE )    return new MergeTool;
79     if ( arg == RANDOM )   return new RandomTool;
80     if ( arg == REVERT )   return new RevertTool;
81     if ( arg == SORT )     return new SortTool;
82     if ( arg == SPLIT )    return new SplitTool;
83     if ( arg == STATS )    return new StatsTool;
84
85     // unknown arg
86     return 0;
87 }
88
89 // print help info
90 int Help(int argc, char* argv[]) {
91   
92     // check for 'bamtools help COMMAND' to print tool-specific help message
93     if (argc > 2) {
94         
95         // determine desired sub-tool
96         AbstractTool* tool = CreateTool( argv[2] );
97
98         // if tool known, print its help screen
99         if ( tool ) return tool->Help();
100     }
101
102     // print general BamTools help message
103     cerr << endl;
104     cerr << "usage: bamtools [--help] COMMAND [ARGS]" << endl;
105     cerr << 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 << "\trevert          Removes duplicate marks and restores original base qualities" << endl;
116     cerr << "\tsort            Sorts the BAM file according to some criteria" << endl;
117     cerr << "\tsplit           Splits a BAM file on user-specified property, creating a new BAM output file for each value found" << endl;
118     cerr << "\tstats           Prints some basic statistics from input BAM file(s)" << endl;
119     cerr << endl;
120     cerr << "See 'bamtools help COMMAND' for more information on a specific command." << endl;
121     cerr << endl;
122     return EXIT_SUCCESS;
123 }
124
125 // print version info
126 int Version(void) {
127
128     stringstream versionStream("");
129     versionStream << BAMTOOLS_VERSION_MAJOR << "."
130                   << BAMTOOLS_VERSION_MINOR << "."
131                   << BAMTOOLS_VERSION_BUILD;
132
133     cout << endl;
134     cout << "bamtools " << versionStream.str() << endl;
135     cout << "Part of BamTools API and toolkit" << endl;
136     cout << "Primary authors: Derek Barnett, Erik Garrison, Michael Stromberg" << endl;
137     cout << "(c) 2009-2011 Marth Lab, Biology Dept., Boston College" << endl;
138     cout << endl;
139     return EXIT_SUCCESS;
140 }
141
142 // toolkit entry point
143 int main(int argc, char* argv[]) {
144
145     // just 'bamtools'
146     if ( (argc == 1) ) return Help(argc, argv);
147     
148     // 'bamtools help', 'bamtools --help', or 'bamtools -h'
149     if ( IsHelp(argv[1]) ) return Help(argc, argv); 
150     
151     // 'bamtools version', 'bamtools --version', or 'bamtools -v'
152     if ( IsVersion(argv[1]) ) return Version(); 
153         
154     // determine desired sub-tool, run if found
155     AbstractTool* tool = CreateTool( argv[1] );
156     if ( tool ) return tool->Run(argc, argv);
157
158     // no tool matched, show help
159     return Help(argc, argv);
160 }