Class: GeneValidator::TabularParser

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/genevalidator/tabular_parser.rb

Overview

This class parses the tabular output of BLAST (outfmt 6 & 7)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tab_file = opt[:blast_tabular_file], format = opt[:blast_tabular_options], type = config[:type]) ⇒ TabularParser

Initializes the object



23
24
25
26
27
28
29
# File 'lib/genevalidator/tabular_parser.rb', line 23

def initialize(tab_file = opt[:blast_tabular_file],
               format = opt[:blast_tabular_options], type = config[:type])
  @column_names = format.gsub(/[-\d]/, '').split(/[ ,]/)
  @type         = type
  @tab_results  = analayse_tabular_file(tab_file)
  @rows         = @tab_results.to_enum
end

Instance Attribute Details

#column_namesObject (readonly)

Returns the value of attribute column_names.



18
19
20
# File 'lib/genevalidator/tabular_parser.rb', line 18

def column_names
  @column_names
end

#rowsObject (readonly)

Returns the value of attribute rows.



16
17
18
# File 'lib/genevalidator/tabular_parser.rb', line 16

def rows
  @rows
end

#tab_resultsObject (readonly)

Returns the value of attribute tab_results.



17
18
19
# File 'lib/genevalidator/tabular_parser.rb', line 17

def tab_results
  @tab_results
end

#typeObject (readonly)

Returns the value of attribute type.



19
20
21
# File 'lib/genevalidator/tabular_parser.rb', line 19

def type
  @type
end

Instance Method Details

#analayse_tabular_file(filename) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/genevalidator/tabular_parser.rb', line 33

def analayse_tabular_file(filename)
  results = []
  file    = File.read(filename)
  lines   = CSV.parse(file, col_sep: "\t", skip_lines: /^#/,
                            headers: @column_names)
  lines.each do |line|
    results << line.to_hash
  end
  results
end

#nextObject Also known as: move_to_next_query

move to next query



46
47
48
49
50
51
52
53
# File 'lib/genevalidator/tabular_parser.rb', line 46

def next
  current_entry = @rows.peek['qseqid']
  loop do
    entry = @rows.peek['qseqid']
    @rows.next
    break unless entry == current_entry
  end
end

#parse_next(query_id = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/genevalidator/tabular_parser.rb', line 59

def parse_next(query_id = nil)
  current_id = @rows.peek['qseqid']
  return [] if !query_id.nil? && current_id != query_id
  hit_seq = initialise_classes(current_id)
  move_to_next_query
  hit_seq
rescue StopIteration
  return []
end