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.



118
119
120
121
122
# File 'lib/bio-vcf/vcfrecord.rb', line 118

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



276
277
278
279
280
281
282
283
284
285
# File 'lib/bio-vcf/vcfrecord.rb', line 276

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.



116
117
118
# File 'lib/bio-vcf/vcfrecord.rb', line 116

def header
  @header
end

Instance Method Details

#altObject



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

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

#chromObject Also known as: chr



124
125
126
# File 'lib/bio-vcf/vcfrecord.rb', line 124

def chrom
  @fields[0]
end

#each_sample(list = nil) ⇒ Object

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



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

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



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/bio-vcf/vcfrecord.rb', line 209

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

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



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/bio-vcf/vcfrecord.rb', line 242

def filter expr, ignore_missing_data: true, quiet: false
  begin
    if not respond_to?(:call_cached_filter)
      code =
      """
      def call_cached_filter(rec,fields)
        r = rec
        #{expr}
      end
      """
      self.class.class_eval(code)
    end
    res = call_cached_filter(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

#firstObject

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



163
164
165
# File 'lib/bio-vcf/vcfrecord.rb', line 163

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

#formatObject



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

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

#idObject



138
139
140
# File 'lib/bio-vcf/vcfrecord.rb', line 138

def id
  ids[0]
end

#idsObject



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

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

#infoObject



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

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

#missing_samples?Boolean

Returns:

  • (Boolean)


198
199
200
201
202
203
# File 'lib/bio-vcf/vcfrecord.rb', line 198

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)



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

def normal
  first
end

#posObject



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

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

#qualObject



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

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

#refObject



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

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

#sampleObject

Return the sample as a named hash



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

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

#sample_by_index(i) ⇒ Object



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

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



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

def sample_by_name name
  sample[name]
end

#tumorObject

Return the tumor sample (used in two sample VCF)



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

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

#valid?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/bio-vcf/vcfrecord.rb', line 205

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