Class: BioVcf::VcfHeader
- Inherits:
-
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
43
|
# File 'lib/bio-vcf/vcfheader.rb', line 37
def initialize(debug = false)
@debug = debug
@lines = []
@field = {}
@meta = nil
@cached_filter_index = {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
211
212
213
214
215
216
|
# File 'lib/bio-vcf/vcfheader.rb', line 211
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
#field ⇒ Object
Returns the value of attribute field.
35
36
37
|
# File 'lib/bio-vcf/vcfheader.rb', line 35
def field
@field
end
|
#lines ⇒ Object
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
46
47
48
|
# File 'lib/bio-vcf/vcfheader.rb', line 46
def add line
@lines += line.split(/\n/)
end
|
#columns ⇒ Object
68
69
70
|
# File 'lib/bio-vcf/vcfheader.rb', line 68
def columns
@column ||= column_names.size
end
|
#contig ⇒ Object
176
177
178
|
# File 'lib/bio-vcf/vcfheader.rb', line 176
def contig
find_fields('contig')
end
|
#filter ⇒ Object
172
173
174
|
# File 'lib/bio-vcf/vcfheader.rb', line 172
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
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/bio-vcf/vcfheader.rb', line 136
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.
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/bio-vcf/vcfheader.rb', line 152
def find_fields name
res = {}
@lines.each do | line |
value = line.scan(/###{name}=<(.*)>/)
if value[0]
str = value[0][0]
v = VcfHeaderParser.parse_field(line,@debug)
id = v['ID']
res[id] = v
end
end
res
end
|
168
169
170
|
# File 'lib/bio-vcf/vcfheader.rb', line 168
def format
find_fields('FORMAT')
end
|
#gatkcommandline ⇒ Object
184
185
186
|
# File 'lib/bio-vcf/vcfheader.rb', line 184
def gatkcommandline
find_fields('GATKCommandLine')
end
|
#info ⇒ Object
180
181
182
|
# File 'lib/bio-vcf/vcfheader.rb', line 180
def info
find_fields('INFO')
end
|
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/bio-vcf/vcfheader.rb', line 188
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'
res[k] = v
end
end
end
res['INFO'] = info()
res['FORMAT'] = format()
res['FILTER'] = filter()
res['contig'] = contig()
res['GATKCommandLine'] = gatkcommandline()
@meta = res res
end
|
#num_samples ⇒ Object
94
95
96
|
# File 'lib/bio-vcf/vcfheader.rb', line 94
def num_samples
@num_samples ||= ( samples == nil ? 0 : samples.size )
end
|
72
73
74
75
76
77
78
79
80
|
# File 'lib/bio-vcf/vcfheader.rb', line 72
def (fields)
fields.map { | field |
if field == '#samples'
samples
else
field
end
}.join("\t")
end
|
#sample_index ⇒ Object
98
99
100
101
102
103
104
|
# File 'lib/bio-vcf/vcfheader.rb', line 98
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
|
#sample_subset_index(list) ⇒ Object
Give a list of samples (by index and/or name) and return 0-based index values The cache has to be able to hanle multiple lists - that is why it is a hash.
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/bio-vcf/vcfheader.rb', line 108
def sample_subset_index list
cached = @cached_filter_index[list]
if cached
l = cached
else
l = []
list = samples_index_array() if not list
list.each { |i|
value =
begin
Integer(i)
rescue
idx = samples.index(i)
if idx != nil
idx
else
raise "Unknown sample name '#{i}'"
end
end
l << value
}
@cached_filter_index[list] = l
end
l
end
|
#samples ⇒ Object
82
83
84
85
86
87
88
|
# File 'lib/bio-vcf/vcfheader.rb', line 82
def samples
@samples ||= if column_names.size > 8
column_names[9..-1]
else
[]
end
end
|
#samples_index_array ⇒ Object
90
91
92
|
# File 'lib/bio-vcf/vcfheader.rb', line 90
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
51
52
53
54
55
56
57
58
|
# File 'lib/bio-vcf/vcfheader.rb', line 51
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
|
#version ⇒ Object
60
61
62
|
# File 'lib/bio-vcf/vcfheader.rb', line 60
def version
@version ||= lines[0].scan(/##fileformat=VCFv(\d+\.\d+)/)[0][0]
end
|