dukelearntoprogram DNA链找基因问题 Java

1.FindGeneSimple

介绍:

利用密码子ATGTAA找到DNA链中Gene的简易程序

Java

public class FindGeneSimpleAndTest {
    public String findGeneSimple(String dna){

        String result = "";
        int startIndex = dna.indexOf("ATG");
        if(startIndex == -1) // no ATG
        {
            return "";
        }
        int stopIndex = dna.indexOf("TAA", startIndex + 3);

        if(stopIndex == -1){ // no TTA
            return "";
        }
        result = dna.substring(startIndex, stopIndex + 3);
        return result;
    }

    public void testFindGeneSimple(){
        String dna = "AATGCGTAATATGGT";
        System.out.println("DNA strand is " + dna);
        String gene = findGeneSimple(dna);
        System.out.println("Gene is " + gene);

        dna = "AATGCTAGGGTAATATGGT";
        System.out.println("DNA strand is " + dna);
        gene = findGeneSimple(dna);
        System.out.println("Gene is " + gene);

        dna = "ATCCTATGCTTCGGCTGCTCTAATATGGT";
        System.out.println("DNA strand is " + dna);
        gene = findGeneSimple(dna);
        System.out.println("Gene is " + gene);

        dna = "TTATTA";
        System.out.println("DNA strand is " + dna);
        gene = findGeneSimple(dna);
        System.out.println("Gene is " + gene);

        dna = "CGATGGTTTG";
        System.out.println("DNA strand is " + dna);
        gene = findGeneSimple(dna);
        System.out.println("Gene is " + gene);

    }

    public static void main(String[] args) {
        FindGeneSimpleAndTest findGeneSimpleAndTest = new FindGeneSimpleAndTest();
        findGeneSimpleAndTest.testFindGeneSimple();
    }


}

2.FindGeneWhile

介绍:

利用密码子ATGTAA找到DNA链中Gene的简易程序,同时考虑基因长度必须为3的倍数(1个密码子有3个碱基)

Java

public class FindGeneWhile {
    public String findGene(String dna){
        int startIndex = dna.indexOf("ATG");
        int currIndex = dna.indexOf("TAA", startIndex + 3);
        while(currIndex != -1){
            if( (currIndex - startIndex) % 3 == 0){
                //if so
               return dna.substring(startIndex, currIndex + 3);
            }
            //if no
            else{
                currIndex = dna.indexOf("TAA", currIndex + 1);
            }
        }
        return "";
    }
    public void testFindGeneSimple(){
        String dna = "AATGCGTAATTAATCG";
        System.out.println("DNA strand is ");
        String gene = findGene(dna);
        System.out.println("Gene is " + gene);
    }

    public static void main(String[] args) {
        FindGeneWhile findGeneWhile = new FindGeneWhile();
        findGeneWhile.testFindGeneSimple();
    }
}

3.AllCodons

介绍:

利用密码子ATG找到DNA链中Gene的简易程序,同时考虑基因长度必须为3的倍数(1个密码子有3个碱基),并且ATG对应3个密码子TAATAGTGA

Java

public class AllCodons {
    public int findStopCondon(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 dnaStr.length();
    }

    public String findGene(String dna){
        int startIndex = dna.indexOf("ATG");
        if(startIndex == -1){
            return "";
        }
        int taaIndex = findStopCondon(dna, startIndex, "TAA");
        int tagIndex = findStopCondon(dna, startIndex, "TAG");
        int tgaIndex = findStopCondon(dna, startIndex, "TGA");

        int temp = Math.min(taaIndex, tagIndex);
        int minIndex = Math.min(temp, tgaIndex);
        if(minIndex == dna.length()){//nothing found
            return "";
        }

        return dna.substring(startIndex, minIndex + 3);


    }

    public void testFindStop(){
        String dna = "xxxyyyzzzTAAxxxyyyzzzTAAxx";
        int dex = findStopCondon(dna, 0, "TAA");
        if(dex != 9) System.out.println("error on 9");

        dna = "AATGCGTAATATGGT";
        System.out.println("DNA strand is " + dna);
        String gene = findGene(dna);
        System.out.println("Gene is " + gene);

        dna = "AATGCTAGGGTAATATGGT";
        System.out.println("DNA strand is " + dna);
        gene = findGene(dna);
        System.out.println("Gene is " + gene);

        dna = "ATCCTATGCTTCGGCTGCTCTAATATGGT";
        System.out.println("DNA strand is " + dna);
        gene = findGene(dna);
        System.out.println("Gene is " + gene);

        dna = "TTATTA";
        System.out.println("DNA strand is " + dna);
        gene = findGene(dna);
        System.out.println("Gene is " + gene);

        dna = "CGATGGTTTG";
        System.out.println("DNA strand is " + dna);
        gene = findGene(dna);
        System.out.println("Gene is " + gene);
    }

    public static void main(String[] args) {
        AllCodons allCodons = new AllCodons();
        allCodons.testFindStop();
    }
}

4. AllCodons(printAllGenes)

介绍:

利用密码子ATG找到DNA链中Gene的简易程序,同时考虑基因长度必须为3的倍数(1个密码子有3个碱基),并且ATG对应3个密码子TAATAGTGA。并且可以在一条dna链中找到多个基因。

Java

public class AllCodons {
    public int findStopCondon(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 dnaStr.length();
    }

    public String findGene(String dna, int where){
        int startIndex = dna.indexOf("ATG", where);
        //System.out.println("hapens or not");
        if(startIndex == -1){
            return "";
        }
        //System.out.println("hapens or not 2");
        int taaIndex = findStopCondon(dna, startIndex, "TAA");
        int tagIndex = findStopCondon(dna, startIndex, "TAG");
        int tgaIndex = findStopCondon(dna, startIndex, "TGA");
       // System.out.println("hapens or not 3");
        int minIndex = 0;
        if( taaIndex == -1 || (tgaIndex != -1 && tgaIndex < taaIndex) ){
            minIndex = tgaIndex;
        }else {
            minIndex = taaIndex;
        }
        if(minIndex == -1 || (tgaIndex != -1 && tagIndex < minIndex)){
            minIndex = tagIndex;
        }
        if(minIndex == -1){
            return "";
        }
        //System.out.println("hapens or not 4");
        //System.out.println(minIndex);
        return dna.substring(startIndex, minIndex + 3);
    }

    public void printAllGenes(String dna){
        int startIndex = 0;
        String currentGene;
        while (true){
            currentGene = findGene(dna, startIndex);
            //System.out.println("happens");
            if(currentGene.isEmpty()){
                break;
            }
            System.out.println(currentGene);
            //System.out.println("before sI= " + startIndex);
            startIndex = dna.indexOf(currentGene, startIndex) + currentGene.length();
           // System.out.println("after sI= " + startIndex);
        }

    }
    public void testFindStop(String dna){
        System.out.println("Testing printAllGenes on " + dna);
        printAllGenes(dna);
    }

    public static void main(String[] args) {
        AllCodons allCodons = new AllCodons();
        allCodons.testFindStop("ATGATCTAATTTATGCTGCAACGGTGAAGA");
        allCodons.testFindStop("");
        allCodons.testFindStop("ATGATCATAAGAAGATAATAGAGGGCCATGTAA");
    }
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值