Java代码无法在dna字符串中找到基因

时间:2017-10-11 08:57:54

标签: java

以下代码来自Java mooc,它应该可以找到给定文件中的所有基因。问题是我的getAllGenes方法没有为给定的dna字符串返回任何基因。我根本不知道代码有什么问题。 我正在测试的文件(brca1line.fa)位于https://github.com/polde-live/duke-java-1/tree/master/AllGenesFinder/dna

谢谢。

这是我的java代码。

    public class AllGenesStored {
        public int findStopCodon(String dnaStr,
                                 int startIndex, 
                                 String stopCodon){                                 
                int currIndex = dnaStr.indexOf(stopCodon,startIndex+3);
                while (currIndex != -1 ) {
                   int diff = currIndex - startIndex;
                   if (diff % 3  == 0) {
                       return currIndex;
                   }
                   else {
                       currIndex = dnaStr.indexOf(stopCodon, currIndex + 1);
                   }
                }
                return -1;

        }
        public String findGene(String dna, int where) {
            int startIndex = dna.indexOf("ATG", where);
            if (startIndex == -1) {
                return "";
            }
            int taaIndex = findStopCodon(dna,startIndex,"TAA");
            int tagIndex = findStopCodon(dna,startIndex,"TAG");
            int tgaIndex = findStopCodon(dna,startIndex,"TGA");
            int minIndex = 0;
            if (taaIndex == -1 ||
                (tgaIndex != -1 && tgaIndex < taaIndex)) {
                minIndex = tgaIndex;
            }
            else {
                minIndex = taaIndex;
            }
            if (minIndex == -1 ||
                (tagIndex != -1 && tagIndex < minIndex)) {
                minIndex = tagIndex;
            }
            if (minIndex == -1){
                return "";
            }
            return dna.substring(startIndex,minIndex + 3);
        }
        public StorageResource getAllGenes(String dna) {
          //create an empty StorageResource, call it geneList
          StorageResource geneList = new StorageResource();
          //Set startIndex to 0
          int startIndex = 0;
          //Repeat the following steps
          while ( true ) {
              //Find the next gene after startIndex
              String currentGene = findGene(dna, startIndex);
              //If no gene was found, leave this loop 
              if (currentGene.isEmpty()) {
                  break;
              }
              //Add that gene to geneList
              geneList.add(currentGene);
              //Set startIndex to just past the end of the gene
              startIndex = dna.indexOf(currentGene, startIndex) +
                           currentGene.length();
            }
          //Your answer is geneList
          return geneList;
        }
        public void testOn(String dna) {
            System.out.println("Testing getAllGenes on " + dna);
            StorageResource genes = getAllGenes(dna);
            for (String g: genes.data()) {
                System.out.println(g);
            }
        }
        public void test() {
            FileResource fr = new FileResource();
            String dna = fr.asString();
            //      ATGv  TAAv  ATG   v  v  TGA   
            //testOn("ATGATCTAATTTATGCTGCAACGGTGAAGA");
            testOn(dna);
            //      ATGv  v  v  v  TAAv  v  v  ATGTAA
            //testOn("ATGATCATAAGAAGATAATAGAGGGCCATGTAA");
        }
    }

1 个答案:

答案 0 :(得分:1)

文件中的数据类似于“acaagtttgtacaaaaaagcagaagggccgtcaaggcccaccatgcctattggatccaaagagaggccaacatttttt”。您正在搜索仅包含小写字符的字符串中的大写字符。 String.indexOf因此永远不会找到TAA,TAG或TGA。将字符串更改为小写。

int startIndex = dna.indexOf("atg", where);
...    
int taaIndex = findStopCodon(dna,startIndex,"taa");
int tagIndex = findStopCodon(dna,startIndex,"tag");
int tgaIndex = findStopCodon(dna,startIndex,"tga");

回复以下评论:如果您希望能够像处理文本一样处理混合大小写,则需要先lowercase()字符串。