Class: BioVcf::VcfHeader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(debug = false) ⇒ VcfHeader

Returns a new instance of VcfHeader.



37
38
39
40
41
42
# File 'lib/bio-vcf/vcfheader.rb', line 37

def initialize(debug = false)
  @debug = debug
  @lines = []
  @field = {}
  @meta = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



182
183
184
185
186
187
# File 'lib/bio-vcf/vcfheader.rb', line 182

def method_missing(m, *args, &block)
  name = m.to_s
  value = find_field(name)
  return value if value
  raise "Unknown VCF header query '#{name}'"
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



35
36
37
# File 'lib/bio-vcf/vcfheader.rb', line 35

def field
  @field
end

#linesObject (readonly)

Returns the value of attribute lines.



35
36
37
# File 'lib/bio-vcf/vcfheader.rb', line 35

def lines
  @lines
end

Instance Method Details

#add(line) ⇒ Object

Add a new field to the header



45
46
47
# File 'lib/bio-vcf/vcfheader.rb', line 45

def add line
  @lines += line.split(/\n/)
end

#column_namesObject



63
64
65
# File 'lib/bio-vcf/vcfheader.rb', line 63

def column_names
  @column_names ||= VcfHeaderParser::get_column_names(@lines)
end

#columnsObject



67
68
69
# File 'lib/bio-vcf/vcfheader.rb', line 67

def columns
  @column ||= column_names.size
end

#contigObject



147
148
149
# File 'lib/bio-vcf/vcfheader.rb', line 147

def contig
  find_fields('contig')
end

#filterObject



143
144
145
# File 'lib/bio-vcf/vcfheader.rb', line 143

def filter
  find_fields('FILTER')
end

#find_field(name) ⇒ Object

Look for a line in the header with the field name and return the value, otherwise return nil



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/bio-vcf/vcfheader.rb', line 107

def find_field name
  return field[name] if field[name]
  @lines.each do | line |
    value = line.scan(/###{name}=(.*)/)
    if value[0]
      v = value[0][0]
      field[name] = v
      return v
    end
  end
  nil
end

#find_fields(name) ⇒ Object

Look for all the lines that match the field name and return a hash of hashes. An empty hash is returned when there are no matches.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/bio-vcf/vcfheader.rb', line 123

def find_fields name
  res = {}
  @lines.each do | line |
    value = line.scan(/###{name}=<(.*)>/)
    if value[0]
      str = value[0][0]
      # p str
      v = VcfHeaderParser.parse_field(line,@debug)
      id = v['ID']
      res[id] = v
    end
  end
  # p res
  res
end

#formatObject



139
140
141
# File 'lib/bio-vcf/vcfheader.rb', line 139

def format 
  find_fields('FORMAT')
end

#gatkcommandlineObject



155
156
157
# File 'lib/bio-vcf/vcfheader.rb', line 155

def gatkcommandline
  find_fields('GATKCommandLine')
end

#infoObject



151
152
153
# File 'lib/bio-vcf/vcfheader.rb', line 151

def info
  find_fields('INFO')
end

#metaObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/bio-vcf/vcfheader.rb', line 159

def meta
  return @meta if @meta
  res = { 'INFO' => {}, 'FORMAT' => {}, 'FILTER' => {}, 'contig' => {}, 'GATKCommandLine' => {} }
  @lines.each do | line |
    value = line.scan(/##(.*?)=(.*)/)
    if value[0]
      k,v = value[0]
      if k != 'FORMAT' and k != 'INFO' and k != 'FILTER' and k != 'contig' and k != 'GATKCommandLine'
        # p [k,v]
        res[k] = v
      end
    end
  end
  res['INFO'] = info()
  res['FORMAT'] = format()
  res['FILTER'] = filter()
  res['contig'] = contig()
  res['GATKCommandLine'] = gatkcommandline()
  # p [:res, res]
  @meta = res # cache values
  res
end

#num_samplesObject



93
94
95
# File 'lib/bio-vcf/vcfheader.rb', line 93

def num_samples
  @num_samples ||= ( samples == nil ? 0 : samples.size )
end

#printable_header_line(fields) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/bio-vcf/vcfheader.rb', line 71

def printable_header_line(fields)
  fields.map { | field |
    if field == '#samples'
      samples
    else
      field
    end
  }.join("\t")
end

#sample_indexObject



97
98
99
100
101
102
103
# File 'lib/bio-vcf/vcfheader.rb', line 97

def sample_index
  return @sample_index if @sample_index
  index = {}
  samples.each_with_index { |k,i| index[k] = i+9 ; index[k.downcase] = i+9 }
  @sample_index = index
  index
end

#samplesObject



81
82
83
84
85
86
87
# File 'lib/bio-vcf/vcfheader.rb', line 81

def samples
  @samples ||= if column_names.size > 8
                 column_names[9..-1]
               else
                 []
               end
end

#samples_index_arrayObject



89
90
91
# File 'lib/bio-vcf/vcfheader.rb', line 89

def samples_index_array
  @all_samples_index ||= column_names[9..-1].fill{|i| i}
end

#tag(h) ⇒ Object

Push a special key value list to the header



50
51
52
53
54
55
56
57
# File 'lib/bio-vcf/vcfheader.rb', line 50

def tag h
  h2 = h.dup
  [:show_help,:skip_header,:verbose,:quiet,:debug].each { |key| h2.delete(key) }
  info = h2.map { |k,v| k.to_s.capitalize+'='+'"'+v.to_s+'"' }.join(',')
  line = '##BioVcf=<'+info+'>'
  @lines.insert(-2,line)
  line
end

#versionObject



59
60
61
# File 'lib/bio-vcf/vcfheader.rb', line 59

def version
  @version ||= lines[0].scan(/##fileformat=VCFv(\d+\.\d+)/)[0][0]
end