-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_download_phenotypes.R
More file actions
184 lines (162 loc) · 9.5 KB
/
1_download_phenotypes.R
File metadata and controls
184 lines (162 loc) · 9.5 KB
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
##################################################
## Project: DGRPool
## Script purpose: Downloading phenotypes using JSON API
## Version: 1.0.0
## Date Created: 2022 Dec 22
## Date Modified: 2024 Jul 15
## Author: Vincent Gardeux (vincent.gardeux@epfl.ch)
##################################################
# Libraries
suppressPackageStartupMessages(library(jsonlite))
suppressPackageStartupMessages(library(data.table))
# Parameters
output_file <- "data.all_pheno_15_07_24_filtered.rds"
# Reading all studies
download.file("https://dgrpool.epfl.ch/studies.json", destfile = "studies.json.gz", quiet = T)
json_studies <- fromJSON(txt = "studies.json.gz")
tmp <- file.remove("studies.json.gz")
json_studies <- json_studies[with(json_studies, order(id)),]
rownames(json_studies) <- json_studies$id
message(nrow(json_studies), " studies found")
# Reading all phenotypes
download.file("https://dgrpool.epfl.ch/phenotypes.json?all=1", destfile = "phenotypes.json.gz", quiet = T)
json_phenotypes <- fromJSON(txt = "phenotypes.json.gz")
tmp <- file.remove("phenotypes.json.gz")
json_phenotypes <- json_phenotypes[with(json_phenotypes, order(id)),]
rownames(json_phenotypes) <- json_phenotypes$id
message(nrow(json_phenotypes), " phenotypes found")
# Reading all "standard" DGRP lines
data_dgrps <- fread("https://dgrpool.epfl.ch/studies/1/get_file?name=dgrp_lines.tsv&namespace=downloads", sep = "\t", data.table = F)
data.dgrp_lines <- data_dgrps$dgrp
message(length(unique(data.dgrp_lines)), " DGRP lines found")
total_phenotype_number <- 0
total_phenotype_sex_specific <- 0
nb_issues_pheno <- c()
nb_obsolete_pheno <- c()
nb_issues <- c()
nb_obsolete <- c()
studies_status <- c()
no_phenotype_studies <- c()
# Reading each study
tmp <- data.frame(dgrp = data.dgrp_lines)
rownames(tmp) <- data.dgrp_lines
data.all_pheno <- list("F" = tmp, "M" = tmp, "NA" = tmp)
for(sid in json_studies$id) {
sub <- subset(json_studies, id == sid)
message("Study id = ", sid, "\ncreated at ", sub$created_at, ", modified at ", sub$updated_at)
download.file(url = paste0("https://dgrpool.epfl.ch/studies/",sub$id,".json"), destfile = "study.json", quiet = T)
json_study <- fromJSON(txt = "study.json")
tmp <- file.remove("study.json")
studies_status <- c(studies_status, json_study$status)
if(json_study$created_at != sub$created_at) stop("ERROR created at")
if(json_study$updated_at != sub$updated_at) stop("ERROR updated at")
message(json_study$first_author, " et al., ", json_study$title, ", ", json_study$journal_id, " ", json_study$volume, "(", json_study$issue, "), ", json_study$year)
message(length(json_study$phenotype_ids), " associated phenotypes")
dgrp_lines <- names(json_study$pheno_mean)
message(length(dgrp_lines), " DGRP lines")
data_pheno <- list() # List by sex
if(length(dgrp_lines) > 0){
# First, find all phenotypes across DGRP lines
list_pheno_all <- c()
for(dgrp in dgrp_lines){
data_dgrp <- json_study$pheno_mean[[dgrp]]
list_pheno_all <- unique(c(list_pheno_all, names(data_dgrp)[names(data_dgrp) != "sex"]))
}
if(length(list_pheno_all) >0){
# Now find values for each DGRP line
for(dgrp in dgrp_lines){
data_dgrp <- json_study$pheno_mean[[dgrp]]
#data_dgrp <- json_study$pheno_sum[[dgrp]]
list_pheno <- names(data_dgrp)[names(data_dgrp) != "sex"]
if(!all(list_pheno %in% list_pheno_all)) stop("ERROR PHENO")
data_dgrp$sex[is.na(data_dgrp$sex)] <- "NA" # For avoiding issues
for(s in data_dgrp$sex){
phe <- data_pheno[[s]]
if(is.null(phe)){
phe <- data.frame(matrix(nrow = length(dgrp_lines), ncol = length(list_pheno) + 1))
colnames(phe) <- c("dgrp", list_pheno)
phe$dgrp <- dgrp_lines
rownames(phe) <- dgrp_lines
}
for(p in list_pheno){
data.subpheno <- subset(json_phenotypes, id == p)
if(nrow(data.subpheno) == 0){
nb_issues_pheno <- c(nb_issues_pheno, p)
nb_issues <- c(nb_issues, paste0("S", sid, "_", p, "_", s))
} else {
if(data.subpheno$obsolete){
nb_obsolete_pheno <- c(nb_obsolete_pheno, p)
nb_obsolete <- c(nb_obsolete, paste0("S", sid, "_", p, "_", s))
} else {
phe[dgrp, p] <- data_dgrp[[p]][data_dgrp$sex == s]
}
}
}
data_pheno[[s]] <- phe
}
}
# Match our data
#dgrp_lines <- gsub(x = dgrp_lines, pattern = "DGRP", replacement = "line")
#dgrp_lines <- gsub(x = dgrp_lines, pattern = "line_0", replacement = "line_")
#dgrp_lines <- gsub(x = dgrp_lines, pattern = "line_0", replacement = "line_")
#common_lines <- intersect(data.dgrp_lines, dgrp_lines)
#if(length(common_lines) != length(dgrp_lines)){
# message("[WARNING] Only ", length(common_lines), " lines intersect with the reference ", length(data.dgrp_lines), " lines. ", length(dgrp_lines) - length(common_lines), " line phenotypes are ignored.")
#}
for(sex in names(data_pheno))
{
phenotypes <- data_pheno[[sex]]
#phenotypes$dgrp <- gsub(x = phenotypes$dgrp, pattern = "DGRP", replacement = "line")
rownames(phenotypes) <- phenotypes$dgrp
data.all_pheno[[sex]][phenotypes$dgrp,paste0("S", sid, "_", colnames(phenotypes)[colnames(phenotypes) != "dgrp"])] <- phenotypes[,colnames(phenotypes) != "dgrp"]
}
message("Available sex (",length(names(data_pheno)), "): [", paste(names(data_pheno), collapse = ", "), "]")
message("Available phenotypes (",ncol(data_pheno[[1]]) - 1, "): [", paste(colnames(data_pheno[[1]])[-1], collapse = ", "), "]\n")
total_phenotype_number <- total_phenotype_number + (ncol(data_pheno[[1]]) - 1)
total_phenotype_sex_specific <- total_phenotype_sex_specific + (ncol(data_pheno[[1]]) - 1) * length(names(data_pheno))
} else { no_phenotype_studies <<- c(no_phenotype_studies, sid); message("No phenotype available for this study\n") }
} else { no_phenotype_studies <<- c(no_phenotype_studies, sid); message("No DGRP available for this study\n") }
}
## DEBUG
nb_obsolete_pheno <- unique(nb_obsolete_pheno)
nb_obsolete <- unique(nb_obsolete)
nb_issues_pheno <- unique(nb_issues_pheno)
nb_issues <- unique(nb_issues)
message("Finished. Total number of phenotypes loaded: ", total_phenotype_number)
message("Finished. Total number of phenotypes OBSOLETE: ", length(nb_obsolete_pheno))
message("Finished. Total number of phenotypes WITH ISSUES: ", length(nb_issues_pheno))
## STATS
names(studies_status) <- json_studies$id
studies_status_tab <- as.data.frame(table(studies_status))
rownames(studies_status_tab) <- studies_status_tab$studies_status
phenotypes.all.sex_specific <- unique(c(paste0(colnames(data.all_pheno[["F"]])[2:ncol(data.all_pheno[["F"]])], "_F"), paste0(colnames(data.all_pheno[["M"]])[2:ncol(data.all_pheno[["M"]])], "_M"), paste0(colnames(data.all_pheno[["NA"]])[2:ncol(data.all_pheno[["NA"]])], "_NA")))
message("- ", length(unique(data.dgrp_lines)), " DGRP lines")
message("- ", nrow(json_studies), " studies")
message("-- ", studies_status_tab["Submitted", "Freq"], " submitted studies")
message("-- ", studies_status_tab["Under curation", "Freq"], " studies under curation")
message("-- ", studies_status_tab["Curated", "Freq"], " curated studies")
message("- ", nrow(json_phenotypes) - length(nb_obsolete_pheno) - length(nb_issues_pheno), " phenotypes (", length(phenotypes.all.sex_specific) - length(nb_obsolete) - length(nb_issues), " sex-specific phenotypes)")
message("- ", length(no_phenotype_studies), " studies WITHOUT phenotype data attached")
message("- ", nrow(json_studies) - length(no_phenotype_studies), " studies WITH phenotype data attached")
## Filter NA poop
for(sex in c("M", "F", "NA")){
to.keep <- apply(data.all_pheno[[sex]], 2, function(x) sum(is.na(x))) != nrow(data.all_pheno[[sex]])
data.all_pheno[[sex]] <- data.all_pheno[[sex]][,to.keep]
}
## Check again
phenotypes.all.sex_specific <- unique(c(paste0(colnames(data.all_pheno[["F"]])[2:ncol(data.all_pheno[["F"]])], "_F"), paste0(colnames(data.all_pheno[["M"]])[2:ncol(data.all_pheno[["M"]])], "_M"), paste0(colnames(data.all_pheno[["NA"]])[2:ncol(data.all_pheno[["NA"]])], "_NA")))
phenotypes.all <- unique(c(colnames(data.all_pheno[["F"]])[2:ncol(data.all_pheno[["F"]])], colnames(data.all_pheno[["M"]])[2:ncol(data.all_pheno[["M"]])], colnames(data.all_pheno[["NA"]])[2:ncol(data.all_pheno[["NA"]])]))
# Remove the first column, which is DGRP
message("All studies")
message(length(phenotypes.all), " phenotypes with associated data (", length(phenotypes.all.sex_specific ), " sex-specific phenotypes)")
message("- ", ncol(data.all_pheno[["M"]]) - 1, " phenotypes with male data") # Remove the first column, which is DGRP
message("- ", ncol(data.all_pheno[["F"]]) - 1, " phenotypes with female data") # Remove the first column, which is DGRP
message("- ", ncol(data.all_pheno[["NA"]]) - 1, " phenotypes with undefined sex data") # Remove the first column, which is DGRP
message("Curated studies")
curated_studies <- paste0("S", names(studies_status)[studies_status == "Curated"])
message(sum(limma::strsplit2(x = phenotypes.all, split = "_")[,1] %in% curated_studies), " phenotypes with associated data (", sum(limma::strsplit2(phenotypes.all.sex_specific, split = "_")[,1] %in% curated_studies), " sex-specific phenotypes)")
message("- ", sum(limma::strsplit2(x = colnames(data.all_pheno[["M"]]), split = "_")[,1] %in% curated_studies), " phenotypes with male data")
message("- ", sum(limma::strsplit2(x = colnames(data.all_pheno[["F"]]), split = "_")[,1] %in% curated_studies), " phenotypes with female data")
message("- ", sum(limma::strsplit2(x = colnames(data.all_pheno[["NA"]]), split = "_")[,1] %in% curated_studies), " phenotypes with undefined sex data")
## Save object
saveRDS(data.all_pheno, file = output_file)