Search code examples
rqiime

How to extract the same data from different files


In order to extract a frecuency table from a qiime2 artifact and write It to a tsv output you have to:

rel_freq_table <- read_qza("05.TaxonomyClassification/rel_freq/1_rel_freq_table.qza")
rel_freq_table <- data.frame(rel_freq_table$data)
rel_freq_table <-  setDT(rel_freq_table, keep.rownames = TRUE )
colnames(rel_freq_table)[1]<- c( "Feature.ID" )
write.table(rel_freq_table, file = "1_rel_freq_table.tsv", row.names = FALSE, dec = ",")

Now, I have seven qiime2 artifacts

rel_freqs <- c("05.TaxonomyClassification/rel_freq/1_rel_freq_table.qza","05.TaxonomyClassification/rel_freq/2_rel_freq_table.qza",
           "05.TaxonomyClassification/rel_freq/3_rel_freq_table.qza","05.TaxonomyClassification/rel_freq/4_rel_freq_table.qza",
           "05.TaxonomyClassification/rel_freq/5_rel_freq_table.qza","05.TaxonomyClassification/rel_freq/6_rel_freq_table.qza",
           "05.TaxonomyClassification/rel_freq/7_rel_freq_table.qza")

I would like to automatize the process so I only have to run the command once. I have tried with loops and lapply but no luck, does anybody has any idea?


Solution

  • You can write a function and apply it to each file using lapply :

    read_qiime2_write_tsv <- function(filename) {
      rel_freq_table <- read_qza(filename)
      rel_freq_table <- data.frame(rel_freq_table$data)
      rel_freq_table <-  setDT(rel_freq_table, keep.rownames = TRUE )
      colnames(rel_freq_table)[1]<- c( "Feature.ID" )
      write.table(rel_freq_table, 
              file = paste0(tools::file_path_sans_ext(basename(filename)), '.tsv'), 
              row.names = FALSE, dec = ",")
    }
    
    lapply(rel_freqs, read_qiime2_write_tsv)