{"id":17961216,"url":"https://github.com/erictleung/phyloseq-cheatsheet","last_synced_at":"2025-03-25T03:31:34.503Z","repository":{"id":72464202,"uuid":"176306313","full_name":"erictleung/phyloseq-cheatsheet","owner":"erictleung","description":":notebook: Minimal cheatsheet for functions in the phyloseq R package","archived":false,"fork":false,"pushed_at":"2021-05-08T17:13:07.000Z","size":516,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-19T08:39:47.205Z","etag":null,"topics":["bioconductor","bioinformatics","cheatsheet","data-analysis","microbiome","microbiota","notes","phyloseq","r"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc0-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/erictleung.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2019-03-18T14:42:10.000Z","updated_at":"2024-01-24T15:49:09.000Z","dependencies_parsed_at":null,"dependency_job_id":"b9120124-6464-4645-bc15-1481c1497a44","html_url":"https://github.com/erictleung/phyloseq-cheatsheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erictleung%2Fphyloseq-cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erictleung%2Fphyloseq-cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erictleung%2Fphyloseq-cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erictleung%2Fphyloseq-cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/erictleung","download_url":"https://codeload.github.com/erictleung/phyloseq-cheatsheet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245394751,"owners_count":20608122,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bioconductor","bioinformatics","cheatsheet","data-analysis","microbiome","microbiota","notes","phyloseq","r"],"created_at":"2024-10-29T11:08:40.421Z","updated_at":"2025-03-25T03:31:34.489Z","avatar_url":"https://github.com/erictleung.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# phyloseq-cheatsheet\n\nMinimal cheatsheet for functions in the phyloseq R package.\n\n**Contents**\n\n- [Workflow](#workflow)\n- [Class accessors](#class-accessors)\n- [Code snippets](#code-snippets)\n- [Insights](#insights)\n- [Useful Resources](#useful-resources)\n\n\n## Workflow\n\n![Analysis workflow](./img/analysis_workflow.png)\n\n\u003e Attribution: McMurdie, Holmes (2013)\n\u003e https://doi.org/10.1371/journal.pone.0061217.g002\n\n\n## Class accessors\n\n![phyloseq class](./img/phyloseq_class.png)\n\n\u003e Attribution: McMurdie, Holmes (2013)\n\u003e https://doi.org/10.1371/journal.pone.0061217.g003\n\n\n## Code snippets\n\nThe table here is meant to be a quick look up on how to use common functions.\n\nThe table below has four columns:\n\n1. **Type** = Input, Data, Wrangle, Plot\n2. **Function** = Function as called in script\n3. **Description** = Terse description of function\n4. **Example** = Minimal code example to demonstrate function; will often make\n   use of built-in data from `phyloseq`\n\n| Type    | Function         | Description          | Example                |\n|---------|------------------|----------------------|------------------------|\n| Data    | `enterotype`     |                      | `data(enterotype)`     |\n| Data    | `GlobalPatterns` |                      | `data(GlobalPatterns)` |\n| Data    | `esophagus`      | Subset of esophageal | `data(esophagus)`      |\n| Wrangle | `psmelt`         | Change to data frame | `psmelt(esophagus)`    |\n\n\n## Insights\n\n### `prune_*` versus `subset_*`\n\nThere is a subtle difference between the `prune_*` and `subset_*` functions.\n\nThe `prune_` functions (e.g., `prune_samples()`) keep the set of observations based\non the data in the phyloseq object itself, such as conditioning on one of the\ncolumns.\n\nThe `subset_` functions (e.g., `subset_samples()`) keep the set of observations based\nauxillary data and/or evaluated expressions. In other words, it is a wrapper function\naround the base R `subset()` function.\n\nHere are some examples:\n\n```r\n# Source: https://joey711.github.io/phyloseq/preprocess.html\nGP.chl \u003c- subset_taxa(GlobalPatterns, Phylum == \"Chlamydiae\")\nGP.chl \u003c- prune_samples(sample_sums(GP.chl) \u003e= 20, GP.chl)\n\n# Subset using sample names themselves\nGP.subset \u003c- prune_samples(c(\"CL3\", \"CC1\", \"SV1\"), GlobalPatters)\n```\n\nThe `subset_taxa()` function uses the already present `Phylum` column in the taxonomy\ntable to subset the data. \n\nMeanwhile, the `prune_samples()` function uses\n\n- an expression that evaluates to `TRUE/FALSE`\n- a character vector of sample names\n\nin order to subset the samples. This sort of expression\ncould not be evaluated using the `subset_samples()` function.\n\nSee https://joey711.github.io/phyloseq/preprocess.html#preprocessing for more.\n\n\n### Using `subset_*` functions\n\nThe `subset_*` functions (for example, `subset_samples()`) uses base R's `subset()`\nfunction. The documentation of this function notes:\n\n\u003e This is a convenience function intended for use interactively. For programming it\n\u003e is better to use the standard subsetting functions like [, and in particular the\n\u003e non-standard evaluation of argument subset can have unanticipated consequences.\n\nIn other words, the `subset()` function and `phyloseq::subset_*` function by\nassociation don't work well within functions.\n\nTo get around this, I have been using `phyloseq::prune_samples()` and having code\nprior to it to get the sample names I want based on a boolean check.\n\n**Note**: doesn't run reproducibly (yet), but the sentiment remains.\n\n```r\n# Get sample names\nkeep_samples \u003c- sample_data(GlobalPatterns)$sample_name %in% c(\"CL3\", \"CC1\", \"SV1\")\n\n# Subset using sample names themselves\nGP.subset \u003c- prune_samples(keep_samples, GlobalPatters)\n```\n\nSource: https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/subset\n\n### Extracting data frames into tibbles\n\nWhen extracting data frames from `phyloseq` using accessors like `sample_data()` can\nend up with unintended consequences, as noted here.\n\nIn this example, the code shows various ways to access the sample data and converting\nit to a non-`phyloseq` data frame and looking at the dimensions of the object.\n\nNotice the cases when the dimensions go from 26 x 7 to 8 x 8.\n\n``` r\nlibrary(phyloseq)\nlibrary(magrittr)\nlibrary(tibble)\n\ndata(\"GlobalPatterns\")\n\n# Natural use but with phyloseq object\nGlobalPatterns %\u003e%\n    sample_data %\u003e%\n    dim()\n#\u003e [1] 26  7\n\n# Using base R\nGlobalPatterns %\u003e%\n    sample_data %\u003e%\n    data.frame() %\u003e%\n    dim()\n#\u003e [1] 26  7\n\n# Using base R \"as\" convention\nGlobalPatterns %\u003e%\n    sample_data %\u003e%\n    as.data.frame() %\u003e%\n    dim()\n#\u003e [1] 26  7\n\n# Use tibble convention\nGlobalPatterns %\u003e%\n    sample_data() %\u003e%\n    as_tibble() %\u003e%\n    dim()\n#\u003e Warning in class(x) \u003c- c(subclass, tibble_class): Setting class(x) to\n#\u003e multiple strings (\"tbl_df\", \"tbl\", ...); result will no longer be an S4\n#\u003e object\n#\u003e [1] 26  7\n\n# Use tibble function to keep row names\nGlobalPatterns %\u003e%\n    sample_data() %\u003e%\n    rownames_to_column() %\u003e%\n    dim()\n#\u003e [1] 8 8\n\n# Using base R and row names to column\nGlobalPatterns %\u003e%\n    sample_data %\u003e%\n    data.frame() %\u003e%\n    rownames_to_column() %\u003e%\n    dim()\n#\u003e [1] 26  8\n\n# Using base R \"as\" convention and row names to column\nGlobalPatterns %\u003e%\n    sample_data %\u003e%\n    as.data.frame() %\u003e%\n    rownames_to_column() %\u003e%\n    dim()\n#\u003e [1] 8 8\n\n# Use tibble convention and row names to column\nGlobalPatterns %\u003e%\n    sample_data() %\u003e%\n    as_tibble() %\u003e%\n    rownames_to_column() %\u003e%\n    dim()\n#\u003e Warning in class(x) \u003c- c(subclass, tibble_class): Setting class(x) to\n#\u003e multiple strings (\"tbl_df\", \"tbl\", ...); result will no longer be an S4\n#\u003e object\n#\u003e [1] 26  8\n```\n\n\u003csup\u003eCreated on 2019-06-17 by the [reprex package](https://reprex.tidyverse.org) (v0.2.1)\u003c/sup\u003e\n\n\n### Creating new variables in data\n\nA common task is to quickly create new variables in the data (e.g., sample data).\n\nInstead of extracting the entire data and the reassigning it, you can simply access the variable itself directly using the `$` accessor.\n\n``` r\n# Load phyloseq and data\nlibrary(phyloseq)\ndata(\"enterotype\")\nenterotype\n#\u003e phyloseq-class experiment-level object\n#\u003e otu_table()   OTU Table:         [ 553 taxa and 280 samples ]\n#\u003e sample_data() Sample Data:       [ 280 samples by 9 sample variables ]\n#\u003e tax_table()   Taxonomy Table:    [ 553 taxa by 1 taxonomic ranks ]\n\n# See variables we can change\nhead(sample_data(enterotype))\n#\u003e           Enterotype Sample_ID SeqTech  SampleID     Project Nationality Gender\n#\u003e AM.AD.1         \u003cNA\u003e   AM.AD.1  Sanger   AM.AD.1      gill06    american      F\n#\u003e AM.AD.2         \u003cNA\u003e   AM.AD.2  Sanger   AM.AD.2      gill06    american      M\n#\u003e AM.F10.T1       \u003cNA\u003e AM.F10.T1  Sanger AM.F10.T1 turnbaugh09    american      F\n#\u003e AM.F10.T2          3 AM.F10.T2  Sanger AM.F10.T2 turnbaugh09    american      F\n#\u003e DA.AD.1            2   DA.AD.1  Sanger   DA.AD.1     MetaHIT      danish      F\n#\u003e DA.AD.1T        \u003cNA\u003e  DA.AD.1T  Sanger      \u003cNA\u003e        \u003cNA\u003e        \u003cNA\u003e   \u003cNA\u003e\n#\u003e           Age ClinicalStatus\n#\u003e AM.AD.1    28        healthy\n#\u003e AM.AD.2    37        healthy\n#\u003e AM.F10.T1  NA          obese\n#\u003e AM.F10.T2  NA          obese\n#\u003e DA.AD.1    59        healthy\n#\u003e DA.AD.1T   NA           \u003cNA\u003e\n\n# Change variable\nsample_data(enterotype)$Over30 \u003c- sample_data(enterotype)$Age \u003e 30\n\n# See changes\nhead(sample_data(enterotype)[, c(\"Age\", \"Over30\")])\n#\u003e           Age Over30\n#\u003e AM.AD.1    28  FALSE\n#\u003e AM.AD.2    37   TRUE\n#\u003e AM.F10.T1  NA     NA\n#\u003e AM.F10.T2  NA     NA\n#\u003e DA.AD.1    59   TRUE\n#\u003e DA.AD.1T   NA     NA\n```\n\n\u003csup\u003eCreated on 2020-09-10 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)\u003c/sup\u003e\n\n\n## Useful Resources\n\n- [`phyloseq` Official Website](https://joey711.github.io/phyloseq/index.html)\n- [DIY: Public Restroom Bacteria](http://joey711.github.io/phyloseq-demo/Restroom-Biogeography)\n- [Vignette for phyloseq: Analysis of high-throughput microbiome census data](https://www.bioconductor.org/packages/devel/bioc/vignettes/phyloseq/inst/doc/phyloseq-analysis.html)\n\n\n## License\n\n[![CC0](http://mirrors.creativecommons.org/presskit/buttons/88x31/svg/cc-zero.svg)](https://creativecommons.org/publicdomain/zero/1.0/)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferictleung%2Fphyloseq-cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ferictleung%2Fphyloseq-cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferictleung%2Fphyloseq-cheatsheet/lists"}