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.*;
025import uk.ac.roslin.ensembl.dao.compara.HomologyDAO;
026import uk.ac.roslin.ensembl.dao.database.DBRegistry;
027import uk.ac.roslin.ensembl.dao.database.DBSpecies;
028import uk.ac.roslin.ensembl.datasourceaware.compara.DAHomologyPairRelationship;
029import uk.ac.roslin.ensembl.datasourceaware.core.DAChromosome;
030import uk.ac.roslin.ensembl.datasourceaware.core.DADNASequence;
031import uk.ac.roslin.ensembl.datasourceaware.core.DAGene;
032import uk.ac.roslin.ensembl.model.Coordinate;
033import uk.ac.roslin.ensembl.model.Mapping;
034import uk.ac.roslin.ensembl.model.MappingSet;
035import static org.junit.Assert.*;
036import uk.ac.roslin.ensembl.config.DBConnection.DataSource;
037
038/**
039 *
040 * @author tpaterso
041 */
042public class EnsemblSyntenies {
043
044    //demonstrating ability to find regions of conserved synteny between 
045    //one species and another
046    
047    public static void main(String[] args) throws Exception {
048
049
050        DBRegistry eReg = DBRegistry.createRegistryForDataSource(DataSource.ENSEMBLDB);
051        
052        DBSpecies human = eReg.getSpeciesByAlias("human");
053        DBSpecies chimp = eReg.getSpeciesByAlias("chimpanzee");    
054        
055        DAChromosome h1_70 = human.getChromosomeByName("1", "70");
056        DAChromosome h1 = human.getChromosomeByName("1" );
057        HashMap<DADNASequence, MappingSet> regionsOfConservedSynteny70 = h1_70.getRegionsOfConservedSynteny(new Coordinate(1400000,1600000), chimp);
058        HashMap<DADNASequence, MappingSet> regionsOfConservedSynteny = h1.getRegionsOfConservedSynteny(new Coordinate(1400000,1600000), chimp);
059        
060        
061        assertNotNull(regionsOfConservedSynteny70);
062        assertNotNull(regionsOfConservedSynteny);
063        
064        assertFalse(regionsOfConservedSynteny70.isEmpty());
065        assertFalse(regionsOfConservedSynteny.isEmpty());
066        
067        
068        DBSpecies finch = eReg.getSpeciesByAlias("zebra finch");
069        DBSpecies chicken = eReg.getSpeciesByAlias("chicken");
070
071        DAChromosome cChr3 = chicken.getChromosomeByName("3");
072        HomologyDAO dao = cChr3.getComparaFactory().getHomologyDAO();
073        
074        HashMap<DADNASequence, MappingSet> syntenies =
075                (HashMap<DADNASequence, MappingSet>)
076                    dao.getRegionsOfConservedSynteny(cChr3, new Coordinate(1, 5000000), finch);
077        
078        
079        int size = 0;
080        int targetsize = 0;
081        for (MappingSet ms: syntenies.values()) {
082          size += ms.size();  
083          
084           for (Mapping m: ms) {
085               for (DAHomologyPairRelationship h: ((DAGene)m.getTarget()).getHomologiesWithoutLazyLoad()) {
086                   targetsize++;
087               }
088           }
089
090        }
091        System.out.println("target count: "+targetsize);
092        System.out.println("homologue count: "+size);
093        
094        System.out.println("SYNTENIC REGIONS in Zebra Finch of Chick Chromosome 3\n__________________________________________________\r\n");
095
096        int count =1;
097
098        for ( DADNASequence dna: syntenies.keySet()) {
099
100            System.out.println(count++ + ". "+dna.getCoordSystem().getType().toString()
101                        +" "
102                        +dna.getName()+ " "+syntenies.get(dna).getExtent().toString());
103
104            for (Mapping mp : syntenies.get(dna)) {
105                
106                for (DAHomologyPairRelationship h: ((DAGene)mp.getTarget()).getHomologiesWithoutLazyLoad() ) {
107                
108                System.out.println("\t"+((DAGene) mp.getTarget()).getStableID()
109                        +" ["+h.getTarget().getStableID()+"]"
110                        +" : "+mp.getSourceCoordinates().toString());
111                
112                }
113            }
114
115        }
116
117        System.out.println("\nList of genes on chick chr3 (1-1000000), with finch homologues...\n");
118
119       List<DAGene> genes = cChr3.getGenesOnRegion(1, 1000000);
120
121        for (DAGene g : genes) {
122
123            System.out.println(g.getSpecies().getCommonName()+" Gene "+g.getStableID()
124                    +" "+finch.getCommonName()+ " homologues:");
125
126
127
128            for (DAHomologyPairRelationship hpr : g.getHomologies(finch)) {
129
130                DAGene target = hpr.getTarget();
131                MappingSet mappings = target.getAnnotationLevelMappings();
132
133                for (Mapping m : mappings) {
134
135                System.out.println("\t"+hpr.getType().toString()+" : "
136                        +target.getStableID()+" ["+m.getTargetType().toString()
137                        +" "+((DADNASequence) m.getTarget()).getName()
138                        +":"+m.getTargetCoordinates().toString()+"]");
139    }
140            }
141        }
142        
143
144        //NB this will infact duplicate the relationship found already
145        syntenies =
146                (HashMap<DADNASequence, MappingSet>)
147                    dao.getRegionsOfConservedSynteny(cChr3, new Coordinate(1, 5000000), finch, "6");
148               
149        size = 0;
150        targetsize = 0;
151        for (MappingSet ms: syntenies.values()) {
152          size += ms.size();  
153          
154           for (Mapping m: ms) {
155               for (DAHomologyPairRelationship h: ((DAGene)m.getTarget()).getHomologiesWithoutLazyLoad()) {
156                   targetsize++;
157               }
158           }
159
160        }
161        
162        
163        System.out.println("filtered to chromosome: 6");
164        System.out.println("target count: "+targetsize);
165        System.out.println("homologue count: "+size);
166        
167        System.out.println("SYNTENIC REGIONS in Zebra Finch Chromosome 6 of Chick \n__________________________________________________\r\n");
168
169        count =1;
170
171        for ( DADNASequence dna: syntenies.keySet()) {
172
173            System.out.println(count++ + ". "+dna.getCoordSystem().getType().toString()
174                        +" "
175                        +dna.getName()+ " "+syntenies.get(dna).getExtent().toString());
176
177            for (Mapping mp : syntenies.get(dna)) {
178                
179                for (DAHomologyPairRelationship h: ((DAGene)mp.getTarget()).getHomologiesWithoutLazyLoad() ) {
180                
181                System.out.println("\t"+((DAGene) mp.getTarget()).getStableID()
182                        +" ["+h.getTarget().getStableID()+"]"
183                        +" : "+mp.getSourceCoordinates().toString());
184                
185                }
186            }
187
188        }
189
190        
191        System.out.println("\n\n*************************\nCOMPLETED FUNCTIONAL TEST\n*************************\n");
192}
193
194}