jbisanz / qiime2R

Import qiime2 artifacts to R
MIT License
156 stars 53 forks source link

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

Closed jakubguzek closed 8 months ago

jakubguzek commented 8 months ago

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)}
  )
}
jakubguzek commented 8 months ago

I opened a pull request with proposed change #63