]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_ruby/lib/maasha/sam.rb
addid missing S_END to sam.rb
[biopieces.git] / code_ruby / lib / maasha / sam.rb
index 272d8fc977f859141f2f9b3100f24013099a4fae..ba9ffb0ed255e476e210a3eb7fbcd33f0a4d494c 100644 (file)
@@ -59,12 +59,13 @@ class Sam < Filesys
     bp = {}
 
     bp[:REC_TYPE] = 'SAM'
-    bp[:Q_ID]   = sam[:QNAME]
-    bp[:STRAND] = sam[:FLAG].revcomp? ? '-' : '+'
-    bp[:S_ID]   = sam[:RNAME]
-    bp[:S_BEG]  = sam[:POS]
-    bp[:MAPQ]   = sam[:MAPQ]
-    bp[:CIGAR]  = sam[:CIGAR]
+    bp[:Q_ID]     = sam[:QNAME]
+    bp[:STRAND]   = sam[:FLAG].revcomp? ? '-' : '+'
+    bp[:S_ID]     = sam[:RNAME]
+    bp[:S_BEG]    = sam[:POS]
+    bp[:S_END]    = sam[:POS] + sam[:SEQ].length - 1
+    bp[:MAPQ]     = sam[:MAPQ]
+    bp[:CIGAR]    = sam[:CIGAR].to_s
 
     unless sam[:RNEXT] == '*'
       bp[:Q_ID2]  = sam[:RNEXT]
@@ -85,11 +86,18 @@ class Sam < Filesys
     bp
   end
 
-  # Create align descriptors according to the KISS format description:
+  # Class method to convert a Biopiece record
+  # into a SAM entry.
+  def self.to_sam(bp)
+    "FISK" # FIXME
+  end
+
+  # Create alignment descriptors according to the KISS
+  # format description:
   # http://code.google.com/p/biopieces/wiki/KissFormat
   def self.align_descriptors(sam)
-    offset     = 0
-    align      = []
+    offset = 0
+    align  = []
 
     # Insertions
     sam[:CIGAR].each do |len, op|
@@ -111,11 +119,12 @@ class Sam < Filesys
       if m =~ /\d+/      # Matches
         offset += m.to_i
       elsif m[0] == '^'  # Deletions
-        m[1 .. -1].each_char do |nt|
-          align << [offset, "#{nt}>-"]
-
-          deletions += 1
-          offset    += 1
+        m.each_char do |nt|
+          unless nt == '^'
+            align << [offset, "#{nt}>-"]
+            deletions += 1
+            offset    += 1
+          end
         end
       else               # Mismatches
         m.each_char do |nt|
@@ -467,58 +476,58 @@ class Sam < Filesys
     # Method to test if template have
     # multiple fragments in sequencing.
     def multi?
-      flag & FLAG_MULTI
+      (flag & FLAG_MULTI) == 0
     end
 
     # Method to test if each fragment
     # properly aligned according to the aligner.
     def aligned?
-      flag & FLAG_ALIGNED 
+      (flag & FLAG_ALIGNED) == 0
     end
     
     # Method to test if the fragment was unmapped.
     def unmapped?
-      flag & FLAG_UNMAPPED
+      (flag & FLAG_UNMAPPED) == 0
     end
 
     # Method to test if the next fragment was unmapped.
     def next_unmapped?
-      flag & FLAG_NEXT_UNMAPPED
+      (flag & FLAG_NEXT_UNMAPPED) == 0
     end
 
     # Method to test if the fragment was reverse complemented.
     def revcomp?
-      flag & FLAG_REVCOMP
+      (flag & FLAG_REVCOMP) == 0
     end
 
     # Method to test if the next fragment was reverse complemented.
     def next_revcomp?
-      flag & FLAG_NEXT_REVCOMP
+      (flag & FLAG_NEXT_REVCOMP) == 0
     end
 
     # Method to test if the fragment was first in the template.
     def first?
-      flag & FLAG_FIRST
+      (flag & FLAG_FIRST) == 0
     end
 
     # Method to test if the fragment was last in the template.
     def last?
-      flag & FLAG_LAST
+      (flag & FLAG_LAST) == 0
     end
 
     # Method to test for secondary alignment.
     def secondary_alignment?
-      flag & FLAG_SECONDARY_ALIGNMENT
+      (flag & FLAG_SECONDARY_ALIGNMENT) == 0
     end
 
     # Method to test for quality fail.
     def quality_fail?
-      flag & FLAG_QUALITY_FAIL
+      (flag & FLAG_QUALITY_FAIL) == 0
     end
 
     # Method to test for PCR or optical duplicates.
     def duplicates?
-      flag & FLAG_DUPLICATES
+      (flag & FLAG_DUPLICATES) == 0
     end
   end
 end