-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkin_compare_functions.R
69 lines (57 loc) · 2.36 KB
/
kin_compare_functions.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
check_relationship <- function(row, external_df){
if (!is.na(row["relationship"])) {
return(row["relationship"])
}else{
if(isTRUE(row["Var1"] == row["Var2"])) {
return("same_sample")
}
else if (isTRUE(external_df$genotype[external_df$sample %in% row["Var1"]] ==
external_df$genotype[external_df$sample %in% row["Var2"]])) {
# Check if the individuals in the row are siblings
return("technical_replicate")}
# Check if the individuals in the row are parent-offspring
if (isTRUE(external_df$sample %in% row["Var1"]) &
isTRUE(external_df$mother %in% row["Var2"])) {
return("parent-offspring")
} else if (isTRUE(external_df$families[external_df$sample %in% row["Var1"]] ==
external_df$families[external_df$sample %in% row["Var2"]])) {
# Check if the individuals in the row are siblings
return("sibling")
} else if (isTRUE(external_df$site[external_df$sample %in% row["Var1"]] ==
external_df$site[external_df$sample %in% row["Var2"]])) {
# Check if the individuals in the row are from the same site
return("same_site")
} else if (isTRUE(external_df$dominant_pop[external_df$sample %in% row["Var1"]] ==
external_df$dominant_pop[external_df$sample %in% row["Var2"]])) {
# Check if the individuals in the row are from the same genetic group
return("same_genetic_group")
}
else{ return("unrelated") }
}
}
dist_kinship_matrix <- function(gt){
kin <- as.matrix(dist(gt, diag=TRUE))
kin_invert <- 1- (kin/max(kin, na.rm=TRUE))
return(kin_invert)
}
popkin_kinship_matrix <- function(gt){
X <- t(gt)
subpops <- rownames(gt)
kin <- popkin(X, subpops)
return(kin)
}
snprelate_kinship_matrix <- function(snpKin){
snpKin_matrix <- snpKin$kinship
colnames(snpKin_matrix) <- snpKin$sample.id
rownames(snpKin_matrix) <- snpKin$sample.id
return(snpKin_matrix)
}
kinship_by_relationship <- function(kin,parent_offspring, meta){ # kin is any pairwise kinship matrix, meta must have sample column
kin[upper.tri(kin, diag=FALSE)] <- NA
kin_df <- melt(kin, na.rm=TRUE)
if("tissue" %in% colnames(meta)){
kin_df <- merge(kin_df, parent_offspring, all.x=TRUE) %>% as.data.frame()
}
kin_df$relationship <- apply(kin_df, 1, check_relationship, external_df = meta)
return(kin_df)
}