-1

I've written a very quick blast script in r to enable interfacing with the NCBI blast API. Sometimes however, the result url takes a while to load and my script throws an error until the url is ready. Is there an elegant way (i.e. a tryCatch option) to handle the error until the result is returned or timeout after a specified time?

  library(rvest)
  ## Definitive set of blast API instructions can be found here: https://www.ncbi.nlm.nih.gov/staff/tao/URLAPI/new/BLAST_URLAPI.html
  ## Generate query URL
  query_url <-
    function(QUERY,
             PROGRAM = "blastp", 
             DATABASE = "nr",
             ...) {
      put_url_stem <-
        'https://www.ncbi.nlm.nih.gov/blast/Blast.cgi?CMD=Put'
      arguments = list(...)
      paste0(
        put_url_stem,
        "&QUERY=",
        QUERY,
        "&PROGRAM=",
        PROGRAM,
        "&DATABASE=",
        DATABASE,
        arguments
      )
    }

  blast_url <- query_url(QUERY = "NP_001117.2")      ## test query
  blast_session <- html_session(blast_url)               ## create session
  blast_form <- html_form(blast_session)[[1]]         ## pull form from session
  RID <- blast_form$fields$RID$value      ## extract RID identifier

  get_url <- function(RID, ...) {
    get_url_stem <-
      "https://www.ncbi.nlm.nih.gov/blast/Blast.cgi?CMD=Get"
    arguments = list(...)
    paste0(get_url_stem, "&RID=", RID, "&FORMAT_TYPE=XML", arguments)
  }
  hits_xml <- read_xml(get_url(RID))    ## this is the sticky part 

Sometimes it takes several minutes for the get_url to go live so what I would like is to do is to keep trying let's say every 20-30 seconds until it either produces the url or times out after a pre-specified time.

biomiha
  • 1,358
  • 2
  • 12
  • 25

1 Answers1

1

I think you may find this answer about the use of tryCatch useful

Regarding the 'keep trying until timeout' part. I imagine you can work on top of this other answer about a tryCatch loop on error

Hope it helps.