Class: BetterSam
- Inherits:
-
Object
- Object
- BetterSam
- Defined in:
- lib/bettersam.rb
Instance Attribute Summary collapse
-
#chrom ⇒ Object
Returns the value of attribute chrom.
-
#cigar ⇒ Object
Returns the value of attribute cigar.
-
#cigar_list ⇒ Object
readonly
Returns the value of attribute cigar_list.
-
#flag ⇒ Object
Returns the value of attribute flag.
-
#insert ⇒ Object
Returns the value of attribute insert.
-
#length ⇒ Object
Returns the value of attribute length.
-
#mapq ⇒ Object
Returns the value of attribute mapq.
-
#mchrom ⇒ Object
Returns the value of attribute mchrom.
-
#mpos ⇒ Object
Returns the value of attribute mpos.
-
#name ⇒ Object
Returns the value of attribute name.
-
#pos ⇒ Object
Returns the value of attribute pos.
-
#qual ⇒ Object
Returns the value of attribute qual.
-
#seq ⇒ Object
Returns the value of attribute seq.
-
#snp ⇒ Object
Returns the value of attribute snp.
-
#tags ⇒ Object
Returns the value of attribute tags.
Instance Method Summary collapse
-
#both_mapped? ⇒ Boolean
pair convenience methods.
-
#contains_snp?(snp) ⇒ Boolean
snp storing.
- #edit_distance ⇒ Object
- #endpos ⇒ Object
-
#exact_match? ⇒ Boolean
cigar parsing methods.
- #first_in_pair? ⇒ Boolean
- #get_base_at(p) ⇒ Object
-
#initialize(line = nil) ⇒ BetterSam
constructor
A new instance of BetterSam.
- #mark_snp(snp) ⇒ Object
- #mate_reverse_strand? ⇒ Boolean
- #mate_unmapped? ⇒ Boolean
- #pair_opposite_strands? ⇒ Boolean
- #pair_same_strand? ⇒ Boolean
- #parse_cigar ⇒ Object
- #parse_line(line) ⇒ Object
- #pcr_duplicate? ⇒ Boolean
- #primary_aln? ⇒ Boolean
-
#put_snp ⇒ Object
find the location of a snp on the genome.
- #quality_fail? ⇒ Boolean
-
#read_paired? ⇒ Boolean
flag parsing convenience methods.
- #read_properly_paired? ⇒ Boolean
- #read_reverse_strand? ⇒ Boolean
- #read_unmapped? ⇒ Boolean
- #second_in_pair? ⇒ Boolean
-
#transfer_snp(bs) ⇒ Object
load in another bettersam object.
Constructor Details
#initialize(line = nil) ⇒ BetterSam
Returns a new instance of BetterSam.
26 27 28 29 |
# File 'lib/bettersam.rb', line 26 def initialize(line=nil) @tags = {} parse_line(line) unless line.nil? end |
Instance Attribute Details
#chrom ⇒ Object
Returns the value of attribute chrom.
21 22 23 |
# File 'lib/bettersam.rb', line 21 def chrom @chrom end |
#cigar ⇒ Object
Returns the value of attribute cigar.
21 22 23 |
# File 'lib/bettersam.rb', line 21 def cigar @cigar end |
#cigar_list ⇒ Object (readonly)
Returns the value of attribute cigar_list.
24 25 26 |
# File 'lib/bettersam.rb', line 24 def cigar_list @cigar_list end |
#flag ⇒ Object
Returns the value of attribute flag.
21 22 23 |
# File 'lib/bettersam.rb', line 21 def flag @flag end |
#insert ⇒ Object
Returns the value of attribute insert.
22 23 24 |
# File 'lib/bettersam.rb', line 22 def insert @insert end |
#length ⇒ Object
Returns the value of attribute length.
22 23 24 |
# File 'lib/bettersam.rb', line 22 def length @length end |
#mapq ⇒ Object
Returns the value of attribute mapq.
21 22 23 |
# File 'lib/bettersam.rb', line 21 def mapq @mapq end |
#mchrom ⇒ Object
Returns the value of attribute mchrom.
21 22 23 |
# File 'lib/bettersam.rb', line 21 def mchrom @mchrom end |
#mpos ⇒ Object
Returns the value of attribute mpos.
21 22 23 |
# File 'lib/bettersam.rb', line 21 def mpos @mpos end |
#name ⇒ Object
Returns the value of attribute name.
21 22 23 |
# File 'lib/bettersam.rb', line 21 def name @name end |
#pos ⇒ Object
Returns the value of attribute pos.
21 22 23 |
# File 'lib/bettersam.rb', line 21 def pos @pos end |
#qual ⇒ Object
Returns the value of attribute qual.
22 23 24 |
# File 'lib/bettersam.rb', line 22 def qual @qual end |
#seq ⇒ Object
Returns the value of attribute seq.
22 23 24 |
# File 'lib/bettersam.rb', line 22 def seq @seq end |
#snp ⇒ Object
Returns the value of attribute snp.
23 24 25 |
# File 'lib/bettersam.rb', line 23 def snp @snp end |
#tags ⇒ Object
Returns the value of attribute tags.
22 23 24 |
# File 'lib/bettersam.rb', line 22 def @tags end |
Instance Method Details
#both_mapped? ⇒ Boolean
pair convenience methods
119 120 121 |
# File 'lib/bettersam.rb', line 119 def both_mapped? !(self.read_unmapped? && self.mate_unmapped?) end |
#contains_snp?(snp) ⇒ Boolean
snp storing
179 180 181 |
# File 'lib/bettersam.rb', line 179 def contains_snp?(snp) snp >= @pos and snp < self.endpos end |
#edit_distance ⇒ Object
132 133 134 |
# File 'lib/bettersam.rb', line 132 def edit_distance @tags[:NM] end |
#endpos ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/bettersam.rb', line 147 def endpos if !@cigar_list self.parse_cigar end e = @pos @cigar_list.each do |h| a = h.to_a bases = a[0][0] match = a[0][1] if match =~ /[MD]/ e += bases end end return e end |
#exact_match? ⇒ Boolean
cigar parsing methods
143 144 145 |
# File 'lib/bettersam.rb', line 143 def exact_match? @tags[:NM]==0 end |
#first_in_pair? ⇒ Boolean
97 98 99 |
# File 'lib/bettersam.rb', line 97 def first_in_pair? @flag & $flags[7] != 0 end |
#get_base_at(p) ⇒ Object
255 256 257 |
# File 'lib/bettersam.rb', line 255 def get_base_at(p) @seq[p] end |
#mark_snp(snp) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/bettersam.rb', line 183 def mark_snp(snp) if self.contains_snp?(snp) if !@cigar_list self.parse_cigar end p = @pos s = snp @cigar_list.each do |h| if p > s and s >= @pos @snp = s - @pos else a = h.to_a bases = a[0][0] match = a[0][1] if match == "M" p += bases elsif match == "I" s += bases elsif match == "D" s -= bases end end end if p > s and s >= @pos @snp = s - @pos end end @snp end |
#mate_reverse_strand? ⇒ Boolean
93 94 95 |
# File 'lib/bettersam.rb', line 93 def mate_reverse_strand? @flag & $flags[6] != 0 end |
#mate_unmapped? ⇒ Boolean
85 86 87 |
# File 'lib/bettersam.rb', line 85 def mate_unmapped? @flag & $flags[4] != 0 end |
#pair_opposite_strands? ⇒ Boolean
123 124 125 126 |
# File 'lib/bettersam.rb', line 123 def pair_opposite_strands? (!self.read_reverse_strand? && self.mate_reverse_strand?) || (self.read_reverse_strand? && !self.mate_reverse_strand?) end |
#pair_same_strand? ⇒ Boolean
128 129 130 |
# File 'lib/bettersam.rb', line 128 def pair_same_strand? !self.pair_opposite_strands? end |
#parse_cigar ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/bettersam.rb', line 163 def parse_cigar str = @cigar l = str.length @cigar_list = [] while str.length>0 if str =~ /([0-9]+[MIDNSHPX=]+)/ @cigar_list << {$1[0..-2].to_i => $1[-1]} str = str.slice($1.length, l) else puts str end end end |
#parse_line(line) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/bettersam.rb', line 31 def parse_line(line) return false if line[0] == "@" f = line.chomp.split("\t", -1) raise "SAM lines must have at least 11 fields (had #{f.size})" if f.size < 11 # colnames = %w(1:name 2:flag 3:chr 4:pos 5:mapq 6:cigar 7:mchr 8:mpos 9:insrt 10:seq 11:qual) @name = f[0] @flag = int_or_raw(f[1]) @chrom = f[2] @pos = int_or_neg1(f[3]) @mapq = int_or_neg1(f[4]) @cigar = f[5] @mchrom = f[6] @mpos = int_or_neg1(f[7]) @insert = int_or_raw(f[8]) @seq = f[9] @qual = f[10] @length = nil @tags = {} i = 11 while i < f.size tag = f[i] i += 1 a = tag.split(":") raise line if a.length != 3 if a[1]=="i" @tags[a[0].to_sym] = a[2].to_i elsif a[1]=="Z" @tags[a[0].to_sym] = a[2] else @tags[a[0].to_sym] = a[2] end end return true; end |
#pcr_duplicate? ⇒ Boolean
113 114 115 |
# File 'lib/bettersam.rb', line 113 def pcr_duplicate? @flag & $flags[11] != 0 end |
#primary_aln? ⇒ Boolean
105 106 107 |
# File 'lib/bettersam.rb', line 105 def primary_aln? (@flag & $flags[9]) == 0 end |
#put_snp ⇒ Object
find the location of a snp on the genome
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/bettersam.rb', line 222 def put_snp # find the location of a snp on the genome if @snp if !@cigar_list self.parse_cigar end s = @snp p = 0 @cigar_list.each do |h| if p > s return s+@pos else a = h.to_a bases = a[0][0] match = a[0][1] if match=="M" p += bases elsif match=="D" s += bases elsif match=="I" s -= bases end end end if p > s return s+@pos end else puts "need to run mark_snp and transfer_snp first" return nil end return -1 end |
#quality_fail? ⇒ Boolean
109 110 111 |
# File 'lib/bettersam.rb', line 109 def quality_fail? @flag & $flags[10] != 0 end |
#read_paired? ⇒ Boolean
flag parsing convenience methods
73 74 75 |
# File 'lib/bettersam.rb', line 73 def read_paired? @flag & $flags[1] != 0 end |
#read_properly_paired? ⇒ Boolean
77 78 79 |
# File 'lib/bettersam.rb', line 77 def read_properly_paired? @flag & $flags[2] != 0 end |
#read_reverse_strand? ⇒ Boolean
89 90 91 |
# File 'lib/bettersam.rb', line 89 def read_reverse_strand? @flag & $flags[5] != 0 end |
#read_unmapped? ⇒ Boolean
81 82 83 |
# File 'lib/bettersam.rb', line 81 def read_unmapped? @flag & $flags[3] != 0 end |
#second_in_pair? ⇒ Boolean
101 102 103 |
# File 'lib/bettersam.rb', line 101 def second_in_pair? @flag & $flags[8] !=0 end |
#transfer_snp(bs) ⇒ Object
load in another bettersam object
213 214 215 216 217 218 219 220 |
# File 'lib/bettersam.rb', line 213 def transfer_snp(bs) # load in another bettersam object if !self.read_unmapped? and !bs.read_unmapped? if (self.read_reverse_strand? and bs.read_reverse_strand?) or (!self.read_reverse_strand? and !bs.read_reverse_strand?) @snp = bs.snp else end end end |