qiime2 distance matrix as input for R

Hello,

I'm trying to analyse skin Microbiota from human samples. (V3-V4 Illumina Miseq.) I finished the moving pictures tutorial up to the point of creating the distance matrices, everything looks neat.

Now I'd like to take these results into R to create PCoA Plots.

1.) I exported the Bray-Curtis-Distance-Matrix from qiime2 via this command:

qiime tools export --input-path bray_curtis_distance_matrix.qza --output-path bray-curtis-for-R

This gives me a .tsv file which looks like this:

So far, so good.

2.) Now I'd like to feed this table as a distance matrix into R.

library("readr")
dist_bray <- read_tsv('distance-matrix-for-R.tsv')
print(dist_bray)
> print(dist_bray)
# A tibble: 130 × 131
   ...1      1-12-…¹ 1-12-…² 1-12-…³ 1-8-3…⁴ 1-8-3…⁵ 1-8-3…⁶ 2-21-…⁷ 2-21-…⁸ 2-21-…⁹ 3-15-…˟ 3-15-…˟
   <chr>       <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>

Whenever I try to feed this into as.dist-Function (be it as a df or a tibble) I get the following error message:

> imported_dist_matrix<-as.dist(dist_bray_df)
Warning messages:
1: In storage.mode(m) <- "numeric" : NAs introduced by coercion
2: In as.dist.default(dist_bray_df) : non-square matrix
> imported_dist_matrix<-as.dist(dist_bray)
Warning messages:
1: In storage.mode(m) <- "numeric" : NAs introduced by coercion
2: In as.dist.default(dist_bray) : non-square matrix

Why does my qiime2-Distance matrix turn non-square as indicated by the "130x131" how can I prevent this? I cannot run metaMDS without having this formatted as a distance-matrix.

Thank you in advance!

1 Like

I solved my issue like this. In the end it all boiled down to basic R things like what is a tibble vs a matrix vs a dataframe.
The solution was: set column names to TRUE and omit the row names

dist_bray <- read_tsv(file='distance-matrix-for-R.tsv', col_names=TRUE)
dist_bray[1] <- NULL
correct<-as.dist(dist_bray)
metaMDS(correct)

I'm just leaving this here in case anyone else will find this useful in the future.

Best Regards!

4 Likes

@e_flat_minor

An alternative solution is to use the excellent qiime2R library to import the qza objects directly into R.
It will intuitively save the data as the right type.

eg.

dist <- read_qza('bray_curtis_distance_matrix.qza')$data

The read_qza function can also import PCoA ordinations, alpha diversity values, featuretables etc.

Calum

2 Likes