001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * Created on 10-18-2010
021 *
022 * @author Andy Yates
023 */
024package org.biojava.nbio.core.sequence.views;
025
026import org.biojava.nbio.core.exceptions.CompoundNotFoundException;
027import org.biojava.nbio.core.sequence.compound.NucleotideCompound;
028import org.biojava.nbio.core.sequence.compound.RNACompoundSet;
029import org.biojava.nbio.core.sequence.template.*;
030
031import java.util.HashMap;
032import java.util.Map;
033
034/**
035 * Attempts to do on the fly translation of RNA by not requesting the compounds
036 * until asked.
037 *
038 * @author ayates
039 */
040public class RnaSequenceView extends SequenceProxyView<NucleotideCompound> implements ProxySequenceReader<NucleotideCompound> {
041
042        private CompoundSet<NucleotideCompound> rnaCompounds;
043        private Map<NucleotideCompound, NucleotideCompound> dnaToRna = null;
044        private Map<NucleotideCompound, NucleotideCompound> rnaToDna = null;
045
046        public RnaSequenceView(Sequence<NucleotideCompound> sourceDna) {
047                this(sourceDna, RNACompoundSet.getRNACompoundSet());
048        }
049
050        public RnaSequenceView(Sequence<NucleotideCompound> sourceDna,
051                        CompoundSet<NucleotideCompound> rnaCompounds) {
052                super(sourceDna);
053                this.rnaCompounds = rnaCompounds;
054        }
055
056        @Override
057        public String getSequenceAsString() {
058                return SequenceMixin.toString(this);
059        }
060
061        @Override
062        public NucleotideCompound getCompoundAt(int position) {
063                NucleotideCompound dna = getViewedSequence().getCompoundAt(position);
064                return getDnaToRna().get(dna);
065        }
066
067        @Override
068        public int getIndexOf(NucleotideCompound compound) {
069                return getViewedSequence().getIndexOf(getRnaToDna().get(compound));
070        }
071
072        @Override
073        public int getLastIndexOf(NucleotideCompound compound) {
074                return getViewedSequence().getLastIndexOf(getRnaToDna().get(compound));
075        }
076
077        public Map<NucleotideCompound, NucleotideCompound> getRnaToDna() {
078                if(rnaToDna == null) {
079                        buildTranslators();
080                }
081                return rnaToDna;
082        }
083
084        public Map<NucleotideCompound, NucleotideCompound> getDnaToRna() {
085                if(dnaToRna == null) {
086                        buildTranslators();
087                }
088                return dnaToRna;
089        }
090
091        protected void buildTranslators() {
092                Map<NucleotideCompound, NucleotideCompound> localDnaToRna =
093                                new HashMap<NucleotideCompound, NucleotideCompound>();
094                Map<NucleotideCompound, NucleotideCompound> localRnaToDna =
095                                new HashMap<NucleotideCompound, NucleotideCompound>();
096
097                NucleotideCompound thymine =
098                                getViewedSequence().getCompoundSet().getCompoundForString("T");
099                NucleotideCompound lowerThymine =
100                                getViewedSequence().getCompoundSet().getCompoundForString("t");
101
102                for (NucleotideCompound dnaBase : getViewedSequence().getCompoundSet().getAllCompounds()) {
103                        NucleotideCompound equivalent;
104                        if (dnaBase.equals(thymine)) {
105                                equivalent = rnaCompounds.getCompoundForString("U");
106                        }
107                        else if (dnaBase.equals(lowerThymine)) {
108                                equivalent = rnaCompounds.getCompoundForString("u");
109                        }
110                        else {
111                                equivalent = rnaCompounds.getCompoundForString(
112                                        dnaBase.toString());
113                        }
114                        localDnaToRna.put(dnaBase, equivalent);
115                        localRnaToDna.put(equivalent, dnaBase);
116                }
117                this.dnaToRna = localDnaToRna;
118                this.rnaToDna = localRnaToDna;
119        }
120
121        @Override
122        public void setCompoundSet(CompoundSet<NucleotideCompound> compoundSet) {
123                throw new UnsupportedOperationException("Unsupported operation; create a new viewed sequence");
124        }
125
126        @Override
127        public void setContents(String sequence) throws CompoundNotFoundException {
128                throw new UnsupportedOperationException("Unsupported operation; create a new viewed sequence");
129        }
130}