> Pasi Havia wrote:
Hi,
I am making a little program which calculates Wright's inbreeding
coefficient.
What I have a trouble with is converting the formula into a code.
The formula is:
Fx=?[(0,5) n + n´ + 1 * (1 + FA)]
So which part are you having problems with?
double ComputeCoeefici ent(int n1, int n2) {
return pow(0.5, n1+n2-1);
}
This is if you are using the author's notation for the generations.
(which is off by one for the sire and dame lines, so it ends up being
-1 rather than +1).
The formula is:
Fx=?[(0,5) n + n´ + 1 * (1 + FA)]
So which part are you having problems with?
double ComputeCoeefici ent(int n1, int n2) {
return pow(0.5, n1+n2-1);
}
This is if you are using the author's notation for the generations.
(which is off by one for the sire and dame lines, so it ends up being
-1 rather than +1).
No you´re not right. If some of the sibling have same mother/father then
this calculation won't do. Note that the FA is calculated the same way as Fx
which will lead into recursion.
I already got two alternative options to implement this but I'm having
problems with both of them.
Option #1
Pedigree is in tree structure.
function inbreed(dog k)
inbreed = 0
(mother's_grand parent_, n1) = first_grandpare nt(k.mother)
(father's_grand parent, n2) = first_grandpare nt(k.father)
do while mother's_grandp arent == null
(mother's_grand parent, n1) = next_grandparen t(k.mother,
mother's_grandp arent)
do while father's_grandp arent == null
(father's_grand parent, n2) = next_grandparen t(k.father,
father's_grandp arent)
if mother's_grandp arent == father's_grandp arent
inbreed += (0,5^(n1 + n2 + 1)) * (1 +
inbreed(mother' s_grandparent))
Option #2
Using recursion to solve the formula.
function calculate_inbre ed(dog)
return inbreed1(dog.mo ther, 0, dog.father, 0)
function inbreed1(dog1, n1, dog2, n2)
inbreed = inbreed2(dog1, n1, dog2, n2)
if dog1.mother
inbreed += inbreed1(dog1.m other, n1+1, dog2, n2)
if dog1.father
inbreed += inbreed1(dog1.f ather, n1+1, dog2, n2)
return inbreed
function inbreed2(dog1, n1, dog2, n2)
inbreed = 0
if dog1 == dog2
inbreed += (0,5^(n1 + n2 + 1)) * (1 + inbreed(dog1))
if dog2.mother
inbreed += inbreed_father( dog1, n1, dog2.mother, n2+1)
if dog2.father
inbreed += inbreed_father( dog1, n1, dog2.father, n2+1)
return inbreed
In the option #2 I don't know what calculation I should perform in
inbreed_father method. So the problem goes a little more deep than you first
expected I think ;)
-Pasi