In: Computer Science
Java: create a program that reads in a piece of DNA sequence from a sequence file (dna.seq) (alternatively you can use the getRandomSeq(long) method of the RandomSeq class to generate a piece of DNA sequence), and then print out all the codons in three forward reading frames. Design a method called codon() that can be used to find all the codons from three reading frames. The method will take in an argument, the reading frame (1, 2, or 3), and return an array or ArrayList with all the codons.
The DNA sequence is:
TCAGCGAGATGGGAAAGATCACCTTCTTCGAGGACCGAGGCTTCCAGGGC
Reading frame #1 codons are:
TCA GCG AGA TGG GAA AGA TCA CCT TCT TCG AGG ACC GAG GCT TCC AGG
Reading frame #2 codons are:
CAG CGA GAT GGG AAA GAT CAC CTT CTT CGA GGA CCG AGG CTT CCA GGG
Reading frame #3 codons are:
AGC GAG ATG GGA AAG ATC ACC TTC TTC GAG GAC CGA GGC TTC CAG GGC
CODE:
* Please provide the path to file. Copy file's location and
paste.
* Filename is dna.seq
* Contents are as given by you
TCAGCGAGATGGGAAAGATCACCTTCTTCGAGGACCGAGGCTTCCAGGGC
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class DNA {
public static void addToList(ArrayList<String> arr , int start , String sequence) {
String temp="";
for(int i=start;i<sequence.length();i++) {
temp +=Character.toString(sequence.charAt(i));
if(temp.length() == 3) {
arr.add(temp);
temp = "";
}
}
}
public static ArrayList < String > getAllCodons(String sequence , int frame) {
ArrayList<String> arr = new ArrayList<String>();
switch(frame) {
case 1:
addToList(arr , 0 , sequence);
break;
case 2:
addToList(arr, 1, sequence);
break;
case 3:
addToList(arr, 2, sequence);
break;
default:
System.out.println("Invalid reading frame");
}
return arr;
}
public static void main(String[] args) {
File myObj = new File("D:\\BackendWorkspace\\MyWork\\src\\search\\" + "dna" + ".seq");
try {
String sequence = "";
Scanner myReader = new Scanner(myObj);
Scanner sc = new Scanner(System.in);
System.out.println("Enter the reading frame 1,2 or 3");
int frame = sc.nextInt();
sc.close();
while (myReader.hasNextLine()) {
sequence = myReader.nextLine();
}
myReader.close();
ArrayList<String> codons = getAllCodons(sequence, frame);
System.out.println(codons);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
OUTPUT
Kindly rate the
answer.