Class: Bioinform::MatrixParser

Inherits:
Object
  • Object
show all
Defined in:
lib/bioinform/parsers/matrix_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MatrixParser

Returns a new instance of MatrixParser.

Raises:



7
8
9
10
11
12
13
14
15
16
# File 'lib/bioinform/parsers/matrix_parser.rb', line 7

def initialize(options = {})
  @has_name = options.fetch(:has_name, :auto)
  @name_pattern = options.fetch(:name_pattern, /^>?\s*(?<name>[^-+\d.\t\r\n][^\t\r\n]*).*$/)
  @has_header_row = options.fetch(:has_header_row, false)
  @has_header_column = options.fetch(:has_header_column, false)
  @nucleotides_in = options.fetch(:nucleotides_in, :auto)
  @fix_nucleotides_number = options.fetch(:fix_nucleotides_number, 4)

  raise Error, ':nucleotides_in option should be either :rows or :columns' unless  [:rows, :columns, :auto].include?(@nucleotides_in)
end

Instance Attribute Details

#fix_nucleotides_numberObject (readonly)

fix_nucleotides_number – raises if matrix has not enough nucleotide columns



6
7
8
# File 'lib/bioinform/parsers/matrix_parser.rb', line 6

def fix_nucleotides_number
  @fix_nucleotides_number
end

#has_header_columnObject (readonly)

fix_nucleotides_number – raises if matrix has not enough nucleotide columns



6
7
8
# File 'lib/bioinform/parsers/matrix_parser.rb', line 6

def has_header_column
  @has_header_column
end

#has_header_rowObject (readonly)

fix_nucleotides_number – raises if matrix has not enough nucleotide columns



6
7
8
# File 'lib/bioinform/parsers/matrix_parser.rb', line 6

def has_header_row
  @has_header_row
end

#has_nameObject (readonly)

fix_nucleotides_number – raises if matrix has not enough nucleotide columns



6
7
8
# File 'lib/bioinform/parsers/matrix_parser.rb', line 6

def has_name
  @has_name
end

#name_patternObject (readonly)

fix_nucleotides_number – raises if matrix has not enough nucleotide columns



6
7
8
# File 'lib/bioinform/parsers/matrix_parser.rb', line 6

def name_pattern
  @name_pattern
end

#nucleotides_inObject (readonly)

fix_nucleotides_number – raises if matrix has not enough nucleotide columns



6
7
8
# File 'lib/bioinform/parsers/matrix_parser.rb', line 6

def nucleotides_in
  @nucleotides_in
end

Instance Method Details

#parse(input) ⇒ Object



63
64
65
# File 'lib/bioinform/parsers/matrix_parser.rb', line 63

def parse(input)
  parse!(input) rescue nil
end

#parse!(input) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bioinform/parsers/matrix_parser.rb', line 23

def parse!(input)
  lines = input.strip.lines.to_a
  if @has_name == :auto
    match = lines.first.match(@name_pattern)
    if match
      lines.shift
      name = match[:name]
    end
  elsif @has_name == false
    name = nil
  else
    match = lines.shift.match(@name_pattern)
    raise Error, "Name pattern doesn't match"  unless match
    name = match[:name]
  end
  lines.shift  if @has_header_row
  matrix = lines.map(&:rstrip).reject(&:empty?).map{|line| line.split }
  matrix = matrix.map{|row| row.drop(1) }  if @has_header_column
  matrix = matrix.map{|row| row.map{|el| Float(el) } }

  case @nucleotides_in
  when :columns
    matrix = matrix
  when :rows
    matrix = matrix.transpose
  when :auto
    if @fix_nucleotides_number && need_transpose?(matrix)
      matrix = matrix.transpose
    end
  end

  if @fix_nucleotides_number
    raise Error, 'Not enough nucleotides in a matrix'  unless matrix.all?{|pos| pos.size >= @fix_nucleotides_number}
    matrix = matrix.map{|pos| pos.first(@fix_nucleotides_number) }
  end
  {matrix: matrix, name: name}
rescue => e
  raise Error, e.message
end

#valid?(input) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/bioinform/parsers/matrix_parser.rb', line 67

def valid?(input)
  result = parse!(input)
rescue
  false
end