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.datasourceaware.core;
022    
023    import java.util.ArrayList;
024    import java.util.Collection;
025    import java.util.List;
026    import java.util.TreeMap;
027    import org.apache.log4j.Logger;
028    import uk.ac.roslin.ensembl.dao.factory.DAOCoreFactory;
029    import uk.ac.roslin.ensembl.exception.DAOException;
030    import uk.ac.roslin.ensembl.model.ObjectType;
031    import uk.ac.roslin.ensembl.model.core.Transcript;
032    import uk.ac.roslin.ensembl.config.FeatureType;
033    import uk.ac.roslin.ensembl.model.core.Exon;
034    
035    /**
036     *
037     * @author tpaterso
038     */
039    public class DATranscript extends DAFeature implements Transcript{
040    
041        DATranslation translation = null;
042        private String stableID = null;
043        private String biotype = null;
044        private DAGene gene = null;
045        private boolean canonical = false;
046        private Integer geneID = null;
047        private TreeMap<Integer, DAExon> exonHash = null;
048        final static Logger LOGGER = Logger.getLogger(DATranscript.class);
049    
050        public DATranscript() {
051            super();
052        }
053    
054        public DATranscript(DAOCoreFactory daoFactory) {
055            super(daoFactory);
056        }
057    
058    
059        public ObjectType getType() {
060            return FeatureType.transcript;
061        }
062    
063        public DATranslation getTranslation() {
064            if (translation == null) {
065    
066                if (this.getId()==null) {
067                    return translation;
068                }
069    
070                try {
071                    translation = (DATranslation) this.getDaoFactory().getTranslationDAO().getTranslationByTranscriptID(this.id);
072                    translation.setTranscript(this);
073                }
074                catch (Exception e) {
075                    LOGGER.info("failed to get Translation", e);
076                }
077    
078            }
079            return translation;
080    
081    
082        }
083    
084        @Override
085        void reinitialize() throws DAOException {
086           //not used yet
087        }
088    
089        public String getStableID() {
090            return stableID;
091        }
092    
093        public void setStableID(String stableID) {
094            this.stableID = stableID;
095        }
096    
097        public String getBiotype() {
098            return this.biotype;
099        }
100    
101        public void setBiotype(String biotype) {
102            this.biotype = biotype;
103        }
104    
105        @Override
106        public DAGene getGene() {
107            if (gene==null && this.geneID != null) {
108                try {
109                    gene = (DAGene) this.getDaoFactory().getGeneDAO().getGeneByID(geneID);
110                } catch (Exception e) {
111                    LOGGER.info("Error thrown whilst trying to retrieve Gene for a Transcript", e );
112                }
113            }
114    
115            return gene;
116        }
117    
118        public void setGene(DAGene gene) {
119            this.gene = gene;
120        }
121    
122        public Integer getGeneID() {
123            return geneID;
124        }
125    
126        public void setGeneID(Integer geneID) {
127            this.geneID = geneID;
128        }
129    
130        public boolean isCanonical() {
131            return canonical;
132        }
133    
134        public void setCanonical(boolean canonical) {
135            this.canonical = canonical;
136        }
137    
138        @Override
139        public String getDisplayName() {
140            return (displayName!=null) ? displayName : stableID ;
141        }
142    
143        @Override
144        public Collection<DAExon> getExons() {
145            if (exonHash == null) {
146    
147                try {
148                    exonHash = new TreeMap<Integer, DAExon>();
149                    return (Collection<DAExon>) this.getDaoFactory().getExonDAO().getExonsForTranscript(this);
150                } catch (Exception e) {
151                    exonHash = new TreeMap<Integer, DAExon>();
152                    LOGGER.info("Threw DAOException on tryiong to populate exons for a transcript", e);
153                }
154    
155            }
156            return this.exonHash.values();
157        }
158    
159        public void addExon(Exon exon) {
160            if (exonHash==null) {
161                 exonHash = new TreeMap<Integer, DAExon>();
162            }
163            try {
164                DAExon e = (DAExon) exon;
165                this.exonHash.put(e.getRank(), e);
166            } catch (Exception ex) {
167                LOGGER.info("failed to add an exon to the transcript ", ex);
168            }
169        }
170    
171    }