| 1 |
/* |
| 2 |
* @progname genetics.ll |
| 3 |
* @version 2.0.1 |
| 4 |
* @author Eggert |
| 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. It does this by finding all the |
| 11 |
common ancestors, known or implied, and their ancestral distance along |
| 12 |
any known path to the two people. |
| 13 |
|
| 14 |
genetics - a LifeLines report program to calculate degree of relatedness |
| 15 |
by Jim Eggert ([email protected]) |
| 16 |
Version 1 (15 Sept 1995) |
| 17 |
Version 2 (19 Sept 1995) added multiple identical birth capability |
| 18 |
Version 2.0.1 (1 Jul 2002) Fix to run with newer LifeLines (Perry Rapp) |
| 19 |
|
| 20 |
This LifeLines report program computes the degree of blood relatedness |
| 21 |
between any two people in a database. It does this by finding all the |
| 22 |
common ancestors, known or implied, and their ancestral distance along |
| 23 |
any known path to the two people. Ancestors are assumed to exist even |
| 24 |
when they are not explicitly in the database if their existence can be |
| 25 |
deduced from the family structure. This most commonly occurs when the |
| 26 |
mother of a family is unknown, but can be assumed to be identical when |
| 27 |
two children are in the database as siblings. Likewise, when both the |
| 28 |
mother and the father are missing, this program will assume them to be |
| 29 |
identical for siblings in a family. If any of the ancestors are twins |
| 30 |
or other multiple identical births, the program will determine this as |
| 31 |
a possibility based on equality of birthyears and will ask the user to |
| 32 |
verify the identical nature of the twins. |
| 33 |
|
| 34 |
Because the program is pretty picky, it will only report half-siblings |
| 35 |
and half-cousins. You are forced to add up the halves to get the full |
| 36 |
picture. But it will find all known genetic relationships between the |
| 37 |
two individuals and calculate a genetic overlap fraction. This number |
| 38 |
ranges between 0 (not related) to 1 (same person). The program cannot |
| 39 |
handle nontraditional families (when more than one husband and/or wife |
| 40 |
exists in the family). And it doesn't check for adoptions, it assumes |
| 41 |
that all children are the genetic children of their parents. |
| 42 |
|
| 43 |
This code uses the unusual construct of a table of lists of lists. |
| 44 |
*/ |
| 45 |
|
| 46 |
global(twin_table) |
| 47 |
|
| 48 |
proc main() { |
| 49 |
|
| 50 |
table(twin_table) |
| 51 |
|
| 52 |
/* Get the first individual and find ancestors and multiplicities */ |
| 53 |
getindimsg(p1,"Enter first person.") |
| 54 |
table(anc1_table) |
| 55 |
set(kp1,save(key(p1))) |
| 56 |
call recur_anc(kp1,0,anc1_table,0) |
| 57 |
|
| 58 |
/* Get the second individual and find ancestors and relatedness |
| 59 |
only up to common ancestors. |
| 60 |
*/ |
| 61 |
getindimsg(p2,"Enter second person.") |
| 62 |
list(lca_list) |
| 63 |
table(lca_table) |
| 64 |
set(kp2,save(key(p2))) |
| 65 |
call recur_anc(kp2,lca_list,lca_table,anc1_table) |
| 66 |
|
| 67 |
/* Now calculate relations */ |
| 68 |
if (length(lca_list)) { |
| 69 |
print(kp1," ",name(indi(kp1))," is\n",kp2," ",name(indi(kp2)),"'s\n") |
| 70 |
list(gsums) |
| 71 |
set(gmax,0) |
| 72 |
forlist(lca_list,lca,ilca) { |
| 73 |
set(ll,lookup(anc1_table,lca)) |
| 74 |
set(gl1,getel(ll,1)) |
| 75 |
set(kl1,getel(ll,2)) |
| 76 |
set(ll,lookup(lca_table,lca)) |
| 77 |
set(gl2,getel(ll,1)) |
| 78 |
set(kl2,getel(ll,2)) |
| 79 |
forlist(gl1,g1,il1) { |
| 80 |
set(k1,getel(kl1,il1)) |
| 81 |
forlist(gl2,g2,il2) { |
| 82 |
set(k2,getel(kl2,il2)) |
| 83 |
call print_rel(kp1,k1,k2,g1,g2) |
| 84 |
set(gsum,add(g1,g2)) |
| 85 |
enqueue(gsums,gsum) |
| 86 |
if (gt(gsum,gmax)) { set(gmax,gsum) } |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
else { |
| 92 |
print(kp1," ",name(indi(kp1))," and ",kp2," ",name(indi(kp2)), |
| 93 |
" are not related by blood.\n") |
| 94 |
return() |
| 95 |
} |
| 96 |
|
| 97 |
/* Add up path weights */ |
| 98 |
set(gsum,0) |
| 99 |
forlist(gsums,g,gnum) { |
| 100 |
set(gpow,1) |
| 101 |
while(lt(g,gmax)) { |
| 102 |
set(gpow,add(gpow,gpow)) |
| 103 |
incr(g) |
| 104 |
} |
| 105 |
set(gsum,add(gsum,gpow)) |
| 106 |
} |
| 107 |
/* Cancel common factors of 2 */ |
| 108 |
if (gsum) { |
| 109 |
while (not(mod(gsum,2))) { |
| 110 |
set(gsum,div(gsum,2)) |
| 111 |
decr(gmax) |
| 112 |
} |
| 113 |
} |
| 114 |
/* Figure common denominator */ |
| 115 |
set(gpow,1) |
| 116 |
while(gmax) { |
| 117 |
set(gpow,add(gpow,gpow)) |
| 118 |
decr(gmax) |
| 119 |
} |
| 120 |
/* Print out final answer */ |
| 121 |
print("Expected degree of genetic overlap: ",d(gsum),"/",d(gpow),"\n") |
| 122 |
} |
| 123 |
|
| 124 |
/* This is the magic routine that does the real work. |
| 125 |
If there is no input stop_table, calculate all the |
| 126 |
ancestors along all paths of the input person, and return |
| 127 |
the ancestors and their multiplicities. |
| 128 |
If there is an input stop_table, calculate the ancestors |
| 129 |
up to the ones contained in the stop table, and return |
| 130 |
only the ones in the stop table and their multiplicities. |
| 131 |
Notes: If there were a fortable() iterator, then the anc_list |
| 132 |
would be unnecessary. The fake keys are used to simulate |
| 133 |
ancestors who aren't explicitly in the database. |
| 134 |
The table entries are lists of two elements. The first element |
| 135 |
is a list of generation counts for a path to that ancestor or |
| 136 |
his or her twin, the second element is a list of actual keys of the |
| 137 |
ancestor. These actual keys differ only if the ancestor is a twin. |
| 138 |
If the ancestor is a twin, the key to the table entry is the key of |
| 139 |
the "oldest" twin. |
| 140 |
*/ |
| 141 |
proc recur_anc(kp,anc_list,anc_table,stop_table) { |
| 142 |
list(keys) |
| 143 |
list(gens) |
| 144 |
enqueue(keys,kp) |
| 145 |
enqueue(gens,0) |
| 146 |
while (ka,dequeue(keys)) { |
| 147 |
set(g,dequeue(gens)) |
| 148 |
set(k,first_twin(ka)) |
| 149 |
if (stop_table) { |
| 150 |
set(stop,lookup(stop_table,k)) |
| 151 |
} |
| 152 |
if (or(not(stop_table),stop)) { |
| 153 |
if (ll,lookup(anc_table,k)) { |
| 154 |
set(l,getel(ll,1)) |
| 155 |
set(kl,getel(ll,2)) |
| 156 |
} |
| 157 |
else { |
| 158 |
list(ll) |
| 159 |
list(l) |
| 160 |
list(kl) |
| 161 |
enqueue(ll,l) |
| 162 |
enqueue(ll,kl) |
| 163 |
insert(anc_table,k,ll) |
| 164 |
if (anc_list) { enqueue(anc_list,k) } |
| 165 |
} |
| 166 |
enqueue(l,g) |
| 167 |
enqueue(kl,ka) |
| 168 |
} |
| 169 |
if (not(stop)) { |
| 170 |
if (a,indi(k)) { |
| 171 |
incr(g) |
| 172 |
if (par,parents(a)) { |
| 173 |
if (aa,father(a)) { |
| 174 |
enqueue(keys,save(key(aa))) |
| 175 |
} |
| 176 |
else { |
| 177 |
enqueue(keys,save(concat("H0",key(par)))) /* fake */ |
| 178 |
} |
| 179 |
if (aa,mother(a)) { |
| 180 |
enqueue(keys,save(key(aa))) |
| 181 |
} |
| 182 |
else { |
| 183 |
enqueue(keys,save(concat("W0",key(par)))) /* fake */ |
| 184 |
} |
| 185 |
enqueue(gens,g) |
| 186 |
enqueue(gens,g) |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
proc print_rel(kp1,k1,k2,g1,g2) { |
| 194 |
set(p1,indi(kp1)) |
| 195 |
if (lt(g1,g2)) { set(deg,g1) set(rem,sub(g2,g1)) } |
| 196 |
else { set(deg,g2) set(rem,sub(g1,g2)) } |
| 197 |
if (strcmp(k1,k2)) { |
| 198 |
incr(deg) /* twin ancestors */ |
| 199 |
set(halftwin,"twin-") |
| 200 |
} else { set(halftwin,"half-") } |
| 201 |
if (eq(deg,0)) { |
| 202 |
if (eq(rem,0)) { print("self") } |
| 203 |
else { |
| 204 |
while (gt(rem,2)) { print("g") decr(rem) } |
| 205 |
if (gt(rem,1)) { print("grand") } |
| 206 |
if (gt(g1,g2)) { |
| 207 |
/* print("half-") */ |
| 208 |
if (male(p1)) { print("son") } |
| 209 |
elsif (female(p1)) { print("daughter") } |
| 210 |
else { print("child") } |
| 211 |
} |
| 212 |
else { |
| 213 |
if (male(p1)) { print("father") } |
| 214 |
elsif (female(p1)) { print("mother") } |
| 215 |
else { print("parent") } |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
elsif (eq(deg,1)) { |
| 220 |
if (eq(rem,0)) { |
| 221 |
print(halftwin) |
| 222 |
if (male(p1)) { print("brother") } |
| 223 |
elsif (female(p1)) { print("sister") } |
| 224 |
else { print("sibling") } |
| 225 |
} |
| 226 |
else { |
| 227 |
while (gt(rem,2)) { print("g") decr(rem) } |
| 228 |
if (gt(rem,1)) { print("grand") } |
| 229 |
if (gt(g1,g2)) { |
| 230 |
print(halftwin) |
| 231 |
if (male(p1)) { print("nephew") } |
| 232 |
elsif (female(p1)) { print("niece") } |
| 233 |
else { print("niece/nephew") } |
| 234 |
} |
| 235 |
else { |
| 236 |
if (male(p1)) { print("uncle") } |
| 237 |
elsif (female(p1)) { print("aunt") } |
| 238 |
else { print("aunt/uncle") } |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
else { |
| 243 |
print(ord(sub(deg,1))," ",halftwin,"cousin") |
| 244 |
if (eq(rem,1)) { print(" once") } |
| 245 |
elsif (eq(rem,2)) { print(" twice") } |
| 246 |
elsif (eq(rem,3)) { print(" thrice") } |
| 247 |
elsif (gt(rem,3)) { print(" ",card(rem)," times") } |
| 248 |
if (rem) { print(" removed") } |
| 249 |
} |
| 250 |
print("\n via their ancestor ",k1," ") |
| 251 |
if (p1,indi(k1)) { print(name(p1)) } |
| 252 |
else { |
| 253 |
print("Unknown ") |
| 254 |
if (strcmp(substring(k1,1,1),"H")) { print("wife") } |
| 255 |
else { print("husband") } |
| 256 |
print(" in family ",substring(k1,3,strlen(k1))) |
| 257 |
} |
| 258 |
if (strcmp(k1,k2)) { |
| 259 |
print("\n and twin ",k2," ",name(indi(k2))) |
| 260 |
} |
| 261 |
print("\n") |
| 262 |
} |
| 263 |
|
| 264 |
func first_twin(pkey) { |
| 265 |
if (tkey,lookup(twin_table,pkey)) { return(tkey) } |
| 266 |
set(ft,0) |
| 267 |
if (p,indi(pkey)) { |
| 268 |
if (parents(p)) { |
| 269 |
if (b,birbapyear(p)) { |
| 270 |
set(loop,1) |
| 271 |
while(loop) { |
| 272 |
set(loop,0) |
| 273 |
if (q,prevsib(p)) { |
| 274 |
if (not(strcmp(sex(p),sex(q)))) { |
| 275 |
if (eq(b,birbapyear(q))) { |
| 276 |
print(key(p)," ",name(p), |
| 277 |
birbapdate(p)," and\n") |
| 278 |
print(key(q)," ",name(q), |
| 279 |
birbapdate(q)) |
| 280 |
getint(rt, |
| 281 |
"Are these individuals identical twins? (0=no, 1=yes)") |
| 282 |
if (rt) { |
| 283 |
set(p,q) |
| 284 |
set(loop,1) |
| 285 |
set(ft,p) |
| 286 |
print(" are twins\n\n") |
| 287 |
} |
| 288 |
else { print(" are not twins\n\n") } |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
if (ft) { set(tkey,save(key(ft))) } else { set(tkey,pkey) } |
| 297 |
insert(twin_table,pkey,tkey) |
| 298 |
return(tkey) |
| 299 |
} |
| 300 |
|
| 301 |
func birbapyear(person) { |
| 302 |
if (b,birth(person)) { |
| 303 |
if (byear,atoi(year(b))) { return(byear) } |
| 304 |
} |
| 305 |
if (b,baptism(person)) { |
| 306 |
if (byear,atoi(year(b))) { return(byear) } |
| 307 |
} |
| 308 |
return(0) |
| 309 |
} |
| 310 |
|
| 311 |
func birbapdate(person) { |
| 312 |
if (b,birth(person)) { |
| 313 |
if (byear,atoi(year(b))) { return(concat(" born ",date(b))) } |
| 314 |
} |
| 315 |
if (b,baptism(person)) { |
| 316 |
if (byear,atoi(year(b))) { return(concat(" bapt ",date(b))) } |
| 317 |
} |
| 318 |
return("") |
| 319 |
} |