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