-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTransitionsAndTransversions.java
More file actions
49 lines (38 loc) · 1.31 KB
/
Copy pathTransitionsAndTransversions.java
File metadata and controls
49 lines (38 loc) · 1.31 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
//http://rosalind.info/problems/tran/
import java.util.Scanner;
public class TransitionsAndTransversions {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[] purines = new char[] {'A','G'};
char[] pyrimidines = new char[] {'C','T'};
double transitions = 0;
double transversions = 0;
String label1 = sc.nextLine(); //ignore dna label
String s1 = sc.nextLine(); //first dna string
String label2 = sc.nextLine(); //ignore dna label
String s2 = sc.nextLine(); //second dna string
//both strings are of equal length
for (int i = 0; i < s1.length(); i++) {
if( s1.charAt(i) != s2.charAt(i) ) {
//check if it is a transversion
if( (member(purines,s1.charAt(i)) && member(pyrimidines,s2.charAt(i))) || (member(pyrimidines,s1.charAt(i)) && member(purines,s2.charAt(i))))
transversions++;
else
transitions++;
}
}
System.out.println(transitions/transversions);
//Rosalind may give 'wrong answer' if output is not rounded to 11 decimal points, if so, use Decimal Format class
}
//determines if given element is in given array
public static boolean member(char[] arr, char c) {
boolean result = false;
for (int i = 0; i < arr.length; i++) {
if(arr[i] == c) {
result = true;
break;
}
}
return result;
}
}