Snippets
Elvis Morales Find all the genes from a DNA string file and using StorageResource class. With others data finders added.
Created by
Elvis Morales
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 | /**
* Find all the genes from a DNA string file and using StorageResource class.
*
* @author (Elvis Morales)
* @version (1.0)
*/
import edu.duke.*;
import java.io.File;
public class FindMultipleGenesStorage {
public 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) );
}
public StorageResource storeAll(String dna) {
String dnaLow = dna.toLowerCase();
int start = 0;
StorageResource genes = new StorageResource();
while (true) {
int loc = dnaLow.indexOf( "atg", start );
if ( loc == -1 ) {
break;
}
int stop = findStopIndex( dnaLow, loc+3 );
if ( stop != dna.length() ) {
genes.add( dna.substring(loc, stop+3) );
start = stop + 3;
} else {
start = start + 3;
}
}
return genes;
}
public int codonQtyCalc(String dna) {
String dnaLow = dna.toLowerCase();
int start = 0;
int codonQty = 0;
while (true) {
int pos = dnaLow.indexOf("ctg", start);
if (pos == -1) {
break;
}
codonQty += 1;
start = pos + 3;
}
return codonQty;
}
public int findLongGene( StorageResource genes ) {
int lGeneL = 0;
for ( String gene : genes.data() ) {
if ( lGeneL < gene.length() ) {
lGeneL = gene.length();
}
}
return lGeneL;
}
public void testStorageFinder() {
FileResource dnaFile = new FileResource();
String dnaFileStr = dnaFile.asString();
StorageResource genesFound = storeAll( dnaFileStr );
System.out.println( "Number of genes found: "+genesFound.size() );
System.out.println( "CTG codon found: "+codonQtyCalc( dnaFileStr ) );
System.out.println( "Longest gene length: "+findLongGene( genesFound ) );
printGenes( genesFound );
}
public float cgRatio( String dna ) {
String dnaLow = dna.toLowerCase();
int cgCount = 0;
int start = 0;
while (true) {
int pos = dnaLow.indexOf("c", start);
if (pos == -1) {
start = 0;
break;
}
cgCount += 1;
start = pos + 1;
}
while (true) {
int pos = dnaLow.indexOf("g", start);
if (pos == -1) {
start = 0;
break;
}
cgCount += 1;
start = pos + 1;
}
return ( (float) cgCount ) / dna.length();
}
public void printGenes( StorageResource sr ) {
int sixtyCharQty = 0;
int highCgRatioQty = 0;
float cgRatioConst = (float) 0.35;
for ( String s : sr.data() ) {
if ( s.length() > 60 ) {
System.out.println( "String longer than 60 characters: "+s );
sixtyCharQty++;
}
if ( cgRatio(s) > cgRatioConst ) {
System.out.println( "String with C-G-ratio higher than 0.35: "+s );
highCgRatioQty++;
}
}
System.out.println( "60 characters qty: "+sixtyCharQty );
System.out.println( "Strings with C-G-ratio higher than 0.35: "+highCgRatioQty );
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.