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.dao.database.coreaccess; 023 024import java.util.ArrayList; 025import java.util.List; 026import org.apache.ibatis.session.SqlSession; 027import uk.ac.roslin.ensembl.config.DBConnection.DataSource; 028import uk.ac.roslin.ensembl.dao.coreaccess.TranslationDAO; 029import uk.ac.roslin.ensembl.dao.factory.DAOCollectionCoreFactory; 030import uk.ac.roslin.ensembl.dao.factory.DAOSingleSpeciesCoreFactory; 031import uk.ac.roslin.ensembl.datasourceaware.core.DATranscript; 032import uk.ac.roslin.ensembl.datasourceaware.core.DATranslation; 033import uk.ac.roslin.ensembl.exception.DAOException; 034import uk.ac.roslin.ensembl.mapper.core.TranslationMapper; 035import uk.ac.roslin.ensembl.mapper.query.FeatureQuery; 036import uk.ac.roslin.ensembl.model.IdentifiableObject; 037import uk.ac.roslin.ensembl.model.core.Transcript; 038 039/** 040 * 041 * @author tpaterso 042 */ 043public class DBTranslationDAO extends DBCoreObjectDAO implements TranslationDAO { 044 045 public DBTranslationDAO() { 046 super(); 047 } 048 049 public DBTranslationDAO(DAOSingleSpeciesCoreFactory factory) { 050 051 super(factory); 052 } 053 054 public DBTranslationDAO(DAOCollectionCoreFactory factory) { 055 super(factory); 056 057 } 058 059 @Override 060 public DATranslation getTranslationByID(Integer id) throws DAOException { 061 if (id == null) { 062 return null; 063 } 064 065 FeatureQuery q = new FeatureQuery(); 066 q.setFeatureID(id); 067 068 DATranslation out = null; 069 SqlSession session = null; 070 071 try { 072 session = this.getFactory().getNewSqlSession(); 073 TranslationMapper mapper = session.getMapper(TranslationMapper.class); 074 out = mapper.getTranslation(q); 075 } catch (Exception e) { 076 throw new DAOException("Failed to call getTranslation", e); 077 } finally { 078 if (session != null) { 079 session.close(); 080 } 081 } 082 if (out!=null) { 083 out.setDaoFactory(daoFactory); 084 } 085 return out; 086 } 087 088 @Override 089 public DATranslation getTranslationByStableID(String stableID) throws DAOException { 090 if (stableID == null) { 091 return null; 092 } 093 094 FeatureQuery q = new FeatureQuery(); 095 q.setFeatureStableID(stableID); 096 097 DATranslation out = null; 098 SqlSession session = null; 099 100 try { 101 session = this.getFactory().getNewSqlSession(); 102 TranslationMapper mapper = session.getMapper(TranslationMapper.class); 103 out = mapper.getTranslation(q); 104 } catch (Exception e) { 105 throw new DAOException("Failed to call getTranslation", e); 106 } finally { 107 if (session != null) { 108 session.close(); 109 } 110 } 111 if (out!=null) { 112 out.setDaoFactory(daoFactory); 113 } 114 return out; 115 } 116 117 @Override 118 public List<DATranslation> getTranslationsForTranscript(Transcript transcript) throws DAOException { 119 120 if (transcript ==null || !(transcript instanceof DATranscript) || transcript.getId()==null ) { 121 return null; 122 } 123 124 DATranscript trc = (DATranscript) transcript; 125 126 SqlSession session = null; 127 List<DATranslation> result = new ArrayList<DATranslation>(); 128 129 try { 130 session = this.getFactory().getNewSqlSession(); 131 TranslationMapper mapper = session.getMapper(TranslationMapper.class); 132 result = mapper.getTranslationsForTranscript(trc); 133 } catch (Exception e) { 134 throw new DAOException("Failed to call getTranslationsForTranscript", e); 135 } finally { 136 if (session != null) { 137 session.close(); 138 } 139 } 140 141 if (result!=null && !result.isEmpty()) { 142 for (DATranslation trl: result) { 143 trl.setDaoFactory(daoFactory); 144 trl.setTranscript(trc); 145 trl.setSpecies(trc.getSpecies()); 146 } 147 } 148 149 trc.addTranslations(result); 150 return result; 151 152 } 153 154 @Override 155 public void reInitialize(IdentifiableObject object) throws DAOException { 156 157 if (object == null || !(object instanceof DATranslation)) { 158 throw new DAOException("Object not a DATranslation"); 159 } 160 161 DATranslation translation = (DATranslation) object; 162 163 //the DAO method requires a stableID 164 if (translation.getStableID() == null || translation.getStableID().isEmpty()) { 165 return; 166 } 167 168 DATranslation temp = this.getTranslationByStableID(translation.getStableID()); 169 170 if (temp != null) { 171 // to ensure no lazyloading 172 temp.setInitialized(true); 173 translation.setId(temp.getId()); 174 translation.setCreationDate(temp.getCreationDate()); 175 translation.setModificationDate(temp.getModificationDate()); 176 translation.setFirstExonID(temp.getFirstExonID()); 177 translation.setFirstExonStart(temp.getFirstExonStart()); 178 translation.setLastExonID(temp.getLastExonID()); 179 translation.setLastExonEnd(temp.getLastExonEnd()); 180 translation.setTranscriptID(temp.getTranscriptID()); 181 } 182 183 translation.setInitialized(true); 184 185 } 186 187 /** 188 * Returns a list of Ensembl Translations matching the query VegaID string. 189 * Vega currently curates gene, transcript and protein annotations for a human 190 * and and a few key regions of other vertebrate species. Calling this method 191 * on EnsemblGenomes (invertebrate) species will return an empty list by default. 192 * @param id a valid vega ID (these begin 'OTT...') 193 * 194 * @throws DAOException 195 */ 196 @Override 197 public List<DATranslation> getTranslationsForVegaID(String id) throws DAOException { 198 199 //return an empty list rather than null 200 List<DATranslation> out = new ArrayList<DATranslation>(); 201 202 if (id==null || id.isEmpty() 203 || !this.getFactory().getRegistry().getDatasourceType().equals(DataSource.ENSEMBLDB)) { 204 return out; 205 } 206 207 SqlSession session = null; 208 209 try { 210 session = this.getFactory().getNewSqlSession(); 211 TranslationMapper mapper = session.getMapper(TranslationMapper.class); 212 out = mapper.getVegaTranslations(id); 213 } catch (Exception e) { 214 throw new DAOException("Failed to call getTranslationsForVegaID", e); 215 } finally { 216 if (session != null) { 217 session.close(); 218 } 219 } 220 221 if (out == null) { 222 out = new ArrayList<DATranslation>(); 223 } 224 225 for (DATranslation t: out) { 226 t.setDaoFactory(this.getFactory()); 227 } 228 229 /* 230 * NOTE: we dont set the query VegaID on the return gene as this being null 231 * is the trigger for lazyloading a vega XRef. 232 * NOR can we make a valid Vega XRef at this point as we dont have enough information 233 * (ie we dont have its database ID). 234 */ 235 236// if (!out.isEmpty()) { 237// 238// ExternalDB edb = this.daoFactory.getDatabase().getExternalDB(ExternalDBType.VegaProtein.toString()); 239// DAXRef xref = new DAXRef(); 240// xref.setDaoFactory(this.getFactory()); 241// xref.setDB(edb); 242// xref.setPrimaryAccession(id); 243// xref.setDisplayID(id); 244// xref.setDBName(edb.getDBName()); 245// 246// for (DATranslation t : out) { 247// t.setDaoFactory(daoFactory); 248// t.setVegaProteinID(id); 249// t.addTypedXRefs(ExternalDBType.VegaProtein, new ArrayList<DAXRef>(Arrays.asList(xref))); 250// } 251// } 252 return out; 253 } 254 255}