sylfy 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -0
  3. data/Rakefile +13 -0
  4. data/lib/sylfy.rb +12 -25
  5. data/lib/sylfy/add.rb +10 -0
  6. data/lib/sylfy/add/bio_kegg_kgml.rb +108 -0
  7. data/lib/sylfy/datamodel.rb +141 -0
  8. data/lib/sylfy/feature.rb +128 -0
  9. data/lib/sylfy/mathf.rb +40 -0
  10. data/lib/sylfy/pattern.rb +14 -0
  11. data/lib/sylfy/service.rb +34 -0
  12. data/lib/sylfy/service/biocycrest.rb +41 -0
  13. data/lib/sylfy/service/cactusncirest.rb +64 -0
  14. data/lib/sylfy/service/cbioportal.rb +156 -0
  15. data/lib/sylfy/service/chembl.rb +106 -0
  16. data/lib/sylfy/service/ebisoap.rb +55 -0
  17. data/lib/sylfy/service/ensemblrest.rb +64 -0
  18. data/lib/sylfy/service/ensemblrest/archive.rb +35 -0
  19. data/lib/sylfy/service/ensemblrest/comparative.rb +146 -0
  20. data/lib/sylfy/service/ensemblrest/xrefs.rb +73 -0
  21. data/lib/sylfy/service/hgncrest.rb +52 -0
  22. data/lib/sylfy/service/keggrest.rb +6 -19
  23. data/lib/sylfy/service/keggrest/conv.rb +64 -40
  24. data/lib/sylfy/service/keggrest/find.rb +27 -48
  25. data/lib/sylfy/service/keggrest/get.rb +82 -0
  26. data/lib/sylfy/service/keggrest/link.rb +17 -37
  27. data/lib/sylfy/service/keggrest/list.rb +18 -50
  28. data/lib/sylfy/service/lipidmaprest.rb +228 -0
  29. data/lib/sylfy/service/pubchem.rb +71 -0
  30. data/lib/sylfy/service/pubchemrest.rb +249 -0
  31. data/lib/sylfy/service/rest.rb +26 -0
  32. data/lib/sylfy/service/soapwsdl.rb +78 -0
  33. data/lib/sylfy/service/unichemrest.rb +106 -0
  34. data/lib/sylfy/utils.rb +18 -0
  35. data/lib/sylfy/utils/keyhash.rb +1149 -0
  36. data/lib/sylfy/utils/reactionkey.rb +197 -0
  37. data/lib/sylfy/version.rb +1 -1
  38. data/sylfy.gemspec +11 -15
  39. data/test/test_kegg_rest.rb +58 -0
  40. data/test/test_reactionkey.rb +37 -0
  41. metadata +87 -15
  42. data/lib/sylfy/math.rb +0 -24
  43. data/lib/sylfy/service/keggrest/restKegg_get.rb +0 -130
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cd2c538ef9a50e980f048613f58adc04c6d74a32
4
- data.tar.gz: fbf52a043c05cec06323f1283e391e42aaea7ef5
3
+ metadata.gz: 1d2d470deb529a4e69442709c0205f1e6f69b09f
4
+ data.tar.gz: a8861d929296e449c5269131f5d5ba428c6001b6
5
5
  SHA512:
6
- metadata.gz: 574c26d637131356a91c70420285bea69ce79ae1259a5769e158a972225e0a6431ef9d4aae57f4bc74dab2ad3dd02b333f065aab442d28735aa9c9c419a12490
7
- data.tar.gz: c6c5f9cc4c6c59b72027bd30a6d2edc2281a6646ca5ad62969ffe0785f27a969ba6b591c7095a48a3ccc70f9dac978631413e441ef5d8ab96a869bf66fd8f07c
6
+ metadata.gz: 04fd7926e7d18992116e4f09c5019d020457a16f3872d13cdae8fffbbd959aa1485a7e853afa0939d57c6dbf160d6f7579627b3cbbb371cf22a5173bbbbe9c0a
7
+ data.tar.gz: e934410e8654a74c486cd2a69af9d18f1228914d9cdb5f4a80b77d54d6814a5db43ae5a373529a2be003202956bd0b39b8bda71fc236fd974e26037f7980e9d9
data/README.md CHANGED
@@ -1,2 +1,4 @@
1
1
  sylfy
2
2
  =====
3
+
4
+ <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png" /></a><br />This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US">Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License</a>.
@@ -0,0 +1,13 @@
1
+ require 'rake/testtask'
2
+ require 'yard'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ YARD::Rake::YardocTask.new do |t|
9
+ t.files = ['lib/**/*.rb', '-']
10
+ end
11
+
12
+ #desc "Run tests"
13
+ #task :default => :test
@@ -1,6 +1,6 @@
1
1
  #
2
- # UniSysDB library in Ruby
3
- # Copyright (C) 2012
2
+ # Systems biology library for Ruby (Sylfy)
3
+ # Copyright (C) 2012-2013
4
4
  #
5
5
  # author: Natapol Pornputtapong <natapol@chalmers.se>
6
6
  #
@@ -9,35 +9,22 @@
9
9
 
10
10
  # raise "Please, use ruby 1.9.0 or later." if RUBY_VERSION < "1.9.0"
11
11
 
12
- # == Overview
13
- #
14
- #
15
- #
16
- # == Introduction
17
- #
18
- #
19
- #
20
- # == Queries
21
- #
22
- #
23
- #
24
- # === Environmental Variables
25
- #
26
- #
27
- #
28
- # === Parameters
29
- #
30
- #
31
12
 
32
- $: << File.join(File.expand_path(File.dirname(__FILE__)))
33
13
 
34
- require 'gsl'
14
+ $: << File.join(File.expand_path(File.dirname(__FILE__)))
35
15
 
16
+ require 'csv'
36
17
 
18
+ require 'bio'
37
19
 
38
- require 'sylfy/math'
20
+ require 'sylfy/mathf'
21
+ require 'sylfy/service'
22
+ require 'sylfy/feature'
23
+ require 'sylfy/add'
24
+ require 'sylfy/utils'
25
+ require 'sylfy/pattern'
39
26
 
40
27
  module Sylfy
41
28
 
42
29
 
43
- end
30
+ end
@@ -0,0 +1,10 @@
1
+ #
2
+ # UniSysDB library in Ruby
3
+ # Copyright (C) 2012
4
+ #
5
+ # author: Natapol Pornputtapong <natapol.por@gmail.com>
6
+ #
7
+ # Documentation: Natapol Pornputtapong (RDoc'd and embellished by William Webber)
8
+ #
9
+
10
+ require 'sylfy/add/bio_kegg_kgml'
@@ -0,0 +1,108 @@
1
+ require 'open-uri'
2
+
3
+ # additional module of bio ruby library
4
+ module Bio
5
+ # Class Bio::KEGG
6
+ class KEGG
7
+ # Class Bio::KEGG::KGML
8
+ class KGML
9
+ # convert KGML to mjson (map json)
10
+ def to_mjson()
11
+ mjson = {:identifier => :id}
12
+
13
+ # <pathway> attributes
14
+ mjson[:name] = @name
15
+ mjson[:org] = @org
16
+ mjson[:number] = @number
17
+ mjson[:title] = @title
18
+ mjson[:link] = @link
19
+
20
+
21
+ mjson[:image] = {}
22
+ mjson[:image][:source] = @image
23
+
24
+ begin
25
+ dim = open(@image).read()[0x10..0x18].unpack('NN')
26
+ mjson[:image][:width] = dim[0]
27
+ mjson[:image][:height] = dim[1]
28
+ rescue
29
+
30
+ end
31
+
32
+
33
+ # <entry> attributes
34
+ @entries.each do |entry|
35
+ entryinfo = {:id => entry.id, :entryname => entry.name, :link => entry.link, :entrytype => entry.type, :keggid => entry.names}
36
+
37
+ entryinfo[:reaction] = entry.reaction if entry.reaction
38
+
39
+ # <graphics> attributes
40
+ graphicinfo = {}
41
+ if entry.graphics.length == 1
42
+ graphicinfo[:fill] = entry.graphics[0].bgcolor if entry.graphics[0].bgcolor
43
+ graphicinfo[:stroke] = entry.graphics[0].fgcolor if entry.graphics[0].fgcolor
44
+ graphicinfo[:name] = entry.graphics[0].name if entry.graphics[0].name
45
+
46
+ case entry.graphics[0].type
47
+ when 'rectangle'
48
+ graphicinfo[:type] = :rect
49
+ graphicinfo[:width] = entry.graphics[0].width
50
+ graphicinfo[:height] = entry.graphics[0].height
51
+ graphicinfo[:x] = entry.graphics[0].x
52
+ graphicinfo[:y] = entry.graphics[0].y
53
+ when 'roundrectangle'
54
+ graphicinfo[:type] = :rect
55
+ graphicinfo[:width] = entry.graphics[0].width
56
+ graphicinfo[:height] = entry.graphics[0].height
57
+ graphicinfo[:x] = entry.graphics[0].x
58
+ graphicinfo[:y] = entry.graphics[0].y
59
+ graphicinfo[:r] = 10
60
+ when 'circle'
61
+ graphicinfo[:type] = :circle
62
+ graphicinfo[:r] = entry.graphics[0].width/2
63
+ graphicinfo[:cx] = entry.graphics[0].x
64
+ graphicinfo[:cy] = entry.graphics[0].y
65
+ when 'line'
66
+ graphicinfo[:type] = :path
67
+ paths = []
68
+ entry.graphics[0].coords.each {|coord| paths.push(coord.join(" "))}
69
+ graphicinfo[:fill] = entry.graphics[0].bgcolor if entry.graphics[0].bgcolor
70
+ graphicinfo[:stroke] = entry.graphics[0].fgcolor if entry.graphics[0].fgcolor
71
+ graphicinfo[:name] = entry.graphics[0].name if entry.graphics[0].name
72
+ graphicinfo[:d] ||= ""
73
+ graphicinfo[:d] += "M #{paths.join("L")}" if !paths.empty?()
74
+ graphicinfo[:"stroke-width"] = 3
75
+ graphicinfo[:"stroke-linecap"] = "square"
76
+ end
77
+
78
+ else
79
+ entry.graphics.each do |graph|
80
+ if graph.type == 'line'
81
+ paths = []
82
+ graph.coords.each {|coord| paths.push(coord.join(" "))}
83
+ graphicinfo[:type] = :path
84
+ graphicinfo[:fill] = graph.bgcolor if graph.bgcolor
85
+ graphicinfo[:stroke] = graph.fgcolor if graph.fgcolor
86
+ graphicinfo[:name] = graph.name if graph.name
87
+ graphicinfo[:d] ||= ""
88
+ graphicinfo[:d] += "M #{paths.join("L")}" if !paths.empty?()
89
+ graphicinfo[:"stroke-width"] = 3
90
+ graphicinfo[:"stroke-linecap"] = "square"
91
+ else
92
+ raise "Graphic parsinf error to #{id}:#{entry.id}"
93
+ end
94
+ end
95
+ end
96
+
97
+ entryinfo[:graphic] = graphicinfo
98
+ mjson[graphicinfo[:type]] ||= []
99
+ mjson[graphicinfo[:type]].push(entryinfo)
100
+
101
+ # <component> attributes
102
+
103
+ end
104
+ return mjson
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,141 @@
1
+ # author: Natapol Pornputtapong <natapol.por@gmail.com>
2
+ #
3
+ # Documentation: Natapol Pornputtapong (RDoc'd and embellished by William Webber)
4
+ #
5
+ require 'json'
6
+
7
+ module Sylfy
8
+ # Model template
9
+ class DataModel
10
+
11
+ class DuplicateError < Exception
12
+
13
+ end
14
+
15
+ attr_reader :data
16
+ # Initialize function
17
+ # @param data [Hash] data object
18
+ def initialize(data = nil)
19
+ @data = {class: self.class.to_sym}
20
+ if data
21
+ data.each_pair do |field, value|
22
+ self.send "#{field}=".to_sym, value if self.method_defined?(field)
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ # convert data object to JSON
29
+ # @return [JSON]
30
+ def to_json()
31
+ return @data.to_json
32
+ end
33
+ # @return [String]
34
+ def to_str()
35
+ return JSON::pretty_generate(@data)
36
+ end
37
+ # @return [Hash]
38
+ def to_hash()
39
+ return @data
40
+ end
41
+ # Test if data object is valid
42
+ def valid?()
43
+ if @@validates
44
+ result = true
45
+ @@validates.each_pair do |field, rules|
46
+ rules.each_pair do |type, rule|
47
+ case type.to_sym
48
+ when :type
49
+ if rule.kind_of?(Array)
50
+ rule.each {|e| result &&= self.assert_type_of?(e, @data[field.to_sym])}
51
+ else
52
+ result &&= self.assert_type_of?(rule, @data[field.to_sym])
53
+ end
54
+ when :presence
55
+ result &&= !(rule ^ (@data[field.to_sym] != nil && !@data[field.to_sym].empty?))
56
+ when :format
57
+ if rule.kind_of?(Array)
58
+ rule.each {|e| result &&= self.assert_match?(e, @data[field.to_sym])}
59
+ else
60
+ result &&= self.assert_match?(rule, @data[field.to_sym])
61
+ end
62
+ end
63
+ end
64
+ end
65
+ return result
66
+ else
67
+ return true
68
+ end
69
+ end
70
+
71
+ def assert_type_of?(crass, value)
72
+ if crass < DataModel
73
+ return crass.new(value).valid?
74
+ else
75
+ return value.kind_of?(crass)
76
+ end
77
+ end
78
+
79
+ protected :assert_type_of?
80
+
81
+ def assert_match?(pattern, value)
82
+ return value.kind_of?(String) && value =~ pattern ? true : false
83
+ end
84
+
85
+ protected :assert_match?
86
+
87
+ def self.has_one(*attrs)
88
+ @@has_one ||= []
89
+ attrs.each do |attr|
90
+ if !self.method_defined?(attr)
91
+ @@has_one.push(attr)
92
+ self.class_eval do
93
+ define_method(attr) do
94
+ @data[attr]
95
+ end
96
+
97
+ define_method("#{attr}=") do |param|
98
+ @data[attr] = param.kind_of?(DataModel) ? param.to_hash : param
99
+ end
100
+ end
101
+ else
102
+ raise DuplicateError, "#{attr.inspect} is already defined."
103
+ end
104
+ end
105
+ end
106
+
107
+ def self.has_many(*attrs)
108
+ @@has_many ||= []
109
+ attrs.each do |attr|
110
+ if !self.method_defined?(attr)
111
+ @@has_many.push(attr)
112
+ self.class_eval do
113
+ define_method(attr) do
114
+ @data[attr] = []
115
+ end
116
+
117
+ define_method("#{attr}=") do |*param|
118
+ @data[attr] = param.flatten
119
+ end
120
+
121
+ define_method("#{attr}_add") do |*param|
122
+ @data[attr] += param.flatten
123
+ end
124
+ end
125
+ else
126
+ raise DuplicateError, "#{attr.inspect} is already defined."
127
+ end
128
+ end
129
+ end
130
+
131
+ def self.validates(*attrs, rules)
132
+ @@validates ||= {}
133
+ attrs.each do |attr|
134
+ @@validates[attr] ||= {}
135
+ @@validates[attr].merge!(rules)
136
+ end
137
+ end
138
+
139
+ #class << self;self;end.send :protected, :subjects_xml, :subjects_html
140
+ end
141
+ end
@@ -0,0 +1,128 @@
1
+ #
2
+ # UniSysDB library in Ruby
3
+ # Copyright (C) 2012
4
+ #
5
+ # author: Natapol Pornputtapong <natapol@chalmers.se>
6
+ #
7
+ # Documentation: Natapol Pornputtapong (RDoc'd and embellished by William Webber)
8
+ #
9
+ # This class is used by Ondine
10
+
11
+ module Sylfy
12
+
13
+ class Feature
14
+
15
+ attr_reader :member, :listnumber, :length
16
+
17
+ def initialize(listnumber, member, length)
18
+ @listnumber = listnumber
19
+ @member = member
20
+ @length = length
21
+ end
22
+
23
+ protected :initialize
24
+
25
+ def self.open(filename, options)
26
+ #@listnumber = {:path => { "path:map00010"=>["Glycolysis / Gluconeogenesis"], ...}, "gene" => {"path:map00010"=>["ko:K00001", "ko:K00002", ...], ...}, "comp" => {... }
27
+ #"ko:K02790"=>{"gene"=>["path:map00010", "path:map00500", "path:map02060"]}
28
+ listnumber = {}
29
+ member = {}
30
+ length = {}
31
+ # type group member
32
+ CSV.open(filename, "rb", options).readlines.each do |row|
33
+ length[row[0]] = 0 if !length.has_key?(row[0])
34
+ listnumber[row[0]] = {} if !listnumber.has_key?(row[0])
35
+ listnumber[row[0]][row[1]] = [] if !listnumber[row[0]].has_key?(row[1])
36
+ listnumber[row[0]][row[1]].push(row[2])
37
+
38
+ if row[0] != 'path'
39
+ if !member.has_key?(row[2]) &&
40
+ member[row[2]] = {}
41
+ member[row[2]].default = []
42
+ length[row[0]] += 1
43
+ end
44
+
45
+ member[row[2]][row[0]] = [] if !member[row[2]].has_key?(row[0])
46
+ member[row[2]][row[0]].push(row[1])
47
+
48
+ end
49
+ end
50
+ return Sylfy::Feature.new(listnumber, member, length)
51
+ end
52
+
53
+ def self.fromKEGG(species = 'ref')
54
+
55
+ listnumber = {}
56
+ member = {}
57
+ complist = Service::KEGGREST.link("compound/pathway")
58
+
59
+ if species == 'ref'
60
+ pathwaylist = Service::KEGGREST.list("pathway")
61
+ genelist = Service::KEGGREST.link("ko/pathway")
62
+ genelist.delete_if{|k, v| k !~ /^path:map/ }
63
+ complist.delete_if{|k, v| k !~ /^path:map/ }
64
+
65
+ else
66
+ pathwaylist = Service::KEGGREST.list("pathway/#{species}")
67
+ genelist = Service::KEGGREST.link("#{species}/pathway")
68
+ tmp = {}
69
+ complist.each_pair do |k, v|
70
+ tmp["path:#{species}#{k[-6..-1]}"] = v if !tmp.has_key?("path:#{species}#{k[-6..-1]}")
71
+ end
72
+ complist = tmp
73
+ end
74
+
75
+ listnumber["path"] = pathwaylist
76
+ listnumber["gene"] = genelist
77
+ listnumber["comp"] = complist
78
+
79
+ length = {"path" => pathwaylist.length}
80
+ genelist.each_pair do |k, v|
81
+ v.each do |entry|
82
+ if !member.has_key?(entry)
83
+ member[entry] = {}
84
+ member[entry].default = []
85
+ end
86
+
87
+ member[entry]["gene"] = [] if !member[entry].has_key?("gene")
88
+ member[entry]["gene"].push(k)
89
+ end
90
+ end
91
+
92
+ @length["gene"] = member.length
93
+
94
+ complist.each_pair do |k, v|
95
+ v.each do |entry|
96
+ if !member.has_key?(entry)
97
+ member[entry] = {}
98
+ member[entry].default = []
99
+ end
100
+
101
+ member[entry]["comp"] = [] if !member[entry].has_key?("comp")
102
+ member[entry]["comp"].push(k)
103
+ end
104
+ end
105
+
106
+ @length["comp"] = member.length - @length["gene"]
107
+
108
+ return Sylfy::Feature.new(listnumber, member, length)
109
+
110
+ end
111
+ #def enrichAnal(array, type = [])
112
+ # featureCount = {}
113
+ # array.each do |entry|
114
+ # if featureMember.has_key?(entry[1])
115
+ #
116
+ # end
117
+ #
118
+ # featureMember[entry[0]]
119
+ # featureCount[row[0]] = {} if !featureCount.has_key?(row[0])
120
+ # featureCount[row[0]][row[1]] = 0 if !featureCount[row[0]].has_key?(row[1])
121
+ # end
122
+ #
123
+ #end
124
+ end
125
+
126
+ end
127
+
128
+