Class: KingdomDB
- Inherits:
-
Object
- Object
- KingdomDB
- Defined in:
- lib/kingdom_db.rb
Constant Summary collapse
- ROOT_ID =
"1"
- SCIENTIFIC_NAME =
"scientific name"
- DEFAULT_FILTER =
[ "Bacteria", "Archaea", "Viruses", "NONE" ]
Instance Method Summary collapse
- #get_filter(name_array) ⇒ Object
- #id_from_gi(gi_number) ⇒ Object
- #id_from_name(taxon_name) ⇒ Object
-
#initialize(server, user, password, database) ⇒ KingdomDB
constructor
A new instance of KingdomDB.
- #match_filter(taxon_name, filter_hash) ⇒ Object
- #name_from_gi(gi_number) ⇒ Object
- #name_from_id(taxon_id) ⇒ Object
- #node_rank_from_id(taxon_id) ⇒ Object
- #parent_id_from_id(taxon_id) ⇒ Object
Constructor Details
#initialize(server, user, password, database) ⇒ KingdomDB
Returns a new instance of KingdomDB.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/kingdom_db.rb', line 14 def initialize(server, user, password, database) connect_string = 'mysql2://'+ server + '/' + database + '?user=' + user if !password.nil? connect_string = connect_string + '&password=' + password end if !defined?(RUBY_ENGINE) #This is most likey 1.8.7 else if RUBY_ENGINE == 'jruby' #This is JRuby, using jdbc connect_string = 'jdbc:' + connect_string end end @database = Sequel.connect(connect_string) @filter_hit_cache = {} end |
Instance Method Details
#get_filter(name_array) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/kingdom_db.rb', line 111 def get_filter(name_array) filter_hash = Hash[name_array.collect { |taxon_name| [taxon_name, id_from_name(taxon_name)] }] return filter_hash end |
#id_from_gi(gi_number) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/kingdom_db.rb', line 94 def id_from_gi(gi_number) db_results = @database[:proteingiToTaxonId].filter(:gi => gi_number).map(:taxonid) if db_results.size == 0 raise("No results for gi " + gi_number.to_s) elsif db_results.size > 1 raise("Results not unique: " + db_results.inspect) else return db_results[0].to_s end end |
#id_from_name(taxon_name) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/kingdom_db.rb', line 36 def id_from_name(taxon_name) db_results = @database[:names].select(:taxonid, :class).filter(:name => taxon_name).all if db_results.size > 1 #If we get more than one result, check if we got a scientific name db_results.delete_if{|x| x[:class] != SCIENTIFIC_NAME} end if db_results.size > 1 raise("Results not unique: " + db_results.inspect) end if db_results.size == 0 raise("No results for taxon name " + taxon_name.to_s) end return db_results[0][:taxonid].to_s end |
#match_filter(taxon_name, filter_hash) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/kingdom_db.rb', line 118 def match_filter(taxon_name, filter_hash) current_species_id = id_from_name(taxon_name) history = [] while ((current_species_id.to_i > ROOT_ID.to_i)&&(!filter_hash.has_value?(current_species_id))) if @filter_hit_cache.has_key? current_species_id #Cache hit current_species_id = @filter_hit_cache[current_species_id] break else parent_id = parent_id_from_id(current_species_id) history << current_species_id current_species_id = parent_id end end history.each { |i| @filter_hit_cache[i] = current_species_id } if current_species_id == ROOT_ID return nil else return name_from_id(current_species_id) end end |
#name_from_gi(gi_number) ⇒ Object
106 107 108 109 |
# File 'lib/kingdom_db.rb', line 106 def name_from_gi(gi_number) taxonid = id_from_gi(gi_number) name_from_id(taxonid) end |
#name_from_id(taxon_id) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/kingdom_db.rb', line 55 def name_from_id(taxon_id) db_results = @database[:names].filter(:taxonid => taxon_id.to_s, :class => SCIENTIFIC_NAME).map(:name) if db_results.size == 0 raise("No results for taxon id " + taxon_id.to_s) elsif db_results.size > 1 raise("Results not unique: " + db_results.inspect) else return db_results[0] end end |
#node_rank_from_id(taxon_id) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/kingdom_db.rb', line 80 def node_rank_from_id(taxon_id) db_results = @database[:nodes].filter(:taxonid => taxon_id.to_s).map(:rank) if db_results.size == 0 raise("No results for taxon id " + taxon_id.to_s) elsif db_results.size > 1 raise("Results not unique: " + db_results.inspect) else return db_results[0].to_s end end |
#parent_id_from_id(taxon_id) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/kingdom_db.rb', line 67 def parent_id_from_id(taxon_id) db_results = @database[:nodes].filter(:taxonid => taxon_id.to_s).map(:parenttaxonid) if db_results.size == 0 raise("No results for taxon id " + taxon_id.to_s) elsif db_results.size > 1 raise("Results not unique: " + db_results.inspect) else return db_results[0].to_s end end |