Assignment 3: Translating RNA
Goals: Learn to use the String class; practice working with lists; practice accumulators.
Instructions
This is a standalone variant of Assignment 3.
Be very, very careful with naming. The solution files expect your submissions to use the class, interface, field, method, constructor, argument, and file names specified here.
The submission will be organized as follows:
Homework 3 Problem 1: The RNA.java file
1 Problem 1: Translating RNA
For all questions in this problem, be sure to follow the design recipe carefully:
Give sufficient examples of data, and sufficient tests, to test your methods thoroughly.
If you find yourself wanting to use a field-of-field, stop. Fill out the template for each method, and figure out another design.
Think carefully about how to use dynamic dispatch, and where to define methods, to keep your code as simple and clean as possible.
Note: you may want to use the String class’s .length() method, which returns the number of characters in the string.
In this problem, you are going to simulate part of the process of transforming DNA into proteins. We will represent a DNA template strand as an ILoString of length-1 strings representing the DNA bases "A", "T", "C" or "G". An RNA sequence is similar, except it uses the base "U" instead of "T".
Use exactly the following list representation, including the class, interface, field, and constructor names and types shown here. You may add methods as the problem requires, but do not rename these parts of the data definition.
// a list of strings interface ILoString { } // an empty list of strings class MtLoString implements ILoString { MtLoString() { } } // a nonempty list of strings class ConsLoString implements ILoString { String first; ILoString rest; ConsLoString(String first, ILoString rest) { this.first = first; this.rest = rest; } } // a list of lists of strings interface ILoLoString { } // an empty list of lists of strings class MtLoLoString implements ILoLoString { MtLoLoString() { } } // a nonempty list of lists of strings class ConsLoLoString implements ILoLoString { ILoString first; ILoLoString rest; ConsLoLoString(ILoString first, ILoLoString rest) { this.first = first; this.rest = rest; } }
As a warmup problem (not graded; not required), design a method transcribe that produces the complementary RNA sequence from this DNA template strand by transforming each base:
DNA template base | RNA base |
A | U |
T | A |
C | G |
G | C |
In this paragraph, RNA sequences are written as multi-letter strings for brevity, but in your representation, they will be lists of length-1 strings.
The following edge cases are part of the required behavior:
An empty RNA sequence, or one containing only one or two bases, produces no proteins.
If the first complete codon is a stop codon, produce one empty protein, then continue at the base immediately following that stop codon.
After every stop codon, start a fresh codon frame at the next base.
Ignore one or two trailing bases that do not complete a codon; they are not a codon and do not create another protein.
For example, the RNA sequence "ACAGAUAG" translates into a single protein ("ACA", "GAU"): even though the last three bases are a stop codon, that is just a coincidence. The way the breakpoints fall between codons means that they get split up, and the last two bases just get ignored.
For another example, "ACAUAGUUG" translates into two proteins, each one codon long: "ACA" and "UUG".
Design a method translate that transforms an RNA sequence (an ILoString) into a list of proteins (i.e. an ILoLoString). This will be the only method we directly call to test your code; however, you will certainly need to design some helper methods. Our tests will not care what order you produce the proteins in, but they will care that each protein’s codons are in the correct order.
For the required translate method, translation begins at the very first base of the RNA sequence. Do not search for "AUG" before beginning. For this method, "AUG" is an ordinary non-stop codon.
Hint: you will certainly need to use at least one accumulator parameter in at least one helper method. There are several valid designs for this problem; work through a wish-list process to figure out what helpers you might want.
Hint: you probably do not need to define any methods on ILoLoString for this problem.
Hint: Be sure to test RNA sequences that produce proteins longer than just one codon: this will motivate needing at least one more helper method.
Optional ungraded extension
This extension is optional practice and carries no course points.
The story above is certainly not a complete description of RNA translation. To be slightly more accurate: RNA only starts translating into proteins when it encounters the start codon "AUG".
Leave the required translate method unchanged. Instead, design a second method with this signature:
ILoLoString translateFromStartCodons();
This method skips bases until it encounters "AUG", includes that "AUG" as the first codon of a protein, and translates until a stop codon. After a stop codon, it resumes searching for the next "AUG"; any bases in the gap are ignored.
The required grader calls only translate; it will not call translateFromStartCodons. If you try this extension, test the additional method yourself and submit the assignment only once.