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
024
025
026import java.util.ArrayList;
027import java.util.List;
028import uk.ac.roslin.ensembl.config.DBConnection.DataSource;
029import uk.ac.roslin.ensembl.config.EnsemblDBType;
030import uk.ac.roslin.ensembl.dao.database.DBRegistry;
031import uk.ac.roslin.ensembl.dao.database.DBSingleSpeciesCoreDatabase;
032import uk.ac.roslin.ensembl.dao.database.DBSpecies;
033import uk.ac.roslin.ensembl.datasourceaware.core.DAAssembledDNASequence;
034import uk.ac.roslin.ensembl.datasourceaware.core.DAChromosome;
035import uk.ac.roslin.ensembl.datasourceaware.core.DADNASequence;
036
037public class RevalidateAndLazyLoadSequences {
038
039    public  static void main(String[] args) throws Exception {
040
041        DBRegistry ensembldbRegistry =  DBRegistry.createRegistryForDataSource(DataSource.ENSEMBLDB);
042        
043        DBSpecies sp = ensembldbRegistry.getSpeciesByAlias("chicken");
044
045        DBSingleSpeciesCoreDatabase GGdb  = (DBSingleSpeciesCoreDatabase) sp
046                .getDatabaseByTypeAndVersion(EnsemblDBType.core, "68");
047        
048        DBSingleSpeciesCoreDatabase GGdb74  = (DBSingleSpeciesCoreDatabase) sp
049                .getDatabaseByTypeAndVersion(EnsemblDBType.core, "74");
050
051        //make a dummy datasourceawre sequence
052        DADNASequence s1 = new DADNASequence(GGdb.getCoreFactory());
053
054        //intialize its id
055        s1.setId(102799);
056
057        System.out.println("s1 type before validation - should be DASequence: "+s1.getClass().getSimpleName());
058
059        //force validation will create a new object
060        s1 = GGdb.getCoreFactory().getSequenceDAO().getValidatedSequence(s1);
061        
062       // System.out.println("s1: "+s1.getId()+" "+s1.getName());
063       //System.out.println("s1 v72 id: "+sp.getChromosomeByName(s1.getName(), "72").getId());
064        System.out.println("s1 type after validation - should be DAChromosome: "+s1.getClass().getSimpleName());
065        
066        DADNASequence s2 = GGdb.getCoreFactory().getSequenceDAO().getSequenceByID(102799);
067        System.out.println("102799 type before validation- should be a chromosome: "+s2.getClass().getSimpleName());
068        s2 = GGdb.getCoreFactory().getSequenceDAO().getValidatedSequence(s2);
069        System.out.println("102799 type after validation- should still be a chromosome: "+s2.getClass().getSimpleName());
070        //System.out.println("s2: "+s2.getId()+" "+s2.getName());
071        //System.out.println("s2 v72 id: "+sp.getChromosomeByName(s2.getName(), "72").getId());
072        
073        s1 = new DADNASequence(GGdb.getCoreFactory());
074
075        //intialize its id
076        s1.setId(44648);
077
078        System.out.println("s1 type before validation - should be DASequence: "+s1.getClass().getSimpleName());
079
080        //force validation will create a new object
081        s1 = GGdb74.getCoreFactory().getSequenceDAO().getValidatedSequence(s1);
082        System.out.println("s1 type after validation - should be DAChromosome: "+s1.getClass().getSimpleName());
083        
084        s2 = GGdb74.getCoreFactory().getSequenceDAO().getSequenceByID(44648);
085        System.out.println("44648 type before validation- should be a chromosome: "+s2.getClass().getSimpleName());
086        s2 = GGdb74.getCoreFactory().getSequenceDAO().getValidatedSequence(s2);
087        System.out.println("44648 type after validation- should still be a chromosome: "+s2.getClass().getSimpleName());
088
089        
090        DADNASequence s3 = GGdb.getCoreFactory().getSequenceDAO().getSequenceByID(54919);
091        System.out.println("54919 type before validation - should be a sequence: "+s3.getClass().getSimpleName());
092        s3 = GGdb.getCoreFactory().getSequenceDAO().getValidatedSequence(s3);
093        System.out.println("54919 type after validation- should still be a sequence: "+s3.getClass().getSimpleName());
094       
095
096
097        DADNASequence s4 = new DADNASequence(GGdb.getCoreFactory());
098        s4.setId(102799);
099        DADNASequence s5 = new DADNASequence(GGdb.getCoreFactory());
100        s5.setId(54919);
101        DADNASequence s6 = new DADNASequence(GGdb.getCoreFactory());
102        DADNASequence s8 = new DADNASequence(GGdb.getCoreFactory());
103        s8.setId(-999);
104
105        List<DADNASequence> test = new ArrayList<DADNASequence>();
106        test.add(s4);
107        test.add(s5);
108        test.add(s6);
109        test.add(s8);
110
111        System.out.println();
112        System.out.println("before");
113        for (DADNASequence s : test) {
114            System.out.println(s.getId()+" : "+s.getClass().getSimpleName());
115        }
116
117        test = (List<DADNASequence>) GGdb.getCoreFactory().getSequenceDAO()
118                .getValidatedSequences( test);
119        
120        System.out.println();
121        System.out.println("after");
122        for (DADNASequence s : test) {
123            System.out.println(s.getId()+" : "+s.getClass().getSimpleName());
124        }
125
126        System.out.println();
127
128        s6 = GGdb.getCoreFactory().getSequenceDAO().getValidatedSequence(s6);
129        String n = (s6==null)? "yes":"no";
130        System.out.println("is s6 null :" +n);
131
132
133        DADNASequence s7 = new DADNASequence(GGdb.getCoreFactory());
134        s7.setId(-999);
135        s7 = GGdb.getCoreFactory().getSequenceDAO().getValidatedSequence(s7);
136
137
138        n = (s7==null)? "yes":"no";
139        System.out.println("is s7 null :" +n);
140        
141        System.out.println();
142
143        DADNASequence a = new DADNASequence();
144        System.out.println("a type: "+a.getType());
145        DADNASequence b = new DAAssembledDNASequence();
146
147        System.out.println("b type: "+b.getType());
148        DADNASequence c = new DAChromosome();
149        System.out.println("c type: "+c.getType());
150
151
152        //you cant change the objct class in place!! 
153        
154        DADNASequence d = new DADNASequence(GGdb.getCoreFactory());
155         d.setId(102799);
156         System.out.println("d class: "+d.getClass().getSimpleName());
157         //getType forces lazyload but not validation
158         System.out.println("d type: "+d.getType());
159         System.out.println("d class: "+d.getClass().getSimpleName());
160         
161         
162         DADNASequence e = new DADNASequence(GGdb.getCoreFactory());
163         e.setId(102799);
164         System.out.println("e class: "+e.getClass().getSimpleName());
165         
166         //getCoordSystem forces lazyload but not  validation
167         System.out.println("e coordsytem type: "+e.getCoordSystem().getType());
168         System.out.println("e class: "+e.getClass().getSimpleName());
169         System.out.println("e  type: "+e.getType());
170         System.out.println("e class: "+e.getClass().getSimpleName());
171        System.out.println("\n\n*****************************\n* COMPLETED FUNCTIONAL TEST *\n*****************************\n");
172
173
174    }
175
176}