使用StorageResource

时间:2015-12-27 07:13:28

标签: java

我正在使用存储资源存储我的程序找到的基因。这是我正在做的任务。我想确保得到这些结果:

  • 打印所有超过60个字符的字符串
  • 打印超过60个字符的字符串数
  • 打印C-G比率高于0.35
  • 的字符串
  • 打印C-G比率高于0.35
  • 的字符串数

如何使用StorageResource存储我从程序中获取的基因数据? 另外作为旁注,有一个错误,表示无法在for (File f: dr.selectedFiles()){找到符号。

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);
                //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();
       for (File f: dr.selectedFiles()){
       FileResource fr = new FileResource("/Desktop/DNA_Testing/tagfinder2/brca1line.fa");
       String dna = fr.asString();
       StorageResource s1 = storeAll(dna);


        System.out.println("size = " + s1.size());

    }  
  }
  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){

        //create a FindMultiGenesFile object FMG
        FindMultiGenes5 FMG = new FindMultiGenes5();

        //read a DNA sequence from file
        String dna = FMG.readStrFromFile();

        //call FMG.findGenes() method to get all genes, and store all genes in an 
         FMG.findGenes(dna);

        //store all genes into a document
        StorageResource dnaStore = new StorageResource();



        int longerthan60 = 0;
        int CGGreaterthan35 = 0;


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

2 个答案:

答案 0 :(得分:0)

错误 - 找不到符号(文件f:dr.selectedFiles()){。是因为你忘了导入java.io. *;

你可以存储这样的基因:

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

答案 1 :(得分:0)

尝试这样,更容易:

String file = "AAC...(put the dna here).....";
StorageResource genes = storeAll(file);
System.out.println("Number of genes found: " + genes.size());
printGenes(genes);

我认为整个代码不起作用。如果你能妥善解决,请告诉我。