简体   繁体   English

需要帮助使用Java创建氨基酸置换系统

[英]Need help creating permuation system of amino acids using java

My problem requires me to create a list of all 20 amino acids permutations based on a user imputed chain length. 我的问题需要我根据用户估算的链长创建所有20个氨基酸排列的列表。 For example, I currently have code that works if the user wants a 3 chain length. 例如,如果用户想要3个链长,我目前有一些代码可以工作。 This is 20^3 possibilities. 这是20 ^ 3的可能性。 So I have 3 nested for loops that run through all possibilities, then a counter that outputs the number of permutations to make sure the answer is correct. 所以我有3个嵌套的for循环,它们遍历所有可能性,然后是一个计数器,输出输出排列数以确保答案正确。 How could I code this method so it output permutations based on user input? 如何编码此方法,以便根据用户输入输出排列?

protected void getPermutations(int chainlength) {
    int counter = 0;
    for (int i = 0; i < 20; i++) {
        for (int j = 0; j < 20; j++) {
            for (int k = 0; k < 20; k++) {
                System.out.println(AcidArray[i].getTcode() + "-"
                        + AcidArray[j].getTcode() + "-"
                        + AcidArray[k].getTcode());
                counter++;
            }
        }
    }
    System.out.println("chain length = " + chainlength);
    System.out.println(counter + " permutations");
}

Thanks 谢谢

Recursion is your friend in this situation 在这种情况下, 递归是您的朋友

protected String getPermutations(int chainlength) {
    int counter = 0;
    if(chainlength > 0) { // so that the counter is not 1
        counter = getSubPermutations("", chainlength));
    }
    System.out.println("chain length = " + chainlength);
    System.out.println(counter + " permutations");
}

private int getSubPermutations(String prefix, int chainlength){
   if(chainlength == 0){ //The bottom of the stack, print out the combination
      System.out.println(prefix.substring(0,prefix.length-1)); //remove the final '-'
      return 1;
   } else {
      int counter = 0
      for(int i = 0; i < 20; i++) {
        //Add this level T code to the string and pass it on
        counter += getSubPermutations(prefix + AcidArray[i].getTcode() + "-", chainlength-1);
      }
      return counter;
   }
}

What this will do is make a tree of calls. 这将是建立呼叫树。 If chainlength is one then it will call getSubPermutations with 1 . 如果chainlength为1,则它将使用1调用getSubPermutations This will run through the for loop calling getSubPermutations again with the String for the first value and a chainlength of 0 . 这将通过for循环再次运行,其中第一个值为String,链长为0再次调用getSubPermutations In this case the string will only have one T code in it. 在这种情况下,字符串中仅包含一个T代码。 Each inner call will hit the first if statement so it will print out the string containing one T code and return 1 . 每个内部调用将命中第一个if语句,因此它将打印出包含一个T代码的字符串并返回1 All these will be added up so the counter returned to getPermutations will be 20. By this stage all the permutations will have been printed out. 所有这些都将加起来,因此返回到getPermutations的计数器将为20。到此阶段,所有排列都将被打印出来。

As chain length increases getSubPermuations is called recursively. 随着链长的增加,递归调用getSubPermuations With a chainlength of 2 it will call getSubPermutations 20 times with a chain length of 1 , passing in the string of the T code. 链长为2 ,它将以链长为1调用getSubPermutations 20次,并传入T代码的字符串。 Each of these will call getSubPermutations with a chainlength of 0, with a string containing two T codes . 每一个都将调用getSubPermutations长为0的getSubPermutations, 该字符串包含两个T代码 This will then print out the full string and return 1 . 然后将打印出完整的字符串并返回1 These return values will get added up to 20 as in the previous example but now when they are returned to the next level they are added up to return a final 400 to getPermutations and 400 Strings will have been printed. 这些返回值将像上一个示例一样累加到20,但是现在当它们返回到下一个级别时,它们被累加起来以返回最终的400到getPermutations并且将打印400个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM