Class: Vcf
- Inherits:
-
Object
- Object
- Vcf
- Defined in:
- lib/vcf.rb
Instance Attribute Summary collapse
-
#alt ⇒ Object
Returns the value of attribute alt.
-
#chrom ⇒ Object
Returns the value of attribute chrom.
-
#filter ⇒ Object
Returns the value of attribute filter.
-
#format ⇒ Object
Returns the value of attribute format.
-
#id ⇒ Object
Returns the value of attribute id.
-
#info ⇒ Object
Returns the value of attribute info.
-
#pos ⇒ Object
Returns the value of attribute pos.
-
#qual ⇒ Object
Returns the value of attribute qual.
-
#ref ⇒ Object
Returns the value of attribute ref.
-
#samples ⇒ Object
Returns the value of attribute samples.
Instance Method Summary collapse
-
#initialize(line = nil, sample_names = nil) ⇒ Vcf
constructor
A new instance of Vcf.
- #int_or_raw(x) ⇒ Object
- #parse_line(line, sample_names = nil) ⇒ Object
Constructor Details
#initialize(line = nil, sample_names = nil) ⇒ Vcf
Returns a new instance of Vcf.
5 6 7 8 9 |
# File 'lib/vcf.rb', line 5 def initialize(line=nil, sample_names=nil) @info = {} @samples = {} parse_line(line, sample_names) if line != nil end |
Instance Attribute Details
#alt ⇒ Object
Returns the value of attribute alt.
3 4 5 |
# File 'lib/vcf.rb', line 3 def alt @alt end |
#chrom ⇒ Object
Returns the value of attribute chrom.
3 4 5 |
# File 'lib/vcf.rb', line 3 def chrom @chrom end |
#filter ⇒ Object
Returns the value of attribute filter.
3 4 5 |
# File 'lib/vcf.rb', line 3 def filter @filter end |
#format ⇒ Object
Returns the value of attribute format.
3 4 5 |
# File 'lib/vcf.rb', line 3 def format @format end |
#id ⇒ Object
Returns the value of attribute id.
3 4 5 |
# File 'lib/vcf.rb', line 3 def id @id end |
#info ⇒ Object
Returns the value of attribute info.
3 4 5 |
# File 'lib/vcf.rb', line 3 def info @info end |
#pos ⇒ Object
Returns the value of attribute pos.
3 4 5 |
# File 'lib/vcf.rb', line 3 def pos @pos end |
#qual ⇒ Object
Returns the value of attribute qual.
3 4 5 |
# File 'lib/vcf.rb', line 3 def qual @qual end |
#ref ⇒ Object
Returns the value of attribute ref.
3 4 5 |
# File 'lib/vcf.rb', line 3 def ref @ref end |
#samples ⇒ Object
Returns the value of attribute samples.
3 4 5 |
# File 'lib/vcf.rb', line 3 def samples @samples end |
Instance Method Details
#int_or_raw(x) ⇒ Object
11 12 13 |
# File 'lib/vcf.rb', line 11 def int_or_raw(x) Integer.new(x) rescue x end |
#parse_line(line, sample_names = nil) ⇒ Object
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/vcf.rb', line 15 def parse_line(line, sample_names=nil) return false if line[0,1] == '#' f = line.chomp.split("\t", -1) raise "VCF lines must have at least 8 fields" if f.size < 8 @chrom = f[0] @pos = f[1].to_i @id = f[2] @ref = f[3] @alt = f[4] @qual = int_or_raw(f[5]) @filter = f[6] @info = {} info_vec = f[7].split(";") info_vec.each do |x| keyval = x.split("=", -1) if keyval.size == 2 # If it's key=value @info[keyval[0]] = keyval[1] else # Otherwise, it's just a flag @info[x] = "" end end @samples = {} return true if f.size == 8 # Has just upto info raise "Can't have format with no samples" if f.size == 9 @format = f[8] sample_keys = @format.split(":") num_samples = f.size - 9 # How many fields are past the format if sample_names == nil # Make the sample names just ["1", "2", ... , "num_samples}" sample_names = (1..num_samples).to_a.map{|i| i.to_s} elsif sample_names.size != num_samples raise "Unexpected number of samples (#{num_samples}) based on the provided sample names (#{sample_names.inspect})" end sample_names.each_with_index do |sample_name, sample_index| i = sample_index + 9 # index into columns (f) sample_values = f[i].split(":") raise "Expected number of sample values to be <= number of sample keys in FORMAT column Format=#{@format} but sample=#{f[i]}" if sample_values.size > sample_keys.size @samples[sample_name] = {} sample_keys.each_with_index {|key, value_index| @samples[sample_name][key] = sample_values[value_index] || ""} end return true; end |