Class: Sam

Inherits:
Object
  • Object
show all
Defined in:
lib/sam.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line = nil) ⇒ Sam

Returns a new instance of Sam.



5
6
7
8
# File 'lib/sam.rb', line 5

def initialize(line=nil)
  @tags = {}
  parse_line(line) if line != nil
end

Instance Attribute Details

#chromObject

Returns the value of attribute chrom.



3
4
5
# File 'lib/sam.rb', line 3

def chrom
  @chrom
end

#cigarObject

Returns the value of attribute cigar.



3
4
5
# File 'lib/sam.rb', line 3

def cigar
  @cigar
end

#flagObject

Returns the value of attribute flag.



3
4
5
# File 'lib/sam.rb', line 3

def flag
  @flag
end

#insertObject

Returns the value of attribute insert.



3
4
5
# File 'lib/sam.rb', line 3

def insert
  @insert
end

#mapqObject

Returns the value of attribute mapq.



3
4
5
# File 'lib/sam.rb', line 3

def mapq
  @mapq
end

#mchromObject

Returns the value of attribute mchrom.



3
4
5
# File 'lib/sam.rb', line 3

def mchrom
  @mchrom
end

#mposObject

Returns the value of attribute mpos.



3
4
5
# File 'lib/sam.rb', line 3

def mpos
  @mpos
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/sam.rb', line 3

def name
  @name
end

#posObject

Returns the value of attribute pos.



3
4
5
# File 'lib/sam.rb', line 3

def pos
  @pos
end

#qualObject

Returns the value of attribute qual.



3
4
5
# File 'lib/sam.rb', line 3

def qual
  @qual
end

#seqObject

Returns the value of attribute seq.



3
4
5
# File 'lib/sam.rb', line 3

def seq
  @seq
end

#tagsObject

Returns the value of attribute tags.



3
4
5
# File 'lib/sam.rb', line 3

def tags
  @tags
end

Instance Method Details

#parse_line(line) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sam.rb', line 10

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]

  @tags = {}
  i = 11
  while i < f.size
    tag = f[i]
    i += 1
    colon_index = tag.rindex(':') 
    raise line if f.rindex == nil
    key = tag[0, colon_index]
    value = int_or_raw(tag[colon_index + 1, tag.size - colon_index] || "")
    @tags[key] = value
  end

  return true;
end