English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i need to write a java program that reads a DNA sequence form the keyboard, callls the countBase() method, and outputs the resulting base counts.

Can some one help me with this as for now i have only about 24 hours to do it. i have try my best for the last few days but without success.

2007-07-25 17:42:06 · 3 answers · asked by cherry 5 in Computers & Internet Programming & Design

3 answers

class Test {
int aCount = 0, gCount = 0, tCount = 0, cCount = 0;

void countBases(String in) throws IOException {
StringReader sr = new StringReader(in);
int c;
while ((c = sr.read()) != -1) {
switch (c) {
case 'a': aCount++; break;
case 'g': gCount++; break;
case 't': tCount++; break;
case 'c': cCount++; break;
} } }

public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader( new InputStreamReader( System.in));
String bases = br.readLine().toLowerCase();
Test t = new Test();
t.countBases(bases);
System.out.println("a = " + t.aCount);
System.out.println("t = " + t.tCount);
System.out.println("g = " + t.gCount);
System.out.println("c = " + t.cCount);
} catch (Throwable th) {
th.printStackTrace();
}
System.exit(0);
} }

===============
Sample input and output:

aaaaaaaaaaaaaaaaaatttgc

a = 18
t = 3
g = 1
c = 1

2007-07-25 18:07:06 · answer #1 · answered by McFate 7 · 0 0

public class LCS {
// These are "constants" which indicate a direction in the backtracking array.
private static final int NEITHER = 0;
private static final int UP = 1;
private static final int LEFT = 2;
private static final int UP_AND_LEFT = 3;

public static String LCSAlgorithm(String a, String b) {
int n = a.length();
int m = b.length();
int S[][] = new int[n+1][m+1];
int R[][] = new int[n+1][m+1];
int ii, jj;

// It is important to use <=, not <. The next two for-loops are initialization
for(ii = 0; ii <= n; ++ii) {
S[ii][0] = 0;
R[ii][0] = UP;
}
for(jj = 0; jj <= m; ++jj) {
S[0][jj] = 0;
R[0][jj] = LEFT;
}

// This is the main dynamic programming loop that computes the score and
// backtracking arrays.
for(ii = 1; ii <= n; ++ii) {
for(jj = 1; jj <= m; ++jj) {

if( a.charAt(ii-1) == b.charAt(jj-1) ) {
S[ii][jj] = S[ii-1][jj-1] + 1;
R[ii][jj] = UP_AND_LEFT;
}

else {
S[ii][jj] = S[ii-1][jj-1] + 0;
R[ii][jj] = NEITHER;
}

if( S[ii-1][jj] >= S[ii][jj] ) {
S[ii][jj] = S[ii-1][jj];
R[ii][jj] = UP;
}

if( S[ii][jj-1] >= S[ii][jj] ) {
S[ii][jj] = S[ii][jj-1];
R[ii][jj] = LEFT;
}
}
}

// The length of the longest substring is S[n][m]
ii = n;
jj = m;
int pos = S[ii][jj] - 1;
char lcs[] = new char[ pos+1 ];

// Trace the backtracking matrix.
while( ii > 0 || jj > 0 ) {
if( R[ii][jj] == UP_AND_LEFT ) {
ii--;
jj--;
lcs[pos--] = a.charAt(ii);
}

else if( R[ii][jj] == UP ) {
ii--;
}

else if( R[ii][jj] == LEFT ) {
jj--;
}
}

return new String(lcs);
}

public static void main(String args[]) {
try {
String s = LCSAlgorithm(args[0], args[1]);
System.out.println(s);
}
catch(Exception e) {
e.printStackTrace();
}
}
}

2007-07-26 07:12:06 · answer #2 · answered by angel04 3 · 0 0

May be you can contact a Java expert to help you out. Check websites like http://oktutorial.com/

2007-07-26 04:12:47 · answer #3 · answered by Anonymous · 0 1

fedest.com, questions and answers