gender_specific_pathways.R 8.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env Rscript

# ================================================================================================
# Libraries
# ================================================================================================
library("yaml")
library("readxl")
library("tidyverse")
library("ggplot2")
source("../libs/conf/confR.R")
source("../libs/utils/utils.R")
message(paste0("[", Sys.time(), "] Libraries loaded."))

# ================================================================================================
# Configuration
# ================================================================================================
options(bitmapType = "cairo")
config          <- read_config(config_dirs = c("../Confs/", "./"))
output_data_dir <- paste0(config$global_data_dir, config$local_data_dir)
input_data_dir  <- paste0(config$global_data_dir, config$local_input_data_dir)
message(paste0("[", Sys.time(), "] Configuration done."))

# ================================================================================================
# Functions
# ================================================================================================

#' @title Compute the specificity scores of enriched pathways.
#' 
#' @description This functions takes two dataframes that contain the enriched pathways of a
#' reference and a control experiments. The functions then merge the two dataframes, computes
#' a Delta score that represents the specificity of the enrichemnt in the reference experiment
#' with respect to the control experiment. It then returs the pathways and their delta scores,
#' possibly filtered to retain only the most significant pathways.
#' 
#' @param DB_ref The reference data-frame, contains at least the pathway identifiers ("ID),
#' descriptions ("Description") and adjusted P values ("p.adjust").
#' @param DB_ctrl The control data-frame, contains at least the pathway identifiers ("ID),
#' descriptions ("Description") and adjusted P values ("p.adjust").
#' @pval_thres The threshold to use to return only the pathways that reach that level of
#' significance either in the control or in the reference experiments.
#' @return A dataframe that contains the original data augmented with the Delta scores.
compute_delta <- function(DB_ref, DB_ctrl, pval_thres = 0.1) {

  # Merging both dataframes, keeping onlyh the common functional terms.
  DB <- merge(x = DB_ref, y = DB_ctrl, by = "ID") %>%
    mutate (Description = Description.x,
            pval_ref    = p.adjust.x,
            pval_ctrl    = p.adjust.y) %>% select(ID, Description, pval_ref, pval_ctrl)

  # We compute the delta (-log P based delta).
  # Delta = -log10(P value reference) + log10( P value control).
  DB$logP_ref  <- -log10(DB$pval_ref)
  DB$logP_ctrl <- -log10(DB$pval_ctrl)
  DB$Delta     <- DB$logP_ref - DB$logP_ctrl

  # We keep only pathways that are significant or almost significant at least once (either
  # in ref or in control).
  res <- DB %>% filter(pval_ref < pval_thres | pval_ctrl < pval_thres) %>% arrange(Delta)
  
  # We clean and return the selected pathways.
  rm(DB)
  return(res)
}

# ================================================================================================
# Main
# ================================================================================================

# This script aims at merging the results of functional enrichment for male and female DEGs in
# order to select the pathways that are only enriched for one of the two genders (gender specific
# pathways).
#
gsp_data <- c()

#
# KEGG by CP
#
M_filename <- paste0(output_data_dir, "CP_Male_gsea_KEGG.tsv")
F_filename <- paste0(output_data_dir, "CP_Female_gsea_KEGG.tsv")
M_data  <- read.delim(M_filename, row.names = 1, stringsAsFactors = FALSE) %>%
  select(ID, Description, p.adjust)
F_data  <- read.delim(F_filename, row.names = 1, stringsAsFactors = FALSE) %>%
  select(ID, Description, p.adjust)
gsp_data <- rbind(gsp_data, compute_delta(M_data, F_data) %>%
                    mutate(tool = "CP", source = "KEGG"))
rm(M_filename, F_filename, M_data, F_data)

#
# MSIGDB by CP
#
M_filename <- paste0(output_data_dir, "CP_Male_gsea_MSIGDB.tsv")
F_filename <- paste0(output_data_dir, "CP_Female_gsea_MSIGDB.tsv")
M_data  <- read.delim(M_filename, row.names = 1, stringsAsFactors = FALSE) %>%
  select(ID, Description, p.adjust)
F_data  <- read.delim(F_filename, row.names = 1, stringsAsFactors = FALSE) %>%
  select(ID, Description, p.adjust)
gsp_data <- rbind(gsp_data, compute_delta(M_data, F_data) %>%
                    mutate(tool = "CP", source = "MSIGDB"))
rm(M_filename, F_filename, M_data, F_data)

#
# REACTOME by CP
#
M_filename <- paste0(output_data_dir, "CP_Male_gsea_REACTOME.tsv")
F_filename <- paste0(output_data_dir, "CP_Female_gsea_REACTOME.tsv")
M_data  <- read.delim(M_filename, row.names = 1, stringsAsFactors = FALSE) %>%
  select(ID, Description, p.adjust)
F_data  <- read.delim(F_filename, row.names = 1, stringsAsFactors = FALSE) %>%
  select(ID, Description, p.adjust)
gsp_data <- rbind(gsp_data, compute_delta(M_data, F_data) %>%
                    mutate(tool = "CP", source = "REACTOME"))
rm(M_filename, F_filename, M_data, F_data)

#
# GO-BP by CP
#
M_filename <- paste0(output_data_dir, "CP_Male_gsea_GOBP.tsv")
F_filename <- paste0(output_data_dir, "CP_Female_gsea_GOBP.tsv")
M_data  <- read.delim(M_filename, row.names = 1, stringsAsFactors = FALSE) %>%
  select(ID, Description, p.adjust)
F_data  <- read.delim(F_filename, row.names = 1, stringsAsFactors = FALSE) %>%
  select(ID, Description, p.adjust)
gsp_data <- rbind(gsp_data, compute_delta(M_data, F_data) %>%
                    mutate(tool = "CP", source = "GOBP"))
rm(M_filename, F_filename, M_data, F_data)

#
# GO-CC by CP
#
M_filename <- paste0(output_data_dir, "CP_Male_gsea_GOCC.tsv")
F_filename <- paste0(output_data_dir, "CP_Female_gsea_GOCC.tsv")
M_data  <- read.delim(M_filename, row.names = 1, stringsAsFactors = FALSE) %>%
  select(ID, Description, p.adjust)
F_data  <- read.delim(F_filename, row.names = 1, stringsAsFactors = FALSE) %>%
  select(ID, Description, p.adjust)
gsp_data <- rbind(gsp_data, compute_delta(M_data, F_data) %>%
                    mutate(tool = "CP", source = "GOCC"))
rm(M_filename, F_filename, M_data, F_data)

#
# GO-MF by CP
#
M_filename <- paste0(output_data_dir, "CP_Male_gsea_GOMF.tsv")
F_filename <- paste0(output_data_dir, "CP_Female_gsea_GOMF.tsv")
M_data  <- read.delim(M_filename, row.names = 1, stringsAsFactors = FALSE) %>%
  select(ID, Description, p.adjust)
F_data  <- read.delim(F_filename, row.names = 1, stringsAsFactors = FALSE) %>%
  select(ID, Description, p.adjust)
gsp_data <- rbind(gsp_data, compute_delta(M_data, F_data) %>%
                    mutate(tool = "CP", source = "GOMF"))
rm(M_filename, F_filename, M_data, F_data)

#
# MSIGDB by G2P
#
M_filename <- paste0(output_data_dir, "G2P_Male_MSIGDB.tsv")
F_filename <- paste0(output_data_dir, "G2P_Female_MSIGDB.tsv")
M_data  <- read.delim(M_filename, row.names = 1, stringsAsFactors = FALSE)
M_data$adj_pv = M_data$pv * dim(M_data)[1]
M_data$adj_pv[M_data$adj_pv > 1] <- 1
M_data <- M_data %>%
  select(pid, pname, adj_pv) %>% filter(!is.na(adj_pv)) %>%
  rename(ID = pid, Description = pname, p.adjust = adj_pv)
minp_M <- min(M_data$p.adjust[M_data$p.adjust > 0])
M_data$p.adjust[M_data$p.adjust == 0] <- minp_M / 100
F_data  <- read.delim(F_filename, row.names = 1, stringsAsFactors = FALSE)
F_data$adj_pv = F_data$pv * dim(F_data)[1]
F_data$adj_pv[F_data$adj_pv > 1] <- 1
F_data <- F_data %>%
  select(pid, pname, adj_pv) %>% filter(!is.na(adj_pv)) %>%
  rename(ID = pid, Description = pname, p.adjust = adj_pv)
minp_F <- min(F_data$p.adjust[F_data$p.adjust > 0])
F_data$p.adjust[F_data$p.adjust == 0] <- minp_F / 100
gsp_data <- rbind(gsp_data, compute_delta(M_data, F_data) %>%
                    mutate(tool = "G2P", source = "MSIGDB"))
rm(M_filename, F_filename, M_data, F_data, minp_M, minp_F)

# Save file with all specific pathways.
ofile <- paste0(output_data_dir, "Gender_specific_pathways.tsv")
write.table(gsp_data, file = ofile,  sep = "\t", quote = FALSE, col.names = NA)
rm(ofile, gsp_data)
message(paste0("[", Sys.time(), "] Gender specific pathways identified."))

# Final cleaning.
rm(config, input_data_dir, output_data_dir)
sessionInfo()