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.EnsemblCoordSystemType; 028import uk.ac.roslin.ensembl.dao.database.DBRegistry; 029import uk.ac.roslin.ensembl.dao.database.DBSpecies; 030import uk.ac.roslin.ensembl.dao.factory.DAOCoreFactory; 031import uk.ac.roslin.ensembl.datasourceaware.core.DAChromosome; 032import uk.ac.roslin.ensembl.datasourceaware.core.DADNASequence; 033import uk.ac.roslin.ensembl.datasourceaware.core.DAGene; 034import uk.ac.roslin.ensembl.model.core.DNASequence; 035 036/** 037 * 038 * @author tpaterso 039 */ 040public class ChromosomeCaching { 041 042 //demonstrating the use of the chromosome cache 043 //chromosomes for a given species/version are only instantiated once - and then cached for reuse 044 //their properties are lazy loaded as needed 045 046 //if they are wrongly instantiated as Generic DADNASequences - they can be validated 047 048 //note code fixed and working at release 67 ( this is id dependent tests ) 049 050 public static void main(String[] args) throws Exception { 051 052 DBRegistry eReg = DBRegistry.createRegistryForDataSource(DataSource.ENSEMBLDB); 053 DBSpecies sp = eReg.getSpeciesByAlias("human"); 054 055 DAGene g = sp.getGeneByStableID("ENSG00000153551"); 056 System.out.println("current version:"+g.getDBVersion()); 057 DAChromosome c1 = (DAChromosome) g.getLoadedMappings(EnsemblCoordSystemType.chromosome).first().getTarget(); 058 DAChromosome c1_2 = (DAChromosome) g.getChromosomeMapping().getTarget(); 059 System.out.println("first fetch of chr: "+c1.getHashID() +" : "+c1.hashCode()); 060 System.out.println("second fetch of chr: "+c1_2.getHashID() +" : "+c1_2.hashCode()); 061 062 DAChromosome c2 = sp.getChromosomeByName(c1.getChromosomeName()); 063 System.out.println("current version:"+c2.getDBVersion()); 064 System.out.println("third fetch of chr: "+c2.getHashID() +" : "+c2.hashCode()); 065 066 DAGene g2 = sp.getGeneByStableID("ENSG00000170293"); 067 System.out.println("current version:"+g2.getDBVersion()); 068 DAChromosome c3 = (DAChromosome) g2.getLoadedMappings(EnsemblCoordSystemType.chromosome).first().getTarget(); 069 DAChromosome c3_2 = (DAChromosome) g2.getChromosomeMapping().getTarget(); 070 System.out.println("fourth fetch of chr: "+c3.getHashID() +" : "+c3.hashCode()); 071 System.out.println("fifth fetch of chr: "+c3_2.getHashID() +" : "+c3_2.hashCode()); 072 073 DAChromosome c4 = sp.getChromosomeByName("4","67"); 074 System.out.println("current version:"+c4.getDBVersion()); 075 System.out.println("first fetch of chr: "+c4.getHashID() +" : "+c4.hashCode()); 076 077 DAGene g3 = sp.getGeneByStableID("ENSG00000186777","67"); 078 System.out.println("current version:"+g3.getDBVersion()); 079 DAChromosome c5 = (DAChromosome) g3.getLoadedMappings(EnsemblCoordSystemType.chromosome).first().getTarget(); 080 DAChromosome c5_2 = (DAChromosome) g3.getChromosomeMapping().getTarget(); 081 System.out.println("second fetch of chr: "+c5.getHashID() +" : "+c5.hashCode()); 082 System.out.println("third fetch of chr: "+c5_2.getHashID() +" : "+c5_2.hashCode()); 083 084 DADNASequence s = (DADNASequence) g3.getDaoFactory().getSequenceDAO().getSequenceByID(27524); 085 System.out.println("fourth fetch of chr: "+s.getHashID() +" : "+s.hashCode()); 086 087 DADNASequence s1 = new DADNASequence(g3.getDaoFactory()); 088 s1.setId(27524); 089 System.out.println("s1 type before validation - should be DASequence: " 090 +s1.getClass().getSimpleName() +" : "+s1.getHashID() +" : "+s1.hashCode()); 091 092 //note this changes the object referenced by 's1' 093 s1 = (DADNASequence) g3.getDaoFactory().getSequenceDAO().getValidatedSequence(s1); 094 095 System.out.println("s1 type after validation - should be DAChromosome: " 096 +s1.getClass().getSimpleName() +" : "+s1.getHashID() +" : "+s1.hashCode() ); 097 098 //this will do the query but then replace the result with the cached chromosome! 099 DADNASequence s2 = (DADNASequence) g3.getDaoFactory().getSequenceDAO().getSequenceByID(27524); 100 101 System.out.println("s2 type before validation- should be a chromosome: " 102 +s2.getClass().getSimpleName()+" : "+s2.getHashID() +" : "+s2.hashCode() ); 103 104 s2 = (DADNASequence) g3.getDaoFactory().getSequenceDAO().getValidatedSequence(s2); 105 System.out.println("s2 type after validation- should still be a chromosome: " 106 +s2.getClass().getSimpleName()+" : "+s2.getHashID() +" : "+s2.hashCode()); 107 108 109 //showing validation works to convert from DADNASequence 110 //objects to DAAssembledSequences or DAChromosomes 111 112 DAOCoreFactory f = g3.getDaoFactory(); 113 114 DADNASequence s4 = new DADNASequence(f); 115 s4.setId(27524); 116 DADNASequence s5 = new DADNASequence(f); 117 s5.setId(54919); 118 DADNASequence s6 = new DADNASequence(f); 119 DADNASequence s8 = new DADNASequence(f); 120 s8.setId(-999); 121 122 List<DNASequence> test = new ArrayList<DNASequence>(); 123 test.add(s4); 124 test.add(s5); 125 test.add(s6); 126 test.add(s8); 127 128 System.out.println(""); 129 System.out.println("before"); 130 for (DNASequence x : test) { 131 if (x == null) { 132 System.out.println("null object"); 133 } 134 System.out.println(x.getId()+" : "+x.getClass().getSimpleName()+" : "+x.getHashID()+" : "+x.hashCode()); 135 } 136 137 test = (List<DNASequence> ) f.getSequenceDAO().getValidatedSequences(test); 138 139 System.out.println(""); 140 System.out.println("after"); 141 for (DNASequence x : test) { 142 System.out.println(x.getId()+" : "+x.getClass().getSimpleName()+" : "+x.getHashID()+" : "+x.hashCode()); 143 } 144 145 146 System.out.println("\n\n*************************\nCOMPLETED FUNCTIONAL TEST\n*************************\n"); 147 } 148 149 150}