bio-biosql 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,257 @@
1
+ module Bio
2
+ class SQL
3
+ class Biodatabase < DummyBase
4
+ has_many :bioentries, :class_name =>"Bioentry", :foreign_key => "biodatabase_id"
5
+ validates_uniqueness_of :name
6
+ end
7
+ class BioentryDbxref < DummyBase
8
+ #delete set_sequence_name nil
9
+ set_primary_key nil #bioentry_id,dbxref_id
10
+ belongs_to :bioentry, :class_name => "Bioentry"
11
+ belongs_to :dbxref, :class_name => "Dbxref"
12
+ end
13
+
14
+ class BioentryPath < DummyBase
15
+ set_primary_key nil
16
+ #delete set_sequence_name nil
17
+ belongs_to :term, :class_name => "Term"
18
+ #da sistemare per poter procedere.
19
+ belongs_to :object_bioentry, :class_name=>"Bioentry"
20
+ belongs_to :subject_bioentry, :class_name=>"Bioentry"
21
+ end #BioentryPath
22
+
23
+ class BioentryQualifierValue < DummyBase
24
+ #NOTE: added rank to primary_keys, now it's finished.
25
+ set_primary_keys :bioentry_id, :term_id, :rank
26
+ belongs_to :bioentry, :class_name => "Bioentry"
27
+ belongs_to :term, :class_name => "Term"
28
+ end #BioentryQualifierValue
29
+
30
+ class Bioentry < DummyBase
31
+ belongs_to :biodatabase, :class_name => "Biodatabase"
32
+ belongs_to :taxon, :class_name => "Taxon"
33
+ has_one :biosequence
34
+ #, :class_name => "Biosequence", :foreign_key => "bioentry_id"
35
+ has_many :comments, :class_name =>"Comment", :order =>'rank'
36
+ has_many :seqfeatures, :class_name => "Seqfeature", :order=>'rank'
37
+ has_many :bioentry_references, :class_name=>"BioentryReference" #, :foreign_key => "bioentry_id"
38
+ has_many :bioentry_dbxrefs, :class_name => "BioentryDbxref"
39
+ has_many :object_bioentry_relationships, :class_name=>"BioentryRelationship", :foreign_key=>"object_bioentry_id" #non mi convince molto credo non funzioni nel modo corretto
40
+ has_many :subject_bioentry_relationships, :class_name=>"BioentryRelationship", :foreign_key=>"subject_bioentry_id" #non mi convince molto credo non funzioni nel modo corretto
41
+ has_many :object_bioentry_paths, :class_name=>"BioentryPath", :foreign_key=>"object_bioentry_id" #non mi convince molto credo non funzioni nel modo corretto
42
+ has_many :subject_bioentry_paths, :class_name=>"BioentryPath", :foreign_key=>"subject_bioentry_id" #non mi convince molto credo non funzioni nel modo corretto
43
+
44
+ has_many :cdsfeatures, :class_name=>"Seqfeature", :foreign_key =>"bioentry_id", :conditions=>["term.name='CDS'"], :include=>"type_term"
45
+ has_many :references, :through=>:bioentry_references, :class_name => "Reference"
46
+ has_many :terms, :through=>:bioentry_qualifier_values, :class_name => "Term"
47
+ #NOTE: added order_by for multiple hit and manage ranks correctly
48
+ has_many :bioentry_qualifier_values, :order=>"bioentry_id,term_id,rank", :class_name => "BioentryQualifierValue"
49
+
50
+ #per la creazione richiesti:
51
+ #name, accession, version
52
+ # validates_uniqueness_of :accession, :scope=>[:biodatabase_id]
53
+ # validates_uniqueness_of :name, :scope=>[:biodatabase_id]
54
+ # validates_uniqueness_of :identifier, :scope=>[:biodatabase_id]
55
+
56
+ end
57
+ class BioentryReference < DummyBase
58
+ set_primary_keys :bioentry_id, :reference_id, :rank
59
+ belongs_to :bioentry, :class_name => "Bioentry"
60
+ belongs_to :reference , :class_name => "Reference"
61
+ end
62
+ class BioentryRelationship < DummyBase
63
+ #delete set_primary_key "bioentry_relationship_id"
64
+ set_sequence_name "bieontry_relationship_pk_seq"
65
+ belongs_to :object_bioentry, :class_name => "Bioentry"
66
+ belongs_to :subject_bioentry, :class_name => "Bioentry"
67
+ belongs_to :term
68
+ end
69
+ class Biosequence < DummyBase
70
+ set_primary_keys :bioentry_id, :version
71
+ #delete set_sequence_name "biosequence_pk_seq"
72
+ belongs_to :bioentry, :foreign_key=>"bioentry_id"
73
+ #has_one :bioentry
74
+ #, :class_name => "Bioentry"
75
+ end
76
+ class Comment < DummyBase
77
+ belongs_to :bioentry, :class_name => "Bioentry"
78
+ end
79
+ class DbxrefQualifierValue < DummyBase
80
+ #think to use composite primary key
81
+ set_primary_key nil #dbxref_id, term_id, rank
82
+ #delete set_sequence_name nil
83
+ belongs_to :dbxref, :class_name => "Dbxref"
84
+ belongs_to :term, :class_name => "Term"
85
+ end
86
+ class Dbxref < DummyBase
87
+ #set_sequence_name "dbxref_pk_seq"
88
+ has_many :dbxref_qualifier_values, :class_name => "DbxrefQualifierValue"
89
+ has_many :locations, :class_name => "Location"
90
+ has_many :references, :class_name=>"Reference"
91
+ has_many :term_dbxrefs, :class_name => "TermDbxref"
92
+ has_many :bioentry_dbxrefs, :class_name => "BioentryDbxref"
93
+ #TODO: check is with bioentry there is an has_and_belongs_to_many relationship has specified in schema overview.
94
+ end
95
+ class LocationQualifierValue < DummyBase
96
+ set_primary_key nil #location_id, term_id
97
+ #delete set_sequence_name nil
98
+ belongs_to :location, :class_name => "Location"
99
+ belongs_to :term, :class_name => "Term"
100
+ end
101
+ class Location < DummyBase
102
+ #set_sequence_name "location_pk_seq"
103
+ belongs_to :seqfeature, :class_name => "Seqfeature"
104
+ belongs_to :dbxref, :class_name => "Dbxref"
105
+ belongs_to :term, :class_name => "Term"
106
+ has_many :location_qualifier_values, :class_name => "LocationQualifierValue"
107
+
108
+ def to_s
109
+ if strand==-1
110
+ str="complement("+start_pos.to_s+".."+end_pos.to_s+")"
111
+ else
112
+ str=start_pos.to_s+".."+end_pos.to_s
113
+ end
114
+ return str
115
+ end
116
+
117
+ def sequence
118
+ seq=""
119
+ unless self.seqfeature.bioentry.biosequence.seq.nil?
120
+ seq=Bio::Sequence::NA.new(self.seqfeature.bioentry.biosequence.seq[start_pos-1..end_pos-1])
121
+ seq.reverse_complement! if strand==-1
122
+ end
123
+ return seq
124
+ end
125
+
126
+
127
+
128
+ end
129
+ class Ontology < DummyBase
130
+ has_many :terms, :class_name => "Term"
131
+ has_many :term_paths, :class_name => "TermPath"
132
+ has_many :term_relationships, :class_name => "TermRelationship"
133
+ end
134
+ class Reference < DummyBase
135
+ belongs_to :dbxref, :class_name => "Dbxref"
136
+ has_many :bioentry_references, :class_name=>"BioentryReference"
137
+ has_many :bioentries, :through=>:bioentry_references
138
+ end
139
+ class SeqfeatureDbxref < DummyBase
140
+ set_primary_keys :seqfeature_id, :dbxref_id
141
+ #delete set_sequence_name nil
142
+ belongs_to :seqfeature, :class_name => "Seqfeature", :foreign_key => "seqfeature_id"
143
+ belongs_to :dbxref, :class_name => "Dbxref", :foreign_key => "dbxref_id"
144
+ end
145
+ class SeqfeaturePath < DummyBase
146
+ set_primary_keys :object_seqfeature_id, :subject_seqfeature_id, :term_id
147
+ set_sequence_name nil
148
+ belongs_to :object_seqfeature, :class_name => "Seqfeature", :foreign_key => "object_seqfeature_id"
149
+ belongs_to :subject_seqfeature, :class_name => "Seqfeature", :foreign_key => "subject_seqfeature_id"
150
+ belongs_to :term, :class_name => "Term"
151
+ end
152
+ class SeqfeatureQualifierValue < DummyBase
153
+ set_primary_keys :seqfeature_id, :term_id, :rank
154
+ set_sequence_name nil
155
+ belongs_to :seqfeature
156
+ belongs_to :term, :class_name => "Term"
157
+ end
158
+ class Seqfeature <DummyBase
159
+ set_sequence_name "seqfeature_pk_seq"
160
+ belongs_to :bioentry
161
+ #, :class_name => "Bioentry"
162
+ belongs_to :type_term, :class_name => "Term", :foreign_key => "type_term_id"
163
+ belongs_to :source_term, :class_name => "Term", :foreign_key =>"source_term_id"
164
+ has_many :seqfeature_dbxrefs, :class_name => "SeqfeatureDbxref", :foreign_key => "seqfeature_id"
165
+ has_many :seqfeature_qualifier_values, :order=>'rank', :foreign_key => "seqfeature_id"
166
+ #, :class_name => "SeqfeatureQualifierValue"
167
+ has_many :locations, :class_name => "Location", :order=>'rank'
168
+ has_many :object_seqfeature_paths, :class_name => "SeqfeaturePath", :foreign_key => "object_seqfeature_id"
169
+ has_many :subject_seqfeature_paths, :class_name => "SeqfeaturePath", :foreign_key => "subject_seqfeature_id"
170
+ has_many :object_seqfeature_relationships, :class_name => "SeqfeatureRelationship", :foreign_key => "object_seqfeature_id"
171
+ has_many :subject_seqfeature_relationships, :class_name => "SeqfeatureRelationship", :foreign_key => "subject_seqfeature_id"
172
+
173
+ #get the subsequence described by the locations objects
174
+ def sequence
175
+ return self.locations.inject(Bio::Sequence::NA.new("")){|seq, location| seq<<location.sequence}
176
+ end
177
+
178
+ #translate the subsequences represented by the feature and its locations
179
+ #not considering the qualifiers
180
+ #Return a Bio::Sequence::AA object
181
+ def translate(*args)
182
+ self.sequence.translate(*args)
183
+ end
184
+ end
185
+ class SeqfeatureRelationship <DummyBase
186
+ set_sequence_name "seqfeatue_relationship_pk_seq"
187
+ belongs_to :term, :class_name => "Term"
188
+ belongs_to :object_seqfeature, :class_name => "Seqfeature"
189
+ belongs_to :subject_seqfeature, :class_name => "Seqfeature"
190
+ end
191
+ class TaxonName < DummyBase
192
+ set_primary_keys :taxon_id, :name, :name_class
193
+ belongs_to :taxon, :class_name => "Taxon"
194
+ end
195
+ class Taxon < DummyBase
196
+ set_sequence_name "taxon_pk_seq"
197
+ has_many :taxon_names, :class_name => "TaxonName"
198
+ has_one :taxon_scientific_name, :class_name => "TaxonName", :conditions=>"name_class = 'scientific name'"
199
+ has_one :taxon_genbank_common_name, :class_name => "TaxonName", :conditions=>"name_class = 'genbank common name'"
200
+ has_one :bioentry, :class_name => "Bioentry"
201
+ end
202
+ class TermDbxref < DummyBase
203
+ set_primary_key nil #term_id, dbxref_id
204
+ #delete set_sequence_name nil
205
+ belongs_to :term, :class_name => "Term"
206
+ belongs_to :dbxref, :class_name => "Dbxref"
207
+ end
208
+ class TermPath < DummyBase
209
+ set_sequence_name "term_path_pk_seq"
210
+ belongs_to :ontology, :class_name => "Ontology"
211
+ belongs_to :subject_term, :class_name => "Term"
212
+ belongs_to :object_term, :class_name => "Term"
213
+ belongs_to :predicate_term, :class_name => "Term"
214
+ end
215
+ class Term < DummyBase
216
+ belongs_to :ontology, :class_name => "Ontology"
217
+ has_many :seqfeature_qualifier_values, :class_name => "SeqfeatureQualifierValue"
218
+ has_many :dbxref_qualifier_values, :class_name => "DbxrefQualifierValue"
219
+ has_many :bioentry_qualifer_values, :class_name => "BioentryQualifierValue"
220
+ has_many :bioentries, :through=>:bioentry_qualifier_values
221
+ has_many :locations, :class_name => "Location"
222
+ has_many :seqfeature_relationships, :class_name => "SeqfeatureRelationship"
223
+ has_many :term_dbxrefs, :class_name => "TermDbxref"
224
+ has_many :term_relationship_terms, :class_name => "TermRelationshipTerm"
225
+ has_many :term_synonyms, :class_name => "TermSynonym"
226
+ has_many :location_qualifier_values, :class_name => "LocationQualifierValue"
227
+ has_many :seqfeature_types, :class_name => "Seqfeature", :foreign_key => "type_term_id"
228
+ has_many :seqfeature_sources, :class_name => "Seqfeature", :foreign_key => "source_term_id"
229
+ has_many :term_path_subjects, :class_name => "TermPath", :foreign_key => "subject_term_id"
230
+ has_many :term_path_predicates, :class_name => "TermPath", :foreign_key => "predicate_term_id"
231
+ has_many :term_path_objects, :class_name => "TermPath", :foreign_key => "object_term_id"
232
+ has_many :term_relationship_subjects, :class_name => "TermRelationship", :foreign_key =>"subject_term_id"
233
+ has_many :term_relationship_predicates, :class_name => "TermRelationship", :foreign_key =>"predicate_term_id"
234
+ has_many :term_relationship_objects, :class_name => "TermRelationship", :foreign_key =>"object_term_id"
235
+ has_many :seqfeature_paths, :class_name => "SeqfeaturePath"
236
+ end
237
+ class TermRelationship < DummyBase
238
+ set_sequence_name "term_relationship_pk_seq"
239
+ belongs_to :ontology, :class_name => "Ontology"
240
+ belongs_to :subject_term, :class_name => "Term"
241
+ belongs_to :predicate_term, :class_name => "Term"
242
+ belongs_to :object_term, :class_name => "Term"
243
+ has_one :term_relationship_term, :class_name => "TermRelationshipTerm"
244
+ end
245
+ class TermRelationshipTerm < DummyBase
246
+ #delete set_sequence_name nil
247
+ set_primary_key :term_relationship_id
248
+ belongs_to :term_relationship, :class_name => "TermRelationship"
249
+ belongs_to :term, :class_name => "Term"
250
+ end
251
+ class TermSynonym < DummyBase
252
+ #delete set_sequence_name nil
253
+ set_primary_key nil
254
+ belongs_to :term, :class_name => "Term"
255
+ end
256
+ end #SQL
257
+ end #Bio
@@ -0,0 +1,39 @@
1
+ #require 'dm-ar-finders'
2
+ #require 'dm-core'
3
+ require 'erb'
4
+ require 'composite_primary_keys'
5
+
6
+ module Bio
7
+ class SQL
8
+ class DummyBase < ActiveRecord::Base
9
+ #NOTE: Using postgresql, not setting sequence name, system will discover the name by default.
10
+ #NOTE: this class will not establish the connection automatically
11
+ self.abstract_class = true
12
+ self.pluralize_table_names = false
13
+ #prepend table name to the usual id, avoid to specify primary id for every table
14
+ self.primary_key_prefix_type = :table_name_with_underscore
15
+ #biosql_configurations=YAML::load(ERB.new(IO.read(File.join(File.dirname(__FILE__),'./config', 'database.yml'))).result)
16
+ #self.configurations=biosql_configurations
17
+ #self.establish_connection "development"
18
+ end #DummyBase
19
+
20
+ require 'bio/io/biosql/ar-biosql'
21
+
22
+ # #no check is made
23
+ def self.establish_connection(configurations, env)
24
+ # #configurations is an hash similar what YAML returns.
25
+
26
+ #configurations.assert_valid_keys('development', 'production','test')
27
+ #configurations[env].assert_valid_keys('hostname','database','adapter','username','password')
28
+ DummyBase.configurations = configurations
29
+ connection = DummyBase.establish_connection "#{env}"
30
+ #Init of basis terms and ontologies
31
+ Ontology.first(:conditions => ["name = ?", 'Annotation Tags']) || Ontology.create({:name => 'Annotation Tags'})
32
+ Ontology.first(:conditions => ["name = ?", 'SeqFeature Keys']) || Ontology.create({:name => 'SeqFeature Keys'})
33
+ Ontology.first(:conditions => ["name = ?", 'SeqFeature Sources']) ||Ontology.create({:name => 'SeqFeature Sources'})
34
+ Term.first(:conditions => ["name = ?", 'EMBLGenBankSwit']) || Term.create({:name => 'EMBLGenBankSwit', :ontology => Ontology.first(:conditions => ["name = ?", 'SeqFeature Sources'])})
35
+ connection
36
+ end #establish_connection
37
+
38
+ end #SQL
39
+ end #Bio
@@ -0,0 +1,21 @@
1
+ #This is the database configuration specific for BioSQL
2
+ #User can configure it's db here
3
+
4
+ development:
5
+ adapter: jdbcmysql
6
+ database: bioseq
7
+ username: febo
8
+ password:
9
+ hostname: localhost
10
+
11
+ test:
12
+ adapter: postgresql
13
+ database: biorails_test
14
+ username: rails
15
+ password:
16
+
17
+ production:
18
+ adapter: postgresql
19
+ database: biorails_production
20
+ username: rails
21
+ password:
@@ -0,0 +1,79 @@
1
+ #module Bio
2
+ # class SQL
3
+ # #no check is made
4
+ # def self.establish_connection(configurations, env)
5
+ # #configurations is an hash similar what YAML returns.
6
+ # #{:database=>"biorails_development", :adapter=>"postgresql", :username=>"rails", :password=>nil}
7
+ # configurations.assert_valid_keys('development', 'production','test')
8
+ # configurations[env].assert_valid_keys('hostname','database','adapter','username','password')
9
+ # DummyBase.configurations = configurations
10
+ # DummyBase.establish_connection "#{env}"
11
+ #end
12
+
13
+
14
+ #require 'rubygems'
15
+ #require 'composite_primary_keys'
16
+ #require 'erb'
17
+ # BiosqlPlug
18
+
19
+ =begin
20
+ Ok Hilmar gives to me some clarification
21
+ 1) "EMBL/GenBank/SwissProt" name in term table, is only a convention assuming data loaded by genbank embl ans swissprot formats.
22
+ If your features come from others ways for example blast or alignment ... whatever.. the user as to take care about the source.
23
+
24
+
25
+ =end
26
+ =begin
27
+ TODO:
28
+ 1) source_term_id => surce_term and check before if the source term is present or not and the level, the root should always be something "EMBL/GenBank/SwissProt" or contestualized.
29
+ 2) Into DummyBase class delete connection there and use Bio::ArSQL.establish_connection which reads info from a yml file.
30
+ 3) Chk Locations in Biofeatures ArSQL
31
+ =end
32
+ module Bio
33
+ class SQL
34
+
35
+ require 'bio/io/biosql/biosql'
36
+ autoload :Sequence, 'bio/db/biosql/sequence'
37
+
38
+ def self.fetch_id(id)
39
+ Bio::SQL::Bioentry.find(id)
40
+ end
41
+
42
+ def self.fetch_accession(accession)
43
+ # Bio::SQL::Bioentry.exists?(:accession => accession) ? Bio::SQL::Sequence.new(:entry=>Bio::SQL::Bioentry.find_by_accession(accession)) : nil
44
+ Bio::SQL::Sequence.new(:entry=>Bio::SQL::Bioentry.find_by_accession(accession.upcase))
45
+ end
46
+
47
+ def self.exists_accession(accession)
48
+ # Bio::SQL::Bioentry.find_by_accession(accession.upcase).nil? ? false : true
49
+ !Bio::SQL::Bioentry.find_by_accession(accession.upcase).nil?
50
+ end
51
+
52
+ def self.exists_database(name)
53
+ # Bio::SQL::Biodatabase.find_by_name(name).nil? ? false : true
54
+ !Bio::SQL::Biodatabase.first(:name=>name).nil?
55
+ end
56
+
57
+ def self.list_entries
58
+ Bio::SQL::Bioentry.all.collect do|entry|
59
+ {:id=>entry.bioentry_id, :accession=>entry.accession}
60
+ end
61
+ end
62
+
63
+ def self.list_databases
64
+ Bio::SQL::Biodatabase.all.collect do|entry|
65
+ {:id=>entry.biodatabase_id, :name => entry.name}
66
+ end
67
+ end
68
+
69
+ def self.delete_entry_id(id)
70
+ Bio::SQL::Bioentry.delete(id)
71
+ end
72
+
73
+ def self.delete_entry_accession(accession)
74
+ Bio::SQL::Bioentry.find_by_accession(accession.upcase).destroy!
75
+ end
76
+
77
+ end #biosql
78
+
79
+ end #Bio
@@ -0,0 +1,86 @@
1
+ #
2
+ # test/bioruby_test_helper.rb - Helper module for testing bioruby
3
+ #
4
+ # Copyright:: Copyright (C) 2009 Naohisa Goto <ng@bioruby.org>
5
+ # License:: The Ruby License
6
+ #
7
+
8
+ require 'pathname'
9
+
10
+ unless defined? BioRubyTestDebug then
11
+ BioRubyTestDebug = ENV['BIORUBY_TEST_DEBUG'].to_s.empty? ? false : true
12
+ if BioRubyTestDebug then
13
+ $stderr.puts "BioRuby test debug enabled."
14
+ end
15
+ end #BioRubyTestDebug
16
+
17
+ unless defined? BioRubyTestGem then
18
+ gem_version = ENV['BIORUBY_TEST_GEM']
19
+ if gem_version then
20
+ $stderr.puts 'require "rubygems"' if BioRubyTestDebug
21
+ require "rubygems"
22
+ if gem_version.empty? then
23
+ $stderr.puts "gem 'bio'" if BioRubyTestDebug
24
+ gem 'bio'
25
+ else
26
+ $stderr.puts "gem 'bio', #{gem_version.inspect}" if BioRubyTestDebug
27
+ gem 'bio', gem_version
28
+ end
29
+ end
30
+ BioRubyTestGem = gem_version
31
+ end
32
+
33
+ unless defined? BioRubyTestLibPath then
34
+ libpath = ENV['BIORUBY_TEST_LIB']
35
+ unless libpath then
36
+ libpath = Pathname.new(File.join(File.dirname(__FILE__),
37
+ "..", "lib")).cleanpath.to_s
38
+ end
39
+
40
+ # do not add path to $: if BIORUBY_TEST_LIB is empty string
41
+ # or BioRubyTestGem is true.
42
+ if (libpath and libpath.empty?) or BioRubyTestGem then
43
+ libpath = nil
44
+ end
45
+
46
+ if libpath then
47
+ libpath.freeze
48
+
49
+ unless $:[0] == libpath then
50
+ $:.unshift(libpath)
51
+ if BioRubyTestDebug then
52
+ $stderr.puts "Added #{libpath.inspect} to $:."
53
+ end
54
+ else
55
+ if BioRubyTestDebug then
56
+ $stderr.puts "NOT added #{libpath.inspect} to $:. because it is already on the top of $:."
57
+ end
58
+ end
59
+ end
60
+
61
+ # (String or nil) Path to be added to $:.
62
+ # It may or may not be the path of bioruby.
63
+ BioRubyTestLibPath = libpath
64
+
65
+ if BioRubyTestDebug then
66
+ $stderr.print "$: = [", "\n"
67
+ $stderr.puts($:.collect { |x| "\t#{x.inspect}" }.join(",\n"))
68
+ $stderr.print "]", "\n"
69
+ end
70
+ end #BioRubyTestLibPath
71
+
72
+ unless defined? BioRubyTestDataPath and BioRubyTestDataPath
73
+ datapath = ENV['BIORUBY_TEST_DATA']
74
+ if datapath.to_s.empty? then
75
+ datapath = Pathname.new(File.join(File.dirname(__FILE__),
76
+ "data")).cleanpath.to_s
77
+ end
78
+ datapath.freeze
79
+
80
+ # (String) Path to the test data.
81
+ BioRubyTestDataPath = datapath
82
+
83
+ if BioRubyTestDebug then
84
+ $stderr.print "DataPath = ", BioRubyTestDataPath.inspect, "\n"
85
+ end
86
+ end