001/**
002 * Copyright (C) 2010-2015 The Roslin Institute <contact andy.law@roslin.ed.ac.uk>
003 *
004 * This file is part of JEnsembl: a Java API to Ensembl data sources developed by the
005 * Bioinformatics Group at The Roslin Institute, The Royal (Dick) School of
006 * Veterinary Studies, University of Edinburgh.
007 *
008 * Project hosted at: http://jensembl.sourceforge.net
009 *
010 * This is free software: you can redistribute it and/or modify
011 * it under the terms of the GNU General Public License (version 3) as published by
012 * the Free Software Foundation.
013 *
014 * This software is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 *
019 * You should have received a copy of the GNU General Public License
020 * in this software distribution. If not, see: http://opensource.org/licenses/gpl-3.0.html
021 */
022package uk.ac.roslin.ensembl.dao.database;
023
024import java.util.*;
025import uk.ac.roslin.ensembl.config.EnsemblDBType;
026import uk.ac.roslin.ensembl.datasourceaware.core.*;
027import uk.ac.roslin.ensembl.exception.BacterialCollectionException;
028import uk.ac.roslin.ensembl.exception.ConfigurationException;
029import uk.ac.roslin.ensembl.exception.DAOException;
030import uk.ac.roslin.ensembl.model.core.CollectionSpecies;
031import uk.ac.roslin.ensembl.model.database.Database;
032import uk.ac.roslin.ensembl.model.database.DatabaseType;
033
034
035public class DBCollectionSpecies extends DBSpecies implements CollectionSpecies {
036  
037    private DBCollectionCoreDatabase mostRecentCoreDatabase = null;
038
039    private TreeSet<DBCollectionDatabase> databases = new TreeSet<DBCollectionDatabase>();
040
041    protected TreeMap<String,Integer> versionIDmap = new TreeMap<String,Integer>();
042
043    public DBCollectionSpecies() {
044        dbType = EnsemblDBType.collection_core;
045    }
046
047    public DBCollectionSpecies(DBCollectionCoreDatabase database) throws ConfigurationException {
048        this();
049        this.addDatabase(database);
050        this.registry =  database.getRegistry();
051        this.mostRecentEnsemblSchemaVersion = registry.getMostRecentEnsemblVersion();
052     }
053
054    public void setIDForVersion(Integer ID, String version) {
055        this.versionIDmap.put(version, ID);
056    }
057    
058    //different logic to DBSpecies
059    @Override
060    public void setProperty(HashMap row) {
061        boolean oldStyleKeys = true;
062
063        String key = (String) row.get("meta_key");
064        String value = (String) row.get("meta_value");
065        Integer schemaVersion = Integer.parseInt((String) row.get("schemaVersion"));
066        String dbVersion = (String) row.get("dbVersion");
067        Integer dbV = 0;
068        try {
069                dbV = Integer.parseInt(dbVersion);
070        } catch (Exception e) {}
071        
072        //making aliases all lower case now
073        if (key.startsWith("species.") 
074                && ! key.equals("species.division")
075                    && ! key.equals("species.stable_id_prefix")) {
076            aliases.add(value.toLowerCase());
077        }
078                
079        if (schemaVersion != null && schemaVersion >= 58) {
080            oldStyleKeys = false;
081        }
082        //pre dbVersion 58 the db_name held the binomial
083        //and compara_name, file_name and sql_name = the db style name
084        //this is a mess
085
086        if (oldStyleKeys) {
087            if (key.equals("species.db_name")) {
088                this.speciesBinomial = value;
089
090            } else if (key.equals("species.compara_name")) {
091                this.databaseStyleSpeciesName = value;
092                this.setDatabaseStyleSpeciesName(dbV,value);
093                this.comparaNameByVersion.put(dbVersion ,value);
094            } 
095            //if we dont have a comparaname use the sql name
096            else if (key.equals("species.sql_name") 
097                    && 
098                    (this.databaseStyleSpeciesName==null || this.databaseStyleSpeciesName.isEmpty())) {
099                this.databaseStyleSpeciesName = value;
100                this.setDatabaseStyleSpeciesName(dbV,value);
101                this.comparaNameByVersion.put(dbVersion ,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        //post schema 67  The following meta keys are retired
108        //species.short_name
109        //species.ensembl_common_name
110        //species.ensembl_alias_name
111        //The following meta keys will be added
112        //species.url
113        //species.display_name
114         
115        } else {
116            
117            //in release 24/77 they seemed to drop species.common_name,
118            //species.ensembl_alias_name, species.short_name
119            
120            if (key.equals("species.production_name")) {
121                this.databaseStyleSpeciesName = value;
122                this.setDatabaseStyleSpeciesName(dbV,value);
123                this.comparaNameByVersion.put(dbVersion ,value);
124
125            } else if (key.equals("species.scientific_name")) {
126                this.speciesBinomial = value;
127            } else if (key.equals("species.short_name")
128                    || key.equals("species.display_name")) {
129                this.shortName = value;
130                if (schemaVersion>76) {
131                    this.commonName = value;
132                }
133            } else if (key.equals("species.division")) {
134                this.setComparaDivision(value);
135            } else if (key.equals("species.url")) {
136                    this.setUrlName(value);
137            } else if (key.equals("assembly.accession")) {
138                      this.setAssemblyAccession(dbVersion, value);                      
139            } else if (key.equals("assembly.name")) {
140                     this.setAssemblyName(dbVersion, value);   
141            }
142        }
143
144
145        if (key.equals("species.stable_id_prefix")) {
146            this.ensemblStablePrefix = value;
147        } else if (key.equals("species.common_name")
148            || key.equals("species.ensembl_common_name")) {
149            this.commonName = value;
150        } else if (key.equals("species.taxonomy_id")) {
151            this.taxonomyID = value;
152        } 
153
154    }
155
156
157    @Override
158    public Integer getDBSpeciesID(String version) {
159        if (version == null || version.isEmpty()) {
160            if (this.highestDBRelease!=null && this.highestDBRelease!=0 ) {
161                 version = this.highestDBRelease.toString();  
162            } else {
163                return null;
164            }
165        }
166        if (!this.highestDBRelease.toString().equals(version)) {
167            throw new BacterialCollectionException(
168                    "The Bacterial Registry can now only handle a single database release. "
169                    +"This registry is configured with release:"+this.highestDBRelease);
170        }        
171        return this.versionIDmap.get(version);
172    }
173    
174    
175    @Deprecated //for CollectionSpecies
176    @Override
177    public Integer getDBSpeciesID() {
178        return this.versionIDmap.get(this.highestDBRelease.toString());
179    }
180
181    @Override
182    public int compareTo(Object o) {
183        
184        //now that this uses databaseStyleSpeciesNamewe want to try and make sure there always is one
185        //when we parse the species meta data
186        
187        //also have a fallback to use the binomial where we have an empty dbstyle name
188        
189        if (o==null) {
190            throw new NullPointerException();
191        }
192        CollectionSpecies sp = (CollectionSpecies) o;
193        
194        if (this.databaseStyleSpeciesName!=null && !this.databaseStyleSpeciesName.isEmpty()
195                && sp.getDatabaseStyleName()!=null && !sp.getDatabaseStyleName().isEmpty()) {
196            return this.databaseStyleSpeciesName.compareTo(sp.getDatabaseStyleName());
197        } else {
198            return this.getSpeciesBinomial().compareTo(sp.getSpeciesBinomial());
199        }
200
201    }
202
203    @Override
204    public int hashCode() {
205        int hash = 7;
206        hash = 79 * hash + ( (this.databaseStyleSpeciesName != null && !this.databaseStyleSpeciesName.isEmpty())
207                ? this.databaseStyleSpeciesName.hashCode() :                
208                (this.toString()!=null) ? this.toString().hashCode() : 0);
209        return hash;
210    }
211
212    
213    @Override
214    public boolean equals(Object o) {
215        return ( o != null && getClass() == o.getClass() && this.compareTo(o)==0 );
216    }
217
218    @Override
219    public DBCollectionCoreDatabase getMostRecentCoreDatabase() {
220        return (DBCollectionCoreDatabase) this.mostRecentCoreDatabase;
221    }
222
223    public void setMostRecentCoreDatabase(DBCollectionCoreDatabase currentCoreDatabase) {
224        this.mostRecentCoreDatabase = currentCoreDatabase;
225    }
226
227    @Override
228    public TreeSet<DBCollectionDatabase> getDatabases() {
229        return this.databases;
230    }
231
232    @Override
233    public void addDatabases(TreeSet<? extends Database> databases) {
234        for (Database d : databases) {
235                this.addDatabase( d);
236        }
237    }
238
239    @Override
240    public void addDatabase(Database database) {
241            try {
242                this.databases.add((DBCollectionDatabase) database);
243                if( database.getType().equals(dbType)) {
244                    this.dbVersions.add(database.getDBVersion());
245                    this.schemaVersions.add(database.getSchemaVersion());
246                   //check that we are not adding a more recent release than we know about
247                    if ( database.getIntDBVersion() > this.highestDBRelease ) {
248                        this.highestDBRelease = database.getIntDBVersion(); 
249                        this.mostRecentCoreDatabase = (DBCollectionCoreDatabase) database;
250                    }
251                }
252        } catch (Exception e) {
253        }
254    }
255
256    @Override
257    public TreeSet<DBCollectionDatabase> getDatabasesByType(DatabaseType type) {
258        TreeSet<DBCollectionDatabase> out = new TreeSet<DBCollectionDatabase>();
259        for (DBCollectionDatabase d : databases) {
260            if (d.getType()==type) {
261                out.add(d);
262            }
263        }
264        return out;
265    }
266
267    @Override
268    public TreeSet<DBCollectionDatabase> getDatabasesByVersion(String version) {       
269        if (!this.highestDBRelease.toString().equals(version)) {
270            throw new BacterialCollectionException(
271                    "The Bacterial Registry can now only handle a single database release. "
272                    +"This registry is configured with release:"+this.highestDBRelease);
273        }
274        TreeSet<DBCollectionDatabase> out = new TreeSet<DBCollectionDatabase>();
275        for (DBCollectionDatabase d : databases) {
276            if (d.getDBVersion().equals(version)) {
277                out.add(d);
278            }
279        }
280        return out;
281    }
282
283    @Override
284    public DBCollectionDatabase getDatabaseByTypeAndVersion(DatabaseType type, String version) {
285        if (!this.highestDBRelease.toString().equals(version)) {
286            throw new BacterialCollectionException(
287                    "The Bacterial Registry can now only handle a single database release. "
288                    +"This registry is configured with release:"+this.highestDBRelease);
289        }
290         for (DBCollectionDatabase d : databases) {
291            if (d.getDBVersion().equalsIgnoreCase(version) && d.getType()== type) {
292               return d;
293            }
294        }
295        return null;
296    }
297
298
299    @Override
300    public DAChromosome getChromosomeByName(String name, String dbVersion) throws DAOException {
301     
302        DAChromosome chr = null;
303
304        if (dbVersion == null || dbVersion.isEmpty()) {
305            dbVersion = this.highestDBRelease.toString();
306        }
307        if (!this.highestDBRelease.toString().equals(dbVersion)) {
308            throw new BacterialCollectionException(
309                    "The Bacterial Registry can now only handle a single database release. "
310                    +"This registry is configured with release:"+this.highestDBRelease);
311        }
312        if (!this.versionIDmap.containsKey(dbVersion)) {
313            throw new DAOException("No version "+dbVersion+" for "+this.toString());
314        }
315
316
317        if (!chromosomes.containsKey(dbVersion)) {
318            chromosomes.put(dbVersion, new TreeMap<String, DAChromosome>());
319        }
320
321        if (chromosomes.get(dbVersion).containsKey(name)) {
322            return chromosomes.get(dbVersion).get(name);
323        } else {
324
325            chr = ((DBCollectionCoreDatabase) this.getDatabaseByTypeAndVersion
326                (EnsemblDBType.collection_core, dbVersion) )
327                    .getChromosomeByName(this, name);
328        }
329
330        if (chr != null) {
331            //make sure we use the actual CHromosome Name - not the search Name
332            chromosomes.get(dbVersion).put(chr.getChromosomeName(), chr);
333        }
334
335        return chr;
336    }
337
338    @Override
339    public TreeMap<String, DAChromosome> getChromosomes(String dbVersion) throws DAOException {
340
341        if (dbVersion == null || dbVersion.isEmpty()) {
342            dbVersion = this.highestDBRelease.toString();
343        }
344        if (!this.highestDBRelease.toString().equals(dbVersion)) {
345            throw new BacterialCollectionException(
346                    "The Bacterial Registry can now only handle a single database release. "
347                    +"This registry is configured with release:"+this.highestDBRelease);
348        }        
349
350        if (!this.versionIDmap.containsKey(dbVersion)) {
351            throw new DAOException("No version "+dbVersion+" for "+this.toString());
352        }
353
354        if (this.allChromosomesRetrieved.contains(dbVersion)) {
355            return this.chromosomes.get(dbVersion);
356        }
357
358        List<DAChromosome> chrs = null;
359
360        if (!this.chromosomes.containsKey(dbVersion)) {
361            this.chromosomes.put(dbVersion, new TreeMap<String, DAChromosome>());
362        }
363
364        chrs = ((DBCollectionCoreDatabase) this.getDatabaseByTypeAndVersion(EnsemblDBType.collection_core, dbVersion)).getChromosomes(this);
365
366        this.allChromosomesRetrieved.add(dbVersion);
367
368        if (chrs != null) {
369            for (DAChromosome c : chrs) {
370                if (!chromosomes.get(dbVersion).containsKey(c.getChromosomeName())) {
371                    chromosomes.get(dbVersion).put(c.getChromosomeName(), c);
372                }
373            }
374        }
375
376        return this.chromosomes.get(dbVersion);
377    }
378
379    @Override
380    public DAAssembledDNASequence getFragmentByName(String name, String dbVersion) throws DAOException {
381        DAAssembledDNASequence ass = null;
382
383        if (dbVersion == null || dbVersion.isEmpty()) {
384            dbVersion = this.highestDBRelease.toString();
385        }
386        if (!this.highestDBRelease.toString().equals(dbVersion)) {
387            throw new BacterialCollectionException(
388                    "The Bacterial Registry can now only handle a single database release. "
389                    +"This registry is configured with release:"+this.highestDBRelease);
390        }
391        if (!this.versionIDmap.containsKey(dbVersion)) {
392            throw new DAOException("No version "+dbVersion+" for "+this.toString());
393        }
394
395
396        if (!fragments.containsKey(dbVersion)) {
397            fragments.put(dbVersion, new TreeMap<String, DAAssembledDNASequence>());
398        }
399
400        if (fragments.get(dbVersion).containsKey(name)) {
401            return fragments.get(dbVersion).get(name);
402        } else {
403
404            ass = ((DBCollectionCoreDatabase) this.getDatabaseByTypeAndVersion
405                (EnsemblDBType.collection_core, dbVersion) )
406                    .getFragmentByName(this, name);
407        }
408
409        if (ass != null) {
410            //make sure we use the actual CHromosome Name - not the search Name
411            fragments.get(dbVersion).put(ass.getName(), ass);
412        }
413
414        return ass;
415    }
416
417    @Override
418    public TreeMap<String, DAAssembledDNASequence> getFragments(String dbVersion) throws DAOException {
419        if (dbVersion == null || dbVersion.isEmpty()) {
420            dbVersion = this.highestDBRelease.toString();
421        }
422        if (!this.highestDBRelease.toString().equals(dbVersion)) {
423            throw new BacterialCollectionException(
424                    "The Bacterial Registry can now only handle a single database release. "
425                    +"This registry is configured with release:"+this.highestDBRelease);
426        }
427        if (!this.versionIDmap.containsKey(dbVersion)) {
428            throw new DAOException("No version "+dbVersion+" for "+this.toString());
429        }
430
431        if (this.allFragmentsRetrieved.contains(dbVersion)) {
432            return this.fragments.get(dbVersion);
433        }
434
435        List<DAAssembledDNASequence> ass = null;
436
437        if (!this.fragments.containsKey(dbVersion)) {
438            this.fragments.put(dbVersion, new TreeMap<String, DAAssembledDNASequence>());
439        }
440
441        ass = ((DBCollectionCoreDatabase) this.getDatabaseByTypeAndVersion(EnsemblDBType.collection_core, dbVersion)).getFragments(this);
442
443        this.allFragmentsRetrieved.add(dbVersion);
444
445        if (ass != null) {
446            for (DAAssembledDNASequence a : ass) {
447                if (!fragments.get(dbVersion).containsKey(a.getName())) {
448                    fragments.get(dbVersion).put(a.getName(), a);
449                }
450            }
451        }
452
453        return this.fragments.get(dbVersion);
454    }
455    
456    /**
457     * Method returns a List of Transcripts that have been mapped to the given CCDS Identifier 
458     * (This may be the CCDS accession or the CCDS accession.schemaVersion).
459     * There may be  more than one Ensembl Transcript returned.
460     * CCDS currently curates consensus canonical transcripts for human and mouse. Calling this method
461     * on EnsemblGenomes (invertebrate) species will return an empty list by default
462     * via the wrapped TranscriptDAO method). 
463     * @param ccdsID
464     * @param dbVersion
465     * 
466     * @throws DAOException 
467     */
468    @Override
469    public List<DATranscript> getTranscriptsForCcdsID(String ccdsID, String dbVersion) throws DAOException {
470        List<DATranscript> transcripts = new ArrayList<DATranscript>();
471
472        if (ccdsID==null || ccdsID.isEmpty()) {
473            return transcripts;
474        }
475
476        if (dbVersion == null || dbVersion.isEmpty()) {
477            if (this.highestDBRelease!=null && highestDBRelease!=0) {
478                 dbVersion = this.highestDBRelease.toString();  
479            } else {
480                return transcripts;
481            }
482        }
483        if (!this.highestDBRelease.toString().equals(dbVersion)) {
484            throw new BacterialCollectionException(
485                    "The Bacterial Registry can now only handle a single database release. "
486                    +"This registry is configured with release:"+this.highestDBRelease);
487        }        
488
489        try {
490            transcripts = (List<DATranscript>) ((DBCollectionCoreDatabase) this.getDatabaseByTypeAndVersion(
491                EnsemblDBType.collection_core, dbVersion)).getCoreFactory(this).getTranscriptDAO().getTranscriptsForCcdsID(ccdsID.trim());
492        } catch (Exception e) {
493            if (e instanceof DAOException) {
494                throw (DAOException) e;
495            } else {
496                throw new DAOException(e);
497            }
498        }
499
500        return transcripts;
501    }
502    
503    /**
504     * Method returns a List of Transcripts that have been mapped to the given Vega Identifier.
505     * (there may be  more than one Ensembl Transcript).
506     * Vega currently curates gene, transcript and protein annotations for a human 
507     * and and a few key regions of other vertebrate species. Calling this method
508     * on EnsemblGenomes (invertebrate) species will return an empty list by default
509     * Via the wrapped TranscriptDAO method). 
510     * @param vegaID
511     * @param dbVersion
512     * 
513     * @throws DAOException 
514     */
515    @Override
516    public List<DATranscript> getTranscriptsForVegaID(String vegaID, String dbVersion) throws DAOException {
517        List<DATranscript> transcripts = new ArrayList<DATranscript>();
518
519        if (vegaID==null || vegaID.isEmpty()) {
520            return transcripts;
521        }
522
523        if (dbVersion == null || dbVersion.isEmpty()) {
524            if (this.highestDBRelease!=null && highestDBRelease!=0) {
525                 dbVersion = this.highestDBRelease.toString();  
526            } else {
527                return transcripts;
528            }
529        }
530        if (!this.highestDBRelease.toString().equals(dbVersion)) {
531            throw new BacterialCollectionException(
532                    "The Bacterial Registry can now only handle a single database release. "
533                    +"This registry is configured with release:"+this.highestDBRelease);
534        }          
535
536        try {
537            transcripts = (List<DATranscript>) ((DBCollectionCoreDatabase) this.getDatabaseByTypeAndVersion(
538                EnsemblDBType.collection_core, dbVersion)).getCoreFactory(this).getTranscriptDAO().getTranscriptsForVegaID(vegaID.trim());
539        } catch (Exception e) {
540            if (e instanceof DAOException) {
541                throw (DAOException) e;
542            } else {
543                throw new DAOException(e);
544            }
545        }
546
547        return transcripts;
548    }
549       
550    /**
551     * Method returns a List of Translations that have been mapped to the given Vega Identifier.
552     * (there may be  more than one Ensembl Translation).
553     * Vega currently curates gene, transcript and protein annotations for a human 
554     * and and a few key regions of other vertebrate species. Calling this method
555     * on EnsemblGenomes (invertebrate) species will return an empty list by default
556     * Via the wrapped TranslationDAO method). 
557     * @param vegaID
558     * @param dbVersion
559     * 
560     * @throws DAOException 
561     */
562    @Override
563    public List<DATranslation> getTranslationsForVegaID(String vegaID, String dbVersion) throws DAOException {
564        List<DATranslation> translations = new ArrayList<DATranslation>();
565        if (vegaID==null || vegaID.isEmpty()) {
566            return translations;
567        }
568
569        if (dbVersion == null || dbVersion.isEmpty()) {
570            if (this.highestDBRelease!=null && highestDBRelease!=0) {
571                 dbVersion = this.highestDBRelease.toString();  
572            } else {
573                return translations;
574            }
575        }
576        if (!this.highestDBRelease.toString().equals(dbVersion)) {
577            throw new BacterialCollectionException(
578                    "The Bacterial Registry can now only handle a single database release. "
579                    +"This registry is configured with release:"+this.highestDBRelease);
580        }  
581
582        try {
583            translations = (List<DATranslation>) ((DBCollectionCoreDatabase) this.getDatabaseByTypeAndVersion(
584                EnsemblDBType.collection_core, dbVersion)).getCoreFactory(this).getTranslationDAO().getTranslationsForVegaID(vegaID.trim());
585        } catch (Exception e) {
586            if (e instanceof DAOException) {
587                throw (DAOException) e;
588            } else {
589                throw new DAOException(e);
590            }
591        }
592
593        return translations;
594    }
595       
596    /**
597     * Method returns a List of Genes that have been mapped to the given Vega Identifier.
598     * (there may be  more than one Ensembl Gene).
599     * Vega currently curates gene, transcript and protein annotations for a human 
600     * and and a few key regions of other vertebrate species. Calling this method
601     * on EnsemblGenomes (invertebrate) species will return an empty list by default
602     * Via the wrapped GeneDAO method). 
603     * @param vegaID
604     * @param dbVersion
605     * 
606     * @throws DAOException 
607     */
608    @Override
609    public List<DAGene> getGenesForVegaID(String vegaID, String dbVersion) throws DAOException {
610        List<DAGene> genes = new ArrayList<DAGene>();
611
612        if (vegaID==null || vegaID.isEmpty()) {
613            return genes;
614        }
615
616        if (dbVersion == null || dbVersion.isEmpty()) {
617            if (this.highestDBRelease!=null && highestDBRelease!=0) {
618                 dbVersion = this.highestDBRelease.toString();  
619            } else {
620                return genes;
621            }
622        }
623        if (!this.highestDBRelease.toString().equals(dbVersion)) {
624            throw new BacterialCollectionException(
625                    "The Bacterial Registry can now only handle a single database release. "
626                    +"This registry is configured with release:"+this.highestDBRelease);
627        }  
628        try {
629            genes = (List<DAGene>) ((DBCollectionCoreDatabase) this.getDatabaseByTypeAndVersion(
630                EnsemblDBType.collection_core, dbVersion)).getCoreFactory(this).getGeneDAO().getGenesForVegaID(vegaID.trim());
631        } catch (Exception e) {
632            if (e instanceof DAOException) {
633                throw (DAOException) e;
634            } else {
635                throw new DAOException(e);
636            }
637        }
638
639        return genes;
640    }
641
642    @Override
643    public DAGene getGeneByStableID(String stableID, String dbVersion) throws DAOException {
644
645        DAGene gene = null;
646
647        if (stableID==null || stableID.isEmpty()) {
648            return null;
649        }
650
651        if (dbVersion == null || dbVersion.isEmpty()) {
652            if (this.highestDBRelease!=null && highestDBRelease!=0) {
653                 dbVersion = this.highestDBRelease.toString();  
654            } else {
655                return null;
656            }
657        }
658        if (!this.highestDBRelease.toString().equals(dbVersion)) {
659            throw new BacterialCollectionException(
660                    "The Bacterial Registry can now only handle a single database release. "
661                    +"This registry is configured with release:"+this.highestDBRelease);
662        }          
663
664        try {
665        gene = (DAGene)
666                ((DBCollectionCoreDatabase) this.getDatabaseByTypeAndVersion(
667                EnsemblDBType.collection_core, dbVersion)).getCoreFactory(this).getGeneDAO().getGeneByStableID(stableID.trim());
668        } catch (Exception e) {
669            if (e instanceof DAOException) {
670                throw (DAOException) e;
671            } else {
672                throw new DAOException(e);
673            }
674        }
675        return gene;
676    }
677    
678    @Override
679    public DATranscript getTranscriptByStableID(String stableID, String dbVersion) throws DAOException {
680
681        DATranscript transcript = null;
682
683        if (stableID==null || stableID.isEmpty()) {
684            return null;
685        }
686
687        if (dbVersion == null || dbVersion.isEmpty()) {
688            if (this.highestDBRelease!=null && highestDBRelease!=0) {
689                 dbVersion = this.highestDBRelease.toString();  
690            } else {
691                return null;
692            }
693        }
694        if (!this.highestDBRelease.toString().equals(dbVersion)) {
695            throw new BacterialCollectionException(
696                    "The Bacterial Registry can now only handle a single database release. "
697                    +"This registry is configured with release:"+this.highestDBRelease);
698        }  
699        try {
700        transcript = (DATranscript)
701                ((DBCollectionCoreDatabase) this.getDatabaseByTypeAndVersion(
702                EnsemblDBType.collection_core, dbVersion)).getCoreFactory(this).getTranscriptDAO().getTranscriptByStableID(stableID.trim());
703        } catch (Exception e) {
704            if (e instanceof DAOException) {
705                throw (DAOException) e;
706            } else {
707                throw new DAOException(e);
708            }
709        }
710        return transcript;
711    }
712
713    @Override
714    public DATranslation getTranslationByStableID(String stableID, String dbVersion) throws DAOException {
715
716        DATranslation translation = null;
717
718        if (stableID==null || stableID.isEmpty()) {
719            return null;
720        }
721
722        if (dbVersion == null || dbVersion.isEmpty()) {
723            if (this.highestDBRelease!=null && highestDBRelease!=0) {
724                 dbVersion = this.highestDBRelease.toString();  
725            } else {
726                return null;
727            }
728        }
729        if (!this.highestDBRelease.toString().equals(dbVersion)) {
730            throw new BacterialCollectionException(
731                    "The Bacterial Registry can now only handle a single database release. "
732                    +"This registry is configured with release:"+this.highestDBRelease);
733        }  
734        try {
735            translation = (DATranslation)
736                    ((DBCollectionCoreDatabase) this.getDatabaseByTypeAndVersion(
737                    EnsemblDBType.collection_core, dbVersion)).getCoreFactory(this).getTranslationDAO().getTranslationByStableID(stableID.trim());
738        } catch (Exception e) {
739            if (e instanceof DAOException) {
740                throw (DAOException) e;
741            } else {
742                throw new DAOException(e);
743            }
744        }
745
746        return translation;
747    }
748            
749    @Override
750    public String getAssemblyName(String dbVersion) {
751
752        String out = "";
753        if (dbVersion == null || dbVersion.isEmpty()) {
754            if (this.highestDBRelease!=null && highestDBRelease!=0 ) {
755                 dbVersion = this.highestDBRelease.toString();  
756            } else {
757                return out;
758            }
759        }
760        if (!this.highestDBRelease.toString().equals(dbVersion)) {
761            throw new BacterialCollectionException(
762                    "The Bacterial Registry can now only handle a single database release. "
763                    +"This registry is configured with release:"+this.highestDBRelease);
764        }          
765       //we no longer have a lazy load for AssemblyName  - cos this should be filled in on initialisation
766//        if (this.assemblyNamesByVersion.get(dbVersion) == null) {
767//            if (this.getDatabaseByTypeAndVersion(EnsemblDBType.collection_core, dbVersion) != null) {
768//                out = ((CollectionCoreDatabase) this.getDatabaseByTypeAndVersion(EnsemblDBType.collection_core, dbVersion)).getAssemblyName(this);
769//            }
770//            this.assemblyNamesByVersion.put(dbVersion, out);
771//        }
772
773        out = this.assemblyNamesByVersion.get(dbVersion);
774        return out == null ? "" : out;
775    }
776
777    @Override
778    public String getAssemblyAccession(String dbVersion) {
779            
780        String out = "";
781
782        if (dbVersion == null || dbVersion.isEmpty()) {
783            if (this.highestDBRelease!=null && highestDBRelease!=0) {
784                 dbVersion = this.highestDBRelease.toString();  
785            } else {
786                return out;
787            }
788        }
789        if (!this.highestDBRelease.toString().equals(dbVersion)) {
790            throw new BacterialCollectionException(
791                    "The Bacterial Registry can now only handle a single database release. "
792                    +"This registry is configured with release:"+this.highestDBRelease);
793        }  
794        //we no longer have a lazy load for AssemblyAccesion  - cos this should be filled in on initialisation
795//        if (this.assemblyAccessionsByVersion.get(dbVersion) == null) {
796//            if (this.getDatabaseByTypeAndVersion(EnsemblDBType.collection_core, dbVersion) != null) {
797//                out = ((CollectionCoreDatabase) this.getDatabaseByTypeAndVersion(EnsemblDBType.collection_core, dbVersion)).getAssemblyAccession(this);
798//            }
799//            this.assemblyAccessionsByVersion.put(dbVersion, out);
800//        }
801
802        out = this.assemblyAccessionsByVersion.get(dbVersion);
803        return out == null ? "" : out;
804    }
805    
806    @Override
807    public void setAssemblyExceptions(String version) throws DAOException {
808         //not implemented yet
809    }     
810    
811    
812    @Override
813    public List<DAGene> getGenesForExactName(String name, String dbVersion) throws DAOException {
814        //return an empty list rather than null
815        List<DAGene> out = new ArrayList<DAGene>();        
816        if (name==null || name.isEmpty() || name.startsWith("%")) {
817            return out;
818        }
819        
820        if (dbVersion == null || dbVersion.isEmpty()) {
821            if (this.highestDBRelease!=null && this.highestDBRelease!=0) {
822                 dbVersion = this.highestDBRelease.toString();  
823            } else {
824                return out;
825            }
826        }
827        if (!this.highestDBRelease.toString().equals(dbVersion)) {
828            throw new BacterialCollectionException(
829                    "The Bacterial Registry can now only handle a single database release. "
830                    +"This registry is configured with release:"+this.highestDBRelease);
831        }          
832        try {
833            out = ((DBCollectionCoreDatabase) this.getDatabaseByTypeAndVersion(
834                EnsemblDBType.collection_core, dbVersion)).getCoreFactory(this).getGeneDAO().getGenesByExactName(name.trim()); 
835        } catch (Exception e) {
836            if (e instanceof DAOException) {
837                throw (DAOException) e;
838            } else {
839                throw new DAOException(e);
840            }
841        }
842        
843        return out;
844    }
845           
846    @Override
847    public String getComparaName(String version) {
848        // unlike the case for DBSpecies, all versions of the species are parsed on Registry init()
849        // so there is no lazy load required
850        if (version == null || version.isEmpty()) {
851            if (this.highestDBRelease!=null && this.highestDBRelease!=0) {
852                 version = this.highestDBRelease.toString();  
853            } else {
854                return "";
855            }
856        }
857        if (!this.highestDBRelease.toString().equals(version)) {
858            throw new BacterialCollectionException(
859                    "The Bacterial Registry can now only handle a single database release. "
860                    +"This registry is configured with release:"+this.highestDBRelease);
861        }          
862        return (this.comparaNameByVersion.get(version)==null)?"":this.comparaNameByVersion.get(version);
863    }
864}