Class: Bioinform::MatrixParser
- Inherits:
-
Object
- Object
- Bioinform::MatrixParser
- Defined in:
- lib/bioinform/parsers/matrix_parser.rb
Instance Attribute Summary collapse
-
#fix_nucleotides_number ⇒ Object
readonly
fix_nucleotides_number – raises if matrix has not enough nucleotide columns.
-
#has_header_column ⇒ Object
readonly
fix_nucleotides_number – raises if matrix has not enough nucleotide columns.
-
#has_header_row ⇒ Object
readonly
fix_nucleotides_number – raises if matrix has not enough nucleotide columns.
-
#has_name ⇒ Object
readonly
fix_nucleotides_number – raises if matrix has not enough nucleotide columns.
-
#name_pattern ⇒ Object
readonly
fix_nucleotides_number – raises if matrix has not enough nucleotide columns.
-
#nucleotides_in ⇒ Object
readonly
fix_nucleotides_number – raises if matrix has not enough nucleotide columns.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ MatrixParser
constructor
A new instance of MatrixParser.
- #parse(input) ⇒ Object
- #parse!(input) ⇒ Object
- #valid?(input) ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ MatrixParser
Returns a new instance of MatrixParser.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/bioinform/parsers/matrix_parser.rb', line 7 def initialize( = {}) @has_name = .fetch(:has_name, :auto) @name_pattern = .fetch(:name_pattern, /^>?\s*(?<name>[^-+\d.\t\r\n][^\t\r\n]*).*$/) @has_header_row = .fetch(:has_header_row, false) @has_header_column = .fetch(:has_header_column, false) @nucleotides_in = .fetch(:nucleotides_in, :auto) @fix_nucleotides_number = .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_number ⇒ Object (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_column ⇒ Object (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_row ⇒ Object (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_name ⇒ Object (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_pattern ⇒ Object (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_in ⇒ Object (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. end |
#valid?(input) ⇒ Boolean
67 68 69 70 71 |
# File 'lib/bioinform/parsers/matrix_parser.rb', line 67 def valid?(input) result = parse!(input) rescue false end |