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