Skip to content

Commit 26ff865

Browse files
committed
Added dna_fraction_tests.R script
1 parent f20c915 commit 26ff865

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

data-raw/dna_fraction_tests.R

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
library(tidyverse)
2+
3+
4+
# Read data --------------------------------------------
5+
6+
dna_table <- data.table::fread("shiny-app-bs4/libs/dna-content-fractions.csv")
7+
8+
9+
# Calculate fraction ---------------------------------------
10+
11+
get_fraction <- function(dna_table, chromosome, color, sex) {
12+
# Construct color/chromosome table
13+
color_table <-
14+
cbind(
15+
color,
16+
chromosome
17+
) %>%
18+
as.data.frame() %>%
19+
mutate(
20+
chromosome = as.character(chromosome)
21+
)
22+
23+
# Full table
24+
full_table <- inner_join(color_table, dna_table, by = "chromosome") %>%
25+
group_by(color) %>%
26+
summarise(frac = sum(base::get(paste0("fraction_", sex))))
27+
28+
# Calculate first sum
29+
single_sum <- full_table %>%
30+
dplyr::select(frac) %>%
31+
summarise(sum(frac * (1 - frac))) %>%
32+
unname() %>%
33+
unlist()
34+
35+
# Calculate second sum
36+
if (nrow(full_table) >= 2) {
37+
cross_sum <- full_table[["frac"]] %>%
38+
combn(2) %>%
39+
t() %>%
40+
as.data.frame() %>%
41+
summarise(sum(V1 * V2)) %>%
42+
unname() %>%
43+
unlist()
44+
} else {
45+
cross_sum <- 0
46+
}
47+
48+
return(2 / 0.974 * (single_sum - cross_sum))
49+
}
50+
51+
52+
# Test function --------------------------------------------
53+
54+
# From Excel sheet
55+
get_fraction(
56+
dna_table,
57+
c(1, 4),
58+
c("red", "green"),
59+
"male"
60+
)
61+
62+
get_fraction(
63+
dna_table,
64+
c(1, 4),
65+
c("red", "red"),
66+
"male"
67+
)
68+
69+
# From IAEA
70+
get_fraction(
71+
dna_table,
72+
c(1, 2, 4, 3, 5, 6),
73+
c(rep("red", 3), rep("green", 3)),
74+
"female"
75+
)
76+
77+
get_fraction(
78+
dna_table,
79+
c(1, 2, 4),
80+
c(rep("red", 3)),
81+
"female"
82+
)

0 commit comments

Comments
 (0)