-
Notifications
You must be signed in to change notification settings - Fork 0
/
positions.R
137 lines (92 loc) · 3.23 KB
/
positions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
set_positions <- function(data, init_id){
# Get first absolute generation
gens_relative <- sapply(data$individuals, function(x) if(is.null(x$gen_relative)) NA else x$gen_relative)
gens_relative_unique <- sort(unique(gens_relative, na.rm=T))
gen_relative_first <- min(gens_relative_unique)
# Sort by generation
data$individuals <- data$individuals[order(gens_relative)]
# Create list for positions
positions <- list()
.build_tree <- function(id, gen_absolute){
#cat("IND: ", id, "\n", sep ="")
# Get invidiual
ind <- data$individuals[[id]]
# Gen generation (as character)
gen_chr <- as.character(gen_absolute)
# Get spouses
spouse_ids <- .get_spouses(data,id)
# IF this individual does not have a position...
if(!id %in% positions[[gen_chr]]){
# Append to position
positions[[gen_chr]] <<- c(positions[[gen_chr]], id)
# Set generation
data$individuals[[id]][["gen"]] <<- gen_absolute
for(fam_id in names(spouse_ids)){
#cat(strrep(" ", gen_absolute), "FAM:", fam_id, "\n", "")
# Ge spouse id
spouse_id <- spouse_ids[fam_id]
# Set spouse to same generation
data$individuals[[spouse_id]][["gen"]] <<- gen_absolute
# Append spouse to position
if(!spouse_id %in% positions[[gen_chr]]){
positions[[gen_chr]] <<- c(positions[[gen_chr]], spouse_id)
}
child_ids <- .get_children(data, fam_id)
for(child_id in child_ids){
.build_tree(child_id, gen_absolute + 1)
}
}
}
}
# Build initial tree
gen_relative_init <- data$individuals[[init_id]][['gen_relative']] - gen_relative_first + 1
.build_tree(init_id, gen_relative_init)
# Fill in remaining
for(id in names(data$individuals)){
gen <- data$individuals[[id]][['gen_relative']] - gen_relative_first + 1
.build_tree(id, gen)
}
if(!all(names(data$individuals) %in% unlist(positions))) stop("Not all positions have been determined")
# Apply positions
for(i in 1:length(positions)){
for(j in 1:length(positions[[i]])){
id <- positions[[i]][j]
data$individuals[[id]][["position"]] <- j
}
}
# Clear 'gen_relative'
for(id in names(data$individuals)){
data$individuals[[id]][['gen_relative']] <- NULL
}
return(data)
}
.get_spouses <- function(data, ind_id){
ind <- data$individuals[[ind_id]]
fams_ids <- ind[["FAMS"]]
if(!is.null(fams_ids)){
spouses <- sapply(fams_ids, function(fam_id){
setdiff(unlist(data$families[[fam_id]][c('HUSB', 'WIFE')]), ind_id)
})
myears <- sapply(fams_ids, function(fam_id){
myear <- data$families[[fam_id]]$MARR$YEAR
if(is.null(myear)) NA else myear
})
morder <- order(myears)
return(spouses[morder])
}else{
return(c())
}
}
.get_children <- function(data, fam_id){
chil <- data$families[[fam_id]]$CHIL
if(is.null(chil)){
return(c())
}else{
byears <- sapply(chil, function(x){
byear <- data$individuals[[x]]$BIRT$YEAR
if(is.null(byear)) NA else byear
})
border <- order(byears)
return(chil[border])
}
}