我正在尝试运行我的程序,但是我收到了很多额外的,不必要的信息。如果在输出窗口中向下滚动,则第二组基因是正确的,不会发生重叠,而不是第一组。我也得到总共202个基因,当时我应该得到89个。
还有其他部分让我很困惑;在我的代码输出中有很多CGRatio和长度数据;我如何组织它,以便程序只给我多少CGRatio> 0.35,以及长度超过60个字符的字符串数量。注意:CGRatio是DNA串中c和g的数量除以字符串的长度。 我还想知道这个文件中有多少个字符。
注意:main方法已被改为printGenes,因为BlueJ环境无法调用main。 注意:我们还没有学会如何使用除基础之外的任何东西; StorageResource类,DirectoryResource类和FileResource类是本课程中教授的,目前为止来自Duke库(即
import java.io.*;
import edu.duke.FileResource;
import edu.duke.StorageResource;
import edu.duke.DirectoryResource;
) 只是基本的操作字符串和for和while循环。
以下是该计划中使用的文件:http://www.dukelearntoprogram.com/course2/data/dna.zip
这是我的代码:
import java.io.*;
import edu.duke.FileResource;
import edu.duke.StorageResource;
import edu.duke.DirectoryResource;
public class FindMultiGenes5 {
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 sequence = dna.toUpperCase();
StorageResource store = new StorageResource();
int index = 0;
while (true) {
index = sequence.indexOf("ATG", index);
if (index == -1)
break;
int stop = findStopIndex(sequence, index + 3);
if (stop != sequence.length()) {
String gene = dna.substring(index, stop + 3);
store.add(gene);
System.out.println("From: " + index + " to " + stop + " Gene: " + gene );//index = sequence.substring(index, stop + 3).length();
index = stop + 3; // start at the end of the stop codon
}else{ index = index + 3;
}
}
return store;//System.out.println(sequence);
}
public 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);
}
System.out.println("size = " + dnaStore.size());
}
public String readStrFromFile(){
FileResource readFile = new FileResource();
String DNA = readFile.asString();
//System.out.println("DNA: " + DNA);
return DNA;
}//end readStrFromFile() method;
public float calCGRatio(String gene){
gene = gene.toUpperCase();
int len = gene.length();
int CGCount = 0;
for(int i=0; i<len; i++){
if(gene.charAt(i) == 'C' || gene.charAt(i) == 'G')
CGCount++;
}//end for loop
System.out.println("CGCount " + CGCount + " Length: " + len + " Ratio: " + (float)CGCount/len);
return (float)CGCount/len;
}//end of calCGRatio() method;
public void printGenes(StorageResource sr){
for(String gene: sr.data()){
if (gene.length() > 60) {
System.out.println(gene.length()+"\t"+gene);
}
if(calCGRatio(gene)> 0.35) {
System.out.println(gene.length()+"\t"+gene);
}
}
//create a FindMultiGenesFile object FMG
FindMultiGenes5 FMG = new FindMultiGenes5();
//read a DNA sequence from file
String dna = FMG.readStrFromFile();
FMG.storeAll(dna);
//store all genes into a document
StorageResource dnaStore = new StorageResource();
int longerthan60 = 0;
int CGGreaterthan35 = 0;
System.out.println("\n There are " + dnaStore.size() + " genes. ");
System.out.println("There are " + longerthan60 + " genes longer than 60.");
System.out.println("There are " + CGGreaterthan35 + " genes with CG ratio greater than 0.35.");
}//end main();
}