]> git.donarmstrong.com Git - bamtools.git/blobdiff - src/api/BamAux.h
Missed an update to BamToolsIndex code in a previous commit
[bamtools.git] / src / api / BamAux.h
index 73e88381b695751473eba93098f4f3cdea5192e5..a122b24496e74c4ffa581b646983818989545791 100644 (file)
@@ -3,7 +3,7 @@
 // Marth Lab, Department of Biology, Boston College\r
 // All rights reserved.\r
 // ---------------------------------------------------------------------------\r
-// Last modified: 27 July 2010 (DB)\r
+// Last modified: 15 September 2010 (DB)\r
 // ---------------------------------------------------------------------------\r
 // Provides the basic constants, data structures, etc. for using BAM files\r
 // ***************************************************************************\r
@@ -19,6 +19,8 @@
 \r
 // C++ includes\r
 #include <exception>\r
+#include <fstream>\r
+#include <iostream>\r
 #include <map>\r
 #include <string>\r
 #include <utility>\r
@@ -143,7 +145,9 @@ struct BamAlignment {
 \r
     // Additional data access methods\r
     public:\r
-       int GetEndPosition(bool usePadded = false) const;       // calculates alignment end position, based on starting position and CIGAR operations\r
+        // calculates alignment end position, based on starting position and CIGAR operations\r
+        // @zeroBased - if true, returns 0-based coordinate; else returns 1-based\r
+       int GetEndPosition(bool usePadded = false, bool zeroBased = true) const;  \r
 \r
     // 'internal' utility methods \r
     private:\r
@@ -268,6 +272,12 @@ struct BamRegion {
         , RightRefID(rightID)\r
         , RightPosition(rightPos)\r
     { }\r
+    \r
+    // member functions\r
+    void clear(void) { LeftRefID = -1; LeftPosition = -1; RightRefID = -1; RightPosition = -1; }\r
+    bool isLeftBoundSpecified(void) const { return ( LeftRefID != -1 && LeftPosition != -1 ); }\r
+    bool isNull(void) const { return ( !isLeftBoundSpecified() && !isRightBoundSpecified() ); }\r
+    bool isRightBoundSpecified(void) const { return ( RightRefID != -1 && RightPosition != -1 ); }\r
 };\r
 \r
 // ----------------------------------------------------------------\r
@@ -350,6 +360,11 @@ inline void SwapEndian_64p(char* data) {
     SwapEndian_64(value);\r
 }\r
 \r
+inline bool FileExists(const std::string& filename) {\r
+    std::ifstream f(filename.c_str(), std::ifstream::in);\r
+    return !f.fail();\r
+}\r
+\r
 // ----------------------------------------------------------------\r
 // BamAlignment member methods\r
 \r
@@ -405,7 +420,7 @@ inline void BamAlignment::SetIsUnmapped(bool ok)           { if (ok) AlignmentFl
 \r
 // calculates alignment end position, based on starting position and CIGAR operations\r
 inline \r
-int BamAlignment::GetEndPosition(bool usePadded) const {\r
+int BamAlignment::GetEndPosition(bool usePadded, bool zeroBased) const {\r
 \r
     // initialize alignment end to starting position\r
     int alignEnd = Position;\r
@@ -422,7 +437,12 @@ int BamAlignment::GetEndPosition(bool usePadded) const {
             alignEnd += (*cigarIter).Length;\r
         }\r
     }\r
-    return alignEnd;\r
+    \r
+    // adjust for zeroBased, if necessary\r
+    if (zeroBased) \r
+        return alignEnd - 1;\r
+    else \r
+        return alignEnd;\r
 }\r
 \r
 inline\r
@@ -783,12 +803,12 @@ bool BamAlignment::GetTag(const std::string& tag, uint32_t& destination) const {
             case 'f':\r
             case 'Z':\r
             case 'H':\r
-                printf("ERROR: Cannot store tag of type %c in integer destination\n", type);\r
+                fprintf(stderr, "ERROR: Cannot store tag of type %c in integer destination\n", type);\r
                 return false;\r
 \r
             // unknown tag type\r
             default:\r
-                printf("ERROR: Unknown tag storage class encountered: [%c]\n", type);\r
+                fprintf(stderr, "ERROR: Unknown tag storage class encountered: [%c]\n", type);\r
                 return false;\r
         }\r
           \r
@@ -851,12 +871,12 @@ bool BamAlignment::GetTag(const std::string& tag, float& destination) const {
             // unsupported type (var-length strings)\r
             case 'Z':\r
             case 'H':\r
-                printf("ERROR: Cannot store tag of type %c in integer destination\n", type);\r
+                fprintf(stderr, "ERROR: Cannot store tag of type %c in integer destination\n", type);\r
                 return false;\r
 \r
             // unknown tag type\r
             default:\r
-                printf("ERROR: Unknown tag storage class encountered: [%c]\n", type);\r
+                fprintf(stderr, "ERROR: Unknown tag storage class encountered: [%c]\n", type);\r
                 return false;\r
         }\r
           \r
@@ -978,7 +998,7 @@ bool BamAlignment::SkipToNextTag(const char storageType, char* &pTagData, unsign
 \r
         default: \r
             // error case\r
-            printf("ERROR: Unknown tag storage class encountered: [%c]\n", storageType);\r
+            fprintf(stderr, "ERROR: Unknown tag storage class encountered: [%c]\n", storageType);\r
             return false;\r
     }\r
     \r