]> git.donarmstrong.com Git - bamtools.git/commitdiff
Added GetSoftClips() method to BamAlignment
authorderek <derekwbarnett@gmail.com>
Thu, 13 Oct 2011 14:03:22 +0000 (10:03 -0400)
committerderek <derekwbarnett@gmail.com>
Thu, 13 Oct 2011 14:03:22 +0000 (10:03 -0400)
CMakeLists.txt
docs/Doxyfile
src/api/BamAlignment.cpp
src/api/BamAlignment.h
src/api/CMakeLists.txt

index 7ee7f83371ba848d2ce89d414b0c8c5dbedccdcb..31bc9806969270a47d412bb8fc70f6ccc68b1691 100644 (file)
@@ -32,7 +32,7 @@ ensure_out_of_source_build ("
 # set BamTools version information
 set (BamTools_VERSION_MAJOR 2)
 set (BamTools_VERSION_MINOR 0)
-set (BamTools_VERSION_BUILD 1)
+set (BamTools_VERSION_BUILD 2)
 
 # set our library and executable destination dirs
 set (EXECUTABLE_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/bin")
index fb500ad54b9e28379e444d89827cb1abc49d797f..da1bf4ed6b70b524546eaefef5e57ddbcbb4b0e4 100644 (file)
@@ -31,7 +31,7 @@ PROJECT_NAME           = BamTools
 # This could be handy for archiving the generated documentation or 
 # if some version control system is used.
 
-PROJECT_NUMBER         = 2.0.1
+PROJECT_NUMBER         = 2.0.2
 
 # The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) 
 # base path where the generated documentation will be put. 
index 78d7d6b22b8801e374d168a4684ce06cca83d86b..013937a99e7bd96fb44e1249d7c7fe4d2a1966ff 100644 (file)
@@ -2,7 +2,7 @@
 // BamAlignment.cpp (c) 2009 Derek Barnett
 // Marth Lab, Department of Biology, Boston College
 // ---------------------------------------------------------------------------
-// Last modified: 10 October 2011 (DB)
+// Last modified: 13 October 2011 (DB)
 // ---------------------------------------------------------------------------
 // Provides the BamAlignment data structure
 // ***************************************************************************
@@ -443,6 +443,94 @@ std::string BamAlignment::GetErrorString(void) const {
     return ErrorString;
 }
 
+/*! \fn bool BamAlignment::GetSoftClips(std::vector<int>& clipSizes, std::vector<int>& readPositions, std::vector<int>& genomePositions, bool usePadded = false) const
+    \brief Identifies if an alignment has a soft clip. If so, identifies the
+           sizes of the soft clips, as well as their positions in the read and reference.
+
+    \param[out] clipSizes       vector of the sizes of each soft clip in the alignment
+    \param[out] readPositions   vector of the 0-based read locations of each soft clip in the alignment.
+                                These positions are basically indexes within the read, not genomic positions.
+    \param[out] genomePositions vector of the 0-based genome locations of each soft clip in the alignment
+    \param[in]  usePadded       inserted bases affect reported position. Default is false, so that
+                                reported position stays 'sync-ed' with reference coordinates.
+
+    \return \c true if any soft clips were found in the alignment
+*/
+bool BamAlignment::GetSoftClips(vector<int>& clipSizes,
+                                vector<int>& readPositions,
+                                vector<int>& genomePositions,
+                                bool usePadded) const
+{
+    // initialize positions & flags
+    int refPosition  = Position;
+    int readPosition = 0;
+    bool softClipFound = false;
+    bool firstCigarOp  = true;
+
+    // iterate over cigar operations
+    vector<CigarOp>::const_iterator cigarIter = CigarData.begin();
+    vector<CigarOp>::const_iterator cigarEnd  = CigarData.end();
+    for ( ; cigarIter != cigarEnd; ++cigarIter) {
+        const CigarOp& op = (*cigarIter);
+
+        switch ( op.Type ) {
+
+            // increase both read & genome positions on CIGAR chars [DMXN=]
+            case Constants::BAM_CIGAR_DEL_CHAR      :
+            case Constants::BAM_CIGAR_MATCH_CHAR    :
+            case Constants::BAM_CIGAR_MISMATCH_CHAR :
+            case Constants::BAM_CIGAR_REFSKIP_CHAR  :
+            case Constants::BAM_CIGAR_SEQMATCH_CHAR :
+                refPosition  += op.Length;
+                readPosition += op.Length;
+                break;
+
+            // increase read position on insertion, genome position only if @usePadded is true
+            case Constants::BAM_CIGAR_INS_CHAR :
+                readPosition += op.Length;
+                if ( usePadded )
+                    refPosition += op.Length;
+                break;
+
+            case Constants::BAM_CIGAR_SOFTCLIP_CHAR :
+
+                softClipFound = true;
+
+                //////////////////////////////////////////////////////////////////////////////
+                // if we are dealing with the *first* CIGAR operation
+                // for this alignment, we increment the read position so that
+                // the read and genome position of the clip are referring to the same base.
+                // For example, in the alignment below, the ref position would be 4, yet
+                //              the read position would be 0. Thus, to "sync" the two,
+                //              we need to increment the read position by the length of the
+                //              soft clip.
+                // Read:  ATCGTTTCGTCCCTGC
+                // Ref:   GGGATTTCGTCCCTGC
+                // Cigar: SSSSMMMMMMMMMMMM
+                //
+                // NOTE: This only needs to be done if the soft clip is the _first_ CIGAR op.
+                //////////////////////////////////////////////////////////////////////////////
+                if ( firstCigarOp )
+                    readPosition += op.Length;
+
+                // track the soft clip's size, read position, and genome position
+                clipSizes.push_back(op.Length);
+                readPositions.push_back(readPosition);
+                genomePositions.push_back(refPosition);
+
+            // any other CIGAR operations have no effect
+            default :
+                break;
+        }
+
+        // clear our "first pass" flag
+        firstCigarOp = false;
+    }
+
+    // return whether any soft clips found
+    return softClipFound;
+}
+
 /*! \fn bool BamAlignment::GetTagType(const std::string& tag, char& type) const
     \brief Retrieves the BAM tag type-code associated with requested tag name.
 
index 9a8e7be0a7351a405317d6c0c281ad01c384a3c0..22535c981a648a0b7f81b7cfc8b19b7b84effbe2 100644 (file)
@@ -2,7 +2,7 @@
 // BamAlignment.h (c) 2009 Derek Barnett
 // Marth Lab, Department of Biology, Boston College
 // ---------------------------------------------------------------------------
-// Last modified: 10 October 2011 (DB)
+// Last modified: 12 October 2011 (DB)
 // ---------------------------------------------------------------------------
 // Provides the BamAlignment data structure
 // ***************************************************************************
@@ -100,6 +100,12 @@ struct API_EXPORT BamAlignment {
         // returns a description of the last error that occurred
         std::string GetErrorString(void) const;
 
+        // retrieves the size, read locations and reference locations of soft-clip operations
+        bool GetSoftClips(std::vector<int>& clipSizes,
+                          std::vector<int>& readPositions,
+                          std::vector<int>& genomePositions,
+                          bool usePadded = false) const;
+
     // public data fields
     public:
         std::string Name;               // read name
index fcd096189343126df52de3ba3bb9165131ccafc5..bee347be36facacee02f29715e221c50a5398e62 100644 (file)
@@ -49,7 +49,7 @@ set( BamToolsAPISources
 
 # create main BamTools API shared library
 add_library( BamTools SHARED ${BamToolsAPISources} )
-set_target_properties( BamTools PROPERTIES SOVERSION "2.0.1" )
+set_target_properties( BamTools PROPERTIES SOVERSION "2.0.2" )
 set_target_properties( BamTools PROPERTIES OUTPUT_NAME "bamtools" )
 
 # create main BamTools API static library