-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindingAllGenes.java
More file actions
211 lines (154 loc) · 4.62 KB
/
FindingAllGenes.java
File metadata and controls
211 lines (154 loc) · 4.62 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package week2;
import edu.duke.FileResource;
import edu.duke.StorageResource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Week 2.
*/
public class FindingAllGenes {
public static void main(String[] args) {
FileResource fr = new FileResource("../data/GRch38dnapart.fa");
StorageResource sore = FindingAllGenes.findProtein(fr.asString());
FindingAllGenes.printGensInformation(sore);
FindingAllGenes.countCTGCodon(fr.asString());
}
/**
* prints all the Strings that are longer than 60 characters
* prints the number of Strings that are longer than 60 characters
* prints the Strings whose C-G-ratio is higher than 0.35
* prints the number of strings whose C-G-ratio is higher than 0.35
*
* @param sr
*/
public static void printGensInformation(StorageResource sr) {
int genCount = 0;
for (String word : sr.data()) {
if (word.length() > 60) {
// System.out.println("Gene: " + word);
genCount++;
}
}
System.out.println("Gene 60 count: " + genCount);
genCount = 0;
for (String word : sr.data()) {
if (cgRatio(word) > 0.35) {
// System.out.println("C-G Gen: " + word);
genCount++;
}
}
System.out.println("C-G Count: " + genCount);
System.out.println("Longest gene is: " + getLongestGene(sr).length());
}
/**
* Find all proteins in a gene
*
* @param dna
*/
public static StorageResource findProtein(String dna) {
dna = dna.toLowerCase();
//find start position of the codon
int endPos = 0;
StorageResource store = new StorageResource();
while (true) {
// get start position of a codom
int start = dna.indexOf("atg", endPos);
if (start == -1) {
break;
}
//find end position of a codon
int end = findEndPosition(dna, start + 3);
if (end == -1) {
endPos = start + 3;
continue;
}
//
endPos = end + 3;
store.add(dna.substring(start, endPos));
}
System.out.println("Total proteins: " + store.size());
return store;
}
/**
* Find end position of the certain protein
*
* @param dna
* @param startPos
* @return
*/
public static int findEndPosition(String dna, int startPos) {
String[] endTags = {"tag", "tga", "taa"};
List<Integer> endsPoints = new ArrayList<>();
for (String s : endTags) {
int index = dna.indexOf(s, startPos);
int diff = (index - startPos) % 3;
if (index != -1 && diff == 0) {
endsPoints.add(index);
}
}
if (endsPoints.isEmpty()) {
return -1;
}
return Collections.min(endsPoints);
}
/**
* Compute C-G ratio for the certain protein
*
* @param dna
* @return
*/
public static float cgRatio(String dna) {
dna = dna.toLowerCase();
int dnaLen = dna.length();
int gCount = countLetterInWord('g', dna);
int cCount = countLetterInWord('c', dna);
return (float) (gCount + cCount) / dnaLen;
}
/**
* Count number of a letter in a word
*
* @param letter
* @param word
* @return
*/
public static int countLetterInWord(char letter, String word) {
int counter = 0;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == letter) {
counter++;
}
}
return counter;
}
/**
* Find the longest gene in a collection of genes
*
* @param sr is a StorageResource of genes
* @return
*/
public static String getLongestGene(StorageResource sr) {
int maxLen = 0;
String longestGene = "";
for (String gene : sr.data()) {
int len = gene.length();
if (len > maxLen) {
maxLen = len;
longestGene = gene;
}
}
return longestGene;
}
/**
* Count the codon CTG in a strand of DNA
*
* @param dna is a strand of DNA
* @return count of CTG
*/
public static int countCTGCodon(String dna) {
dna = dna.toLowerCase();
int count = dna.length() - dna.replace("ctg", "").length();
System.out.println("CTG Count: " + count / 3);
return count;
}
}