生信人

找回密码
立即注册
搜索
热搜: 活动 交友 discuz
发新帖

0

收听

12

听众

318

主题
发表于 2023-5-11 21:21:11 | 查看: 2449| 回复: 0
phyloseq:
  1. https://github.com/joey711/phyloseq
复制代码
加载 phyloseq 包
  1. library(phyloseq);packageVersion("phyloseq")
  2. library(Biostrings);packageVersion("Biostrings")
  3. library(ggplot2); packageVersion("ggplot2")
  4. theme_set(theme_bw())
复制代码
从 dada2 结果中生成 phyloseq 输入文件
  1. #metadata
  2. samples.out <- rownames(seqtab.nochim)
  3. samples.out

  4. subject <- sapply(strsplit(samples.out, "D"), `[`, 1)
  5. subject
  6. gender <- substr(subject,1,1)
  7. subject <- substr(subject,2,999)
  8. day <- as.integer(sapply(strsplit(samples.out, "D"), `[`, 2))
  9. samdf <- data.frame(Subject=subject, Gender=gender, Day=day)
  10. samdf$When <- "Early"
  11. samdf$When[samdf$Day>100] <- "Late"
  12. rownames(samdf) <- samples.out
  13. samdf
复制代码
  1. #读入文件,otu_table,sample_data,tax_table
  2. ps <- phyloseq(otu_table(seqtab.nochim, taxa_are_rows=FALSE),
  3.                sample_data(samdf),
  4.                tax_table(taxid),
  5.               phy_tree(fitGTR$tree))
  6. #方法二:
  7. ps1 <- phyloseq(otu_table(seqtab.nochim, taxa_are_rows=FALSE),
  8.                sample_data(samdf),
  9.                tax_table(taxa))
  10. library("ape")
  11. random_tree = rtree(ntaxa(ps1), rooted=TRUE, tip.label=taxa_names(ps1))
  12. plot(random_tree)

  13. ps = merge_phyloseq(ps1, random_tree)



  14. #去除Mock
  15. ps <- prune_samples(sample_names(ps) != "Mock", ps) # Remove mock sample


  16. dna <- Biostrings::DNAStringSet(taxa_names(ps))
  17. dna
  18. names(dna) <- taxa_names(ps)
  19. names(dna)
  20. ps <- merge_phyloseq(ps, dna)
  21. ps
  22. taxa_names(ps) <- paste0("ASV", seq(ntaxa(ps)))
  23. ps
复制代码
alpha 多样性
  1. plot_richness(ps, x="Day", measures=c("Shannon", "Simpson"), color="When")

  2. # Transform data to proportions as appropriate for Bray-Curtis distances
  3. ps.prop <- transform_sample_counts(ps, function(otu) otu/sum(otu))
  4. ord.nmds.bray <- ordinate(ps.prop, method="NMDS", distance="bray")
  5. plot_ordination(ps.prop, ord.nmds.bray, color="When", title="Bray NMDS")
复制代码
条形图
  1. top20 <- names(sort(taxa_sums(ps), decreasing=TRUE))[1:20]
  2. ps.top20 <- transform_sample_counts(ps, function(OTU) OTU/sum(OTU))
  3. ps.top20 <- prune_taxa(top20, ps.top20)
  4. plot_bar(ps.top20, x="Day", fill="family") + facet_wrap(~When, scales="free_x")
复制代码
画树
  1. plot_tree(ps)
  2. plot_tree(ps, "treeonly")
  3. plot_tree(ps, "treeonly", nodeplotblank)
  4. plot_tree(ps, "treeonly", nodeplotblank, ladderize="left")
  5. plot_tree(ps, "treeonly", nodeplotblank, ladderize=TRUE)
  6. plot_tree(ps, nodelabf=nodeplotblank, label.tips="taxa_names", ladderize="left")

  7. # plot_tree(ps, "anythingelse")
  8. # plot_tree(ps, nodelabf=nodeplotboot(), ladderize="left", color="SampleType")
  9. # plot_tree(ps, nodelabf=nodeplotboot(), ladderize="left", color="Class")
  10. # plot_tree(ps, nodelabf=nodeplotboot(), ladderize="left", color="SampleType", shape="Class")
  11. # # The default
  12. # plot_tree(ps, color="SampleType", ladderize="left")
复制代码

收藏回复 显示全部楼层 道具 举报

您需要登录后才可以回帖 登录 | 立即注册

QQ|Archiver|手机版|小黑屋|生信人 ( 萌ICP备20244422号 )

GMT+8, 2024-11-23 21:01 , Processed in 0.074787 second(s), 29 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表