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 */ 022/* 023 * To change this template, choose Tools | Templates 024 * and open the template in the editor. 025 */ 026package uk.ac.roslin.ensembl.demo; 027 028import java.io.File; 029import java.io.FileWriter; 030import java.util.Collection; 031import java.util.List; 032import java.util.TreeMap; 033import java.util.TreeSet; 034import uk.ac.roslin.ensembl.config.DBConnection.DataSource; 035import uk.ac.roslin.ensembl.config.EnsemblDBType; 036import uk.ac.roslin.ensembl.config.FeatureType; 037import uk.ac.roslin.ensembl.dao.database.*; 038import uk.ac.roslin.ensembl.datasourceaware.core.DAChromosome; 039import uk.ac.roslin.ensembl.datasourceaware.core.DAGene; 040import uk.ac.roslin.ensembl.exception.DAOException; 041import uk.ac.roslin.ensembl.model.Coordinate; 042import uk.ac.roslin.ensembl.model.Mapping; 043import uk.ac.roslin.ensembl.model.MappingSet; 044 045/** 046 * 047 * @author tpaterso 048 */ 049public class LogicFromSavantPlugin { 050 051 /* 052 * The Savant Plugin embeds JEnsembl functionality in a plugin for the 053 * Savant Genome Browser. Salient features of JEnsembl mediated data retrieval 054 * are shown in this demonstration code. 055 * see uk.ac.roslin.savantensembl.EnsemblConnect 056 * in Savant Plugin Code (JEnsembl's sourceforge subversion and File Download page) 057 */ 058 059 public static void main(String[] args) throws Exception { 060 061 062 //the user selects which source to use 063 // from DataSource.ENSEMBLDB,DataSource.ENSEMBLDB_ARCHIVES,DataSource.ENSEMBLGENOMES 064 DataSource source = DataSource.ENSEMBLDB; 065 066 // a registry is made ( and cached in the App ) 067 DBRegistry registry = DBRegistry.createRegistryForDataSource(source); 068 069 //the collection of available species is used as the basis for a user selection widget 070 Collection<DBSpecies> species = registry.getSpecies(); 071 072 //if we have chosen to work with DataSource.ENSEMBLBACTERIA 073 // instead we do 074 //Collection<? extends DBSpecies> species = registry.getCollectionSpecies(); 075 076 // the chosen species is actually selected from the list 077 DBSpecies currentSpecies = registry.getSpeciesByAlias("cow"); 078 079 080 //the list of available core databases is used for a release/version selection widget 081 TreeSet<? extends DBDatabase> dbs; 082 if (currentSpecies instanceof DBCollectionSpecies) { 083 dbs = currentSpecies.getDatabasesByType(EnsemblDBType.collection_core); 084 } else { 085 dbs = currentSpecies.getDatabasesByType(EnsemblDBType.core); 086 } 087 088 089 //again the database is actually selected by the user, here we just get one from the registry 090 DBDatabase currentDB = registry.getDatabase("cow", EnsemblDBType.core, null); 091 092 //or for bacteria.. 093 //DBDatabase currentDB = registry.getDatabase("Bacillus pumilus (strain SAFR-032)", EnsemblDBType.collection_corecore, "14"); 094 095 //the list of chromosomes for that species is retireved and used as the basis of a selection widget 096 TreeMap<String, DAChromosome> chromosomes = new TreeMap<String, DAChromosome>(); 097 098 List<DAChromosome> temp = null; 099 if (currentDB instanceof DBSingleSpeciesCoreDatabase) { 100 temp = ((DBSingleSpeciesCoreDatabase) currentDB).getChromosomes(); 101 } else { 102 temp = ((DBCollectionCoreDatabase) currentDB).getChromosomes(currentSpecies); 103 } 104 for (DAChromosome c : temp) { 105 chromosomes.put(c.getChromosomeName(), c); 106 } 107 108 //the user selects a chromosome 109 DAChromosome currentChromosome = chromosomes.get("MT"); 110 111 String genomeTrackName = "chr_" + currentChromosome + "_" 112 + currentChromosome.getSpecies().getDatabaseStyleName() 113 + "_" + currentChromosome.getDBVersion(); 114 115 //the selected chromosome is added to the Savant display as a gGEnomTrack... 116 117 118 //if the user selects to load the sequence 119 //currently we have to load the whole sequence as a fasta file 120 //which is used for display by Savant 121 122 //obviously dont do this for a big chromosome... could do it for a Mitochondion 123 124 File fastaFile = new File("chr_" + currentChromosome + "_" + currentChromosome.getSpecies().getDatabaseStyleName() 125 + "_" + currentChromosome.getDBVersion() + ".fasta"); 126 FileWriter wr = new FileWriter(fastaFile); 127 128 StringBuilder sb = new StringBuilder(currentChromosome.getSequenceAsString()); 129 wr.write(">chr_" + currentChromosome + "_" + currentChromosome.getSpecies().getDatabaseStyleName() 130 + "_" + currentChromosome.getDBVersion() + "\n"); 131 wr.write(sb.toString()); 132 wr.write("\r\n"); 133 wr.close(); 134 //you should now have a file of sequence 135 136 //when the user chooses to get the genes... 137 138 List<DAGene> genes = null; 139 File genesFile = null; 140 File genesSavantFile = null; 141 String genesBedFileName = genomeTrackName + ".genes.bed"; 142 143 //the format will be zipped up 144 String genesSavantFileName = genesBedFileName + ".savant.gz"; 145 146 MappingSet mappings = null; 147 try { 148 genes = currentChromosome.getGenesOnRegion(1, currentChromosome.getLength()); 149 mappings = currentChromosome.getLoadedMappings(FeatureType.gene); 150 } catch (DAOException ex) { 151 System.out.println("Failed to retrieve genes for " 152 + currentChromosome.getSpecies() 153 + "chr " + currentChromosome 154 + " release " + currentChromosome.getDBVersion() 155 + "\n" + ex.getMessage()); 156 } 157 158 try { 159 160 genesFile = new File(genesBedFileName); 161 genesSavantFile = new File(genesSavantFileName); 162 FileWriter w = new FileWriter(genesFile); 163 164 for (Mapping m : mappings) { 165 166 if (m.getSourceCoordinates().getStrand().equals(Coordinate.Strand.REVERSE_STRAND)) { 167 w.write(genomeTrackName + "\t" 168 + m.getSourceCoordinates().getStart() + "\t" 169 + m.getSourceCoordinates().getEnd() + "\t" 170 + ((DAGene) m.getTarget()).getStableID() + "\t0\t" 171 + "-\n"); 172 } else { 173 w.write(genomeTrackName + "\t" 174 + m.getSourceCoordinates().getStart() + "\t" 175 + m.getSourceCoordinates().getEnd() + "\t" 176 + ((DAGene) m.getTarget()).getStableID() + "\t0\t" 177 + "+\n"); 178 } 179 } 180 w.close(); 181 // you should now have a file of mappings 182 183 //this file is then the basis of a new gen track in the Savant browser 184 //FormatUtils.formatFile(genesFile, genesSavantFile, FileType.INTERVAL_BED, false); 185 186 } catch (Exception e) { 187 System.out.println("Failed to write gene track file for : " 188 + currentChromosome.getSpecies() 189 + "chr " + currentChromosome 190 + " release " + currentChromosome.getDBVersion() 191 + "\n" + e.getMessage()); 192 } 193 194 System.out.println("\n\n*************************\nCOMPLETED FUNCTIONAL TEST\n*************************\n"); 195 196 } 197}