jbisanz/qiime2R

`Error in file(con, 'r") : cannot open the connection` when user inputs incorrect filename into metadata parameter in `qza_to_phyloseq``

Closed this issue · 1 comments

Desired behavior would be to output a more useful error message.

Currently, the following code

library(qiime2R)
qza_to_phyloseq(features = "features.qza",
                tree = "tree.qza",
                taxonomy = "taxonomy.qza",
                metadata = "metadata.tsv")

produces the following error if file metadata.tsv doesn't exist:

Error in file(con, "r") : cannot open the connection

which is not very informative.
This is due to the fact that there is no check for existence of a file in the body of is_q2metadata function.

is_q2metadata <- function(file){
  suppressWarnings(
  if(grepl("^#q2:types", readLines(file)[2])){return(TRUE)}else{return(FALSE)}
  )
}

I propose the following, simple change to provide users with a more informative error message in such cases.

is_q2metadata <- function(file){

  if (!file.exists(file)){stop("Input metadata file (",file,") not found. Please check path and/or use list.files() to see files in current working directory.")}

  suppressWarnings(
  if(grepl("^#q2:types", readLines(file)[2])){return(TRUE)}else{return(FALSE)}
  )
}

I opened a pull request with proposed change #63