【发布时间】:2015-12-28 04:33:20
【问题描述】:
我正在尝试调试我的程序是否有错误;例如String geneList = FMG.storeAll(dna);线上的错误incompatible types: edu.duke.StorageResource cannot be converted to java.lang.String是什么意思?
storeAll 和 printGenes 方法是我遇到的问题。我似乎无法正确使用存储基因的语法。我对它应该是什么样子有一个基本的轮廓,但我不知道如何正确使用 StorageResource。同样在 printGenes 方法中,我不知道需要什么,所以我想要的所有打印语句都会执行。具体来说,我想知道(语法是什么)如何计算基因数量,计算 DNA 链中的字符等。此外,我应该调用方法的位置有问题我已经创建了;我想知道代码中的确切位置。任何建议将不胜感激。
(PS:main方法已经改成printGenes方法了,因为我的班级用的是BlueJ,在里面不能调用Main方法。
您可以在此处下载完成此作业所需的文件,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) {
//CATGTAATAGATGAATGACTGATAGATATGCTTGTATGCTATGAAAATGTGAAATGACCCAdna = "CATGTAATAGATGAATGACTGATAGATATGCTTGTATGCTATGAAAATGTGAAATGACCCA";
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();
String geneList = FMG.storeAll(dna);
//store all genes into a document
StorageResource dnaStore = new StorageResource();
System.out.println("\n There are " + geneList.size() + " genes. ");
int longerthan60 = 0;
int CGGreaterthan35 = 0;
for(int i=0; i<geneList.size(); i++){
if(!dnaStore.contains(geneList.get(i)))
dnaStore.add(geneList.get(i));
if(geneList.get(i).length() > 60) longerthan60++;
if(FMG.calCGRatio(geneList.get(i)) > 0.35) CGGreaterthan35++;
}
System.out.println("dnaStore.size: " + dnaStore.size());
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();
}
【问题讨论】:
-
您是否需要帮助,因为您遇到了一些问题,或者您是否希望我们真正做这项工作?你只会得到前者,只有在你告诉我们具体有什么问题以及你卡在哪里之后。
-
我已编辑问题以反映我遇到的真正问题。
标签: java dna-sequence