@@ -60,11 +60,17 @@ public class RNAToAminoAcidTranslator extends
6060 private final AminoAcidCompound unknownAminoAcidCompound ;
6161 private final AminoAcidCompound methionineAminoAcidCompound ;
6262 private final boolean translateNCodons ;
63+
6364 // If true, then translation will stop at the first stop codon encountered
6465 // in the reading frame (the stop codon will be included as the last residue
6566 // in the resulting ProteinSequence, unless removed by #trimStops)
6667 private final boolean stopAtStopCodons ;
6768
69+ // If true, then translation will not start until the first start codon
70+ // encountered in the reading frame. The start codon will be included as the
71+ // first residue in the resulting ProteinSequence
72+ private final boolean waitForStartCodon ;
73+
6874 /**
6975 * @deprecated Retained for backwards compatability, setting
7076 * {@link #stopAtStopCodons} to <code>false</code>
@@ -103,8 +109,10 @@ public RNAToAminoAcidTranslator(
103109 methionineAminoAcidCompound = aminoAcids .getCompoundForString ("M" );
104110 // Set to false for backwards compatability
105111 stopAtStopCodons = false ;
112+ waitForStartCodon = false ;
106113 }
107114
115+ @ Deprecated
108116 public RNAToAminoAcidTranslator (
109117 SequenceCreatorInterface <AminoAcidCompound > creator ,
110118 CompoundSet <NucleotideCompound > nucleotides ,
@@ -138,6 +146,44 @@ public RNAToAminoAcidTranslator(
138146 unknownAminoAcidCompound = aminoAcids .getCompoundForString ("X" );
139147 methionineAminoAcidCompound = aminoAcids .getCompoundForString ("M" );
140148 this .stopAtStopCodons = stopAtStopCodons ;
149+ // Set for backwards compatibility
150+ waitForStartCodon = false ;
151+ }
152+
153+ public RNAToAminoAcidTranslator (
154+ SequenceCreatorInterface <AminoAcidCompound > creator ,
155+ CompoundSet <NucleotideCompound > nucleotides ,
156+ CompoundSet <Codon > codons ,
157+ CompoundSet <AminoAcidCompound > aminoAcids , Table table ,
158+ boolean trimStops , boolean initMetOnly , boolean translateNCodons ,
159+ boolean stopAtStopCodons , boolean waitForStartCodon ) {
160+
161+ super (creator , nucleotides , aminoAcids );
162+ this .trimStops = trimStops ;
163+ this .initMetOnly = initMetOnly ;
164+ this .translateNCodons = translateNCodons ;
165+
166+ quickLookup = new HashMap <Table .CaseInsensitiveTriplet , Codon >(codons
167+ .getAllCompounds ().size ());
168+ aminoAcidToCodon = new HashMap <AminoAcidCompound , List <Codon >>();
169+
170+ List <Codon > codonList = table .getCodons (nucleotides , aminoAcids );
171+ for (Codon codon : codonList ) {
172+ quickLookup .put (codon .getTriplet (), codon );
173+ codonArray [codon .getTriplet ().intValue ()] = codon ;
174+
175+ List <Codon > codonL = aminoAcidToCodon .get (codon .getAminoAcid ());
176+ if (codonL == null ) {
177+ codonL = new ArrayList <Codon >();
178+ aminoAcidToCodon .put (codon .getAminoAcid (), codonL );
179+ }
180+ codonL .add (codon );
181+
182+ }
183+ unknownAminoAcidCompound = aminoAcids .getCompoundForString ("X" );
184+ methionineAminoAcidCompound = aminoAcids .getCompoundForString ("M" );
185+ this .stopAtStopCodons = stopAtStopCodons ;
186+ this .waitForStartCodon = waitForStartCodon ;
141187 }
142188
143189 /**
@@ -157,6 +203,9 @@ public List<Sequence<AminoAcidCompound>> createSequences(
157203
158204 boolean first = true ;
159205
206+ // If not waiting for a start codon, start translating immediately
207+ boolean doTranslate = !waitForStartCodon ;
208+
160209 for (SequenceView <NucleotideCompound > element : iter ) {
161210 AminoAcidCompound aminoAcid = null ;
162211
@@ -168,19 +217,28 @@ public List<Sequence<AminoAcidCompound>> createSequences(
168217 Codon target = null ;
169218
170219 target = quickLookup .get (triplet );
171- if (target != null )
172- aminoAcid = target .getAminoAcid ();
173- if (aminoAcid == null && translateNCodons ()) {
174- aminoAcid = unknownAminoAcidCompound ;
175- } else {
176- if (first && initMetOnly && target .isStart ()) {
177- aminoAcid = methionineAminoAcidCompound ;
178- }
220+
221+ // Check for a start
222+ if (doTranslate == false && target .isStart ()) {
223+ doTranslate = true ;
179224 }
225+
226+ if (doTranslate ) {
227+ if (target != null )
228+ aminoAcid = target .getAminoAcid ();
229+ if (aminoAcid == null && translateNCodons ()) {
230+ aminoAcid = unknownAminoAcidCompound ;
231+ } else {
232+ if (first && initMetOnly && target .isStart ()) {
233+ aminoAcid = methionineAminoAcidCompound ;
234+ }
235+ }
180236
181- addCompoundsToList (Arrays .asList (aminoAcid ), workingList );
237+ addCompoundsToList (Arrays .asList (aminoAcid ), workingList );
238+ }
182239
183- if (stopAtStopCodons && target .isStop ()) {
240+ if (doTranslate && stopAtStopCodons && target .isStop ()) {
241+ // Check if we need to stop, but dont stop until started!
184242 break ;
185243 }
186244
0 commit comments