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 uk.ac.roslin.ensembl.config.EnsemblComparaDivision;
024    import uk.ac.roslin.ensembl.model.database.ComparisonDatabase;
025    import uk.ac.roslin.ensembl.dao.database.factory.DBDAOComparaFactory;
026    import uk.ac.roslin.ensembl.exception.ConfigurationException;
027    import uk.ac.roslin.ensembl.config.EnsemblDBType;
028    
029    /**
030     *
031     * @author paterson
032     */
033    public class DBComparisonDatabase extends DBDatabase implements ComparisonDatabase {
034    
035        private String comparisonDivisionName = "multi";
036        private EnsemblComparaDivision comparisonDivision = EnsemblComparaDivision.MULTI;
037    
038        public DBComparisonDatabase(String db_name, EnsemblDBType type, DBRegistry registry) throws ConfigurationException {
039            super(db_name, type, registry);
040            init();
041        }
042    
043        private void init() throws ConfigurationException {
044    
045            //ensembdbl are of style species_genus_type_version_build (i.e. 2 suffixes after type)
046            //ensemblgenomes are of style species_genus_type_version_ensembldbversion_build (ie 3 suffixes after type)
047    
048            //delete the leading type
049            String sub = dBName.replace(type + "_", "");
050    
051            //does suffix have >1 token
052            if (sub.contains("_")) {
053                String next = sub.substring(0, sub.indexOf("_"));
054    
055                //if we have a word here, it is a comparisonGroupName
056                if (next.matches("^[a-z]+")) {
057                    comparisonDivisionName = next;
058                    sub = sub.replace(next + "_", "");
059                    //if >1 suffix token
060                    if (sub.contains("_")) {
061    
062                        next = sub.substring(0, sub.indexOf("_"));
063                        //if double barreld comparisonGroupName name
064                        if (next.matches("^[a-z]+")) {
065                            comparisonDivisionName = comparisonDivisionName.concat("_" + next);
066                            sub = sub.replace(next + "_", "");
067                            //if >1 suffix token
068                            if (sub.contains("_")) {
069                                dbVersion = sub.substring(0,sub.indexOf("_"));
070                                intDBVersion = Integer.parseInt(dbVersion);
071                                schemaVersion = sub.replace(dbVersion + "_", "");
072                                intSchemaVersion = Integer.parseInt(schemaVersion);
073                            } else {
074                                this.dbVersion = sub;
075                                intDBVersion = Integer.parseInt(dbVersion);
076                                this.schemaVersion = dbVersion;
077                                this.intSchemaVersion = intDBVersion;
078                            }
079    
080    
081                        } else {
082                            dbVersion = next;
083                            intDBVersion = Integer.parseInt(dbVersion);
084                            schemaVersion = sub.substring(sub.indexOf("_") + 1);
085                            intSchemaVersion = Integer.parseInt(schemaVersion);
086                        }
087                    } else // only one suffix token
088                    {
089                        dbVersion = sub;
090                        intDBVersion = Integer.parseInt(dbVersion);
091                        schemaVersion = dbVersion;
092                        intSchemaVersion = intDBVersion;
093                    }
094                } else //no word so straight in to suffixes
095                {
096                    dbVersion = next;
097                    intDBVersion = Integer.parseInt(dbVersion);
098                    if (sub.contains("_")) {
099                        schemaVersion = sub.substring(sub.indexOf("_") + 1);
100                        intSchemaVersion = Integer.parseInt(schemaVersion);
101                    } else {
102                        schemaVersion = dbVersion;
103                        intSchemaVersion = intDBVersion;
104                    }
105                }
106            } else //suffix has only one token, the ensembl release
107            {
108                this.dbVersion = sub;
109                intDBVersion = Integer.parseInt(dbVersion);
110                this.schemaVersion = dbVersion;
111                this.intSchemaVersion = intDBVersion;
112            }
113    
114            this.comparisonDivision = EnsemblComparaDivision.getEnsemblComparaDivision(comparisonDivisionName);
115    
116        }
117    
118        @Override
119        public EnsemblComparaDivision getComparisonDivision() {
120            return comparisonDivision;
121        }
122    
123        public DBDAOComparaFactory getComparaFactory() {
124            if (this.factory != null) {
125                return (DBDAOComparaFactory) this.factory;
126            } else {
127                try {
128                    this.factory = new DBDAOComparaFactory(this);
129                } catch (Exception ex) {
130                }
131                return (DBDAOComparaFactory) this.factory;
132    
133            }
134        }
135    }