vmikk / metagMisc

Miscellaneous functions for metagenomic analysis.
MIT License
46 stars 11 forks source link

phyloseq_to_df error: Sample names #18

Closed otaviolovison closed 2 years ago

otaviolovison commented 3 years ago

Hello!

I had performed several analysis using phyloseq_to_df without problem, but now I am having an error: "Error: Sample names in 'physeq' could not be automatically converted to the syntactically valid column names in data.frame (see 'make.names'). Consider renaming with 'sample_names'.

The sample names appear to have no ivalid character (they are just numbers).

sample_names(ps) [1] "1" "10" "11" "13" "14" "15" "2" "20" "27" "28" "3" "5" "7" "8" "9"

Any suggestions? Thank you so much!

vmikk commented 3 years ago

Hello Otávio!

Unfortunately, R does not allow column names to start with numbers. For example, if you'll try to load a data with read.table, by default R will add "X" before the number in column names.

As a workaround, you may rename the samples in phyloseq object, convert to data.frame, and rename samples back:

sample_names(ps) <- paste0("z_", sample_names(ps))
psd <- phyloseq_to_df(ps)
colnames(psd) <- gsub(pattern = "^z_", replacement = "", x = colnames(psd))

With kind regards, Vladimir

otaviolovison commented 3 years ago

It worked perfectly!

Thank you so much!

tboonf commented 2 years ago

Hi @vmikk - Received same error message when trying to use the same function. Apparently, it is because there is special characters (hyphen) - in the sample ID (too many samples to change individually). Any work around that you might be able to suggest ?

vmikk commented 2 years ago

Hello @tboonf! Have you tried the same trick with temporary sample renaming? E.g., you may replace a hyphen with double underscore (or something else that is not present in your sample names):

sample_names(ps) <- gsub(pattern = "-", replacement = "__", x = sample_names(ps))
psd <- phyloseq_to_df(ps)
colnames(psd) <- gsub(pattern = "__", replacement = "-", x = colnames(psd))

HTH, Vladimir