-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathOrphanet_annotate_genes_to_ancestors.R
executable file
·250 lines (180 loc) · 8.37 KB
/
Orphanet_annotate_genes_to_ancestors.R
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# Associate disease genes to ancestor terms
# Ize Buphamalai
# Updated: 09.04.2020
# input: ontology structure: ordo (EBI lookup service rols object)
# input: gene-orphanet disease association
# The code find all ancestors for all terms and sequentially associate genes to upper levels on ontology
#################
## Load required packages and data
################
library(pacman)
pacman::p_load("rols", "stringr")
#Read orphanet gene association data (immediate association)
source("./read_orphanet_gene_association_data.R")
#################
## Ontological data extraction
################
# extract the ontology structure from Ontology Lookup service
print("read the ontology from the EBI ontology lookup service")
ordo = Ontology("ordo")
terms = terms(ordo)
# store all term IDs
all_terms = termId(terms)
terms_df = as(terms, "data.frame")
## translational table between diseases with genes and disease groups
terms_with_genes = terms[termId(terms) %in% orphaGeneDisease$orphaID]
##############
# Modify a function to collect the ancestor terms from the ontology
# is_a: natural recognition of ancestor search
# part_of: a disease is a part of a disease group, but not directly detected from ancestor search on rols package.
# the following part of the code is to search two levels up on 'part of' branch
###############
# create a new function based on PartOf
# suppress messsage for terms with no associated 'PartOf' terms
# following is the copy of PartOf from the rols package, modified by removing the message line
# and replace it to the function
partOf_suppress = function (id)
{
stopifnot(inherits(id, "Term"))
url <- id@links$part_of[[1]]
if (is.null(url)) {
return(NULL)
}
x <- httr::GET(url)
stop_for_status(x)
cx <- content(x)
ans <- lapply(cx[["_embedded"]][[1]], makeTerm)
names(ans) <- sapply(ans, termId)
Terms(x = ans)
}
assignInNamespace("partOf", partOf_suppress, ns="rols")
#fixInNamespace("partOf", "rols")
partOf_extend = function(term){
firstLevel = partOf(term)
secondLevel = lapply(firstLevel, function(x) partOf(x))
results = c(unlist(firstLevel), unlist(secondLevel))
results = unique(unlist(lapply(results, termId)))
return(results)
}
# annotate 'partof' terms to the terms with associated genes
############
print(" annotate 'partof' terms to the terms with associated genes")
# compute ' part of' terms - results are terms that the disease is a 'part of'
terms_with_genes_parts_extend = lapply(terms_with_genes, function(x) suppressMessages(partOf_extend(x)))
# remove terms labelled as NULL
terms_with_genes_parts_extend_rm_null = terms_with_genes_parts_extend[!sapply(terms_with_genes_parts_extend, is.null)]
# create a function to retrieve all ancestors of a term
###############
print("retrieve all ancestors of all terms")
all_ancestors = function(termlist){
if(is.character(termlist)){
termlist = terms[all_terms %in% termlist]
}
IDancestors = lapply(termlist, function(y) termId(ancestors((y))))
IDancestors = unique(unlist(IDancestors))
return(IDancestors)
}
# apply the function -- this process might take very long
terms_with_genes_ancestors = lapply(terms_with_genes_parts_extend_rm_null, all_ancestors)
#############
## get the parents info
all_children = function(termlist){
if(is.character(termlist)){
termlist = terms[all_terms %in% termlist]
}
IDchildren = lapply(termlist, function(y) termId(children((y))))
IDchildren = unique(unlist(IDchildren))
return(IDchildren)
}
# apply the function -- this process might take very long
# descendants of the rare genetic diseases
termlist = descendants(terms[all_terms == "Orphanet:98053"][[1]])
terms_children = lapply(termlist, function(x) suppressMessages(children(x)))
term_childern_chr = terms_children[!sapply(terms_children, length) == 0]
term_childern_chr = lapply(term_childern_chr, termId)
term_childern_df = melt(term_childern_chr)
colnames(term_childern_df) = c("to", "from")
disease_descendants = lapply(terms[all_terms == "Orphanet:98053"], descendants)
#################
# store the list of genes associated to disease group
#################
print("annotate disease associate genes cumulatively to ancestors")
# Expanded disease Gene List: associate gene to ancestor terms as well
expandedDiseaseGeneList = list()
for(i in names(orphaGeneDiseaseList)){
extended_terms = terms_with_genes_parts_extend_rm_null[[i]]
if(!is.null(extended_terms)){
#extended_terms = termId(extended_terms)
for(j in extended_terms){
if(!is_empty(expandedDiseaseGeneList[[j]])){
expandedDiseaseGeneList[[j]] = append(expandedDiseaseGeneList[[j]], orphaGeneDiseaseList[[i]])
}
else{
expandedDiseaseGeneList[[j]] = orphaGeneDiseaseList[[i]]
}
}
}
}
# combine expanded association with the original association
combinedDiseaseGeneList = c(expandedDiseaseGeneList, orphaGeneDiseaseList)
print("saving the results")
saveRDS(combinedDiseaseGeneList, file = "../cache/orphanet_disease_gene_association_with_ancestors.RDS")
##################
#################
# orphaID to name
#################
orphaID_to_name_df = read_csv("../data/orphanumbers_to_name.csv", col_names = c("orphaID", "orphaName"))
orphaID_to_name_df$orphaID = paste0("Orphanet:", orphaID_to_name_df$orphaID)
orpha_to_gene_df = melt(orphaGeneDiseaseList)
colnames(orpha_to_gene_df) = c("gene", "orphaID")
############################
# extract the children of 'rare genetic diseases' and the labels
rare_genetic_subterms <- termId(children(terms[all_terms == "Orphanet:98053"][[1]]))
rare_genetic_subterms_label <- sapply(rare_genetic_subterms, function(x) terms[[x]]@label)
# take only ancestors that are in rare genetic diseases
terms_with_genes_ancestors_in_rare_genetic <- lapply(terms_with_genes_ancestors, function(x) intersect(x, rare_genetic_subterms))
terms_with_genes_ancestors_in_rare_genetic_label <- lapply(terms_with_genes_ancestors_in_rare_genetic, function(x) paste(rare_genetic_subterms_label[x],collapse = ";"))
terms_rare_genetic_label_df <- melt(terms_with_genes_ancestors_in_rare_genetic_label)
colnames(terms_rare_genetic_label_df) <- c("roots", "orphaID")
#load orphaGeneDisease
source("./read_orphanet_gene_association_data.R")
orphaGeneDisease_with_ancestor <- left_join(orphaGeneDisease, terms_rare_genetic_label_df)
orphaGeneDisease_with_ancestor$genes <- sapply(orphaGeneDisease_with_ancestor$genes, function(x) paste(x, collapse = ";"))
orphaGeneDisease_with_ancestor <- left_join(orphaGeneDisease_with_ancestor, terms_df[,c("id", "label","description")], by = c("orphaID" = "id"))
write_tsv(orphaGeneDisease_with_ancestor[,c("orphaID","label","genes","n_genes", "description", "roots")], path = "../data/orphaNet_disease_gene_association_with_roots.tsv")
###################
## add the onset
#################
## Onset information
#################
onsetfiles = list.files("../data/orphanet_onset/")
# get orphanet IDs that the onset was mentioned
onset_id = list()
for(i in onsetfiles){
onset_disease = readLines(paste0("../data/orphanet_onset/", i))
onset_id[[i]] = terms_df %>% dplyr::filter(label %in% onset_disease) %>% pull(id)
}
orpha_to_onset_df = melt(onset_id); colnames(orpha_to_onset_df) = c("orphaID", "onset")
#################
## merge orpha - gene - onset
# join the data
orpha_gene_onset_df = full_join(orpha_to_onset_df, orpha_to_gene_df)
# make onset a ordered factor
orpha_gene_onset_df$onset = factor(orpha_gene_onset_df$onset, levels = c("antenatal", "neonatal", "infancy", "childhood",
"adolescent", "adult", "all_ages", "elderly"),
ordered = T)
saveRDS(orpha_gene_onset_df, "../cache/orpha_gene_onset_df.RDS")
# 1.summarise for each disease
orpha_by_earliest_onset_df = orpha_gene_onset_df %>%
dplyr::filter(!is.na(onset), !is.na(gene)) %>%
group_by(orphaID) %>% summarise(earliest_onset = min(onset))
# map the orphaName to it
orpha_by_earliest_onset_df = left_join(orpha_by_earliest_onset_df, orphaID_to_name_df)
# 2. summarise based on earliest onset for each gene
gene_by_earliest_onset_df = orpha_gene_onset_df %>%
dplyr::filter(!is.na(onset), !is.na(gene)) %>%
group_by(gene) %>% summarise(earliest_onset = min(onset))
# number of genes associated to each onset type
gene_by_earliest_onset_df %>% group_by(earliest_onset) %>% count()
# save the information
saveRDS(gene_by_earliest_onset_df, "../cache/gene_by_earliest_onset_orphanet.RDS")