我正在尝试调试程序中的错误;例如,在incompatible types: edu.duke.StorageResource cannot be converted to java.lang.String行上的错误String geneList = FMG.storeAll(dna);是什么意思?

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();
}

最佳答案

您的storeAll方法定义为

public StorageResource storeAll(String dna)


这意味着storeAll方法返回类型为StorageResource的对象。但是你用过

String geneList = FMG.storeAll(dna);


左侧使用的变量类型为string,而右侧返回的变量类型为StorageResource。无法完成此操作,因为Java不知道如何将StorageResource转换为String。就您的问题而言,不必编写很多行。要打印存储在StorageResource中的基因,您可以编写:

public void printAll(StorageResource s){
    for(String gene : s.data())
        System.out.println(gene);
}


要计算长度大于60或cg比大于0.35的基因,或一次性查找最长的基因,可以修改上述程序并执行以下操作

public void printAll(StorageResource s){
    int count60 = 0;
    int cg35 = 0;
    int longest = null;

    for (String gene : s.data()){
        System.out.println(gene.length() + "\t" + gene);
        if (gene.length() > 60) count60++;
        if (cgRatio(gene) > 0.35) cg35++;
        if (gene.length() > longest) longest = gene.length();
    }

    System.out.println("Greater than 60 nucleotides is "+ count60);
    System.out.println("Longest gene length is "+ longest);
    System.out.println("CG ratio greater than 0.35 is "+ countCG);
}


要查找CG比,您可以执行以下操作

public float cgRatio(String gene){
    int c = gene.length() - gene.replace("c", "").length();
    int g = gene.length() - gene.replace("g", "").length();

    return ((float) c + g) / gene.length();
}

09-30 21:16