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.HashSet;
026import java.util.Iterator;
027import java.util.List;
028import java.util.TreeSet;
029import uk.ac.roslin.ensembl.config.DBConnection.DataSource;
030import uk.ac.roslin.ensembl.config.EnsemblDBType;
031import uk.ac.roslin.ensembl.dao.database.*;
032import uk.ac.roslin.ensembl.datasourceaware.core.DAChromosome;
033import uk.ac.roslin.ensembl.exception.NonUniqueException;
034import uk.ac.roslin.ensembl.model.database.CollectionDatabase;
035
036public class BacteriaConnection {
037
038    //demonstrated connection to the Bacterial collections in EnsemblesGenomes datasource
039
040    //various standard operations demonstrated 
041    public static void main(String[] args) throws Exception {
042
043        System.out.println("uninitialized bacterial registry report: ");
044        
045        DBRegistry uRegistry = DBRegistry.createUninitializedRegistryForDataSource(DataSource.ENSEMBLBACTERIA);       
046        System.out.println(uRegistry.getVersionReport());
047        System.out.println(uRegistry.getBriefRegistryReport());
048        
049        DBRegistry bacterialRegistry =DBRegistry.createRegistryForDataSourceAtReleaseVersion(DataSource.ENSEMBLBACTERIA, uRegistry.getHighestReleaseVersion());
050       
051        System.out.println("current bacterial registry report: ");
052        System.out.println(bacterialRegistry.getVersionReport());
053        System.out.println(bacterialRegistry.getBriefRegistryReport());       
054        
055        System.out.println("******************************* ");
056
057        //the 'standard' species in the database
058        System.out.println("all DBSpecies");
059        for (DBSpecies s : bacterialRegistry.getSpecies()) {
060            System.out.println(s.getCommonName() + "\n\t ["+s.getShortName()+"]" +" ("+s.getSpeciesBinomial()+")");
061        }
062        if (bacterialRegistry.getSpecies().isEmpty()) {
063            System.out.println("No DBSpecies!");
064        }
065        //bacterial species that are organised into collections
066        System.out.println("all DBCollectionSpecies");
067        for (DBCollectionSpecies s : bacterialRegistry.getCollectionSpecies()) {
068            System.out.println(s.getCommonName()+ "\n\t ["+s.getShortName()+"]" +" ("+s.getSpeciesBinomial()+")");
069        }
070        System.out.println("");
071
072        //get all the CollectionSpecies ( which currently are Bacteria ) 
073        //which are in the CURRENT HIGHEST release configured
074        List<DBSpecies> bactSpeciess = new ArrayList<DBSpecies>();
075        for (DBCollection c : bacterialRegistry.getCollectionsByDBVersion(
076                "" + bacterialRegistry.getHighestReleaseVersion())) {
077            bactSpeciess.addAll(c.getSpecies());
078        }
079        System.out.println("bacteria in : " + bacterialRegistry.getHighestReleaseVersion());
080        for (DBSpecies s : bactSpeciess) {
081            System.out.println(s);
082        }
083
084        //will print out details of each collection
085        
086        for (DBCollection c : bacterialRegistry.getCollections()) {
087            System.out.println(c.getCollectionName() + " (version " + c.getDBVersion() + ")");
088            int i = 1;
089            for (DBCollectionSpecies s : c.getSpecies()) {
090                System.out.println(i++ + ":\t" + s.getComparaName(c.getDBVersion()));
091            }
092        }
093
094
095        System.out.println("TESTING LIVE ENSEMBLBACTERIA");
096
097       
098
099        System.out.println("all  of the collection databases are:");
100        System.out.println("*************************************\n");
101
102//        for (CollectionOfSpecies r : bacterialRegistry.getCollectionsByDBVersion(bacterialRegistry.getMostRecentDBVersion())) {        
103        for (DBCollection coll : bacterialRegistry.getCollections()) {
104
105            System.out.println("collection name: " + coll.getCollectionName());
106            System.out.println("\tschema version: " + coll.getSchemaVersion());
107            System.out.println("\tdb version: " + coll.getDBVersion());
108            System.out.println("\tSpecies: "+ coll.getSpecies().size());
109//          System.out.println("\t_________________________________\n Species included:\n");
110//
111//            for (CollectionSpecies sp : r.getSpecies()) {
112//                System.out.println("\t\t" + sp.getSpeciesBinomial() + " : " + sp.getCommonName());
113//            }
114
115            System.out.println("\t\tDatabases");
116
117            for (CollectionDatabase d : coll.getDatabases()) {
118                System.out.println("\t\t\t" + d.getdBName());
119            }
120
121            System.out.println("\t\t" + EnsemblDBType.collection_core);
122
123            for (CollectionDatabase d : coll.getDatabasesByType(EnsemblDBType.collection_core)) {
124                System.out.println("\t\t\t" + d.getdBName());
125            }
126            System.out.println("\t\t" + EnsemblDBType.collection_funcgen);
127
128            for (CollectionDatabase d : coll.getDatabasesByType(EnsemblDBType.collection_funcgen)) {
129                System.out.println("\t\t\t" + d.getdBName());
130            }
131            System.out.println("\t\t" + EnsemblDBType.collection_variation);
132
133            for (CollectionDatabase d : coll.getDatabasesByType(EnsemblDBType.collection_variation)) {
134                System.out.println("\t\t\t" + d.getdBName());
135            }
136            System.out.println("*************************************\n");
137        }
138
139        DBSpecies s = bacterialRegistry.getSpeciesByAlias("Bacillus anthracis str. CDC 684");
140        List<DBCollectionSpecies> accessioned = bacterialRegistry.getGCAccessionedCollectionSpecies();
141        List<DBCollectionSpecies> named = bacterialRegistry.getNamedCollectionSpecies();
142        
143        
144        if (accessioned.contains(s)) {
145            System.out.println("sp is in accessioned list: "+s.getSpeciesBinomial()+" "+s.hashCode());
146            System.out.println("dbname "+s.getDatabaseStyleName());
147            System.out.println("url "+s.getUrlName());
148            System.out.println("stem "+s.getAssemblyAccessionStem());
149            System.out.println("aliases "+s.getAliases().toString());
150            System.out.println("most recent core "+s.getMostRecentCoreDatabase().getdBName());
151            System.out.println("databases: "+s.getDatabases().toString());
152            System.out.println("----------------------------------------------\n");
153        } else {
154            System.out.println("sp is NOT in accessioned list");
155        }
156        
157        if (named.contains(s)) {
158            System.out.println("sp is in named list: "+s.getSpeciesBinomial()+" "+s.hashCode());
159        } else {
160            System.out.println("sp is NOT in named list");
161        }
162        DBCollectionSpecies s2= bacterialRegistry.getCSpeciesByGCAccessionStem(s.getAssemblyAccessionStem());
163        
164        if (s.equals(s2)) {
165            System.out.println("retrieval by accession is identical: "+s.getSpeciesBinomial()+" "+s.hashCode());   
166        } else {
167            System.out.println("retrieval by accession is NOT identical");   
168        }
169        
170        String st2 = bacterialRegistry.getGCAssemblyAccessionForAlias(s.getSpeciesBinomial());
171        String st = bacterialRegistry.getEnsemblNameForAlias(s.getSpeciesBinomial());
172        
173        DBSpecies s3 = bacterialRegistry.getSpeciesByAlias(st);
174        
175        if (s.equals(s3)) {
176            System.out.println("retrieval by alias is identical: "+s.getSpeciesBinomial()+" "+s.hashCode());   
177        } else {
178            System.out.println("retrieval by alias is NOT identical");   
179        }
180        
181        for (DBCollectionSpecies sp:named) {
182            if (sp.getAliases().contains(st)) {
183                System.out.println(" name indexed species with this ensembl name "+st+" = "+sp.getSpeciesBinomial()
184                        + " "+sp.hashCode());
185                System.out.println("stem "+sp.getAssemblyAccessionStem());
186                System.out.println("dbname "+sp.getDatabaseStyleName());
187                System.out.println("url "+sp.getUrlName());
188                System.out.println("aliases "+sp.getAliases().toString());
189                System.out.println("most recent core "+sp.getMostRecentCoreDatabase().getdBName());
190                System.out.println("databases: "+sp.getDatabases().toString());
191                
192                if (s.equals(sp)) {
193                    System.out.println("which is the same object as the accession indexed species");
194                    
195                } else {
196                    System.out.println("which is NOT the same object as the accession indexed species"); 
197                }
198                System.out.println("-----------------------------------------------------\n");
199            }
200        }
201          
202
203        DBCollectionSpecies pumi = (DBCollectionSpecies) bacterialRegistry.getSpeciesByAlias("Bacillus pumilus SAFR-032");
204
205        System.out.println("DB for Bacillus pumilus SAFR-032");
206
207        TreeSet<Integer> versions = new TreeSet<Integer>();
208
209        for (DBCollectionDatabase d : pumi.getDatabasesByType(EnsemblDBType.collection_core)) {
210
211            System.out.println("core version " + d.getDBVersion());
212            versions.add(Integer.parseInt(d.getDBVersion()));
213
214        }
215
216
217        DAChromosome chrP = (DAChromosome) ((DBCollectionCoreDatabase) pumi.getDatabaseByTypeAndVersion(EnsemblDBType.collection_core, versions.last().toString())).getCoreFactory(pumi).getChromosomeDAO().getChromosomeByName("Chromosome");
218
219        System.out.println(chrP.getSpecies().getSpeciesBinomial() + " : " + chrP.getId() + " : " + chrP.getChromosomeName());
220
221        
222        
223        
224        
225        DBSpecies spG3;
226        DBSpecies spG4;
227        
228          try {
229            spG3 = bacterialRegistry.getSpeciesByAlias("h37rv");
230
231            
232            
233            
234        } catch (NonUniqueException nonUniqueException) {
235            System.out.print("Mycobacterium tuberculosis H37Rv is not unique....");
236            HashSet<DBSpecies> allHits = (HashSet<DBSpecies>) nonUniqueException.getAllHits();
237            System.out.println(allHits.size());
238            Iterator<DBSpecies> iterator = allHits.iterator();
239            
240            int count = 0;
241            for (DBSpecies sp: allHits) {
242              System.out.println(count++ +": "+sp.getDatabaseStyleName()+" "+sp.getComparaName(null));  
243            }
244
245        }
246        
247        
248        
249        
250        
251        
252        
253        
254        System.out.println("\n\n*****************************\n* COMPLETED FUNCTIONAL TEST *\n*****************************\n");
255
256    }
257}