@@ -109,16 +109,14 @@ fetch_interpro <- function(uniprot_ids = NULL,
109
109
.f = ~ {
110
110
# Initialize the next page URL with the base query URL
111
111
next_url <- paste0(" https://www.ebi.ac.uk/interpro/api/" , .x , " &page_size=" , page_size )
112
- attempts <- 0
113
112
all_results <- list ()
114
113
115
114
# progress bar variable
116
115
total_pages <- 1
117
116
current_page <- 1
118
117
119
118
while (! is.null(next_url )) {
120
- result <- tryCatch(
121
- {
119
+
122
120
# Call the protti function to query the API
123
121
query <- protti ::: try_query(next_url ,
124
122
type = " application/json" ,
@@ -129,44 +127,29 @@ fetch_interpro <- function(uniprot_ids = NULL,
129
127
# Store the results from the current page
130
128
all_results [[current_page ]] <- query
131
129
130
+
132
131
# initialize progress bar
133
- if (show_progress == TRUE & current_page == 1 & length(query_url_part ) == 1 & ! is.null(query $ count )) {
132
+ if (show_progress == TRUE & current_page == 1 & length(query_url_part ) == 1 & ( ! is( query , " character " ) && ! is .null(query $ count ) )) {
134
133
pb <- progress :: progress_bar $ new(
135
134
total = ceiling(query $ count / page_size ),
136
135
format = " Fetching Pages [:bar] :current/:total (:percent) :eta"
137
136
)
138
137
}
139
138
140
139
# tick progress bar
141
- if (show_progress == TRUE & length(query_url_part ) == 1 & ! is.null(query $ count ) && ! pb $ finished ) {
140
+ if (show_progress == TRUE & length(query_url_part ) == 1 & ( ! is( query , " character " ) && ! is .null(query $ count ) ) && ! pb $ finished ) {
142
141
pb $ tick()
143
142
}
144
143
145
- # Get the next page URL from the "next" field
146
- next_url <- query $ `next`
144
+ if (! is(query , " character" )) {
145
+ # Get the next page URL from the "next" field
146
+ next_url <- query $ `next`
147
+ } else {
148
+ next_url <- NULL
149
+ }
147
150
148
151
# add to page counter
149
152
current_page <- current_page + 1
150
-
151
- attempts <- 0 # Reset attempts counter on successful request
152
-
153
- NULL
154
- },
155
- error = function (e ) {
156
- if (grepl(" 408" , e $ message )) {
157
- Sys.sleep(2 ) # Sleep and retry for timeout errors (408)
158
- NULL
159
- } else {
160
- attempts <<- attempts + 1
161
- if (attempts < max_tries ) {
162
- Sys.sleep(2 ) # Sleep and retry up to "max_tries" times
163
- NULL
164
- } else {
165
- stop(paste(" Error retrieving data for URL:" , next_url , " - " , e $ message ))
166
- }
167
- }
168
- }
169
- )
170
153
}
171
154
all_results
172
155
},
@@ -175,10 +158,31 @@ fetch_interpro <- function(uniprot_ids = NULL,
175
158
}
176
159
177
160
if (! missing(manual_query )) {
178
- # return raw data for manual queries
179
- return (query_result )
161
+ # Check if any element is of type character to report issues with the fetching to the user.
162
+ if (any(purrr :: map_lgl(query_result [[1 ]], is.character ))) {
163
+
164
+ issue <- purrr :: keep(query_result [[1 ]], is.character )[[1 ]]
165
+
166
+ message(" \n There was an issue fetching from InterPro:\n " , issue )
167
+ return (NULL )
168
+ } else {
169
+ # return raw data for manual queries
170
+ return (query_result )
171
+ }
172
+ }
173
+
174
+ # If any element is character stop the function and report the issue
175
+ if (exists(" query_result" ) && any(purrr :: map_lgl(query_result , .f = ~ {purrr :: map_lgl(.x , is.character )}))) {
176
+
177
+ problematic_ids <- uniprot_ids [purrr :: map_lgl(query_result , .f = ~ {any(purrr :: map_lgl(.x , is.character ))})]
178
+ issue <- purrr :: keep(query_result , .p = ~ {purrr :: map_lgl(.x , is.character )}) %> %
179
+ unlist()
180
+
181
+ message(" \n There was an issue fetching from InterPro:\n " , paste0(problematic_ids , " : " , issue , " \n " ))
182
+ return (NULL )
180
183
}
181
184
185
+
182
186
# if residue level information should be returned, fetch it here
183
187
if (return_residue_info ) {
184
188
query_residue <- purrr :: map_dfr(
@@ -193,6 +197,10 @@ fetch_interpro <- function(uniprot_ids = NULL,
193
197
timeout = timeout
194
198
)
195
199
200
+ if (is(query , " character" )){
201
+ result <- data.frame (accession = .x ,
202
+ issue = query )
203
+ } else {
196
204
if (length(query ) == 0 ) {
197
205
result <- data.frame (accession = .x )
198
206
} else {
@@ -231,12 +239,21 @@ fetch_interpro <- function(uniprot_ids = NULL,
231
239
) %> %
232
240
mutate(accession = .x )
233
241
}
242
+ }
234
243
235
244
result
236
245
},
237
246
.progress = progress_option_residue
238
247
)
239
248
249
+ if (" issue" %in% colnames(query_residue )){
250
+ problematic_ids <- query_residue %> %
251
+ dplyr :: filter(! is.na(issue ))
252
+
253
+ message(" \n There was an issue fetching from InterPro:\n " , paste0(problematic_ids $ accession , " : " , problematic_ids $ issue , " \n " ))
254
+ return (NULL )
255
+ }
256
+
240
257
return (query_residue )
241
258
}
242
259
0 commit comments