Debian Codegraph @

reports/genetics2.ll

raw
1 /*
2 * @progname genetics2.ll
3 * @version 1 of 1995-10-05
4 * @author Alexander Ottl ([email protected])
5 * @category
6 * @output Text
7 * @description
8
9 This LifeLines report program computes the degree of blood relatedness
10 between any two people in a database.
11
12 Genetic distance d(A,B) is defined recursively by:
13 d(A,A) = 1
14 d(A,B) = d(B,A)
15 d(A,B) = d(F(A),B) / 2 + d(M(A),B) / 2
16 with F(A) and M(A) being the father and mother of A.
17
18 The recursive procedure computedist() follows that definition.
19 That's the beauty of recursion.
20
21 by Alexander Ottl ([email protected])
22 Version 1 (5 Oct 1995)
23
24 */
25
26 global(R0)
27 global(R1)
28
29 proc main()
30 {
31 getindimsg(A, "First person:")
32 getindimsg(B, "Second person:")
33 call computedist(A, B)
34 print("\nExpected degree of genetic overlap: ",
35 d(R0), "/", d(R1), "\n")
36 }
37
38 /* BOOL ancestor( INDI, INDI ) */
39 func ancestor(A, B)
40 {
41 if(not(strcmp(key(A),key(B)))) {
42 return(1)
43 }
44 families(A, Fam, Spo, Num1) {
45 children(Fam, Chl, Num2) {
46 if(ancestor(Chl, B)) {
47 return(1)
48 }
49 }
50 }
51 return(0)
52 }
53
54 /* Actually this should be a function returning a rational number.
55 I might use a list, but I chose to use two global variables
56 R0 and R1 for the numerator and denominator */
57 /* VOID computedist( INDI, INDI ) */
58 proc computedist(A, B)
59 {
60 /* Recursion must terminate some time.
61 One's distance to himself is 1/1 */
62 if(not(strcmp(key(A),key(B)))) {
63 set(R0,1)
64 set(R1,1)
65 }
66 /* If there is a direct line from A down to B, we must work our way
67 upwards from B. There must of course then be no line
68 from B down to A, but no one is his own ancestor, right? */
69 elsif(ancestor(A, B)) {
70 /* print("Common ancestor: ", name(A), "\n") */
71 call computedist(B, A)
72 }
73 /* Now we try to work our way upwards through the parents */
74 else {
75 set(R0,0)
76 set(R1,1)
77 if(F,father(A)) {
78 call computedist(F, B)
79 /* Result by half */
80 set(R1, mul(2, R1))
81 }
82 if(M,mother(A)) {
83 /* Save previous result */
84 set(Res0, R0)
85 set(Res1, R1)
86 call computedist(M, B)
87 /* Result by half */
88 set(R1, mul(2, R1))
89 /* Adding up with previous result */
90 set(common, mul(R1, Res1))
91 set(R0, add(mul(R0, Res1), mul(R1, Res0)))
92 set(R1, common)
93 call normalize()
94 }
95 }
96 }
97
98 /* This is not an all-purpose normalizing function.
99 We expect the denominator R1 to be a power of 2 and
100 to be greater than the numerator R0. */
101 /* VOID normalize(VOID) */
102 proc normalize()
103 {
104 if(R0) {
105 while(not(mod(R0,2))) {
106 set(R0, div(R0,2))
107 set(R1, div(R1,2))
108 }
109 }
110 else {
111 set(R1,1)
112 }
113 }