Skip to content

Commit 050a87b

Browse files
WT: Added error handling for when POST requests fail in functional module discovery
1 parent 4fb56c3 commit 050a87b

1 file changed

Lines changed: 77 additions & 47 deletions

File tree

R/downstream_analysis.R

Lines changed: 77 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -191,58 +191,88 @@ RunFMD_RNA <- function(gene_list, network = "global", log_flag = FALSE) {
191191
q = concatenated_gene_list
192192
)
193193
# Submit POST request to gene_check_url with our list of genes
194-
gene_check_post_request = httr::POST(gene_check_url, body = gene_check_payload, encode = "form", httr::verbose())
195-
gene_check_post_response <- jsonlite::fromJSON(httr::content(gene_check_post_request, as = "text"))
196-
# We can parse the response to grab our final list of entrez IDs that we will use for our FMD job
197-
final_gene_list <- c()
198-
final_gene_list_entrez <- c()
199-
for(i in 1:nrow(gene_check_post_response)) {
200-
current_gene_row <- gene_check_post_response[i,]
201-
if(current_gene_row$num_matches > 0) {
202-
final_gene_list <- c(final_gene_list, current_gene_row$query)
203-
current_entrez_id <- current_gene_row$matches
204-
final_gene_list_entrez <- c(final_gene_list_entrez, current_gene_row$matches[[1]][1,]$entrez)
194+
gene_check_post_request <- tryCatch(
195+
{
196+
print_SPEEDI("Submitting POST request to get final list of entrez IDs in HumanBase", log_flag)
197+
httr::POST(gene_check_url, body = gene_check_payload, encode = "form", httr::verbose())
198+
},
199+
error=function(cond) {
200+
print_SPEEDI("Error submitting POST request. Cancelling functional module discovery job.", log_flag)
201+
print_SPEEDI(cond, log_flag)
202+
return("ERROR")
205203
}
206-
}
207-
final_gene_list_entrez <- as.character(final_gene_list_entrez)
208-
print_SPEEDI(paste0("Length of final gene list for HumanBase: ", length(final_gene_list)), log_flag)
209-
print_SPEEDI(paste0("Final gene list: ", paste(final_gene_list, collapse = ' ')), log_flag)
210-
211-
if(length(final_gene_list) < 20) {
212-
print_SPEEDI("Fewer than 20 input genes were present in HumanBase, so we cannot run FMD", log_flag)
213-
gc()
214-
return(NULL)
215-
}
216-
217-
# Each FMD job has an associated cache key (generated via SHA1) in the URL so we can refer to the results later
218-
# This cache key is based on the sorted gene list (entrez) and the chosen network
219-
# We generate the cache key below using the same algorithm as HumanBase (that way, you'll get the
220-
# same cache key regardless of whether you use the UI or this script)
221-
final_gene_list_entrez <- sort(final_gene_list_entrez)
222-
fmd_payload <- list(
223-
entrez = final_gene_list_entrez
224204
)
225-
fmd_hash <- as.character(jsonlite::toJSON(fmd_payload))
226-
fmd_hash <- paste(substr(fmd_hash, 1, 1), " ", substr(fmd_hash, 2, nchar(fmd_hash)), sep = "")
227-
fmd_hash <- paste(substr(fmd_hash, 1, 11), " ", substr(fmd_hash, 12, nchar(fmd_hash)), sep = "")
228-
fmd_hash <- paste(substr(fmd_hash, 1, nchar(fmd_hash) - 1), " ", substr(fmd_hash, nchar(fmd_hash), nchar(fmd_hash)), sep = "")
229-
fmd_hash <- paste0(fmd_hash, network)
230-
fmd_hash <- digest::digest(fmd_hash, algo = "sha1", serialize = F)
231-
# Create final FMD submission URL (based on network and hash)
232-
current_fmd_url <- paste0(fmd_submission_url, network, "&body_tag=", fmd_hash)
233-
# Submit FMD POST request (payload is our sorted list of entrez IDs)
234-
fmd_submission_post_request <- httr::POST(current_fmd_url, body = fmd_payload, encode = "json", httr::verbose())
235-
fmd_submission_post_response <- jsonlite::fromJSON(httr::content(fmd_submission_post_request, as = "text"))
236-
if(length(fmd_submission_post_response$enrichment) == 0) {
237-
print_SPEEDI("No enrichment found for input genes", log_flag)
205+
if(gene_check_post_request == "ERROR") {
238206
gc()
239207
return(NULL)
208+
} else {
209+
gene_check_post_response <- jsonlite::fromJSON(httr::content(gene_check_post_request, as = "text"))
210+
# We can parse the response to grab our final list of entrez IDs that we will use for our FMD job
211+
final_gene_list <- c()
212+
final_gene_list_entrez <- c()
213+
for(i in 1:nrow(gene_check_post_response)) {
214+
current_gene_row <- gene_check_post_response[i,]
215+
if(current_gene_row$num_matches > 0) {
216+
final_gene_list <- c(final_gene_list, current_gene_row$query)
217+
current_entrez_id <- current_gene_row$matches
218+
final_gene_list_entrez <- c(final_gene_list_entrez, current_gene_row$matches[[1]][1,]$entrez)
219+
}
220+
}
221+
final_gene_list_entrez <- as.character(final_gene_list_entrez)
222+
print_SPEEDI(paste0("Length of final gene list for HumanBase: ", length(final_gene_list)), log_flag)
223+
print_SPEEDI(paste0("Final gene list: ", paste(final_gene_list, collapse = ' ')), log_flag)
224+
225+
if(length(final_gene_list) < 20) {
226+
print_SPEEDI("Fewer than 20 input genes were present in HumanBase, so we cannot run FMD", log_flag)
227+
gc()
228+
return(NULL)
229+
}
230+
231+
# Each FMD job has an associated cache key (generated via SHA1) in the URL so we can refer to the results later
232+
# This cache key is based on the sorted gene list (entrez) and the chosen network
233+
# We generate the cache key below using the same algorithm as HumanBase (that way, you'll get the
234+
# same cache key regardless of whether you use the UI or this script)
235+
final_gene_list_entrez <- sort(final_gene_list_entrez)
236+
fmd_payload <- list(
237+
entrez = final_gene_list_entrez
238+
)
239+
fmd_hash <- as.character(jsonlite::toJSON(fmd_payload))
240+
fmd_hash <- paste(substr(fmd_hash, 1, 1), " ", substr(fmd_hash, 2, nchar(fmd_hash)), sep = "")
241+
fmd_hash <- paste(substr(fmd_hash, 1, 11), " ", substr(fmd_hash, 12, nchar(fmd_hash)), sep = "")
242+
fmd_hash <- paste(substr(fmd_hash, 1, nchar(fmd_hash) - 1), " ", substr(fmd_hash, nchar(fmd_hash), nchar(fmd_hash)), sep = "")
243+
fmd_hash <- paste0(fmd_hash, network)
244+
fmd_hash <- digest::digest(fmd_hash, algo = "sha1", serialize = F)
245+
# Create final FMD submission URL (based on network and hash)
246+
current_fmd_url <- paste0(fmd_submission_url, network, "&body_tag=", fmd_hash)
247+
# Submit FMD POST request (payload is our sorted list of entrez IDs)
248+
fmd_submission_post_request <- tryCatch(
249+
{
250+
print_SPEEDI("Submitting POST request to submit functional module discovery job", log_flag)
251+
httr::POST(current_fmd_url, body = fmd_payload, encode = "json", httr::verbose())
252+
},
253+
error=function(cond) {
254+
print_SPEEDI("Error submitting POST request. Cancelling functional module discovery job.", log_flag)
255+
print_SPEEDI(cond, log_flag)
256+
return("ERROR")
257+
}
258+
)
259+
if(fmd_submission_post_request == "ERROR") {
260+
gc()
261+
return(NULL)
262+
} else {
263+
fmd_submission_post_response <- jsonlite::fromJSON(httr::content(fmd_submission_post_request, as = "text"))
264+
if(length(fmd_submission_post_response$enrichment) == 0) {
265+
print_SPEEDI("No enrichment found for input genes", log_flag)
266+
gc()
267+
return(NULL)
268+
}
269+
cached_url = paste0("https://hb.flatironinstitute.org/module/overview/?body_tag=", fmd_hash)
270+
print_SPEEDI(paste0("Done submitting FMD job! Associated URL is: ", cached_url), log_flag)
271+
print_SPEEDI("Functional module discovery analysis complete", log_flag)
272+
gc()
273+
return(list(cached_url, fmd_submission_post_response))
274+
}
240275
}
241-
cached_url = paste0("https://hb.flatironinstitute.org/module/overview/?body_tag=", fmd_hash)
242-
print_SPEEDI(paste0("Done submitting FMD job! Associated URL is: ", cached_url), log_flag)
243-
print_SPEEDI("Functional module discovery analysis complete", log_flag)
244-
gc()
245-
return(list(cached_url, fmd_submission_post_response))
246276
}
247277

248278
#' Grab associated HumanBase networks for current cell type and reference tissue

0 commit comments

Comments
 (0)