001 /** 002 * Copyright (C) 2010 The Roslin Institute <contact andy.law@roslin.ed.ac.uk> 003 * 004 * This file is part of the Ensembl Java API demonstration project developed by the 005 * Bioinformatics Group at The Roslin Institute, The Royal (Dick) School of 006 * Veterinary Studies, University of Edinburgh. 007 * 008 * This is free software: you can redistribute it and/or modify 009 * it under the terms of the GNU General Public License (version 3) as published by 010 * the Free Software Foundation. 011 * 012 * This software is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * in this software distribution. If not, see <http://www.gnu.org/licenses/gpl-3.0.html/>. 019 */ 020 021 package uk.ac.roslin.ensembl.dao.database; 022 023 import java.util.HashMap; 024 import java.util.List; 025 import java.util.TreeMap; 026 import java.util.TreeSet; 027 import uk.ac.roslin.ensembl.datasourceaware.core.DAChromosome; 028 import uk.ac.roslin.ensembl.datasourceaware.core.DAGene; 029 import uk.ac.roslin.ensembl.exception.DAOException; 030 import uk.ac.roslin.ensembl.model.database.Database; 031 import uk.ac.roslin.ensembl.model.database.DatabaseType; 032 import uk.ac.roslin.ensembl.model.core.CollectionSpecies; 033 import uk.ac.roslin.ensembl.config.EnsemblDBType; 034 import uk.ac.roslin.ensembl.exception.ConfigurationException; 035 import uk.ac.roslin.ensembl.model.database.CollectionCoreDatabase; 036 037 038 public class DBCollectionSpecies extends DBSpecies implements CollectionSpecies { 039 040 private DBCollectionDatabase currentCoreDatabase = null; 041 042 private TreeSet<DBCollectionDatabase> databases = new TreeSet<DBCollectionDatabase>(); 043 044 private TreeMap<String,Integer> versionIDmap = new TreeMap<String,Integer>(); 045 046 private TreeSet<String> allChromosomesRetrieved = new TreeSet<String>(); 047 048 public DBCollectionSpecies() { 049 dbType = EnsemblDBType.collection_core; 050 } 051 052 public DBCollectionSpecies(DBCollectionDatabase database) throws ConfigurationException { 053 this(); 054 this.setDatabase(database); 055 } 056 057 public void setDatabase(DBCollectionDatabase database) throws ConfigurationException{ 058 try { 059 this.databases.add(database); 060 this.registry = database.getRegistry(); 061 this.mostRecentEnsemblSchemaVersion = registry.getMostRecentEnsemblVersion(); 062 this.mostRecentDBRelease = registry.getMostRecentDBVersion(); 063 064 if (this.mostRecentDBRelease.equalsIgnoreCase(database.getDBVersion())) { 065 this.currentCoreDatabase = database; 066 } 067 } catch (Exception e) { 068 throw new ConfigurationException(e); 069 } 070 071 } 072 public void setIDForVersion(Integer ID, String version) { 073 this.versionIDmap.put(version, ID); 074 } 075 076 //different logic to DBSpecies 077 @Override 078 public void setProperty(HashMap row) { 079 080 boolean oldStyleKeys = true; 081 082 String key = (String) row.get("meta_key"); 083 String value = (String) row.get("meta_value"); 084 Integer version = Integer.parseInt((String) row.get("schemaVersion")); 085 086 if (version != null && version >= 58) { 087 oldStyleKeys = false; 088 } 089 090 //pre dbVersion 58 the db_name held the binomial 091 //and compara_name, file_name and sql_name = the db style name 092 //this is a mess 093 094 if (oldStyleKeys) { 095 if (key.equals("species.db_name")) { 096 this.speciesBinomial = value; 097 aliases.add(value); 098 //this.databaseStyleSpeciesName = value; 099 } else if (key.equals("species.compara_name")) { 100 this.databaseStyleSpeciesName = value; 101 this.comparaName = value; 102 } 103 //from dbVersion 58 the binomial is held in scientific name 104 //and compara_name, file_name,db_name, production_name and sql_name 105 //all seem to hold the db style name 106 107 } else { 108 if (key.equals("species.production_name")) { 109 this.databaseStyleSpeciesName = value; 110 this.comparaName = value; 111 aliases.add(value); 112 } else if (key.equals("species.scientific_name")) { 113 this.speciesBinomial = value; 114 aliases.add(value); 115 } else if (key.equals("species.short_name")) { 116 this.shortName = value; 117 aliases.add(value); 118 } else if (key.equals("species.division")) { 119 this.setComparaDivision(value); 120 } 121 } 122 123 124 if (key.equals("species.stable_id_prefix")) { 125 this.ensemblStablePrefix = value; 126 } else if (key.equals("species.common_name") 127 || key.equals("species.ensembl_common_name")) { 128 aliases.add(value); 129 this.commonName = value; 130 } else if (key.equals("species.taxonomy_id")) { 131 aliases.add(value); 132 this.taxonomyID = value; 133 } else if (key.equals("species.alias") 134 // || key.equals("species.ensembl_common_name") 135 || key.equals("species.file_name") 136 || key.equals("species.compara_name") 137 || key.equals("species.sql_name") 138 || key.equals("species.ensembl_alias_name")) { 139 aliases.add(value); 140 } 141 } 142 143 144 @Override 145 public Integer getDBSpeciesID(String version) { 146 return this.versionIDmap.get(version); 147 } 148 149 @Override 150 public int compareTo(CollectionSpecies o) { 151 152 if (this.getDatabaseStyleName()!=null && o != null && o.getDatabaseStyleName()!= null) { 153 return this.getDatabaseStyleName().compareTo(((DBCollectionSpecies)o).getDatabaseStyleName()); 154 } else { 155 156 if (this.getDatabaseStyleName()!=null) { 157 return 1; 158 } else { 159 return -1; 160 } 161 } 162 163 } 164 165 @Override 166 public DBCollectionCoreDatabase getCurrentCoreDatabase() { 167 return (DBCollectionCoreDatabase) this.currentCoreDatabase; 168 } 169 170 public void setCurrentCoreDatabase(DBCollectionCoreDatabase currentCoreDatabase) { 171 this.currentCoreDatabase = currentCoreDatabase; 172 } 173 174 @Override 175 public TreeSet<DBCollectionDatabase> getDatabases() { 176 return this.databases; 177 } 178 179 @Override 180 public void addDatabases(TreeSet<? extends Database> databases) { 181 for (Database d : databases) { 182 try { 183 this.databases.add((DBCollectionDatabase) d); 184 } catch (Exception e) { 185 } 186 } 187 } 188 189 @Override 190 public void addDatabase(Database database) { 191 192 try { 193 this.databases.add((DBCollectionDatabase) database); 194 } catch (Exception e) { 195 } 196 197 } 198 199 @Override 200 public TreeSet<DBCollectionDatabase> getDatabasesByType(DatabaseType type) { 201 TreeSet<DBCollectionDatabase> out = new TreeSet<DBCollectionDatabase>(); 202 for (DBCollectionDatabase d : databases) { 203 if (d.getType()==type) { 204 out.add(d); 205 } 206 } 207 return out; 208 } 209 210 @Override 211 public TreeSet<DBCollectionDatabase> getDatabasesByVersion(String version) { 212 TreeSet<DBCollectionDatabase> out = new TreeSet<DBCollectionDatabase>(); 213 for (DBCollectionDatabase d : databases) { 214 if (d.getDBVersion().equals(version)) { 215 out.add(d); 216 } 217 } 218 return out; 219 } 220 221 @Override 222 public DBCollectionDatabase getDatabaseByTypeAndVersion(DatabaseType type, String version) { 223 for (DBCollectionDatabase d : databases) { 224 if (d.getDBVersion().equalsIgnoreCase(version) && d.getType()== type) { 225 return d; 226 } 227 } 228 return null; 229 } 230 231 232 @Override 233 public DAChromosome getChromosomeByName(String name, String dbVersion) throws DAOException { 234 235 DAChromosome chr = null; 236 237 if (dbVersion == null || dbVersion.isEmpty()) { 238 dbVersion = this.mostRecentDBRelease; 239 } 240 241 if (!this.versionIDmap.containsKey(dbVersion)) { 242 throw new DAOException("No version "+dbVersion+" for "+this.toString()); 243 } 244 245 246 if (!chromosomes.containsKey(dbVersion)) { 247 chromosomes.put(dbVersion, new TreeMap<String, DAChromosome>()); 248 } 249 250 if (chromosomes.get(dbVersion).containsKey(name)) { 251 return chromosomes.get(dbVersion).get(name); 252 } else { 253 254 chr = ((DBCollectionCoreDatabase) this.getDatabaseByTypeAndVersion 255 (EnsemblDBType.collection_core, dbVersion) ) 256 .getChromosomeByName(this, name); 257 } 258 259 if (chr != null) { 260 chromosomes.get(dbVersion).put(name, chr); 261 } 262 263 return chr; 264 } 265 266 @Override 267 public TreeMap<String, DAChromosome> getChromosomes(String dbVersion) throws DAOException { 268 269 if (dbVersion == null || dbVersion.isEmpty()) { 270 dbVersion = this.mostRecentDBRelease; 271 } 272 273 if (!this.versionIDmap.containsKey(dbVersion)) { 274 throw new DAOException("No version "+dbVersion+" for "+this.toString()); 275 } 276 277 if (this.allChromosomesRetrieved.contains(dbVersion)) { 278 return this.chromosomes.get(dbVersion); 279 } 280 281 List<DAChromosome> chrs = null; 282 283 if (!this.chromosomes.containsKey(dbVersion)) { 284 this.chromosomes.put(dbVersion, new TreeMap<String, DAChromosome>()); 285 } 286 287 chrs = ((DBCollectionCoreDatabase) this.getDatabaseByTypeAndVersion(EnsemblDBType.collection_core, dbVersion)).getChromosomes(this); 288 289 this.allChromosomesRetrieved.add(dbVersion); 290 291 if (chrs != null) { 292 for (DAChromosome c : chrs) { 293 if (!chromosomes.get(dbVersion).containsKey(c.getChromosomeName())) { 294 chromosomes.get(dbVersion).put(c.getChromosomeName(), c); 295 } 296 } 297 } 298 299 return this.chromosomes.get(dbVersion); 300 } 301 302 @Override 303 public DAGene getGeneByID(Integer id, String dbVersion) throws DAOException { 304 305 DAGene gene = null; 306 307 if (dbVersion == null || dbVersion.isEmpty()) { 308 dbVersion = this.mostRecentDBRelease; 309 } 310 311 try { 312 gene = ((DBCollectionCoreDatabase) this.getDatabaseByTypeAndVersion( 313 EnsemblDBType.collection_core, dbVersion)).getCoreFactory(this).getGeneDAO().getGeneByID(id); 314 } catch (Exception e) { 315 if (e instanceof DAOException) { 316 throw (DAOException) e; 317 } else { 318 throw new DAOException(e); 319 } 320 } 321 322 return gene; 323 } 324 325 @Override 326 public DAGene getGeneByStableID(String stableID, String dbVersion) throws DAOException { 327 328 DAGene gene = null; 329 330 if (dbVersion == null || dbVersion.isEmpty()) { 331 dbVersion = this.mostRecentDBRelease; 332 } 333 334 try { 335 gene = ((DBCollectionCoreDatabase) this.getDatabaseByTypeAndVersion( 336 EnsemblDBType.collection_core, dbVersion)).getCoreFactory(this).getGeneDAO().getGeneByStableID(stableID.trim()); 337 } catch (Exception e) { 338 if (e instanceof DAOException) { 339 throw (DAOException) e; 340 } else { 341 throw new DAOException(e); 342 } 343 } 344 return gene; 345 } 346 347 @Override 348 public String getAssembly() { 349 return this.getAssembly(null); 350 } 351 352 353 @Override 354 public String getAssembly(String dbVersion) { 355 356 if (dbVersion == null || dbVersion.isEmpty()) { 357 dbVersion = mostRecentDBRelease; 358 } 359 360 if ( this.getDatabaseByTypeAndVersion(EnsemblDBType.collection_core, dbVersion)!=null) { 361 return ((CollectionCoreDatabase) 362 this.getDatabaseByTypeAndVersion(EnsemblDBType.collection_core, dbVersion)).getAssembly(this); 363 } else { 364 return null; 365 } 366 } 367 }