-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodonFinder.java
executable file
·182 lines (160 loc) · 5.89 KB
/
CodonFinder.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* This program finds the codons, number of DNA strands and CG Ratio above 0.35
*
* @ShreyamDuttaGupta
*
*/
import edu.duke.*;
public class CodonFinder {
private int countCodons(String dnaIn, String codonIn){
String dna = dnaIn.toLowerCase();
int count = 0;
int lastIndex = 0;
String findStr = codonIn.toLowerCase();
while ((lastIndex = dna.indexOf(findStr, lastIndex)) != -1) {
count++;
lastIndex += findStr.length();
}
return count;
}
private float cgRatio(String dnaIn){
String dna = dnaIn.toLowerCase();
int count = 0;
int lastIndex = 0;
String findStr = "c";
while ((lastIndex = dna.indexOf(findStr, lastIndex)) != -1) {
count++;
lastIndex += findStr.length();
}
// save c count
float cCount = count;
//reset count and index
lastIndex = 0;
count = 0;
findStr = "g";
while ((lastIndex = dna.indexOf(findStr, lastIndex)) != -1) {
count++;
lastIndex += findStr.length();
}
float gCount = count;
return (cCount/gCount);
}
private int findStartIndex(String dna,int index){
//standardize dna to lower case
//dna.toLowerCase();
int start = dna.toLowerCase().indexOf("atg", index);
return start;
}
private int findStopIndex(String dnaIn, int index){
//first standardize DNA to lowercase
String dna = dnaIn.toLowerCase();
int stopTga = dna.indexOf("tga", index);
if(stopTga == -1 || (stopTga-index)%3 != 0){
stopTga = dna.length();
}
int stopTag = dna.indexOf("tag", index);
if(stopTag == -1 || (stopTag-index)%3 != 0){
stopTag = dna.length();
}
int stopTaa = dna.indexOf("taa", index);
if(stopTaa == -1 || (stopTaa-index)%3 != 0){
stopTaa = dna.length();
}
return Math.min(Math.min(stopTag, stopTga), stopTaa);
}
private void printGenes(StorageResource store){
StorageResource longDna = new StorageResource();
StorageResource cgDna = new StorageResource();
int maxLengthOfGene = 0;
for (String dna: store.data()){
if (dna.length()>60){
longDna.add(dna);
}
if(cgRatio(dna)>0.35){
cgDna.add(dna);
}
if(dna.length() > maxLengthOfGene){
maxLengthOfGene = dna.length();
}
}
System.out.println("The following " +longDna.size()+ "genes have more than 60 nucliotides:");
//for (String dna: longDna.data()){
// System.out.println(dna);
//}
System.out.println("The following " +cgDna.size()+ "genes have a C/G Ratio of higher than 0,35:");
//for (String dna: cgDna.data()){
// System.out.println(dna);
//}
System.out.println("The longest gene has the length: " +maxLengthOfGene);
}
private StorageResource storeAll(String dna){
int index = 0;
StorageResource store = new StorageResource();
while (true){
int startCodonIndex = findStartIndex(dna,index);
if (startCodonIndex == -1){
break;
}
int endCodonIndex = findStopIndex(dna, startCodonIndex+3);
if ((((endCodonIndex+3)-startCodonIndex) % 3 == 0 )&&endCodonIndex != dna.length()){
if(endCodonIndex == dna.length()){
store.add(dna.substring(startCodonIndex, endCodonIndex));
index = endCodonIndex;
}else{
store.add(dna.substring(startCodonIndex, endCodonIndex+3));
index = endCodonIndex+3;
}
} else{
index = startCodonIndex+3;
}
}
return store;
}
private void printAll(String dna){
int index = 0;
while (true){
int startCodonIndex = findStartIndex(dna,index);
if (startCodonIndex == -1){
break;
}
int endCodonIndex = findStopIndex(dna, startCodonIndex+3);
if (((endCodonIndex+3)-startCodonIndex) % 3 == 0){
System.out.println(dna.substring(startCodonIndex, endCodonIndex+3));
index = endCodonIndex+3;
}else{
index = startCodonIndex+3;
}
}
}
public void testFinder(){
//String dna = "ATGTGCAACCCGGGTTTAAAATAAGTTCCCAAATTTTTAA";
//String dna = "ATGAAATGAAAA";
//String dna = "ccatgccctaataaatgtctgtaatgtaga";
String dna = "CATGTAATAGATGAATGACTGATAGATATGCTTGTATGCTATGAAAATGTGAAATGACCCA";
System.out.println("DNA string is:");
System.out.println(dna);
System.out.println("Gene found is: ");
//printAll(dna);
StorageResource store = storeAll(dna);
for (String gene: store.data()){
System.out.println(gene);
}
}
public void testStorageFinder(){
FileResource dnaFile = new FileResource("brca1line.fa");
//FileResource dnaFile = new FileResource("GRch38dnapart.fa");
String dna = dnaFile.asString();
//String dna = "CATGTAATAGATGAATGACTGATAGATATGCTTGTATGCTATGAAAATGTGAAATGACCCA";
StorageResource store = storeAll(dna);
System.out.println("Ther are " +store.size()+ " potential genes in the data.");
printGenes(store);
System.out.println("The codon CTG appear in a strand of DNA the following times: " + countCodons(dna, "CTG"));
}
public void testCGRatio(){
String dna = "ATGAACACCTGA";
System.out.println("DNA string is:");
System.out.println(dna);
float result = cgRatio(dna);
System.out.println("The C to G Nocliotide Ratio is: " +result);
}
}