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.test;
023
024import java.util.HashMap;
025import java.util.List;
026import static org.junit.Assert.*;
027import uk.ac.roslin.ensembl.config.EnsemblComparaDivision;
028import uk.ac.roslin.ensembl.config.EnsemblCoordSystemType;
029import uk.ac.roslin.ensembl.config.FeatureType;
030import uk.ac.roslin.ensembl.dao.database.DBRegistry;
031import uk.ac.roslin.ensembl.dao.database.DBSpecies;
032import uk.ac.roslin.ensembl.dao.factory.DAOCoreFactory;
033import uk.ac.roslin.ensembl.datasourceaware.core.*;
034import uk.ac.roslin.ensembl.exception.NonUniqueException;
035import uk.ac.roslin.ensembl.model.CoordinateSet;
036import uk.ac.roslin.ensembl.model.Mapping;
037import uk.ac.roslin.ensembl.model.MappingSet;
038import uk.ac.roslin.ensembl.model.ObjectType;
039
040/**
041 *
042 * @author tpaterso
043 */
044public class Chromosome {
045
046    static DBRegistry eReg;
047    static DAChromosome chr20_75;
048    static DBSpecies sp;
049
050
051    public Chromosome() throws NonUniqueException {
052        eReg = RegistryProvider.geteReg();        
053        assertNotNull(eReg);
054                
055        sp = eReg.getSpeciesByAlias("human");
056        assertNotNull(sp);
057        
058        try {
059            chr20_75 = sp.getChromosomeByName("20", "75");
060        } catch (Exception e) {
061            AssertionError ae = new AssertionError(
062                    "Failed to get  chromosome by name");
063            ae.initCause(e);
064            throw ae;
065        }
066        assertNotNull(chr20_75);
067    }
068
069
070    public void testChromosomeCache() {
071
072        DAGene g = null;
073
074        try {
075            g = sp.getGeneByStableID("ENSG00000153551");
076        } catch (Exception e) {
077            AssertionError ae = new AssertionError(
078                    "Failed to get human gene ENSG00000153551");
079            ae.initCause(e);
080            throw ae;
081        }
082
083        DAChromosome c1 = null;
084
085        try {
086            c1 = (DAChromosome) g.getChromosomeMapping().getTarget();
087        } catch (Exception e) {
088            AssertionError ae = new AssertionError(
089                    "Failed to get chromosome for human gene ENSG00000153551");
090            ae.initCause(e);
091            throw ae;
092        }
093
094        assertSame(c1.getCoordSystem().getType(), EnsemblCoordSystemType.chromosome);
095
096        final int c1hash = c1.hashCode();
097
098        DAChromosome c1_b = null;
099        try {
100            c1_b = sp.getChromosomeByName(c1.getChromosomeName());
101        } catch (Exception e) {
102            AssertionError ae = new AssertionError(
103                    "Failed to get chromosome By name");
104            ae.initCause(e);
105            throw ae;
106        }
107
108        assertSame(c1_b.getCoordSystem().getType(), EnsemblCoordSystemType.chromosome);
109
110        final int c1_bhash = c1_b.hashCode();
111
112        assertTrue(c1hash == c1_bhash);
113
114        DAChromosome c1_c = null;
115        try {
116            c1_c = sp.getChromosomeByName(c1.getChromosomeName(), new Integer(sp.getHighestDBRelease() - 1).toString());
117        } catch (Exception e) {
118            AssertionError ae = new AssertionError(
119                    "Failed to get chromosome By name and version");
120            ae.initCause(e);
121            throw ae;
122        }
123
124        assertSame(c1_c.getCoordSystem().getType(), EnsemblCoordSystemType.chromosome);
125
126        final int c1_chash = c1_c.hashCode();
127
128        assertFalse(c1hash == c1_chash);
129    }
130
131  
132    public void testChromosomeValidation() {
133
134        DAOCoreFactory factory = null;
135        try {
136            factory = chr20_75.getDaoFactory();
137        } catch (Exception e) {
138            AssertionError ae = new AssertionError(
139                    "Failed to get Dao Factory for chromosome");
140            ae.initCause(e);
141            throw ae;
142        }
143
144        DADNASequence s = null;
145        try {
146            s = (DADNASequence) factory.getSequenceDAO().getSequenceByID(27524);
147        } catch (Exception e) {
148            AssertionError ae = new AssertionError(
149                    "Failed to get sequence by ID");
150            ae.initCause(e);
151            throw ae;
152        }
153
154        assertTrue(s instanceof DADNASequence);
155        assertTrue(s instanceof DAChromosome);
156        assertSame(s.getCoordSystem().getType(), EnsemblCoordSystemType.chromosome);
157
158        DADNASequence s1 = new DADNASequence(factory);
159        s1.setId(27524);
160
161        assertTrue(s1 instanceof DADNASequence);
162        assertFalse(s1 instanceof DAChromosome);
163        assertNotSame(s, s1);
164
165        //this would force lazyload which actually validates the chromosome
166        //assertSame(s1.getCoordSystem().getType(), EnsemblCoordSystemType.chromosome);
167
168        try {
169            s1 = (DADNASequence) factory.getSequenceDAO().getValidatedSequence(s1);
170        } catch (Exception e) {
171            AssertionError ae = new AssertionError(
172                    "Failed to validate sequence");
173            ae.initCause(e);
174            throw ae;
175        }
176
177        assertTrue(s1 instanceof DADNASequence);
178        assertTrue(s1 instanceof DAChromosome);
179        assertSame(s, s1);
180
181        assertSame(s1.getCoordSystem().getType(), EnsemblCoordSystemType.chromosome);
182
183        DADNASequence s2 = null;
184
185        try {
186            s2 = (DADNASequence) factory.getSequenceDAO().getSequenceByID(27524);
187        } catch (Exception e) {
188            AssertionError ae = new AssertionError(
189                    "Failed to get sequence by ID");
190            ae.initCause(e);
191            throw ae;
192        }
193
194        assertTrue(s2 instanceof DADNASequence);
195        assertTrue(s2 instanceof DAChromosome);
196        assertSame(s2.getCoordSystem().getType(), EnsemblCoordSystemType.chromosome);
197
198        try {
199            s2 = (DADNASequence) factory.getSequenceDAO().getValidatedSequence(s2);
200        } catch (Exception e) {
201            AssertionError ae = new AssertionError(
202                    "Failed to validate sequence");
203            ae.initCause(e);
204            throw ae;
205        }
206
207        assertTrue(s2 instanceof DADNASequence);
208        assertTrue(s2 instanceof DAChromosome);
209
210        assertSame(s, s2);
211
212    }
213
214
215    public void testAssembly() {
216
217        assertNotNull(chr20_75);
218        assertTrue(chr20_75.getLength() == 63025520);
219        DAAssembly completeAssembly = chr20_75.getCompleteAssembly();
220        assertNotNull(completeAssembly);
221        assertTrue(completeAssembly.getAssemblyStart() == 1);
222        assertTrue(completeAssembly.getAssemblyStop() == 63025520);
223        assertSame(completeAssembly.getParent(), chr20_75);
224        try {
225            assertFalse(completeAssembly.getStitchedMappings(1, 1000000).isEmpty());
226            assertFalse(completeAssembly.getStitchedMappings().isEmpty());
227            assertTrue(completeAssembly.getStitchedMappings(1, 1000000).size() == 11);
228            assertTrue(completeAssembly.getStitchedMappings().size() == 645);
229        } catch (Exception e) {
230            AssertionError ae = new AssertionError(
231                    "Failed to getStitchedMappings");
232            ae.initCause(e);
233            throw ae;
234        }
235    }
236
237
238    public void testGenes() {
239
240        assertTrue(chr20_75.getLoadedMappings(FeatureType.gene).isEmpty());
241        List<DAGene> genesOnRegion = null;
242        try {
243                genesOnRegion = chr20_75.getGenesOnRegion(500000, 1000000);
244        } catch (Exception e) {
245            AssertionError ae = new AssertionError(
246                    "Failed to getGenesOnRegion");
247            ae.initCause(e);
248            throw ae;
249        }                
250        assertTrue(genesOnRegion.size() == 10);
251        assertTrue(chr20_75.getLoadedMappings(FeatureType.gene).size() == 10);
252
253        HashMap<ObjectType, CoordinateSet> mappedRegions = chr20_75.getMappedRegions();
254        assertTrue(mappedRegions.containsKey(FeatureType.gene));
255        assertFalse(mappedRegions.containsKey(FeatureType.exon));
256    }
257
258
259    public void testPAR()  {
260
261        DAChromosome chrX_75 = null;
262        DAChromosome chrY_75 = null;
263        try {
264                chrX_75 =  sp.getChromosomeByName("X", "75");
265                chrY_75 = sp.getChromosomeByName("Y", "75");
266        } catch (Exception e) {
267            AssertionError ae = new AssertionError(
268                    "Failed to get X and Y chromosomes");
269            ae.initCause(e);
270            throw ae;
271        }                   
272                
273        assertFalse(chr20_75.isPAR());
274        assertFalse(chrX_75.isPAR());
275        assertTrue(chrY_75.isPAR());
276
277        assertNull(chr20_75.getParChromosome());
278        assertNull(chr20_75.getPseudoAutosomalRegion());
279        assertNull(chrX_75.getParChromosome());
280        assertNull(chrX_75.getPseudoAutosomalRegion());
281        assertNotNull(chrY_75.getParChromosome());
282        assertNotNull(chrY_75.getPseudoAutosomalRegion());
283
284        MappingSet  stitchedMappingsX = null; 
285        MappingSet  stitchedMappingsY = null; 
286        try {
287                stitchedMappingsX = chrX_75.getCompleteAssembly().getStitchedMappings();
288                stitchedMappingsY = chrY_75.getCompleteAssembly().getStitchedMappings();
289        } catch (Exception e) {
290            AssertionError ae = new AssertionError(
291                    "Failed to get X and Y stitched mappings");
292            ae.initCause(e);
293            throw ae;
294        }         
295                
296
297        assertTrue(stitchedMappingsX.size() > stitchedMappingsY.size());
298        assertTrue(stitchedMappingsY.containsAll(chrY_75.getPseudoAutosomalRegion()));
299
300        int loop = 0;
301        int size = stitchedMappingsY.size();
302
303        for (Mapping m : stitchedMappingsY) {
304
305            if (loop == 0) {
306                assertTrue(m.getTarget() instanceof GapSequence);
307            }
308            if (loop == 1) {
309                assertSame(m.getTarget(), chrX_75);
310            }
311            if (loop == size - 1) {
312                assertSame(m.getTarget(), chrX_75);
313            }
314            loop++;
315        }
316
317    }
318    
319
320    public void testSequence() {
321        
322        DAChromosome chr21_75 = null;
323        try {
324                chr21_75 =  sp.getChromosomeByName("21", "75");
325        } catch (Exception e) {
326            AssertionError ae = new AssertionError(
327                    "Failed to get chromosome2 1");
328            ae.initCause(e);
329            throw ae;
330        }  
331        
332        assertTrue(chr21_75.getLength()==48129895);
333        assertTrue(chr21_75.getSequenceAsString(1,10).equalsIgnoreCase("NNNNNNNNNN"));
334        assertTrue(chr21_75.getSequenceAsString(48129886,48129895).equalsIgnoreCase("NNNNNNNNNN"));
335        assertTrue(chr21_75.getSequenceAsString(100001,100010).equalsIgnoreCase("NNNNNNNNNN"));
336        assertTrue(chr21_75.getSequenceAsString(15245251,15245260).equalsIgnoreCase("TGTGCATCTT"));
337        assertTrue(chr21_75.getReverseComplementSequenceAsString(15245251,15245260).equalsIgnoreCase("AAGATGCACA"));
338        
339    }
340
341    public void testProperties() {
342        
343        String name = "22";
344        String version = "75";
345        
346        DAChromosome chr22_75 = null;
347        try {
348                chr22_75 =  sp.getChromosomeByName(name, version);
349        } catch (Exception e) {
350            AssertionError ae = new AssertionError(
351                    "Failed to get chromosome22");
352            ae.initCause(e);
353            throw ae;
354        }  
355
356        assertTrue(chr22_75.getLength()==51304566);
357        assertTrue(chr22_75.getBioEnd()==51304566);
358        assertTrue(chr22_75.getDBSeqLength()==51304566);
359        assertTrue(chr22_75.getName().equalsIgnoreCase(name));
360        assertTrue(chr22_75.getChromosomeName().equalsIgnoreCase(name));
361        assertTrue(chr22_75.getSchemaVersion().equalsIgnoreCase(version));
362        assertTrue(chr22_75.getCoordSystem().isTopLevel());
363        assertTrue(chr22_75.getCoordSystem().getRank()==1);
364        assertTrue(chr22_75.getCoordSystem().getType().equals(EnsemblCoordSystemType.chromosome));
365        assertFalse(chr22_75.isPAR());
366        assertTrue(chr22_75.getCodonTableID()==1);
367        assertTrue(chr22_75.getType().equals(EnsemblCoordSystemType.chromosome));
368        assertTrue(chr22_75.getComparaDivision().equals(EnsemblComparaDivision.MULTI));
369        
370    }
371
372
373    public static void main(String[] args) throws Exception {
374        
375        Chromosome chr = new Chromosome();
376        
377        
378        
379        try {
380            chr.testChromosomeCache();
381            chr.testAssembly();
382            chr.testChromosomeValidation();
383            
384            chr.testGenes();
385            
386            chr.testPAR();
387            
388            chr.testProperties();
389            
390            chr.testSequence();
391        } catch (Exception e) {
392            System.out.println(e.getMessage());
393            throw new Exception(e);
394        }
395        
396        System.out.println("Successfuly completes Chromosome.java");
397 
398    }
399}