Write the void method processGenes that has one parameter sr, which is a StorageResource of strings. This
Question:
Write the void method processGenes that has one parameter sr, which is a StorageResource of strings. This method processes all the strings in sr to find out information about them. Specifically, it should:
print all the Strings in sr that are longer than 9 characters
print the number of Strings in sr that are longer than 9 characters
print the Strings in sr whose C-G-ratio is higher than 0.35
print the number of strings in sr whose C-G-ratio is higher than 0.35
print the length of the longest gene in sr
Write a method testProcessGenes. This method will call your processGenes method on different test cases. Think of five DNA strings to use as test cases. These should include: one DNA string that has some genes longer than 9 characters, one DNA string that has no genes longer than 9 characters, one DNA string that has some genes whose C-G-ratio is higher than 0.35, and one DNA string that has some genes whose C-G-ratio is lower than 0.35. Write code in testProcessGenes to call processGenes five times with StorageResources made from each of your five DNA string test cases.
This is my code up to this exercise. I've modified my test strings to follow their guidelines above.
public class Part1 { public int findStopCodon (String dna, int startIndex, String stopCodon) { int currIndex=dna.indexOf(stopCodon,startIndex+3); while (currIndex != -1) { int difference = currIndex - startIndex; if (difference % 3 == 0) {return currIndex;} else {currIndex=dna.indexOf(stopCodon,currIndex+1);} } return -1;} 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"); int minIndex = 0; if (taaIndex==-1 || (tagIndex !=-1 && tagIndex < taaIndex)) {minIndex = tagIndex;} else {minIndex = taaIndex;} if (minIndex==-1 || (tgaIndex != -1 && tgaIndex < minIndex)) {minIndex=tgaIndex;} if(minIndex ==-1) {return "";} return dna.substring(startIndex,minIndex+3); } public void printAllGenes(String dna) { int startIndex = 0; while (true) {String currentGene = findGene(dna, startIndex); if (currentGene.isEmpty()) {break;} System.out.println(currentGene); startIndex=dna.indexOf(currentGene, startIndex)+currentGene.length(); } } 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 float cgRatio (String dna){ int indexC = dna.indexOf("C"); int countC = 0; while(indexC != -1) { countC +=1; indexC=dna.indexOf("C", indexC + 1); } int indexG = dna.indexOf("G"); int countG = 0; while(indexG != -1) { countG +=1; indexG=dna.indexOf("G", indexG + 1); } return (float) (countC+ countG)/ dna.length(); } public int countCTG(String dna) { int CTGindex=dna.toUpperCase().indexOf("CTG"); int CTGcount=0; while (CTGindex != -1) {CTGcount = CTGcount +1; CTGindex = dna.toUpperCase().indexOf("CTG",CTGindex+3); } return CTGcount;} public void countAllGenes(String dna) { int startIndex = 0; int count = 0; while (true) {String currentGene = findGene(dna, startIndex); if (currentGene.isEmpty()) {System.out.println(count); break;} count=count+1; startIndex=dna.indexOf(currentGene, startIndex)+currentGene.length(); } } public void testOn(String dna) {System.out.println("Testing countAllGenes on "+dna); printAllGenes (dna); countAllGenes(dna); System.out.println("now for StorageResource:"); StorageResource genes = getAllGenes(dna); for (String g:genes.data()) {System.out.println(g); } } public void test() { testOn("ATGCGCTAAATGCGCCGCTAGATGTTTTTTTTTTAA123"); testOn("ATGCCCTAA12ATGTTTTAA"); testOn("ATGCCCCCCCCCCCCCCCTAAATGTTTTAA"); testOn(""); testOn("ATG123TAA"); } }
Java An Introduction To Problem Solving And Programming
ISBN: 9780134462035
8th Edition
Authors: Walter Savitch