Skip to content

Commit 8c508f8

Browse files
committed
Added an option for waitForStartCodon to RNA To Amino Acid Translator,
and implemented in Transcription Engine and Builder. Implemented Test Case
1 parent 398ad5e commit 8c508f8

3 files changed

Lines changed: 103 additions & 13 deletions

File tree

biojava3-core/src/main/java/org/biojava3/core/sequence/transcription/RNAToAminoAcidTranslator.java

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

biojava3-core/src/main/java/org/biojava3/core/sequence/transcription/TranscriptionEngine.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ public static class Builder {
190190
private boolean trimStop = true;
191191
private boolean translateNCodons = true;
192192
private boolean decorateRna = false;
193-
//Set at false for backwards compatibility
193+
// Set at false for backwards compatibility
194194
private boolean stopAtStopCodons = false;
195+
private boolean waitForStartCodon = false;
195196

196197
/**
197198
* The method to finish any calls to the builder with which returns a
@@ -284,12 +285,24 @@ public Builder translateNCodons(boolean translateNCodons) {
284285
return this;
285286
}
286287

287-
/** If set, then the last codon translated in the resulting peptide sequence will be the stop codon */
288+
/**
289+
* If set, then the last codon translated in the resulting peptide
290+
* sequence will be the stop codon
291+
*/
288292
public Builder stopAtStopCodons(boolean stopAtStopCodons) {
289293
this.stopAtStopCodons = stopAtStopCodons;
290294
return this;
291295
}
292296

297+
/**
298+
* If set, then translation will not start until a start codon is
299+
* encountered
300+
*/
301+
public Builder waitForStartCodon(boolean waitForStartCodon) {
302+
this.waitForStartCodon = waitForStartCodon;
303+
return this;
304+
}
305+
293306
/**
294307
* Performs an optimisation where RNASequences are not translated into
295308
* their own objects but are views onto the base DNA sequence.
@@ -337,7 +350,8 @@ private RNAToAminoAcidTranslator getRnaAminoAcidTranslator() {
337350
return new RNAToAminoAcidTranslator(getProteinCreator(),
338351
getRnaCompounds(), getCodons(), getAminoAcidCompounds(),
339352
getTable(), isTrimStop(), isInitMet(),
340-
isTranslateNCodons(), isStopAtStopCodons());
353+
isTranslateNCodons(), isStopAtStopCodons(),
354+
isWaitForStartCodon());
341355
}
342356

343357
private CompoundSet<Codon> getCodons() {
@@ -386,5 +400,9 @@ private boolean isDecorateRna() {
386400
private boolean isStopAtStopCodons() {
387401
return stopAtStopCodons;
388402
}
403+
404+
private boolean isWaitForStartCodon() {
405+
return waitForStartCodon;
406+
}
389407
}
390408
}

biojava3-core/src/test/java/org/biojava3/core/sequence/TranslationTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,20 @@ public void translateStopAtInternalStops(){
184184
String testpep = volvoxPep.getSequenceAsString().split("\\*")[0];
185185
assertThat("Translation stops at Stop", pep, is(testpep));
186186
}
187+
188+
@Test
189+
public void waitForStartCodon(){
190+
//Should not start translation until a start codon is encountered
191+
TranscriptionEngine e = new TranscriptionEngine.Builder().waitForStartCodon(true).build();
192+
RNASequence rna = new RNASequence("UCCAUGAGC");
193+
String pep = rna.getProteinSequence(e).getSequenceAsString();
194+
assertThat("Translation starts at Start Codon",pep, is("MS"));
195+
196+
//And should start at start of sequence (NB this is implied by success of all other tests)
197+
e = new TranscriptionEngine.Builder().waitForStartCodon(false).build();
198+
pep = rna.getProteinSequence(e).getSequenceAsString();
199+
assertThat("Translation starts at start of sequence", pep, is("SMS"));
200+
}
187201

188202
@Test
189203
public void translateInitMet() {

0 commit comments

Comments
 (0)