Class: BioVcf::VcfNucleotideCount4

Inherits:
Object
  • Object
show all
Defined in:
lib/bio-vcf/vcfgenotypefield.rb

Overview

Helper class for a list of (variant) values, such as A,G. The [] function does the hard work. You can pass in an index (integer) or nucleotide which translates to an index. (see ./features for examples)

Instance Method Summary collapse

Constructor Details

#initialize(alt, list) ⇒ VcfNucleotideCount4

Returns a new instance of VcfNucleotideCount4.



19
20
21
22
# File 'lib/bio-vcf/vcfgenotypefield.rb', line 19

def initialize alt,list
  @alt = alt
  @list = list.split(/,/).map{|i| i.to_i}
end

Instance Method Details

#[](idx) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bio-vcf/vcfgenotypefield.rb', line 24

def [] idx
  if idx.kind_of?(Integer)
    # return a value
    @list[idx]
  elsif idx.kind_of?(String)
    # return a value
    @list[["A","C","G","T"].index(idx)]
  else idx.kind_of?(Array)
    # return a list of values
    idx.map { |nuc|
      idx2 = ["A","C","G","T"].index(nuc)
      # p [idx,nuc,idx2,@list]
      @list[idx2]
    }
  end
end

#max(list = @alt) ⇒ Object

Return the max value on the nucleotides in the list (typically rec.alt)



46
47
48
49
# File 'lib/bio-vcf/vcfgenotypefield.rb', line 46

def max list = @alt
  values = self[list]
  values.reduce(0){ |memo,v| (v>memo ? v : memo) }
end

#min(list = @alt) ⇒ Object



51
52
53
54
# File 'lib/bio-vcf/vcfgenotypefield.rb', line 51

def min list = @alt
  values = self[list]
  values.reduce(MAXINT){ |memo,v| (v<memo ? v : memo) }
end

#sum(list = @alt) ⇒ Object



56
57
58
59
# File 'lib/bio-vcf/vcfgenotypefield.rb', line 56

def sum list = @alt
  values = self[list]
  values.reduce(0){ |memo,v| v+memo }
end

#to_aryObject



41
42
43
# File 'lib/bio-vcf/vcfgenotypefield.rb', line 41

def to_ary
  @list
end