Zettelkasten

source("_preloader.r", verbose=T)


######
# Psych - correlate everything to everything w/ t-value
######
out.dir = file.path(resultsDir, "results.03 - 1")
dir.create(out.dir)

#overlapping.norm.glucold = c("hsa-miR-375", "hsa-miR-203a-3p")
overlapping.norm.glucold <- readr::read_csv(
    file.path("results.02", "all.limma.meta.analysis.csv")
  ) %>%
  dplyr::filter(meta.p.adj.bh <= 0.05) %>%
  dplyr::pull(g.new.id)

PsychMasterTable = miRNASampleTable %>%
  tibble::rownames_to_column(
    "rowid"
  ) %>%
  dplyr::rename(
    Orig.Sample.Id = Sample.Id,
    Sample.Id = rowid
  ) %>%
  dplyr::mutate(
    Expr.Sample.Id = paste0(
      "X",
      gsub(
        x = Sample.Id,
        pattern = "^X|_$",
        replacement = ""
      )
    )
  ) %>%
  dplyr::filter(
    Expr.Sample.Id %in% colnames(miRNAGeneExprData)
  ) %>%
  tibble::column_to_rownames("Sample.Id")

snp = miRNAData[
    overlapping.norm.glucold,
    rownames(PsychMasterTable)
  ] %>%
  tibble::rownames_to_column("snpid")

sign.miRNA.expr.data = snp[snp$snpid %in% overlapping.norm.glucold, ]
sign.miRNA.expr.data.RowNames = sign.miRNA.expr.data$snpid
sign.miRNA.expr.data = sign.miRNA.expr.data %>% dplyr::select(-snpid)
sign.miRNA.expr.data = sign.miRNA.expr.data %>% t() %>% as.data.frame()
colnames(sign.miRNA.expr.data) = sign.miRNA.expr.data.RowNames

ge = limma::voom(miRNAGeneExprData)[, PsychMasterTable$Expr.Sample.Id] %>% as.data.frame()
colnames(ge) = rownames(PsychMasterTable)

mRNA.expr.data = ge %>% tibble::rownames_to_column("geneid")
mRNA.expr.data.RowNames = mRNA.expr.data$geneid
mRNA.expr.data = mRNA.expr.data %>% dplyr::select(-geneid)
mRNA.expr.data = mRNA.expr.data %>% t() %>% as.data.frame()
colnames(mRNA.expr.data) = mRNA.expr.data.RowNames

x1 = sign.miRNA.expr.data
y1 = mRNA.expr.data

cor = psych::corr.test(
  x = x1,
  y = y1,
  method = "pearson",
  adjust = "none"
)


r0=cor$r
r0=t(r0)

p0=cor$p
p0=t(p0)

t0=cor$t
t0=t(t0)

mart = useMart(
  biomart="ENSEMBL_MART_ENSEMBL",
  host="grch37.ensembl.org",
  path="/biomart/martservice",
  dataset="hsapiens_gene_ensembl"
)
G_list = getBM(
  filters = "ensembl_gene_id",
  attributes = c(
    "ensembl_gene_id",
    "hgnc_symbol"
  ),
  values = unique(rownames(r0)),
  mart = mart
)
G_list = G_list %>%
  dplyr::group_by(ensembl_gene_id) %>%
  dplyr::mutate(i = dplyr::row_number()) %>%
  dplyr::filter(i == 1) %>%
  dplyr::select(ensembl_gene_id, hgnc_symbol) %>%
  tibble::column_to_rownames("ensembl_gene_id")

psychTable = r0 %>%
  as.data.frame() %>%
  tibble::rownames_to_column("row.names") %>%
  tidyr::gather(
    key = "miRNA.id",
    value = "r",
    -row.names
  ) %>%
  dplyr::left_join(
    y = t0 %>%
      as.data.frame() %>%
      tibble::rownames_to_column("row.names") %>%
      tidyr::gather(
        key = "miRNA.id",
        value = "t",
        -row.names
      ),
    by = c(
      "row.names" = "row.names",
      "miRNA.id" = "miRNA.id"
    )
  ) %>%
  dplyr::left_join(
    y = p0 %>%
      as.data.frame() %>%
      tibble::rownames_to_column("row.names") %>%
      tidyr::gather(
        key = "miRNA.id",
        value = "p",
        -row.names
      ),
    by = c(
      "row.names" = "row.names",
      "miRNA.id" = "miRNA.id"
    )
  ) %>%
  dplyr::mutate(
    gene.symbol = G_list[row.names, "hgnc_symbol"]
  )

readr::write_csv(
  x = cor$r %>% t() %>% as.data.frame() %>% tibble::rownames_to_column("row.id") %>% dplyr::mutate(gene.symbol = G_list[row.id, "hgnc_symbol"]),
  path = file.path(out.dir, "psych.r.csv")
)

readr::write_csv(
  x = cor$p %>% t() %>% as.data.frame() %>% tibble::rownames_to_column("row.id") %>% dplyr::mutate(gene.symbol = G_list[row.id, "hgnc_symbol"]),
  path = file.path(out.dir, "psych.p.csv")
)

readr::write_csv(
  x = cor$t %>% t() %>% as.data.frame() %>% tibble::rownames_to_column("row.id") %>% dplyr::mutate(gene.symbol = G_list[row.id, "hgnc_symbol"]),
  path = file.path(out.dir, "psych.t.csv")
)


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

network.analysis.genes <- readr::write_csv(
  x = psychTable %>%
    group_by(miRNA.id) %>%
    mutate(
      p.adj = stats::p.adjust(
        p = p,
        method = "BH",
        n = dplyr::n()
      )
    ),
  path = file.path(out.dir, "psych.with.fdr.csv")
) %>%
dplyr::filter(
  p.adj <= 0.05
)


readr::write_csv(
  x = PsychMasterTable %>%
    tibble::rownames_to_column("row.names"),
  path = file.path(out.dir, "psych.patientTable.csv")
)

readr::write_csv(
  x = x1 %>%
    tibble::rownames_to_column("row.names"),
  path = file.path(out.dir, "psych.x.miRNA.csv")
)

readr::write_csv(
  x = y1 %>%
    tibble::rownames_to_column("row.names"),
  path = file.path(out.dir, "psych.y.mRNA.csv")
)

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

readr::write_csv(
  x = ge %>%
    tibble::rownames_to_column("entrez.id"),
  path = file.path(out.dir, "ge.csv")
)