Java程序故障

Java程序故障,java,dna-sequence,Java,Dna Sequence,我问题的前半部分:当我试图运行我的程序时,它会一直加载;它从不显示结果。有人能检查一下我的代码并在某处发现一个错误吗。这个程序的目的是找到一个起始DNA密码子ATG并一直寻找直到找到一个终止密码子TAA或TAG或TGA,然后从头到尾打印出基因。我用的是BlueJ 我问题的后半部分:我应该写一个程序,其中需要采取以下步骤: To find the first gene, find the start codon ATG. Next look immediately past ATG for the

我问题的前半部分:当我试图运行我的程序时,它会一直加载;它从不显示结果。有人能检查一下我的代码并在某处发现一个错误吗。这个程序的目的是找到一个起始DNA密码子ATG并一直寻找直到找到一个终止密码子TAA或TAG或TGA,然后从头到尾打印出基因。我用的是BlueJ

我问题的后半部分:我应该写一个程序,其中需要采取以下步骤:

To find the first gene, find the start codon ATG.
Next look immediately past ATG for the first occurrence of each of the three stop codons TAG, TGA, and TAA.
If the length of the substring between ATG and any of these three stop codons is a multiple of three, then a candidate for a gene is the start codon through the end of the stop codon.
If there is more than one valid candidate, the smallest such string is the gene. The gene includes the start and stop codon.
If no start codon was found, then you are done.
If a start codon was found, but no gene was found, then start searching for another gene via the next occurrence of a start codon starting immediately after the start codon that didn't yield a gene.
If a gene was found, then start searching for the next gene immediately after this found gene.
请注意,根据该算法,对于字符串“ATGCTGACCTAGE”,ATGCTGACCTAGE可以是一个基因,但ATGCTGACTGA不会是,即使它较短,因为首先找到的是另一个“TGA”实例,它与起始密码子的距离不是三的倍数

在我的作业中,我还被要求提出以下方法:

具体来说,要实现该算法,您应该执行以下操作

Write the method findStopIndex that has two parameters dna and index, where dna is a String of DNA and index is a position in the string. This method finds the first occurrence of each stop codon to the right of index. From those stop codons that are a multiple of three from index, it returns the smallest index position. It should return -1 if no stop codon was found and there is no such position. This method was discussed in one of the videos.
Write the void method printAll that has one parameter dna, a String of DNA. This method should print all the genes it finds in DNA. This method should repeatedly look for a gene, and if it finds one, print it and then look for another gene. This method should call findStopIndex. This method was also discussed in one of the videos.
Write the void method testFinder that will use the two small DNA example strings shown below. For each string, it should print the string, and then print the genes found in the string. Here is sample output that includes the two DNA strings:
样本输出为:

阿特加阿特加

发现的基因是:

阿特加

DNA字符串是:

CCATGCCTATAATGTCTGTAATGTAGA

发现的基因包括:

atgccctaa

atgtctgtaatgtag

DNA字符串是:

CATGTAATAGAATGAATGACTGATAGATATATCTTGTAGATGAATGAATGAATGAATGAATGAATGAATGAATGAATGAATAGATGCTTGTATGAATGAATGAATGAATGAATGAATGAATGCCA

发现的基因包括:

ATGTAA

ATGAATGACTGATAG

阿加塔加

ATGTGA

我仔细考虑了一下,发现这段代码非常接近正常工作状态。我只需要输出指令中要求的结果。希望这不会太混乱,我只是不知道如何在起始密码子之后寻找终止密码子,然后如何获取基因序列。我也希望通过找到三个标签(tag、tga、taa)中哪一个更接近atg来了解如何获得最接近的基因序列。我知道这是很多,但希望这一切都有意义

import edu.duke.*;
import java.io.*;

public class FindMultiGenes {
    public String findGenes(String dnaOri) {
        String gene = new String();
        String dna = dnaOri.toLowerCase();
        int start = -1;
        while(true){
            start = dna.indexOf("atg", start);
            if (start == -1) {
                break;
            }
            int stop = findStopCodon(dna, start); 
            if(stop > start){
                String currGene = dnaOri.substring(start, stop+3);

                System.out.println("From: " + start + " to " + stop + "Gene: "    
                +currGene);}
        }
        return gene;
    } 

    private int findStopCodon(String dna, int start){   
        for(int i = start + 3; i<dna.length()-3; i += 3){
            String currFrameString = dna.substring(i, i+3);

            if(currFrameString.equals("TAG")){
                return i;

            } else if( currFrameString.equals("TGA")){
                return i;

            } else if( currFrameString.equals("TAA")){
                return i;

            }
        }   
        return -1;
    }

    public void testing(){


        FindMultiGenes FMG = new FindMultiGenes();

        String dna =     
        "CATGTAATAGATGAATGACTGATAGATATGCTTGTATGCTATGAAAATGTGAAATGACCCA";

        FMG.findGenes(dna);




        System.out.println("DNA string is: " + dna);

    } 
}   
import edu.duke.*;
导入java.io.*;
公共类查找多基因{
公共字符串findGenes(字符串dnaOri){
字符串基因=新字符串();
字符串dna=dnaOri.toLowerCase();
int start=-1;
while(true){
start=dna.indexOf(“atg”,start);
如果(开始==-1){
打破
}
int-stop=findStopCodon(dna,start);
如果(停止>启动){
字符串currGene=dnaOri.substring(开始、停止+3);
System.out.println(“从:“+start+”到“+stop+”基因:”
+(基因);}
}
返回基因;
} 
私有int findStopCodon(字符串dna,int start){

对于(inti=start+3;i将行
start=dna.indexOf(“atg”,start);
更改为

start = dna.indexOf("atg", start + 1);
当前发生的情况是,您在索引
k
中找到
“atg”
,并在下一次运行中搜索字符串以查找下一个
“atg”
k
开始。这将在完全相同的位置查找下一个匹配项,因为起始位置是包含的。因此,您将一次又一次地查找相同的索引
k
,并且永远不会停止

通过将索引增加1,您可以跳过当前找到的索引
k
,并从
k+1
开始搜索下一个匹配项

这个程序的目的是找到一个起始DNA密码子ATG并一直寻找直到找到一个终止密码子TAA或TAG或TGA,然后从头到尾打印出基因

由于第一次搜索总是从0开始,您可以在那里设置开始索引,然后从结果中搜索停止密码子。这里我使用1个停止密码子进行搜索:

public static void main(String[] args) {

    String dna = "CATGTAATAGATGAATGACTGATAGATATGCTTGTATGCTATGAAAATGTGAAATGACCCA";
    String sequence = dna.toLowerCase();
    int index = 0;
    int newIndex = 0;
    while (true) {
        index = sequence.indexOf("atg", index);
        if (index == -1)
            return;
        newIndex = sequence.indexOf("tag", index + 3);
        if (newIndex == -1) // Check needed only if a stop codon is not guaranteed for each start codon.
            return;
        System.out.println("From " + (index + 3) + " to " + newIndex + " Gene: " + sequence.substring(index + 3, newIndex));
        index = newIndex + 3;
    }
}
输出:

From 4 to 7 Gene: taa
From 13 to 22 Gene: aatgactga
From 4 to 7 Gene: TAA
From 13 to 22 Gene: AATGACTGA
此外,您还可以使用正则表达式为您完成许多工作:

public static void main(String[] args) {

    String dna = "CATGTAATAGATGAATGACTGATAGATATGCTTGTATGCTATGAAAATGTGAAATGACCCA";

    Pattern p = Pattern.compile("ATG([ATGC]+?)TAG");
    Matcher m = p.matcher(dna);

    while (m.find())
        System.out.println("From " + m.start(1) + " to " + m.end(1) + " Gene: " + m.group(1));
}
输出:

From 4 to 7 Gene: taa
From 13 to 22 Gene: aatgactga
From 4 to 7 Gene: TAA
From 13 to 22 Gene: AATGACTGA

您的
while(true)
循环永远不会结束。我如何修复它?我写了一个break语句,但它似乎无助于循环结束。我认为
findStopCodon
中的字符串常量必须是小写。你可能对一个正则表达式解决方案感兴趣,它可以在几行内完成所有这些工作。@a.K.你是什么意思?我只是把算法放在一个标准中rd
main
。除了那一个,我没有写任何方法。你可以复制粘贴它,然后自己尝试。我的错误是,我一开始没有看到
main
。@A.K.它在
main
中,这样它就可以作为一个独立的程序轻松运行。它将替换执行搜索的代码部分。当我运行你的代码时,它会显示不兼容pes:第一次返回时缺少返回值。这是为什么?@A.K.您没有正确复制某些内容。我编写的任何方法都没有返回值。只需将代码放入新类并运行它。如果出现错误,请告诉我它在哪一行。