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

Java代码无法在dna字符串中找到基因,java,Java,下面的代码来自JavaMooc,它应该可以找到给定文件中的所有基因。问题是我的getAllGenes方法没有返回给定dna字符串的任何基因。我只是不知道代码出了什么问题。 我测试的文件(brca1line.fa)位于此处 多谢各位 这是我的java代码 public class AllGenesStored { public int findStopCodon(String dnaStr, int star

下面的代码来自JavaMooc,它应该可以找到给定文件中的所有基因。问题是我的getAllGenes方法没有返回给定dna字符串的任何基因。我只是不知道代码出了什么问题。 我测试的文件(brca1line.fa)位于此处

多谢各位

这是我的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");
        }
    }
公共类AllGenesStored{
public int findStopCodon(字符串dnaStr,
int startIndex,
字符串停止密码子){
int currendex=dnaStr.indexOf(stopCodon,startIndex+3);
while(currendex!=-1){
int diff=currendex-startIndex;
如果(差异%3==0){
收益率指数;
}
否则{
currendex=dnaStr.indexOf(终止密码子,currendex+1);
}
}
返回-1;
}
公共字符串findGene(字符串dna,int-where){
int startIndex=dna.indexOf(“ATG”,其中);
如果(startIndex==-1){
返回“”;
}
int-taaIndex=findStopCodon(dna,startIndex,“TAA”);
int tagIndex=findStopCodon(dna,startIndex,“TAG”);
int TGADINDEX=找到的密码子(dna,startIndex,“TGA”);
int minIndex=0;
如果(taaIndex==-1||
(TGADINDEX!=-1&&TGADINDEX
文件中的数据类似于“ACAAGTGTTACAAAAAAAGCAGAGGCCGTCAGCCACACATGCCTATGGATCCAAGAGGAGCCACATTTTT”。您正在搜索仅包含小写字符的字符串中的大写字符<代码>字符串。因此,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()
字符串。

您可以更具体一些,比如“对于输入[xyz],我希望输出[abc]而获得[def]”。你试过调试吗?你有单元测试吗?也许,你也可以使用
String#equalsIgnoreCase
而不是仅仅使用
equals
,这样即使数据格式不规则,比如
aCAagtTt…
,也能确保成功。你不能使用
equalsIgnoreCase
来查找(相当长的)字符串中的子字符串的位置。