]> git.donarmstrong.com Git - bamtools.git/commitdiff
Cleaned up help formatting & added some string utilities
authorderek <derekwbarnett@gmail.com>
Sat, 11 Jun 2011 20:55:08 +0000 (16:55 -0400)
committerderek <derekwbarnett@gmail.com>
Sat, 11 Jun 2011 20:55:08 +0000 (16:55 -0400)
src/utils/CMakeLists.txt
src/utils/bamtools_options.cpp
src/utils/bamtools_options.h
src/utils/bamtools_utilities.cpp
src/utils/bamtools_utilities.h

index d1e26ff4a4f0fdd3b4e5c68d484615fedb5fdae1..5bbe6248cadf291fcc7132b5b1c3151e44723bbd 100644 (file)
@@ -24,6 +24,6 @@ target_link_libraries ( BamTools-utils BamTools )
 
 # set BamTools library properties
 set_target_properties( BamTools-utils PROPERTIES
-                       SOVERSION   1.0.0
+                       SOVERSION   1.0.2
                        OUTPUT_NAME bamtools-utils
                      )
index c46d0049c2b244046a58c88d18af48b63a5911d2..157357f66d778201371ddbc748b484f3578ee007 100644 (file)
@@ -3,7 +3,7 @@
 // Marth Lab, Department of Biology, Boston College
 // All rights reserved.
 // ---------------------------------------------------------------------------
-// Last modified: 19 November 2010
+// Last modified: 11 June 2011
 // ---------------------------------------------------------------------------
 // Parses command line arguments and creates a help menu
 // ---------------------------------------------------------------------------
@@ -32,12 +32,15 @@ string Options::m_description;                   // the main description
 string Options::m_exampleArguments;              // the example arguments
 vector<OptionGroup> Options::m_optionGroups;     // stores the option groups
 map<string, OptionValue> Options::m_optionsMap;  // stores the options in a map
-string Options::m_stdin  = "stdin";              // string representation of stdin
-string Options::m_stdout = "stdout";             // string representation of stdout
+const string Options::m_stdin  = "stdin";        // string representation of stdin
+const string Options::m_stdout = "stdout";       // string representation of stdout
 
 // adds a simple option to the parser
-void Options::AddOption(const string& argument, const string& optionDescription, bool& foundArgument, OptionGroup* group) {
-
+void Options::AddOption(const string& argument,
+                        const string& optionDescription,
+                        bool& foundArgument,
+                        OptionGroup* group)
+{
     Option o;
     o.Argument    = argument;
     o.Description = optionDescription;
@@ -110,7 +113,8 @@ void Options::DisplayHelp(void) {
                     const std::string stringValue = optionIter->DefaultValue;
                     sb << stringValue;
                 } else {
-                    printf("ERROR: Found an unsupported data type for argument %s when casting the default value.\n", optionIter->Argument.c_str());
+                    printf("ERROR: Found an unsupported data type for argument %s when casting the default value.\n",
+                           optionIter->Argument.c_str());
                     exit(1);
                 }
 
@@ -231,15 +235,18 @@ void Options::Parse(int argc, char* argv[], int offset) {
                             vector<string>* pVectorValue = (vector<string>*)ovMapIter->second.pValue;
                             pVectorValue->push_back(val);
                         } else {
-                            printf("ERROR: Found an unsupported data type for argument %s when parsing the arguments.\n", argument.c_str());
+                            printf("ERROR: Found an unsupported data type for argument %s when parsing the arguments.\n",
+                                   argument.c_str());
                             exit(1);
                         }
                     } else {
-                        errorBuilder << ERROR_SPACER << "The argument (" << argument << ") expects a value, but none was found." << endl;
+                        errorBuilder << ERROR_SPACER << "The argument (" << argument
+                                     << ") expects a value, but none was found." << endl;
                         foundError = true;
                     }
                 } else {
-                    errorBuilder << ERROR_SPACER << "The argument (" << argument << ") expects a value, but none was found." << endl;
+                    errorBuilder << ERROR_SPACER << "The argument (" << argument
+                                 << ") expects a value, but none was found." << endl;
                     foundError = true;
                 }
             }
@@ -249,7 +256,8 @@ void Options::Parse(int argc, char* argv[], int offset) {
     // check if we missed any required parameters
     for (ovMapIter = m_optionsMap.begin(); ovMapIter != m_optionsMap.end(); ++ovMapIter) {
         if (ovMapIter->second.IsRequired && !*ovMapIter->second.pFoundArgument) {
-            errorBuilder << ERROR_SPACER << ovMapIter->second.ValueTypeDescription << " was not specified. Please use the " << ovMapIter->first << " parameter." << endl;
+            errorBuilder << ERROR_SPACER << ovMapIter->second.ValueTypeDescription
+                         << " was not specified. Please use the " << ovMapIter->first << " parameter." << endl;
             foundError = true;
         }
     }
@@ -264,7 +272,10 @@ void Options::Parse(int argc, char* argv[], int offset) {
 }
 
 // sets the program info
-void Options::SetProgramInfo(const string& programName, const string& description, const string& arguments) {
+void Options::SetProgramInfo(const string& programName,
+                             const string& description,
+                             const string& arguments)
+{
     m_programName      = programName;
     m_description      = description;
     m_exampleArguments = arguments;
index 7b9073dad6f335b905ec9bf33a09c1fd039b7ed0..98ddc3d36c29e9f784509e4d88c2af194757d2bc 100644 (file)
@@ -3,7 +3,7 @@
 // Marth Lab, Department of Biology, Boston College
 // All rights reserved.
 // ---------------------------------------------------------------------------
-// Last modified: 19 November 2010
+// Last modified: 11 June 2011
 // ---------------------------------------------------------------------------
 // Parses command line arguments and creates a help menu
 // ---------------------------------------------------------------------------
@@ -34,8 +34,8 @@
 namespace BamTools {
 
 #define ARGUMENT_LENGTH       35
-#define DESC_LENGTH_FIRST_ROW 50
-#define DESC_LENGTH           39
+#define DESC_LENGTH_FIRST_ROW 30
+#define DESC_LENGTH           42
 #define MAX_LINE_LENGTH       78
 
 #ifdef WIN32
@@ -130,7 +130,9 @@ class UTILS_EXPORT Options {
         // parses the command line
         static void Parse(int argc, char* argv[], int offset = 0);
         // sets the program info
-        static void SetProgramInfo(const std::string& programName, const std::string& description, const std::string& arguments);
+        static void SetProgramInfo(const std::string& programName,
+                                   const std::string& description,
+                                   const std::string& arguments);
         // returns string representation of stdin
         static const std::string& StandardIn(void);
         // returns string representation of stdout
@@ -149,9 +151,9 @@ class UTILS_EXPORT Options {
         // stores the options in a map
         static std::map<std::string, OptionValue> m_optionsMap;
         // string representation of stdin
-        static std::string m_stdin;
+        static const std::string m_stdin;
         // string representation of stdout
-        static std::string m_stdout;
+        static const std::string m_stdout;
 };
 
 // adds a value option to the parser
index bb65c7b3bbabc23a93235cf8e6c524a316248e69..3b6d46a1063a2add5abdb00cf9ac03ff8405eb95 100644 (file)
@@ -3,7 +3,7 @@
 // Marth Lab, Department of Biology, Boston College
 // All rights reserved.
 // ---------------------------------------------------------------------------
-// Last modified: 26 January 2011
+// Last modified: 9 June 2011
 // ---------------------------------------------------------------------------
 // Provides general utilities used by BamTools sub-tools.
 // ***************************************************************************
@@ -15,8 +15,10 @@ using namespace BamTools;
 
 #include <algorithm>
 #include <cstdlib>
+#include <cstring>
 #include <fstream>
 #include <iostream>
+#include <sstream>
 using namespace std;
 
 namespace BamTools {
@@ -31,6 +33,26 @@ const char REVCOMP_LOOKUP[] = {'T',  0,  'G', 'H',
   
 } // namespace BamTools 
   
+// returns true if 'source' contains 'pattern'
+bool Utilities::Contains(const string& source, const string& pattern) {
+    return ( source.find(pattern) != string::npos );
+}
+
+// returns true if 'source' contains 'c'
+bool Utilities::Contains(const std::string &source, const char c) {
+    return ( source.find(c) != string::npos );
+}
+
+// returns true if 'source' ends with 'pattern'
+bool Utilities::EndsWith(const string& source, const string& pattern) {
+    return ( source.find(pattern) == (source.length() - pattern.length()) );
+}
+
+// returns true if 'source' ends with 'c'
+bool Utilities::EndsWith(const std::string& source, const char c) {
+    return ( source.find(c) == (source.length() - 1) );
+}
+
 // check if a file exists
 bool Utilities::FileExists(const string& filename) {
     ifstream f(filename.c_str(), ifstream::in);
@@ -270,3 +292,41 @@ void Utilities::ReverseComplement(string& sequence) {
     // reverse it
     Reverse(sequence);
 }
+
+vector<string> Utilities::Split(const string& source, const char delim) {
+
+    stringstream ss(source);
+    string field;
+    vector<string> fields;
+
+    while ( getline(ss, field, delim) )
+        fields.push_back(field);
+    return fields;
+}
+
+vector<string> Utilities::Split(const string& source, const string& delims) {
+
+    vector<string> fields;
+
+    char* tok;
+    char cchars [source.size()+1];
+    char* cstr = &cchars[0];
+    strcpy(cstr, source.c_str());
+    tok = strtok(cstr, delims.c_str());
+    while (tok != NULL) {
+        fields.push_back(tok);
+        tok = strtok(NULL, delims.c_str());
+    }
+
+    return fields;
+}
+
+// returns true if 'source' starts with 'pattern'
+bool Utilities::StartsWith(const string& source, const string& pattern) {
+    return ( source.find(pattern) == 0 );
+}
+
+// returns true if 'source' starts with 'c'
+bool Utilities::StartsWith(const std::string &source, const char c) {
+    return ( source.find(c) == 0 );
+}
index 3701690fbc6f6a3499dcd3f744d04b5e8e06024f..3320e33351ceca803fed906d4af9c883ecce2120 100644 (file)
@@ -3,7 +3,7 @@
 // Marth Lab, Department of Biology, Boston College
 // All rights reserved.
 // ---------------------------------------------------------------------------
-// Last modified: 26 January 2011
+// Last modified: 9 June 2011
 // ---------------------------------------------------------------------------
 // Provides general utilities used by BamTools sub-tools.
 // ***************************************************************************
@@ -16,6 +16,7 @@
 #include <cassert>
 #include <stdexcept>
 #include <string>
+#include <vector>
 
 #define BAMTOOLS_ASSERT_UNREACHABLE assert( false )
 #define BAMTOOLS_ASSERT_MESSAGE( condition, message ) if (!( condition )) throw std::runtime_error( message );
@@ -28,6 +29,14 @@ class BamMultiReader;
 class UTILS_EXPORT Utilities {
   
     public: 
+        // returns true if 'source' contains 'pattern' or 'c'
+        static bool Contains(const std::string& source, const std::string& pattern);
+        static bool Contains(const std::string& source, const char c);
+
+        // returns true if 'source' ends with 'pattern' or 'c'
+        static bool EndsWith(const std::string& source, const std::string& pattern);
+        static bool EndsWith(const std::string& source, const char c);
+
         // check if a file exists
         static bool FileExists(const std::string& fname);
         
@@ -44,6 +53,14 @@ class UTILS_EXPORT Utilities {
         // sequence utilities
         static void Reverse(std::string& sequence);
         static void ReverseComplement(std::string& sequence);
+
+        // split string on delimiter character (or string of allowed delimiters)
+        static std::vector<std::string> Split(const std::string& source, const char delim);
+        static std::vector<std::string> Split(const std::string& source, const std::string& delims);
+
+        // returns true if 'source' starts with 'pattern' or 'c'
+        static bool StartsWith(const std::string& source, const std::string& pattern);
+        static bool StartsWith(const std::string &source, const char c);
 };
 
 } // namespace BamTools