Skip to content

Commit

Permalink
Added dna_fraction_tests.R script
Browse files Browse the repository at this point in the history
  • Loading branch information
aldomann committed Sep 6, 2020
1 parent f20c915 commit 26ff865
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions data-raw/dna_fraction_tests.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
library(tidyverse)


# Read data --------------------------------------------

dna_table <- data.table::fread("shiny-app-bs4/libs/dna-content-fractions.csv")


# Calculate fraction ---------------------------------------

get_fraction <- function(dna_table, chromosome, color, sex) {
# Construct color/chromosome table
color_table <-
cbind(
color,
chromosome
) %>%
as.data.frame() %>%
mutate(
chromosome = as.character(chromosome)
)

# Full table
full_table <- inner_join(color_table, dna_table, by = "chromosome") %>%
group_by(color) %>%
summarise(frac = sum(base::get(paste0("fraction_", sex))))

# Calculate first sum
single_sum <- full_table %>%
dplyr::select(frac) %>%
summarise(sum(frac * (1 - frac))) %>%
unname() %>%
unlist()

# Calculate second sum
if (nrow(full_table) >= 2) {
cross_sum <- full_table[["frac"]] %>%
combn(2) %>%
t() %>%
as.data.frame() %>%
summarise(sum(V1 * V2)) %>%
unname() %>%
unlist()
} else {
cross_sum <- 0
}

return(2 / 0.974 * (single_sum - cross_sum))
}


# Test function --------------------------------------------

# From Excel sheet
get_fraction(
dna_table,
c(1, 4),
c("red", "green"),
"male"
)

get_fraction(
dna_table,
c(1, 4),
c("red", "red"),
"male"
)

# From IAEA
get_fraction(
dna_table,
c(1, 2, 4, 3, 5, 6),
c(rep("red", 3), rep("green", 3)),
"female"
)

get_fraction(
dna_table,
c(1, 2, 4),
c(rep("red", 3)),
"female"
)

0 comments on commit 26ff865

Please sign in to comment.