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