-
Notifications
You must be signed in to change notification settings - Fork 16
/
StoringAllGenes.java
148 lines (129 loc) · 4.7 KB
/
StoringAllGenes.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
/**
* Assignemnt 6: Storing All Genes / Processing DNA Strings
* The progeam finds all the genes in a DNA string and then stores them using the
* StorageResource class
*
* @version January 28, 2016
*/
package assignment6;
import edu.duke.*;
import java.io.File;
import java.util.ArrayList;
public class StoringAllGenes
{
/** The method finds the first occurrence of each stop codon to the right of index */
public static int findStopIndex(String dna, int index){
int stop1 = dna.indexOf("tga", index);
if (stop1 == -1 || (stop1 - index) % 3 != 0) {
stop1 = dna.length();
}
int stop2 = dna.indexOf("taa", index);
if (stop2 == -1 || (stop2 - index) % 3 != 0) {
stop2 = dna.length();
}
int stop3 = dna.indexOf("tag", index);
if (stop3 == -1 || (stop3 - index) % 3 != 0) {
stop3 = dna.length();
}
return Math.min(stop1, Math.min(stop2,stop3));
}
/** The method creates and returns a StorageResource containing the genes found */
public static StorageResource storeAll(String dna){
String dnaLow = dna.toLowerCase();
StorageResource store = new StorageResource();
int start = 0;
while (true) {
int loc = dnaLow.indexOf( "atg", start );
if ( loc == -1 ) {
break;
}
int stop = findStopIndex( dnaLow, loc+3 );
if ( stop != dna.length() ) {
//System.out.println( dna.substring(loc, stop+3) );
store.add(dna.substring(loc, stop+3));
start = stop + 3;
} else {
start = start + 3;
}
}
return store;
}
/** The method returns the ratio of C’s and G’s in dna as a fraction of the entire strand of DNA */
public static float cgRatio(String dna){
dna = dna.toLowerCase();
int len = dna.length();
int CGCount = 0;
for(int i=0; i<len; i++){
if(dna.charAt(i) == 'c' || dna.charAt(i) == 'g')
CGCount++;
}//end for loop
//System.out.println("CGCount " + CGCount + " Length: " + len + " Ratio: " + (float)CGCount/len);
return ((float)CGCount)/len;
}
/** Find how many times the codon CTG appears in a strand of DNA */
public static void FindCTG(StorageResource s){
int countctg = 0;
for (String gene : s.data()) {
gene = gene.toLowerCase();
int start = 0;
while (true) {
int pos = gene.indexOf("ctg", start);
if (pos == -1) {
break;
}
countctg += 1;
start = pos + 3;
}
}
System.out.println("CTG: " + countctg);
}
/** The method processes all the strings in sr */
public static void printGenes(StorageResource sr){
int longerthan60 = 0;
int CGGreaterthan35 = 0;
int longestgene = 0;
int tempgene = 0;
ArrayList<String> longerthansixty = new ArrayList<String>();
ArrayList<String> CGGreaterthanthirtyfive = new ArrayList<String>();
for (String gene : sr.data()) {
tempgene = gene.length();
if (longestgene<tempgene){
longestgene = tempgene;
}
if (gene.length()>60){
longerthan60++;
longerthansixty.add(gene);
}
if (cgRatio(gene)>0.35){
CGGreaterthan35++;
CGGreaterthanthirtyfive.add(gene);
}
}
System.out.println("dnaStore.size: " + sr.size());
System.out.println("There are " + sr.size() + " genes. ");
System.out.println("\nThere are " + longerthan60 + " genes longer than 60.");
for (String gene: longerthansixty){
System.out.println(gene);
}
System.out.println("\nThere are " + CGGreaterthan35 + " genes with CG ratio greater than 0.35.");
for (String gene: CGGreaterthanthirtyfive){
System.out.println(gene);
}
System.out.println("\nlongestgene.size: " + longestgene);
}
public static void testStorageFinder() {
DirectoryResource dr = new DirectoryResource();
StorageResource dnaStore = new StorageResource();
for (File f : dr.selectedFiles()) {
FileResource fr = new FileResource(f);
String s = fr.asString();
dnaStore = storeAll(s);
printGenes(dnaStore);
FindCTG(dnaStore);
}
}
// Demo
public static void main(String[] args) {
testStorageFinder();
}
}