Saturday 6 November 2021

Independent Alleles (Rosalind | English)

             

rosalind


Hi everyone, how are you? This time, I want to discuss about one problem, bioinformatics problem, that exist in rosalind.info's web. The title is "Independent Alleles". For the reference, you can first check out the problem that will be discussed (here). 

Overview

In this problem, we will given 2 integers representing "which generation" and "the number of minimal offspring of AaBb", let's call them k and N

Our task is to find the probability of the AaBb offspring appearance; with minimum number of N; at the k-th generation. 

The idea is, to find the number of the AaBb offspring appearance with minimum number of N; then, divide it by the number of all possibilities. 


The probability of coloring X and Y.

The Code 

This is the code for solving this problem (with java language): 
  1. public double comb (int n, int k) {
  2. double atas = 1.0;
  3. for (int i = 1; i <= n; i++) {
  4. atas *= i;
  5. }
  6. for (int i = 1; i <= k; i++) {
  7. atas /= i;
  8. }
  9. for (int i = 1; i <= n - k; i++) {
  10. atas /= i;
  11. }
  12. return atas;
  13. }
  14. public void solve() {
  15. int k = in.readInt();
  16.         int n = in.readInt();
  17. int base = 1;
  18. for (int i = 0; i < k; i++) base *= 2;
  19.         double res = 0.0;
  20. for (int i = base; i >= n; i--) {
  21. res += comb(base, i) * Math.pow(1, i) * Math.pow(3, base - i) / Math.pow(4, base);
  22. }
  23. out.printf("%.3f\n", res);
  24. }

First, we can find the number of all possibilities (2^k). Here, I used the variable base to counting that possibilities. 

Then, for the calculation, I prefer to do it with the binomial distribution (line 20-22). The essence of the binomial distribution itself is to process all of the combination possible while counting each probability of these combination. That is exactly what we did in the code above: here, we were looking for the AaBb offspring's possible combinations that have N offsprings minimum while counting each probability of these combination. 

Input and Output

In the code above, I used in.readInt() function for entering the integer-form data (line 15-16). That function is an extend from one of the system inputs which are available in java, called BufferedReader. 

And for the output I used out.printf() function (line 23). That function is a modification from System.out.printf() function which is very familiar in java. You can see the additional code for that modification (input and output) in my complete code at github.

That's it. If you want to ask something, you can write it in the comment section below. I hope this article is useful and see you in the next article!


Reference :
Source of image 1 :https://www.facebook.com/ProjectRosalind/
Source of image 2 :http://rosalind.info/problems/lia/

No comments:

Post a Comment