phyloseq icon indicating copy to clipboard operation
phyloseq copied to clipboard

subset_samples() does not work if objects are not in the global env

Open AliciaMstt opened this issue 4 years ago • 2 comments

Hi,

This is a minor issue, but since it toke me a while to find what was going on I decided to let you know:

I created a simple function to remove reads from negative controls by pcr batch, which looks as follows:

remove_NC<-function(phyloseq_tables, batch_id, NC_name){ 
  ### This functions removes the reads from NC of all samples from a given PCR batch
  # returning a matrix of the otu table w/o the nc 
  
  ### arguments:
  ## phyloseq_tables: a phyloseq object result of importing a taxonomy.biom file
  ## batch_id: character vector with the name of the batch ID. Asumes it exists in a variable
  # "container_name" within the phyloseq tables
  ## NC_name: character vector with the name of the NC sample.
  
  ### Function:
  # subset pcr batch
  batch<- subset_samples(phyloseq_tables, container_name == batch_id)
  print("input data looks like:")
  print(batch)
  
  ## get otu matrix
  batch_OTU<-as.matrix(otu_table(batch))
  
  ## get NC vector
  NC<-as.vector(batch_OTU[,NC_name])
  
  ## Remove reads present in NC for each OTU
  batch_clean<-batch_OTU-NC
  
  ## Repleace negative numbers with 0 since we can't have negative reads
  batch_clean<-replace(batch_clean, batch_clean < 0, 0)
  
  ## tell the user what happened
  print(paste("NC sample", NC_name, "was used to filter",  sum(NC), "reads in total"))
  
  ## return results
  batch_clean
  
}

The code of the function ran well if all if its arguments were created as objects, but if I tried running it providing the arguments as part of the function, like this:

batch1_filtered<-remove_NC(phyloseq_tables=phyloseq_tables, batch_id = "Plate_1", NC_name = "NC16sI")

Then I got the error:

Error in eval(e, x, parent.frame()) : objet 'batch_id' not found

After testing different things we realized that the issue was caused by subset_samples() not working unless everything it needs could be found in the global env.

I solved it adding batch_id <<- batch_id before calling subset_samples().

This solved the issue foe me, but I'm letting you know in case you find it worth fixing within subset_samples().

Cheers,

Alicia

AliciaMstt avatar Jul 31 '20 01:07 AliciaMstt