I don't think your problem actually has to do with the header, I think the issue may lie in the fact that your sample names are only numbers. In my opinion nothing good can come from using numbers as sample IDs, I would recommend using something containing a more descript alpha-numeric unique identifier. It would appear based on my playing around that phyloseq does not take numbers as sample names so this is where your issue is. I would recommend reading your objects in separately, appending and appending something on like below:
#not actually tested, may need to fix a typo or two
table<-read_qza("table.qza")$data
meta<-read_q2metadata("met.tsv")
meta$SampleID<-paste0("Sample_", meta$SampleID)
colnames(table)<-paste0("Sample_", colnames(table))
#Then build your phyloseq object manually
Thank you for your help Jordan! When I try to build the phyloseq object manually, I get the following error after typing the command:
colnames(tax_table)<-c(“Kingdom”,“Phylum”,“Class”,“Order”,“Family”,“Genus”,“Species”)
Error in dimnames(x) <- dn :
length of ‘dimnames’ [2] not equal to array extent
I suspect this is because some of the entries in my tax_table go to only the genus level and some are unassigned. Anything I can do to make the above command work?
That sounds about correct, you could double check with length(colnames(tax_table)) or dim(tax_table). In this case, just remove the ,"Species" from your first line and the problem should be solved.
I have a similar error when I tried to read in my alpha diversity artifacts.
Could you please let me know how you resolved it?
shannon <- read_qza("shannon_vector.qza")
Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, :
line 387 did not have 2 elements
Abby, I think your problem is quite different, my best guess is that it has something to do with odd characters in your samples names, or if your sample names are numbers. Can you create a copy of your artifact, change the name to shannon_vector.zip, unzip it, and then show me the top few lines of yourartifact/data/alpha-diversity.tsv?
hmm, nothing looks too strange, looking closer at the error, the issue is on line 387, is this the end of your alpha-diversity.tsv file? What are the sample names around this line?
shannon <- read_qza("shannon_vector.qza")
Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, :
line 8 did not have 2 elements
Hi,
I am facing a similar problem when importing my metadata. I've tried all the suggestions but still didn't find a solution for the problem.
metadata<-read_q2metadata("metadata4b.tsv")
Error in read_q2metadata("metadata4b.tsv") :
Metadata does not define types (ie second line does not start with #q2:types)
I have a metadata table with 74 samples. The first row are the identifiers of each column.
When I import using this command: metadata<-read.table("metadata4b.txt"), it works but it says that I have 75 observations instead of 74.
Then when I go on to generate some graphics I always get errors messages which I believe is because of this.
For example, when I try to generate barplots using this command:
metadata %>%
filter(!is.na(shannon)) %>%
ggplot(aes(x=season, y=shannon, fill=season)) +
stat_summary(geom="bar", fun.data=mean_se, color="black") + #here black is the outline for the bars
geom_jitter(shape=21, width=0.2, height=0) +
coord_cartesian(ylim=c(2,7)) + # adjust y-axis
facet_grid(~species) + # create a panel for each body site
xlab("Antibiotic Usage") +
ylab("Shannon Diversity") +
theme_q2r() +
scale_fill_manual(values=c("cornflowerblue","indianred","chocolate")) + #specify custom colors
theme(legend.position="none") #remove the legend as it isn't needed
ggsave("../../../images/Shannon_by_abx.pdf", height=3, width=4, device="pdf") # save a PDF 3 inches by 4 inches
I get this error:
Error: Problem with filter() input ..1.
x Input ..1 must be of size 75 or 1, not size 74.
i Input ..1 is !is.na(shannon).
Run rlang::last_error() to see where the error occurred.
To be more clear on the conceptual side, read_q2metadata() is specifically designed for the q2 metadata format wherein the second line contains the data types (which starts with #q2:types. The error message you were both receiving is the result of the second line of your metadata not containing that definition line, and thus, not being appropriate for loading with read_q2metadata(). As such you can use any function to read your metadata (of which read_tsv is my favourite!).
If you try to read a q2 metadata file using read_tsv() you will see that it imports the definition line as its own sample with the id #q2:types which is probably not desirable in many cases.
Note @Danilo_Reis, in your example of using read.table() as opposed to read_tsv() you need to be sure to indicate that your columns have names as below. I would also specify that it is tab (\t) separated just to be safe:
Hi @jbisanz ,
Thanks for your reply! It finally worked!
But I still get some error messages when I try to generate the graph you show in your tutorial.
I am using this command to generate bar plots (shannon diversity in three seasons - spring, summer, autumn grouped by each plant species (A,B,C and D).
Do you have any idea what can be the reason?
metadata %>%
filter(!is.na(shannon)) %>%
ggplot(aes(x=season, y=shannon, fill=season)) +
stat_summary(geom=“bar”, fun.data=mean_se, color=“black”) + #here black is the outline for the bars
geom_jitter(shape=21, width=0.2, height=0) +
coord_cartesian(ylim=c(2,7)) + # adjust y-axis
facet_grid(~species) + # create a panel for each body site
xlab(“Seasons”) +
ylab(“Shannon Diversity”) +
theme_q2r() +
scale_fill_manual(values=c(“cornflowerblue”,“indianred”,“chocolate”)) + #specify custom colors
theme(legend.position=“none”) #remove the legend as it isn’t needed
ggsave("…/…/…/images/Shannon_by_abx.pdf", height=3, width=4, device=“pdf”) # save a PDF 3 inches by 4 inches
Don’t know how to automatically pick scale for object of type data.frame. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (74): y
Hi @jbisanz ,
Now it worked! I don’t know why but it generated a “shannon” column in the medatada but with a different name (shannon_entropy). That`s the reason why the last command was not working.