-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuntingtons.java
More file actions
50 lines (46 loc) · 1.82 KB
/
Copy pathHuntingtons.java
File metadata and controls
50 lines (46 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class Huntingtons {
// Returns the maximum number of consecutive repeats of CAG in the DNA string.
public static int maxRepeats(String dna) {
int count = 0, max = 0, x;
for (int i = 0; i < dna.length() - 2; i += x) {
String s = dna.substring(i, i + 3);
if (s.equals("CAG")) {
count++;
x = 3;
}
else {
count = 0;
x = 1;
}
if (count > max) max = count;
}
return max;
}
// Returns a copy of s, with all whitespace (spaces, tabs, and newlines) removed.
public static String removeWhitespace(String s) {
String s1 = s.replace(" ", "");
String s2 = s1.replace("\t", "");
String s3 = s2.replace("\n", "");
return s3;
}
// Returns one of these diagnoses corresponding to the maximum number of repeats:
// "not human", "normal", "high risk", or "Huntington's".
public static String diagnose(int maxRepeats) {
String result;
if (maxRepeats >= 0 && maxRepeats <= 9) result = "not human";
else if (maxRepeats >= 10 && maxRepeats <= 35) result = "normal";
else if (maxRepeats >= 36 && maxRepeats <= 39) result = "high risk";
else if (maxRepeats >= 40 && maxRepeats <= 180) result = "Huntington" + "'" + "s";
else result = "not human";
return result;
}
// Take the name of a file as a command-line argument.
// Read the genetic sequence from the file using the In class.
public static void main(String[] args) {
In in = new In(args[0]);
String s = in.readAll();
String dna = removeWhitespace(s);
int maxRepeats = maxRepeats(dna);
System.out.println("max repeats = " + maxRepeats + "\n" + diagnose(maxRepeats));
}
}