← all problems

DNA: Bases and Codons

Write fourteen small methods in Bases.java that treat DNA the way a cell does: one base or one codon at a time. You will pair bases, copy DNA into RNA, read a codon off a sequence, look an amino acid up in the genetic code, and decide what a mutation does to a protein. Read the Instructions tab first: it explains the biology and every method. Use Run code to experiment with your own main method (ungraded); Run tests grades everything without saving a score; Submit tests grades and saves it.

You're not logged in — Run code and Run tests work fine without an account, but Submit tests needs one to save your score, and your in-progress code won't be saved between visits either. Log in with Google before you submit.

Bases.java — editable

Background


Every living thing stores instructions in DNA, and a cell reads those instructions in small, exact steps. You will write the first of those steps in Java: working with one base or one codon at a time. Strings and if statements are all you need; the next lab adds loops and reads whole genomes.

DNA is a string of four letters


DNA is a long chain of four kinds of building block, named for their bases: A (adenine), C (cytosine), G (guanine) and T (thymine). In this lab a base is a one-letter String ("A", not 'A'), and a piece of DNA is a String of them, like "ATGGCC". You will pull one base out with substring(i, i + 1) and compare it with equals, exactly like the Strings you already know.

DNA is a double strand, and the two strands are held together by base pairing: A pairs with T, and C pairs with G. So if one strand reads ATGC, the other strand, read in the same direction, is TACG. That partner letter is the base's complement.

Two strands of DNA, with A paired to T and C paired to G, and the strands running in opposite directions

Two groups of bases have names. A and G are purines (two rings), and C and T are pyrimidines (one ring). G and C are special for another reason: they are joined by three bonds, while A and T are joined by two, so a stretch of DNA that is rich in G and C is held together a little more tightly.

From DNA to protein


A gene is read in two steps. First the cell transcribes a stretch of DNA into a related molecule called RNA. RNA is the same as DNA except that it has U (uracil) where DNA has T. So transcribing a coding-strand base means turning T into U and leaving A, C and G alone.

Second, the cell translates the RNA into a protein. It reads three bases at a time. Each group of three is a codon, and each codon stands for one amino acid (the building blocks of proteins) or for a stop signal. There are 4 × 4 × 4 = 64 codons and only 20 amino acids, so most amino acids have more than one codon: GCT, GCC, GCA and GCG all mean alanine. Biologists write the amino acids as one letter each (M is methionine, W is tryptophan) and a stop as *. The codon ATG means methionine and is also where translation usually starts; the stops are TAA, TAG and TGA.

The genetic code as a table: the first base picks a block, the second a row, the third a column

You will not type out 64 cases. The starter code gives you the whole code as one 64-letter String, with the codons in the order T, C, A, G. First you write a small helper, baseIndex, that says where a base sits in that order: "T" is 0, "C" is 1, "A" is 2, "G" is 3, and anything that is not exactly one of those four capital letters is -1 (so "", "AC", "a" and "N" all give -1). Then the amino acid for a codon is the letter at index 16 × (the index of the first base) + 4 × (the index of the second base) + (the index of the third base), where each index is whatever baseIndex returns. For ATG: baseIndex("A") is 2, baseIndex("T") is 0 and baseIndex("G") is 3, so the index is 16 2 + 4 0 + 3 = 35, and the 36th letter of the table is M.

Mutations


A mutation is a change to the DNA. If it changes one base inside a codon, the effect on the protein depends on what the new codon means:
  • Silent: the new codon means the same amino acid (GCT to GCC, both alanine). The protein does not change.
  • Missense: a different amino acid. This is the kind of change behind sickle-cell disease: GAG (glutamic acid) became GTG (valine) in one gene.
  • Nonsense: an amino acid codon became a stop (TGG to TGA), so the protein is cut short.
  • Stop-lost: a stop codon became an amino acid, so the protein keeps going when it should have ended.

Adding or removing bases is different. The cell reads in threes, so adding or removing a number of bases that is not a multiple of 3 shifts the grouping of every codon after that point (a frameshift), usually ruining the protein. Adding or removing exactly 3 just adds or removes one amino acid.

This lab is a simplified model. Real mutations also happen in the stretches of DNA that do not code for a protein, real cells have a few more start codons. The labs say where the model stops.

The methods


Write all fourteen methods in Bases.java. Every one is safe to call with anything: bad input gives the "nothing" answer described in the comments ("", "?", false, -1.0 or "invalid") and never throws an exception. There are no loops in this lab and you do not need any. Several methods use the ones before them, so work from the top.
MethodWhat it does
isValidBase(b)Is b exactly "A", "C", "G" or "T"?
complement(b)The pairing base. Write it with an if / else if chain, or with indexOf and substring: your choice
isPurine(b), isGC(b)Boolean expressions with ||
transcribe(b)T becomes U, nothing else changes
codonAt(seq, i)The three bases starting at i, with a bounds check
isStopCodon(codon)One of the three stops
isInFrame(start, pos)Is pos a whole number of codons after start?
baseIndex(b)Where a base sits in the order T, C, A, G: 0, 1, 2 or 3, and -1 if b is not a base
translateCodon(codon)The genetic code as a 64-letter table, using baseIndex
reverseComplementCodon(codon)Pair each base and reverse the order
mutate(seq, pos, base)Replace one base
classifyMutation(codon, pos, newBase)Silent, missense, nonsense or stop-lost
isFrameshift(lengthChange)% with negative numbers

Traps to watch for

  • "ACGT".indexOf("") is 0 and "ACGT".indexOf("AC") is 0: so indexOf alone does not tell you a String is a base. Check with isValidBase first.
  • substring throws an exception when its indexes are off the end of the String. Test the length before you call it.
  • To compare Strings, use equals. == compares whether two names refer to the same object, which is not the question.
  • A negative number can have a negative remainder: -3 % 3 is 0, and -1 % 3 is -1. Think about what each of these means for a frame and a frameshift.
  • In Java, 1 / 2 is 0, because both numbers are ints. 1 / 2.0 is 0.5.

Reverse complement


The two strands of DNA run in opposite directions, so the partner of ATG is not TAC read left to right: it is CAT. Pair each base and reverse the order. This is why reverseComplementCodon takes the last base first. The next lab does it for a sequence of any length.

Testing your work


Run code runs the main method in Bases.java, which is yours to experiment with (try System.out.println(Bases.complement("G"));). Run tests runs every graded test and shows which pass, without saving a score. Submit tests does the same and saves your score for your teacher. A method that goes into an endless loop fails only its own tests, not the whole lab.