Class: MutationSet::Sample

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mutation_set.rb

Direct Known Subclasses

Indelocator, Maf, MuTect, VCF

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mutation_config = nil, suppress_headers = nil) ⇒ Sample

Returns a new instance of Sample.



289
290
291
292
293
294
295
296
297
298
299
# File 'lib/mutation_set.rb', line 289

def initialize(mutation_config=nil,suppress_headers=nil)
  @lines = []

  @mutation_config = YAML.load_file(mutation_config) if mutation_config

  @headers = required.map(&:to_sym) unless suppress_headers

  @preamble_lines = []

  @index = {}
end

Class Attribute Details

.commentObject (readonly)

Returns the value of attribute comment.



187
188
189
# File 'lib/mutation_set.rb', line 187

def comment
  @comment
end

.requiredObject (readonly)

Returns the value of attribute required.



187
188
189
# File 'lib/mutation_set.rb', line 187

def required
  @required
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



185
186
187
# File 'lib/mutation_set.rb', line 185

def headers
  @headers
end

#linesObject (readonly)

Returns the value of attribute lines.



184
185
186
# File 'lib/mutation_set.rb', line 184

def lines
  @lines
end

#mutation_configObject (readonly)

Returns the value of attribute mutation_config.



184
185
186
# File 'lib/mutation_set.rb', line 184

def mutation_config
  @mutation_config
end

#preamble_linesObject (readonly)

Returns the value of attribute preamble_lines.



184
185
186
# File 'lib/mutation_set.rb', line 184

def preamble_lines
  @preamble_lines
end

#samplesObject (readonly)

Returns the value of attribute samples.



184
185
186
# File 'lib/mutation_set.rb', line 184

def samples
  @samples
end

Class Method Details

.comments(c) ⇒ Object



192
193
194
# File 'lib/mutation_set.rb', line 192

def comments c
  @comment = c
end

.read(filename, mutation_config = nil) ⇒ Object



196
197
198
199
200
201
202
# File 'lib/mutation_set.rb', line 196

def read(filename,mutation_config=nil)
  set = new mutation_config, true

  set.load_file filename

  return set
end

.requires(*terms) ⇒ Object



188
189
190
# File 'lib/mutation_set.rb', line 188

def requires *terms
  @required = terms
end

Instance Method Details

#[](key) ⇒ Object



333
334
335
# File 'lib/mutation_set.rb', line 333

def [](key)
  @lines[key]
end

#add_line(fields) ⇒ Object



262
263
264
265
266
# File 'lib/mutation_set.rb', line 262

def add_line fields
  @lines.push self.class.const_get(:Line).new(clean_fields(fields), self)

  index_line @lines.last
end

#blacklist(file) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
# File 'lib/mutation_set.rb', line 313

def blacklist file
  case file
  when /.gtf$/
    require 'gtf'
    @blacklist ||= GTF.new(file).to_interval_list
  when /.vcf$/
    require 'vcf'
    @blacklist ||= VCF.read(file).to_interval_list
  end
  @blacklist
end

#clean_fields(fields) ⇒ Object



268
269
270
# File 'lib/mutation_set.rb', line 268

def clean_fields fields
  fields.is_a?(Array) ? fields.map{|f| f == "NA" ? "" : f } : fields
end

#clean_header(s) ⇒ Object



254
255
256
# File 'lib/mutation_set.rb', line 254

def clean_header s
  s.to_s.gsub(/\s+/,"_").gsub(/[^\w]+/,"").downcase.to_sym
end

#clean_headersObject



258
259
260
# File 'lib/mutation_set.rb', line 258

def clean_headers
  @headers.map {|h| clean_header h}
end

#eachObject



341
342
343
344
345
# File 'lib/mutation_set.rb', line 341

def each
  @lines.each do |l|
    yield l
  end
end

#enforce_headers(array) ⇒ Object



284
285
286
287
# File 'lib/mutation_set.rb', line 284

def enforce_headers array
  raise "File lacks required headers: #{(required.map(&:downcase)-array.map(&:downcase)).join(", ")}" if !(required.map(&:downcase) - array.map(&:downcase)).empty?
  @headers = array
end

#find_mutation(line) ⇒ Object



276
277
278
# File 'lib/mutation_set.rb', line 276

def find_mutation line
  @index[ line.key ]
end

#format_line(l) ⇒ Object



250
251
252
# File 'lib/mutation_set.rb', line 250

def format_line l
  l.to_s
end

#index_line(line) ⇒ Object



272
273
274
# File 'lib/mutation_set.rb', line 272

def index_line line
  @index[ line.key ] = line
end

#inspectObject



329
330
331
# File 'lib/mutation_set.rb', line 329

def inspect
  to_s
end

#load_file(filename) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/mutation_set.rb', line 205

def load_file filename
  File.foreach(filename) do |l|
    fields = l.chomp.split(/\t/,-1)
    if !headers
      if fields.first.downcase == required.first.downcase
        enforce_headers fields
      else
        preamble_lines.push l
      end
      next
    end
    add_line fields
  end

  post_read_hook
end

#output(f) ⇒ Object



240
241
242
243
244
245
246
247
248
# File 'lib/mutation_set.rb', line 240

def output f
  f.puts preamble
  f.puts headers.join("\t")
  @lines.each do |l|
    l = yield l if block_given?
    next if !l || l.invalid?
    f.puts format_line(l)
  end
end

#preambleObject



222
223
224
# File 'lib/mutation_set.rb', line 222

def preamble
  preamble_lines.join("")
end


232
233
234
235
236
237
238
# File 'lib/mutation_set.rb', line 232

def print f=nil
  if f
    write f
  else
    output STDOUT
  end
end

#requiredObject



280
281
282
# File 'lib/mutation_set.rb', line 280

def required
  self.class.required
end

#sort_by!(&block) ⇒ Object



337
338
339
# File 'lib/mutation_set.rb', line 337

def sort_by! &block
  @lines.sort_by! &block
end

#to_interval_listObject



325
326
327
# File 'lib/mutation_set.rb', line 325

def to_interval_list
  IntervalList.new self.map{|g| [ g.chrom, g.start, g.stop, g ] }
end

#whitelist(file) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
# File 'lib/mutation_set.rb', line 301

def whitelist file
  case file
  when /.gtf$/
    require 'gtf'
    @whitelist ||= GTF.new(file).to_interval_list
  when /.vcf$/
    require 'vcf'
    @whitelist ||= VCF.read(file).to_interval_list
  end
  @whitelist
end

#write(file) ⇒ Object



226
227
228
229
230
# File 'lib/mutation_set.rb', line 226

def write file
  File.open(file,"w") do |f|
    output f
  end
end