Class: Bioinform::Parser

Inherits:
Object show all
Includes:
SingleMotifParser
Defined in:
lib/bioinform/parsers/parser.rb,
lib/bioinform/parsers/splittable_parser.rb

Defined Under Namespace

Modules: MultipleMotifsParser, SingleMotifParser

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SingleMotifParser

#each, included

Constructor Details

#initialize(*input) ⇒ Parser

Returns a new instance of Parser.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bioinform/parsers/parser.rb', line 14

def initialize(*input)
  if input.size == 1  # [ [1,2,3,4] ],  [  [[1,2,3,4],[5,6,7,8]] ]

    if input.first.is_a?(Array) && input.first.all?{|el| el.is_a? Numeric}  # [ [1,2,3,4] ]

      @input = input
    else  # [  [[1,2,3,4],[5,6,7,8]] ]

      @input = input.first
    end
  else #[ [1,2,3,4], [5,6,7,8] ], [   ]

    @input = input
  end
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



12
13
14
# File 'lib/bioinform/parsers/parser.rb', line 12

def input
  @input
end

Class Method Details

.array_from_acgt_hash(hsh) ⇒ Object

1, C: 2, G: 3, T: 4 –> [1,2,3,4] [1,2], C: [3,4], G: [5,6], T: [7,8] –> [[1,3,5,7],] ( == [[1,2], [3,4], [5,6], [7,8]].transpose)



53
54
55
56
57
58
# File 'lib/bioinform/parsers/parser.rb', line 53

def self.array_from_acgt_hash(hsh)
  hsh = normalize_hash_keys(hsh)
  raise 'some of hash keys A,C,G,T are missing or hash has excess keys' unless hsh.keys.sort == [:A,:C,:G,:T]
  result = [:A,:C,:G,:T].collect{|letter| hsh[letter] }
  result.all?{|el| el.is_a?(Array)} ? result.transpose : result
end

.choose(input, data_model = PM) ⇒ Object



36
37
38
# File 'lib/bioinform/parsers/parser.rb', line 36

def self.choose(input, data_model = PM)
  data_model.choose_parser(input).new(input)
end

.need_tranpose?(input) ⇒ Boolean

point whether matrix input positions(need not be transposed – false) or letters(need – true) as first index

[1,3,5,7], [2,4,6,8]

–> false

[1,2],,[5,6],

–> true

Returns:

  • (Boolean)


84
85
86
# File 'lib/bioinform/parsers/parser.rb', line 84

def self.need_tranpose?(input)
  (input.size == 4) && input.any?{|x| x.size != 4}
end

.normalize_hash_keys(hsh) ⇒ Object

1, C: 2, ‘g’ => 3, ‘T’ => 4 –> 1, C: 2, G: 3, T: 4



61
62
63
# File 'lib/bioinform/parsers/parser.rb', line 61

def self.normalize_hash_keys(hsh)
  hsh.collect_hash{|key,value| [key.to_s.upcase.to_sym, value] }
end

.parse(*input) ⇒ Object



43
44
45
# File 'lib/bioinform/parsers/parser.rb', line 43

def self.parse(*input)
  self.new(*input).parse
end

.parse!(*input) ⇒ Object



40
41
42
# File 'lib/bioinform/parsers/parser.rb', line 40

def self.parse!(*input)
  self.new(*input).parse!
end

.transform_input(input) ⇒ Object



76
77
78
79
# File 'lib/bioinform/parsers/parser.rb', line 76

def self.transform_input(input)
  result = try_convert_to_array(input).map{|el| try_convert_to_array(el)}
  need_tranpose?(result) ? result.transpose : result
end

.try_convert_to_array(input) ⇒ Object

[1,2,3,4], [2,3,4,5]

–> [[1,2,3,4], [2,3,4,5]]

C:2, G:3, T:4, C:3, G:4, T:5

–> [C:2, G:3, T:4, C:3, G:4, T:5]

=> [1,2,3], :c => [2,3,4], ‘g’ => [3,4,5], ‘T’ => [4,5,6] –> [[1,2,3],,[3,4,5],].transpose



68
69
70
71
72
73
74
# File 'lib/bioinform/parsers/parser.rb', line 68

def self.try_convert_to_array(input)
  case input
  when Array then input
  when Hash then array_from_acgt_hash(input)
  else raise TypeError, 'input of Bioinform::Parser::array_from_acgt_hash should be Array or Hash'
  end
end

.valid_matrix?(matrix) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/bioinform/parsers/parser.rb', line 47

def self.valid_matrix?(matrix)
  PM.valid_matrix?(matrix)
end

Instance Method Details

#parseObject



32
33
34
# File 'lib/bioinform/parsers/parser.rb', line 32

def parse
  parse! rescue nil
end

#parse!Object

Raises:



26
27
28
29
30
# File 'lib/bioinform/parsers/parser.rb', line 26

def parse!
  matrix = self.class.transform_input(input)
  raise InvalidMatrix unless self.class.valid_matrix?(matrix)
  OpenStruct.new(matrix: matrix)
end