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.demo;
023
024import java.util.ArrayList;
025import java.util.List;
026import uk.ac.roslin.ensembl.config.DBConnection.DataSource;
027import uk.ac.roslin.ensembl.config.EnsemblDBType;
028import uk.ac.roslin.ensembl.dao.database.*;
029import uk.ac.roslin.ensembl.model.core.ProteinFeature;
030import uk.ac.roslin.ensembl.model.database.Database;
031import uk.ac.roslin.ensembl.model.database.SingleSpeciesCoreDatabase;
032
033public class GenomesConnection {
034
035    //demonstrated connection to the EnsemblesGenomes datasource
036    //and the retrieval of species 
037    
038    //various standard operations demonstrated 
039    public static void main(String[] args) throws Exception {
040
041        DBRegistry ensemblgenomesRegistry = DBRegistry.createRegistryForDataSource(DataSource.ENSEMBLGENOMES);
042        
043        System.out.println(ensemblgenomesRegistry.getVersionReport());
044
045        System.out.println(ensemblgenomesRegistry.getBriefRegistryReport());
046        
047        System.out.println("ensemblgenomes registry report: ");
048        System.out.println("******************************* ");
049
050        //the 'standard' species in the database
051        System.out.println("all DBSpecies");
052        for (DBSpecies s : ensemblgenomesRegistry.getSpecies()) {
053            System.out.println(s.getCommonName() + "\n\t ["+s.getShortName()+"]" +" ("+s.getSpeciesBinomial()+")");
054        }
055        //bacterial species that are organised into collections - no longer retrieved by the EnsemblGenomes configuration
056        System.out.println("all DBCollectionSpecies");
057        for (DBCollectionSpecies s : ensemblgenomesRegistry.getCollectionSpecies()) {
058            System.out.println(s.getCommonName()+ "\n\t ["+s.getShortName()+"]" +" ("+s.getSpeciesBinomial()+")");
059        }
060        if (ensemblgenomesRegistry.getCollectionSpecies().isEmpty()) {
061            System.out.println("No Collections now!");
062        }
063        
064        System.out.println("...");
065
066        //get all the CollectionSpecies ( which currently are Bacteria ) 
067        //which are in the CURRENT HIGHEST release configured
068        List<DBSpecies> bactSpeciess = new ArrayList<DBSpecies>();
069        for (DBCollection c : ensemblgenomesRegistry.getCollectionsByDBVersion(
070                "" + ensemblgenomesRegistry.getHighestReleaseVersion())) {
071            bactSpeciess.addAll(c.getSpecies());
072        }
073        System.out.println("bacteria in : " + ensemblgenomesRegistry.getHighestReleaseVersion());
074        for (DBSpecies s : bactSpeciess) {
075            System.out.println(s);
076        }
077        if (bactSpeciess.isEmpty()) {
078            System.out.println("No Bacteria now!");
079        }
080        System.out.println("...");
081
082        //will print out details of each collection
083        
084        for (DBCollection c : ensemblgenomesRegistry.getCollections()) {
085            System.out.println(c.getCollectionName() + " (version " + c.getDBVersion() + ")");
086            int i = 1;
087            for (DBCollectionSpecies s : c.getSpecies()) {
088                System.out.println(i++ + ":\t" + s.getComparaName(c.getDBVersion()));
089            }
090        }
091
092
093        System.out.println("TESTING LIVE ENSEMBLGENOMES");
094
095        System.out.println("**********************************\ngrape databases\n\n");
096
097        for (Database d : ensemblgenomesRegistry.getSpeciesByAlias("grape").getDatabases()) {
098            System.out.println("\t" + d.getdBName());
099        }
100
101        SingleSpeciesCoreDatabase db = (SingleSpeciesCoreDatabase) ensemblgenomesRegistry.getDatabase("grape");
102        System.out.println("current grape top level: " + db.getTopLevelCoordSystem().getId());
103        System.out.println("current grape chromosome level: " + db.getChromosomeLevelCoordSystem().getId());
104        System.out.println("current grape sequence level: " + db.getSequenceLevelCoordSystem().getId());
105
106        String grape = "grape"; // relies on alias look up
107        int translationID = 21316;
108        System.out.println("Get features from Ensemblgenomes release 8 for grape translation id =" + translationID);
109
110        List<? extends ProteinFeature> result3 = ((DBSingleSpeciesCoreDatabase) ensemblgenomesRegistry.getDatabase(grape, EnsemblDBType.core, "8")).getCoreFactory().getProteinFeatureDAO().getProteinFeaturesByTranslationID(translationID);
111
112
113        System.out.println("retrieved: " + result3.size() + " features.");
114
115        System.out.println("Get features from Ensemblgenomes release 4 for grape translation id =" + translationID);
116
117        grape = "bob";
118        try {
119            ensemblgenomesRegistry.addSessionAlias("vitis_vinifera", grape);
120            System.out.println("added alias bob ok");
121        } catch (Exception exception) {
122            System.out.println("failed to add alias bob for vitis_vinifera");
123        }
124
125
126        List<? extends ProteinFeature> result4 = ((DBSingleSpeciesCoreDatabase) ensemblgenomesRegistry.getDatabase(grape, EnsemblDBType.core, "8")).getCoreFactory().getProteinFeatureDAO().getProteinFeaturesByTranslationID(translationID);
127
128
129        System.out.println("retrieved: " + result4.size() + " features. Using the new alias.");
130        
131        System.out.println("\n\n*****************************\n* COMPLETED FUNCTIONAL TEST *\n*****************************\n");
132
133    }
134}