2015-12-28 19 views
1

我正在嘗試調試我的程序是否有錯誤;例如行String geneList = FMG.storeAll(dna);上的錯誤incompatible types: edu.duke.StorageResource cannot be converted to java.lang.String是什麼意思?調試/解碼DNA Java

storeAll和printGenes方法是我遇到的問題。我似乎無法獲得存儲基因的正確語法。我有一個應該看起來像什麼的基本輪廓,但我不知道如何正確使用StorageResource。同樣在printGenes方法中,我不知道需要什麼,以便執行所有我想要的打印語句。具體而言,我想知道(語法是什麼)如何計算基因數,計算DNA鏈中的字符數等。另外,我還有一個問題,我應該在哪裏調用方法的位置我已經創建了;我想知道代碼中的確切位置。任何建議將不勝感激。

(PS:主要方法已更改爲printGenes方法,因爲我的類使用的BlueJ中,你不能把主要方法

在這裏你可以下載必要做這個作業文件, 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(); 
} 
+1

你想幫助* *在你堅持的東西,或者你要我們實際上做* *這項工作?只有當你告訴我們什麼是特別壞的以及你卡在哪裏之後,你纔會得到前者。 – Makoto

+0

我編輯了這個問題,以反映我遇到的實際問題。 –

回答

0

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