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 uk.ac.roslin.ensembl.config.EnsemblDBType;
025import uk.ac.roslin.ensembl.exception.ConfigurationException;
026import uk.ac.roslin.ensembl.model.core.Species;
027import uk.ac.roslin.ensembl.model.database.Registry;
028import uk.ac.roslin.ensembl.model.database.SingleSpeciesDatabase;
029
030/**
031 *
032 * @author tpaterso
033 */
034
035
036// will make this abstract once we fill in all the subtypes
037
038public class DBSingleSpeciesDatabase extends DBDatabase implements SingleSpeciesDatabase {
039
040     protected DBSpecies species = null;
041     protected String dbSpeciesName;
042     
043     protected String originalDBName = null;
044     protected String originalDBSpeciesName = null;
045
046    public DBSingleSpeciesDatabase(String db_name, EnsemblDBType type, Registry registry) throws ConfigurationException {
047        super(db_name, type, registry);
048        init();
049    }
050
051    private void init() throws ConfigurationException {
052
053        //ensembdbl are of style species_genus_type_version_build (i.e. 2 suffixes after type: '_release(=schema)_build')
054        //ensemblgenomes are of style species_genus_type_version_ensembldbversion_build (ie 3 suffixes after type: '_release_schema_build')
055
056        //this code will fall over if the magic conventions of db naming change!
057        //or if we have both styles in the same database!
058        //see DBRegistry line 215
059
060        this.dbSpeciesName = dBName.substring(0, dBName.indexOf("_" + this.type.toString()));
061
062        //hack for renaming species
063        //if renamining rules have been read into the  registry on configuration
064        //we look up whether this databse should be renamed - and then do it
065        this.originalDBName = dBName;
066        this.originalDBSpeciesName = dbSpeciesName;
067        if (this.registry!=null && this.registry.getRenamedDBs().containsKey(this.dbSpeciesName)) {
068            this.dbSpeciesName = this.registry.getRenamedDBs().get(this.dbSpeciesName);
069            dBName = dBName.replace(this.originalDBSpeciesName, this.dbSpeciesName);
070        }
071         
072        //WARNING: is this correct that i want to change the classifier to the new Name
073        this.dBClassifier = this.dbSpeciesName;
074              
075        String sub = dBName.replace(dbSpeciesName + "_" + type.toString() + "_", "");
076
077        //does the suffix have >1 token
078        if (sub.contains("_")) {
079            this.dbVersion = sub.substring(0, sub.indexOf("_"));
080            intDBVersion = Integer.parseInt(dbVersion);
081
082            //check if it has three suffixes
083            sub = sub.replace(dbVersion + "_", "");
084            //does suffix have >2 tokens
085            if (sub.contains("_")) {
086                this.schemaVersion = sub.substring(0, sub.indexOf("_"));
087                this.intSchemaVersion = Integer.parseInt(schemaVersion);
088                build = sub.replace(schemaVersion + "_", "");
089
090            } else //suffix has 2 tokens, therefore second is build
091            {
092                schemaVersion = dbVersion;
093                intSchemaVersion = intDBVersion;
094                build = sub;
095            }
096
097        } else //suffix has only one token, the ensembl release
098        {
099            this.dbVersion = sub;
100            intDBVersion = Integer.parseInt(dbVersion);
101            this.schemaVersion = dbVersion;
102            this.intSchemaVersion = intDBVersion;
103        }
104
105    }
106
107    @Override
108    public String getDbSpeciesName() {
109        return dbSpeciesName;
110    }
111
112    @Override
113    public void setDbSpeciesName(String species) {
114        this.dbSpeciesName = species;
115    }
116
117
118    @Override
119    public void setSpecies(Species species) {
120        this.species = (DBSpecies) species;
121    }
122
123    @Override
124    public DBSpecies getSpecies() {
125        return this.species;
126    }
127
128    /**
129     * Returns the original DBName as read from the getDatabases query.
130     * i.e. the real name of the database at the datasource without any renaming.
131     *  
132     */
133    public String getOriginalDBName() {
134        return originalDBName;
135    }
136    
137    /**
138     * Returns the real DBName as read from the getDatabases query.
139     * i.e. the real name of the database at the datasource without any renaming.
140     * Overwrites the method in the abstract parent cos if there has been
141     * renaming we want to return the original.
142     *  
143     */
144    @Override
145    public String getdBName() {
146        return originalDBName;
147    }
148    
149    /**
150     * Returns the renamed DBName if it was changed on instantiation due to a rewrite 
151     * rule in the database configuration file.
152     * i.e. if there has been a renaming this name cannot be used to  access the database.
153     *  
154     */    
155    public String getRenamedDBName() {
156        return dBName;
157    }
158
159    /**
160     * Has the database been renamed due to a rule in the database configuration file.
161     *  
162     */
163    public boolean isDBRenamed() {
164        return !dBName.equals(originalDBName);
165    }
166    
167    
168     /**
169     * Returns the original DBSpeciesName as parsed from the originalDName.
170     * (i.e. from the real name of the database at the datasource without any renaming.)
171     * However if there has been renaming this name will now only be ab alia to the species not it db-style-name.
172     *  
173     */
174    public String getOriginalDBSpeciesName() {
175        return originalDBSpeciesName;
176    }
177    
178    
179
180    
181}