]> git.donarmstrong.com Git - bamtools.git/blobdiff - src/toolkit/bamtools.cpp
Major update to BamTools version 1.0
[bamtools.git] / src / toolkit / bamtools.cpp
index 39ec8427e4c017230c6b5f526f7b8a8bda822147..4875a9a10bdb3ba1dbfdc9ac50d1f5ccfc0ebe07 100644 (file)
@@ -3,12 +3,11 @@
 // Marth Lab, Department of Biology, Boston College
 // All rights reserved.
 // ---------------------------------------------------------------------------
-// Last modified: 19 September 2010
+// Last modified: 21 March 2011 (DB)
 // ---------------------------------------------------------------------------
 // Integrates a number of BamTools functionalities into a single executable.
 // ***************************************************************************
 
-#include <iostream>
 #include "bamtools_convert.h"
 #include "bamtools_count.h"
 #include "bamtools_coverage.h"
 #include "bamtools_index.h"
 #include "bamtools_merge.h"
 #include "bamtools_random.h"
+#include "bamtools_revert.h"
 #include "bamtools_sort.h"
 #include "bamtools_split.h"
 #include "bamtools_stats.h"
-using namespace std;
+#include "bamtools_version.h"
+#include <cstdio>
+#include <cstdlib>
+#include <iostream>
+#include <sstream>
+#include <string>
 using namespace BamTools;
+using namespace std;
 
-// ------------------------------------------
 // bamtools subtool names
 static const string CONVERT  = "convert";
 static const string COUNT    = "count";
@@ -33,22 +38,34 @@ static const string HEADER   = "header";
 static const string INDEX    = "index";
 static const string MERGE    = "merge";
 static const string RANDOM   = "random";
+static const string REVERT   = "revert";
 static const string SORT     = "sort";
 static const string SPLIT    = "split";
 static const string STATS    = "stats";
 
-// ------------------------------------------
-// bamtools help/version names
-static const string HELP       = "help";
-static const string LONG_HELP  = "--help";
-static const string SHORT_HELP = "-h";
-
+// bamtools help/version constants
+static const string HELP          = "help";
+static const string LONG_HELP     = "--help";
+static const string SHORT_HELP    = "-h";
 static const string VERSION       = "version";
 static const string LONG_VERSION  = "--version";
 static const string SHORT_VERSION = "-v";
 
-// ------------------------------------------
-// Subtool factory method
+// determine if string is a help constant
+static bool IsHelp(char* str) {
+    return ( str == HELP ||
+             str == LONG_HELP ||
+             str == SHORT_HELP );
+}
+
+// determine if string is a version constant
+static bool IsVersion(char* str) {
+    return ( str == VERSION ||
+             str == LONG_VERSION ||
+             str == SHORT_VERSION );
+}
+
+// subtool factory method
 AbstractTool* CreateTool(const string& arg) {
   
     // determine tool type based on arg
@@ -60,6 +77,7 @@ AbstractTool* CreateTool(const string& arg) {
     if ( arg == INDEX )    return new IndexTool;
     if ( arg == MERGE )    return new MergeTool;
     if ( arg == RANDOM )   return new RandomTool;
+    if ( arg == REVERT )   return new RevertTool;
     if ( arg == SORT )     return new SortTool;
     if ( arg == SPLIT )    return new SplitTool;
     if ( arg == STATS )    return new StatsTool;
@@ -68,31 +86,20 @@ AbstractTool* CreateTool(const string& arg) {
     return 0;
 }
 
-// ------------------------------------------
-// Print help info
+// print help info
 int Help(int argc, char* argv[]) {
   
-    // 'bamtools help COMMAND'
+    // check for 'bamtools help COMMAND' to print tool-specific help message
     if (argc > 2) {
         
+       // determine desired sub-tool
         AbstractTool* tool = CreateTool( argv[2] );
-//         if ( argv[2] == CONVERT )  tool = new ConvertTool;
-//         if ( argv[2] == COUNT )    tool = new CountTool;
-//         if ( argv[2] == COVERAGE ) tool = new CoverageTool;
-//         if ( argv[2] == FILTER )   tool = new FilterTool;
-//         if ( argv[2] == HEADER )   tool = new HeaderTool;
-//         if ( argv[2] == INDEX )    tool = new IndexTool;
-//         if ( argv[2] == MERGE )    tool = new MergeTool;
-//         if ( argv[2] == RANDOM )   tool = new RandomTool;
-//         if ( argv[2] == SORT )     tool = new SortTool;
-//         if ( argv[2] == SPLIT )    tool = new SplitTool;
-//         if ( argv[2] == STATS )    tool = new StatsTool;
-        
+
         // if tool known, print its help screen
         if ( tool ) return tool->Help();
     }
 
-    // either 'bamtools help' or unrecognized argument after 'help'
+    // print general BamTools help message
     cerr << endl;
     cerr << "usage: bamtools [--help] COMMAND [ARGS]" << endl;
     cerr << endl;
@@ -105,28 +112,33 @@ int Help(int argc, char* argv[]) {
     cerr << "\tindex           Generates index for BAM file" << endl;
     cerr << "\tmerge           Merge multiple BAM files into single file" << endl;
     cerr << "\trandom          Select random alignments from existing BAM file(s)" << endl;
+    cerr << "\trevert          Removes duplicate marks and restores original base qualities" << endl;
     cerr << "\tsort            Sorts the BAM file according to some criteria" << endl;
     cerr << "\tsplit           Splits a BAM file on user-specified property, creating a new BAM output file for each value found" << endl;
     cerr << "\tstats           Prints some basic statistics from input BAM file(s)" << endl;
     cerr << endl;
     cerr << "See 'bamtools help COMMAND' for more information on a specific command." << endl;
     cerr << endl;
-    return 0;
+    return EXIT_SUCCESS;
 }
 
-// ------------------------------------------
-// Print version info
+// print version info
 int Version(void) {
+
+    stringstream versionStream("");
+    versionStream << BAMTOOLS_VERSION_MAJOR << "."
+                  << BAMTOOLS_VERSION_MINOR << "."
+                  << BAMTOOLS_VERSION_BUILD;
+
     cout << endl;
-    cout << "bamtools v0.8.xx" << endl;
+    cout << "bamtools " << versionStream.str() << endl;
     cout << "Part of BamTools API and toolkit" << endl;
     cout << "Primary authors: Derek Barnett, Erik Garrison, Michael Stromberg" << endl;
-    cout << "(c) 2009-2010 Marth Lab, Biology Dept., Boston College" << endl;
+    cout << "(c) 2009-2011 Marth Lab, Biology Dept., Boston College" << endl;
     cout << endl;
-    return 0;
+    return EXIT_SUCCESS;
 }
 
-// ------------------------------------------
 // toolkit entry point
 int main(int argc, char* argv[]) {
 
@@ -134,27 +146,15 @@ int main(int argc, char* argv[]) {
     if ( (argc == 1) ) return Help(argc, argv);
     
     // 'bamtools help', 'bamtools --help', or 'bamtools -h'
-    if ( (argv[1] == HELP) || (argv[1] == LONG_HELP) || (argv[1] == SHORT_HELP) ) return Help(argc, argv); 
+    if ( IsHelp(argv[1]) ) return Help(argc, argv); 
     
     // 'bamtools version', 'bamtools --version', or 'bamtools -v'
-    if ( (argv[1] == VERSION) || (argv[1] == LONG_VERSION) || (argv[1] == SHORT_VERSION) ) return Version(); 
+    if ( IsVersion(argv[1]) ) return Version(); 
         
-    // determine desired sub-tool
+    // determine desired sub-tool, run if found
     AbstractTool* tool = CreateTool( argv[1] );
-//     if ( argv[1] == CONVERT )  tool = new ConvertTool;
-//     if ( argv[1] == COUNT )    tool = new CountTool;
-//     if ( argv[1] == COVERAGE ) tool = new CoverageTool;
-//     if ( argv[1] == FILTER )   tool = new FilterTool;
-//     if ( argv[1] == HEADER )   tool = new HeaderTool;
-//     if ( argv[1] == INDEX )    tool = new IndexTool;
-//     if ( argv[1] == MERGE )    tool = new MergeTool;
-//     if ( argv[1] == RANDOM )   tool = new RandomTool;
-//     if ( argv[1] == SORT )     tool = new SortTool;
-//     if ( argv[1] == SPLIT )    tool = new SplitTool;
-//     if ( argv[1] == STATS )    tool = new StatsTool;
-    
-    // if found, run tool
     if ( tool ) return tool->Run(argc, argv);
-    // no match found, show help
-    else return Help(argc, argv); 
+
+    // no tool matched, show help
+    return Help(argc, argv);
 }