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.Collection;
025import java.util.HashMap;
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.EnsemblDBType;
030import uk.ac.roslin.ensembl.dao.database.DBRegistry;
031import uk.ac.roslin.ensembl.dao.database.DBSpecies;
032import uk.ac.roslin.ensembl.datasourceaware.core.DAChromosome;
033import uk.ac.roslin.ensembl.datasourceaware.core.DADNASequence;
034import uk.ac.roslin.ensembl.datasourceaware.core.DAGene;
035import uk.ac.roslin.ensembl.exception.NonUniqueException;
036import uk.ac.roslin.ensembl.model.Coordinate;
037import uk.ac.roslin.ensembl.model.Mapping;
038import uk.ac.roslin.ensembl.model.MappingSet;
039import uk.ac.roslin.ensembl.model.compara.HomologyPairRelationship;
040import uk.ac.roslin.ensembl.model.core.Gene;
041import uk.ac.roslin.ensembl.model.core.Species;
042
043/**
044 *
045 * @author tpaterso
046 */
047public class Compara {
048
049    static DBRegistry reg;
050    static DBRegistry greg;
051    static DBRegistry breg;
052
053    public Compara() {
054
055    }
056
057    public void testEnsemblGeneCompara()  throws NonUniqueException {
058
059        String v = reg.getMostRecentEnsemblVersion();
060
061        assertNotNull(reg.getDatabase(EnsemblComparaDivision.MULTI.toString(), EnsemblDBType.compara, null));
062        assertTrue(reg.getDatabase(EnsemblComparaDivision.MULTI.toString(), EnsemblDBType.compara, null).getSchemaVersion().equals(v));
063
064        Species us = reg.getSpeciesByAlias("human");
065        assertNotNull(us);
066
067
068        Gene gene = null;
069        //BRCA2 should be stable!
070        try {
071            gene = us.getGeneByStableID("ENSG00000139618");
072        } catch (Exception e) {
073            AssertionError ae = new AssertionError(
074                    "Failed to get human gene ENSG00000139618 ");
075            ae.initCause(e);
076            throw ae;
077        }
078        assertNotNull(gene);
079        assertTrue(gene.getHomologiesWithoutLazyLoad().isEmpty());
080        assertFalse(gene.getHomologies(reg.getSpeciesByAlias("chimp")).isEmpty());
081        assertFalse(gene.getHomologiesWithoutLazyLoad().isEmpty());
082
083        int c1 = gene.getHomologiesWithoutLazyLoad().size();
084
085        HomologyPairRelationship h = gene.getHomologies(reg.getSpeciesByAlias("chimp")).get(0);
086
087        assertSame(reg.getSpeciesByAlias(h.getTargetProperties().getSpeciesName()), reg.getSpeciesByAlias("chimp"));
088        assertTrue(h.getTarget() instanceof DAGene);
089        assertSame(h.getSource(), gene);
090        assertNotNull(h.getTargetProperties());
091        assertNotNull(h.getSourceProperties());
092
093        try {
094            gene = us.getGeneByStableID("ENSG00000139618");
095        } catch (Exception e) {
096            AssertionError ae = new AssertionError(
097                    "Failed to get human gene ENSG00000139618 ");
098            ae.initCause(e);
099            throw ae;
100        }
101        assertNotNull(gene);
102        assertTrue(gene.getHomologiesWithoutLazyLoad().isEmpty());
103        assertFalse(gene.getHomologies().isEmpty());
104        assertTrue(gene.getHomologiesWithoutLazyLoad().size() == c1);
105
106        try {
107            gene = us.getGeneByStableID("ENSG00000139618");
108        } catch (Exception e) {
109            AssertionError ae = new AssertionError(
110                    "Failed to get human gene ENSG00000139618 ");
111            ae.initCause(e);
112            throw ae;
113        }
114        assertNotNull(gene);
115        assertTrue(gene.getHomologiesWithoutLazyLoad().isEmpty());
116        assertFalse(gene.getHomologies(EnsemblComparaDivision.MULTI).isEmpty());
117        assertTrue(gene.getHomologiesWithoutLazyLoad().size() == c1);
118
119
120
121
122    }
123
124    public void testGenomesGeneCompara() throws NonUniqueException {
125
126
127
128        String v = greg.getMostRecentEnsemblVersion();
129
130        assertNotNull(greg.getDatabase(EnsemblComparaDivision.PLANTS.toString(), EnsemblDBType.compara, null));
131        assertNotNull(greg.getDatabase(EnsemblComparaDivision.PAN_HOMOLOGY.toString(), EnsemblDBType.compara, null));
132
133        Species us = null;
134        try {
135            us = greg.getSpeciesByAlias("Oryza sativa");
136        } catch (NonUniqueException nonUniqueException) {
137            Collection allHits = nonUniqueException.getAllHits();
138            
139            System.out.println("Multiple Hits:");
140            for (Object o:allHits) {
141                System.out.println("\t"+((Species) o).getDatabaseStyleName());
142            }
143            
144        }
145        us = greg.getSpeciesByAlias("oryza_sativa");
146        assertNotNull(us);
147        
148
149        Gene gene = null;
150        try {
151            gene = us.getGeneByStableID("OS01G0976000");
152        } catch (Exception e) {
153            AssertionError ae = new AssertionError(
154                    "Failed to get rice gene OS01G0976000 ");
155            ae.initCause(e);
156            throw ae;
157        }
158
159
160        assertNotNull(gene);
161        assertTrue(gene.getHomologiesWithoutLazyLoad().isEmpty());
162        assertFalse(gene.getHomologies(greg.getSpeciesByAlias("Sorghum bicolor")).isEmpty());
163        assertFalse(gene.getHomologiesWithoutLazyLoad().isEmpty());
164
165        int c1 = gene.getHomologiesWithoutLazyLoad().size();
166
167        HomologyPairRelationship h = gene.getHomologies(greg.getSpeciesByAlias("Sorghum bicolor")).get(0);
168
169        assertSame(greg.getSpeciesByAlias(h.getTargetProperties().getSpeciesName()), greg.getSpeciesByAlias("Sorghum bicolor"));
170        assertTrue(h.getTarget() instanceof DAGene);
171        assertSame(h.getSource(), gene);
172        assertNotNull(h.getTargetProperties());
173        assertNotNull(h.getSourceProperties());
174
175        try {
176            gene = us.getGeneByStableID("OS01G0976000");
177        } catch (Exception e) {
178            AssertionError ae = new AssertionError(
179                    "Failed to get rice gene OS01G0976000 ");
180            ae.initCause(e);
181            throw ae;
182        }
183        assertNotNull(gene);
184        assertTrue(gene.getHomologiesWithoutLazyLoad().isEmpty());
185        assertFalse(gene.getHomologies().isEmpty());
186        assertTrue(gene.getHomologiesWithoutLazyLoad().size() == c1);
187
188        try {
189            gene = us.getGeneByStableID("OS01G0976000");
190        } catch (Exception e) {
191            AssertionError ae = new AssertionError(
192                    "Failed to get rice gene OS01G0976000 ");
193            ae.initCause(e);
194            throw ae;
195        }
196        assertNotNull(gene);
197        assertTrue(gene.getHomologiesWithoutLazyLoad().isEmpty());
198        assertFalse(gene.getHomologies(EnsemblComparaDivision.PLANTS).isEmpty());
199
200        assertTrue(gene.getHomologiesWithoutLazyLoad().size() == c1);
201        try {
202            // version 17 doesnt seem to have any pan homologies
203            gene = us.getGeneByStableID("LOC_Os01g09760", "16");
204        } catch (Exception e) {
205            AssertionError ae = new AssertionError(
206                    "Failed to get rice gene LOC_Os01g09760 ");
207            ae.initCause(e);
208            throw ae;
209        }
210        assertNotNull(gene);
211        assertTrue(gene.getHomologiesWithoutLazyLoad().isEmpty());
212        assertFalse(gene.getHomologies(EnsemblComparaDivision.PAN_HOMOLOGY).isEmpty());
213
214
215
216
217    }
218
219
220    public void testBacteriaGeneCompara() throws NonUniqueException {
221
222
223        String v = breg.getMostRecentEnsemblVersion();
224
225        assertNotNull(breg.getDatabase(EnsemblComparaDivision.BACTERIA.toString(), EnsemblDBType.compara, null));
226
227        Species ecoli = breg.getSpeciesByAlias("escherichia_coli_str_k_12_substr_mg1655");
228        assertNotNull(ecoli);
229
230        //BRCA2 should be stable!
231        Gene gene = null;
232
233        try {
234            gene = ecoli.getGeneByStableID("b0003");
235        } catch (Exception e) {
236            AssertionError ae = new AssertionError(
237                    "Failed to get coli gene b0003");
238            ae.initCause(e);
239            throw ae;
240        }
241
242        assertNotNull(gene);
243        assertTrue(gene.getHomologiesWithoutLazyLoad().isEmpty());
244        assertFalse(gene.getHomologies(breg.getSpeciesByAlias("Peptoclostridium difficile 630")).isEmpty());
245        assertFalse(gene.getHomologiesWithoutLazyLoad().isEmpty());
246
247        int c1 = gene.getHomologiesWithoutLazyLoad().size();
248
249        HomologyPairRelationship h = gene.getHomologies(breg.getSpeciesByAlias("Peptoclostridium difficile 630")).get(0);
250
251        assertSame(breg.getSpeciesByAlias(h.getTargetProperties().getSpeciesName()), breg.getSpeciesByAlias("Peptoclostridium difficile 630"));
252        assertTrue(h.getTarget() instanceof DAGene);
253        assertSame(h.getSource(), gene);
254        assertNotNull(h.getTargetProperties());
255        assertNotNull(h.getSourceProperties());
256
257
258        try {
259            gene = ecoli.getGeneByStableID("b0003");
260        } catch (Exception e) {
261            AssertionError ae = new AssertionError(
262                    "Failed to get coli gene b0003 ");
263            ae.initCause(e);
264            throw ae;
265        }
266        assertNotNull(gene);
267        assertTrue(gene.getHomologiesWithoutLazyLoad().isEmpty());
268        assertFalse(gene.getHomologies().isEmpty());
269        assertTrue(gene.getHomologiesWithoutLazyLoad().size() == c1);
270        try {
271            gene = ecoli.getGeneByStableID("b0003");
272        } catch (Exception e) {
273            AssertionError ae = new AssertionError(
274                    "Failed to get coli gene b0003 ");
275            ae.initCause(e);
276            throw ae;
277        }
278        assertNotNull(gene);
279        assertTrue(gene.getHomologiesWithoutLazyLoad().isEmpty());
280        assertTrue(gene.getHomologies(EnsemblComparaDivision.BACTERIA).isEmpty());
281        assertFalse(gene.getHomologies(EnsemblComparaDivision.PAN_HOMOLOGY).isEmpty());
282        assertTrue(gene.getHomologiesWithoutLazyLoad().size() == c1);
283
284
285
286
287    }
288
289
290    public void testEnsemblSyntenyCompara() throws NonUniqueException {
291
292
293        DBSpecies finch = reg.getSpeciesByAlias("zebra finch");
294        DBSpecies chicken = reg.getSpeciesByAlias("chicken");
295        DAChromosome cChr3 = null;
296
297        try {
298            cChr3 = chicken.getChromosomeByName("3");
299        } catch (Exception e) {
300            AssertionError ae = new AssertionError(
301                    "Failed to get chicken chromosome 3");
302            ae.initCause(e);
303            throw ae;
304        }
305        assertNotNull(cChr3.getComparaFactory());
306
307        HashMap<DADNASequence, MappingSet> syntenies = null;
308        try {
309            syntenies = cChr3.getRegionsOfConservedSynteny(new Coordinate(500000, 1000000), finch);
310        } catch (Exception e) {
311            AssertionError ae = new AssertionError(
312                    "Failed to get finch syntenies for chicken chromosome 3");
313            ae.initCause(e);
314            throw ae;
315        }
316
317        assertNotNull(syntenies);
318        assertFalse(syntenies.isEmpty());
319
320        boolean hitChr3 = false;
321
322        for (DADNASequence dna : syntenies.keySet()) {
323
324            if ( dna.getName().equals("3")) {
325                hitChr3 = true;
326            }
327
328            assertNotNull(dna.getCoordSystem());
329            assertSame(dna.getCoordSystem().getType(), EnsemblCoordSystemType.chromosome);
330            assertNotNull(syntenies.get(dna).getExtent());
331
332            for (Mapping mp : syntenies.get(dna)) {
333                assertTrue(mp.getSource() instanceof DAChromosome);
334                assertTrue(mp.getTarget() instanceof DAGene);
335            }
336
337        }
338        assertTrue(hitChr3);
339
340
341    }
342
343
344    public void testGenomesSyntenyCompara() throws NonUniqueException {
345
346        DBSpecies rice = greg.getSpeciesByAlias("oryza_sativa");
347        DBSpecies sorghum = greg.getSpeciesByAlias("Sorghum bicolor");
348        DAChromosome rChr1 = null;
349        try {
350            rChr1 = rice.getChromosomeByName("1");
351        } catch (Exception e) {
352            AssertionError ae = new AssertionError(
353                    "Failed to get rice chromosome 1");
354            ae.initCause(e);
355            throw ae;
356        }
357
358        assertNotNull(rChr1.getComparaFactory());
359
360        HashMap<DADNASequence, MappingSet> syntenies = null;
361        try {
362            syntenies = rChr1.getRegionsOfConservedSynteny(new Coordinate(800000, 1000000), sorghum);
363        } catch (Exception e) {
364            AssertionError ae = new AssertionError(
365                    "Failed to getsorghum syntenies for rice chromosome 1");
366            ae.initCause(e);
367            throw ae;
368        }
369        assertNotNull(syntenies);
370        assertFalse(syntenies.isEmpty());
371
372        boolean hitChr3 = false;
373
374        for (DADNASequence dna : syntenies.keySet()) {
375
376            if (dna.getName().equals("3")) {
377                hitChr3 = true;
378            }
379
380            assertNotNull(dna.getCoordSystem());
381            assertSame(dna.getCoordSystem().getType(), EnsemblCoordSystemType.chromosome);
382            assertNotNull(syntenies.get(dna).getExtent());
383
384            for (Mapping mp : syntenies.get(dna)) {
385                assertTrue(mp.getSource() instanceof DAChromosome);
386                assertTrue(mp.getTarget() instanceof DAGene);
387            }
388
389        }
390        assertTrue(hitChr3);
391
392
393    }
394
395
396    public void testBacteriaSyntenyCompara() throws NonUniqueException {
397
398
399        DBSpecies ecoli = breg.getSpeciesByAlias("escherichia_coli_str_k_12_substr_mg1655");
400        DBSpecies cdiff = breg.getSpeciesByAlias("Peptoclostridium difficile 630");
401        DAChromosome pChr1 = null;
402        try {
403            pChr1 = ecoli.getChromosomeByName("Chromosome");
404        } catch (Exception e) {
405            AssertionError ae = new AssertionError(
406                    "Failed to get coli chromosome ");
407            ae.initCause(e);
408            throw ae;
409        }
410
411        assertNotNull(pChr1.getComparaFactory());
412
413        HashMap<DADNASequence, MappingSet> syntenies = null;
414        try {
415            syntenies = pChr1.getRegionsOfConservedSynteny(new Coordinate(1, 10000), cdiff);
416        } catch (Exception e) {
417            AssertionError ae = new AssertionError(
418                    "Failed to get cdiff syntenies for coli chromosome ");
419            ae.initCause(e);
420            throw ae;
421        }
422        assertNotNull(syntenies);
423        assertFalse(syntenies.isEmpty());
424
425        boolean hitChr1 = false;
426
427        for (DADNASequence dna : syntenies.keySet()) {
428
429            if (dna.getName().equalsIgnoreCase("chromosome")) {
430                hitChr1 = true;
431            }
432
433            assertNotNull(dna.getCoordSystem());
434            assertSame(dna.getCoordSystem().getType(), EnsemblCoordSystemType.chromosome);
435            assertNotNull(syntenies.get(dna).getExtent());
436
437            for (Mapping mp : syntenies.get(dna)) {
438                assertTrue(mp.getSource() instanceof DAChromosome);
439                assertTrue(mp.getTarget() instanceof DAGene);
440            }
441
442        }
443        assertTrue(hitChr1);
444
445
446    }
447    
448    public static void main(String[] args) throws Exception {
449        Compara c = new Compara();
450
451        try {
452            breg = RegistryProvider.getbReg();
453            assertNotNull(breg);
454            c.testBacteriaGeneCompara();
455            c.testBacteriaSyntenyCompara();
456            
457            reg = RegistryProvider.geteReg();
458            assertNotNull(reg);
459            c.testEnsemblGeneCompara();
460            c.testEnsemblSyntenyCompara();
461            
462            greg = RegistryProvider.getgReg();
463            assertNotNull(greg);
464            c.testGenomesGeneCompara();
465            c.testGenomesSyntenyCompara();
466        } catch (Exception e) {
467            System.out.println(e.getMessage());
468            throw new Exception(e);
469        }
470        
471        System.out.println("Successfuly completes Compara.java");
472    }
473}