joey711 / phyloseq

phyloseq is a set of classes, wrappers, and tools (in R) to make it easier to import, store, and analyze phylogenetic sequencing data; and to reproducibly share that data and analysis with others. See the phyloseq front page:
http://joey711.github.io/phyloseq/
567 stars 187 forks source link

Hi @marwa38 You can do this without using functions from the `microbiome` package. #1724

Closed ElizabethOLeary closed 5 months ago

ElizabethOLeary commented 5 months ago
          Hi @marwa38 You can do this without using functions from the `microbiome` package.
library(phyloseq)
library(ggplot2)
data(GlobalPatterns)

GP <- GlobalPatterns

# Agglomerate taxa at genus level
GP.genus <- tax_glom(GP, taxrank = "Genus")

# Top N taxa
N <- 20
top <- names(sort(taxa_sums(GP.genus), decreasing = TRUE))[1:N]

# Calculate relative abundance
GP.genus.prop <- transform_sample_counts(GP.genus, function(x) x / sum(x) )

# Subset object to top N taxa
GP.genus.prop.top <- prune_taxa(top, GP.genus.prop)
> top
 [1] "331820" "279599" "171551" "360229" "94166"  "98605"  "326977" "550960"
 [9] "470973" "471122" "256977" "298875" "323845" "557211" "259569" "536311"
[17] "291090" "357795" "228764" "299892"

> GP.genus.prop.top
phyloseq-class experiment-level object
otu_table()   OTU Table:         [ 20 taxa and 26 samples ]
sample_data() Sample Data:       [ 26 samples by 7 sample variables ]
tax_table()   Taxonomy Table:    [ 20 taxa by 7 taxonomic ranks ]
phy_tree()    Phylogenetic Tree: [ 20 tips and 19 internal nodes ]

If you wish to use psmelt:

> GP.genus.prop.top %>% psmelt() %>% head(5)
       OTU   Sample Abundance X.SampleID  Primer Final_Barcode Barcode_truncated_plus_T Barcode_full_length SampleType
266 331820  M11Fcsw 0.7212633    M11Fcsw ILBC_05        AAGCTG                   CAGCTT         CGACTGCAGCT      Feces
117 279599 LMEpi24M 0.7141147   LMEpi24M ILBC_13        ACACTG                   CAGTGT         CATGAACAGTG Freshwater
270 331820  M31Fcsw 0.5450176    M31Fcsw ILBC_04        AAGAGA                   TCTCTT         TCGACATCTCT      Feces
61  256977      CC1 0.5125579        CC1 ILBC_02        AACTCG                   CGAGTT         CATCGACGAGT       Soil
233 323845      NP5 0.4210806        NP5 ILBC_21        ACATGA                   TCATGT         GAACGATCATG      Ocean
                                     Description  Kingdom          Phylum               Class             Order
266     M1, Day 1, fecal swab, whole body study  Bacteria   Bacteroidetes         Bacteroidia     Bacteroidales
117 Lake Mendota Minnesota, 24 meter epilimnion  Bacteria   Cyanobacteria    Nostocophycideae        Nostocales
270      M3, Day 1, fecal swab, whole body study Bacteria   Bacteroidetes         Bacteroidia     Bacteroidales
61      Cedar Creek Minnesota, grassland, pH 6.1 Bacteria Verrucomicrobia      Spartobacteria Spartobacteriales
233       Newport Pier, CA surface water, Time 3 Bacteria  Proteobacteria Gammaproteobacteria Oceanospirillales
                Family              Genus
266     Bacteroidaceae        Bacteroides
117        Nostocaceae     Dolichospermum
270     Bacteroidaceae        Bacteroides
61  Spartobacteriaceae               MC18
233     Halomonadaceae CandidatusPortiera

Originally posted by @ycl6 in https://github.com/joey711/phyloseq/issues/1516#issuecomment-975938425