Class: BioVcf::VcfRecord

Inherits:
Object
  • Object
show all
Includes:
VcfRecordCall
Defined in:
lib/bio-vcf/vcfrecord.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from VcfRecordCall

#call_diff, #call_normal_count, #call_nuc, #call_tumor_count, #call_tumor_relative_count, #get_gt, #index

Constructor Details

#initialize(fields, header) ⇒ VcfRecord

Returns a new instance of VcfRecord.



129
130
131
132
133
# File 'lib/bio-vcf/vcfrecord.rb', line 129

def initialize fields, header
  @fields = fields
  @header = header
  @sample_by_index = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Return the sample



312
313
314
315
316
317
318
319
320
321
# File 'lib/bio-vcf/vcfrecord.rb', line 312

def method_missing(m, *args, &block)  
  name = m.to_s
  if name =~ /\?$/
    # Query for empty sample name
    @sample_index ||= @header.sample_index
    return !VcfSample::empty?(@fields[@sample_index[name.chop]])
  else
    sample[name]
  end
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



127
128
129
# File 'lib/bio-vcf/vcfrecord.rb', line 127

def header
  @header
end

Instance Method Details

#add_to_filter_field(str) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/bio-vcf/vcfrecord.rb', line 296

def add_to_filter_field str
  filter = @fields[6]
  if not filter or filter == '.' or filter == 'PASS'
    filter = str
  else
    values = filter.split(/;/)
    if not values.include?(str)
      filter = filter +';'+str
    end
  end
  filter = '.' if filter == nil or filter == ''
  @fields[6] = filter
  filter
end

#altObject



157
158
159
# File 'lib/bio-vcf/vcfrecord.rb', line 157

def alt
  @alt ||= @fields[4].split(/,/)
end

#chromObject Also known as: chr



135
136
137
# File 'lib/bio-vcf/vcfrecord.rb', line 135

def chrom
  @fields[0]
end

#each_sample(list = nil) ⇒ Object

Walk the samples. list contains an Array of int (the index)



208
209
210
211
# File 'lib/bio-vcf/vcfrecord.rb', line 208

def each_sample(list = nil)
  list = @header.samples_index_array() if not list 
  list.each { |i| yield VcfSample::Sample.new(self,sample_by_index(i.to_i)) }
end

#eval(expr, ignore_missing_data: true, quiet: false) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/bio-vcf/vcfrecord.rb', line 230

def eval expr, ignore_missing_data: true, quiet: false
  begin
    if not respond_to?(:call_cached_eval)
      code =
      """
      def call_cached_eval(rec,fields)
        r = rec
        #{expr}
      end
      """
      self.class.class_eval(code)
    end
    res = call_cached_eval(self,@fields)
    if res.kind_of?(Array)
      res.join("\t")
    else
      res
    end
  rescue NoMethodError => e
    if not quiet
      $stderr.print "RECORD ERROR!\n"
      $stderr.print [@fields],"\n"
      $stderr.print expr,"\n"
    end
    if ignore_missing_data
      $stderr.print e.message if not quiet
      return false
    else
      raise
    end
  end
end

#filterObject



165
166
167
# File 'lib/bio-vcf/vcfrecord.rb', line 165

def filter
  @filter ||= @fields[6]
end

#firstObject

Return the first (single) sample (used in one sample VCF)



178
179
180
# File 'lib/bio-vcf/vcfrecord.rb', line 178

def first
  @first ||= VcfGenotypeField.new(@fields[9],format,@header,ref,alt)
end

#formatObject



173
174
175
# File 'lib/bio-vcf/vcfrecord.rb', line 173

def format
  @format ||= VcfRecordParser.get_format(@fields[8])
end

#gfilter(expr, ignore_missing_data: true, quiet: false) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/bio-vcf/vcfrecord.rb', line 263

def gfilter expr, ignore_missing_data: true, quiet: false
  begin
    if not respond_to?(:call_cached_filter)
      code =
      """
      def call_cached_gfilter(rec,fields)
        r = rec
        #{expr}
      end
      """
      self.class.class_eval(code)
    end
    res = call_cached_gfilter(self,@fields)
    if res.kind_of?(Array)
      res.join("\t")
    else
      res
    end
  rescue NoMethodError => e
    if not quiet
      $stderr.print "RECORD ERROR!\n"
      $stderr.print [@fields],"\n"
      $stderr.print expr,"\n"
    end
    if ignore_missing_data
      $stderr.print e.message if not quiet
      return false
    else
      raise
    end
  end
end

#idObject



149
150
151
# File 'lib/bio-vcf/vcfrecord.rb', line 149

def id
  ids[0]
end

#idsObject



145
146
147
# File 'lib/bio-vcf/vcfrecord.rb', line 145

def ids 
  @ids ||= @fields[2].split(';')
end

#infoObject



169
170
171
# File 'lib/bio-vcf/vcfrecord.rb', line 169

def info
  @info ||= VcfRecordParser.get_info(@fields[7])
end

#missing_samples?Boolean

Returns:

  • (Boolean)


219
220
221
222
223
224
# File 'lib/bio-vcf/vcfrecord.rb', line 219

def missing_samples?
  @fields[9..-1].each { |sample|
    return true if VcfSample::empty?(sample)
  }
  false
end

#normalObject

Return the normal sample (used in two sample VCF)



183
184
185
# File 'lib/bio-vcf/vcfrecord.rb', line 183

def normal
  first
end

#posObject



141
142
143
# File 'lib/bio-vcf/vcfrecord.rb', line 141

def pos
  @pos ||= @fields[1].to_i
end

#qualObject



161
162
163
# File 'lib/bio-vcf/vcfrecord.rb', line 161

def qual
  @qual ||= @fields[5].to_f
end

#refObject



153
154
155
# File 'lib/bio-vcf/vcfrecord.rb', line 153

def ref
  @refs ||= @fields[3]
end

#sampleObject

Return the sample as a named hash



193
194
195
# File 'lib/bio-vcf/vcfrecord.rb', line 193

def sample 
  @sample ||= VcfGenotypeFields.new(@fields,format,@header,ref,alt)
end

#sample_by_index(i) ⇒ Object



201
202
203
204
205
# File 'lib/bio-vcf/vcfrecord.rb', line 201

def sample_by_index i
  # p @fields
  raise "Can not index sample on parameter <#{i}>" if not i.kind_of?(Integer)
  @sample_by_index[i] ||= VcfGenotypeField.new(@fields[i+9],format,@header,ref,alt)
end

#sample_by_name(name) ⇒ Object



197
198
199
# File 'lib/bio-vcf/vcfrecord.rb', line 197

def sample_by_name name
  sample[name]
end

#samplesObject



213
214
215
216
217
# File 'lib/bio-vcf/vcfrecord.rb', line 213

def samples
  list = []
  each_sample { |s| list << s }
  list
end

#tumorObject

Return the tumor sample (used in two sample VCF)



188
189
190
# File 'lib/bio-vcf/vcfrecord.rb', line 188

def tumor
  @tumor ||= VcfGenotypeField.new(@fields[10],format,@header,ref,alt)
end

#valid?Boolean

Returns:

  • (Boolean)


226
227
228
# File 'lib/bio-vcf/vcfrecord.rb', line 226

def valid?
  @fields.size == @header.column_names.size
end