001 /** 002 * Copyright (C) 2010 The Roslin Institute <contact andy.law@roslin.ed.ac.uk> 003 * 004 * This file is part of the Ensembl Java API demonstration project developed by the 005 * Bioinformatics Group at The Roslin Institute, The Royal (Dick) School of 006 * Veterinary Studies, University of Edinburgh. 007 * 008 * This is free software: you can redistribute it and/or modify 009 * it under the terms of the GNU General Public License (version 3) as published by 010 * the Free Software Foundation. 011 * 012 * This software is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * in this software distribution. If not, see <http://www.gnu.org/licenses/gpl-3.0.html/>. 019 */ 020 021 package uk.ac.roslin.ensembl.dao.database.coreaccess; 022 023 import uk.ac.roslin.ensembl.datasourceaware.core.DACoordinateSystem; 024 import java.util.HashMap; 025 import java.util.List; 026 import org.apache.ibatis.session.SqlSession; 027 import uk.ac.roslin.ensembl.model.Mapping; 028 import uk.ac.roslin.ensembl.model.MappingSet; 029 import uk.ac.roslin.ensembl.dao.coreaccess.AssemblyDAO; 030 import uk.ac.roslin.ensembl.datasourceaware.core.DAAssembledDNASequence; 031 import uk.ac.roslin.ensembl.datasourceaware.core.DADNASequence; 032 import uk.ac.roslin.ensembl.dao.database.factory.DBDAOCollectionCoreFactory; 033 import uk.ac.roslin.ensembl.dao.database.factory.DBDAOSingleSpeciesCoreFactory; 034 import uk.ac.roslin.ensembl.dao.factory.DAOCollectionCoreFactory; 035 import uk.ac.roslin.ensembl.dao.factory.DAOSingleSpeciesCoreFactory; 036 import uk.ac.roslin.ensembl.mapper.core.SequenceMapper; 037 import uk.ac.roslin.ensembl.exception.DAOException; 038 import uk.ac.roslin.ensembl.model.core.CoordinateSystem; 039 import uk.ac.roslin.ensembl.model.core.DNASequence; 040 041 /** 042 * 043 * @author paterson 044 */ 045 public class DBAssemblyDAO extends DBCoreObjectDAO implements AssemblyDAO{ 046 047 public DBAssemblyDAO() { 048 super(); 049 } 050 051 public DBAssemblyDAO(DAOSingleSpeciesCoreFactory factory) { 052 super(factory); 053 } 054 055 public DBAssemblyDAO(DAOCollectionCoreFactory factory) { 056 super(factory); 057 } 058 059 @Override 060 public MappingSet getComponentMappingsByStartStop(DNASequence assembledSequence, Integer begin, Integer stop) throws DAOException { 061 062 063 if (assembledSequence==null || begin==null || stop==null 064 || assembledSequence.getId() == null 065 || assembledSequence.getCoordSystem() == null 066 || assembledSequence.getCoordSystem().getId() == null ) { 067 throw new DAOException("Invalid parameters passed to getComponentMappingsByStartStopDNASequence assembledSequence, Integer begin, Integer stop "); 068 } 069 070 MappingSet componentMappings = new MappingSet(); 071 List<Mapping> results; 072 DAAssembledDNASequence parent; 073 SqlSession session = null; 074 075 try { 076 parent = (DAAssembledDNASequence) assembledSequence; 077 } catch (ClassCastException e) { 078 throw new DAOException("This sequence is not an assembly!"); 079 } 080 081 Integer start = begin; 082 Integer end = stop; 083 CoordinateSystem seqLevelCS = null; 084 085 try { 086 if (singleSpecies) { 087 seqLevelCS = ssFactory.getDatabase().getSequenceLevelCoordSystem(); 088 } else { 089 seqLevelCS = collFactory.getDatabase().getSequenceLevelCS(species); 090 } 091 092 if (seqLevelCS==null || seqLevelCS.getId()==null ) { 093 throw new DAOException("Failed to call to retrieve the CoordinateSystem for Sequences"); 094 } 095 096 Integer seqLevelCoordSysID = seqLevelCS.getId(); 097 Integer parentCoordSysID = parent.getCoordSystem().getId(); 098 099 if (seqLevelCoordSysID == parentCoordSysID) { 100 //the parent is at sequence level - so dont need a projection 101 //probably prevent this call happening before here 102 throw new DAOException("This sequence is not an assembly!"); 103 } 104 105 Integer seqRegionID = parent.getId(); 106 107 //none of these should be null 108 109 HashMap parameters = new HashMap(); 110 parameters.put("seqRegionID", seqRegionID); 111 parameters.put("start", start); 112 parameters.put("end", end); 113 parameters.put("parentCoordSysID", parentCoordSysID); 114 parameters.put("seqLevelCoordSysID", seqLevelCoordSysID); 115 116 117 118 119 120 session = this.getFactory().getNewSqlSession(); 121 SequenceMapper mapper = session.getMapper(SequenceMapper.class); 122 results = mapper.getComponentSequences(parameters); 123 } catch (Exception e) { 124 throw new DAOException("Failed to call getComponentMappingsByStartStop", e); 125 } finally { 126 if (session != null) { 127 session.close(); 128 } 129 } 130 131 if (results ==null || results.isEmpty()) { 132 return componentMappings; 133 } 134 135 if (results != null) { 136 for (Mapping mapping: results) { 137 138 mapping.setSource(parent); 139 //everything returned from this query is a sequence level component 140 ((DADNASequence) mapping.getTarget()).setDaoFactory(daoFactory); 141 //this is now done by mybatis 142 //((DADNASequence) mapping.getTarget()).setSequenceStorage(mapping.getReader()); 143 144 //((DADNASequence) mapping.getTarget()).setType(seqLevelCS.getType()); 145 ((DADNASequence) mapping.getTarget()).setCoordSystem(seqLevelCS); 146 147 //this adds the reverse mapping to the DA DNASequence - 148 //not sure if this is required, or if better stored as another type of relationship 149 150 if (componentMappings.add(mapping)) { 151 Mapping.addReverseMapping(mapping); 152 } 153 } 154 } 155 156 157 return componentMappings; 158 159 } 160 161 162 163 }