-
Notifications
You must be signed in to change notification settings - Fork 0
/
AllCodonStored.java
177 lines (161 loc) · 5.3 KB
/
AllCodonStored.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
/**
* Write a description of AllCodonStored here.
*
* @author (your name)
* @version (a version number or a date)
*/
import edu.duke.*;
import java.io.*;
public class AllCodonStored {
public int findStopCodon(String dnaStr, int startIndex, String stopCodon){
int currIndex = dnaStr.indexOf(stopCodon,startIndex+3);
while(currIndex !=-1){
int diff = currIndex-startIndex;
if(diff %3 == 0){
return currIndex;
}
else{
currIndex = dnaStr.indexOf(stopCodon, currIndex+1);
}
}
return dnaStr.length();
}
public String findGene(String dna, int where){
int startIndex = dna.indexOf("ATG", where);
if(startIndex == -1){
return "";
}
int taaIndex = findStopCodon(dna,startIndex,"TAA");
int tagIndex = findStopCodon(dna,startIndex,"TAG");
int tgaIndex = findStopCodon(dna,startIndex,"TGA");
//look for minimum index
//int temp = Math.min(taaIndex,tgaIndex);
//int minIndex = Math.min(temp,tagIndex);
int minIndex = 0;
if(taaIndex == -1 ||
(tgaIndex != -1 && tgaIndex < taaIndex)){
minIndex =tgaIndex;
}
else{
minIndex=taaIndex;
}
if(minIndex == -1 ||
(tagIndex != -1 && tagIndex < minIndex)){
minIndex =tagIndex;
}
if(minIndex == dna.length()){
return "";
}
if(dna.length() == minIndex){
return "";
}
return dna.substring(startIndex,minIndex+3);
}
public int countCTG(String dna){
int ctgIndex = dna.indexOf("CTG",0);
int count = 0;
while(ctgIndex != -1){
count++;
ctgIndex = dna.indexOf("CTG",ctgIndex+3);
}
return count;
}
public StorageResource getAllGenes(String dna){
StorageResource geneList = new StorageResource();
int startIndex = 0;
while(true){
String currentGene = findGene(dna,startIndex);
if(currentGene.isEmpty()){
break;
}
geneList.add(currentGene);
startIndex = dna.indexOf(currentGene, startIndex) +
currentGene.length();
}
return geneList;
}
public double countCg(String dna){
int cIndex = dna.indexOf("C",0);
int countC = 0;
while(cIndex != -1){
countC++;
cIndex = dna.indexOf("C",cIndex+1);
}
int gIndex = dna.indexOf("G",0);
int countG = 0;
while(gIndex != -1){
countG++;
gIndex = dna.indexOf("G",gIndex+1);
}
return (double)countC/countG;
}
//this takes a storageResource parameter
public void processGenes(StorageResource sr){
int countGene = 0;
int greaterThanSixty = 0;
int countCg =0;
int geneLongest =0;
//finding gene with lenght greater than 9
for(String s : sr.data()){
countGene++;
if(s.length()>60){
greaterThanSixty++;
}
//findding the gene with the cg ratio greater than 0.35
double cg = countCg(s);
if(cg>0.35){
countCg++;
}
//finding the longest gene
int currLength = s.length();
int prevLength =0;
if(geneLongest==0){
geneLongest = currLength;
}
else{
if(currLength>geneLongest){
geneLongest = currLength;
}
else{
prevLength = currLength;
}
}
}
System.out.println("The number of gene is "+countGene);
System.out.println("The number of gene greater 60 is "+ greaterThanSixty);
System.out.println("The number of gene with CG ratio greater 0.35 is "+countCg);
System.out.println("The longest gene has a length"+geneLongest);
}
public String mystery(String dna){
int pos = dna.indexOf("T");
int count = 0;
int startPos = 0;
String newDna = "";
if(pos==-1){
return dna;
}
while (count<3){
count += 1;
newDna = newDna + dna.substring(startPos,pos);
startPos = pos+1;
pos = dna.indexOf("T",startPos);
if(pos==-1){
break;
}
}
newDna = newDna + dna.substring(startPos);
return newDna;
}
public void test (){
URLResource page =
new URLResource("https://users.cs.duke.edu/~rodger/GRch38dnapart.fa");
String source = page.asString();
StorageResource gene = getAllGenes(source);
processGenes(gene);
int CTG = countCTG(source);
System.out.println("The number of CTG in the DNA is "+CTG);
String dna = "ATGCCCTAG";
String mystery = mystery(dna);
System.out.println("the mystery method"+mystery);
}
}