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 */
021package org.biojava.nbio.core.sequence.transcription;
022
023import org.biojava.nbio.core.sequence.RNASequence;
024import org.biojava.nbio.core.sequence.compound.NucleotideCompound;
025import org.biojava.nbio.core.sequence.io.template.SequenceCreatorInterface;
026import org.biojava.nbio.core.sequence.template.AbstractCompoundTranslator;
027import org.biojava.nbio.core.sequence.template.CompoundSet;
028import org.biojava.nbio.core.sequence.template.ProxySequenceReader;
029import org.biojava.nbio.core.sequence.template.Sequence;
030import org.biojava.nbio.core.sequence.views.RnaSequenceView;
031
032import java.util.ArrayList;
033import java.util.List;
034
035/**
036 * Performs the first stage of transcription by going from DNA to RNA. This
037 * class will first delegate to {@link Frame} in order to be in the correctly
038 * specified translation frame and then translates T to U. The other
039 * translation carried out is to convert an equivalent compound in DNA to RNA
040 * i.e. for the base A in DNA fetching the equivalent A base in the RNA
041 * {@link CompoundSet}.
042 *
043 * @author ayates
044 */
045public class DNAToRNATranslator extends AbstractCompoundTranslator<NucleotideCompound, NucleotideCompound> {
046
047                private final boolean shortCutTranslation;
048
049        public DNAToRNATranslator(SequenceCreatorInterface<NucleotideCompound> rnaCreator,
050                        CompoundSet<NucleotideCompound> dna, CompoundSet<NucleotideCompound> rna,
051                        boolean shortCutTranslation) {
052                super(rnaCreator, dna, rna);
053                this.shortCutTranslation = shortCutTranslation;
054                defaultMappings();
055                thyamineToUracil();
056        }
057
058                /**
059                 * Overloaded local version which delegates to an optional translator
060                 * when told to (specified during construction).
061                 *
062                 * @param originalSequence The DNA sequence to translate
063                 * @return The translated single sequence
064                 */
065                @Override
066                public List<Sequence<NucleotideCompound>> createSequences(Sequence<NucleotideCompound> originalSequence) {
067                                if(shortCutTranslation) {
068                                                List<Sequence<NucleotideCompound>> result = new ArrayList<Sequence<NucleotideCompound>>(1);
069                                                result.add(wrapToRna(originalSequence));
070                                                return result;
071                                }
072                                else {
073                                                return super.createSequences(originalSequence);
074                                }
075                }
076
077                /**
078                 * Takes in the given DNA Sequence and returns an instance of RNASequence
079                 * which is using {@link RnaSequenceView} as a
080                 * {@link ProxySequenceReader}.
081                 */
082                protected RNASequence wrapToRna(Sequence<NucleotideCompound> dna) {
083                                ProxySequenceReader<NucleotideCompound> rnaView = new RnaSequenceView(dna);
084                                return new RNASequence(rnaView);
085                }
086
087        private void defaultMappings() {
088                NucleotideCompound thymine = getFromCompoundSet().getCompoundForString("T");
089                for(NucleotideCompound dnaBase: getFromCompoundSet().getAllCompounds()) {
090                        if(dnaBase.equalsIgnoreCase(thymine)) {
091                                continue;
092                        }
093                        NucleotideCompound rnaBase = getToCompoundSet().getCompoundForString(
094                                        dnaBase.toString());
095                        addCompounds(dnaBase, rnaBase);
096                }
097
098        }
099
100        private void thyamineToUracil() {
101                addCompounds(getFromCompoundSet().getCompoundForString("T"),
102                                getToCompoundSet().getCompoundForString("U"));
103                addCompounds(getFromCompoundSet().getCompoundForString("t"),
104                                getToCompoundSet().getCompoundForString("u"));
105        }
106
107        public Sequence<NucleotideCompound> createSequence(Sequence<NucleotideCompound> originalSequence, Frame frame) {
108                Sequence<NucleotideCompound> wrapped = frame.wrap(originalSequence);
109                return super.createSequence(wrapped);
110        }
111
112        @Override
113        public Sequence<NucleotideCompound> createSequence(Sequence<NucleotideCompound> originalSequence) {
114                return createSequence(originalSequence, Frame.getDefaultFrame());
115        }
116
117        @Override
118        protected void postProcessCompoundLists(
119                        List<List<NucleotideCompound>> compoundLists) {
120                //No post processing needed
121        }
122}