Zettelkasten

source("_preloader.r", verbose=T)
source("../other.useful.functions.r", verbose=TRUE)

# load
tT_1 <- readr::read_csv(
    file.path(resultsDir, "results.01", "limma.csv")
  ) %>%
  tibble::column_to_rownames("id")


PsychMasterTable <- readr::read_csv(
    file.path(resultsDir, "results.03 - 1", "psych.patientTable.csv")
  ) %>%
  tibble::column_to_rownames("row.names")

psychTable <- readr::read_csv(
    file.path(resultsDir, "results.03 - 2", "meta.psych.csv")
)


######
# Predictive Targets
# Only with significant miRNA's (not long non-coding RNA)
######
out.dir <- file.path(resultsDir, "results.04")
dir.create(out.dir)

miRnaIds <- readr::read_csv(
    file.path("results.02", "all.limma.meta.analysis.csv")
  ) %>%
  dplyr::filter(meta.p.adj.bh <= 0.05) %>%
  dplyr::select(
    g.new.id
  ) %>%
  dplyr::mutate(
    id = g.new.id
  ) %>%
  dplyr::rename(
    miRnaId = g.new.id
  )


predictiveTargets = data.frame(
  target = character(),
  miRNA_id = character(),
  rank_final = numeric(),
  rank_product = numeric(),
  entrez_id = numeric(),
  gene_symbol = character()
)


for (i in 1:nrow(miRnaIds)) {
  miRnaId = str_replace(
    as.character(miRnaIds[i, "miRnaId"]),
    pattern = "hsa-",
    replacement = ""
  )
  targets = miRNAtap::getPredictedTargets(miRnaId, species = "hsa", synonyms = F)

  target_symbols <- mapIds(
    org.Hs.eg.db,
    keys = rownames(targets),
    column = "SYMBOL",
    keytype = "ENTREZID",
    multivals = "first"
  )

  targetTable = cbind(
      target = rep(miRnaId, length(target_symbols)),
      miRNA_id = as.character(miRnaIds[i, "miRnaId"]),
      targets[, c("rank_final", "rank_product")],
      gene_symbol = target_symbols[rownames(targets)]
    ) %>%
    as.data.frame() %>%
    tibble::rownames_to_column("entrez_id") %>%
    dplyr::select(
      target,
      miRNA_id,
      rank_final,
      rank_product,
      entrez_id,
      gene_symbol
    )

  predictiveTargets = rbind(predictiveTargets, targetTable)

  readr::write_csv(
    x = targetTable,
    path = file.path(out.dir, paste0(miRnaId, ".csv"))
  )

  rm(list = c("miRnaId", "targets", "target_symbols", "targetTable"))
}

predictiveTargets = predictiveTargets %>%
  dplyr::mutate(
    target = as.character(target),
    miRNA_id = as.character(miRNA_id),
    rank_final = as.numeric(rank_final),
    rank_product = as.numeric(rank_product),
    entrez_id = as.numeric(entrez_id),
    gene_symbol = as.character(gene_symbol)
  )

readr::write_csv(
  x = predictiveTargets,
  path = file.path(out.dir, "predictiveTargets.csv")
)

######
# Combine Psych and Predictive Targets
######
correlationVsPredictiveTargets = psychTable %>%
  dplyr::left_join(
    y = predictiveTargets,
    by = c(
      "miRNA.id" = "miRNA_id",
      "gene.symbol" = "gene_symbol"
    )
  ) %>%
  dplyr::filter(
    !is.na(rank_final)
  ) %>%
  adj.p.value(
    group.col = "miRNA.id",
    p.col = "meta.p"
  ) %>%
  dplyr::arrange(
    meta.p,
    rank_final,
    miRNA.id
  )

readr::write_csv(
  x = correlationVsPredictiveTargets,
  path = file.path(out.dir, "predictiveTargets.adjusted.csv")
)