Class: Bioinform::Parser
- Includes:
- SingleMotifParser
- Defined in:
- lib/bioinform/parsers/parser.rb,
lib/bioinform/parsers/splittable_parser.rb
Direct Known Subclasses
StringParser, TrivialCollectionParser, TrivialParser, YAMLCollectionParser, YAMLParser
Defined Under Namespace
Modules: MultipleMotifsParser, SingleMotifParser
Instance Attribute Summary collapse
-
#input ⇒ Object
readonly
Returns the value of attribute input.
Class Method Summary collapse
-
.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).
- .choose(input, data_model = PM) ⇒ Object
- .need_tranpose?(input) ⇒ Boolean
-
.normalize_hash_keys(hsh) ⇒ Object
1, C: 2, ‘g’ => 3, ‘T’ => 4 –> 1, C: 2, G: 3, T: 4.
- .parse(*input) ⇒ Object
- .parse!(*input) ⇒ Object
- .transform_input(input) ⇒ Object
- .try_convert_to_array(input) ⇒ Object
- .valid_matrix?(matrix) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(*input) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
- #parse! ⇒ Object
Methods included from SingleMotifParser
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
#input ⇒ Object (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
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
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
47 48 49 |
# File 'lib/bioinform/parsers/parser.rb', line 47 def self.valid_matrix?(matrix) PM.valid_matrix?(matrix) end |
Instance Method Details
#parse ⇒ Object
32 33 34 |
# File 'lib/bioinform/parsers/parser.rb', line 32 def parse parse! rescue nil end |
#parse! ⇒ Object
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 |