Beta_div_auto.R Tutorial - sciencesharon/MicrobialSeq GitHub Wiki

Beta_div_auto.R Tutorial

This function is designed for downstream use with the MetaPhlAn4 script calculate_diversity.R

This function takes the output files from calculate_diversity.R as well as a metadata file and directly generates PCoA plots from each beta-diversity matrix.

Example

betadiversity_examples.csv

metadata_example.csv

Function usage example

The function takes the input of a list of files and works with multiple file inputs. For this example, I am using just the one file input.

Read the beta diversity files as a list into R.

files <- list("/path/to/betadiversity_examples.csv")

Read the metadata file into R.

met <- read.csv("/path/to/metadata_example.csv", row.names = 1)

Optionally define a subset of the samples to subset your beta diversity matrix

subset.group <- met[c(1:50),c(1)]

If you are subsetting the beta diversity matrix, don't forget to subset your metadata as well.

met.subset <- met[subset.group,]

Run the function

  • file_paths: this is the list of file paths to your beta diversity files

  • subset_names: this is the list of column & row names by which to subset the beta diversity matrices

  • output_dir: this is the directory to save the output files

  • prefix: this is the prefix to be added to each output file

  • colour & fill: these are your grouping variable found in the metadata (must be one of the metadata column names) - I typically set these as the same variable.

  • title: optionally adds a title to the PCoA plot

  • fill_palette:optionally changes the colors of the ellipses for the grouping variables

  • plot_height and plot_width optionally changes the dimensions of the output .pdf files

  • plot: by default is TRUE, if FALSE does not output PCoA plots for the beta diversity matrices and instead only outputs the matrices to the global environment and as .csv files

pcoa <- process_tsv_files(file_paths = files, subset_names = subset.group, output_dir = "/path/to/output/file", prefix = "test", met = met.subset, colour = "Group", fill = "Group", title = "Title", fill_palette = c("GroupA" = "#4CBB17", "GroupB" = "#5D3FD3"), plot_width = 6, plot_height = 5, plot = TRUE)

Example output files

test_pcoa_betadiversity_examples.csv

test_betadiversity_examples.csv

test_betadiversity_examples.pdf