Import MetaPhlAn3 to Phyloseq
0
0
Entering edit mode
Saraswati • 0
@dbb56555
Last seen 5 weeks ago
India

Hello Sir I am Saraswati and I am working on whole shotgun metagenomics datasets, for that purpose I profiled the taxonomy using the MetaPhlAn3 software but now I want to use phyloseq package to generate more figures. I saw your code on GitHub to import the metaphlan3 files to phyloseq but I am facing few problems. I am pasting my code here. My main problem is that I need details of importing process as I do not have very good hands-on R programming.

Code should be placed in three backticks as shown below

> library("phyloseq")
> library(readxl)
> library("ggplot2")
> library(curatedMetagenomicData)
> theme_set(theme_bw())
> otumat=read.csv("~/Documents/FLOOR-METAGENOME-NOVASEQ/METAPHLAN-RES/MetaPhlAn3-Final_Res/A1_R12_profile_(copy).txt", sep="\t", strip.white = T, stringsAsFactors = F, row.names=1)
> metaphlanToPhyloseq <- function(
+   metaphlandir=("~/Documents/FLOOR-METAGENOME-NOVASEQ/METAPHLAN-RES/MetaPhlAn3-Final_Res/A1_R12_profile_(copy).txt"),
+   metadat=NULL,
+   simplify=TRUE){
+   ## tax is a matrix or data.frame with the table of taxonomic abundances, rows are taxa, columns are samples
+   ## metadat is an optional data.frame of specimen metadata, rows are samples, columns are variables
+   ## if simplify=TRUE, use only the most detailed level of taxa names in the final object
+   ## metaphlanToPhyloseq("~/Downloads/metaphlan_bugs_list")
+   .getMetaphlanTree <- function(removeGCF=TRUE, simplify=TRUE){
+     if (!requireNamespace("ape")) {
+       stop("Please install the ape package to read Newick trees")
+     }
+     nwkfile <- bzfile(system.file("~/Documents/FLOOR-METAGENOME-NOVASEQ/SUMMARY/mpa_v30_CHOCOPhlAn_201901_species_tree.nwk",
+                                   package="curatedMetagenomicData"))
+    tree <- ape::read.tree(nwkfile)
+     close(nwkfile)
+     if(removeGCF)
+       tree$tip.label <- sub("\|GCF_[0-9]+$", "", tree$tip.label)
Error: '\|' is an unrecognized escape in character string starting ""\|"
>     if(simplify)
+       tree$tip.label <- gsub(".+\|", "", tree$tip.label)
Error: '\|' is an unrecognized escape in character string starting "".+\|"
>     return(tree)
Error: object 'tree' not found
>   }
Error: unexpected '}' in "  }"
>   .joinListOfMatrices <- function(obj) {
+     rnames <- Reduce(union, lapply(obj, rownames))
+     cnames <- names(obj)
+     if (!all(isUnique(cnames))) {
+       stop("Column names are not unique.")
+     }
+     output <- matrix(0,
+                      nrow = length(rnames),
+                      ncol = length(cnames),
+                      dimnames = list(rnames, cnames)
+     )
+     for (i in seq_along(obj)) {
+       output[match(rownames(obj[[i]]), rownames(output)), i] <- obj[[i]][, 1]
+     }
+     return(output)
+   }
>   fnames <- list.files(metaphlandir)
Error in list.files(metaphlandir) : object 'metaphlandir' not found
>   bug.l <- lapply(fnames, function(x){
+     res <- read.delim(file.path(metaphlandir, x), stringsAsFactors = FALSE, row.names = 1)
+     colnames(res) <- x
+     return(res)
+   })
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'X' in selecting a method for function 'lapply': object 'fnames' not found
>   names(bug.l) <- fnames
Error: object 'fnames' not found
>   tax <- .joinListOfMatrices(bug.l)
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'x' in selecting a method for function 'Reduce': error in evaluating the argument 'X' in selecting a method for function 'lapply': object 'bug.l' not found
Called from: h(simpleError(msg, call))
Browse[1]>   xnames = rownames(tax)
Error during wrapup: object 'tax' not found
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Browse[1]>   shortnames = gsub(".+\|", "", xnames)
Error during wrapup: '\|' is an unrecognized escape in character string starting "".+\|"
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Browse[1]>   if(simplify){
+     rownames(tax) = shortnames
+   }
Error during wrapup: object 'simplify' not found
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Browse[1]>   x2 = strsplit(xnames, split="|", fixed=TRUE)
Error during wrapup: object 'xnames' not found
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Browse[1]>   taxmat = matrix(NA, ncol=max(sapply(x2, length)), nrow=length(x2))
Error during wrapup: object 'x2' not found
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Browse[1]>   colnames(taxmat) = c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species", "Strain")[1:ncol(taxmat)]
Error during wrapup: object 'taxmat' not found
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Browse[1]>   rownames(taxmat) = rownames(tax)
Error during wrapup: object 'tax' not found
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Browse[1]>   for (i in 1:nrow(taxmat)){
+     taxmat[i, 1:length(x2[[i]])] <- x2[[i]]
+   }
Error during wrapup: object 'taxmat' not found
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Browse[1]>   taxmat = gsub("[a-z]__", "", taxmat)
Error during wrapup: object 'taxmat' not found
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Browse[1]>   taxmat = phyloseq::tax_table(taxmat)
Error during wrapup: error in evaluating the argument 'object' in selecting a method for function 'tax_table': object 'taxmat' not found
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Browse[1]>   otutab = phyloseq::otu_table(tax, taxa_are_rows=TRUE)
Error during wrapup: error in evaluating the argument 'object' in selecting a method for function 'otu_table': object 'tax' not found
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Browse[1]>   tree <- .getMetaphlanTree(simplify=simplify)
Error during wrapup: could not find function ".getMetaphlanTree"
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Browse[1]>   if(is.null(metadat)){
+     metadat <- data.frame(file=fnames, row.names=fnames, stringsAsFactors = FALSE)
+   }
Error during wrapup: object 'metadat' not found
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Browse[1]>   res = phyloseq::phyloseq(taxmat, otutab, phyloseq::sample_data(metadat), tree)
Error during wrapup: object 'taxmat' not found
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Browse[1]>   return(res)
Error during wrapup: object 'res' not found
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Browse[1]> }
Error during wrapup: unexpected '}' in "}"
Error: no more error handlers available (recursive errors?); invoking 'abort' restart


sessionInfo( )
Metaphlan3 Metagenomics phyloseq • 114 views
ADD COMMENT
0
Entering edit mode

Hi Saraswati, thanks for your interest in that function. Just to clarify for others, it's not part of the curatedMetagenomicData package or Bioconductor, but it looks similar to something I wrote for a workshop some years ago (here, although it looks like you're using something adapted by someone else from here) and haven't updated for the new taxonomic labeling format of MetaPhlAn3. I'm not sure why you have syntax errors just defining the function - it looks like something happened in the copy-pasting, but that would be hard to troubleshoot from the output with all the "+" signs, browse functions etc. I'm not a maintainer or MetaPhlAn or phyloseq so this is outside of my current priorities, but I'll make a new function when I can and put it in an installable package to save you the copy-pasting. This might take some weeks though, so if you're in a hurry you might want to just work with the phyloseq package's constructor function directly.

ADD REPLY

Login before adding your answer.

Traffic: 473 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6