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
|
# 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
#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
45
46
47
|
# File 'lib/bio-vcf/vcfheader.rb', line 45
def add line
@lines += line.split(/\n/)
end
|
#columns ⇒ Object
67
68
69
|
# File 'lib/bio-vcf/vcfheader.rb', line 67
def columns
@column ||= column_names.size
end
|
#contig ⇒ Object
147
148
149
|
# File 'lib/bio-vcf/vcfheader.rb', line 147
def contig
find_fields('contig')
end
|
#filter ⇒ Object
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]
v = VcfHeaderParser.parse_field(line,@debug)
id = v['ID']
res[id] = v
end
end
res
end
|
139
140
141
|
# File 'lib/bio-vcf/vcfheader.rb', line 139
def format
find_fields('FORMAT')
end
|
#gatkcommandline ⇒ Object
155
156
157
|
# File 'lib/bio-vcf/vcfheader.rb', line 155
def gatkcommandline
find_fields('GATKCommandLine')
end
|
#info ⇒ Object
151
152
153
|
# File 'lib/bio-vcf/vcfheader.rb', line 151
def info
find_fields('INFO')
end
|
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'
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
93
94
95
|
# File 'lib/bio-vcf/vcfheader.rb', line 93
def num_samples
@num_samples ||= ( samples == nil ? 0 : samples.size )
end
|
71
72
73
74
75
76
77
78
79
|
# File 'lib/bio-vcf/vcfheader.rb', line 71
def (fields)
fields.map { | field |
if field == '#samples'
samples
else
field
end
}.join("\t")
end
|
#sample_index ⇒ Object
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
|
#samples ⇒ Object
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_array ⇒ Object
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
|
#version ⇒ Object
59
60
61
|
# File 'lib/bio-vcf/vcfheader.rb', line 59
def version
@version ||= lines[0].scan(/##fileformat=VCFv(\d+\.\d+)/)[0][0]
end
|