This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
df <- read.csv("KDM_data_25.csv", stringsAsFactors = FALSE)
fisher_vs_A3 <- function(df, genotype_label) {
  d <- df %>% filter(Genotype == genotype_label)
  stopifnot("A3" %in% d$Mutation)
  A3 <- d %>% filter(Mutation == "A3") %>% slice(1)
  out_list <- lapply(setdiff(unique(d$Mutation), "A3"), function(mod) {
    mrow <- d %>% filter(Mutation == mod) %>% slice(1)
    mat <- matrix(
      c(mrow$Males, mrow$Females, A3$Males, A3$Females),
      nrow = 2, byrow = TRUE,
      dimnames = list(Mutation = c(mod, "A3"), Sex = c("M","F"))
    )
    ft_two  <- fisher.test(mat, alternative = "two.sided")
    ft_less <- fisher.test(mat, alternative = "less")  # modifier lower than LexA
    data.frame(
      Genotype      = genotype_label,
      Mutation          = mod,
      M_mod         = mrow$Males,
      F_mod         = mrow$Females,
      M_A3        = A3$Males,
      F_A3        = A3$Females,
      prop_mod      = mrow$Males / (mrow$Males + mrow$Females),
      prop_A3     = A3$Males / (A3$Males + A3$Females),
      OR_hat        = unname(ft_two$estimate),
      OR_LCL95      = ft_two$conf.int[1],
      OR_UCL95      = ft_two$conf.int[2],
      p_two_sided   = ft_two$p.value,
      p_one_less    = ft_less$p.value,
      stringsAsFactors = FALSE
    )
  })
  out_tbl <- bind_rows(out_list)
  out_tbl$p_FDR_two_sided <- p.adjust(out_tbl$p_two_sided, method = "BH")
  out_tbl$p_FDR_one_less  <- p.adjust(out_tbl$p_one_less,  method = "BH")
  out_tbl %>% arrange(p_FDR_one_less, p_one_less)
}
# Run separately for each genotype
fisher_HWT  <- fisher_vs_A3(df, "HWT")
fisher_K16R <- fisher_vs_A3(df, "K16R")
# Optional combined table
fisher_all <- bind_rows(fisher_HWT, fisher_K16R)
# Peek
fisher_HWT
fisher_K16R
# View(fisher_all)

```