Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error : invalid class “phyloseq” object: Component taxa/OTU names do not match. #1752

Open
Saesha5 opened this issue May 24, 2024 · 0 comments

Comments

@Saesha5
Copy link

Saesha5 commented May 24, 2024

Load necessary libraries

library(phyloseq)

metadata <- "C:/Users/Saesha Verma/OneDrive/Desktop/Analysis/Metadata_table"
asv <- "C:/Users/Saesha Verma/OneDrive/Desktop/Analysis/ASV_table"
taxa <- "C:/Users/Saesha Verma/OneDrive/Desktop/Analysis/Taxa_table"

Create empty lists to store data

metadata_list <- list()
asv_list <- list()
taxa_list <- list()

Loop through files in each directory

for (i in 1:56) {

Construct file paths

metadata_file <- paste0(metadata, "/Study", i, ".csv")
asv_file <- paste0(asv, "/ASVtable.SILVA.16S_", i, ".csv")
taxa_file <- paste0(taxa, "/Taxa_data_", i, ".csv")

Read files

metadata_list[[i]] <- read.csv(metadata_file, header = TRUE, sep = ",")
asv_list[[i]] <- read.csv(asv_file, header = TRUE, sep = ",")
taxa_list[[i]] <- read.csv(taxa_file, header = TRUE, sep = ",")

Convert ASV data to numeric, excluding the first column if it's a row name or identifier

asv_list[[i]][, -1] <- lapply(asv_list[[i]][, -1], as.numeric)
}

Convert specific columns to numeric in metadata (if necessary)

for (i in 1:56) {
if (!is.numeric(metadata_list[[i]][, 1])) {
metadata_list[[i]][, 1] <- as.numeric(as.character(metadata_list[[i]][, 1]))
}
}

Create empty list to store phyloseq objects

physeq_list <- list()

Loop through data frames and create phyloseq objects

for (i in 1:56) {

Ensure the OTU table is a numeric matrix

asv_matrix <- as.matrix(asv_list[[i]][, -1])
rownames(asv_matrix) <- asv_list[[i]][, 1] # Assuming the first column is the row names/identifiers
head(taxa_names(asv_matrix))

Ensure the taxonomy table is a matrix and the row names match the OTU table

taxa_matrix <- as.matrix(taxa_list[[i]])
rownames(taxa_matrix) <- taxa_list[[i]][, 1] # Assuming the first column is the row names/identifiers

Create phyloseq object

phyloseq_1 <- phyloseq(
otu_table(asv_matrix, taxa_are_rows = TRUE),
sample_data(metadata_list[[i]]),
tax_table(as.matrix(taxa_list[[i]]))
)

Assign the phyloseq object to the list

phyloseq_list[[i]] <- phyloseq_1
}

Name each phyloseq object in the list

names(physeq_list) <- paste0("physeq_", 1:56)

On running Loop through data frames to create phyloseq objects, I encounter following error
Error in validObject(.Object) : invalid class “phyloseq” object:
Component taxa/OTU names do not match.
Taxa indices are critical to analysis.
Try taxa_names()

Need to create a merged phyloseq object for 56 studies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant