Intro to Computer science DNA coding problem. I just need help with the \"best a
ID: 3787461 • Letter: I
Question
Intro to Computer science DNA coding problem. I just need help with the "best alignment score" section of this problem. Below, I provided the description of the program as well as my current coding process. Like I said previously, I only need help on how to code the alignment score section.
Description:
"For example, let’s say that the two input sequences are “AATCTATA” and “AAGATA”. There are three possible alignments:
A A T C T A T A
(no space) --> A A G A T A
A A T C T A T A
(1 space) --> A A G A T A
A A T C T A T A
(2 spaces) --> A A G A T A
In general, mutations can be a substitution of one nucleotide for another (for example, a “G” being replaced by a “T”), an insertion that adds one or more nucleotides, or a deletion that deletes one or more nucleotides. To keep things simple, we will concern ourselves only with the first of these three: point mutations. For simple, gap-free alignments, we compute a score using a simple rule: if the two corresponding characters match, we add a match score of one (1); if they don’t match, the match score is zero (0). The total score for an alignment is the sum of the character scores, and the alignment with the highest score is the best match. So, for example, the scores for the three alignments above are 4, 1, and 3, and the best alignment is the first one. You will use this simple alignment method in your program.
Your program will prompt, via the console, for the first sequence and compute its basic statistics, then prompt, and validate, user input of a second sequence. It will compute that second sequence’s basic statistics, too. Then, your program will compute the scores for all possible alignments for those two strings (you will want to have a method that takes two strings, plus an offset for shifting the shorter string relative to the longer one, and returns an int score).
Thus, for the two input sequences “AATCTATA” and “AAGATA”, your program will output a report similar to the following:
Sequence 1: AATCTATA
C-count: 1
CG-ratio: 0.125
Complement: TTAGATAT
Sequence 2: AAGATA
C-count: 0
CG-ratio: 0.167
Complement: TTCTAT
Best alignment score: 4
AATCTATA
AAGATA
My code:
import java.util.Scanner;
public class dna
{
public static boolean isvalid(String s)
{
for (int i = 0; i< s.length() ; i++)
{
if(s.charAt(i) != 'C' && s.charAt(i) != 'G' && s.charAt(i) != 'A' && s.charAt(i) != 'T' )
{
return false;
}
}
return true;
}
public static int countc(String s)
{
int c = 0;
for (int i = 0; i< s.length() ; i++)
{
if(s.charAt(i) == 'C' )
{
c = c+1;
}
}
return c;
}
public static double countcg(String s)
{
int c = 0;
for (int i = 0; i< s.length() ; i++)
{
if(s.charAt(i) == 'C' || s.charAt(i) == 'G' )
{
c = c+1;
}
}
return c;
}
public static void Complement(String s)
{
String comp = s;
for (int i = 0; i< s.length() ; i++)
{
if(s.charAt(i) == 'C')
{
System.out.print('G');
}
if(s.charAt(i) == 'G')
{
System.out.print('C');
}
if(s.charAt(i) == 'A')
{
System.out.print('T');
}
if(s.charAt(i) == 'T')
{
System.out.print('A');
}
}
System.out.println("");
}
public static void main(String[] args)
{
String DNA;
while(true)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter DNA Sequence: ");
DNA = scanner.nextLine();
if(isvalid(DNA))
{
System.out.println("Sequence 1: " + DNA);
System.out.println("C-count: " + countc(DNA));
System.out.println("CG-ratio: " + countcg(DNA)/DNA.length());
System.out.print("Complement: ");
Complement(DNA);
break;
}
else
{
System.out.println("Please enter Valid DNA Sequence!");
}
}
String DNA2;
while(true)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter DNA Sequence: ");
DNA2 = scanner.nextLine();
if(isvalid(DNA2))
{
System.out.println("Sequence 2: " + DNA2);
System.out.println("C-count: " + countc(DNA2));
System.out.println("CG-ratio: " + countcg(DNA2)/DNA2.length());
System.out.print("Complement: ");
Complement(DNA2);
System.out.println("Best alignment score:");
System.out.println(DNA);
System.out.println(DNA2);
break;
}
else
{
System.out.println("Please enter Valid DNA Sequence!");
}
}
}
}
Explanation / Answer
import java.util.*;
public class DNAsequence {
static Scanner sc=new Scanner(System.in);
static int countc=0;
static int countg=0;
static float fraction=0.0f;
public static void main(String[] args) {
boolean valid;
String DNA;
String DNA2;
while(true)
{
System.out.println("Enter DNA Sequence: ");
DNA = sc.nextLine();
if(chkvalid(DNA))
{
System.out.println("Sequence 1: " + DNA);
countss(DNA);
countgg(DNA);
detfraction(DNA);
System.out.print("Complement: ");
complement(DNA);
break;
}
else
{
System.out.println("Please enter Valid DNA Sequence!");
}
}
while(true)
{
System.out.println("Enter DNA2 Sequence: ");
DNA2 = sc.nextLine();
if(chkvalid(DNA2))
{
System.out.println("Sequence 1: " + DNA2);
countss(DNA2);
countgg(DNA2);
detfraction(DNA2);
System.out.print("Complement: ");
complement(DNA2);
break;
}
else
{
System.out.println("Please enter Valid DNA Sequence!");
}
}
bestalignment(DNA,DNA2);
}
private static boolean chkvalid(String DNA) {
if (DNA.matches("[ATGC]+")) {
return true;
}else
return false;
}
public static void countss(String DNA){
for(int i=0;i<DNA.length();i++){
if(DNA.charAt(i)=='C')
countc++;
}
System.out.println("count of c:"+countc);
}
public static void countgg(String DNA){
for(int i=0;i<DNA.length();i++){
if(DNA.charAt(i)=='G')
countg++;
}
System.out.println("count of g:"+countg);
}
public static void detfraction(String DNA){
if(countc >=DNA.length()/2||countg>=DNA.length()/2)
fraction=0.5f;
System.out.println("cg ratio:"+fraction);
}
public static void complement(String DNA){
String cp="";
for(int i=0;i<DNA.length();i++){
if(DNA.charAt(i)=='A')
cp+='T';
else if(DNA.charAt(i)=='T')
cp+='A';
if(DNA.charAt(i)=='C')
cp+='G';
else if(DNA.charAt(i)=='G')
cp+='C';
}
System.out.println(cp);
}
public static void bestalignment(String DNA,String DNA2){
int add=0;
for(int j=0;j<DNA2.length();j++){
char a=DNA.charAt(j);
char b=DNA2.charAt(j);
if(a==b)
add++;
}
System.out.println(add);
}
}
OUTPUT:
Enter DNA Sequence:
CTGAC
Sequence 1: CTGAC
count of c:2
count of g:1
cg ratio:0.5
Complement: GACTG
Enter DNA2 Sequence:
CTGGT
Sequence 1: CTGGT
count of c:3
count of g:3
cg ratio:0.5
Complement: GACCA
3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.