I need to write a java program about translating the DNA seq
Solution
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class DNAToProtein extends Applet implements ActionListener {
JButton translate=new JButton(\"Convert\");
JButton done=new JButton(\"Done\");
JTextArea message=new JTextArea(2,20);
JTextField startPositionInput=new JTextField(\"Start position=1.\", 15);
JTextArea dnaSequenceInput=new JTextArea (\"Type a DNA sequence here\", 4, 30);
JTextArea codonOutput=new JTextArea(\"Incomplete codons will show an asterisk for each missing letter.\", 4, 30);
int maxCodons=100; // Maximum number of triplets we can handle in this program.
int maxAminoAcids=100; // Maximum number of amino acids in a protein we can handle.
int numCodons=0; // No triplets obtained so far.
int numAminoAcids=0; // Number of Amino Acids in the protein seqeunce so far.
JFrame myFrame=new JFrame(\"DNA Sequence Tripletizer\");
JPanel p=new JPanel(new GridLayout(2,1));
boolean incompleteSeq; // Indicates if a given DNA sequence is incomplete.
public void init(){
translate.addActionListener(this);
done.addActionListener(this);
codonOutput.setBackground(Color.orange);
p.setBackground(Color.blue);
startPositionInput.setBackground(Color.yellow);
dnaSequenceInput.setBackground(Color.blue);
dnaSequenceInput.setForeground(Color.yellow);
message.setFont(new Font(\"SansSerif\", Font.BOLD, 20));
message.setText(\"Welcome to CS 177 /178 Programming with Multimedia Objects\ \");
message.append(\"Fall 2004\ \");
message.append(\"DNA To Protein Converter\");
p.setBackground(Color.blue);
p.add(translate); p.add(done);
this.add(new JTextField(\"This area is useless.\"));
this.add(new JTextField(\"Close this applet when done\"));
this.setBackground(Color.orange);
// Add panel and widgets to the frame.
myFrame.getContentPane().add(BorderLayout.WEST, p);
myFrame.getContentPane().add(BorderLayout.NORTH, message);
myFrame.getContentPane().add(BorderLayout.SOUTH, dnaSequenceInput);
myFrame.getContentPane().add(BorderLayout.EAST, startPositionInput);
myFrame.getContentPane().add(BorderLayout.CENTER,codonOutput);
// Set some properties of the frame (resizability, color, and size).
myFrame.isResizable(); // Makes the frame resizable.
myFrame.setBounds(140, 140, 800, 300); // Sets the top left corner coordinbaties to 140, 140 and size to 800x300 pixels.
myFrame.getContentPane().setBackground(Color.orange);
myFrame.show();
}
// Process translate request.
public void actionPerformed (ActionEvent e){
Object source=e.getSource();
// Fetch DNA sequence and the start position from where translation is to begin.
// Check for validity and translate a valid DNA sequence to protein sequence, and display.
if(source==done){
myFrame.dispose();
}
else{ // Otherwise this must be the CONVERT button click. Process the request.
processConvert();
}
} // End of actionPerformed()
// Process convert request.
public void processConvert(){
String dnaInput;
String [] codonsFromDNA=null;
String [] proteinFromCodons=null;
int startPosition=1; // Default start position.
dnaInput=dnaSequenceInput.getText(); // Get DNA sequence typed by the user.
startPosition=getStartPosition();
dnaSequenceInput.setText(\"You typed:\ \"+dnaInput);
if(valid(dnaInput)){
codonsFromDNA=convertToCodons(startPosition,dnaInput); // This and the next statement can be combined into one statement. Can you do it?
displaycodons(codonsFromDNA);
proteinFromCodons=convertToProtein(codonsFromDNA);
displayProtein(proteinFromCodons);
dnaSequenceInput.setText(\"Next sequence please:\");
}
else{
codonOutput.setText(\"Invalid DNA sequence. Try again.\");
}
} // End of processConvert()
// Get start position that might have been typed by the user.
// If no start position ias typed then return the default position (=1).
public int getStartPosition(){
String s=startPositionInput.getText();
int position=1;
// Now convert the user types start position to an int. Catch any exception.
try{
position=Integer.parseInt(s);
if(position<=0)position=1; // Negative or 0 value is also invalid.
}
catch(Exception e){
position=1;
}
startPositionInput.setText(\"Start position: \"+position);
return(position);
}// End getStartPosition()
// Check if the DNA seqeunce given by the user contains correct neucleotide codes.
// Note that we consider only four valid 1-letter codes, namely T, C, A, and G.
public boolean valid(String dna){
char [] validCodes={\'T\', \'C\', \'A\', \'G\'};
boolean validOrNot=true; // Assume that dna is a valid string.
for (int position=0; position<dna.length(); position++){
char nextChar=dna.charAt(position);
if(!(nextChar==validCodes[0]|| nextChar==validCodes[1] || nextChar==validCodes[2] || nextChar==validCodes[3])){
validOrNot=false;
break;
}
}
return(validOrNot);
}// End of valid()
// Convert the dna neucleotide sequence to an array of codons. For example, ACTAGG will be converted to
// an aray of two codons: ACT, AGG. If the length of the dna string is not a multiple of 3 then the last codon returned
// will be of the form C?? or CC? where C is a neucleotide code.
public String [] convertToCodons(int start, String dna){
String [] codons=new String[maxCodons]; // This array holds the codons locally before returning it to the caller.
// Value of start must be >= 1. 1 corresponds to the FIRST position .
// For example to letter A in string \"ACGT\".
if(start<1)start=1;
int codonsInDNA=(dna.length()-(start-1))/3; // Note that start
String acodon=\"\"; // Initialize the next codon to empty string
int nextPosition=start-1; // This is the start position of the next codon.
incompleteSeq=false; // Suppose that the seqeunce IS complete.
// First get as many complete codons as possible.
for(int nextCodon=0; nextCodon<codonsInDNA; nextCodon++){
codons[nextCodon]=new String(\"\");
// codons[nextcodon]=String.valueOf(dna.charAt(nextPosition))+String.valueOf(dna.charAt(nextPosition+1))+ String.valueOf(dna.charAt(nextPosition+2));
codons[nextCodon]=dna.substring(nextPosition, nextPosition+3);
nextPosition=nextPosition+3;
}
// Check if the entire DNA string is exhausted. There might be 1 or 2 letters remaining
// if the length is not a multiple of 3. For example: dna=ACTG, then the codons returned will be
// ACT and G??.
int remaining=(dna.length()-(start-1))%3;
if(remaining==1){
codons[codonsInDNA]=String.valueOf(dna.charAt(nextPosition))+\"**\";
incompleteSeq=true;
}
else if(remaining==2){
codons[codonsInDNA]=String.valueOf(dna.charAt(nextPosition))+String.valueOf(dna.charAt(nextPosition+1))+\"*\";
incompleteSeq=true;
}
else if(remaining<0 && remaining>2)
message.setText(\"Internal program error in convertTocodon\");
// Update number of codons in case there is an extra incomplete codon.
if(remaining!=0)
numCodons=codonsInDNA+1;
else
numCodons=codonsInDNA;
// All codons constructed. Hey! Return to the caller.
return(codons);
} // End of convertTocodon()
// Convert a Codon seqeunce to Amino acid sequence.
public String [] convertToProtein(String [] codons){
String [] protein=new String[maxAminoAcids];
String nextAminoAcid=\"\"; // Initialize to empty string;
for (int i=0; i<numCodons; i++){
nextAminoAcid=String.valueOf(GeneticCode.codonToAmino(codons[i]));
// Check if this was a STOP codon and process accordingly.
if (nextAminoAcid.equals(\"?\")){
protein[i]=\"STOP\";
}
else{
protein[i]=nextAminoAcid;
}
}
numAminoAcids=numCodons;
return(protein);
}// End of convertToProtein()
//Display codons.
public void displaycodons(String[] codons){
codonOutput.setText(\"\");
codonOutput.setText(\"Total codons: \"+numCodons);
if(incompleteSeq)codonOutput.append(\" DNA sequence is incomplete.\");
codonOutput.append(\"\ \");
for (int nextCodon=0; nextCodon<numCodons; nextCodon++){
codonOutput.append(codons[nextCodon]+\"\ \");
}
}// End of displayCodons()
// Display amino acid sequence.
public void displayProtein(String [] proteinSeq){
codonOutput.append(\"Protein sequence:\ \");
for (int nextAminoAcid=0; nextAminoAcid<numAminoAcids; nextAminoAcid++){
codonOutput.append(proteinSeq[nextAminoAcid]+\" \");
}
}// End of displayProtein().
}// End of applet class.








