gene_ontology 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2011 Brigham Young University
2
+ Authored by John T. Prince
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,23 @@
1
+ = gene_ontology
2
+
3
+ Parses gene ontology .obo files, links terms through is_a and provides methods
4
+ to find levels and traverse the tree.
5
+
6
+ == Examples
7
+
8
+ go = GeneOntology.new.from_file("gene_ontology.obo")
9
+ go.header # => a GeneOntology::Header object
10
+ go.id_to_term # => a hash from GO id to the GeneOntology::Term
11
+ some_term = go.id_to_term.values.first
12
+
13
+ # traverse the tree upwards, beginning with the current Term
14
+ some_term.each do |term|
15
+ term.name =~ /Plasma Membrane/i
16
+ end
17
+ some_term.level # => how many levels down from the top 3
18
+ # molecular_function, biol comp. etc are level 0
19
+
20
+ == Copyright
21
+
22
+ See LICENSE
23
+
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gem|
6
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
7
+ gem.name = "gene_ontology"
8
+ gem.homepage = "http://github.com/princelab/gene_ontology"
9
+ gem.license = "MIT"
10
+ gem.summary = %Q{parses gene ontology (GO) .obo files and provides tree traversal methods}
11
+ gem.description = %{Parses gene ontology (GO) .obo files, links terms through is_a and provides methods
12
+ to find levels and traverse the tree.}
13
+ gem.email = "jtprince@gmail.com"
14
+ gem.authors = ["John T. Prince"]
15
+ gem.add_development_dependency "spec-more", ">= 0"
16
+ gem.add_development_dependency "bundler", "~> 1.0.0"
17
+ gem.add_development_dependency "jeweler", "~> 1.5.2"
18
+ gem.add_development_dependency "rcov", ">= 0"
19
+ end
20
+ Jeweler::RubygemsDotOrgTasks.new
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.pattern = 'spec/**/*_spec.rb'
26
+ spec.verbose = true
27
+ end
28
+
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |spec|
31
+ spec.libs << 'spec'
32
+ spec.pattern = 'spec/**/*_spec.rb'
33
+ spec.verbose = true
34
+ end
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "gene_ontology #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,134 @@
1
+
2
+ # typical usage:
3
+ #
4
+ # terms = GeneOntology.new.from_file(filename)
5
+ class GeneOntology
6
+
7
+ attr_accessor :header
8
+ attr_accessor :id_to_term
9
+
10
+ # returns self
11
+ def from_file(filename)
12
+ File.open(filename) do |io|
13
+ @header = parse_header(io)
14
+ @id_to_term = parse_terms(io)
15
+ end
16
+ self
17
+ end
18
+
19
+ # returns a header object
20
+ def parse_header(io)
21
+ header = Header.new
22
+ while md=io.gets.match(/^([\w\-]+): (.*)/)
23
+ key, val = md[1].to_sym, md[2]
24
+ if key == :subsetdef
25
+ header.subsetdefs.push(val)
26
+ elsif !header.respond_to?(key)
27
+ header.other[key] = val
28
+ else
29
+ header[key] = val
30
+ end
31
+ end
32
+ header
33
+ end
34
+
35
+ # turns is_a links from strings to actual GeneOntology objects
36
+ # returns id_to_term
37
+ def self.link!(terms)
38
+ id_to_term = {}
39
+ terms.each {|term| id_to_term[term.id] = term }
40
+ terms.each do |term|
41
+ term.is_a.map! {|id| id_to_term[id] }
42
+ end
43
+ id_to_term
44
+ end
45
+
46
+ # returns id_to_term
47
+ # is_a points to an array of actual objects
48
+ def parse_terms(io, opts={})
49
+ opts = {:link => true}.merge(opts)
50
+ terms = []
51
+ in_term = false
52
+ while line = io.gets
53
+ if (md=line.match(/^(\w+): (.+)/)) && (in_term)
54
+ key, val = md[1].to_sym, md[2].split(' ! ').first
55
+ if Term::PLURAL.include?(key)
56
+ terms.last.send(key) << val
57
+ else
58
+ terms.last.send("#{key}=", val)
59
+ end
60
+ elsif line !~ /\w/
61
+ in_term = false
62
+ elsif line =~ /\[Term\]/
63
+ terms << Term.new
64
+ in_term = true
65
+ end
66
+ end
67
+ id_to_term = self.class.link!(terms)
68
+ id_to_term.values.each {|term| term.find_level }
69
+ id_to_term
70
+ end
71
+
72
+ # synonym, xref, consider and is_a are arrays. level is how far down the
73
+ # heirarchy the term is. 0 is the top level (molecular function, biological
74
+ # process...)
75
+ class Term
76
+ include Enumerable
77
+ attr_accessor *%w(id level alt_id intersection_of replaced_by created_by creation_date disjoint_from relationship name namespace def subset comment is_obsolete synonym xref consider is_a).map(&:to_sym)
78
+
79
+ PLURAL = [:synonym, :xref, :consider, :is_a]
80
+ def initialize
81
+ PLURAL.each {|k| self.send("#{k}=", []) }
82
+ self
83
+ end
84
+
85
+ def inspect
86
+ "<[#{level}]#{@id}: #{@name} is_a.size=#{@is_a.size}>"
87
+ end
88
+
89
+ # starting with that term, traverses upwards in the tree
90
+ def each(&block)
91
+ block.call(self)
92
+ is_a.each do |term|
93
+ term.each(&block)
94
+ end
95
+ end
96
+
97
+ # returns a unique array of go terms at that level
98
+ def trace_to_level(n=1)
99
+ if self.level == n
100
+ [self]
101
+ elsif n > self.level
102
+ []
103
+ else
104
+ self.is_a.map {|anc| anc.trace_to_level(n) }.flatten.uniq
105
+ end
106
+ end
107
+
108
+ # returns the number of levels below the top (top 3 categories [mf, bp,
109
+ # cc] are at level 0)
110
+ def find_level
111
+ if @level ; @level
112
+ else
113
+ @level =
114
+ if @is_a.size == 0 ; 0
115
+ else
116
+ @is_a.map {|term| term.find_level }.min + 1
117
+ end
118
+ end
119
+ end
120
+ end
121
+
122
+ # subsetdefs is an array, other is a hash for any key/value pairs not
123
+ # already defined here
124
+ Header = Struct.new( *%w(format-version date saved-by auto-generated-by synonymtypedef systematic_synonym default-namespace remark ontology subsetdefs other).map(&:to_sym) )
125
+ class Header
126
+ def initialize(*args)
127
+ super(*args)
128
+ self.subsetdefs = []
129
+ self.other = {}
130
+ self
131
+ end
132
+ end
133
+ end
134
+
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe "GeneOntology" do
4
+ it "fails" do
5
+ should.flunk "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bacon'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'gene_ontology'
7
+
8
+ Bacon.summary_on_exit
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gene_ontology
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - John T. Prince
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-24 00:00:00 -06:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: spec-more
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 1.0.0
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: jeweler
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 1.5.2
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rcov
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ type: :development
59
+ version_requirements: *id004
60
+ description: |-
61
+ Parses gene ontology (GO) .obo files, links terms through is_a and provides methods
62
+ to find levels and traverse the tree.
63
+ email: jtprince@gmail.com
64
+ executables: []
65
+
66
+ extensions: []
67
+
68
+ extra_rdoc_files:
69
+ - LICENSE
70
+ - README.rdoc
71
+ files:
72
+ - .document
73
+ - LICENSE
74
+ - README.rdoc
75
+ - Rakefile
76
+ - VERSION
77
+ - lib/gene_ontology.rb
78
+ - spec/gene_ontology_spec.rb
79
+ - spec/spec_helper.rb
80
+ has_rdoc: true
81
+ homepage: http://github.com/princelab/gene_ontology
82
+ licenses:
83
+ - MIT
84
+ post_install_message:
85
+ rdoc_options: []
86
+
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ requirements: []
102
+
103
+ rubyforge_project:
104
+ rubygems_version: 1.6.2
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: parses gene ontology (GO) .obo files and provides tree traversal methods
108
+ test_files:
109
+ - spec/gene_ontology_spec.rb
110
+ - spec/spec_helper.rb