]> git.donarmstrong.com Git - bamtools.git/blob - bamtools.cpp
Implemented Mosaik-style command line parser, instead of former GetOpt parser. Setup...
[bamtools.git] / 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: 1 June 2010
7 // ---------------------------------------------------------------------------
8 // Integrates a number of BamTools functionalities into a single executable.
9 // ***************************************************************************
10
11 // Std C/C++ includes
12 #include <iostream>
13
14 // BamTools includes
15 #include "bamtools_count.h"
16 #include "bamtools_coverage.h"
17 #include "bamtools_header.h"
18 #include "bamtools_index.h"
19 #include "bamtools_merge.h"
20 #include "bamtools_sam.h"
21 #include "bamtools_sort.h"
22 #include "bamtools_stats.h"
23
24 using namespace std;
25 using namespace BamTools;
26
27 // ------------------------------------------
28 // bamtools subtool names
29 static const string COUNT    = "count";
30 static const string COVERAGE = "coverage";
31 static const string HEADER   = "header";
32 static const string INDEX    = "index";
33 static const string MERGE    = "merge";
34 static const string SAM      = "sam";
35 static const string SORT     = "sort";
36 static const string STATS    = "stats";
37
38 // ------------------------------------------
39 // bamtools help/version names
40 static const string HELP       = "help";
41 static const string LONG_HELP  = "--help";
42 static const string SHORT_HELP = "-h";
43
44 static const string VERSION       = "version";
45 static const string LONG_VERSION  = "--version";
46 static const string SHORT_VERSION = "-v";
47
48 // ------------------------------------------
49 // Print help info
50 int Help(int argc, char* argv[]) {
51   
52     // 'bamtools help COMMAND'
53     AbstractTool* tool(0);
54     if (argc > 2) {
55         if ( argv[2] == COUNT )    tool = new CountTool;
56         if ( argv[2] == COVERAGE ) tool = new CoverageTool;
57         if ( argv[2] == HEADER )   tool = new HeaderTool;
58         if ( argv[2] == INDEX )    tool = new IndexTool;
59         if ( argv[2] == MERGE )    tool = new MergeTool;
60         if ( argv[2] == SAM )      tool = new SamTool;
61         if ( argv[2] == SORT )     tool = new SortTool;
62         if ( argv[2] == STATS )    tool = new StatsTool;
63     }
64     
65     if ( tool ) return tool->Help();
66     else {
67      
68         // either 'bamtools help' or unrecognized argument after 'help'
69         cerr << endl;
70         cerr << "usage: bamtools [--help] COMMAND [ARGS]" << endl;
71         cerr << endl;
72         cerr << "Available bamtools commands:" << endl;
73         cerr << "\tcount     Prints number of alignments in BAM file" << endl;
74         cerr << "\tcoverage  Prints coverage statistics from the input BAM file" << endl;
75         cerr << "\theader    Prints BAM header information" << endl;
76         cerr << "\tindex     Generates index for BAM file" << endl;
77         cerr << "\tmerge     Merge multiple BAM files into single file" << endl;
78         cerr << "\tsam       Prints the BAM file in SAM (text) format" << endl;
79         cerr << "\tsort      Sorts the BAM file according to some criteria" << endl;
80         cerr << "\tstats     Prints some basic statistics from the input BAM file" << endl;
81         cerr << endl;
82         cerr << "See 'bamtools help COMMAND' for more information on a specific command." << endl;
83         cerr << endl;
84         return 0;
85     }
86 }
87
88 // ------------------------------------------
89 // Print version info
90 int Version(void) {
91     cout << endl;
92     cout << "bamtools v0.x.xx" << endl;
93     cout << "Part of BamTools API and toolkit" << endl;
94     cout << "Primary authors: Derek Barnett, Erik Garrison, Michael Stromberg" << endl;
95     cout << "(c) 2009-2010 Marth Lab, Biology Dept., Boston College" << endl;
96     cout << endl;
97     return 0;
98 }
99
100 // ------------------------------------------
101 // toolkit entry point
102 int main(int argc, char* argv[]) {
103
104     // just 'bamtools'
105     if ( (argc == 1) ) return Help(argc, argv);
106     
107     // 'bamtools help', 'bamtools --help', or 'bamtools -h'
108     if ( (argv[1] == HELP) || (argv[1] == LONG_HELP) || (argv[1] == SHORT_HELP) ) return Help(argc, argv); 
109     
110     // 'bamtools version', 'bamtools --version', or 'bamtools -v'
111     if ( (argv[1] == VERSION) || (argv[1] == LONG_VERSION) || (argv[1] == SHORT_VERSION) ) return Version(); 
112         
113     // determine desired sub-tool
114     AbstractTool* tool(0);
115     if ( argv[1] == COUNT )    tool = new CountTool;
116     if ( argv[1] == COVERAGE ) tool = new CoverageTool;
117     if ( argv[1] == HEADER )   tool = new HeaderTool;
118     if ( argv[1] == INDEX )    tool = new IndexTool;
119     if ( argv[1] == MERGE )    tool = new MergeTool;
120     if ( argv[1] == SAM )      tool = new SamTool;
121     if ( argv[1] == SORT )     tool = new SortTool;
122     if ( argv[1] == STATS )    tool = new StatsTool;
123     
124     // if found, run tool
125     if ( tool ) return tool->Run(argc, argv);
126     // no match found, show help
127     else return Help(argc, argv); 
128 }