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     */
024    package org.biojava3.core.sequence.views;
025    
026    import java.util.HashMap;
027    import java.util.Map;
028    import org.biojava3.core.sequence.compound.NucleotideCompound;
029    import org.biojava3.core.sequence.compound.RNACompoundSet;
030    import org.biojava3.core.sequence.template.CompoundSet;
031    import org.biojava3.core.sequence.template.ProxySequenceReader;
032    import org.biojava3.core.sequence.template.SequenceProxyView;
033    import org.biojava3.core.sequence.template.Sequence;
034    import org.biojava3.core.sequence.template.SequenceMixin;
035    
036    /**
037     * Attempts to do on the fly translation of RNA by not requesting the compounds
038     * until asked.
039     *
040     * @author ayates
041     */
042    public class RnaSequenceView extends SequenceProxyView<NucleotideCompound> implements ProxySequenceReader<NucleotideCompound> {
043    
044        private CompoundSet<NucleotideCompound> rnaCompounds;
045        private Map<NucleotideCompound, NucleotideCompound> dnaToRna = null;
046        private Map<NucleotideCompound, NucleotideCompound> rnaToDna = null;
047    
048        public RnaSequenceView(Sequence<NucleotideCompound> sourceDna) {
049            this(sourceDna, RNACompoundSet.getRNACompoundSet());
050        }
051    
052        public RnaSequenceView(Sequence<NucleotideCompound> sourceDna,
053                CompoundSet<NucleotideCompound> rnaCompounds) {
054            super(sourceDna);
055            this.rnaCompounds = rnaCompounds;
056        }
057    
058        @Override
059        public String getSequenceAsString() {
060            return SequenceMixin.toString(this);
061        }
062    
063        @Override
064        public NucleotideCompound getCompoundAt(int position) {
065            NucleotideCompound dna = getViewedSequence().getCompoundAt(position);
066            return getDnaToRna().get(dna);
067        }
068    
069        @Override
070        public int getIndexOf(NucleotideCompound compound) {
071            return getViewedSequence().getIndexOf(getRnaToDna().get(compound));
072        }
073    
074        @Override
075        public int getLastIndexOf(NucleotideCompound compound) {
076            return getViewedSequence().getLastIndexOf(getRnaToDna().get(compound));
077        }
078    
079        public Map<NucleotideCompound, NucleotideCompound> getRnaToDna() {
080            if(rnaToDna == null) {
081                buildTranslators();
082            }
083            return rnaToDna;
084        }
085    
086        public Map<NucleotideCompound, NucleotideCompound> getDnaToRna() {
087            if(dnaToRna == null) {
088                buildTranslators();
089            }
090            return dnaToRna;
091        }
092    
093        protected void buildTranslators() {
094            Map<NucleotideCompound, NucleotideCompound> localDnaToRna =
095                    new HashMap<NucleotideCompound, NucleotideCompound>();
096            Map<NucleotideCompound, NucleotideCompound> localRnaToDna =
097                    new HashMap<NucleotideCompound, NucleotideCompound>();
098    
099            NucleotideCompound thymine =
100                    getViewedSequence().getCompoundSet().getCompoundForString("T");
101            NucleotideCompound lowerThymine =
102                    getViewedSequence().getCompoundSet().getCompoundForString("t");
103    
104            for (NucleotideCompound dnaBase : getViewedSequence().getCompoundSet().getAllCompounds()) {
105                NucleotideCompound equivalent;
106                if (dnaBase.equals(thymine)) {
107                    equivalent = rnaCompounds.getCompoundForString("U");
108                }
109                else if (dnaBase.equals(lowerThymine)) {
110                    equivalent = rnaCompounds.getCompoundForString("u");
111                }
112                else {
113                    equivalent = rnaCompounds.getCompoundForString(
114                        dnaBase.toString());
115                }
116                localDnaToRna.put(dnaBase, equivalent);
117                localRnaToDna.put(equivalent, dnaBase);
118            }
119            this.dnaToRna = localDnaToRna;
120            this.rnaToDna = localRnaToDna;
121        }
122    
123        @Override
124        public void setCompoundSet(CompoundSet<NucleotideCompound> compoundSet) {
125            throw new UnsupportedOperationException("Unsupported operation; create a new viewed sequence");
126        }
127    
128        @Override
129        public void setContents(String sequence) {
130            throw new UnsupportedOperationException("Unsupported operation; create a new viewed sequence");
131        }
132    }