bioinform 0.1.16 → 0.1.17

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 58c71a402e76e3edcd2105b21dd19a84b37b4c80
4
- data.tar.gz: f300eba436a2b06c5b5feef4bb3fb95c0397b0a7
3
+ metadata.gz: 792006e928db4e7ce443f56f93b94e9f5cbc2e95
4
+ data.tar.gz: f7f4f25e156071fee5f242cc50ecdcd80741b596
5
5
  SHA512:
6
- metadata.gz: 1ab6af7c582ca7de30a7b08c73c5979bda3b727cfd810d52114bbd9b657311dc7d634d0f991fd375b2efb86b730badbc48cc41ea512d05197f46f49b673b0ee5
7
- data.tar.gz: f2162a51dec39a7c500474ac4739c40869c00be3b9f837224e35f21137436ea9a827b0d99d5e82d617f0aff30fa30da3dbfed3066d4684fe4cc629f96f3e8443
6
+ metadata.gz: 4d0c1b14cba03745ecdf19b14579533489619f22f584e3fda64a77b4d72ba0998930b8239ceb5212b0d09ca6aa88879abc29057354cd8b4650ae35dab5845a7e
7
+ data.tar.gz: 8e0a3b680e4e5ef765697abd6c0c449dc303ea75c1d886aef4c01a4a34f38a67008ac9bf2dc4f173ba0f7a3cd769cd22424670689fa727946bcc15767a5689ed
@@ -23,7 +23,7 @@ module Bioinform
23
23
  # end
24
24
 
25
25
  def self.choose_parser(input)
26
- [TrivialParser, YAMLParser, Parser, StringParser, StringFantomParser, JasparParser, TrivialCollectionParser, YAMLCollectionParser].find do |parser|
26
+ [TrivialParser, YAMLParser, Parser, StringParser, Bioinform::MatrixParser.new(has_name: false).wrapper, Bioinform::MatrixParser.new(has_name: true).wrapper, StringFantomParser, JasparParser, TrivialCollectionParser, YAMLCollectionParser].find do |parser|
27
27
  self.new(input, parser) rescue nil
28
28
  end
29
29
  end
@@ -40,6 +40,7 @@ module Bioinform
40
40
  raise 'No one parser can process input' unless parser
41
41
  result = parser.new(input).parse
42
42
  @matrix = result.matrix
43
+ raise 'Non valid matrix' unless self.class.valid_matrix?(@matrix)
43
44
  self.name = result.name
44
45
  # self.tags = result.tags || []
45
46
  self.background = result.background || [1, 1, 1, 1]
@@ -1,5 +1,6 @@
1
1
  require_relative '../support'
2
2
  require_relative '../data_models'
3
+
3
4
  module Bioinform
4
5
  class PWM < PM
5
6
  def score_mean
@@ -1,3 +1,5 @@
1
+ require 'ostruct'
2
+
1
3
  module Bioinform
2
4
  class MatrixParser
3
5
  def initialize(options = {})
@@ -24,7 +26,7 @@ module Bioinform
24
26
 
25
27
  matrix = matrix.transpose if @nucleotides_in == :rows
26
28
  # raise 'Matrix not valid' unless ! matrix.empty? && matrix.all?{|pos| pos.size == 4 }
27
- {matrix: matrix, name: name}
29
+ OpenStruct.new(matrix: matrix, name: name)
28
30
  end
29
31
 
30
32
  def parse(input)
@@ -36,5 +38,28 @@ module Bioinform
36
38
  rescue
37
39
  false
38
40
  end
41
+
42
+ class TemporaryWrapper
43
+ attr_reader :input
44
+ include Bioinform::Parser::ClassMethods
45
+ include Bioinform::Parser::SingleMotifParser::ClassMethods
46
+ def initialize(parser)
47
+ @parser, input = parser, input
48
+ end
49
+ def parse
50
+ @parser.parse(@input)
51
+ end
52
+ def parse!
53
+ @parser.parse!(@input)
54
+ end
55
+ def new(input)
56
+ @input = input
57
+ self
58
+ end
59
+ end
60
+
61
+ def wrapper
62
+ TemporaryWrapper.new(self)
63
+ end
39
64
  end
40
65
  end
@@ -33,56 +33,60 @@ module Bioinform
33
33
  parse! rescue nil
34
34
  end
35
35
 
36
- def self.choose(input, data_model = PM)
37
- data_model.choose_parser(input).new(input)
38
- end
36
+ module ClassMethods
37
+ def choose(input, data_model = PM)
38
+ data_model.choose_parser(input).new(input)
39
+ end
39
40
 
40
- def self.parse!(*input)
41
- self.new(*input).parse!
42
- end
43
- def self.parse(*input)
44
- self.new(*input).parse
45
- end
41
+ def parse!(*input)
42
+ new(*input).parse!
43
+ end
44
+ def parse(*input)
45
+ new(*input).parse
46
+ end
46
47
 
47
- def self.valid_matrix?(matrix)
48
- PM.valid_matrix?(matrix)
49
- end
48
+ def valid_matrix?(matrix)
49
+ PM.valid_matrix?(matrix)
50
+ end
50
51
 
51
- # {A: 1, C: 2, G: 3, T: 4} --> [1,2,3,4]
52
- # {A: [1,2], C: [3,4], G: [5,6], T: [7,8]} --> [[1,3,5,7],[2,4,6,8]] ( == [[1,2], [3,4], [5,6], [7,8]].transpose)
53
- def self.array_from_acgt_hash(hsh)
54
- hsh = normalize_hash_keys(hsh)
55
- raise 'some of hash keys A,C,G,T are missing or hash has excess keys' unless hsh.keys.sort == [:A,:C,:G,:T]
56
- result = [:A,:C,:G,:T].collect{|letter| hsh[letter] }
57
- result.all?{|el| el.is_a?(Array)} ? result.transpose : result
58
- end
52
+ # {A: 1, C: 2, G: 3, T: 4} --> [1,2,3,4]
53
+ # {A: [1,2], C: [3,4], G: [5,6], T: [7,8]} --> [[1,3,5,7],[2,4,6,8]] ( == [[1,2], [3,4], [5,6], [7,8]].transpose)
54
+ def array_from_acgt_hash(hsh)
55
+ hsh = normalize_hash_keys(hsh)
56
+ raise 'some of hash keys A,C,G,T are missing or hash has excess keys' unless hsh.keys.sort == [:A,:C,:G,:T]
57
+ result = [:A,:C,:G,:T].collect{|letter| hsh[letter] }
58
+ result.all?{|el| el.is_a?(Array)} ? result.transpose : result
59
+ end
59
60
 
60
- # {a: 1, C: 2, 'g' => 3, 'T' => 4} --> {A: 1, C: 2, G: 3, T: 4}
61
- def self.normalize_hash_keys(hsh)
62
- hsh.collect_hash{|key,value| [key.to_s.upcase.to_sym, value] }
63
- end
61
+ # {a: 1, C: 2, 'g' => 3, 'T' => 4} --> {A: 1, C: 2, G: 3, T: 4}
62
+ def normalize_hash_keys(hsh)
63
+ hsh.collect_hash{|key,value| [key.to_s.upcase.to_sym, value] }
64
+ end
64
65
 
65
- # [[1,2,3,4], [2,3,4,5]] --> [[1,2,3,4], [2,3,4,5]]
66
- # [{A:1, C:2, G:3, T:4}, {A:2, C:3, G:4, T:5}] --> [{A:1, C:2, G:3, T:4}, {A:2, C:3, G:4, T:5}]
67
- # {:A => [1,2,3], :c => [2,3,4], 'g' => [3,4,5], 'T' => [4,5,6]} --> [[1,2,3],[2,3,4],[3,4,5],[4,5,6]].transpose
68
- def self.try_convert_to_array(input)
69
- case input
70
- when Array then input
71
- when Hash then array_from_acgt_hash(input)
72
- else raise TypeError, 'input of Bioinform::Parser::array_from_acgt_hash should be Array or Hash'
66
+ # [[1,2,3,4], [2,3,4,5]] --> [[1,2,3,4], [2,3,4,5]]
67
+ # [{A:1, C:2, G:3, T:4}, {A:2, C:3, G:4, T:5}] --> [{A:1, C:2, G:3, T:4}, {A:2, C:3, G:4, T:5}]
68
+ # {:A => [1,2,3], :c => [2,3,4], 'g' => [3,4,5], 'T' => [4,5,6]} --> [[1,2,3],[2,3,4],[3,4,5],[4,5,6]].transpose
69
+ def try_convert_to_array(input)
70
+ case input
71
+ when Array then input
72
+ when Hash then array_from_acgt_hash(input)
73
+ else raise TypeError, 'input of Bioinform::Parser::array_from_acgt_hash should be Array or Hash'
74
+ end
73
75
  end
74
- end
75
76
 
76
- def self.transform_input(input)
77
- result = try_convert_to_array(input).map{|el| try_convert_to_array(el)}
78
- need_tranpose?(result) ? result.transpose : result
79
- end
77
+ def transform_input(input)
78
+ result = try_convert_to_array(input).map{|el| try_convert_to_array(el)}
79
+ need_tranpose?(result) ? result.transpose : result
80
+ end
80
81
 
81
- # point whether matrix input positions(need not be transposed -- false) or letters(need -- true) as first index
82
- # [[1,3,5,7], [2,4,6,8]] --> false
83
- # [[1,2],[3,4],[5,6],[7,8]] --> true
84
- def self.need_tranpose?(input)
85
- (input.size == 4) && input.any?{|x| x.size != 4}
82
+ # point whether matrix input positions(need not be transposed -- false) or letters(need -- true) as first index
83
+ # [[1,3,5,7], [2,4,6,8]] --> false
84
+ # [[1,2],[3,4],[5,6],[7,8]] --> true
85
+ def need_tranpose?(input)
86
+ (input.size == 4) && input.any?{|x| x.size != 4}
87
+ end
86
88
  end
89
+
90
+ extend ClassMethods
87
91
  end
88
92
  end
@@ -1,3 +1,3 @@
1
1
  module Bioinform
2
- VERSION = "0.1.16"
2
+ VERSION = "0.1.17"
3
3
  end
@@ -3,15 +3,15 @@ require_relative '../../lib/bioinform/data_models/pm'
3
3
 
4
4
  module Bioinform
5
5
  describe PM do
6
- {:as_pcm => PCM, :as_pwm => PWM, :as_ppm => PPM}.each do |converter_method, result_klass|
6
+ {:as_pcm => [PCM, [[1,10,3,4],[5,6,7,0]]], :as_pwm => [PWM, [[1,2,3,4],[5,6,7,8]]], :as_ppm => [PPM, [[0.1,0.2,0.3,0.4],[0.5,0.1,0.3,0.1]]]}.each do |converter_method, (result_klass, matrix)|
7
7
  describe "##{converter_method}" do
8
8
  before :each do
9
9
  @collection = Collection.new(name: 'Collection 1')
10
- @matrix = [[1,2,3,4],[5,6,7,8]]
10
+ @matrix = matrix
11
11
  @name = 'PM_motif'
12
12
  @background = [0.2,0.3,0.3,0.2]
13
13
  @tags = [@collection, 'Collection 2']
14
- @pm = PM.new(matrix: @matrix, name: @name, background: @background, tags: @tags)
14
+ @pm = PM.new(matrix: matrix, name: @name, background: @background, tags: @tags)
15
15
  @conv_motif = @pm.send converter_method
16
16
  end
17
17
  it "should return an instance of #{result_klass}" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bioinform
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.16
4
+ version: 0.1.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Vorontsov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-26 00:00:00.000000000 Z
11
+ date: 2014-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docopt