Debian Codegraph @

reports/cons.ll

raw
1 /*
2 * @progname cons.ll
3 * @version 1.0
4 * @author Teschler
5 * @category
6 * @output Text
7 * @description
8
9 Calculates coefficient of inbreeding F(A,B) for the offspring
10 of two individuals A and B.
11
12 The consanguity (blood in common) C(A,B) is 2*F(A,B)
13 F(A,B) = sum(0.5^(n(i)+p(i)) * (1+F(J(i))/2)
14 The sum extends over the number of distinct chains of relationship
15 connecting A and B. The ith chain has n(i)+p(i) links ascending
16 from A and B to the common ancestor J(i), whose coefficient of
17 inbreeding is f(J(i)).
18 A chain of relationship consists of all links leading from A and
19 B to a common ancestor J, and has no other point in common except
20 J. Two chains are considered distinct if they differ in at least
21 one link.
22
23 Result goes to file /tmp/t1
24 This is one of my first LL programs so please do not look
25 for elegance ;-)
26
27 [email protected]
28 */
29
30 global(anc_line) /* holds the current way from A towards B */
31 global(to_anc) /* B's ancestors */
32 global(from_anc) /* A's ancestors */
33 global(common_anc) /* A's and B's common ancestors */
34 global(common_stack) /* holds J(i) for later inbreed check */
35 global(anc_line_stack) /* holds lines for later output */
36 global(coefftab) /* holds inbreed coefficients for J(i) */
37
38 func coanc(A,B)
39 {
40 indiset(from_anc)
41 addtoset(from_anc,A,0)
42 set(from_anc,ancestorset(from_anc))
43 addtoset(from_anc,A,0)
44
45 indiset(to_anc)
46 addtoset(to_anc,B,0)
47 set(to_anc,ancestorset(to_anc))
48 addtoset(to_anc,B,0)
49
50 indiset(common_anc)
51 set(common_anc,intersect(from_anc,to_anc))
52
53 list(anc_line)
54 if (lengthset(common_anc))
55 {
56 push(anc_line_stack,"--") /*Marker*/
57 if (gt(lengthset(from_anc),lengthset(to_anc)))
58 {
59 call swap(from_anc,to_anc)
60 call swap(A,B)
61 }
62 call iter(A,0,B)
63 /*
64 At this point we have collected all paths
65 leading from A to B on anc_line_stack
66 Now we have to calculate f(J(i)) for all
67 common ancestors that are in our list of
68 paths (saved on common_stack), then we
69 can sum up things.
70 */
71 while(pers,dequeue(common_stack)) {
72 if (not(lookup(coefftab,key(pers,0)))) {
73 set(pc,coanc(father(pers),mother(pers)))
74 insert(coefftab,key(pers),pc)
75 }
76 }
77 print "Results for :"
78 print fullname(A,0,0,50) sp()
79 print fullname(B,0,0,50) nl()
80 set(result,sum_up())
81 }
82 else
83 {
84 set(result,"0 1")
85 }
86 return(result)
87 }
88
89 proc iter(current,common,target)
90 /* Recursively traverses the tree (better hedge)
91 to find all paths leading from current to
92 target. Makes use of precalculated sets
93 common_anc and to_anc.
94 Fills up a list of paths
95 */
96 {
97 print (".")
98 push(anc_line,current)
99 if (eq(current,target)) {
100 call found(common)
101 pop(anc_line)
102 return()
103 }
104 if (not(common)) {
105 /* We are ascending */
106 if (father(current)) {
107 call iter(father(current),0,target)
108 }
109 if (mother(current)) {
110 call iter(mother(current),0,target)
111 }
112 if (iselement(current,common_anc)) {
113 set(common,current)
114 }
115 }
116 if (common) {
117 /* We have found a common ancestor
118 now we check for descendants */
119 families(current,curfam,spouse,cnt) {
120 children(curfam,curchild,cnt) {
121 if (notchecked(curchild)) {
122 if(iselement(curchild,to_anc)) { /* <- speeds up! */
123 call iter(curchild,common,target)
124 } /* iselement */
125 } /* notchecked */
126 } /* children */
127 } /* families */
128 } /* common */
129 pop(anc_line)
130 }
131
132 proc found(common) {
133 /* Unfortunately LL pushes references.
134 I had liked to push values.
135 Now I have to do my own special stack handling.
136 Not very elegant, though :(
137 */
138 print("!")
139 push(anc_line_stack,"-") /*Marker*/
140 forlist(anc_line,pers,cnt) {
141 push(anc_line_stack,key(pers))
142 }
143 push(anc_line_stack,key(common))
144 push(common_stack,common)
145 }
146
147 func sum_up()
148 {
149 /*
150 pops anc_lines from anc_line_stack and sums
151 up their values.
152 prints them as a side effect, otherwise there would
153 be no need to save all those steps, the length would
154 have been enough
155 */
156 set(sum,"0 1")
157 set(lcnt,0)
158 set(element,pop(anc_line_stack))
159 while(strcmp(element,"--")) {
160 incr(lcnt)
161 set(common,element)
162 print "Common ancestor: " fullname(indi(common),0,0,50) nl()
163 set(factor,lookup(coefftab,common))
164 if (strcmp(factor,"0 1")) {
165 print "(Inbreeding coefficient: " showfrac(factor) ")" nl()
166 }
167 set(length,0)
168 set(pers,pop(anc_line_stack))
169 while(strcmp(pers,"-")) {
170 incr(length)
171 print " " d(length) " " fullname(indi(pers),0,0,50) nl()
172 set(pers,pop(anc_line_stack))
173 }
174 set(element,pop(anc_line_stack))
175 set(factor,addfrac("1 0",factor))
176 set(factor, mulfrac( factor,concat("1 ",d(length))))
177 print "------------" nl()
178 print showfrac(factor) nl() nl()
179 set(sum,addfrac(sum,factor))
180 }
181 print "============" nl()
182 print "Sum: " showfrac(sum) " (" d(lcnt) " different lines)" nl()
183 print nl()
184 return(sum)
185 }
186
187 /*
188 Some functions to handle fractions follow here.
189 Lifelines has no type fraction let's put nominator denominator
190 as space separated strings. As the denominator is always 2^x,
191 we put just x
192 */
193
194 func addfrac(A,B)
195 {
196 set(nomA,atoi(A))
197 set(denA,atoi(substring(A,index(A," ",1),strlen(A))))
198 set(nomB,atoi(B))
199 set(denB,atoi(substring(B,index(B," ",1),strlen(B))))
200
201 while (lt(denA,denB)) {
202 incr(denA)
203 set(nomA,mul(nomA,2))
204 }
205 while (lt(denB,denA)) {
206 incr(denB)
207 set(nomB,mul(nomB,2))
208 }
209
210 set(nomA,add(nomA,nomB))
211 while (eq(0,mod(nomA,2))) {
212 decr(denA)
213 set(nomA,div(nomA,2))
214 }
215
216 set(result,concat(d(nomA)," "))
217 return(concat(result,d(denA)))
218 }
219
220 func mulfrac(A,B)
221 {
222 /* Multiply my funny fractions */
223 set(nomA,atoi(A))
224 set(denA,atoi(substring(A,index(A," ",1),strlen(A))))
225 set(nomB,atoi(B))
226 set(denB,atoi(substring(B,index(B," ",1),strlen(B))))
227 set(nomA,mul(nomA,nomB))
228 set(denA,add(denA,denB))
229 while (eq(0,mod(nomA,2))) {
230 decr(denA)
231 set(nomA,div(nomA,2))
232 }
233
234 set(result,concat(d(nomA)," "))
235 return(concat(result,d(denA)))
236 }
237
238 func showfrac(A)
239 {
240 /* show my funny fractions */
241 set(nomA,atoi(A))
242 set(denA,atoi(substring(A,index(A," ",1),strlen(A))))
243 return(concat(d(nomA),concat("/",d(exp(2,denA)))))
244 }
245
246
247 proc swap(V1,V2)
248 {
249 set(help,V1)
250 set(V1,V2)
251 set(V2,help)
252 }
253
254 /* I'm sure there are better ways to handle the following two ... */
255
256 func iselement(E,S)
257 {
258 indiset(test)
259 addtoset(test,E,0)
260 return (lengthset(intersect(test,S)))
261 }
262
263 func notchecked(i)
264 {
265 forlist(anc_line,pers,cnt) {
266 if (eq(key(pers,0),key(i,0))) { return (0) }
267 }
268 return (1)
269 }
270
271 proc show_stack()
272 {
273 /* for debugging purposes */
274 print "Current:" nl()
275 forlist(anc_line,pers,cnt) {
276 print " " d(cnt) fullname(pers,0,0,50) nl()
277 }
278 }
279
280 proc main() {
281 getindimsg(from,"1st :")
282 getindimsg(to,"2nd :")
283 list(common_stack)
284 list(anc_line_stack)
285 table(coefftab)
286 newfile("/tmp/t1",0)
287 set(cf,mulfrac("2 0",coanc(from,to)))
288 print "Consanguity factor: " showfrac(cf) nl()
289 }