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