├── inst ├── extdata │ ├── taxid.txt │ ├── taxids2.txt │ ├── name.txt │ └── lineage.txt └── CITATION ├── vignettes ├── .gitignore └── pctax.Rmd ├── data ├── pc_tax1.rda ├── all_ec_info.rda └── all_sp_la_zh_name.rda ├── man ├── figures │ ├── pctax.png │ ├── pctax1.png │ ├── README-unnamed-chunk-2-1.png │ └── README-unnamed-chunk-3-1.png ├── cor_net.Rd ├── pc_valid.Rd ├── print.pc_otu.Rd ├── plot.g_test.Rd ├── all_sp_la_zh_name.Rd ├── c_means.Rd ├── get_diff_type.Rd ├── bbtt.Rd ├── all_ec_info.Rd ├── summary.pc_otu.Rd ├── pre_assembly_stats.Rd ├── rarefaction.Rd ├── pre_GEO.Rd ├── pc_tax1.Rd ├── plot.pro_res.Rd ├── pipe.Rd ├── grapes-less-than-greater-than-grapes.Rd ├── pre_fastp.Rd ├── multi_conf.Rd ├── before_tree.Rd ├── df2tree1.Rd ├── gp_dis_density.Rd ├── add_tax.Rd ├── load_N_data.Rd ├── sunburst.Rd ├── suijisenlin.Rd ├── plot.a_res.Rd ├── kwtest.Rd ├── as.dist.b_dist.Rd ├── tax_lca.Rd ├── b_NTI1.Rd ├── envfitt.Rd ├── stamp_plot.Rd ├── b_res_3d.Rd ├── load_mpa_df.Rd ├── mat_dist.Rd ├── gene2id.Rd ├── time_by_cm.Rd ├── rare_curve_sample.Rd ├── convert_taxon_name.Rd ├── check_taxonkit.Rd ├── pc_otu.Rd ├── ALDEX.Rd ├── get_all_sp_la_zh_name.Rd ├── multi_bar.Rd ├── micro_sbatch.Rd ├── procrustes_analyse.Rd ├── df2tree.Rd ├── grap_p_test.Rd ├── volcano_p.Rd ├── m_group_env.Rd ├── rare_curve_species.Rd ├── install_taxonkit.Rd ├── download_taxonkit_dataset.Rd ├── add_strip.Rd ├── permanova2.Rd ├── sangji_plot.Rd ├── as.b_dist.Rd ├── diff_da.Rd ├── pctax-package.Rd ├── batch_mediate.Rd ├── aor.Rd ├── geo_sim.Rd ├── nst.Rd ├── a_diversity.Rd ├── drop_tips_update.Rd ├── ncm.Rd ├── name_or_id2df.Rd ├── pre_tax_table.Rd ├── permanova.Rd ├── z_diversity_decay.Rd ├── nti_rc.Rd ├── plot_N_cycle.Rd ├── z_diversity.Rd ├── RDA_plot.Rd ├── RCbray1.Rd ├── taxonkit_list.Rd ├── ann_tree.Rd ├── myRDA.Rd ├── taxonkit_lca.Rd ├── taxonkit_name2taxid.Rd ├── plot.b_res.Rd ├── b_analyse.Rd ├── plot_element_cycle.Rd ├── taxonkit_lineage.Rd ├── plot_two_tree.Rd ├── taxonkit_filter.Rd └── taxonkit_reformat.Rd ├── pkgdown └── favicon │ ├── favicon.ico │ ├── favicon-16x16.png │ ├── favicon-32x32.png │ ├── mstile-150x150.png │ ├── apple-touch-icon.png │ ├── android-chrome-192x192.png │ ├── android-chrome-512x512.png │ ├── browserconfig.xml │ ├── site.webmanifest │ └── safari-pinned-tab.svg ├── .Rbuildignore ├── pctax.Rproj ├── R ├── utils-pipe.R ├── pctax-package.R ├── data.R ├── stats.R ├── pc_otu.R ├── rarefy.R ├── a_diversity.R ├── additional.R └── eco_micro.R ├── _pkgdown.yml ├── .gitignore ├── NEWS.md ├── .github └── workflows │ ├── R-CMD-check.yaml │ ├── pkgdown.yaml │ └── pr-commands.yaml ├── DESCRIPTION ├── README.md ├── NAMESPACE └── README.Rmd /inst/extdata/taxid.txt: -------------------------------------------------------------------------------- 1 | 9606 2 | 63221 3 | -------------------------------------------------------------------------------- /vignettes/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | *.R 3 | -------------------------------------------------------------------------------- /data/pc_tax1.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asa12138/pctax/HEAD/data/pc_tax1.rda -------------------------------------------------------------------------------- /data/all_ec_info.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asa12138/pctax/HEAD/data/all_ec_info.rda -------------------------------------------------------------------------------- /man/figures/pctax.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asa12138/pctax/HEAD/man/figures/pctax.png -------------------------------------------------------------------------------- /man/figures/pctax1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asa12138/pctax/HEAD/man/figures/pctax1.png -------------------------------------------------------------------------------- /data/all_sp_la_zh_name.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asa12138/pctax/HEAD/data/all_sp_la_zh_name.rda -------------------------------------------------------------------------------- /pkgdown/favicon/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asa12138/pctax/HEAD/pkgdown/favicon/favicon.ico -------------------------------------------------------------------------------- /pkgdown/favicon/favicon-16x16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asa12138/pctax/HEAD/pkgdown/favicon/favicon-16x16.png -------------------------------------------------------------------------------- /pkgdown/favicon/favicon-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asa12138/pctax/HEAD/pkgdown/favicon/favicon-32x32.png -------------------------------------------------------------------------------- /pkgdown/favicon/mstile-150x150.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asa12138/pctax/HEAD/pkgdown/favicon/mstile-150x150.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asa12138/pctax/HEAD/pkgdown/favicon/apple-touch-icon.png -------------------------------------------------------------------------------- /man/figures/README-unnamed-chunk-2-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asa12138/pctax/HEAD/man/figures/README-unnamed-chunk-2-1.png -------------------------------------------------------------------------------- /man/figures/README-unnamed-chunk-3-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asa12138/pctax/HEAD/man/figures/README-unnamed-chunk-3-1.png -------------------------------------------------------------------------------- /pkgdown/favicon/android-chrome-192x192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asa12138/pctax/HEAD/pkgdown/favicon/android-chrome-192x192.png -------------------------------------------------------------------------------- /pkgdown/favicon/android-chrome-512x512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asa12138/pctax/HEAD/pkgdown/favicon/android-chrome-512x512.png -------------------------------------------------------------------------------- /inst/extdata/taxids2.txt: -------------------------------------------------------------------------------- 1 | 131567 2 | 2 3 | 1783257 4 | 74201 5 | 203494 6 | 48461 7 | 1647988 8 | 239934 9 | 239935 10 | 349741 11 | -------------------------------------------------------------------------------- /inst/extdata/name.txt: -------------------------------------------------------------------------------- 1 | Homo sapiens 2 | Akkermansia muciniphila ATCC BAA-835 3 | Akkermansia muciniphila 4 | Mouse Intracisternal A-particle 5 | Wei Shen 6 | uncultured murine large bowel bacterium BAC 54B 7 | Croceibacter phage P2559Y 8 | -------------------------------------------------------------------------------- /pkgdown/favicon/browserconfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | #da532c 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | .gitignore 4 | .RData 5 | .Rhistory 6 | images 7 | images/.* 8 | ^data-raw$ 9 | \.DS_Store 10 | ^_pkgdown\.yml$ 11 | ^docs$ 12 | ^pkgdown$ 13 | ^README\.qmd$ 14 | ^README\.Rmd$ 15 | README_files 16 | README_files/.* 17 | README_cache 18 | README_cache/.* 19 | ^\.github$ 20 | ^codecov\.yml$ 21 | -------------------------------------------------------------------------------- /man/cor_net.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/eco_micro.R 3 | \name{cor_net} 4 | \alias{cor_net} 5 | \title{Correlation network, species-interaction network for omics} 6 | \usage{ 7 | cor_net() 8 | } 9 | \value{ 10 | No value 11 | } 12 | \description{ 13 | Correlation network, species-interaction network for omics 14 | } 15 | -------------------------------------------------------------------------------- /man/pc_valid.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/pc_otu.R 3 | \name{pc_valid} 4 | \alias{pc_valid} 5 | \title{Judge pc_otu is valid or not} 6 | \usage{ 7 | pc_valid(pc) 8 | } 9 | \arguments{ 10 | \item{pc}{a pc_otu object} 11 | } 12 | \value{ 13 | logical 14 | } 15 | \description{ 16 | Judge pc_otu is valid or not 17 | } 18 | -------------------------------------------------------------------------------- /man/print.pc_otu.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/pc_otu.R 3 | \name{print.pc_otu} 4 | \alias{print.pc_otu} 5 | \title{Print} 6 | \usage{ 7 | \method{print}{pc_otu}(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{pc_otu} 11 | 12 | \item{...}{add} 13 | } 14 | \value{ 15 | No value 16 | } 17 | \description{ 18 | Print 19 | } 20 | -------------------------------------------------------------------------------- /man/plot.g_test.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/b_analyse.R 3 | \name{plot.g_test} 4 | \alias{plot.g_test} 5 | \title{Plot g_test} 6 | \usage{ 7 | \method{plot}{g_test}(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{a g_test object} 11 | 12 | \item{...}{add} 13 | } 14 | \value{ 15 | ggplot 16 | } 17 | \description{ 18 | Plot g_test 19 | } 20 | \seealso{ 21 | \code{\link{permanova}} 22 | } 23 | -------------------------------------------------------------------------------- /man/all_sp_la_zh_name.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{all_sp_la_zh_name} 5 | \alias{all_sp_la_zh_name} 6 | \title{all species latin names and chinese names} 7 | \format{ 8 | a dataframe. 9 | \describe{ 10 | \item{latin_name}{latin name} 11 | \item{chinese_name}{chinese name} 12 | } 13 | } 14 | \description{ 15 | all species latin names and chinese names. 16 | } 17 | -------------------------------------------------------------------------------- /man/c_means.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/diff_analyse.R 3 | \name{plot.time_cm} 4 | \alias{plot.time_cm} 5 | \title{Plot time_cm} 6 | \usage{ 7 | \method{plot}{time_cm}(x, mem_thr = 0.6, ...) 8 | } 9 | \arguments{ 10 | \item{x}{time_cm} 11 | 12 | \item{mem_thr}{membership threshold} 13 | 14 | \item{...}{add} 15 | } 16 | \value{ 17 | ggplot 18 | } 19 | \description{ 20 | Plot time_cm 21 | } 22 | -------------------------------------------------------------------------------- /man/get_diff_type.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/diff_analyse.R 3 | \name{get_diff_type} 4 | \alias{get_diff_type} 5 | \title{Get mean and type} 6 | \usage{ 7 | get_diff_type(otutab, group_df) 8 | } 9 | \arguments{ 10 | \item{otutab}{otutab} 11 | 12 | \item{group_df}{a dataframe with rowname same to dist and one group column} 13 | } 14 | \value{ 15 | No value 16 | } 17 | \description{ 18 | Get mean and type 19 | } 20 | -------------------------------------------------------------------------------- /man/bbtt.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/diff_analyse.R 3 | \name{bbtt} 4 | \alias{bbtt} 5 | \title{ggdotchart for diff analysis} 6 | \usage{ 7 | bbtt(res, pvalue = "glm.eBH", topN = 20) 8 | } 9 | \arguments{ 10 | \item{res}{result of ALDEX or kwtest} 11 | 12 | \item{pvalue}{the name of pvaule} 13 | 14 | \item{topN}{topN} 15 | } 16 | \value{ 17 | ggplot 18 | } 19 | \description{ 20 | ggdotchart for diff analysis 21 | } 22 | -------------------------------------------------------------------------------- /man/all_ec_info.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{all_ec_info} 5 | \alias{all_ec_info} 6 | \title{all element cycle information.} 7 | \format{ 8 | a list contains four tables. 9 | \describe{ 10 | \item{ec_node}{chemicals} 11 | \item{ec_link}{reactions} 12 | \item{ec_gene}{genes} 13 | \item{ec_path}{reactions labels} 14 | } 15 | } 16 | \description{ 17 | all element cycle information. 18 | } 19 | -------------------------------------------------------------------------------- /man/summary.pc_otu.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/pc_otu.R 3 | \name{summary.pc_otu} 4 | \alias{summary.pc_otu} 5 | \title{Summary pc_otu} 6 | \usage{ 7 | \method{summary}{pc_otu}(object, ...) 8 | } 9 | \arguments{ 10 | \item{object}{pc_otu} 11 | 12 | \item{...}{add} 13 | } 14 | \value{ 15 | No value 16 | } 17 | \description{ 18 | Summary pc_otu 19 | } 20 | \examples{ 21 | data("pc_tax1") 22 | summary(pc_tax1) 23 | 24 | } 25 | -------------------------------------------------------------------------------- /man/pre_assembly_stats.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/other_software.R 3 | \name{pre_assembly_stats} 4 | \alias{pre_assembly_stats} 5 | \title{Prepare the result from assembly_stats (.json file)} 6 | \usage{ 7 | pre_assembly_stats(jsonfiles) 8 | } 9 | \arguments{ 10 | \item{jsonfiles}{the directory contains .json file} 11 | } 12 | \value{ 13 | data.frame 14 | } 15 | \description{ 16 | Prepare the result from assembly_stats (.json file) 17 | } 18 | -------------------------------------------------------------------------------- /man/rarefaction.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/rarefy.R 3 | \name{rarefaction} 4 | \alias{rarefaction} 5 | \title{Rarefy a otutab} 6 | \usage{ 7 | rarefaction(otutab, sample = NULL) 8 | } 9 | \arguments{ 10 | \item{otutab}{otutab} 11 | 12 | \item{sample}{number} 13 | } 14 | \value{ 15 | a rarefied otutab 16 | } 17 | \description{ 18 | Rarefy a otutab 19 | } 20 | \examples{ 21 | data(otutab, package = "pcutils") 22 | rarefaction(otutab) 23 | } 24 | -------------------------------------------------------------------------------- /man/pre_GEO.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/additional.R 3 | \name{pre_GEO} 4 | \alias{pre_GEO} 5 | \title{prepare the GEO data} 6 | \usage{ 7 | pre_GEO(my_id, GEO_dir = "GEO_data", file = NULL) 8 | } 9 | \arguments{ 10 | \item{my_id}{GEOid} 11 | 12 | \item{GEO_dir}{GEO download dir} 13 | 14 | \item{file}{the downloaded file} 15 | } 16 | \value{ 17 | list(meta = meta, GSE_expr = GSE_expr) 18 | } 19 | \description{ 20 | prepare the GEO data 21 | } 22 | -------------------------------------------------------------------------------- /man/pc_tax1.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data.R 3 | \docType{data} 4 | \name{pc_tax1} 5 | \alias{pc_tax1} 6 | \title{test data (pc_otu class) for pc_tax package.} 7 | \format{ 8 | a pc_otu contains an otutab, metadata and a taxonomy table. 9 | \describe{ 10 | \item{tbls}{contians otutable rawdata} 11 | \item{metas}{contians metadata} 12 | \item{otus}{contians taxomomy table} 13 | } 14 | } 15 | \description{ 16 | an otutab, metadata and a taxonomy table. 17 | } 18 | -------------------------------------------------------------------------------- /man/plot.pro_res.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/b_analyse.R 3 | \name{plot.pro_res} 4 | \alias{plot.pro_res} 5 | \title{Plot pro_res} 6 | \usage{ 7 | \method{plot}{pro_res}(x, group, metadata = NULL, pal = NULL, ...) 8 | } 9 | \arguments{ 10 | \item{x}{pro_res} 11 | 12 | \item{group}{group} 13 | 14 | \item{metadata}{metadata} 15 | 16 | \item{pal}{pal} 17 | 18 | \item{...}{add} 19 | } 20 | \value{ 21 | a ggplot 22 | } 23 | \description{ 24 | Plot pro_res 25 | } 26 | -------------------------------------------------------------------------------- /man/pipe.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utils-pipe.R 3 | \name{\%>\%} 4 | \alias{\%>\%} 5 | \title{Pipe operator} 6 | \usage{ 7 | lhs \%>\% rhs 8 | } 9 | \arguments{ 10 | \item{lhs}{A value or the magrittr placeholder.} 11 | 12 | \item{rhs}{A function call using the magrittr semantics.} 13 | } 14 | \value{ 15 | The result of calling \code{rhs(lhs)}. 16 | } 17 | \description{ 18 | See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. 19 | } 20 | \keyword{internal} 21 | -------------------------------------------------------------------------------- /pkgdown/favicon/site.webmanifest: -------------------------------------------------------------------------------- 1 | { 2 | "name": "", 3 | "short_name": "", 4 | "icons": [ 5 | { 6 | "src": "/android-chrome-192x192.png", 7 | "sizes": "192x192", 8 | "type": "image/png" 9 | }, 10 | { 11 | "src": "/android-chrome-512x512.png", 12 | "sizes": "512x512", 13 | "type": "image/png" 14 | } 15 | ], 16 | "theme_color": "#ffffff", 17 | "background_color": "#ffffff", 18 | "display": "standalone" 19 | } 20 | -------------------------------------------------------------------------------- /inst/CITATION: -------------------------------------------------------------------------------- 1 | bibentry( 2 | header = "To cite pctax in publications use:", 3 | bibtype = "Article", 4 | title = "pctax: Professional Comprehensive Omics Data Analysis Pipeline", 5 | author = c( 6 | as.person("Chen Peng"), 7 | as.person("Chao Jiang") 8 | ), 9 | year = "2024", 10 | journal = "NA", 11 | textVersion = paste("C. Peng, C. Jiang.", 12 | "pctax: Professional Comprehensive Omics Data Analysis Pipeline.", 13 | "(2023). https://github.com/Asa12138/pctax") 14 | ) 15 | -------------------------------------------------------------------------------- /man/grapes-less-than-greater-than-grapes.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utils-pipe.R 3 | \name{\%<>\%} 4 | \alias{\%<>\%} 5 | \title{Assignment pipe} 6 | \usage{ 7 | lhs \%<>\% rhs 8 | } 9 | \arguments{ 10 | \item{lhs}{A value or the magrittr placeholder.} 11 | 12 | \item{rhs}{A function call using the magrittr semantics.} 13 | } 14 | \value{ 15 | The result of calling \code{rhs(lhs)}. 16 | } 17 | \description{ 18 | See \code{magrittr::\link[magrittr:pipe]{\%<>\%}} for details. 19 | } 20 | \keyword{internal} 21 | -------------------------------------------------------------------------------- /man/pre_fastp.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/other_software.R 3 | \name{pre_fastp} 4 | \alias{pre_fastp} 5 | \title{Prepare the result from fastp (.json file)} 6 | \usage{ 7 | pre_fastp(jsonfiles, prefix = c("Raw", "Clean")) 8 | } 9 | \arguments{ 10 | \item{jsonfiles}{the directory contains .json file} 11 | 12 | \item{prefix}{default c("Raw","Clean"), for the before filtering and after filtering.} 13 | } 14 | \value{ 15 | data.frame 16 | } 17 | \description{ 18 | Prepare the result from fastp (.json file) 19 | } 20 | -------------------------------------------------------------------------------- /man/multi_conf.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/diff_analyse.R 3 | \name{multi_conf} 4 | \alias{multi_conf} 5 | \title{Difference analysis} 6 | \usage{ 7 | multi_conf(otutab, group_df) 8 | } 9 | \arguments{ 10 | \item{otutab}{otutab} 11 | 12 | \item{group_df}{a dataframe with rowname same to dist and one group column} 13 | } 14 | \value{ 15 | ggplot 16 | } 17 | \description{ 18 | Difference analysis 19 | } 20 | \examples{ 21 | data(otutab, package = "pcutils") 22 | multi_conf(otutab[1:10, 1:12], metadata["Group"]) 23 | } 24 | -------------------------------------------------------------------------------- /man/before_tree.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/phylogenetic.R 3 | \name{before_tree} 4 | \alias{before_tree} 5 | \title{Before df2tree check} 6 | \usage{ 7 | before_tree(f_tax) 8 | } 9 | \arguments{ 10 | \item{f_tax}{table} 11 | } 12 | \value{ 13 | table 14 | } 15 | \description{ 16 | Before df2tree check 17 | } 18 | \examples{ 19 | wrong_taxdf <- data.frame( 20 | kingdom = c(rep(c("A", "B"), each = 4), "C", NA), 21 | "phylum" = c("A", "a", "b", "c", "c", "c", "d", NA, NA, "e") 22 | ) 23 | before_tree(wrong_taxdf) 24 | 25 | } 26 | -------------------------------------------------------------------------------- /man/df2tree1.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/phylogenetic.R 3 | \name{df2tree1} 4 | \alias{df2tree1} 5 | \title{From a dataframe to construct a phylo (save nwk)} 6 | \usage{ 7 | df2tree1(taxa) 8 | } 9 | \arguments{ 10 | \item{taxa}{dataframe} 11 | } 12 | \value{ 13 | phylo object 14 | } 15 | \description{ 16 | NOTE: this function will transfer all space to \verb{_} 17 | } 18 | \examples{ 19 | if (requireNamespace("ape")) { 20 | data(otutab, package = "pcutils") 21 | df2tree1(taxonomy) -> tax_tree 22 | print(tax_tree) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /man/gp_dis_density.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/b_analyse.R 3 | \name{gp_dis_density} 4 | \alias{gp_dis_density} 5 | \title{Group inter-intra density} 6 | \usage{ 7 | gp_dis_density(otutab, group) 8 | } 9 | \arguments{ 10 | \item{otutab}{an otutab data.frame, samples are columns, taxs are rows.} 11 | 12 | \item{group}{group vector} 13 | } 14 | \value{ 15 | ggplot 16 | } 17 | \description{ 18 | Group inter-intra density 19 | } 20 | \examples{ 21 | data(otutab, package = "pcutils") 22 | gp_dis_density(otutab, metadata["Group"]) 23 | } 24 | -------------------------------------------------------------------------------- /pctax.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: XeLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | PackageRoxygenize: rd,collate,namespace 22 | 23 | PythonType: conda 24 | PythonVersion: 3.9.13 25 | PythonPath: ~/opt/anaconda3/bin/python3.9 26 | -------------------------------------------------------------------------------- /man/add_tax.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/pc_otu.R 3 | \name{add_tax} 4 | \alias{add_tax} 5 | \title{Add taxonomy for a pc_otu object} 6 | \usage{ 7 | add_tax(pc, taxonomy) 8 | } 9 | \arguments{ 10 | \item{pc}{a pc_otu object} 11 | 12 | \item{taxonomy}{a taxomomy data.frame, look out the rownames of taxonomy and otutab should matched!} 13 | } 14 | \value{ 15 | pc_otu 16 | } 17 | \description{ 18 | Add taxonomy for a pc_otu object 19 | } 20 | \examples{ 21 | data(otutab, package = "pcutils") 22 | pc_tax1 <- pc_otu(otutab, metadata) 23 | pc_tax1 <- add_tax(pc_tax1, taxonomy) 24 | } 25 | -------------------------------------------------------------------------------- /man/load_N_data.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/functional.R 3 | \name{load_N_data} 4 | \alias{load_N_data} 5 | \title{Load N-cycle data} 6 | \usage{ 7 | load_N_data() 8 | } 9 | \value{ 10 | list 11 | } 12 | \description{ 13 | Load N-cycle data 14 | } 15 | \references{ 16 | Tu, Q., Lin, L., Cheng, L., Deng, Y. & He, Z. (2019) NCycDB: a curated integrative database for fast and accurate metagenomic profiling of nitrogen cycling genes. Bioinformatics 35, 1040–1048. 17 | Kuypers, M. M. M., Marchant, H. K. & Kartal, B. (2018) The microbial nitrogen-cycling network. Nat Rev Microbiol 16, 263–276. 18 | } 19 | -------------------------------------------------------------------------------- /man/sunburst.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/additional.R 3 | \name{sunburst} 4 | \alias{sunburst} 5 | \title{Plot a sunburst} 6 | \usage{ 7 | sunburst(tree) 8 | } 9 | \arguments{ 10 | \item{tree}{result from \code{\link{ann_tree}}} 11 | } 12 | \value{ 13 | sunburst 14 | } 15 | \description{ 16 | Plot a sunburst 17 | } 18 | \examples{ 19 | \donttest{ 20 | if (requireNamespace("plotly")) { 21 | data(otutab, package = "pcutils") 22 | ann_tree(taxonomy[, c(1, 5, 6, 7)], otutab) -> tree 23 | sunburst(tree) 24 | } 25 | } 26 | } 27 | \seealso{ 28 | \code{\link[=sangji_plot]{sangji_plot()}} 29 | } 30 | -------------------------------------------------------------------------------- /man/suijisenlin.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/diff_analyse.R 3 | \name{suijisenlin} 4 | \alias{suijisenlin} 5 | \title{RandomForest} 6 | \usage{ 7 | suijisenlin(otutab, group_df, topN = 10) 8 | } 9 | \arguments{ 10 | \item{otutab}{otutab} 11 | 12 | \item{group_df}{a dataframe with rowname same to dist and one group column} 13 | 14 | \item{topN}{default: 10} 15 | } 16 | \value{ 17 | diff 18 | } 19 | \description{ 20 | RandomForest 21 | } 22 | \examples{ 23 | if (requireNamespace("randomForest")) { 24 | data(otutab, package = "pcutils") 25 | suijisenlin(otutab, metadata["Group"]) -> rf_res 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /man/plot.a_res.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/a_diversity.R 3 | \name{plot.a_res} 4 | \alias{plot.a_res} 5 | \title{Plot a_res object} 6 | \usage{ 7 | \method{plot}{a_res}(x, group, metadata, ...) 8 | } 9 | \arguments{ 10 | \item{x}{a a_res object} 11 | 12 | \item{group}{one of colname of metadata} 13 | 14 | \item{metadata}{metadata} 15 | 16 | \item{...}{addditional parameters for \code{\link[pcutils]{group_box}} or \code{\link[pcutils]{my_lm}}} 17 | } 18 | \value{ 19 | patchwork object,you can change theme with & 20 | } 21 | \description{ 22 | Plot a_res object 23 | } 24 | \seealso{ 25 | \code{\link{a_diversity}} 26 | } 27 | -------------------------------------------------------------------------------- /man/kwtest.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/diff_analyse.R 3 | \name{kwtest} 4 | \alias{kwtest} 5 | \title{KW test} 6 | \usage{ 7 | kwtest(otutab, group_df, method = "kruskal.test") 8 | } 9 | \arguments{ 10 | \item{otutab}{otutab} 11 | 12 | \item{group_df}{a dataframe with rowname same to dist and one group column} 13 | 14 | \item{method}{"kruskal.test", see \code{\link[ggpubr]{compare_means}}} 15 | } 16 | \value{ 17 | res 18 | } 19 | \description{ 20 | KW test 21 | } 22 | \examples{ 23 | \donttest{ 24 | data(otutab, package = "pcutils") 25 | kwtest(otutab, metadata["Group"]) -> res 26 | bbtt(res, pvalue = "p.format") 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /man/as.dist.b_dist.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/b_analyse.R 3 | \name{as.dist.b_dist} 4 | \alias{as.dist.b_dist} 5 | \title{Transfer b_dist to dist} 6 | \usage{ 7 | \method{as.dist}{b_dist}(m, diag = FALSE, upper = FALSE) 8 | } 9 | \arguments{ 10 | \item{m}{a b_dist object} 11 | 12 | \item{diag}{logical value indicating whether the diagonal of the distance matrix should be printed by \code{print.dist}.} 13 | 14 | \item{upper}{logical value indicating whether the upper triangle of the distance matrix should be printed by \code{print.dist}.} 15 | } 16 | \value{ 17 | dist 18 | } 19 | \description{ 20 | Transfer b_dist to dist 21 | } 22 | -------------------------------------------------------------------------------- /man/tax_lca.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/phylogenetic.R 3 | \name{tax_lca} 4 | \alias{tax_lca} 5 | \title{Calculate the lowest common ancestor (LCA) of a set of taxa} 6 | \usage{ 7 | tax_lca(df) 8 | } 9 | \arguments{ 10 | \item{df}{a data frame with taxonomic information, with columns representing taxonomic levels} 11 | } 12 | \value{ 13 | character 14 | } 15 | \description{ 16 | Calculate the lowest common ancestor (LCA) of a set of taxa 17 | } 18 | \examples{ 19 | df <- data.frame( 20 | A = c("a", "a", "a", "a"), 21 | B = c("x", "x", "y", "y"), 22 | C = c("1", "1", "2", "3"), 23 | stringsAsFactors = FALSE 24 | ) 25 | tax_lca(df) 26 | } 27 | -------------------------------------------------------------------------------- /man/b_NTI1.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/eco_micro.R 3 | \name{b_NTI1} 4 | \alias{b_NTI1} 5 | \title{Calculate beta_NTI} 6 | \usage{ 7 | b_NTI1( 8 | phylo, 9 | otutab, 10 | beta.reps = 9, 11 | weighted = TRUE, 12 | threads = 1, 13 | verbose = TRUE 14 | ) 15 | } 16 | \arguments{ 17 | \item{phylo}{a phylo object} 18 | 19 | \item{otutab}{otutab} 20 | 21 | \item{beta.reps}{how many simulation performed?} 22 | 23 | \item{weighted}{logical} 24 | 25 | \item{threads}{use how many threads to calculate (default:4)} 26 | 27 | \item{verbose}{verbose} 28 | } 29 | \value{ 30 | a dist: b_NTI 31 | } 32 | \description{ 33 | Calculate beta_NTI 34 | } 35 | -------------------------------------------------------------------------------- /man/envfitt.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/b_analyse.R 3 | \name{envfitt} 4 | \alias{envfitt} 5 | \title{Envfit test for RDA result} 6 | \usage{ 7 | envfitt(phy.rda, env, ...) 8 | } 9 | \arguments{ 10 | \item{phy.rda}{a rda result} 11 | 12 | \item{env}{environmental factors} 13 | 14 | \item{...}{add} 15 | } 16 | \value{ 17 | g_test object 18 | } 19 | \description{ 20 | Envfit test for RDA result 21 | } 22 | \examples{ 23 | data(otutab, package = "pcutils") 24 | env <- metadata[, 6:10] 25 | # RDA 26 | myRDA(otutab, env) -> phy.rda 27 | envfitt(phy.rda, env) -> envfit_res 28 | plot(envfit_res) 29 | } 30 | \seealso{ 31 | \code{\link[vegan]{envfit}} 32 | } 33 | -------------------------------------------------------------------------------- /man/stamp_plot.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/diff_analyse.R 3 | \name{stamp_plot} 4 | \alias{stamp_plot} 5 | \title{Stamp style plot} 6 | \usage{ 7 | stamp_plot(otutab, group_df, set_order = NULL, pal = NULL) 8 | } 9 | \arguments{ 10 | \item{otutab}{otutab} 11 | 12 | \item{group_df}{a dataframe with rowname same to dist and one group column} 13 | 14 | \item{set_order}{set order of factor levels} 15 | 16 | \item{pal}{palette} 17 | } 18 | \value{ 19 | ggplot 20 | } 21 | \description{ 22 | Stamp style plot 23 | } 24 | \examples{ 25 | data(otutab, package = "pcutils") 26 | if (requireNamespace("ggfun")) stamp_plot(otutab[1:10, 1:12], metadata["Group"]) 27 | } 28 | -------------------------------------------------------------------------------- /man/b_res_3d.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/b_analyse.R 3 | \name{b_res_3d} 4 | \alias{b_res_3d} 5 | \title{3D plot for b_res} 6 | \usage{ 7 | b_res_3d(b_res, Group, metadata = NULL, ...) 8 | } 9 | \arguments{ 10 | \item{b_res}{a b_res object} 11 | 12 | \item{Group}{group vector for color} 13 | 14 | \item{metadata}{metadata contain Group} 15 | 16 | \item{...}{add} 17 | } 18 | \value{ 19 | plotly list 20 | } 21 | \description{ 22 | 3D plot for b_res 23 | } 24 | \examples{ 25 | if (requireNamespace("plotly")) { 26 | data(otutab, package = "pcutils") 27 | b_analyse(otutab, method = "pca", ndim = 3) -> b_res 28 | b_res_3d(b_res, "Group", metadata) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /man/load_mpa_df.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/phylogenetic.R 3 | \name{load_mpa_df} 4 | \alias{load_mpa_df} 5 | \title{Load a metaphlan format data.frame} 6 | \usage{ 7 | load_mpa_df(mpa_df, sum_unidentified = TRUE, rank_prefixes = pc_rank_prefixes) 8 | } 9 | \arguments{ 10 | \item{mpa_df}{metaphlan format data.frame, rownames are taxon, all value are numeric.} 11 | 12 | \item{sum_unidentified}{logical, whether to sum the unidentified reads to the correspond level.} 13 | 14 | \item{rank_prefixes}{a named vector of rank prefixes, default is \code{pc_rank_prefixes}.} 15 | } 16 | \value{ 17 | a list 18 | } 19 | \description{ 20 | Load a metaphlan format data.frame 21 | } 22 | -------------------------------------------------------------------------------- /inst/extdata/lineage.txt: -------------------------------------------------------------------------------- 1 | 9606 cellular organisms;Eukaryota;Opisthokonta;Metazoa;Eumetazoa;Bilateria;Deuterostomia;Chordata;Craniata;Vertebrata;Gnathostomata;Teleostomi;Euteleostomi;Sarcopterygii;Dipnotetrapodomorpha;Tetrapoda;Amniota;Mammalia;Theria;Eutheria;Boreoeutheria;Euarchontoglires;Primates;Haplorrhini;Simiiformes;Catarrhini;Hominoidea;Hominidae;Homininae;Homo;Homo sapiens Homo sapiens species 2 | 9913 cellular organisms;Eukaryota;Opisthokonta;Metazoa;Eumetazoa;Bilateria;Deuterostomia;Chordata;Craniata;Vertebrata;Gnathostomata;Teleostomi;Euteleostomi;Sarcopterygii;Dipnotetrapodomorpha;Tetrapoda;Amniota;Mammalia;Theria;Eutheria;Boreoeutheria;Laurasiatheria;Artiodactyla;Ruminantia;Pecora;Bovidae;Bovinae;Bos;Bos taurus Bos taurus species 3 | -------------------------------------------------------------------------------- /man/mat_dist.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/b_analyse.R 3 | \name{mat_dist} 4 | \alias{mat_dist} 5 | \title{Calculate distance for otutab} 6 | \usage{ 7 | mat_dist(otutab, method = "bray", spe_nwk = NULL) 8 | } 9 | \arguments{ 10 | \item{otutab}{an otutab data.frame, samples are columns, taxs are rows.} 11 | 12 | \item{method}{Dissimilarity index, partial match to "bray", "euclidean"...see \code{\link[vegan]{vegdist};\link[picante]{unifrac}}} 13 | 14 | \item{spe_nwk}{a phylo tree if use unifrac...} 15 | } 16 | \value{ 17 | dist 18 | } 19 | \description{ 20 | Calculate distance for otutab 21 | } 22 | \examples{ 23 | data(otutab, package = "pcutils") 24 | mat_dist(otutab) 25 | } 26 | -------------------------------------------------------------------------------- /man/gene2id.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/additional.R 3 | \name{gene2id} 4 | \alias{gene2id} 5 | \title{Gene symbolid transfer to entrezIDs (human gene)} 6 | \usage{ 7 | gene2id(genes) 8 | } 9 | \arguments{ 10 | \item{genes}{gene symbols e.g:ASGR2} 11 | } 12 | \value{ 13 | gene entrezIDs dataframe 14 | } 15 | \description{ 16 | Gene symbolid transfer to entrezIDs (human gene) 17 | } 18 | \examples{ 19 | if (requireNamespace("AnnotationDbi") && requireNamespace("org.Hs.eg.db")) { 20 | genes <- c( 21 | "ASGR2", "BEST1", "SIGLEC16", "ECRP", "C1QC", "TCN2", "RNASE2", 22 | "DYSF", "C1QB", "FAM20A", "FCGR1A", "CR1", "HP", "VSIG4", "EGR1" 23 | ) 24 | gene2id(genes) -> geneid 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /man/time_by_cm.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/diff_analyse.R 3 | \name{time_by_cm} 4 | \alias{time_by_cm} 5 | \title{Time series analysis} 6 | \usage{ 7 | time_by_cm(otu_time, n_cluster = 6, min.std = 0) 8 | } 9 | \arguments{ 10 | \item{otu_time}{otutab hebing by a time variable} 11 | 12 | \item{n_cluster}{number of clusters} 13 | 14 | \item{min.std}{min.std} 15 | } 16 | \value{ 17 | time_cm 18 | } 19 | \description{ 20 | Time series analysis 21 | } 22 | \examples{ 23 | \donttest{ 24 | if (interactive()) { 25 | data(otutab, package = "pcutils") 26 | otu_time <- pcutils::hebing(otutab, metadata$Group) 27 | time_by_cm(otu_time, n_cluster = 4) -> time_cm_res 28 | plot(time_cm_res) 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /man/rare_curve_sample.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/rarefy.R 3 | \name{rare_curve_sample} 4 | \alias{rare_curve_sample} 5 | \alias{plot.rare_res} 6 | \title{Rare the sample} 7 | \usage{ 8 | rare_curve_sample(otutab, rep = 30, count_cutoff = 1) 9 | 10 | \method{plot}{rare_res}(x, ...) 11 | } 12 | \arguments{ 13 | \item{otutab}{otutab} 14 | 15 | \item{rep}{repeats number} 16 | 17 | \item{count_cutoff}{cutoff to be 0} 18 | 19 | \item{x}{AOR object} 20 | 21 | \item{...}{add} 22 | } 23 | \value{ 24 | ggplot 25 | 26 | ggplot 27 | } 28 | \description{ 29 | Rare the sample 30 | 31 | Plot a rare curve 32 | } 33 | \examples{ 34 | data(otutab, package = "pcutils") 35 | a <- rare_curve_sample(otutab) 36 | plot(a) 37 | } 38 | -------------------------------------------------------------------------------- /man/convert_taxon_name.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/phylogenetic.R 3 | \name{convert_taxon_name} 4 | \alias{convert_taxon_name} 5 | \title{Convert taxon names between Chinese and Latin} 6 | \usage{ 7 | convert_taxon_name(input_names, mode = "latin_to_chinese", fuzzy = FALSE) 8 | } 9 | \arguments{ 10 | \item{input_names}{input names} 11 | 12 | \item{mode}{conversion mode, "latin_to_chinese" or "chinese_to_latin"} 13 | 14 | \item{fuzzy}{whether to use fuzzy matching, default is FALSE} 15 | } 16 | \value{ 17 | character vector of converted names 18 | } 19 | \description{ 20 | Convert taxon names between Chinese and Latin 21 | } 22 | \examples{ 23 | convert_taxon_name(c("Escherichia coli", "Clostridioides difficile")) 24 | } 25 | -------------------------------------------------------------------------------- /man/check_taxonkit.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/Rtaxonkit.R 3 | \name{check_taxonkit} 4 | \alias{check_taxonkit} 5 | \title{Check taxonkit} 6 | \usage{ 7 | check_taxonkit(print = TRUE) 8 | } 9 | \arguments{ 10 | \item{print}{print} 11 | } 12 | \value{ 13 | taxonkit path 14 | } 15 | \description{ 16 | Check taxonkit 17 | } 18 | \seealso{ 19 | Other Rtaxonkit: 20 | \code{\link{download_taxonkit_dataset}()}, 21 | \code{\link{install_taxonkit}()}, 22 | \code{\link{name_or_id2df}()}, 23 | \code{\link{taxonkit_filter}()}, 24 | \code{\link{taxonkit_lca}()}, 25 | \code{\link{taxonkit_lineage}()}, 26 | \code{\link{taxonkit_list}()}, 27 | \code{\link{taxonkit_name2taxid}()}, 28 | \code{\link{taxonkit_reformat}()} 29 | } 30 | \concept{Rtaxonkit} 31 | -------------------------------------------------------------------------------- /man/pc_otu.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/pc_otu.R 3 | \name{pc_otu} 4 | \alias{pc_otu} 5 | \title{Create a pc_otu class object} 6 | \usage{ 7 | pc_otu(otutab = data.frame(), metadata = data.frame(), taxonomy = NULL, ...) 8 | } 9 | \arguments{ 10 | \item{otutab}{an otutab data.frame, samples are columns, taxs are rows.} 11 | 12 | \item{metadata}{a metadata data.frame, samples are rows} 13 | 14 | \item{taxonomy}{a taxomomy data.frame, look out the rowname of taxonomy and otutab should matched!} 15 | 16 | \item{...}{add} 17 | } 18 | \value{ 19 | pc_otu 20 | } 21 | \description{ 22 | Create a pc_otu class object 23 | } 24 | \examples{ 25 | data(otutab, package = "pcutils") 26 | pc_tax1 <- pc_otu(otutab, metadata) 27 | print(pc_tax1) 28 | } 29 | -------------------------------------------------------------------------------- /man/ALDEX.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/diff_analyse.R 3 | \name{ALDEX} 4 | \alias{ALDEX} 5 | \title{ALDEX} 6 | \usage{ 7 | ALDEX(otutab, group_df) 8 | } 9 | \arguments{ 10 | \item{otutab}{otutab} 11 | 12 | \item{group_df}{a dataframe with rowname same to dist and one group column} 13 | } 14 | \value{ 15 | diff 16 | } 17 | \description{ 18 | ALDEX 19 | } 20 | \examples{ 21 | \donttest{ 22 | if (requireNamespace("ALDEx2")) { 23 | data(otutab, package = "pcutils") 24 | ALDEX(otutab, metadata["Group"]) -> res 25 | res \%>\% 26 | dplyr::top_n(9, -glm.eBH) \%>\% 27 | .[, "tax"] -> sig 28 | data.frame(t(otutab[sig, ])) \%>\% pcutils::group_box(., "Group", metadata) 29 | } 30 | } 31 | } 32 | \references{ 33 | \url{https://cloud.tencent.com/developer/article/1621879} 34 | } 35 | -------------------------------------------------------------------------------- /man/get_all_sp_la_zh_name.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/phylogenetic.R 3 | \name{get_all_sp_la_zh_name} 4 | \alias{get_all_sp_la_zh_name} 5 | \title{get all species Latin and Chinese name from the CCTCC database} 6 | \usage{ 7 | get_all_sp_la_zh_name( 8 | download_dir = "~/Documents/", 9 | each_verbose = FALSE, 10 | max_requests = 50, 11 | max_id = 30609, 12 | failure_ids = NULL 13 | ) 14 | } 15 | \arguments{ 16 | \item{download_dir}{default} 17 | 18 | \item{each_verbose}{each_verbose} 19 | 20 | \item{max_requests}{default 50} 21 | 22 | \item{max_id}{default 30609, try to make sure on the website} 23 | 24 | \item{failure_ids}{failure_ids} 25 | } 26 | \value{ 27 | No value 28 | } 29 | \description{ 30 | get all species Latin and Chinese name from the CCTCC database 31 | } 32 | -------------------------------------------------------------------------------- /R/utils-pipe.R: -------------------------------------------------------------------------------- 1 | #' Pipe operator 2 | #' 3 | #' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. 4 | #' 5 | #' @name %>% 6 | #' @rdname pipe 7 | #' @keywords internal 8 | #' @export 9 | #' @importFrom magrittr %>% 10 | #' @usage lhs \%>\% rhs 11 | #' @param lhs A value or the magrittr placeholder. 12 | #' @param rhs A function call using the magrittr semantics. 13 | #' @return The result of calling `rhs(lhs)`. 14 | NULL 15 | 16 | #' Assignment pipe 17 | #' 18 | #' See \code{magrittr::\link[magrittr:pipe]{\%<>\%}} for details. 19 | #' 20 | #' @name %<>% 21 | #' @keywords internal 22 | #' @export 23 | #' @importFrom magrittr %<>% 24 | #' @usage lhs \%<>\% rhs 25 | #' @param lhs A value or the magrittr placeholder. 26 | #' @param rhs A function call using the magrittr semantics. 27 | #' @return The result of calling `rhs(lhs)`. 28 | NULL 29 | -------------------------------------------------------------------------------- /man/multi_bar.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/diff_analyse.R 3 | \name{multi_bar} 4 | \alias{multi_bar} 5 | \title{Difference analysis} 6 | \usage{ 7 | multi_bar( 8 | otutab, 9 | group_df, 10 | mode = 1, 11 | text_df = NULL, 12 | text_x = NULL, 13 | text_angle = -90, 14 | errorbar = "bottom" 15 | ) 16 | } 17 | \arguments{ 18 | \item{otutab}{otutab} 19 | 20 | \item{group_df}{a dataframe with rowname same to dist and one group column} 21 | 22 | \item{mode}{1~2} 23 | 24 | \item{text_df}{text_df} 25 | 26 | \item{text_x}{text_x} 27 | 28 | \item{text_angle}{text_angle} 29 | 30 | \item{errorbar}{top, bottom, none} 31 | } 32 | \value{ 33 | ggplot 34 | } 35 | \description{ 36 | Difference analysis 37 | } 38 | \examples{ 39 | data(otutab, package = "pcutils") 40 | multi_bar(otutab[1:10, ], metadata["Group"]) 41 | } 42 | -------------------------------------------------------------------------------- /man/micro_sbatch.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/other_software.R 3 | \name{micro_sbatch} 4 | \alias{micro_sbatch} 5 | \title{Microbiome sbatch} 6 | \usage{ 7 | micro_sbatch( 8 | work_dir = "/share/home/jianglab/pengchen/work/asthma/", 9 | step = "fastp", 10 | all_sample_num = 40, 11 | array = 1, 12 | partition = "cpu", 13 | cpus_per_task = 1, 14 | mem_per_cpu = "2G" 15 | ) 16 | } 17 | \arguments{ 18 | \item{work_dir}{work_dir} 19 | 20 | \item{step}{"fastp","rm_human","megahit","prodigal","salmon-quant",...} 21 | 22 | \item{all_sample_num}{all sample number} 23 | 24 | \item{array}{array number} 25 | 26 | \item{partition}{partition} 27 | 28 | \item{cpus_per_task}{cpus_per_task} 29 | 30 | \item{mem_per_cpu}{mem_per_cpu, "2G"} 31 | } 32 | \value{ 33 | No value 34 | } 35 | \description{ 36 | Microbiome sbatch 37 | } 38 | -------------------------------------------------------------------------------- /man/procrustes_analyse.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/b_analyse.R 3 | \name{procrustes_analyse} 4 | \alias{procrustes_analyse} 5 | \title{Procrustes Rotation of Two Configurations and PROTEST} 6 | \usage{ 7 | procrustes_analyse(b_res1, b_res2, nperm = 999, ...) 8 | } 9 | \arguments{ 10 | \item{b_res1}{Target matrix} 11 | 12 | \item{b_res2}{Matrix to be rotated} 13 | 14 | \item{nperm}{numbers of permutations to perform} 15 | 16 | \item{...}{additional} 17 | } 18 | \value{ 19 | pro_res 20 | } 21 | \description{ 22 | Procrustes Rotation of Two Configurations and PROTEST 23 | } 24 | \examples{ 25 | data(otutab, package = "pcutils") 26 | b_analyse(otutab, method = "pca") -> b_res1 27 | b_analyse(otutab * abs(rnorm(10)), method = "pca") -> b_res2 28 | pro_res <- procrustes_analyse(b_res1, b_res2) 29 | plot(pro_res, "Group", metadata) 30 | } 31 | -------------------------------------------------------------------------------- /man/df2tree.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/phylogenetic.R 3 | \name{df2tree} 4 | \alias{df2tree} 5 | \title{From a dataframe to construct a phylo} 6 | \usage{ 7 | df2tree(data, edge_df = FALSE, ignore_pattern = NULL) 8 | } 9 | \arguments{ 10 | \item{data}{dataframe} 11 | 12 | \item{edge_df}{if the data is edge_df ?} 13 | 14 | \item{ignore_pattern}{An optional regular expression pattern to match tip or node labels for dropping.} 15 | } 16 | \value{ 17 | phylo object 18 | } 19 | \description{ 20 | NOTE: this function will do \code{before_tree} first. 21 | } 22 | \examples{ 23 | data(otutab, package = "pcutils") 24 | df2tree(taxonomy) -> tax_tree 25 | print(tax_tree) 26 | # check all nodes matched! 27 | if (requireNamespace("picante")) { 28 | picante::match.phylo.comm(tax_tree, t(otutab)) -> nn 29 | nrow(nn$comm) == nrow(t(otutab)) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /man/grap_p_test.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/b_analyse.R 3 | \name{grap_p_test} 4 | \alias{grap_p_test} 5 | \title{Performs graph-based permutation tests} 6 | \usage{ 7 | grap_p_test(otutab, metadata, group = "Group", nperm = 999, ...) 8 | } 9 | \arguments{ 10 | \item{otutab}{an otutab data.frame, samples are columns, taxs are rows.} 11 | 12 | \item{metadata}{metadata} 13 | 14 | \item{group}{one group name in columns of metadata} 15 | 16 | \item{nperm}{numbers of permutations to perform} 17 | 18 | \item{...}{additional} 19 | } 20 | \value{ 21 | ggplot 22 | } 23 | \description{ 24 | Performs graph-based permutation tests 25 | } 26 | \examples{ 27 | \donttest{ 28 | if (requireNamespace("phyloseqGraphTest") && requireNamespace("phyloseq")) { 29 | data(otutab, package = "pcutils") 30 | grap_p_test(otutab, metadata, "Group") 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /man/volcano_p.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/diff_analyse.R 3 | \name{volcano_p} 4 | \alias{volcano_p} 5 | \title{Volcano plot for difference analysis} 6 | \usage{ 7 | volcano_p( 8 | res, 9 | logfc = 1, 10 | adjp = 0.05, 11 | text = TRUE, 12 | repel = TRUE, 13 | mode = 1, 14 | number = FALSE 15 | ) 16 | } 17 | \arguments{ 18 | \item{res}{result of \code{diff_da} which have colnames: tax, log2FoldChange, padj, compare, sig} 19 | 20 | \item{logfc}{log_fold_change threshold} 21 | 22 | \item{adjp}{adjust_p_value threshold} 23 | 24 | \item{text}{text, TRUE} 25 | 26 | \item{repel}{repel, TRUE} 27 | 28 | \item{mode}{1:normal; 2:multi_contrast} 29 | 30 | \item{number}{show the tax number} 31 | } 32 | \value{ 33 | ggplot 34 | } 35 | \description{ 36 | Volcano plot for difference analysis 37 | } 38 | \seealso{ 39 | \code{\link{diff_da}} 40 | } 41 | -------------------------------------------------------------------------------- /man/m_group_env.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/additional.R 3 | \name{m_group_env} 4 | \alias{m_group_env} 5 | \alias{plot.mant_g} 6 | \title{Multi-table test with env} 7 | \usage{ 8 | m_group_env(g_otutab, env) 9 | 10 | \method{plot}{mant_g}(x, ...) 11 | } 12 | \arguments{ 13 | \item{g_otutab}{multi-otutabs with first column is group} 14 | 15 | \item{env}{environmental factors} 16 | 17 | \item{x}{mant_g object} 18 | 19 | \item{...}{add} 20 | } 21 | \value{ 22 | a mant_g object 23 | 24 | a ggplot 25 | } 26 | \description{ 27 | Multi-table test with env 28 | 29 | Plot mant_g object 30 | } 31 | \examples{ 32 | if (requireNamespace("linkET")) { 33 | data(otutab, package = "pcutils") 34 | cbind(group = rep(c("a", "b", "c"), c(200, 100, 185)), otutab) -> g_otutab 35 | metadata[, 3:8, drop = FALSE] -> env 36 | m_group_env(g_otutab, env) -> mant_g 37 | plot(mant_g) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /man/rare_curve_species.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/rarefy.R 3 | \name{rare_curve_species} 4 | \alias{rare_curve_species} 5 | \title{Rare the species} 6 | \usage{ 7 | rare_curve_species( 8 | otutab, 9 | step = 2000, 10 | method = "richness", 11 | mode = 2, 12 | reps = 3, 13 | threads = 1, 14 | verbose = TRUE 15 | ) 16 | } 17 | \arguments{ 18 | \item{otutab}{otutab} 19 | 20 | \item{step}{default 2000} 21 | 22 | \item{method}{one of "richness","chao1","ace","gc","shannon","simpson","pd","pielou"} 23 | 24 | \item{mode}{1 for little table, 2 for big} 25 | 26 | \item{reps}{reps} 27 | 28 | \item{threads}{use how many threads to calculate (default:1)} 29 | 30 | \item{verbose}{verbose} 31 | } 32 | \value{ 33 | ggplot 34 | } 35 | \description{ 36 | Rare the species 37 | } 38 | \examples{ 39 | data(otutab, package = "pcutils") 40 | a <- rare_curve_species(otutab, mode = 1) 41 | plot(a) 42 | } 43 | -------------------------------------------------------------------------------- /man/install_taxonkit.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/Rtaxonkit.R 3 | \name{install_taxonkit} 4 | \alias{install_taxonkit} 5 | \title{Install taxonkit} 6 | \usage{ 7 | install_taxonkit(make_sure = FALSE, taxonkit_tar_gz = NULL) 8 | } 9 | \arguments{ 10 | \item{make_sure}{make sure to do this} 11 | 12 | \item{taxonkit_tar_gz}{your download taxonkit_tar_gz file from https://github.com/shenwei356/taxonkit/releases/} 13 | } 14 | \value{ 15 | No value 16 | } 17 | \description{ 18 | Install taxonkit 19 | } 20 | \seealso{ 21 | Other Rtaxonkit: 22 | \code{\link{check_taxonkit}()}, 23 | \code{\link{download_taxonkit_dataset}()}, 24 | \code{\link{name_or_id2df}()}, 25 | \code{\link{taxonkit_filter}()}, 26 | \code{\link{taxonkit_lca}()}, 27 | \code{\link{taxonkit_lineage}()}, 28 | \code{\link{taxonkit_list}()}, 29 | \code{\link{taxonkit_name2taxid}()}, 30 | \code{\link{taxonkit_reformat}()} 31 | } 32 | \concept{Rtaxonkit} 33 | -------------------------------------------------------------------------------- /man/download_taxonkit_dataset.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/Rtaxonkit.R 3 | \name{download_taxonkit_dataset} 4 | \alias{download_taxonkit_dataset} 5 | \title{Download taxonkit dataset} 6 | \usage{ 7 | download_taxonkit_dataset(make_sure = FALSE, taxdump_tar_gz = NULL) 8 | } 9 | \arguments{ 10 | \item{make_sure}{make sure to do this} 11 | 12 | \item{taxdump_tar_gz}{your download taxdump_tar_gz file from https://ftp.ncbi.nih.gov/pub/taxonomy/taxdump.tar.gz} 13 | } 14 | \value{ 15 | No value 16 | } 17 | \description{ 18 | Download taxonkit dataset 19 | } 20 | \seealso{ 21 | Other Rtaxonkit: 22 | \code{\link{check_taxonkit}()}, 23 | \code{\link{install_taxonkit}()}, 24 | \code{\link{name_or_id2df}()}, 25 | \code{\link{taxonkit_filter}()}, 26 | \code{\link{taxonkit_lca}()}, 27 | \code{\link{taxonkit_lineage}()}, 28 | \code{\link{taxonkit_list}()}, 29 | \code{\link{taxonkit_name2taxid}()}, 30 | \code{\link{taxonkit_reformat}()} 31 | } 32 | \concept{Rtaxonkit} 33 | -------------------------------------------------------------------------------- /man/add_strip.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/phylogenetic.R 3 | \name{add_strip} 4 | \alias{add_strip} 5 | \title{add strips for a tree plot} 6 | \usage{ 7 | add_strip(trp, some_tax, flat_n = 5, strip_params = NULL) 8 | } 9 | \arguments{ 10 | \item{trp}{tree plot from \code{ggtree}} 11 | 12 | \item{some_tax}{some tax you want to add strip} 13 | 14 | \item{flat_n}{flat the text when taxa number more than \code{flat_n}.} 15 | 16 | \item{strip_params}{parameters parse to \code{\link[ggtree]{geom_strip}}} 17 | } 18 | \value{ 19 | tree plot 20 | } 21 | \description{ 22 | add strips for a tree plot 23 | } 24 | \examples{ 25 | \donttest{ 26 | data(otutab, package = "pcutils") 27 | # run yourself 28 | if (interactive()) { 29 | ann_tree(taxonomy, otutab) -> tree 30 | easy_tree(tree) -> p 31 | some_tax <- table(taxonomy$Phylum) \%>\% 32 | sort(decreasing = TRUE) \%>\% 33 | head(5) \%>\% 34 | names() 35 | add_strip(p, some_tax) 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /R/pctax-package.R: -------------------------------------------------------------------------------- 1 | #' @keywords internal 2 | "_PACKAGE" 3 | 4 | 5 | ## usethis namespace: start 6 | #' @importFrom utils data 7 | #' @importFrom utils head tail 8 | #' @importFrom stats aggregate median var sd setNames runif relevel time na.omit kmeans anova t.test 9 | #' @importFrom tibble column_to_rownames 10 | #' @importFrom tibble rownames_to_column 11 | #' @importFrom reshape2 melt 12 | #' @importFrom pcutils lib_ps hebing trans dabiao generate_labels update_param get_cols t2 change_fac_lev 13 | #' @import dplyr 14 | #' @import ggplot2 15 | ## usethis namespace: end 16 | NULL 17 | 18 | ## quiets concerns of R CMD check re: the .'s that appear in pipelines 19 | if (getRversion() >= "2.15.1") utils::globalVariables(c(".")) 20 | 21 | pctax_theme <- { 22 | ggplot2::theme_classic(base_size = 13) + 23 | ggplot2::theme( 24 | axis.text = ggplot2::element_text(color = "black"), 25 | plot.margin = grid::unit(rep(0.5, 4), "lines"), 26 | strip.background = ggplot2::element_rect(fill = NA) 27 | ) 28 | } 29 | -------------------------------------------------------------------------------- /man/permanova2.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/additional.R 3 | \name{permanova2} 4 | \alias{permanova2} 5 | \title{Permanova between a otutab and a variable (added \code{two})} 6 | \usage{ 7 | permanova2( 8 | otutab, 9 | envs, 10 | norm = TRUE, 11 | each = TRUE, 12 | method = "adonis", 13 | dist = "bray", 14 | two = FALSE, 15 | nperm = 999, 16 | ... 17 | ) 18 | } 19 | \arguments{ 20 | \item{otutab}{an otutab data.frame, samples are columns, taxs are rows.} 21 | 22 | \item{envs}{factors need to test} 23 | 24 | \item{norm}{should normalize?(default:TRUE)} 25 | 26 | \item{each}{test factor one by one, rather than whole} 27 | 28 | \item{method}{adonis/mrpp/anosim/mantel} 29 | 30 | \item{dist}{if use pcoa or nmds, your can choose a dist method (default: bray)} 31 | 32 | \item{two}{two by two adonis test} 33 | 34 | \item{nperm}{numbers of permutations to perform} 35 | 36 | \item{...}{additional} 37 | } 38 | \description{ 39 | Permanova between a otutab and a variable (added \code{two}) 40 | } 41 | -------------------------------------------------------------------------------- /man/sangji_plot.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/additional.R 3 | \name{sangji_plot} 4 | \alias{sangji_plot} 5 | \title{Plot a sankey} 6 | \usage{ 7 | sangji_plot( 8 | tree, 9 | top_N = 5, 10 | notshow = c(), 11 | intermediate = FALSE, 12 | width = 3000, 13 | height = 500, 14 | ... 15 | ) 16 | } 17 | \arguments{ 18 | \item{tree}{result from \code{\link{ann_tree}}} 19 | 20 | \item{top_N}{each level has top_N} 21 | 22 | \item{notshow}{some words you don't want to show} 23 | 24 | \item{intermediate}{logical, show the intermediate rank} 25 | 26 | \item{width}{width} 27 | 28 | \item{height}{height} 29 | 30 | \item{...}{look for parameters in \code{\link[sankeyD3]{sankeyNetwork}}} 31 | } 32 | \value{ 33 | html widget 34 | } 35 | \description{ 36 | Plot a sankey 37 | } 38 | \examples{ 39 | \donttest{ 40 | if (requireNamespace("sankeyD3") && requireNamespace("tidytree")) { 41 | data(otutab, package = "pcutils") 42 | ann_tree(taxonomy[, c(1, 5, 6, 7)], otutab) -> tree 43 | sangji_plot(tree) 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /man/as.b_dist.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/b_analyse.R 3 | \name{as.b_dist} 4 | \alias{as.b_dist} 5 | \alias{plot.dist} 6 | \alias{plot.b_dist} 7 | \title{Transfer dist to b_dist} 8 | \usage{ 9 | as.b_dist(dist, group_df = NULL) 10 | 11 | \method{plot}{dist}(x, group_df = NULL, ...) 12 | 13 | \method{plot}{b_dist}(x, mode = 1, c_group = "inter", ...) 14 | } 15 | \arguments{ 16 | \item{dist}{a dist object} 17 | 18 | \item{group_df}{a dataframe with rowname same to dist and one group column} 19 | 20 | \item{x}{a b_dist} 21 | 22 | \item{...}{additional} 23 | 24 | \item{mode}{1~3} 25 | 26 | \item{c_group}{"inter" or "intra" or both to plot} 27 | } 28 | \value{ 29 | a b_dist with annotation by group 30 | 31 | a pheatmap 32 | 33 | a ggplot or pheatmap 34 | } 35 | \description{ 36 | Transfer dist to b_dist 37 | 38 | Plot dist 39 | 40 | Plot b_dist 41 | } 42 | \examples{ 43 | data(otutab, package = "pcutils") 44 | mat_dist(otutab) \%>\% as.b_dist(., group_df = metadata["Group"]) -> aa 45 | plot(aa) 46 | plot(aa, mode = 2) 47 | } 48 | -------------------------------------------------------------------------------- /R/data.R: -------------------------------------------------------------------------------- 1 | #' @title test data (pc_otu class) for pc_tax package. 2 | #' @description an otutab, metadata and a taxonomy table. 3 | #' 4 | #' @docType data 5 | #' @name pc_tax1 6 | #' @format a pc_otu contains an otutab, metadata and a taxonomy table. 7 | #' \describe{ 8 | #' \item{tbls}{contians otutable rawdata} 9 | #' \item{metas}{contians metadata} 10 | #' \item{otus}{contians taxomomy table} 11 | #' } 12 | NULL 13 | 14 | #' @title all element cycle information. 15 | #' @description all element cycle information. 16 | #' 17 | #' @docType data 18 | #' @name all_ec_info 19 | #' @format a list contains four tables. 20 | #' \describe{ 21 | #' \item{ec_node}{chemicals} 22 | #' \item{ec_link}{reactions} 23 | #' \item{ec_gene}{genes} 24 | #' \item{ec_path}{reactions labels} 25 | #' } 26 | NULL 27 | 28 | #' @title all species latin names and chinese names 29 | #' @description all species latin names and chinese names. 30 | #' 31 | #' @docType data 32 | #' @name all_sp_la_zh_name 33 | #' @format a dataframe. 34 | #' \describe{ 35 | #' \item{latin_name}{latin name} 36 | #' \item{chinese_name}{chinese name} 37 | #' } 38 | NULL 39 | -------------------------------------------------------------------------------- /man/diff_da.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/diff_analyse.R 3 | \name{diff_da} 4 | \alias{diff_da} 5 | \title{Difference analysis} 6 | \usage{ 7 | diff_da( 8 | otutab, 9 | group_df, 10 | ctrl = NULL, 11 | method = "deseq2", 12 | log = TRUE, 13 | add_mini = NULL, 14 | ... 15 | ) 16 | } 17 | \arguments{ 18 | \item{otutab}{otutab} 19 | 20 | \item{group_df}{a dataframe with rowname same to dist and one group column} 21 | 22 | \item{ctrl}{the control group, one level of groups} 23 | 24 | \item{method}{one of "deseq2","edger","limma","t.test","wilcox.test"} 25 | 26 | \item{log}{do log transfer for limma?} 27 | 28 | \item{add_mini}{add_mini when calculate the logFC. e.g (10+0.1)/(0+0.1), default \code{0.5*min(abundance)}} 29 | 30 | \item{...}{other parameters} 31 | } 32 | \value{ 33 | a dataframe 34 | } 35 | \description{ 36 | Difference analysis 37 | } 38 | \examples{ 39 | \donttest{ 40 | if (requireNamespace("limma")) { 41 | data(otutab, package = "pcutils") 42 | diff_da(otutab, metadata["Group"], method = "limma") -> res 43 | volcano_p(res) 44 | volcano_p(res, mode = 2) 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /man/pctax-package.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/pctax-package.R 3 | \docType{package} 4 | \name{pctax-package} 5 | \alias{pctax} 6 | \alias{pctax-package} 7 | \title{pctax: Professional Comprehensive Omics Data Analysis} 8 | \description{ 9 | Provides a comprehensive suite of tools for analyzing omics data. It includes functionalities for alpha diversity analysis, beta diversity analysis, differential abundance analysis, community assembly analysis, visualization of phylogenetic tree, and functional enrichment analysis. With a progressive approach, the package offers a range of analysis methods to explore and understand the complex communities. It is designed to support researchers and practitioners in conducting in-depth and professional omics data analysis. 10 | } 11 | \seealso{ 12 | Useful links: 13 | \itemize{ 14 | \item \url{https://github.com/Asa12138/pctax} 15 | \item Report bugs at \url{https://github.com/Asa12138/pctax/issues} 16 | } 17 | 18 | } 19 | \author{ 20 | \strong{Maintainer}: Chen Peng \email{pengchen2001@zju.edu.cn} (\href{https://orcid.org/0000-0002-9449-7606}{ORCID}) 21 | 22 | } 23 | \keyword{internal} 24 | -------------------------------------------------------------------------------- /_pkgdown.yml: -------------------------------------------------------------------------------- 1 | url: ~ 2 | template: 3 | params: 4 | bootswatch: cosmo 5 | docsearch: 6 | api_key: 599cec31baffa4868cae4e79f180729b 7 | index_name: docsearch 8 | 9 | navbar: 10 | title: "pctax" 11 | left: 12 | - icon: fa-home fa-lg 13 | href: index.html 14 | - text: "Vignettes" 15 | href: articles/index.html 16 | - text: "Functions" 17 | href: reference/index.html 18 | - text: "News" 19 | href: news/index.html 20 | - text: "Other Packages" 21 | menu: 22 | - text: "pcutils" 23 | href: https://asa12138.github.io/pcutils 24 | - text: "pctax" 25 | href: https://asa12138.github.io/pctax 26 | - text: "ReporterScore" 27 | href: https://asa12138.github.io/ReporterScore 28 | - text: "MetaNet" 29 | href: https://asa12138.github.io/MetaNet 30 | - text: "iPhylo" 31 | href: https://iphylo.net 32 | - text: "plot4fun" 33 | href: https://asa12138.github.io/plot4fun 34 | - text: "Asa_web" 35 | href: https://asa12138.github.io/ 36 | right: 37 | - icon: fa-github 38 | href: https://github.com/Asa12138/pctax 39 | -------------------------------------------------------------------------------- /man/batch_mediate.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/stats.R 3 | \name{batch_mediate} 4 | \alias{batch_mediate} 5 | \title{Batch mediate} 6 | \usage{ 7 | batch_mediate(data, mediator_df, nsims = 500, conf.level = 0.95) 8 | } 9 | \arguments{ 10 | \item{data}{data.frame with two columns: X (independent variable) and Y (dependent variable).} 11 | 12 | \item{mediator_df}{data.frame with mediators, each column representing a different mediator variable.} 13 | 14 | \item{nsims}{Number of bootstrap simulations for estimating confidence intervals (default is 500).} 15 | 16 | \item{conf.level}{Confidence level for the confidence intervals (default is 0.95).} 17 | } 18 | \value{ 19 | data.frame 20 | } 21 | \description{ 22 | Batch mediate 23 | } 24 | \examples{ 25 | set.seed(123) 26 | n <- 200 27 | X <- rnorm(n) 28 | M1 <- 0.5 * X + rnorm(n) 29 | M2 <- 0.3 * X + rnorm(n) 30 | M3 <- 0.1 * X + rnorm(n) 31 | Y <- 0.3 * X + 0.4 * M1 + 0.2 * M2 + rnorm(n) 32 | data <- data.frame(X, Y) 33 | mediators <- data.frame(M1, M2, M3) 34 | if (requireNamespace("mediation")) { 35 | results <- batch_mediate(data, mediators) 36 | print(results) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /man/aor.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/rarefy.R 3 | \name{aor} 4 | \alias{aor} 5 | \alias{aor.data.frame} 6 | \alias{plot.AOR} 7 | \title{Calculate Abundance-occupancy_relationship} 8 | \usage{ 9 | aor(otutab, ...) 10 | 11 | \method{aor}{data.frame}( 12 | otutab, 13 | top_r = 0.7, 14 | ocup_n = ceiling(0.8 * ncol(otutab)), 15 | special_n = ceiling(0.1 * ncol(otutab)), 16 | ... 17 | ) 18 | 19 | \method{plot}{AOR}(x, ...) 20 | } 21 | \arguments{ 22 | \item{otutab}{otutab} 23 | 24 | \item{...}{add} 25 | 26 | \item{top_r}{percentage of top relative abundance} 27 | 28 | \item{ocup_n}{percentage of top occupied} 29 | 30 | \item{special_n}{how many occupancy define as specialists} 31 | 32 | \item{x}{AOR object} 33 | } 34 | \value{ 35 | AOR 36 | 37 | ggplot 38 | } 39 | \description{ 40 | Calculate Abundance-occupancy_relationship 41 | 42 | Plot a AOR 43 | } 44 | \examples{ 45 | data(otutab, package = "pcutils") 46 | aor(otutab) -> AOR 47 | plot(AOR) 48 | } 49 | \references{ 50 | Barberán, A., Bates, S. T., Casamayor, E. & Fierer, N. (2012) Using network analysis to explore co-occurrence patterns in soil microbial communities. 51 | } 52 | -------------------------------------------------------------------------------- /man/geo_sim.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/b_analyse.R 3 | \name{geo_sim} 4 | \alias{geo_sim} 5 | \title{Lm for sample similarity and geographical distance} 6 | \usage{ 7 | geo_sim(otutab, geo, method = "bray", spe_nwk = NULL, ...) 8 | } 9 | \arguments{ 10 | \item{otutab}{an otutab data.frame, samples are columns, taxs are rows.} 11 | 12 | \item{geo}{a two-columns dataframe, first is latitude, second is longitude} 13 | 14 | \item{method}{Dissimilarity index, partial match to "bray", "euclidean"...see \code{\link[vegan]{vegdist};\link[picante]{unifrac}}} 15 | 16 | \item{spe_nwk}{a phylo tree if use unifrac...} 17 | 18 | \item{...}{additional} 19 | } 20 | \value{ 21 | a ggplot 22 | } 23 | \description{ 24 | Lm for sample similarity and geographical distance 25 | } 26 | \examples{ 27 | if (requireNamespace("geosphere")) { 28 | library(ggplot2) 29 | data(otutab, package = "pcutils") 30 | metadata[, c("lat", "long")] -> geo 31 | geo_sim(otutab, geo) -> geo_res 32 | } 33 | } 34 | \references{ 35 | Graco-Roza, C. et al. (2022) Distance decay 2.0 - A global synthesis of taxonomic and functional turnover in ecological communities. Glob Ecol Biogeogr 31, 1399–1421. 36 | } 37 | -------------------------------------------------------------------------------- /man/nst.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/eco_micro.R 3 | \name{nst} 4 | \alias{nst} 5 | \title{Calculate NST for each group} 6 | \usage{ 7 | nst(otutab, group_df, threads = 1, file = NULL, rep = 20, save = FALSE) 8 | } 9 | \arguments{ 10 | \item{otutab}{an otutab data.frame, samples are columns, taxs are rows.} 11 | 12 | \item{group_df}{a dataframe with rowname and one group column} 13 | 14 | \item{threads}{default:4} 15 | 16 | \item{file}{filename to save} 17 | 18 | \item{rep}{repeat numbers: suggest 999} 19 | 20 | \item{save}{save the file} 21 | } 22 | \value{ 23 | a b_dist object, dis is MSTij 24 | } 25 | \description{ 26 | Calculate NST for each group 27 | } 28 | \examples{ 29 | \donttest{ 30 | if (requireNamespace("NST")) { 31 | library(ggplot2) 32 | data(otutab, package = "pcutils") 33 | nst(otutab, metadata["Group"]) -> nst_res 34 | plot(nst_res, c_group = "intra") + geom_hline(yintercept = 0.5, lty = 2) + ylab("NST") 35 | } 36 | } 37 | } 38 | \references{ 39 | Ning, D., Deng, Y., Tiedje, J. M. & Zhou, J. (2019) A general framework for quantitatively assessing ecological stochasticity. Proceedings of the National Academy of Sciences 116, 16892–16898. 40 | } 41 | -------------------------------------------------------------------------------- /man/a_diversity.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/a_diversity.R 3 | \name{a_diversity} 4 | \alias{a_diversity} 5 | \alias{a_diversity.data.frame} 6 | \alias{a_diversity.pc_otu} 7 | \alias{a_diversity.numeric} 8 | \title{Calculate a_diversity of otutab} 9 | \usage{ 10 | a_diversity(otutab, ...) 11 | 12 | \method{a_diversity}{data.frame}( 13 | otutab, 14 | method = c("richness", "shannon"), 15 | tree = NULL, 16 | digits = 4, 17 | ... 18 | ) 19 | 20 | \method{a_diversity}{pc_otu}(otutab, method = "all", tbl = "otutab", ...) 21 | 22 | \method{a_diversity}{numeric}(otutab, ...) 23 | } 24 | \arguments{ 25 | \item{otutab}{numeric} 26 | 27 | \item{...}{pass to \code{a_diversity.data.frame}} 28 | 29 | \item{method}{one of "all","richness","chao1","ace","gc","shannon","simpson","pd","pielou"} 30 | 31 | \item{tree}{a iphylo object match the rownames of otutab} 32 | 33 | \item{digits}{maintance how many digits} 34 | 35 | \item{tbl}{which table} 36 | } 37 | \value{ 38 | a a_res object 39 | } 40 | \description{ 41 | Calculate a_diversity of otutab 42 | } 43 | \examples{ 44 | data(otutab, package = "pcutils") 45 | a_diversity(otutab) -> a_res 46 | plot(a_res, "Group", metadata) 47 | } 48 | -------------------------------------------------------------------------------- /man/drop_tips_update.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/phylogenetic.R 3 | \name{drop_tips_update} 4 | \alias{drop_tips_update} 5 | \title{Drop Tips and Update a Phylogenetic Tree} 6 | \usage{ 7 | drop_tips_update(tr, drop_name, pattern = NULL) 8 | } 9 | \arguments{ 10 | \item{tr}{A phylogenetic tree of class \code{phylo}.} 11 | 12 | \item{drop_name}{A character vector of tip or node names to drop. 13 | If missing and \code{pattern} is provided, names matching the pattern will be dropped.} 14 | 15 | \item{pattern}{An optional regular expression pattern to match tip or node labels for dropping.} 16 | } 17 | \value{ 18 | A \code{phylo} object with specified tips removed. 19 | } 20 | \description{ 21 | This function iteratively removes specified tips (or tips matching a pattern) 22 | from a phylogenetic tree without collapsing internal nodes or singleton nodes. 23 | } 24 | \examples{ 25 | if (requireNamespace("ape")) { 26 | library(ape) 27 | tr <- rtree(10) 28 | plot(tr) 29 | # Drop tips containing "t1" or "t2" in their label 30 | tr2 <- drop_tips_update(tr, pattern = "t1|t2") 31 | plot(tr2) 32 | 33 | # Alternatively, specify tips directly 34 | tr3 <- drop_tips_update(tr, drop_name = c("t3", "t5")) 35 | plot(tr3) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /man/ncm.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/eco_micro.R 3 | \name{ncm} 4 | \alias{ncm} 5 | \alias{plot.ncm_res} 6 | \title{Sloan Neutral Model} 7 | \usage{ 8 | ncm(otutab, model = "nls") 9 | 10 | \method{plot}{ncm_res}( 11 | x, 12 | mycols = c(Above = "#069870", Below = "#e29e02", In = "#1e353a"), 13 | text_position = NULL, 14 | pie_text_params = list(size = 2.5), 15 | ... 16 | ) 17 | } 18 | \arguments{ 19 | \item{otutab}{an otutab data.frame, samples are columns, taxs are rows.} 20 | 21 | \item{model}{fit method, one of "nls","mle"} 22 | 23 | \item{x}{a ncm_res object} 24 | 25 | \item{mycols}{mycols} 26 | 27 | \item{text_position}{text_position} 28 | 29 | \item{pie_text_params}{pie text parameters} 30 | 31 | \item{...}{add} 32 | } 33 | \value{ 34 | ncm_res 35 | 36 | ggplot 37 | } 38 | \description{ 39 | Sloan Neutral Model 40 | 41 | Plot ncm_res 42 | } 43 | \examples{ 44 | \donttest{ 45 | if (requireNamespace("Hmisc") && requireNamespace("minpack.lm")) { 46 | data(otutab, package = "pcutils") 47 | ncm(otutab) -> ncm_res 48 | plot(ncm_res) 49 | } 50 | } 51 | } 52 | \references{ 53 | Sloan, W. TRUE. et al. (2006) Quantifying the roles of immigration and chance in shaping prokaryote community structure. Environmental Microbiology 8, 732–740. 54 | } 55 | -------------------------------------------------------------------------------- /man/name_or_id2df.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/Rtaxonkit.R 3 | \name{name_or_id2df} 4 | \alias{name_or_id2df} 5 | \title{Transfer taxon name or taxid to the lineage dataframe} 6 | \usage{ 7 | name_or_id2df( 8 | name_or_id, 9 | mode = "name", 10 | add_prefix = TRUE, 11 | fill_miss_rank = TRUE, 12 | data_dir = NULL 13 | ) 14 | } 15 | \arguments{ 16 | \item{name_or_id}{name or taxid} 17 | 18 | \item{mode}{"id" or "name"} 19 | 20 | \item{add_prefix}{add_prefix} 21 | 22 | \item{fill_miss_rank}{fill_miss_rank} 23 | 24 | \item{data_dir}{directory containing nodes.dmp and names.dmp (default "/Users/asa/.taxonkit")} 25 | } 26 | \value{ 27 | dataframe 28 | } 29 | \description{ 30 | Transfer taxon name or taxid to the lineage dataframe 31 | } 32 | \examples{ 33 | \dontrun{ 34 | name_or_id2df(c("Homo sapiens", "Akkermansia muciniphila ATCC BAA-835")) 35 | } 36 | } 37 | \seealso{ 38 | Other Rtaxonkit: 39 | \code{\link{check_taxonkit}()}, 40 | \code{\link{download_taxonkit_dataset}()}, 41 | \code{\link{install_taxonkit}()}, 42 | \code{\link{taxonkit_filter}()}, 43 | \code{\link{taxonkit_lca}()}, 44 | \code{\link{taxonkit_lineage}()}, 45 | \code{\link{taxonkit_list}()}, 46 | \code{\link{taxonkit_name2taxid}()}, 47 | \code{\link{taxonkit_reformat}()} 48 | } 49 | \concept{Rtaxonkit} 50 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Common R files 2 | .Rproj.user 3 | .Rhistory 4 | .RData 5 | .Ruserdata 6 | .Renviron 7 | *.Rproj 8 | .quarto 9 | .quarto/ 10 | .DS_Store 11 | **/.DS_Store 12 | .DS_Store? 13 | *_cache/ 14 | /cache/ 15 | # R books 16 | _bookdown_files 17 | _book 18 | _freeze 19 | # pkgdown site 20 | docs/ 21 | # RStudio Connect folder 22 | rsconnect/ 23 | # History files 24 | .Rhistory 25 | .Rapp.history 26 | 27 | # Session Data files 28 | .RData 29 | 30 | # User-specific files 31 | .Ruserdata 32 | 33 | # Example code in package build process 34 | *-Ex.R 35 | 36 | # Output files from R CMD build 37 | /*.tar.gz 38 | 39 | # Output files from R CMD check 40 | /*.Rcheck/ 41 | 42 | # RStudio files 43 | .Rproj.user/ 44 | 45 | # produced vignettes 46 | vignettes/*.html 47 | vignettes/*.pdf 48 | 49 | # OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3 50 | .httr-oauth 51 | 52 | # knitr and R markdown default cache directories 53 | *_cache/ 54 | /cache/ 55 | 56 | # Temporary files created by R markdown 57 | *.utf8.md 58 | *.knit.md 59 | 60 | # R Environment Variables 61 | .Renviron 62 | 63 | RMT_temp/ 64 | *.csv 65 | .DS_Store 66 | 67 | inst/doc 68 | /doc/ 69 | /Meta/ 70 | 71 | article 72 | article/* 73 | README.qmd 74 | README.html 75 | README_cache/ 76 | .DS_Store 77 | */.DS_Store 78 | *.Rproj 79 | .gitignore 80 | docs 81 | 82 | -------------------------------------------------------------------------------- /man/pre_tax_table.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/phylogenetic.R 3 | \name{pre_tax_table} 4 | \alias{pre_tax_table} 5 | \title{Complete a taxonomy table} 6 | \usage{ 7 | pre_tax_table( 8 | tax_table, 9 | tax_levels = c("k", "p", "c", "o", "f", "g", "s", "st"), 10 | na_tax = "Unclassified|uncultured|Ambiguous|Unknown|unknown|metagenome|Unassig", 11 | ignore.case = TRUE, 12 | na_repalce = "Unknown" 13 | ) 14 | } 15 | \arguments{ 16 | \item{tax_table}{taxonomy table} 17 | 18 | \item{tax_levels}{a vector whose length longer than \code{ncol(taxdf)}, use to be prefix. Default: c("k", "p", "c", "o", "f", "g","s", "st")} 19 | 20 | \item{na_tax}{grepl some words and turn to \code{na_repalce}, default: "Unclassified|uncultured|Ambiguous|Unknown|unknown|metagenome|Unassig"} 21 | 22 | \item{ignore.case}{ignore.case for \code{na_tax}} 23 | 24 | \item{na_repalce}{defalut: Unknown} 25 | } 26 | \value{ 27 | a good taxonomy table 28 | } 29 | \description{ 30 | Complete a taxonomy table 31 | } 32 | \examples{ 33 | taxmat <- matrix(sample("onelevel", 7 * 2, replace = TRUE), nrow = 2, ncol = 7) \%>\% as.data.frame() 34 | colnames(taxmat) <- c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species") 35 | pre_tax_table(taxmat) 36 | } 37 | \references{ 38 | \code{MicrobiotaProcess} 39 | } 40 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # pctax v0.1.6 Notes 2 | 3 | ## Fixed 4 | 5 | - fixed `taxonkit` function for windows <2025-06-12, Thu> 6 | 7 | # pctax v0.1.5 Notes 8 | 9 | ## Added 10 | 11 | - added `load_mpa_df` function <2025-06-12, Thu> 12 | - added `pre_assembly_stats` function <2025-06-12, Thu> 13 | 14 | # pctax v0.1.4 Notes 15 | 16 | ## Added 17 | 18 | - added `stamp_plot` function <2025-06-12, Thu> 19 | - added `multi_conf` function <2025-06-12, Thu> 20 | 21 | # pctax v0.1.3 Notes 22 | 23 | ## Added 24 | 25 | - added mode 4 for `plot.b_res` function <2025-04-27, Sun> 26 | - added `drop_tips_update()` function <2025-04-27, Sun> 27 | - added `stamp_plot` function <2025-01-14, Tue> 28 | - added `convert_taxon_name` function <2024-11-19, Tue> 29 | - added `plot_two_tree` function <2024-10-22, Tue> 30 | 31 | ## Fixed 32 | 33 | - fixed the file.path in windows for taxonkit <2024-11-27, Wed> 34 | 35 | # pctax v0.1.2 Notes 36 | 37 | ## Added 38 | 39 | - added `topN` argument for `easy_tree()` <2024-06-25, Tue> 40 | 41 | # pctax v0.1.1 Notes 42 | 43 | ## Fixed 44 | 45 | - Use `ggforce` to replace `ggchicklet` as `ggchicklet` is not available at CRAN <2024-03-08, Fri> 46 | 47 | ## Added 48 | 49 | - improved the `plot.b_res`, added the `permanova_res` argument <2024-03-23, Sat> 50 | 51 | # pctax v0.1.0 Notes 52 | 53 | ## Added 54 | 55 | - available at CRAN <2024-02-29, Thu> 56 | 57 | ## Fixed 58 | 59 | - fixed the `ann_tree` <2024-02-29, Thu> 60 | 61 | -------------------------------------------------------------------------------- /man/permanova.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/b_analyse.R 3 | \name{permanova} 4 | \alias{permanova} 5 | \title{Permanova between a otutab and a variable} 6 | \usage{ 7 | permanova( 8 | otutab, 9 | envs, 10 | norm = TRUE, 11 | each = TRUE, 12 | method = "adonis", 13 | dist = "bray", 14 | nperm = 999, 15 | ... 16 | ) 17 | } 18 | \arguments{ 19 | \item{otutab}{an otutab data.frame, samples are columns, taxs are rows.} 20 | 21 | \item{envs}{factors need to test} 22 | 23 | \item{norm}{should normalize?(default:TRUE)} 24 | 25 | \item{each}{test factor one by one, rather than whole} 26 | 27 | \item{method}{adonis/mrpp/anosim/mantel} 28 | 29 | \item{dist}{if use pcoa or nmds, your can choose a dist method (default: bray)} 30 | 31 | \item{nperm}{numbers of permutations to perform} 32 | 33 | \item{...}{additional} 34 | } 35 | \value{ 36 | a g_test object with these columns 37 | \item{group}{the test group or factor} 38 | \item{r}{relationship} 39 | \item{r2}{model R-square} 40 | \item{p_value}{model test p_value} 41 | \item{sig}{whether significant} 42 | } 43 | \description{ 44 | Permanova between a otutab and a variable 45 | } 46 | \examples{ 47 | data(otutab, package = "pcutils") 48 | permanova(otutab, metadata[, c(2:10)]) -> adonis_res 49 | print(adonis_res) 50 | plot(adonis_res) 51 | } 52 | \references{ 53 | \url{https://blog.csdn.net/qq_42458954/article/details/110390488} 54 | } 55 | -------------------------------------------------------------------------------- /man/z_diversity_decay.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/a_diversity.R 3 | \name{z_diversity_decay} 4 | \alias{z_diversity_decay} 5 | \alias{plot.zeta_decay} 6 | \title{Calculate Zeta Diversity with Distance} 7 | \usage{ 8 | z_diversity_decay(otutab, xy_df, group_df = NULL, zetadiv_params = list()) 9 | 10 | \method{plot}{zeta_decay}(x, ribbon = TRUE, ...) 11 | } 12 | \arguments{ 13 | \item{otutab}{A matrix or data frame containing OTU (Operational Taxonomic Unit) counts.} 14 | 15 | \item{xy_df}{Site coordinates.} 16 | 17 | \item{group_df}{A data frame containing group information.} 18 | 19 | \item{zetadiv_params}{Additional parameters to be passed to the Zeta.ddecay function from the zetadiv package.} 20 | 21 | \item{x}{Zeta diversity results obtained from z_diversity_decay function.} 22 | 23 | \item{ribbon}{Logical, whether to add a ribbon to the plot for standard deviation.} 24 | 25 | \item{...}{Additional arguments to be passed to ggplot2 functions.} 26 | } 27 | \value{ 28 | zeta_decay 29 | 30 | A ggplot object. 31 | } 32 | \description{ 33 | This function calculates Zeta diversity for each group in the provided otutab. 34 | } 35 | \examples{ 36 | if (requireNamespace("zetadiv")) { 37 | data(otutab, package = "pcutils") 38 | zeta_decay_result <- z_diversity_decay(otutab, metadata[, c("lat", "long")], 39 | metadata["Group"], 40 | zetadiv_params = list(sam = 10) 41 | ) 42 | plot(zeta_decay_result) 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /man/nti_rc.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/eco_micro.R 3 | \name{nti_rc} 4 | \alias{nti_rc} 5 | \alias{plot.NTI_RC} 6 | \title{Calculate b_NTI and RC_bray for each group} 7 | \usage{ 8 | nti_rc( 9 | otutab, 10 | phylo, 11 | group_df, 12 | threads = 1, 13 | file = NULL, 14 | rep = 20, 15 | save = FALSE 16 | ) 17 | 18 | \method{plot}{NTI_RC}(x, ...) 19 | } 20 | \arguments{ 21 | \item{otutab}{an otutab data.frame, samples are columns, taxs are rows.} 22 | 23 | \item{phylo}{a phylo object} 24 | 25 | \item{group_df}{a dataframe with rowname and one group column} 26 | 27 | \item{threads}{default:4} 28 | 29 | \item{file}{filename to save} 30 | 31 | \item{rep}{repeat numbers: suggest 999} 32 | 33 | \item{save}{save the file} 34 | 35 | \item{x}{NTI_RC object} 36 | 37 | \item{...}{pass to \code{\link[pcutils]{stackplot}}} 38 | } 39 | \value{ 40 | a b_dist object, dis is MSTij 41 | 42 | ggplot 43 | } 44 | \description{ 45 | Calculate b_NTI and RC_bray for each group 46 | 47 | Plot NTI_RC object 48 | } 49 | \examples{ 50 | \donttest{ 51 | if (requireNamespace("NST") && requireNamespace("pctax")) { 52 | data(otutab, package = "pcutils") 53 | pctax::df2tree(taxonomy) -> phylo 54 | nti_rc(otutab, phylo, metadata["Group"]) -> nti_res 55 | plot(nti_res) 56 | } 57 | } 58 | } 59 | \references{ 60 | Ning, D., Deng, Y., Tiedje, J. M. & Zhou, J. (2019) A general framework for quantitatively assessing ecological stochasticity. Proceedings of the National Academy of Sciences 116, 16892–16898. 61 | } 62 | -------------------------------------------------------------------------------- /vignettes/pctax.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Quick start: pctax" 3 | output: 4 | prettydoc::html_pretty: 5 | keep_md: true 6 | toc: true 7 | toc_depth: 3 8 | theme: architect 9 | vignette: > 10 | %\VignetteIndexEntry{pctax} 11 | %\VignetteEngine{knitr::rmarkdown} 12 | %\VignetteEncoding{UTF-8} 13 | --- 14 | 15 | ```{r, include = FALSE} 16 | knitr::opts_chunk$set( 17 | collapse = TRUE, cache = TRUE, 18 | comment = "#>", 19 | dpi = 72 20 | ) 21 | ``` 22 | 23 | ```{r setup,include=FALSE} 24 | library(pctax) 25 | library(pcutils) 26 | ``` 27 | 28 | # pctax 29 | 30 | `pctax` provides a comprehensive suite of tools for analyzing omics data. 31 | 32 | ## Vignette 33 | 34 | Please go to for the full vignette. 35 | 36 | ## Install 37 | 38 | ```{r eval=FALSE} 39 | install.packages("devtools") 40 | devtools::install_github("Asa12138/pcutils") 41 | devtools::install_github("Asa12138/pctax") 42 | ``` 43 | 44 | ## Usage 45 | It includes functionalities for α-diversity analysis, β-diversity analysis, differential abundance analysis, community assembly, visualization of phylogenetic tree and functional enrichment analysis... 46 | 47 | Look at the test data: 48 | ```{r lib} 49 | library(pctax) 50 | library(pcutils) 51 | data(otutab, package = "pcutils") 52 | # help(otutab) 53 | 54 | head(otutab) 55 | head(metadata) 56 | head(taxonomy) 57 | ``` 58 | 59 | ## Citation 60 | Please cite: 61 | 62 | Chen Peng (2023). _pctax: Professional Comprehensive Omics Data Analysis_. R package, . 63 | -------------------------------------------------------------------------------- /pkgdown/favicon/safari-pinned-tab.svg: -------------------------------------------------------------------------------- 1 | 2 | 4 | 7 | 8 | Created by potrace 1.14, written by Peter Selinger 2001-2017 9 | 10 | 12 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /.github/workflows/R-CMD-check.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | 9 | name: R-CMD-check 10 | 11 | jobs: 12 | R-CMD-check: 13 | runs-on: ${{ matrix.config.os }} 14 | 15 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 16 | 17 | strategy: 18 | fail-fast: false 19 | matrix: 20 | config: 21 | # - {os: macos-latest, r: 'release'} 22 | - {os: windows-latest, r: 'release'} 23 | - {os: ubuntu-latest, r: 'release'} 24 | 25 | env: 26 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 27 | R_KEEP_PKG_SOURCE: yes 28 | 29 | steps: 30 | - uses: actions/checkout@v4 31 | 32 | - uses: r-lib/actions/setup-pandoc@v2 33 | 34 | - uses: r-lib/actions/setup-r@v2 35 | with: 36 | r-version: ${{ matrix.config.r }} 37 | http-user-agent: ${{ matrix.config.http-user-agent }} 38 | use-public-rspm: true 39 | 40 | - uses: r-lib/actions/setup-r-dependencies@v2 41 | with: 42 | extra-packages: any::rcmdcheck, any::knitr, any::rmarkdown, any::prettydoc 43 | needs: check 44 | dependencies: '"hard"' 45 | 46 | - uses: r-lib/actions/check-r-package@v2 47 | with: 48 | upload-snapshots: true 49 | build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")' 50 | -------------------------------------------------------------------------------- /.github/workflows/pkgdown.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | release: 9 | types: [published] 10 | workflow_dispatch: 11 | 12 | name: pkgdown 13 | 14 | jobs: 15 | pkgdown: 16 | runs-on: ubuntu-latest 17 | # Only restrict concurrency for non-PR jobs 18 | concurrency: 19 | group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }} 20 | env: 21 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 22 | permissions: 23 | contents: write 24 | steps: 25 | - uses: actions/checkout@v4 26 | 27 | - uses: r-lib/actions/setup-pandoc@v2 28 | 29 | - uses: r-lib/actions/setup-r@v2 30 | with: 31 | use-public-rspm: true 32 | 33 | - uses: r-lib/actions/setup-r-dependencies@v2 34 | with: 35 | extra-packages: any::pkgdown, local::., any::knitr, any::rmarkdown, any::prettydoc 36 | needs: website 37 | dependencies: '"hard"' 38 | 39 | - name: Build site 40 | run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE) 41 | shell: Rscript {0} 42 | 43 | - name: Deploy to GitHub pages 🚀 44 | if: github.event_name != 'pull_request' 45 | uses: JamesIves/github-pages-deploy-action@v4.5.0 46 | with: 47 | clean: false 48 | branch: gh-pages 49 | folder: docs 50 | -------------------------------------------------------------------------------- /man/plot_N_cycle.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/functional.R 3 | \name{plot_N_cycle} 4 | \alias{plot_N_cycle} 5 | \title{Plot the N-cycling pathway and genes} 6 | \usage{ 7 | plot_N_cycle( 8 | my_N_genes = NULL, 9 | just_diff = FALSE, 10 | path_col = NULL, 11 | type_col = c(up = "red", down = "blue", none = NA), 12 | fill_alpha = 0.5, 13 | arrow_size = 0.1, 14 | line_width = 1, 15 | title = "Nitrogen cycling", 16 | legend.position = c(0.85, 0.15) 17 | ) 18 | } 19 | \arguments{ 20 | \item{my_N_genes}{dataframe, "Gene_families","type" should in colnames of my_N_genes} 21 | 22 | \item{just_diff}{logical, just plot the different genes?} 23 | 24 | \item{path_col}{colors of pathways} 25 | 26 | \item{type_col}{colors of types} 27 | 28 | \item{fill_alpha}{alpha, default 0.5} 29 | 30 | \item{arrow_size}{arrow_size, default 0.1} 31 | 32 | \item{line_width}{line_width, default 1} 33 | 34 | \item{title}{title, default "Nitrogen cycling"} 35 | 36 | \item{legend.position}{default c(0.85,0.15)} 37 | } 38 | \value{ 39 | ggplot 40 | } 41 | \description{ 42 | Plot the N-cycling pathway and genes 43 | } 44 | \examples{ 45 | N_data <- load_N_data() 46 | my_N_genes <- data.frame( 47 | `Gene_families` = sample(N_data$N_genes$Gene_families, 10, replace = FALSE), 48 | change = rnorm(10), check.names = FALSE 49 | ) 50 | my_N_genes <- dplyr::mutate(my_N_genes, 51 | type = ifelse(change > 0, "up", ifelse(change < 0, "down", "none")) 52 | ) 53 | plot_N_cycle(my_N_genes, just_diff = FALSE, fill_alpha = 0.2) 54 | # ggsave(filename = "test.pdf", width = 14, height = 10) 55 | } 56 | -------------------------------------------------------------------------------- /man/z_diversity.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/a_diversity.R 3 | \name{z_diversity} 4 | \alias{z_diversity} 5 | \alias{plot.zeta_res} 6 | \title{Calculate Zeta Diversity} 7 | \usage{ 8 | z_diversity(otutab, group_df = NULL, zetadiv_params = list()) 9 | 10 | \method{plot}{zeta_res}(x, lm_model = c("exp", "pl")[1], ribbon = FALSE, text = TRUE, ...) 11 | } 12 | \arguments{ 13 | \item{otutab}{A matrix or data frame containing OTU (Operational Taxonomic Unit) counts.} 14 | 15 | \item{group_df}{A data frame containing group information.} 16 | 17 | \item{zetadiv_params}{Additional parameters to be passed to the Zeta.decline.mc function from the zetadiv package.} 18 | 19 | \item{x}{Zeta diversity results obtained from z_diversity function.} 20 | 21 | \item{lm_model}{The linear model to be used for fitting ('exp' or 'pl').} 22 | 23 | \item{ribbon}{Logical, whether to add a ribbon to the plot for standard deviation.} 24 | 25 | \item{text}{Logical, whether to add R-squared and p-value text annotations.} 26 | 27 | \item{...}{Additional arguments to be passed to ggplot2 functions.} 28 | } 29 | \value{ 30 | zeta_res 31 | 32 | A ggplot object. 33 | } 34 | \description{ 35 | This function calculates Zeta diversity for each group in the provided otutab. 36 | 37 | This function plots the Zeta diversity results obtained from the z_diversity function. 38 | } 39 | \examples{ 40 | \donttest{ 41 | if (requireNamespace("zetadiv")) { 42 | data(otutab, package = "pcutils") 43 | zeta_result <- z_diversity(otutab, metadata["Group"], zetadiv_params = list(sam = 10)) 44 | plot(zeta_result, lm_model = "exp", text = TRUE) 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /man/RDA_plot.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/b_analyse.R 3 | \name{RDA_plot} 4 | \alias{RDA_plot} 5 | \title{Plot RDA res} 6 | \usage{ 7 | RDA_plot( 8 | phy.rda, 9 | Group, 10 | metadata = NULL, 11 | Group2 = NULL, 12 | env_rate = 1, 13 | mode = 1, 14 | tri = FALSE, 15 | Topn = 10, 16 | rate = 1, 17 | margin = FALSE, 18 | box_margin = TRUE, 19 | pal = NULL, 20 | sample_label = TRUE, 21 | stat_ellipse = TRUE, 22 | ellipse_level = 0.95, 23 | coord_fix = FALSE, 24 | bi_text_size = 3, 25 | env_text_param = NULL, 26 | ... 27 | ) 28 | } 29 | \arguments{ 30 | \item{phy.rda}{rda/cca object} 31 | 32 | \item{Group}{group vector for color} 33 | 34 | \item{metadata}{metadata contain Group} 35 | 36 | \item{Group2}{mapping point shape} 37 | 38 | \item{env_rate}{default 1} 39 | 40 | \item{mode}{plot mode:1~3} 41 | 42 | \item{tri}{plot variables segments?} 43 | 44 | \item{Topn}{how many variables to show?} 45 | 46 | \item{rate}{segments length rate} 47 | 48 | \item{margin}{plot the margin boxplot?} 49 | 50 | \item{box_margin}{margin plot box or density?} 51 | 52 | \item{pal}{colors for group} 53 | 54 | \item{sample_label}{plot the labels of samples?} 55 | 56 | \item{stat_ellipse}{plot the stat_ellipse?} 57 | 58 | \item{ellipse_level}{the level of stat_ellipse, defalut 0.95} 59 | 60 | \item{coord_fix}{fix the coordinates y/x ratio} 61 | 62 | \item{bi_text_size}{biplot text size} 63 | 64 | \item{env_text_param}{parameters pass to \code{\link[ggplot2]{geom_text}}} 65 | 66 | \item{...}{add} 67 | } 68 | \value{ 69 | ggplot 70 | } 71 | \description{ 72 | Plot RDA res 73 | } 74 | \seealso{ 75 | \code{\link{myRDA}} 76 | } 77 | -------------------------------------------------------------------------------- /man/RCbray1.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/eco_micro.R 3 | \name{RCbray1} 4 | \alias{RCbray1} 5 | \title{Calculate RCbray-curtis} 6 | \usage{ 7 | RCbray1( 8 | otutab, 9 | reps = 9, 10 | threads = 1, 11 | classic_metric = TRUE, 12 | split_ties = TRUE 13 | ) 14 | } 15 | \arguments{ 16 | \item{otutab}{otutab} 17 | 18 | \item{reps}{how many simulation performed?} 19 | 20 | \item{threads}{use how many threads to calculate (default:4)} 21 | 22 | \item{classic_metric}{standardizes the metric to range from -1 to 1} 23 | 24 | \item{split_ties}{adds half of the number of null observations that are equal to the observed number of shared species to the calculation- this is highly recommended} 25 | } 26 | \value{ 27 | a dist 28 | } 29 | \description{ 30 | Calculate RCbray-curtis 31 | } 32 | \details{ 33 | Parallelized version of the Raup-Crick algorithm for "abundance" data (Stegen et al. 2013). 34 | } 35 | \examples{ 36 | \donttest{ 37 | if (requireNamespace("picante")) { 38 | data(otutab, package = "pcutils") 39 | df2tree(taxonomy) -> phylo 40 | b_NTI1(phylo, otutab) -> bnti_res 41 | RCbray1(otutab, reps = 9) -> rc_res 42 | 43 | data.frame( 44 | type = factor(c("Homo_S", "Heter_S", "Homo_D", "D_limit", "Undominated"), 45 | levels = c("Homo_S", "Heter_S", "Homo_D", "D_limit", "Undominated") 46 | ), 47 | number = c( 48 | sum(bnti_res < (-2)), sum(bnti_res > 2), 49 | sum((abs(bnti_res) < 2) & (abs(rc_res) < 0.95)), 50 | sum((abs(bnti_res) < 2) & (rc_res < (-0.95))), 51 | sum((abs(bnti_res) < 2) & (rc_res > 0.95)) 52 | ) 53 | ) -> com_pro 54 | pcutils::gghuan(com_pro, reorder = FALSE) 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /man/taxonkit_list.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/Rtaxonkit.R 3 | \name{taxonkit_list} 4 | \alias{taxonkit_list} 5 | \title{Taxonkit list} 6 | \usage{ 7 | taxonkit_list( 8 | ids, 9 | indent = " ", 10 | json = FALSE, 11 | show_name = FALSE, 12 | show_rank = FALSE, 13 | data_dir = NULL 14 | ) 15 | } 16 | \arguments{ 17 | \item{ids}{A character vector of TaxIDs to retrieve information for.} 18 | 19 | \item{indent}{The indentation string to use for pretty-printing the output. Default is " ".} 20 | 21 | \item{json}{Logical value indicating whether to output the result in JSON format. Default is FALSE.} 22 | 23 | \item{show_name}{Logical value indicating whether to show the scientific names of taxa. Default is FALSE.} 24 | 25 | \item{show_rank}{Logical value indicating whether to show the ranks of taxa. Default is FALSE.} 26 | 27 | \item{data_dir}{directory containing nodes.dmp and names.dmp (default "/Users/asa/.taxonkit")} 28 | } 29 | \value{ 30 | The output of the Taxonkit list operation. 31 | } 32 | \description{ 33 | This function uses Taxonkit to perform the "list" operation, which retrieves 34 | information about taxa based on their TaxIDs. 35 | } 36 | \examples{ 37 | \dontrun{ 38 | taxonkit_list(ids = c(9605), indent = "-", show_name = TRUE, show_rank = TRUE) 39 | } 40 | } 41 | \seealso{ 42 | Other Rtaxonkit: 43 | \code{\link{check_taxonkit}()}, 44 | \code{\link{download_taxonkit_dataset}()}, 45 | \code{\link{install_taxonkit}()}, 46 | \code{\link{name_or_id2df}()}, 47 | \code{\link{taxonkit_filter}()}, 48 | \code{\link{taxonkit_lca}()}, 49 | \code{\link{taxonkit_lineage}()}, 50 | \code{\link{taxonkit_name2taxid}()}, 51 | \code{\link{taxonkit_reformat}()} 52 | } 53 | \concept{Rtaxonkit} 54 | -------------------------------------------------------------------------------- /man/ann_tree.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/phylogenetic.R 3 | \name{ann_tree} 4 | \alias{ann_tree} 5 | \alias{easy_tree} 6 | \title{Annotate a tree} 7 | \usage{ 8 | ann_tree(f_tax, otutab = NULL, level = ncol(f_tax), ignore_pattern = NULL) 9 | 10 | easy_tree( 11 | tree, 12 | highlight = "Phylum", 13 | colorfill = "color", 14 | topN = NULL, 15 | pal = NULL, 16 | name_prefix = FALSE, 17 | basic_params = NULL, 18 | add_abundance = TRUE, 19 | color_name = "abundance", 20 | add_tiplab = TRUE, 21 | fontsize = NULL 22 | ) 23 | } 24 | \arguments{ 25 | \item{f_tax}{taxonomy dataframe} 26 | 27 | \item{otutab}{otutab, rowname==rowname(taxonomy)} 28 | 29 | \item{level}{1~7} 30 | 31 | \item{ignore_pattern}{An optional regular expression pattern to match tip or node labels for dropping.} 32 | 33 | \item{tree}{result from \code{ann_tree}} 34 | 35 | \item{highlight}{highlight which level, one of \code{tree$level}} 36 | 37 | \item{colorfill}{"color" or "fill"} 38 | 39 | \item{topN}{topN to show} 40 | 41 | \item{pal}{color pal} 42 | 43 | \item{name_prefix}{keep the prefix like "k__" or "p__" in the label? Default: FALSE} 44 | 45 | \item{basic_params}{parameters parse to \code{\link[ggtree]{ggtree}}} 46 | 47 | \item{add_abundance}{logical} 48 | 49 | \item{color_name}{color name} 50 | 51 | \item{add_tiplab}{logical} 52 | 53 | \item{fontsize}{tip label fontsize} 54 | } 55 | \value{ 56 | a treedata 57 | 58 | a ggplot 59 | } 60 | \description{ 61 | Annotate a tree 62 | 63 | Easy way to plot a phylogenetic tree 64 | } 65 | \examples{ 66 | if (interactive()) { 67 | data(otutab, package = "pcutils") 68 | ann_tree(taxonomy, otutab) -> tree 69 | # run yourself 70 | easy_tree(tree, add_abundance = FALSE) -> p 71 | p 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /man/myRDA.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/b_analyse.R 3 | \name{myRDA} 4 | \alias{myRDA} 5 | \alias{myCCA} 6 | \alias{myCAP} 7 | \alias{mydbRDA} 8 | \title{RDA} 9 | \usage{ 10 | myRDA( 11 | otutab, 12 | env, 13 | norm = TRUE, 14 | scale = FALSE, 15 | choose_var = FALSE, 16 | direction = "forward", 17 | nperm = 499, 18 | verbose = TRUE, 19 | method = "rda", 20 | dist = "bray" 21 | ) 22 | 23 | myCCA( 24 | otutab, 25 | env, 26 | norm = TRUE, 27 | scale = FALSE, 28 | choose_var = FALSE, 29 | nperm = 499, 30 | verbose = TRUE 31 | ) 32 | 33 | myCAP( 34 | otutab, 35 | env, 36 | norm = TRUE, 37 | scale = FALSE, 38 | choose_var = FALSE, 39 | nperm = 499, 40 | verbose = TRUE, 41 | dist = "bray" 42 | ) 43 | } 44 | \arguments{ 45 | \item{otutab}{an otutab data.frame, samples are columns, taxs are rows.} 46 | 47 | \item{env}{environmental factors} 48 | 49 | \item{norm}{should normalize? (default:TRUE)} 50 | 51 | \item{scale}{should scale species? (default:FALSE)} 52 | 53 | \item{choose_var}{should choose variables? use forward step} 54 | 55 | \item{direction}{The direction of the stepwise selection, "both", "forward" or "backward", default is "forward"} 56 | 57 | \item{nperm}{number of permutation} 58 | 59 | \item{verbose}{verbose} 60 | 61 | \item{method}{"rda", "cca", "cap", "dbrda"} 62 | 63 | \item{dist}{The name of the dissimilarity (or distance) index for "cap" or "dbrda", for \code{\link[vegan]{vegdist}}} 64 | } 65 | \value{ 66 | rda/cca 67 | } 68 | \description{ 69 | RDA 70 | } 71 | \examples{ 72 | data(otutab, package = "pcutils") 73 | env <- metadata[, 6:10] 74 | # RDA 75 | myRDA(otutab, env) -> phy.rda 76 | RDA_plot(phy.rda, "Group", metadata) 77 | } 78 | \seealso{ 79 | \code{\link[vegan]{vegdist};\link[picante]{unifrac}} 80 | } 81 | -------------------------------------------------------------------------------- /man/taxonkit_lca.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/Rtaxonkit.R 3 | \name{taxonkit_lca} 4 | \alias{taxonkit_lca} 5 | \title{Compute Lowest Common Ancestor (LCA) of TaxIDs} 6 | \usage{ 7 | taxonkit_lca( 8 | file_path, 9 | buffer_size = "1M", 10 | separator = " ", 11 | skip_deleted = FALSE, 12 | skip_unfound = FALSE, 13 | taxids_field = NULL, 14 | text = FALSE, 15 | data_dir = NULL 16 | ) 17 | } 18 | \arguments{ 19 | \item{file_path}{The path to the input file containing TaxIDs. Or file text (text=TRUE)} 20 | 21 | \item{buffer_size}{The size of the line buffer (supported units: K, M, G).} 22 | 23 | \item{separator}{The separator for TaxIDs.} 24 | 25 | \item{skip_deleted}{Whether to skip deleted TaxIDs and compute with the remaining ones.} 26 | 27 | \item{skip_unfound}{Whether to skip unfound TaxIDs and compute with the remaining ones.} 28 | 29 | \item{taxids_field}{The field index of TaxIDs. Input data should be tab-separated (default 1).} 30 | 31 | \item{text}{logical} 32 | 33 | \item{data_dir}{directory containing nodes.dmp and names.dmp (default "/Users/asa/.taxonkit")} 34 | } 35 | \value{ 36 | A character vector containing the computed LCAs. 37 | } 38 | \description{ 39 | This function uses the "taxonkit lca" command to compute the Lowest Common Ancestor (LCA) of TaxIDs. 40 | } 41 | \examples{ 42 | \dontrun{ 43 | taxonkit_lca("239934, 239935, 349741", text = TRUE, separator = ", ") 44 | } 45 | } 46 | \seealso{ 47 | Other Rtaxonkit: 48 | \code{\link{check_taxonkit}()}, 49 | \code{\link{download_taxonkit_dataset}()}, 50 | \code{\link{install_taxonkit}()}, 51 | \code{\link{name_or_id2df}()}, 52 | \code{\link{taxonkit_filter}()}, 53 | \code{\link{taxonkit_lineage}()}, 54 | \code{\link{taxonkit_list}()}, 55 | \code{\link{taxonkit_name2taxid}()}, 56 | \code{\link{taxonkit_reformat}()} 57 | } 58 | \concept{Rtaxonkit} 59 | -------------------------------------------------------------------------------- /man/taxonkit_name2taxid.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/Rtaxonkit.R 3 | \name{taxonkit_name2taxid} 4 | \alias{taxonkit_name2taxid} 5 | \title{Convert Taxonomic Names to TaxIDs} 6 | \usage{ 7 | taxonkit_name2taxid( 8 | file_path, 9 | name_field = NULL, 10 | sci_name = FALSE, 11 | show_rank = FALSE, 12 | text = FALSE, 13 | data_dir = NULL 14 | ) 15 | } 16 | \arguments{ 17 | \item{file_path}{The path to the input file containing taxonomic names. Or file text (text=TRUE)} 18 | 19 | \item{name_field}{The field index of the taxonomic name in the input file (default is 1).} 20 | 21 | \item{sci_name}{Logical value indicating whether to search only for scientific names (default is FALSE).} 22 | 23 | \item{show_rank}{Logical value indicating whether to show the taxonomic rank in the output (default is FALSE).} 24 | 25 | \item{text}{Logical} 26 | 27 | \item{data_dir}{directory containing nodes.dmp and names.dmp (default "/Users/asa/.taxonkit")} 28 | } 29 | \value{ 30 | A character vector containing the output of the "taxonkit_name2taxid" command. 31 | } 32 | \description{ 33 | This function uses the "taxonkit taxonkit_name2taxid" command to convert taxonomic names to corresponding taxonomic IDs (TaxIDs). 34 | } 35 | \examples{ 36 | \dontrun{ 37 | names <- system.file("extdata/name.txt", package = "pctax") 38 | taxonkit_name2taxid(names, name_field = 1, sci_name = FALSE, show_rank = FALSE) 39 | "Homo sapiens" \%>\% taxonkit_name2taxid(text = TRUE) 40 | } 41 | } 42 | \seealso{ 43 | Other Rtaxonkit: 44 | \code{\link{check_taxonkit}()}, 45 | \code{\link{download_taxonkit_dataset}()}, 46 | \code{\link{install_taxonkit}()}, 47 | \code{\link{name_or_id2df}()}, 48 | \code{\link{taxonkit_filter}()}, 49 | \code{\link{taxonkit_lca}()}, 50 | \code{\link{taxonkit_lineage}()}, 51 | \code{\link{taxonkit_list}()}, 52 | \code{\link{taxonkit_reformat}()} 53 | } 54 | \concept{Rtaxonkit} 55 | -------------------------------------------------------------------------------- /man/plot.b_res.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/b_analyse.R 3 | \name{plot.b_res} 4 | \alias{plot.b_res} 5 | \title{Plot a b_res} 6 | \usage{ 7 | \method{plot}{b_res}( 8 | x, 9 | Group, 10 | metadata = NULL, 11 | Group2 = NULL, 12 | mode = 1, 13 | bi = FALSE, 14 | Topn = 10, 15 | rate = 1, 16 | margin = FALSE, 17 | margin_label = TRUE, 18 | permanova_res = NULL, 19 | text_param = list(), 20 | box_margin = TRUE, 21 | box_param = list(), 22 | pal = NULL, 23 | sample_label = TRUE, 24 | stat_ellipse = TRUE, 25 | ellipse_level = 0.95, 26 | add_centroid_label = TRUE, 27 | coord_fix = FALSE, 28 | bi_text_size = 3, 29 | ... 30 | ) 31 | } 32 | \arguments{ 33 | \item{x}{a b_res object} 34 | 35 | \item{Group}{group vector for color} 36 | 37 | \item{metadata}{metadata contain Group} 38 | 39 | \item{Group2}{mapping point shape} 40 | 41 | \item{mode}{plot mode:1~3} 42 | 43 | \item{bi}{plot variables segments?} 44 | 45 | \item{Topn}{how many variables to show?} 46 | 47 | \item{rate}{segments length rate} 48 | 49 | \item{margin}{plot the margin boxplot?} 50 | 51 | \item{margin_label}{margin_label, TRUE} 52 | 53 | \item{permanova_res}{permanova result} 54 | 55 | \item{text_param}{text_param for \code{\link[ggplot2]{annotate}}} 56 | 57 | \item{box_margin}{margin plot box or density?} 58 | 59 | \item{box_param}{box_param for \code{\link[pcutils]{group_box}}} 60 | 61 | \item{pal}{colors for group} 62 | 63 | \item{sample_label}{plot the labels of samples?} 64 | 65 | \item{stat_ellipse}{plot the stat_ellipse?} 66 | 67 | \item{ellipse_level}{the level of stat_ellipse, defalut 0.95} 68 | 69 | \item{add_centroid_label}{add the centroid label in mode 3?} 70 | 71 | \item{coord_fix}{fix the coordinates y/x ratio} 72 | 73 | \item{bi_text_size}{biplot text size} 74 | 75 | \item{...}{add} 76 | } 77 | \value{ 78 | a ggplot 79 | } 80 | \description{ 81 | Plot a b_res 82 | } 83 | \seealso{ 84 | \code{\link{b_analyse}} 85 | } 86 | -------------------------------------------------------------------------------- /man/b_analyse.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/b_analyse.R 3 | \name{b_analyse} 4 | \alias{b_analyse} 5 | \alias{b_analyse.data.frame} 6 | \title{Beta_diversity Ordination: dimensionality reduction} 7 | \usage{ 8 | b_analyse(otutab, ...) 9 | 10 | \method{b_analyse}{data.frame}( 11 | otutab, 12 | norm = TRUE, 13 | method = c("pca"), 14 | group = NULL, 15 | dist = "bray", 16 | ndim = 2, 17 | scale = FALSE, 18 | ... 19 | ) 20 | } 21 | \arguments{ 22 | \item{otutab}{an otutab data.frame, samples are columns, taxs are rows.} 23 | 24 | \item{...}{add} 25 | 26 | \item{norm}{should normalized or not? (hellinger)} 27 | 28 | \item{method}{one of "pca","pcoa","ca","dca","nmds","plsda","tsne","umap","lda","all"} 29 | 30 | \item{group}{if needed, give a group vector} 31 | 32 | \item{dist}{if use pcoa or nmds, your can choose a dist method (default: bray) or input a distance matrix.} 33 | 34 | \item{ndim}{how many dimension be kept? (default:2). 3 for b_res_3d()} 35 | 36 | \item{scale}{scale, default: FALSE} 37 | } 38 | \value{ 39 | b_res object 40 | } 41 | \description{ 42 | Species abundance data can be preprocessed with Hellinger transformation or chord transformation data before PCA analysis. Because the Hellinger distance or chord distance with-without data is equal to \eqn{\sqrt2\sqrt{1-Ochiai\ similarity}}, therefore, the sorting diagram (type 1 scale) of PCA analysis after Hellinger transformation or chord transformation with-without data is internal sample The distance between the squares is the Ochiai distance. \eqn{\sqrt2\sqrt{1-Ochiai\ similarity}} is a distance measure, which is also suitable for the analysis of species data. The processed data is then used for pca without norm. 43 | } 44 | \examples{ 45 | data(otutab, package = "pcutils") 46 | b_analyse(otutab, method = "pca") -> b_res 47 | plot(b_res, "Group", metadata) 48 | } 49 | \references{ 50 | \url{https://www.jianshu.com/p/9694c0b6302d} 51 | \url{https://zhuanlan.zhihu.com/p/25501130} 52 | } 53 | -------------------------------------------------------------------------------- /man/plot_element_cycle.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/functional.R 3 | \name{plot_element_cycle} 4 | \alias{plot_element_cycle} 5 | \title{Plot element cycle} 6 | \usage{ 7 | plot_element_cycle( 8 | cycle = "Nitrogen cycle", 9 | anno_df = NULL, 10 | only_anno = FALSE, 11 | cell_fill = NA, 12 | cell_color = "orange", 13 | use_chinese = FALSE, 14 | chemical_size = 7, 15 | chemical_bold = TRUE, 16 | chemical_color = "black", 17 | chemical_label = TRUE, 18 | reaction_width = 1, 19 | reaction_arrow_size = 4, 20 | reaction_arrow_closed = TRUE, 21 | gene_or_ko = "gene", 22 | gene_size = 3, 23 | gene_x_offset = 0.3, 24 | gene_y_offset = 0.15, 25 | gene_label = TRUE, 26 | gene_color = NULL, 27 | gene_bold = TRUE, 28 | gene_italic = TRUE, 29 | gene_label_fill = "white" 30 | ) 31 | } 32 | \arguments{ 33 | \item{cycle}{one of c("Carbon cycle","Nitrogen cycle","Phosphorus cycle","Sulfur cycle","Iron cycle")} 34 | 35 | \item{anno_df}{anno_df, columns should contains Gene or KO and Group} 36 | 37 | \item{only_anno}{only show genes in anno_df?} 38 | 39 | \item{cell_fill}{cell fill color} 40 | 41 | \item{cell_color}{cell border color} 42 | 43 | \item{use_chinese}{use chinese label?} 44 | 45 | \item{chemical_size}{chemical text size} 46 | 47 | \item{chemical_bold}{chemical text bold} 48 | 49 | \item{chemical_color}{chemical text color} 50 | 51 | \item{chemical_label}{chemical text in geom_label or geom_text?} 52 | 53 | \item{reaction_width}{reaction line width} 54 | 55 | \item{reaction_arrow_size}{reaction arrow size} 56 | 57 | \item{reaction_arrow_closed}{reaction arrow closed?} 58 | 59 | \item{gene_or_ko}{"gene" or "ko"} 60 | 61 | \item{gene_size}{gene text size} 62 | 63 | \item{gene_x_offset}{gene_x_offset} 64 | 65 | \item{gene_y_offset}{gene_y_offset} 66 | 67 | \item{gene_label}{gene text in geom_label or geom_text?} 68 | 69 | \item{gene_color}{gene text color} 70 | 71 | \item{gene_bold}{gene text blod?} 72 | 73 | \item{gene_italic}{gene text italic?} 74 | 75 | \item{gene_label_fill}{gene label fill color} 76 | } 77 | \value{ 78 | ggplot 79 | } 80 | \description{ 81 | Plot element cycle 82 | } 83 | \examples{ 84 | if (requireNamespace("ggforce")) plot_element_cycle() 85 | } 86 | -------------------------------------------------------------------------------- /man/taxonkit_lineage.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/Rtaxonkit.R 3 | \name{taxonkit_lineage} 4 | \alias{taxonkit_lineage} 5 | \title{Retrieve Taxonomic Lineage using taxonkit} 6 | \usage{ 7 | taxonkit_lineage( 8 | file_path, 9 | delimiter = ";", 10 | no_lineage = FALSE, 11 | show_lineage_ranks = FALSE, 12 | show_lineage_taxids = FALSE, 13 | show_name = FALSE, 14 | show_rank = FALSE, 15 | show_status_code = FALSE, 16 | taxid_field = 1, 17 | text = FALSE, 18 | data_dir = NULL 19 | ) 20 | } 21 | \arguments{ 22 | \item{file_path}{The path to the input file with taxonomic IDs. Or file text (text=TRUE)} 23 | 24 | \item{delimiter}{The field delimiter in the lineage (default ";").} 25 | 26 | \item{no_lineage}{Logical, indicating whether to exclude lineage information (default: FALSE).} 27 | 28 | \item{show_lineage_ranks}{Logical, indicating whether to append ranks of all levels in the lineage (default: FALSE).} 29 | 30 | \item{show_lineage_taxids}{Logical, indicating whether to append lineage consisting of taxids (default: FALSE).} 31 | 32 | \item{show_name}{Logical, indicating whether to append scientific name (default: FALSE).} 33 | 34 | \item{show_rank}{Logical, indicating whether to append rank of taxids (default: FALSE).} 35 | 36 | \item{show_status_code}{Logical, indicating whether to show status code before lineage (default: FALSE).} 37 | 38 | \item{taxid_field}{The field index of taxid. Input data should be tab-separated (default: 1).} 39 | 40 | \item{text}{logical,} 41 | 42 | \item{data_dir}{directory containing nodes.dmp and names.dmp (default "/Users/asa/.taxonkit")} 43 | } 44 | \value{ 45 | A character vector containing the taxonomic lineage information. 46 | } 47 | \description{ 48 | Retrieve Taxonomic Lineage using taxonkit 49 | } 50 | \examples{ 51 | \dontrun{ 52 | taxonkit_lineage("9606\n63221", show_name = TRUE, show_rank = TRUE, text = TRUE) 53 | } 54 | } 55 | \seealso{ 56 | Other Rtaxonkit: 57 | \code{\link{check_taxonkit}()}, 58 | \code{\link{download_taxonkit_dataset}()}, 59 | \code{\link{install_taxonkit}()}, 60 | \code{\link{name_or_id2df}()}, 61 | \code{\link{taxonkit_filter}()}, 62 | \code{\link{taxonkit_lca}()}, 63 | \code{\link{taxonkit_list}()}, 64 | \code{\link{taxonkit_name2taxid}()}, 65 | \code{\link{taxonkit_reformat}()} 66 | } 67 | \concept{Rtaxonkit} 68 | -------------------------------------------------------------------------------- /man/plot_two_tree.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/phylogenetic.R 3 | \name{plot_two_tree} 4 | \alias{plot_two_tree} 5 | \title{Plot two trees in one plot} 6 | \usage{ 7 | plot_two_tree( 8 | tree1, 9 | tree2, 10 | edge_df = NULL, 11 | tree2_x = 10, 12 | filter_link = FALSE, 13 | tree1_param = list(), 14 | tree2_param = list(), 15 | line_param = list(), 16 | tree1_tip = FALSE, 17 | tip1_param = list(), 18 | tree2_tip = FALSE, 19 | tip2_param = list(), 20 | tree1_highlight = NULL, 21 | highlight1_param = list(), 22 | highlight1_scale = NULL, 23 | tree2_highlight = NULL, 24 | highlight2_param = list(), 25 | highlight2_scale = ggplot2::scale_fill_hue(na.value = NA) 26 | ) 27 | } 28 | \arguments{ 29 | \item{tree1}{phylo object} 30 | 31 | \item{tree2}{phylo object} 32 | 33 | \item{edge_df}{dataframe with edge information, containing "from" and "to" columns} 34 | 35 | \item{tree2_x}{x position of tree2} 36 | 37 | \item{filter_link}{filter the link between tree1 and tree2} 38 | 39 | \item{tree1_param}{parameters for \code{\link[ggtree]{geom_tree}}} 40 | 41 | \item{tree2_param}{parameters for \code{\link[ggtree]{geom_tree}}} 42 | 43 | \item{line_param}{parameters for \code{\link[ggplot2]{geom_line}}} 44 | 45 | \item{tree1_tip}{tree tip label} 46 | 47 | \item{tip1_param}{parameters for \code{\link[ggtree]{geom_tiplab}}} 48 | 49 | \item{tree2_tip}{tree tip label} 50 | 51 | \item{tip2_param}{parameters for \code{\link[ggtree]{geom_tiplab}}} 52 | 53 | \item{tree1_highlight}{tree1 highlight data.frame} 54 | 55 | \item{highlight1_param}{parameters for \code{\link[ggtree]{geom_hilight}}} 56 | 57 | \item{highlight1_scale}{scale_fill_ for highlight1} 58 | 59 | \item{tree2_highlight}{tree2 highlight data.frame} 60 | 61 | \item{highlight2_param}{parameters for \code{\link[ggtree]{geom_hilight}}} 62 | 63 | \item{highlight2_scale}{scale_fill_ for highlight2} 64 | } 65 | \value{ 66 | ggplot object 67 | } 68 | \description{ 69 | Plot two trees in one plot 70 | } 71 | \examples{ 72 | if (requireNamespace("ggtree")) { 73 | data(otutab, package = "pcutils") 74 | df2tree(taxonomy[1:50, ]) -> tax_tree 75 | df2tree(taxonomy[51:100, ]) -> tax_tree2 76 | link <- data.frame(from = sample(tax_tree$tip.label, 20), to = sample(tax_tree2$tip.label, 20)) 77 | plot_two_tree(tax_tree, tax_tree2, link) 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: pctax 2 | Type: Package 3 | Title: Professional Comprehensive Omics Data Analysis 4 | Version: 0.1.6 5 | Authors@R: 6 | person("Chen", "Peng", email ="pengchen2001@zju.edu.cn", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-9449-7606")) 7 | Description: 8 | Provides a comprehensive suite of tools for analyzing omics data. 9 | It includes functionalities for alpha diversity analysis, beta diversity analysis, differential abundance analysis, community assembly analysis, visualization of phylogenetic tree, and functional enrichment analysis. 10 | With a progressive approach, the package offers a range of analysis methods to explore and understand the complex communities. 11 | It is designed to support researchers and practitioners in conducting in-depth and professional omics data analysis. 12 | License: GPL-3 13 | Encoding: UTF-8 14 | Roxygen: list(markdown = TRUE) 15 | RoxygenNote: 7.3.2 16 | Depends: 17 | R (>= 4.2.0) 18 | LazyData: true 19 | Imports: 20 | pcutils (>= 0.2.5), 21 | dplyr, 22 | ggplot2 (>= 3.2.0), 23 | vegan, 24 | magrittr, 25 | grDevices, 26 | RColorBrewer, 27 | ggrepel, 28 | reshape2, 29 | stats, 30 | tibble, 31 | utils, 32 | ggpubr, 33 | patchwork, 34 | ggnewscale, 35 | ade4, 36 | scales 37 | Suggests: 38 | picante, 39 | httr, 40 | NST, 41 | permute, 42 | aplot, 43 | ggfun, 44 | pheatmap, 45 | MASS, 46 | Rtsne, 47 | mixOmics, 48 | geosphere, 49 | phyloseq, 50 | phyloseqGraphTest, 51 | plotly, 52 | umap, 53 | Hmisc, 54 | minpack.lm, 55 | bbmle, 56 | snow, 57 | foreach, 58 | doSNOW, 59 | tidytree, 60 | ggtree, 61 | ggtreeExtra, 62 | vctrs, 63 | zoo, 64 | ape, 65 | DESeq2, 66 | limma, 67 | ALDEx2, 68 | Mfuzz, 69 | edgeR, 70 | methods, 71 | randomForest, 72 | knitr, 73 | rmarkdown, 74 | MetaNet, 75 | showtext, 76 | jsonlite, 77 | prettydoc, 78 | readxl, 79 | clipr, 80 | zetadiv, 81 | ggforce, 82 | AnnotationDbi, 83 | org.Hs.eg.db, 84 | linkET, 85 | mediation, 86 | pairwiseAdonis, 87 | sankeyD3, 88 | Biobase, 89 | GEOquery 90 | VignetteBuilder: knitr 91 | BugReports: https://github.com/Asa12138/pctax/issues 92 | URL: https://github.com/Asa12138/pctax 93 | ByteCompile: true 94 | biocViews: Microbiome, Software, Visualization 95 | -------------------------------------------------------------------------------- /man/taxonkit_filter.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/Rtaxonkit.R 3 | \name{taxonkit_filter} 4 | \alias{taxonkit_filter} 5 | \title{Filter TaxIDs based on Taxonomic Ranks} 6 | \usage{ 7 | taxonkit_filter( 8 | file_path, 9 | black_list = NULL, 10 | discard_noranks = FALSE, 11 | discard_root = FALSE, 12 | equal_to = NULL, 13 | higher_than = NULL, 14 | lower_than = NULL, 15 | rank_file = NULL, 16 | root_taxid = NULL, 17 | save_predictable_norank = FALSE, 18 | taxid_field = NULL, 19 | text = FALSE, 20 | data_dir = NULL 21 | ) 22 | } 23 | \arguments{ 24 | \item{file_path}{The path to the input file containing TaxIDs. Or file text (text=TRUE)} 25 | 26 | \item{black_list}{A character vector specifying the ranks to discard.} 27 | 28 | \item{discard_noranks}{Logical value indicating whether to discard all ranks without order (default is FALSE).} 29 | 30 | \item{discard_root}{Logical value indicating whether to discard the root taxid (default is FALSE).} 31 | 32 | \item{equal_to}{A character vector specifying the ranks for which TaxIDs should be equal to.} 33 | 34 | \item{higher_than}{The rank above which the TaxIDs should be (exclusive).} 35 | 36 | \item{lower_than}{The rank below which the TaxIDs should be (exclusive).} 37 | 38 | \item{rank_file}{The path to a user-defined ordered taxonomic ranks file.} 39 | 40 | \item{root_taxid}{The root taxid (default is 1).} 41 | 42 | \item{save_predictable_norank}{Logical value indicating whether to save some special ranks without order when using lower_than (default is FALSE).} 43 | 44 | \item{taxid_field}{The field index of the taxid in the input file (default is 1).} 45 | 46 | \item{text}{logical} 47 | 48 | \item{data_dir}{directory containing nodes.dmp and names.dmp (default "/Users/asa/.taxonkit")} 49 | } 50 | \value{ 51 | A character vector containing the output of the "taxonkit filter" command. 52 | } 53 | \description{ 54 | This function uses the "taxonkit filter" command to filter TaxIDs based on taxonomic ranks. 55 | } 56 | \examples{ 57 | \dontrun{ 58 | taxids2 <- system.file("extdata/taxids2.txt", package = "pctax") 59 | taxonkit_filter(taxids2, lower_than = "genus") 60 | } 61 | } 62 | \seealso{ 63 | Other Rtaxonkit: 64 | \code{\link{check_taxonkit}()}, 65 | \code{\link{download_taxonkit_dataset}()}, 66 | \code{\link{install_taxonkit}()}, 67 | \code{\link{name_or_id2df}()}, 68 | \code{\link{taxonkit_lca}()}, 69 | \code{\link{taxonkit_lineage}()}, 70 | \code{\link{taxonkit_list}()}, 71 | \code{\link{taxonkit_name2taxid}()}, 72 | \code{\link{taxonkit_reformat}()} 73 | } 74 | \concept{Rtaxonkit} 75 | -------------------------------------------------------------------------------- /R/stats.R: -------------------------------------------------------------------------------- 1 | #' Batch mediate 2 | #' 3 | #' @param data data.frame with two columns: X (independent variable) and Y (dependent variable). 4 | #' @param mediator_df data.frame with mediators, each column representing a different mediator variable. 5 | #' @param nsims Number of bootstrap simulations for estimating confidence intervals (default is 500). 6 | #' @param conf.level Confidence level for the confidence intervals (default is 0.95). 7 | #' 8 | #' @return data.frame 9 | #' 10 | #' @examples 11 | #' set.seed(123) 12 | #' n <- 200 13 | #' X <- rnorm(n) 14 | #' M1 <- 0.5 * X + rnorm(n) 15 | #' M2 <- 0.3 * X + rnorm(n) 16 | #' M3 <- 0.1 * X + rnorm(n) 17 | #' Y <- 0.3 * X + 0.4 * M1 + 0.2 * M2 + rnorm(n) 18 | #' data <- data.frame(X, Y) 19 | #' mediators <- data.frame(M1, M2, M3) 20 | #' if (requireNamespace("mediation")) { 21 | #' results <- batch_mediate(data, mediators) 22 | #' print(results) 23 | #' } 24 | #' @export 25 | batch_mediate <- function(data, mediator_df, nsims = 500, conf.level = 0.95) { 26 | # 检查必要的包是否已安装 27 | lib_ps("mediation", library = FALSE) 28 | 29 | message("Use the first two columns of data as X and Y") 30 | colnames(data) <- c("X", "Y") 31 | 32 | # 初始化结果数据框 33 | results <- data.frame( 34 | Mediator = character(), 35 | ACME = numeric(), # 平均因果中介效应 36 | ADE = numeric(), # 平均直接效应 37 | Total_Effect = numeric(), # 总效应 38 | Prop_Mediated = numeric(), # 中介比例 39 | ACME_p = numeric(), # ACME的p值 40 | ADE_p = numeric(), # ADE的p值 41 | Total_p = numeric(), # 总效应的p值 42 | stringsAsFactors = FALSE 43 | ) 44 | 45 | # 循环处理每个中介变量 46 | for (mediator_name in colnames(mediator_df)) { 47 | # 合并数据 48 | full_data <- cbind(data, mediator_df[mediator_name]) 49 | colnames(full_data)[ncol(full_data)] <- "M_temp" 50 | 51 | # 拟合模型 52 | model.m <- lm(M_temp ~ X, data = full_data[, c("X", "M_temp"), drop = FALSE]) 53 | model.y <- lm(Y ~ ., data = full_data[, c("Y", "X", "M_temp")]) 54 | 55 | # 进行中介分析 56 | med <- mediation::mediate( 57 | model.m, 58 | model.y, 59 | treat = "X", 60 | mediator = "M_temp", 61 | boot = TRUE, 62 | sims = nsims, 63 | conf.level = conf.level 64 | ) 65 | 66 | # 提取结果 67 | res_row <- data.frame( 68 | Mediator = mediator_name, 69 | ACME = med$d0, 70 | ADE = med$z0, 71 | Total_Effect = med$tau.coef, 72 | Prop_Mediated = med$n0, 73 | ACME_p = med$d0.p, 74 | ADE_p = med$z0.p, 75 | Total_p = med$tau.p, 76 | stringsAsFactors = FALSE 77 | ) 78 | 79 | # 添加到结果数据框 80 | results <- rbind(results, res_row) 81 | } 82 | 83 | # 计算置信区间 84 | ci_lower <- (1 - conf.level) / 2 85 | ci_upper <- conf.level + ci_lower 86 | 87 | return(results) 88 | } 89 | -------------------------------------------------------------------------------- /.github/workflows/pr-commands.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | issue_comment: 5 | types: [created] 6 | 7 | name: Commands 8 | 9 | jobs: 10 | document: 11 | if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/document') }} 12 | name: document 13 | runs-on: ubuntu-latest 14 | env: 15 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 16 | steps: 17 | - uses: actions/checkout@v4 18 | 19 | - uses: r-lib/actions/pr-fetch@v2 20 | with: 21 | repo-token: ${{ secrets.GITHUB_TOKEN }} 22 | 23 | - uses: r-lib/actions/setup-r@v2 24 | with: 25 | use-public-rspm: true 26 | 27 | - uses: r-lib/actions/setup-r-dependencies@v2 28 | with: 29 | extra-packages: any::roxygen2 30 | needs: pr-document 31 | dependencies: '"hard"' 32 | 33 | - name: Document 34 | run: roxygen2::roxygenise() 35 | shell: Rscript {0} 36 | 37 | - name: commit 38 | run: | 39 | git config --local user.name "$GITHUB_ACTOR" 40 | git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" 41 | git add man/\* NAMESPACE 42 | git commit -m 'Document' 43 | 44 | - uses: r-lib/actions/pr-push@v2 45 | with: 46 | repo-token: ${{ secrets.GITHUB_TOKEN }} 47 | 48 | style: 49 | if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/style') }} 50 | name: style 51 | runs-on: ubuntu-latest 52 | env: 53 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 54 | steps: 55 | - uses: actions/checkout@v4 56 | 57 | - uses: r-lib/actions/pr-fetch@v2 58 | with: 59 | repo-token: ${{ secrets.GITHUB_TOKEN }} 60 | 61 | - uses: r-lib/actions/setup-r@v2 62 | 63 | - name: Install dependencies 64 | run: install.packages("styler") 65 | shell: Rscript {0} 66 | 67 | - name: Style 68 | run: styler::style_pkg() 69 | shell: Rscript {0} 70 | 71 | - name: commit 72 | run: | 73 | git config --local user.name "$GITHUB_ACTOR" 74 | git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" 75 | git add \*.R 76 | git commit -m 'Style' 77 | 78 | - uses: r-lib/actions/pr-push@v2 79 | with: 80 | repo-token: ${{ secrets.GITHUB_TOKEN }} 81 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # pctax 5 | 6 | 7 | 8 | [![R-CMD-check](https://github.com/Asa12138/pctax/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/Asa12138/pctax/actions/workflows/R-CMD-check.yaml) 9 | [![](https://img.shields.io/badge/blog-@asa-blue.svg)](https://asa-blog.netlify.app/) 10 | [![](http://cranlogs.r-pkg.org/badges/grand-total/pctax)](https://cran.r-project.org/package=pctax) 11 | [![](http://cranlogs.r-pkg.org/badges/last-month/pctax)](https://cran.r-project.org/package=pctax) 12 | [![](https://www.r-pkg.org/badges/version/pctax?color=green)](https://cran.r-project.org/package=pctax) 13 | [![](https://img.shields.io/badge/devel%20version-0.1.2-green.svg)](https://github.com/Asa12138/pctax) 14 | 15 | 16 | `pctax` provides a comprehensive suite of tools for analyzing omics 17 | data. 18 | 19 | The HTML documentation of the latest version is available at [Github 20 | page](https://asa12138.github.io/pctax/). 21 | 22 | Please go to for the full 23 | vignette. 24 | 25 | ## Installation 26 | 27 | The stable version is available on CRAN: 28 | 29 | install.packages("pctax") 30 | 31 | Or you can install the development version of pctax from 32 | [GitHub](https://github.com/) with: 33 | 34 | ``` r 35 | # install.packages("devtools") 36 | devtools::install_github("Asa12138/pctax") 37 | ``` 38 | 39 | ## 🚀 NEWS 🚀 40 | 41 | Recently, I added a function to draw two trees and their relationships: 42 | 43 | ``` r 44 | data(otutab, package = "pcutils") 45 | df2tree(taxonomy[1:50, ]) -> tax_tree 46 | df2tree(taxonomy[51:100, ]) -> tax_tree2 47 | link <- data.frame(from = sample(tax_tree$tip.label, 20), to = sample(tax_tree2$tip.label, 20)) 48 | plot_two_tree(tax_tree, tax_tree2, link, 49 | tree1_tip = T, tree2_tip = T, 50 | tip1_param = list(size = 2), tip2_param = list(size = 2) 51 | ) 52 | ``` 53 | 54 |
55 | 56 | Two trees and their relationships 57 |

58 | Two trees and their relationships 59 |

60 | 61 |
62 | 63 | Recently I added a function to plot element cycling because element 64 | cycling genes are important in the microbiome (especially the 65 | environmental microbiome). Supports simple cycle diagram drawing of C, 66 | N, P, S, Fe (manual arrangement, there must be some missing parts, will 67 | be continuously added in the future): 68 | 69 | ``` r 70 | plot_element_cycle(cycle = "Nitrogen cycle") 71 | #> recommend ggsave(width = 12,height = 10) 72 | ``` 73 | 74 |
75 | 76 | Nitrogen cycle 77 |

78 | Nitrogen cycle 79 |

80 | 81 |
82 | 83 | ## Usage 84 | 85 | For the full vignette, please visit [pctax: Analyzing Omics Data with 86 | R](https://bookdown.org/Asa12138/pctax_book/). 87 | 88 | **Some Functionalities of `pctax`:** 89 | 90 | ![](man/figures/pctax1.png) 91 | 92 | ## Citation 93 | 94 | Please cite: 95 | 96 | Chen Peng, Chao Jiang (2023). *pctax: Professional Comprehensive 97 | Microbiome Data Analysis Pipeline*. R package, 98 | . 99 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | S3method(a_diversity,data.frame) 4 | S3method(a_diversity,numeric) 5 | S3method(a_diversity,pc_otu) 6 | S3method(aor,data.frame) 7 | S3method(as.dist,b_dist) 8 | S3method(b_analyse,data.frame) 9 | S3method(plot,AOR) 10 | S3method(plot,NTI_RC) 11 | S3method(plot,a_res) 12 | S3method(plot,b_dist) 13 | S3method(plot,b_res) 14 | S3method(plot,dist) 15 | S3method(plot,g_test) 16 | S3method(plot,mant_g) 17 | S3method(plot,ncm_res) 18 | S3method(plot,pro_res) 19 | S3method(plot,rare_res) 20 | S3method(plot,time_cm) 21 | S3method(plot,zeta_decay) 22 | S3method(plot,zeta_res) 23 | S3method(print,pc_otu) 24 | S3method(summary,pc_otu) 25 | export("%<>%") 26 | export("%>%") 27 | export(ALDEX) 28 | export(RCbray1) 29 | export(RDA_plot) 30 | export(a_diversity) 31 | export(add_strip) 32 | export(add_tax) 33 | export(ann_tree) 34 | export(aor) 35 | export(as.b_dist) 36 | export(b_NTI1) 37 | export(b_analyse) 38 | export(b_res_3d) 39 | export(batch_mediate) 40 | export(bbtt) 41 | export(before_tree) 42 | export(check_taxonkit) 43 | export(convert_taxon_name) 44 | export(cor_net) 45 | export(df2tree) 46 | export(df2tree1) 47 | export(diff_da) 48 | export(download_taxonkit_dataset) 49 | export(drop_tips_update) 50 | export(easy_tree) 51 | export(envfitt) 52 | export(gene2id) 53 | export(geo_sim) 54 | export(get_diff_type) 55 | export(gp_dis_density) 56 | export(grap_p_test) 57 | export(install_taxonkit) 58 | export(kwtest) 59 | export(load_N_data) 60 | export(load_mpa_df) 61 | export(m_group_env) 62 | export(mat_dist) 63 | export(micro_sbatch) 64 | export(multi_bar) 65 | export(multi_conf) 66 | export(myCAP) 67 | export(myCCA) 68 | export(myRDA) 69 | export(name_or_id2df) 70 | export(ncm) 71 | export(nst) 72 | export(nti_rc) 73 | export(pc_otu) 74 | export(pc_valid) 75 | export(permanova) 76 | export(plot_N_cycle) 77 | export(plot_element_cycle) 78 | export(plot_two_tree) 79 | export(pre_GEO) 80 | export(pre_assembly_stats) 81 | export(pre_fastp) 82 | export(pre_tax_table) 83 | export(procrustes_analyse) 84 | export(rare_curve_sample) 85 | export(rare_curve_species) 86 | export(rarefaction) 87 | export(sangji_plot) 88 | export(stamp_plot) 89 | export(suijisenlin) 90 | export(sunburst) 91 | export(tax_lca) 92 | export(taxonkit_filter) 93 | export(taxonkit_lca) 94 | export(taxonkit_lineage) 95 | export(taxonkit_list) 96 | export(taxonkit_name2taxid) 97 | export(taxonkit_reformat) 98 | export(time_by_cm) 99 | export(volcano_p) 100 | export(z_diversity) 101 | export(z_diversity_decay) 102 | import(dplyr) 103 | import(ggplot2) 104 | import(scales) 105 | importFrom(ggplot2,geom_tile) 106 | importFrom(magrittr,"%<>%") 107 | importFrom(magrittr,"%>%") 108 | importFrom(pcutils,change_fac_lev) 109 | importFrom(pcutils,dabiao) 110 | importFrom(pcutils,generate_labels) 111 | importFrom(pcutils,get_cols) 112 | importFrom(pcutils,hebing) 113 | importFrom(pcutils,lib_ps) 114 | importFrom(pcutils,t2) 115 | importFrom(pcutils,trans) 116 | importFrom(pcutils,update_param) 117 | importFrom(reshape2,melt) 118 | importFrom(stats,aggregate) 119 | importFrom(stats,anova) 120 | importFrom(stats,as.dist) 121 | importFrom(stats,kmeans) 122 | importFrom(stats,median) 123 | importFrom(stats,na.omit) 124 | importFrom(stats,relevel) 125 | importFrom(stats,runif) 126 | importFrom(stats,sd) 127 | importFrom(stats,setNames) 128 | importFrom(stats,t.test) 129 | importFrom(stats,time) 130 | importFrom(stats,var) 131 | importFrom(tibble,column_to_rownames) 132 | importFrom(tibble,rownames_to_column) 133 | importFrom(utils,data) 134 | importFrom(utils,head) 135 | importFrom(utils,tail) 136 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: 3 | github_document: 4 | html_preview: false 5 | --- 6 | 7 | 8 | 9 | 10 | ```{r, include = FALSE} 11 | knitr::opts_chunk$set( 12 | collapse = TRUE, 13 | comment = "#>", 14 | fig.path = "man/figures/README-", 15 | out.width = "100%" 16 | ) 17 | devtools::load_all("~/Documents/R/pctax/pctax/") 18 | library(pcutils) 19 | library(badger) 20 | ``` 21 | 22 | # pctax 23 | 24 | 25 | [![R-CMD-check](https://github.com/Asa12138/pctax/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/Asa12138/pctax/actions/workflows/R-CMD-check.yaml) 26 | `r badge_custom("blog", "@asa", "blue", "https://asa-blog.netlify.app/")` 27 | `r badge_cran_download("pctax", type = "grand-total")` 28 | `r badge_cran_download("pctax", type = "last-month")` 29 | `r badge_cran_release("pctax", "green")` 30 | `r badge_devel("Asa12138/pctax", color = "green")` 31 | 32 | 33 | `pctax` provides a comprehensive suite of tools for analyzing omics data. 34 | 35 | The HTML documentation of the latest version is available at [Github page](https://asa12138.github.io/pctax/). 36 | 37 | Please go to for the full vignette. 38 | 39 | ## Installation 40 | 41 | The stable version is available on CRAN: 42 | 43 | ``` 44 | install.packages("pctax") 45 | ``` 46 | 47 | Or you can install the development version of pctax from [GitHub](https://github.com/) with: 48 | 49 | ``` r 50 | # install.packages("devtools") 51 | devtools::install_github("Asa12138/pctax") 52 | ``` 53 | 54 | ## 🚀 NEWS 🚀 55 | 56 | Recently, I added a function to draw two trees and their relationships: 57 | 58 | ```{r fig.width = 9, fig.height = 5, fig.cap = "Two trees and their relationships", cache = FALSE} 59 | data(otutab, package = "pcutils") 60 | df2tree(taxonomy[1:50, ]) -> tax_tree 61 | df2tree(taxonomy[51:100, ]) -> tax_tree2 62 | link <- data.frame(from = sample(tax_tree$tip.label, 20), to = sample(tax_tree2$tip.label, 20)) 63 | plot_two_tree(tax_tree, tax_tree2, link, 64 | tree1_tip = T, tree2_tip = T, 65 | tip1_param = list(size = 2), tip2_param = list(size = 2) 66 | ) 67 | ``` 68 | 69 | 70 | Recently I added a function to plot element cycling because element cycling genes are important in the microbiome (especially the environmental microbiome). Supports simple cycle diagram drawing of C, N, P, S, Fe (manual arrangement, there must be some missing parts, will be continuously added in the future): 71 | 72 | ```{r fig.width = 12, fig.height = 10, fig.cap = "Nitrogen cycle", cache = FALSE} 73 | plot_element_cycle(cycle = "Nitrogen cycle") 74 | ``` 75 | 76 | ## Usage 77 | 78 | For the full vignette, please visit [pctax: Analyzing Omics Data with R](https://bookdown.org/Asa12138/pctax_book/). 79 | 80 | **Some Functionalities of `pctax`:** 81 | 82 | ```{r include=FALSE,eval=FALSE} 83 | DiagrammeR::DiagrammeR(diagram = "flowchart LR 84 | B(pctax)--> C{Functionalities} 85 | C --> D[Visualization] 86 | C --> E[Diversity Analysis] 87 | C --> F[Differential Abundance Analysis] 88 | C --> G[Community Assembly Analysis] 89 | C --> H[Functional Enrichment Analysis] 90 | C --> I[Network Analysis] 91 | C --> J[Elemental Cycling Analysis]") 92 | ``` 93 | 94 | ![](man/figures/pctax1.png) 95 | 96 | ## Citation 97 | Please cite: 98 | 99 | Chen Peng, Chao Jiang (2023). _pctax: Professional Comprehensive Microbiome Data Analysis Pipeline_. R package, . 100 | 101 | ```{r include=FALSE,eval=FALSE} 102 | library(hexSticker) 103 | showtext::showtext_auto() 104 | sticker("~/Documents/R/test/icons/enrichment.png", 105 | package = "ReporterScore", 106 | p_size = 17, p_color = "#0C359E", p_y = 1.4, 107 | p_fontface = "bold.italic", p_family = "Comic Sans MS", 108 | s_x = 1, s_y = .75, s_width = 0.6, s_height = 0.6, 109 | h_fill = "#F6F5F5", h_color = "#2D9596", 110 | filename = "man/figures/ReporterScore.png", dpi = 300 111 | ) 112 | ``` 113 | -------------------------------------------------------------------------------- /man/taxonkit_reformat.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/Rtaxonkit.R 3 | \name{taxonkit_reformat} 4 | \alias{taxonkit_reformat} 5 | \title{Reformat Taxonomic Lineage using taxonkit} 6 | \usage{ 7 | taxonkit_reformat( 8 | file_path, 9 | delimiter = NULL, 10 | add_prefix = FALSE, 11 | prefix_kingdom = "K__", 12 | prefix_phylum = "p__", 13 | prefix_class = "c__", 14 | prefix_order = "o__", 15 | prefix_family = "f__", 16 | prefix_genus = "g__", 17 | prefix_species = "s__", 18 | prefix_subspecies = "t__", 19 | prefix_strain = "T__", 20 | fill_miss_rank = FALSE, 21 | format_string = "", 22 | miss_rank_repl_prefix = "unclassified ", 23 | miss_rank_repl = "", 24 | miss_taxid_repl = "", 25 | output_ambiguous_result = FALSE, 26 | lineage_field = 2, 27 | taxid_field = NULL, 28 | pseudo_strain = FALSE, 29 | trim = FALSE, 30 | text = FALSE, 31 | data_dir = NULL 32 | ) 33 | } 34 | \arguments{ 35 | \item{file_path}{The path to the input file with taxonomic lineages. Or file text (text=TRUE)} 36 | 37 | \item{delimiter}{The field delimiter in the input lineage (default ";").} 38 | 39 | \item{add_prefix}{Logical, indicating whether to add prefixes for all ranks (default: FALSE).} 40 | 41 | \item{prefix_kingdom}{The prefix for kingdom, used along with --add-prefix (default: "K__").} 42 | 43 | \item{prefix_phylum}{The prefix for phylum, used along with --add-prefix (default: "p__").} 44 | 45 | \item{prefix_class}{The prefix for class, used along with --add-prefix (default: "c__").} 46 | 47 | \item{prefix_order}{The prefix for order, used along with --add-prefix (default: "o__").} 48 | 49 | \item{prefix_family}{The prefix for family, used along with --add-prefix (default: "f__").} 50 | 51 | \item{prefix_genus}{The prefix for genus, used along with --add-prefix (default: "g__").} 52 | 53 | \item{prefix_species}{The prefix for species, used along with --add-prefix (default: "s__").} 54 | 55 | \item{prefix_subspecies}{The prefix for subspecies, used along with --add-prefix (default: "t__").} 56 | 57 | \item{prefix_strain}{The prefix for strain, used along with --add-prefix (default: "T__").} 58 | 59 | \item{fill_miss_rank}{Logical, indicating whether to fill missing rank with lineage information of the next higher rank (default: FALSE).} 60 | 61 | \item{format_string}{The output format string with placeholders for each rank.} 62 | 63 | \item{miss_rank_repl_prefix}{The prefix for estimated taxon level for missing rank (default: "unclassified ").} 64 | 65 | \item{miss_rank_repl}{The replacement string for missing rank.} 66 | 67 | \item{miss_taxid_repl}{The replacement string for missing taxid.} 68 | 69 | \item{output_ambiguous_result}{Logical, indicating whether to output one of the ambiguous result (default: FALSE).} 70 | 71 | \item{lineage_field}{The field index of lineage. Input data should be tab-separated (default: 2).} 72 | 73 | \item{taxid_field}{The field index of taxid. Input data should be tab-separated. It overrides -i/--lineage-field.} 74 | 75 | \item{pseudo_strain}{Logical, indicating whether to use the node with lowest rank as strain name (default: FALSE).} 76 | 77 | \item{trim}{Logical, indicating whether to not fill missing rank lower than current rank (default: FALSE).} 78 | 79 | \item{text}{logical} 80 | 81 | \item{data_dir}{directory containing nodes.dmp and names.dmp (default "/Users/asa/.taxonkit")} 82 | } 83 | \value{ 84 | A character vector containing the reformatted taxonomic lineages. 85 | } 86 | \description{ 87 | Reformat Taxonomic Lineage using taxonkit 88 | } 89 | \examples{ 90 | \dontrun{ 91 | # Use taxid 92 | taxids2 <- system.file("extdata/taxids2.txt", package = "pctax") 93 | reformatted_lineages <- taxonkit_reformat(taxids2, 94 | add_prefix = TRUE, taxid_field = 1, fill_miss_rank = TRUE 95 | ) 96 | reformatted_lineages 97 | taxonomy <- strsplit2(reformatted_lineages, "\t") 98 | taxonomy <- strsplit2(taxonomy$V2, ";") 99 | 100 | # Use lineage result 101 | taxonkit_lineage("9606\n63221", show_name = TRUE, show_rank = TRUE, text = TRUE) \%>\% 102 | taxonkit_reformat(text = TRUE) 103 | } 104 | } 105 | \seealso{ 106 | Other Rtaxonkit: 107 | \code{\link{check_taxonkit}()}, 108 | \code{\link{download_taxonkit_dataset}()}, 109 | \code{\link{install_taxonkit}()}, 110 | \code{\link{name_or_id2df}()}, 111 | \code{\link{taxonkit_filter}()}, 112 | \code{\link{taxonkit_lca}()}, 113 | \code{\link{taxonkit_lineage}()}, 114 | \code{\link{taxonkit_list}()}, 115 | \code{\link{taxonkit_name2taxid}()} 116 | } 117 | \concept{Rtaxonkit} 118 | -------------------------------------------------------------------------------- /R/pc_otu.R: -------------------------------------------------------------------------------- 1 | #' Create a pc_otu class object 2 | #' 3 | #' @param otutab an otutab data.frame, samples are columns, taxs are rows. 4 | #' @param metadata a metadata data.frame, samples are rows 5 | #' @param taxonomy a taxomomy data.frame, look out the rowname of taxonomy and otutab should matched! 6 | #' @param ... add 7 | #' 8 | #' @export 9 | #' @return pc_otu 10 | #' @examples 11 | #' data(otutab, package = "pcutils") 12 | #' pc_tax1 <- pc_otu(otutab, metadata) 13 | #' print(pc_tax1) 14 | pc_otu <- function(otutab = data.frame(), metadata = data.frame(), taxonomy = NULL, ...) { 15 | if (!is.data.frame(otutab)) stop("otutab must be a df!") 16 | if (!is.data.frame(metadata)) stop("metadata must be a df!") 17 | if (!is.null(taxonomy)) { 18 | if (!is.data.frame(taxonomy)) stop("taxonomy must be a df!") 19 | if (length(taxonomy) != 7) stop("taxonomy should be a df with 'k,p,c,o,f,g,s'!") 20 | pre_tax_table(taxonomy) -> taxonomy 21 | taxonomy[match(rownames(otutab), rownames(taxonomy)), ] %>% na.omit() -> taxonomy 22 | } 23 | ids <- intersect(rownames(metadata), colnames(otutab)) 24 | if (length(ids) < 1) stop("no samples left, check the rownames of metadata or colnames of otutable") 25 | metadata <- metadata[ids, , drop = FALSE] 26 | otutab <- otutab[, ids, drop = FALSE] 27 | otutab <- otutab[rowSums(otutab) > 0, , drop = FALSE] 28 | pc <- list( 29 | tbls = list(otutab = otutab), 30 | metas = list(metadata = metadata), 31 | otus = list(taxonomy = taxonomy), ... 32 | ) 33 | class(pc) <- append("pc_otu", class(pc)) 34 | return(pc) 35 | } 36 | 37 | #' Judge pc_otu is valid or not 38 | #' 39 | #' @param pc a pc_otu object 40 | #' @return logical 41 | #' @export 42 | pc_valid <- function(pc) { 43 | for (i in pc$tbls) if (!is.data.frame(i) & !is.null(i)) stop("tbls must be df!") 44 | for (i in pc$metas) if (!is.data.frame(i) & !is.null(i)) stop("metas must be df!") 45 | for (i in pc$otus) if (!is.data.frame(i) & !is.null(i)) stop("otus must be df!") 46 | if (!all(rownames(pc$metas$metadata) == colnames(pc$tbls$otutab))) stop("Wrong samples! check the rownames of metadata or colnames of otutable") 47 | return(TRUE) 48 | } 49 | 50 | #' Print 51 | #' 52 | #' @param x pc_otu 53 | #' @param ... add 54 | #' 55 | #' @return No value 56 | #' @method print pc_otu 57 | #' @exportS3Method 58 | print.pc_otu <- function(x, ...) { 59 | pc <- x 60 | sprintf("There are %d otus and %d samples!", nrow(pc$tbls$otutab), ncol(pc$tbls$otutab)) %>% print() 61 | for (i in names(pc)) { 62 | pcutils::dabiao(i, print = TRUE) 63 | if (i %in% c("tbls", "metas", "otus")) { 64 | for (j in names(pc[[i]])) { 65 | pcutils::dabiao(j, n = 40, print = TRUE) 66 | if ("data.frame" %in% class(pc[[i]][[j]])) { 67 | print(head(pc[[i]][[j]])) 68 | } else { 69 | print(pc[[i]][[j]]) 70 | } 71 | } 72 | } else { 73 | if ("data.frame" %in% class(pc[[i]])) { 74 | print(head(pc[[i]])) 75 | } else { 76 | print(pc[[i]]) 77 | } 78 | } 79 | } 80 | } 81 | 82 | #' Summary pc_otu 83 | #' @param object pc_otu 84 | #' @param ... add 85 | #' @return No value 86 | #' @method summary pc_otu 87 | #' @exportS3Method 88 | #' @examples 89 | #' data("pc_tax1") 90 | #' summary(pc_tax1) 91 | #' 92 | summary.pc_otu <- function(object, ...) { 93 | pc <- object 94 | pc_valid(pc) 95 | sprintf("There are %d otus and %d samples!", nrow(pc$tbls$otutab), ncol(pc$tbls$otutab)) %>% print() 96 | pcutils::dabiao("tables, include some data filter and tranformat", print = TRUE) 97 | print(names(pc$tbls)) 98 | pcutils::dabiao("metadatas, include some statistics", print = TRUE) 99 | print(names(pc$metas)) 100 | pcutils::dabiao("otus annotation, include some statistics", print = TRUE) 101 | print(names(pc$otus)) 102 | pcutils::dabiao("some other indexs:", print = TRUE) 103 | print(names(pc)[-1:-3]) 104 | } 105 | 106 | #' Add taxonomy for a pc_otu object 107 | #' @export 108 | #' @param pc a pc_otu object 109 | #' @param taxonomy a taxomomy data.frame, look out the rownames of taxonomy and otutab should matched! 110 | #' @return pc_otu 111 | #' @examples 112 | #' data(otutab, package = "pcutils") 113 | #' pc_tax1 <- pc_otu(otutab, metadata) 114 | #' pc_tax1 <- add_tax(pc_tax1, taxonomy) 115 | add_tax <- function(pc, taxonomy) { 116 | if (!"pc_otu" %in% class(pc)) stop(pc, "should be a pc_otu") 117 | if (!is.data.frame(taxonomy)) stop("taxonomy must be a df!") 118 | if (length(taxonomy) != 7) stop("taxonomy should be a df with 'k,p,c,o,f,g,s'!") 119 | 120 | pre_tax_table(taxonomy) -> taxonomy 121 | taxonomy[match(rownames(pc$tbls$otutab), rownames(taxonomy)), ] %>% na.omit() -> taxonomy 122 | pc$otus$taxonomy <- taxonomy 123 | return(pc) 124 | } 125 | -------------------------------------------------------------------------------- /R/rarefy.R: -------------------------------------------------------------------------------- 1 | # rare========== 2 | 3 | #' Rarefy a otutab 4 | #' 5 | #' @param otutab otutab 6 | #' @param sample number 7 | #' 8 | #' @return a rarefied otutab 9 | #' @export 10 | #' 11 | #' @examples 12 | #' data(otutab, package = "pcutils") 13 | #' rarefaction(otutab) 14 | rarefaction <- function(otutab, sample = NULL) { 15 | if (is.null(sample)) sample <- min(colSums(otutab)) 16 | otutab1 <- vegan::rrarefy(t(otutab), sample = sample) 17 | return(as.data.frame(t(otutab1))) 18 | } 19 | 20 | #' Rare the sample 21 | #' 22 | #' @param otutab otutab 23 | #' @param rep repeats number 24 | #' @param count_cutoff cutoff to be 0 25 | #' 26 | #' @export 27 | #' @return ggplot 28 | #' @examples 29 | #' data(otutab, package = "pcutils") 30 | #' a <- rare_curve_sample(otutab) 31 | #' plot(a) 32 | rare_curve_sample <- function(otutab, rep = 30, count_cutoff = 1) { 33 | # 默认值绘制样本箱线图稀释曲线 34 | count <- otutab 35 | count[count < count_cutoff] <- 0 36 | count[count >= count_cutoff] <- 1 37 | n <- dim(count)[2] 38 | x <- unique(as.integer(seq(1, n, length.out = n))) 39 | result <- data.frame(sample = c(0), richness = c(0)) 40 | for (i in x) { 41 | m <- choose(n, i) 42 | if (m > rep) { 43 | m <- rep 44 | } 45 | for (j in 1:m) { 46 | idx <- sample(n, i) 47 | temp <- count[, idx, drop = FALSE] 48 | temp1 <- temp[rowSums(temp) > 0, , drop = FALSE] 49 | result <- rbind(result, c(i, dim(temp1)[1])) 50 | } 51 | } 52 | result <- result[-1, ] 53 | result$sample <- factor(result$sample, levels = unique(result$sample)) 54 | attributes(result)$type <- "sample" 55 | class(result) <- c("rare_res", class(result)) 56 | return(result) 57 | } 58 | 59 | #' Plot a rare curve 60 | #' 61 | #' @param x AOR object 62 | #' @param ... add 63 | #' 64 | #' @rdname rare_curve_sample 65 | #' @method plot rare_res 66 | #' @exportS3Method 67 | #' @return ggplot 68 | plot.rare_res <- function(x, ...) { 69 | result <- x 70 | richness <- rare <- NULL 71 | if (attributes(result)$type == "sample") { 72 | p <- ggplot(result, aes(x = sample, y = richness, fill = sample)) + 73 | geom_boxplot(outlier.size = 0.2, outlier.alpha = 0.5, size = 0.1) + 74 | xlab("Sample size") + 75 | ylab("Richness") + 76 | theme(legend.position = "none") + 77 | theme_bw() 78 | } 79 | if (attributes(result)$type == "species") { 80 | p <- ggplot(result, aes(x = as.numeric(rare), y = alpha, color = sample)) + 81 | geom_line() + 82 | geom_errorbar(aes(ymin = alpha - sd, ymax = alpha + sd)) + 83 | geom_point(size = 1) + 84 | labs( 85 | x = ifelse(attributes(result)$mode == 1, "reads (count)", "reads (cpm)"), 86 | y = attributes(result)$method 87 | ) + 88 | theme_bw() 89 | } 90 | p 91 | } 92 | 93 | #' Rare the species 94 | #' 95 | #' @param otutab otutab 96 | #' @param step default 2000 97 | #' @param method one of "richness","chao1","ace","gc","shannon","simpson","pd","pielou" 98 | #' @param mode 1 for little table, 2 for big 99 | #' @param verbose verbose 100 | #' @param threads use how many threads to calculate (default:1) 101 | #' @param reps reps 102 | #' 103 | #' @export 104 | #' @return ggplot 105 | #' @examples 106 | #' data(otutab, package = "pcutils") 107 | #' a <- rare_curve_species(otutab, mode = 1) 108 | #' plot(a) 109 | rare_curve_species <- function(otutab, step = 2000, method = "richness", mode = 2, 110 | reps = 3, threads = 1, verbose = TRUE) { 111 | i <- rare <- NULL 112 | # 根据抽样步长(step),统计每个稀释梯度下的 Alpha 多样性指数,结果返回至列表 113 | alpha_curves <- \(x, step, method = "richness", tree = NULL) { 114 | x_nrow <- nrow(x) 115 | rare <- rowSums(x) 116 | 117 | alpha_rare <- data.frame() 118 | for (i in 1:x_nrow) { 119 | step_num <- seq(0, rare[i], step) 120 | if (max(step_num) < rare[i]) step_num <- c(step_num, rare[i]) 121 | alpha_rare_i <- NULL 122 | for (step_num_n in step_num) { 123 | pctax::a_diversity(data.frame(t(vegan::rrarefy(x[i, ], step_num_n))), method = method, tree = tree) -> tmp_i 124 | alpha_rare_i <- c(alpha_rare_i, unlist(tmp_i)) 125 | } 126 | alpha_rare <- rbind( 127 | alpha_rare, 128 | data.frame(rare = step_num, alpha = alpha_rare_i, sample = rownames(x)[i]) 129 | ) 130 | } 131 | alpha_rare 132 | } 133 | 134 | if (mode == 1) { 135 | t(otutab) -> totu 136 | } else { 137 | t(round(pcutils::trans(otutab, "cpm"), 0)) -> totu 138 | } 139 | 140 | # parallel 141 | # main function 142 | loop <- function(i) { 143 | alpha_curves(totu, step = step, method = "richness") 144 | } 145 | { 146 | if (threads > 1) { 147 | pcutils::lib_ps("foreach", "doSNOW", "snow", library = FALSE) 148 | if (verbose) { 149 | pb <- utils::txtProgressBar(max = reps, style = 3) 150 | opts <- list(progress = function(n) utils::setTxtProgressBar(pb, n)) 151 | } else { 152 | opts <- NULL 153 | } 154 | cl <- snow::makeCluster(threads) 155 | doSNOW::registerDoSNOW(cl) 156 | res <- foreach::`%dopar%`( 157 | foreach::foreach(i = seq_len(reps), .options.snow = opts), 158 | loop(i) 159 | ) 160 | snow::stopCluster(cl) 161 | gc() 162 | } else { 163 | res <- lapply(seq_len(reps), loop) 164 | } 165 | } 166 | # simplify method 167 | plot_richness <- do.call(rbind, res) 168 | pcutils::del_ps("foreach", "doSNOW") 169 | 170 | result <- plot_richness %>% 171 | dplyr::group_by(., rare, sample) %>% 172 | dplyr::summarise(., mean(alpha), sd(alpha)) %>% 173 | dplyr::select(everything(), alpha = 3, sd = 4) 174 | result$sample <- result$sample %>% factor(., levels = colnames(otutab)) 175 | 176 | attributes(result)$type <- "species" 177 | attributes(result)$mode <- mode 178 | attributes(result)$method <- method 179 | class(result) <- c("rare_res", class(result)) 180 | return(result) 181 | } 182 | 183 | #' Calculate Abundance-occupancy_relationship 184 | #' 185 | #' @param ... add 186 | #' @param otutab object, such as data.frame or pc_otu 187 | #' 188 | #' @export 189 | #' @return AOR 190 | #' @references 191 | #' Barberán, A., Bates, S. T., Casamayor, E. & Fierer, N. (2012) Using network analysis to explore co-occurrence patterns in soil microbial communities. 192 | #' @examples 193 | #' data(otutab, package = "pcutils") 194 | #' aor(otutab) -> AOR 195 | #' plot(AOR) 196 | aor <- function(otutab, ...) { 197 | UseMethod("aor") 198 | } 199 | 200 | #' @param otutab otutab 201 | #' @param top_r percentage of top relative abundance 202 | #' @param ocup_n percentage of top occupied 203 | #' @param special_n how many occupancy define as specialists 204 | #' 205 | #' @method aor data.frame 206 | #' @rdname aor 207 | #' @exportS3Method 208 | aor.data.frame <- function(otutab, top_r = 0.7, 209 | ocup_n = ceiling(0.8 * ncol(otutab)), 210 | special_n = ceiling(0.1 * ncol(otutab)), ...) { 211 | abundance <- relative_abund <- cum <- occupancy <- NULL 212 | otutab %>% 213 | dplyr::transmute( 214 | abundance = rowSums(.), relative_abund = abundance / sum(abundance), 215 | occupancy = rowSums(. > 0) 216 | ) %>% 217 | dplyr::arrange(-abundance) -> AOR 218 | AOR %>% dplyr::mutate(log_abund = log10(relative_abund)) -> AOR 219 | 220 | y <- AOR$occupancy 221 | x <- AOR$log_abund 222 | tmp <- try({ 223 | fit <- stats::nls(y ~ stats::SSlogis(x, Asym, xmid, scal), data = data.frame(x, y)) 224 | # summary(fit) 225 | AOR$fit_y <- stats::predict(fit, newdata = data.frame(x = x)) 226 | }) 227 | if ("try-error" %in% class(tmp)) { 228 | warning("fit failed") 229 | AOR$fit_y <- 0 230 | } 231 | 232 | # find core otus 233 | AOR %>% dplyr::mutate(cum = cumsum(relative_abund), core = ifelse((cum < top_r) & (occupancy > ocup_n), "core", "others")) -> AOR 234 | # habitat generalists and specialists 235 | AOR %>% dplyr::mutate(habitat = ifelse((cum < top_r) & (occupancy > ocup_n), "generalists", 236 | ifelse((cum < top_r) & (occupancy < special_n), "specialists", "others") 237 | )) -> AOR 238 | 239 | class(AOR) <- c("AOR", "data.frame") 240 | return(AOR) 241 | } 242 | 243 | 244 | #' Plot a AOR 245 | #' 246 | #' @param x AOR object 247 | #' @param ... add 248 | #' 249 | #' @import scales 250 | #' @rdname aor 251 | #' @exportS3Method 252 | #' @method plot AOR 253 | #' @return ggplot 254 | plot.AOR <- function(x, ...) { 255 | AOR <- x 256 | log_abund <- occupancy <- fit_y <- core <- habitat <- NULL 257 | 258 | p1 <- ggplot() + 259 | geom_point(data = AOR, aes(x = 10^log_abund, y = occupancy), col = "#79A5C9", alpha = 0.6, size = 2) + 260 | geom_line(data = AOR, aes(x = 10^log_abund, y = fit_y), col = "red", size = 1.5) + 261 | scale_x_log10(labels = scales::label_log()) + 262 | theme_bw() + 263 | labs(x = "mean relative abundnace (%)", y = "occupancy") 264 | # scale_x_continuous() 265 | 266 | p2 <- ggplot() + 267 | geom_point(data = AOR, aes(x = 10^log_abund, y = occupancy, col = core), alpha = 0.6, size = 2) + 268 | scale_x_log10(labels = scales::label_log()) + 269 | scale_color_manual(values = c("others" = "grey", "core" = "#79A5C9")) + 270 | theme_bw() + 271 | labs(x = "mean relative abundnace (%)", y = "occupancy") 272 | 273 | p3 <- ggplot() + 274 | geom_point(data = AOR, aes(x = 10^log_abund, y = occupancy, col = habitat), alpha = 0.6, size = 2) + 275 | scale_x_log10(labels = scales::label_log()) + 276 | scale_color_manual(values = c("others" = "grey", "generalists" = "#79A5C9", "specialists" = "#EE79A5")) + 277 | theme_bw() + 278 | labs(x = "mean relative abundnace (%)", y = "occupancy") 279 | 280 | return(list(p1, p2, p3)) 281 | } 282 | -------------------------------------------------------------------------------- /R/a_diversity.R: -------------------------------------------------------------------------------- 1 | # a_diversity========== 2 | 3 | #' Calculate a_diversity of otutab 4 | #' 5 | #' @param ... add 6 | #' @param otutab otutab 7 | #' 8 | #' @export 9 | #' 10 | #' @examples 11 | #' data(otutab, package = "pcutils") 12 | #' a_diversity(otutab) -> a_res 13 | #' plot(a_res, "Group", metadata) 14 | a_diversity <- function(otutab, ...) { 15 | UseMethod("a_diversity") 16 | } 17 | 18 | #' @param otutab an otutab data.frame, samples are columns, taxs are rows. 19 | #' @param method one of "all","richness","chao1","ace","gc","shannon","simpson","pd","pielou","abundance" 20 | #' @param tree a iphylo object match the rownames of otutab 21 | #' @param digits maintance how many digits 22 | #' @rdname a_diversity 23 | #' @return a a_res object 24 | #' @exportS3Method 25 | #' @method a_diversity data.frame 26 | a_diversity.data.frame <- function(otutab, method = c("richness", "shannon"), tree = NULL, digits = 4, ...) { 27 | all <- c("all", "richness", "chao1", "ace", "gc", "shannon", "simpson", "pd", "pielou", "abundance") 28 | if (!all(method %in% all)) stop(paste0("methods should be some of ", paste0(all, collapse = ","))) 29 | if ("all" %in% method) method <- all[-1] 30 | x <- t(otutab) 31 | a_res <- data.frame(row.names = colnames(otutab)) 32 | if ("richness" %in% method) { 33 | Richness <- rowSums(x > 0) 34 | a_res <- cbind(a_res, Richness) 35 | } 36 | if ("abundance" %in% method) { 37 | Abundance <- rowSums(x) 38 | a_res <- cbind(a_res, Abundance) 39 | } 40 | if ("chao1" %in% method) { 41 | Chao1 <- vegan::estimateR(x)[2, ] 42 | a_res <- cbind(a_res, Chao1) 43 | } 44 | if ("ace" %in% method) { 45 | ACE <- vegan::estimateR(x)[4, ] 46 | a_res <- cbind(a_res, ACE) 47 | } 48 | if ("gc" %in% method) { 49 | Goods_Coverage <- 1 - rowSums(x <= 1) / rowSums(x) 50 | a_res <- cbind(a_res, Goods_Coverage) 51 | } 52 | if ("shannon" %in% method) { 53 | Shannon <- vegan::diversity(x, index = "shannon", ...) 54 | a_res <- cbind(a_res, Shannon) 55 | } 56 | # 注意,这里是Gini-Simpson 指数 57 | if ("simpson" %in% method) { 58 | Simpson <- vegan::diversity(x, index = "simpson", ...) 59 | a_res <- cbind(a_res, Simpson) 60 | } 61 | if ("pielou" %in% method) { 62 | Pielou_evenness <- vegan::diversity(x, index = "shannon") / log(rowSums(x > 0)) 63 | a_res <- cbind(a_res, Pielou_evenness) 64 | } 65 | if ("pd" %in% method) { 66 | if (is.null(tree)) { 67 | warning("pd need tree!") 68 | } else { 69 | lib_ps("picante", library = FALSE) 70 | picante::match.phylo.comm(tree, x) -> match_p 71 | pds <- picante::pd(match_p$comm, match_p$phy, include.root = FALSE) 72 | PD <- pds[, 1] 73 | a_res <- cbind(a_res, PD) 74 | # 净相关指数 75 | # NRI=-ses.mpd(x,cophenetic(spe_nwk),null.model="taxa.labels")[6] 76 | # names(NRI) <- 'NRI' 77 | # 最近邻体指数 78 | # NTI=-ses.mntd(x,cophenetic(spe_nwk),null.model="taxa.labels")[6] 79 | # names(NTI) <- 'NTI' 80 | # result <- cbind(result, PD_whole_tree,NRI,NTI) 81 | } 82 | } 83 | a_res <- round(a_res, digits) 84 | class(a_res) <- c("a_res", "data.frame") 85 | return(a_res) 86 | } 87 | 88 | #' @param otutab a pc_otu 89 | #' 90 | #' @param method one of "all","richness","chao1","ace","gc","shannon","simpson","pd","pielou" 91 | #' @param tbl which table 92 | #' 93 | #' @exportS3Method 94 | #' 95 | #' @rdname a_diversity 96 | #' @method a_diversity pc_otu 97 | a_diversity.pc_otu <- function(otutab, method = "all", tbl = "otutab", ...) { 98 | pc <- otutab 99 | pc_valid(pc) 100 | otutab <- pc$tbls[[tbl]] 101 | pc$metas$a_res <- a_diversity.data.frame(otutab, method = method) 102 | return(pc) 103 | } 104 | 105 | #' @param otutab numeric 106 | #' @param ... pass to `a_diversity.data.frame` 107 | #' 108 | #' @exportS3Method 109 | #' 110 | #' @rdname a_diversity 111 | #' @method a_diversity numeric 112 | a_diversity.numeric <- function(otutab, ...) { 113 | x <- otutab 114 | return(a_diversity.data.frame(data.frame(Sample = x), ...)) 115 | } 116 | 117 | #' Plot a_res object 118 | #' 119 | #' @param x a a_res object 120 | #' @param metadata metadata 121 | #' @param group one of colname of metadata 122 | #' @param ... addditional parameters for \code{\link[pcutils]{group_box}} or \code{\link[pcutils]{my_lm}} 123 | #' 124 | #' @return patchwork object,you can change theme with & 125 | #' @exportS3Method 126 | #' @method plot a_res 127 | #' 128 | #' @seealso \code{\link{a_diversity}} 129 | #' 130 | plot.a_res <- function(x, group, metadata, ...) { 131 | a_res <- x 132 | a_res <- a_res[rownames(metadata), , drop = FALSE] 133 | group1 <- metadata[, group] 134 | if (is.numeric(group1) & !is.factor(group1)) { 135 | p <- pcutils::my_lm(a_res, group, metadata, ...) 136 | } else { 137 | p <- pcutils::group_box(a_res, group, metadata, ...) 138 | } 139 | return(p) 140 | } 141 | 142 | 143 | # test phylogenetic diversity 144 | # if(FALSE){ 145 | # lib_ps("picante") 146 | # data("phylocom") 147 | # View(phylocom$sample) 148 | # ggtree(phylocom$phylo)+geom_tiplab()+theme_tree2() 149 | # samp=data.frame(a=1:3,b=2:4,c=c(1,2,0),d=c(0,0,3),e=0,row.names = paste0("plot",1:3)) 150 | # read.tree(text = "(c:2,(a:1,b:1):2,d:1,f:1):1;",)->test 151 | # ggtree(test)+geom_tiplab()+theme_tree2() 152 | # #prune.sample(samp,test)->test_prune 153 | # match.phylo.comm(test,samp)->match_p 154 | # match_p$phy->test;match_p$comm->samp 155 | # 156 | # test_prune%>%ggtree(.)+geom_tiplab()+theme_tree2() 157 | # pd(samp,test,include.root = FALSE) 158 | # pd(samp,test_prune,include.root = FALSE) 159 | # 160 | # #picante::ses.pd(samp,test) 161 | # #tree noedes distance 162 | # cophenetic(test) 163 | # #每个样方有的物种对的平均谱系距离mpd 164 | # mpd(samp,cophenetic(test)) 165 | # #随机化mpd 166 | # mpd(samp,taxaShuffle(cophenetic(test))) 167 | # #ses.mpd直接计算了 168 | # ses.mpd(samp,cophenetic(test))->mpd_ses 169 | # #净相关指数nri,>0聚集,<0发散 170 | # mpd_ses%>%mutate(nri=-1*(mpd.obs-mpd.rand.mean)/mpd.rand.sd)%>%select(nri) 171 | # #nti类似nri 172 | # #mnpd最近谱系距离均值 173 | # mntd(samp,cophenetic(test)) 174 | # ses.mntd(samp,cophenetic(test))->mnpd_ses 175 | # mnpd_ses%>%mutate(nti=-1*(mntd.obs-mntd.rand.mean)/mntd.rand.sd)%>%select(nti) 176 | # 177 | # #beta-mpd 178 | # comdist(samp,cophenetic(test)) 179 | # #beta-mntd 180 | # comdistnt(samp,cophenetic(test)) 181 | # #pcd 182 | # picante::pcd(samp,test) 183 | # #phylosor 184 | # picante::phylosor(samp,test) 185 | # picante::psd(samp,test) 186 | # picante::raoD(samp,test) 187 | # picante::unifrac(samp,test) 188 | # 189 | # } 190 | 191 | 192 | # z_diversity========== 193 | # https://cloud.tencent.com/developer/article/1672945 194 | 195 | #' Calculate Zeta Diversity with Distance 196 | #' 197 | #' This function calculates Zeta diversity for each group in the provided otutab. 198 | #' 199 | #' @param otutab A matrix or data frame containing OTU (Operational Taxonomic Unit) counts. 200 | #' @param group_df A data frame containing group information. 201 | #' @param zetadiv_params Additional parameters to be passed to the Zeta.ddecay function from the zetadiv package. 202 | #' @param xy_df Site coordinates. 203 | #' 204 | #' @return zeta_decay 205 | #' @export 206 | #' 207 | #' @examples 208 | #' if (requireNamespace("zetadiv")) { 209 | #' data(otutab, package = "pcutils") 210 | #' zeta_decay_result <- z_diversity_decay(otutab, metadata[, c("lat", "long")], 211 | #' metadata["Group"], 212 | #' zetadiv_params = list(sam = 10) 213 | #' ) 214 | #' plot(zeta_decay_result) 215 | #' } 216 | z_diversity_decay <- function(otutab, xy_df, group_df = NULL, zetadiv_params = list()) { 217 | lib_ps("zetadiv", library = FALSE) 218 | 219 | if (is.null(group_df)) { 220 | group_df <- data.frame(row.names = colnames(otutab), Group = rep("all", ncol(otutab)), check.names = FALSE) 221 | } 222 | 223 | zeta_decay <- list() 224 | 225 | for (i in unique(group_df[, 1, drop = TRUE])) { 226 | tmp_df <- pcutils::trans(pcutils::t2(otutab[, rownames(group_df)[group_df[, 1, drop = TRUE] == i]]), "pa") 227 | tmp_xy_df <- xy_df[rownames(group_df)[group_df[, 1, drop = TRUE] == i], ] 228 | tmp_zeta <- do.call( 229 | zetadiv::Zeta.ddecay, 230 | update_param(list( 231 | xy = tmp_xy_df, data.spec = tmp_df, sam = 100, order = 3, 232 | method.glm = "glm.fit2", confint.level = 0.95, 233 | normalize = "Jaccard", plot = FALSE 234 | ), zetadiv_params) 235 | ) 236 | zeta_decay[[i]] <- tmp_zeta 237 | } 238 | 239 | class(zeta_decay) <- "zeta_decay" 240 | return(zeta_decay) 241 | } 242 | 243 | #' Plot Zeta Diversity with Distance Results 244 | #' 245 | #' @param x Zeta diversity results obtained from z_diversity_decay function. 246 | #' @param ribbon Logical, whether to add a ribbon to the plot for standard deviation. 247 | #' @param ... Additional arguments to be passed to ggplot2 functions. 248 | #' 249 | #' @return A ggplot object. 250 | #' @exportS3Method 251 | #' @method plot zeta_decay 252 | #' 253 | #' @rdname z_diversity_decay 254 | plot.zeta_decay <- function(x, ribbon = TRUE, ...) { 255 | zeta_decay <- x 256 | plot_df <- data.frame() 257 | distance <- zeta_val <- Group <- fit <- NULL 258 | 259 | for (i in names(zeta_decay)) { 260 | zeta.bird2 <- zeta_decay[[i]] 261 | # Predictions 262 | preds <- stats::predict(zeta.bird2$reg, 263 | newdata = data.frame(distance.reg = sort(zeta.bird2$distance)), 264 | type = "link", se.fit = TRUE 265 | ) 266 | critval <- 1.96 267 | 268 | # Create a data frame for ggplot 269 | plot_data <- data.frame( 270 | Group = i, 271 | distance = sort(zeta.bird2$distance), 272 | zeta_val = zeta.bird2$zeta.val, 273 | fit = preds$fit, 274 | sd = (critval * preds$se.fit) 275 | ) 276 | 277 | plot_df <- rbind(plot_df, plot_data) 278 | } 279 | 280 | # Plot with ggplot2 281 | p <- ggplot(plot_df, aes(x = distance, y = zeta_val, color = Group)) + 282 | geom_point(shape = 16) + 283 | geom_line(aes(y = fit)) + 284 | labs(x = "Distance", y = paste0("Zeta diversity (Order ", zeta_decay[[1]]$order, ")")) 285 | if (ribbon) { 286 | p <- p + 287 | geom_ribbon(aes(ymin = fit - sd, ymax = fit + sd, group = Group), 288 | color = NA, fill = "grey", alpha = 0.5 289 | ) 290 | } 291 | 292 | return(p) 293 | } 294 | 295 | #' Calculate Zeta Diversity 296 | #' 297 | #' This function calculates Zeta diversity for each group in the provided otutab. 298 | #' 299 | #' @param otutab A matrix or data frame containing OTU (Operational Taxonomic Unit) counts. 300 | #' @param group_df A data frame containing group information. 301 | #' @param zetadiv_params Additional parameters to be passed to the Zeta.decline.mc function from the zetadiv package. 302 | #' 303 | #' @return zeta_res 304 | #' @export 305 | #' 306 | #' @examples 307 | #' \donttest{ 308 | #' if (requireNamespace("zetadiv")) { 309 | #' data(otutab, package = "pcutils") 310 | #' zeta_result <- z_diversity(otutab, metadata["Group"], zetadiv_params = list(sam = 10)) 311 | #' plot(zeta_result, lm_model = "exp", text = TRUE) 312 | #' } 313 | #' } 314 | z_diversity <- function(otutab, group_df = NULL, zetadiv_params = list()) { 315 | lib_ps("zetadiv", library = FALSE) 316 | 317 | if (is.null(group_df)) { 318 | group_df <- data.frame(row.names = colnames(otutab), Group = rep("all", ncol(otutab)), check.names = FALSE) 319 | } 320 | 321 | zeta_res <- list() 322 | 323 | for (i in unique(group_df[, 1, drop = TRUE])) { 324 | tmp_df <- pcutils::trans(pcutils::t2(otutab[, rownames(group_df)[group_df[, 1, drop = TRUE] == i]]), "pa") 325 | tmp_zeta <- do.call( 326 | zetadiv::Zeta.decline.mc, 327 | update_param(list( 328 | data.spec = tmp_df, orders = 1:5, 329 | sam = 100, normalize = "Jaccard", plot = FALSE, silent = TRUE 330 | ), zetadiv_params) 331 | ) 332 | zeta_res[[i]] <- tmp_zeta 333 | } 334 | 335 | class(zeta_res) <- "zeta_res" 336 | return(zeta_res) 337 | } 338 | 339 | #' Plot Zeta Diversity Results 340 | #' 341 | #' This function plots the Zeta diversity results obtained from the z_diversity function. 342 | #' 343 | #' @param x Zeta diversity results obtained from z_diversity function. 344 | #' @param lm_model The linear model to be used for fitting ('exp' or 'pl'). 345 | #' @param ribbon Logical, whether to add a ribbon to the plot for standard deviation. 346 | #' @param text Logical, whether to add R-squared and p-value text annotations. 347 | #' @param ... Additional arguments to be passed to ggplot2 functions. 348 | #' 349 | #' @return A ggplot object. 350 | #' @exportS3Method 351 | #' @method plot zeta_res 352 | #' 353 | #' @rdname z_diversity 354 | plot.zeta_res <- function(x, lm_model = c("exp", "pl")[1], ribbon = FALSE, text = TRUE, ...) { 355 | zeta_res <- x 356 | 357 | plot_df <- data.frame() 358 | p_df <- data.frame() 359 | `Zeta order` <- `Zeta diversity` <- Group <- V1 <- V2 <- r2 <- NULL 360 | for (i in names(zeta_res)) { 361 | zeta.bird2 <- zeta_res[[i]] 362 | plot_df <- rbind(plot_df, data.frame( 363 | "Group" = i, "Zeta order" = zeta.bird2$zeta.order, 364 | "Zeta diversity" = zeta.bird2$zeta.val, 365 | "sd" = zeta.bird2$zeta.val.sd, 366 | check.names = FALSE 367 | )) 368 | 369 | if (lm_model == "exp") { 370 | tmp_lm <- (zeta.bird2$zeta.exp) 371 | } else { 372 | tmp_lm <- (zeta.bird2$zeta.pl) 373 | } 374 | 375 | p_df <- rbind(p_df, data.frame( 376 | "Group" = i, 377 | r2 = round(summary(tmp_lm)$r.squared, 4), 378 | p = round(anova(tmp_lm)$`Pr(>F)`[1], 4) 379 | )) 380 | } 381 | 382 | p <- ggplot(plot_df, aes(x = `Zeta order`, y = `Zeta diversity`, col = Group)) + 383 | geom_point() + 384 | geom_line() 385 | 386 | if (ribbon) { 387 | p <- p + geom_ribbon(aes(ymin = `Zeta diversity` - sd, ymax = `Zeta diversity` + sd, group = Group), 388 | color = NA, fill = "grey", alpha = 0.5 389 | ) 390 | } 391 | 392 | lims <- pcutils::ggplot_lim(p) 393 | p_coor <- pcutils::generate_labels(names(zeta_res), input = c(0.8 * lims$x[2], lims$y[2]), ncols = 1, y_offset = diff(lims$y) * 0.1) %>% as.data.frame() 394 | p_df <- cbind(p_df, p_coor) 395 | 396 | if (text) { 397 | p <- p + 398 | geom_text(data = p_df, aes(x = V1, y = V2, label = paste0("R2= ", r2, "; p= ", p)), show.legend = FALSE) 399 | } 400 | 401 | return(p) 402 | } 403 | -------------------------------------------------------------------------------- /R/additional.R: -------------------------------------------------------------------------------- 1 | ## some suggested pkgs: start 2 | # AnnotationDbi, 3 | # org.Hs.eg.db, 4 | # linkET, 5 | # pairwiseAdonis, 6 | # sankeyD3, 7 | # ggchicklet, 8 | # Biobase, 9 | # GEOquery 10 | ## some suggested pkgs: end 11 | 12 | #' Permanova between a otutab and a variable (added `two`) 13 | #' 14 | #' @param two two by two adonis test 15 | #' @inheritParams permanova 16 | permanova2 <- function(otutab, envs, norm = TRUE, each = TRUE, method = "adonis", 17 | dist = "bray", two = FALSE, nperm = 999, ...) { 18 | all <- c("adonis", "anosim", "mrpp", "mantel") 19 | if (!method %in% all) stop(paste0("method should be one of ", paste0(all, collapse = ", "))) 20 | stopifnot(is.data.frame(envs)) 21 | 22 | match_res <- match_df(otutab, envs) 23 | otutab <- match_res$otutab 24 | env <- match_res$metadata 25 | 26 | data.frame(t(otutab)) -> dat 27 | if (norm) otu.t <- vegan::decostand(dat, "hellinger") else otu.t <- dat 28 | if (each) { 29 | # adnois不只是检验分组变量,连续的数值变量也可以检验。 30 | soil <- NULL 31 | if (method == "adonis") { 32 | for (i in 1:ncol(env)) { 33 | dat.div <- vegan::adonis2(otu.t ~ (env[, i]), permutations = nperm, method = dist) 34 | soil <- rbind(soil, c(colnames(env)[i], dat.div$R2[1], dat.div$`Pr(>F)`[1])) 35 | if (two) { 36 | if ((is.factor(env[, i]) | inherits(env[, i], "Date") | is.character(env[, i]))) { 37 | env[, i] %>% as.factor() -> group 38 | lib_ps("pairwiseAdonis", library = FALSE) 39 | dat.pairwise.adonis <- pairwiseAdonis::pairwise.adonis( 40 | x = otu.t, factors = group, sim.function = "vegdist", 41 | sim.method = dist, p.adjust.m = "BH", 42 | reduce = NULL, perm = nperm 43 | ) 44 | pcutils::sanxian(dat.pairwise.adonis[, c("pairs", "R2", "p.value", "p.adjusted")], 45 | rows = NULL, nrow = Inf 46 | ) %>% print() 47 | } 48 | } 49 | } 50 | } 51 | if (method == "mantel") { 52 | # only numeric variables can do with mantel 53 | env %>% dplyr::select_if(\(x)is.numeric(x) & !is.factor(x)) -> env 54 | species.distance <- vegan::vegdist(otu.t, method = dist) 55 | for (i in 1:ncol(env)) { 56 | dd <- vegan::mantel(species.distance, vegan::vegdist(env[, i, drop = TRUE], method = dist), 57 | method = "pearson", permutations = nperm, na.rm = TRUE 58 | ) 59 | soil <- rbind(soil, c(colnames(env)[i], dd$statistic, dd$signif)) 60 | } 61 | } 62 | # only group variables can do with mrpp/anosim 63 | if (method == "anosim") { 64 | env %>% dplyr::select_if(\(x)class(x) == "Date" | is.factor(x) | is.character(x)) -> env 65 | env %>% dplyr::mutate_all(\(x)as.factor(x)) -> env 66 | for (i in 1:ncol(env)) { 67 | env[, i, drop = TRUE] -> group 68 | anosim_res <- vegan::anosim(otu.t, group, permutations = nperm, distance = dist) 69 | soil <- rbind(soil, c(colnames(env)[i], anosim_res$statistic, anosim_res$signif)) 70 | } 71 | } 72 | if (method == "mrpp") { 73 | env %>% dplyr::select_if(\(x)class(x) == "Date" | is.factor(x) | is.character(x)) -> env 74 | env %>% dplyr::mutate_all(\(x)as.factor(x)) -> env 75 | for (i in 1:ncol(env)) { 76 | env[, i, drop = TRUE] -> group 77 | mrpp_res <- vegan::mrpp(otu.t, group, permutations = nperm, distance = dist) 78 | soil <- rbind(soil, c(colnames(env)[i], mrpp_res$A, mrpp_res$Pvalue)) 79 | } 80 | } 81 | soil <- data.frame(group = soil[, 1], r2 = soil[, 2], p_value = soil[, 3]) 82 | } else { 83 | message("Use method='adonis' to test all factors") 84 | message("Permanova test for all factors, notes that the order of factors would affect the result.") 85 | dat.div <- vegan::adonis2(otu.t ~ ., data = env, permutations = nperm, method = dist) 86 | dat.div <- dat.div[seq_len(nrow(dat.div) - 2), , drop = FALSE] %>% as.data.frame() 87 | soil <- data.frame(group = rownames(dat.div), r2 = dat.div$R2, p_value = dat.div$`Pr(>F)`) 88 | } 89 | soil$r2 <- round(as.numeric(soil$r2), 4) 90 | soil$p_value <- round(as.numeric(soil$p_value), 4) 91 | soil$sig <- soil$p_value < 0.05 92 | if (method %in% c("anosim", "mrpp", "mantel")) colnames(soil)[2] <- "r" 93 | attributes(soil)$method <- method 94 | class(soil) <- c("g_test", "data.frame") 95 | return(soil) 96 | } 97 | 98 | #' Multi-table test with env 99 | #' 100 | #' @param g_otutab multi-otutabs with first column is group 101 | #' @param env environmental factors 102 | #' 103 | #' @return a mant_g object 104 | #' @export 105 | #' 106 | #' @examples 107 | #' if (requireNamespace("linkET")) { 108 | #' data(otutab, package = "pcutils") 109 | #' cbind(group = rep(c("a", "b", "c"), c(200, 100, 185)), otutab) -> g_otutab 110 | #' metadata[, 3:8, drop = FALSE] -> env 111 | #' m_group_env(g_otutab, env) -> mant_g 112 | #' plot(mant_g) 113 | #' } 114 | m_group_env <- function(g_otutab, env) { 115 | group <- r <- p_value <- NULL 116 | groups <- g_otutab$group %>% unique() 117 | all <- data.frame() 118 | for (i in groups) { 119 | filter(g_otutab, group == i)[, -1] -> tmp 120 | suppressWarnings(permanova(tmp, env, method = "mantel") -> res) 121 | all <- rbind(all, data.frame(spec = i, res)) 122 | } 123 | all %>% dplyr::mutate( 124 | rd = cut(r, 125 | breaks = c(-Inf, 0.2, 0.4, Inf), 126 | labels = c("< 0.2", "0.2 - 0.4", ">= 0.4") 127 | ), 128 | pd = cut(p_value, 129 | breaks = c(-Inf, 0.01, 0.05, Inf), 130 | labels = c("< 0.01", "0.01 - 0.05", ">= 0.05") 131 | ) 132 | ) -> all 133 | all <- list(mantel_test = all, env = env) 134 | class(all) <- c("mant_g", class(all)) 135 | return(all) 136 | } 137 | 138 | #' Plot mant_g object 139 | #' 140 | #' @param x mant_g object 141 | #' @param ... add 142 | #' 143 | #' @return a ggplot 144 | #' @exportS3Method 145 | #' @method plot mant_g 146 | #' 147 | #' @rdname m_group_env 148 | plot.mant_g <- function(x, ...) { 149 | env <- x[["env"]] 150 | mantel_test <- x[["mantel_test"]] 151 | pd <- rd <- NULL 152 | # if (FALSE) { 153 | # lib_ps("ggcor", library = FALSE) 154 | # if (isNamespaceLoaded("linkET")) lapply(c("ggcor", "linkET"), unloadNamespace) 155 | # ggcor::set_scale(rev(pcutils::get_cols(pal = "bluered")), 156 | # type = "gradient2n" 157 | # ) 158 | # corp <- ggcor::quickcor(env, type = "lower") + 159 | # # geom_square() + 160 | # ggcor::geom_square(data = ggcor::get_data(show.diag = FALSE)) + 161 | # ggcor::anno_link(aes(colour = pd, size = rd), data = mantel_test, curvature = -0.25) + 162 | # # ggcor::geom_diag_label(size=3)+ 163 | # scale_size_manual(values = c(0.5, 1, 2)) + 164 | # scale_colour_manual(values = c("#F58058", "#F7E874", "#D6D6D6")) + 165 | # guides( 166 | # size = guide_legend( 167 | # title = "Mantel's r", 168 | # override.aes = list(colour = "grey35"), 169 | # order = 2 170 | # ), 171 | # colour = guide_legend( 172 | # title = "Mantel's p", 173 | # override.aes = list(size = 3), 174 | # order = 1 175 | # ), 176 | # fill = guide_colorbar(title = "Pearson's r", order = 3) 177 | # ) 178 | # } 179 | { 180 | lib_ps("linkET", library = FALSE) 181 | corp <- linkET::qcorrplot(linkET::correlate(env), type = "lower", diag = FALSE) + 182 | linkET::geom_square() + 183 | linkET::geom_couple(aes(colour = pd, size = rd), 184 | data = mantel_test, 185 | curvature = linkET::nice_curvature() 186 | ) + 187 | scale_fill_gradientn(colours = (pcutils::get_cols(pal = "bluered"))) + 188 | scale_size_manual(values = c(0.5, 1, 2)) + 189 | scale_colour_manual(values = c("#D95F02", "#1B9E77", "#CCCCCC99")) + 190 | guides( 191 | size = guide_legend( 192 | title = "Mantel's r", 193 | override.aes = list(colour = "grey35"), 194 | order = 2 195 | ), 196 | colour = guide_legend( 197 | title = "Mantel's p", 198 | override.aes = list(size = 3), 199 | order = 1 200 | ), 201 | fill = guide_colorbar(title = "Pearson's r", order = 3) 202 | ) 203 | } 204 | return(corp) 205 | } 206 | 207 | 208 | #' Plot a sankey 209 | #' 210 | #' @param tree result from \code{\link{ann_tree}} 211 | #' @param top_N each level has top_N 212 | #' @param notshow some words you don't want to show 213 | #' @param intermediate logical, show the intermediate rank 214 | #' @param width width 215 | #' @param height height 216 | #' @param ... look for parameters in \code{\link[sankeyD3]{sankeyNetwork}} 217 | #' 218 | #' @export 219 | #' @return html widget 220 | #' @examples 221 | #' \donttest{ 222 | #' if (requireNamespace("sankeyD3") && requireNamespace("tidytree")) { 223 | #' data(otutab, package = "pcutils") 224 | #' ann_tree(taxonomy[, c(1, 5, 6, 7)], otutab) -> tree 225 | #' sangji_plot(tree) 226 | #' } 227 | #' } 228 | sangji_plot <- function(tree, top_N = 5, notshow = c(), intermediate = FALSE, width = 3000, height = 500, ...) { 229 | node <- label <- parent_label <- level <- abundance <- NULL 230 | lib_ps("sankeyD3", library = FALSE) 231 | lib_ps("tidytree", library = FALSE) 232 | 233 | # 桑基图 234 | # select show part 235 | if (length(notshow) > 0) { 236 | tree1 <- tree[!grepl(paste0(notshow, collapse = "|"), tree$label), ] 237 | } else { 238 | tree1 <- tree 239 | } 240 | 241 | tree1 %>% 242 | dplyr::group_by(level) %>% 243 | dplyr::top_n(top_N, abundance) %>% 244 | dplyr::ungroup() -> sangji_dat 245 | 246 | if (intermediate) { 247 | all_node <- sapply(sangji_dat$node, \(i)tidytree::ancestor(tree, i) %>% dplyr::pull(node)) %>% unlist() 248 | all_node <- c(sangji_dat$node, all_node) %>% unique() 249 | tree %>% dplyr::filter(node %in% all_node) -> sangji_dat 250 | } 251 | 252 | # show levels 253 | sangji_dat %>% 254 | dplyr::filter(!label %in% c("", "r__root")) %>% 255 | dplyr::select(label, level, abundance) %>% 256 | as.data.frame() -> nodes 257 | 258 | # tree structure 259 | sangji_dat %>% 260 | dplyr::filter(parent_label != "r__root") %>% 261 | dplyr::select(label, parent_label, abundance) -> links 262 | links <- as.data.frame(links) 263 | 264 | # find the nearest parent 265 | others <- links$parent_label[!links$parent_label %in% nodes$label] 266 | others <- (others[!duplicated(others)]) 267 | tree %>% dplyr::filter(label %in% others) -> o_nodes 268 | others <- o_nodes$label 269 | o_nodes <- o_nodes$node 270 | for (i in seq_along(o_nodes)) { 271 | tidytree::ancestor(tree, o_nodes[i]) %>% dplyr::pull(label) -> tmp 272 | links[links$parent_label == others[i], "parent_label"] <- rev(tmp)[rev(tmp) %in% nodes$label][1] 273 | } 274 | 275 | # le <- c("Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species") 276 | # le[le %in% nodes$level] -> mytax 277 | 278 | colnames(tree1)[(which(colnames(tree1) == "abundance") + 1):ncol(tree1)] -> mytax 279 | 280 | taxRank_to_depth <- setNames(seq_along(mytax) - 1, mytax) 281 | nodes$depth <- taxRank_to_depth[nodes$level %>% as.character()] 282 | 283 | 284 | links$IDsource <- match(links$parent_label, nodes$label) - 1 285 | links$IDtarget <- match(links$label, nodes$label) - 1 286 | # na.omit(links)->links 287 | 288 | do.call(sankeyD3::sankeyNetwork, pcutils::update_param(list( 289 | Links = links, Nodes = nodes, 290 | Source = "IDsource", Target = "IDtarget", Value = "abundance", LinkGroup = "parent_label", 291 | NodeID = "label", NodeGroup = "label", NodePosX = "depth", NodeValue = "abundance", 292 | iterations = 1000, xAxisDomain = mytax, align = "none", 293 | fontFamily = "arial", fontSize = 12, linkGradient = TRUE, 294 | nodeWidth = 15, nodeCornerRadius = 5, highlightChildLinks = TRUE, 295 | orderByPath = TRUE, scaleNodeBreadthsByString = TRUE, 296 | numberFormat = "pavian", dragY = TRUE, nodeShadow = TRUE, 297 | doubleclickTogglesChildren = TRUE, width = width, height = height 298 | ), list(...))) 299 | } 300 | 301 | #' Plot a sunburst 302 | #' 303 | #' @param tree result from \code{\link{ann_tree}} 304 | #' 305 | #' @return sunburst 306 | #' @export 307 | #' @seealso [sangji_plot()] 308 | #' @examples 309 | #' \donttest{ 310 | #' if (requireNamespace("plotly")) { 311 | #' data(otutab, package = "pcutils") 312 | #' ann_tree(taxonomy[, c(1, 5, 6, 7)], otutab) -> tree 313 | #' sunburst(tree) 314 | #' } 315 | #' } 316 | sunburst <- function(tree) { 317 | lib_ps("plotly", library = FALSE) 318 | links <- data.frame("source" = tree$parent_label, "target" = tree$label, "weight" = tree$abundance) 319 | 320 | fig <- plotly::plot_ly( 321 | labels = links$target, parents = links$source, 322 | values = links$weight, type = "sunburst" 323 | ) 324 | fig 325 | } 326 | 327 | 328 | # 329 | #' Gene symbolid transfer to entrezIDs (human gene) 330 | #' 331 | #' @param genes gene symbols e.g:ASGR2 332 | #' 333 | #' @return gene entrezIDs dataframe 334 | #' @export 335 | #' 336 | #' @examples 337 | #' if (requireNamespace("AnnotationDbi") && requireNamespace("org.Hs.eg.db")) { 338 | #' genes <- c( 339 | #' "ASGR2", "BEST1", "SIGLEC16", "ECRP", "C1QC", "TCN2", "RNASE2", 340 | #' "DYSF", "C1QB", "FAM20A", "FCGR1A", "CR1", "HP", "VSIG4", "EGR1" 341 | #' ) 342 | #' gene2id(genes) -> geneid 343 | #' } 344 | gene2id <- function(genes) { 345 | lib_ps("AnnotationDbi", "org.Hs.eg.db", library = FALSE) 346 | # entrezIDs=AnnotationDbi::mget(genes,org.Hs.egSYMBOL2EG, ifnotfound=NA) # 找出基因对应的ID 347 | suppressMessages({ 348 | entrezIDs <- AnnotationDbi::mapIds(org.Hs.eg.db::org.Hs.eg.db, keys = genes, keytype = "SYMBOL", column = "ENTREZID") 349 | }) 350 | entrezIDs <- as.character(entrezIDs) # 获取数据 351 | rt <- data.frame(genes, entrezID = entrezIDs) # 添加一列entrezID 352 | # rt=rt[(rt[,"entrezID"])!="NA",] # 删除没有基因的ID 353 | rt <- rt[!is.na(rt[, "entrezID"]), ] # 删除没有基因的ID 354 | return(rt) 355 | } 356 | 357 | #' prepare the GEO data 358 | #' 359 | #' @param my_id GEOid 360 | #' @param GEO_dir GEO download dir 361 | #' @param file the downloaded file 362 | #' 363 | #' @export 364 | #' @return list(meta = meta, GSE_expr = GSE_expr) 365 | pre_GEO <- function(my_id, GEO_dir = "GEO_data", file = NULL) { 366 | prode_id <- ID <- `Gene Symbol` <- GENE_SYMBOL <- Symbol <- gene_assignment <- GENE <- GeneSymbol <- n_gene <- NULL 367 | # 1.Importing the data 368 | ## change my_id to be the dataset that you want. 369 | lib_ps("GEOquery", "Biobase", library = FALSE) 370 | 371 | if (is.null(file)) { 372 | if (file.exists(paste0(GEO_dir, "/", my_id, "_series_matrix.txt.gz"))) { 373 | file <- paste0(GEO_dir, "/", my_id, "_series_matrix.txt.gz") 374 | } 375 | } 376 | 377 | if (is.null(file)) { 378 | gse <- GEOquery::getGEO(my_id[1], destdir = GEO_dir) 379 | gse <- gse[[1]] 380 | } else { 381 | gse <- GEOquery::getGEO(filename = file, getGPL = FALSE) 382 | } 383 | 384 | GPL_version <- gse@annotation 385 | if (file.exists(paste0(GEO_dir, "/", GPL_version, ".soft.gz"))) { 386 | GPL_data_111 <- GEOquery::getGEO(filename = paste0(GEO_dir, "/", GPL_version, ".soft.gz")) 387 | GPL_data_11 <- GPL_data_111@dataTable@table 388 | } else { 389 | gse <- GEOquery::getGEO(filename = file, getGPL = TRUE) 390 | } 391 | 392 | meta <- Biobase::pData(gse) ## print the sample information 393 | GPL_data_11 <- Biobase::fData(gse) ## print the gene annotation 394 | GSE_data_expr <- Biobase::exprs(gse) ## print the expression data 395 | 396 | # 进行注释 397 | GSE_data_expr <- GSE_data_expr %>% 398 | as.data.frame() %>% 399 | tibble::rownames_to_column() %>% 400 | dplyr::rename(prode_id = "rowname") %>% 401 | mutate(prode_id = as.character(prode_id)) 402 | 403 | if (GPL_version %in% c("GPL570", "GPL571", "GPL96", "GSE22873", "GPL1261", "GPL81", "GPL8300")) { 404 | GPL <- GPL_data_11 %>% 405 | dplyr::select(ID, `Gene Symbol`) %>% 406 | dplyr::rename(prode_id = "ID", GeneSymbol = "Gene Symbol") 407 | GPL$GeneSymbol <- gsub(" ///.*", "", GPL$GeneSymbol) # 一对多取第一个? 408 | } else if (GPL_version %in% c("GPL6254", "GPL10787")) { 409 | GPL <- GPL_data_11 %>% 410 | dplyr::select(ID, `GENE_SYMBOL`) %>% 411 | dplyr::rename(prode_id = "ID", GeneSymbol = "GENE_SYMBOL") 412 | GPL$GeneSymbol <- gsub(" ///.*", "", GPL$GeneSymbol) # 一对多取第一个? 413 | } else if (GPL_version %in% c("GPL6947", "GPL6883", "GPL4866", "GPL4006")) { 414 | GPL <- GPL_data_11 %>% 415 | dplyr::select(ID, `Symbol`) %>% 416 | dplyr::rename(prode_id = "ID", GeneSymbol = "Symbol") 417 | GPL$GeneSymbol <- gsub("/.*", "", GPL$GeneSymbol) # 一对多取第一个? 418 | } else if (GPL_version %in% c("GPL6244", "GPL17586", "GPL6246")) { 419 | GPL <- GPL_data_11 %>% 420 | dplyr::select(ID, gene_assignment) %>% 421 | dplyr::rename(prode_id = "ID", GeneSymbol = "gene_assignment") 422 | tmp <- strsplit(GPL$GeneSymbol, split = " // ") 423 | lapply(tmp, \(x)x[2]) %>% do.call(c, .) -> GPL$GeneSymbol 424 | na.omit(GPL) -> GPL 425 | } else if (GPL_version %in% c("GPL1536")) { 426 | GPL <- GPL_data_11 %>% 427 | dplyr::select(ID, GENE) %>% 428 | dplyr::rename(prode_id = "ID", GeneSymbol = "GENE") 429 | GPL$GeneSymbol <- gsub("/.*", "", GPL$GeneSymbol) 430 | } else if (GPL_version %in% c("GPL10739")) { 431 | GPL <- GPL_data_11 %>% 432 | dplyr::select(ID, gene_assignment) %>% 433 | dplyr::rename(prode_id = "ID", GeneSymbol = "gene_assignment") 434 | tmp <- strsplit(GPL$GeneSymbol, split = " // ") 435 | lapply(tmp, \(x)x[2]) %>% do.call(c, .) -> GPL$GeneSymbol 436 | na.omit(GPL) -> GPL 437 | tmp <- strsplit(GPL$GeneSymbol, split = " /// ") 438 | lapply(tmp, \(x)x[1]) %>% do.call(c, .) -> GPL$GeneSymbol 439 | na.omit(GPL) -> GPL 440 | } else { 441 | stop("unknown GPL version, please check and modify the code.") 442 | } 443 | 444 | GPL <- GPL %>% 445 | add_count(GeneSymbol, name = "n_gene") %>% 446 | add_count(prode_id, name = "n_prode_id") %>% 447 | dplyr::filter(n_gene < 100) %>% # 选择能有对应基因的探针,太多了说明非特异 448 | dplyr::select(c(-3, -4)) %>% 449 | mutate_all(as.character) 450 | 451 | GSE_data_expr[is.na(GSE_data_expr)] <- 0 452 | GSE_expr <- GSE_data_expr %>% 453 | inner_join(GPL, ., by = "prode_id") %>% # p2s和上一步得到的结果再取交集,p2s放在右边是以它为准 454 | dplyr::select(-1) %>% # 去除第一列probe_id 455 | data.frame() %>% # 因为aggregate需要数据框格式 456 | aggregate(. ~ GeneSymbol, data = ., mean) %>% # 以symbol作为因子水平,把相似的数据放在一起取均值,最大值max,中位值median 457 | tibble::column_to_rownames(var = "GeneSymbol") 458 | 459 | rownames(GSE_expr) %>% gsub(" ", "", .) -> rownames(GSE_expr) 460 | 461 | saveRDS(list(meta = meta, GSE_expr = GSE_expr), file = paste0(GEO_dir, "/", my_id, ".RDS")) 462 | return(list(meta = meta, GSE_expr = GSE_expr)) 463 | } 464 | -------------------------------------------------------------------------------- /R/eco_micro.R: -------------------------------------------------------------------------------- 1 | # Network======== 2 | 3 | #' Correlation network, species-interaction network for omics 4 | #' 5 | #' @return No value 6 | #' @export 7 | cor_net <- function() { 8 | if (!requireNamespace("MetaNet")) { 9 | message('All network related functions have been migrated to `MetaNet`, please install `MetaNet`: 10 | remotes::install_github("Asa12138/MetaNet") 11 | visit the website: https://github.com/Asa12138/MetaNet') 12 | } else { 13 | message("All network related functions have been migrated to `MetaNet`, you have already installed `MetaNet`, please: 14 | library(MetaNet) 15 | help(c_net_build)") 16 | } 17 | } 18 | 19 | # Stochastic or Determine========= 20 | #' Calculate NST for each group 21 | #' 22 | #' @param otutab an otutab data.frame, samples are columns, taxs are rows. 23 | #' @param group_df a dataframe with rowname and one group column 24 | #' @param threads default:4 25 | #' @param file filename to save 26 | #' @param rep repeat numbers: suggest 999 27 | #' @param save save the file 28 | #' 29 | #' @return a b_dist object, dis is MSTij 30 | #' @export 31 | #' 32 | #' @references 33 | #' Ning, D., Deng, Y., Tiedje, J. M. & Zhou, J. (2019) A general framework for quantitatively assessing ecological stochasticity. Proceedings of the National Academy of Sciences 116, 16892–16898. 34 | #' @examples 35 | #' \donttest{ 36 | #' if (requireNamespace("NST")) { 37 | #' library(ggplot2) 38 | #' data(otutab, package = "pcutils") 39 | #' nst(otutab, metadata["Group"]) -> nst_res 40 | #' plot(nst_res, c_group = "intra") + geom_hline(yintercept = 0.5, lty = 2) + ylab("NST") 41 | #' } 42 | #' } 43 | nst <- function(otutab, group_df, threads = 1, file = NULL, rep = 20, save = FALSE) { 44 | lib_ps("NST", library = FALSE) 45 | MST.ij.ruzicka <- NULL 46 | tnst <- NST::tNST(comm = t(otutab), group = group_df, rand = rep, output.rand = TRUE, nworker = threads) 47 | 48 | tnst$index.pair %>% 49 | dplyr::mutate(dis = MST.ij.ruzicka) %>% 50 | as.dist.b_dist() -> NST_ij 51 | 52 | as.b_dist(NST_ij, group_df) -> NST 53 | 54 | if (is.null(file)) file <- paste0("nst_res_", date(), ".RDS") 55 | if (save) { 56 | saveRDS(tnst, file = file) 57 | message(paste0("Result saved as ", file)) 58 | } 59 | return(NST) 60 | } 61 | 62 | #' Calculate b_NTI and RC_bray for each group 63 | #' 64 | #' @param otutab an otutab data.frame, samples are columns, taxs are rows. 65 | #' @param phylo a phylo object 66 | #' @param group_df a dataframe with rowname and one group column 67 | #' @param threads default:4 68 | #' @param file filename to save 69 | #' @param rep repeat numbers: suggest 999 70 | #' @param save save the file 71 | #' @return a b_dist object, dis is MSTij 72 | #' @export 73 | #' 74 | #' @references 75 | #' Ning, D., Deng, Y., Tiedje, J. M. & Zhou, J. (2019) A general framework for quantitatively assessing ecological stochasticity. Proceedings of the National Academy of Sciences 116, 16892–16898. 76 | #' @examples 77 | #' \donttest{ 78 | #' if (requireNamespace("NST") && requireNamespace("pctax")) { 79 | #' data(otutab, package = "pcutils") 80 | #' pctax::df2tree(taxonomy) -> phylo 81 | #' nti_rc(otutab, phylo, metadata["Group"]) -> nti_res 82 | #' plot(nti_res) 83 | #' } 84 | #' } 85 | nti_rc <- function(otutab, phylo, group_df, threads = 1, file = NULL, rep = 20, save = FALSE) { 86 | lib_ps("NST", library = FALSE) 87 | bNTI.wt <- RC.bMNTD.wt <- dis <- group <- name1 <- name2 <- variable <- bNTI <- NULL 88 | pdist <- stats::cophenetic(phylo) 89 | 90 | bnti_res <- NST::pNST( 91 | comm = t(otutab), pd = pdist, group = group_df, 92 | pd.wd = tempdir(), rand = rep, nworker = threads, SES = TRUE, RC = TRUE 93 | ) 94 | 95 | bnti_res$index.pair %>% 96 | dplyr::mutate(dis = bNTI.wt) %>% 97 | as.dist.b_dist() -> b_NTI_ij 98 | as.b_dist(b_NTI_ij, group_df) -> b_NTI 99 | bnti_res$index.pair %>% 100 | dplyr::mutate(dis = RC.bMNTD.wt) %>% 101 | as.dist.b_dist() -> RC_ij 102 | as.b_dist(RC_ij, group_df) -> RC 103 | 104 | b_NTI %>% 105 | dplyr::mutate(bNTI = dis, RC = RC$dis) %>% 106 | dplyr::filter(group == "intra") %>% 107 | dplyr::select(name1, name2, variable, bNTI, RC) -> NTI_RC 108 | NTI_RC %>% dplyr::mutate(type = ifelse(bNTI < (-2), "Homo_S", 109 | ifelse(bNTI > 2, "Heter_S", 110 | ifelse(RC < (-0.95), "D_limit", 111 | ifelse(RC > 0.95, "Undominated", "Homo_D") 112 | ) 113 | ) 114 | )) -> NTI_RC 115 | 116 | if (is.null(file)) file <- paste0("nti_rc_res_", date(), ".RDS") 117 | if (save) saveRDS(bnti_res, file = file) 118 | message(paste0("Result saved as ", file)) 119 | class(NTI_RC) <- c("NTI_RC", "data.frame") 120 | return(NTI_RC) 121 | } 122 | 123 | #' Plot NTI_RC object 124 | #' 125 | #' @param x NTI_RC object 126 | #' @param ... pass to \code{\link[pcutils]{stackplot}} 127 | #' 128 | #' @return ggplot 129 | #' @exportS3Method 130 | #' @method plot NTI_RC 131 | #' @rdname nti_rc 132 | plot.NTI_RC <- function(x, ...) { 133 | nti_res <- x 134 | nti_res$type <- factor(nti_res$type, levels = c("Homo_S", "Heter_S", "Homo_D", "D_limit", "Undominated")) 135 | table(nti_res$type, nti_res$variable) %>% 136 | reshape2::melt() %>% 137 | reshape2::acast(Var1 ~ Var2) -> com_p 138 | pcutils::stackplot(com_p, ...) 139 | } 140 | 141 | #' Sloan Neutral Model 142 | #' 143 | #' @param otutab an otutab data.frame, samples are columns, taxs are rows. 144 | #' @param model fit method, one of "nls","mle" 145 | #' 146 | #' @return ncm_res 147 | #' @export 148 | #' @references 149 | #' Sloan, W. TRUE. et al. (2006) Quantifying the roles of immigration and chance in shaping prokaryote community structure. Environmental Microbiology 8, 732–740. 150 | #' @examples 151 | #' \donttest{ 152 | #' if (requireNamespace("Hmisc") && requireNamespace("minpack.lm")) { 153 | #' data(otutab, package = "pcutils") 154 | #' ncm(otutab) -> ncm_res 155 | #' plot(ncm_res) 156 | #' } 157 | #' } 158 | ncm <- function(otutab, model = "nls") { 159 | lib_ps("Hmisc", library = FALSE) 160 | otutab <- otutab[rowSums(otutab) > 0, ] 161 | spp <- t(otutab) 162 | otu.pa <- (spp > 0) * 1 163 | freq <- apply(otu.pa, 2, mean) 164 | N <- mean(apply(spp, 1, sum)) 165 | p <- apply(spp, 2, function(x) mean(x)) / N 166 | d <- 1 / N 167 | if (model == "nls") { 168 | lib_ps("minpack.lm", library = FALSE) 169 | ## Fit model parameter m (or Nm) using Non-linear least squares (NLS) 170 | m.fit <- minpack.lm::nlsLM(freq ~ stats::pbeta(d, N * m * p, N * m * (1 - p), lower.tail = FALSE), start = list(m = 0.1)) 171 | 172 | # coef(m.fit) #get the m value 173 | freq.pred <- stats::pbeta(d, N * stats::coef(m.fit) * p, N * stats::coef(m.fit) * (1 - p), lower.tail = FALSE) 174 | pred.ci <- Hmisc::binconf(freq.pred * nrow(spp), nrow(spp), alpha = 0.05, method = "wilson", return.df = TRUE) 175 | 176 | Rsqr <- 1 - (sum((freq - freq.pred)^2)) / (sum((freq - mean(freq))^2)) 177 | bacnlsALL <- data.frame(p, freq, freq.pred, pred.ci[, 2:3]) 178 | 179 | stats <- data.frame(m = stats::coef(m.fit), R2 = Rsqr, N = N, samples = ncol(otutab), tax = nrow(otutab), d = d) 180 | } else if (model == "mle") { 181 | lib_ps("bbmle", library = FALSE) 182 | # Maximum Likelihood Estimation 183 | neutral.ll <- function(m, sigma) { 184 | R <- freq - stats::pbeta(d, N * m * p, N * m * (1 - p), lower.tail = FALSE) 185 | -sum(stats::dnorm(R, 0, sigma, log = TRUE)) 186 | } 187 | m.mle <- bbmle::mle2(neutral.ll, 188 | start = list(m = 0.01, sigma = 0.1), 189 | method = "Nelder-Mead" 190 | ) 191 | 192 | freq.pred <- stats::pbeta(d, N * m.mle@coef["m"] * p, N * m.mle@coef["m"] * (1 - p), lower.tail = FALSE) 193 | pred.ci <- Hmisc::binconf(freq.pred * nrow(spp), nrow(spp), alpha = 0.05, method = "wilson", return.df = TRUE) 194 | 195 | gRsqr <- 1 - exp(-as.numeric(stats::logLik(m.mle)) / length(p)) 196 | bacnlsALL <- data.frame(p, freq, freq.pred, pred.ci[, 2:3]) 197 | stats <- data.frame(m = m.mle@coef["m"], R2 = gRsqr, N = N, samples = ncol(otutab), tax = nrow(otutab), d = d) 198 | } 199 | bacnlsALL$group <- NA 200 | bacnlsALL$group[bacnlsALL[, 2] < bacnlsALL[, 4]] <- "Below" ## 低于下界 201 | bacnlsALL$group[bacnlsALL[, 2] > bacnlsALL[, 5]] <- "Above" ## 高于上界 202 | bacnlsALL$group[(bacnlsALL[, 2] >= bacnlsALL[, 4]) & (bacnlsALL[, 2] <= bacnlsALL[, 5])] <- "In" ## 中间 203 | bacnlsALL <- bacnlsALL[rownames(otutab), ] 204 | res <- list(stats = stats, ncm_data = bacnlsALL) 205 | class(res) <- "ncm_res" 206 | return(res) 207 | } 208 | 209 | #' Plot ncm_res 210 | #' 211 | #' @param x a ncm_res object 212 | #' @param ... add 213 | #' @param mycols mycols 214 | #' @param text_position text_position 215 | #' @param pie_text_params pie text parameters 216 | #' 217 | #' @return ggplot 218 | #' @exportS3Method 219 | #' @method plot ncm_res 220 | #' @rdname ncm 221 | plot.ncm_res <- function(x, mycols = c("Above" = "#069870", "Below" = "#e29e02", "In" = "#1e353a"), 222 | text_position = NULL, pie_text_params = list(size = 2.5), ...) { 223 | ncm_res <- x 224 | lib_ps("patchwork", library = FALSE) 225 | p <- freq.pred <- Lower <- Upper <- freq <- group <- NULL 226 | out <- ncm_res[[2]] 227 | lincol <- "#4a8fb8" 228 | p1 <- ggplot() + 229 | geom_line(data = out, aes(x = log(p), y = freq.pred), linewidth = 1.2, linetype = 1, col = lincol) + 230 | geom_line(data = out, aes(x = log(p), y = Lower), linewidth = 1.2, linetype = 2, col = lincol) + 231 | geom_line(data = out, aes(x = log(p), y = Upper), linewidth = 1.2, linetype = 2, col = lincol) + 232 | xlab("log10(mean relative abundance)") + 233 | ylab("Occurrence frequency") 234 | 235 | if (is.null(text_position)) { 236 | text_position <- c(log(max(out$p)) - 2, 0.05) 237 | } 238 | 239 | p2 <- p1 + 240 | geom_point(data = out, aes(x = log(p), y = freq, color = group), size = 1) + 241 | scale_colour_manual(values = mycols) + 242 | annotate("text", x = text_position[1], y = text_position[2], label = paste("Nm = ", sprintf("%.0f", ncm_res[[1]][1] * ncm_res[[1]][3]), sep = ""), size = 4) + 243 | annotate("text", x = text_position[1], y = text_position[2] + 0.1, label = paste0("R^2 == ", round(ncm_res[[1]][2], 3)), size = 4, parse = TRUE) + 244 | pctax_theme + theme( 245 | legend.position = c(0.85, 0.3), 246 | legend.title = element_blank(), legend.background = element_rect(I(0)) 247 | ) + 248 | guides(color = guide_legend(override.aes = list(size = 3))) 249 | 250 | out$group %>% 251 | table() %>% 252 | as.data.frame() -> ad 253 | colnames(ad) <- c("type", "n") 254 | pie <- pcutils::gghuan(ad, name = FALSE, text_params = pie_text_params) + 255 | xlim(0.2, 3.3) + 256 | scale_fill_manual(values = mycols) + 257 | theme( 258 | plot.background = element_rect(I(0), linetype = 0), 259 | panel.background = element_rect(I(0)) 260 | ) # 去除图片背景白色 261 | 262 | p2 + patchwork::inset_element(pie, left = -0.1, bottom = 0.6, right = 0.3, top = 1.1) 263 | } 264 | 265 | 266 | #' Calculate beta_NTI 267 | #' 268 | #' @param phylo a phylo object 269 | #' @param otutab otutab 270 | #' @param beta.reps how many simulation performed? 271 | #' @param threads use how many threads to calculate (default:4) 272 | #' @param weighted logical 273 | #' @param verbose verbose 274 | #' 275 | #' @return a dist: b_NTI 276 | #' @export 277 | b_NTI1 <- function(phylo, otutab, beta.reps = 9, weighted = TRUE, threads = 1, verbose = TRUE) { 278 | lib_ps("picante", library = FALSE) 279 | # match tree and otutab (important) 280 | phy_otu_m <- picante::match.phylo.data(phylo, otutab) 281 | # picante::comdistnt:Calculates inter-community mean nearest taxon distance 282 | # cophenetic:Cophenetic Distances for a Hierarchical Clustering 283 | 284 | beta.mntd.weighted <- as.matrix(picante::comdistnt(t(phy_otu_m$data), 285 | stats::cophenetic(phy_otu_m$phy), 286 | abundance.weighted = weighted 287 | )) 288 | 289 | rand.weighted.bMNTD.comp <- array(c(-999), dim = c(ncol(phy_otu_m$data), ncol(phy_otu_m$data), beta.reps)) 290 | if (threads == 1) { 291 | # no parallel 292 | for (rep in 1:beta.reps) { 293 | rand.weighted.bMNTD.comp[, , rep] <- as.matrix( 294 | picante::comdistnt(t(phy_otu_m$data), picante::taxaShuffle(stats::cophenetic(phy_otu_m$phy)), 295 | abundance.weighted = weighted, exclude.conspecifics = FALSE 296 | ) 297 | ) 298 | # print(c(date(),rep)) 299 | } 300 | } else if (threads > 1) { 301 | # parallel 302 | pcutils::lib_ps("foreach", "doSNOW", "snow", library = FALSE) 303 | if (verbose) { 304 | pb <- utils::txtProgressBar(max = beta.reps, style = 3) 305 | opts <- list(progress = function(n) utils::setTxtProgressBar(pb, n)) 306 | } else { 307 | opts <- NULL 308 | } 309 | cl <- snow::makeCluster(threads) 310 | doSNOW::registerDoSNOW(cl) 311 | rand.weighted.bMNTD.comp <- foreach::`%dopar%`(foreach::foreach( 312 | rep = 1:beta.reps, 313 | .options.snow = opts, 314 | .packages = c("picante") 315 | ), as.matrix(picante::comdistnt(t(phy_otu_m$data), 316 | picante::taxaShuffle(stats::cophenetic(phy_otu_m$phy)), 317 | abundance.weighted = TRUE, exclude.conspecifics = FALSE 318 | ))) 319 | snow::stopCluster(cl) 320 | gc() 321 | pcutils::del_ps("doSNOW", "snow", "foreach") 322 | rand.weighted.bMNTD.comp <- simplify2array(rand.weighted.bMNTD.comp) 323 | } 324 | weighted.bNTI <- matrix(c(NA), nrow = ncol(phy_otu_m$data), ncol = ncol(phy_otu_m$data)) 325 | for (columns in 1:(ncol(phy_otu_m$data) - 1)) { 326 | for (rows in (columns + 1):ncol(phy_otu_m$data)) { 327 | rand.vals <- rand.weighted.bMNTD.comp[rows, columns, ] 328 | weighted.bNTI[rows, columns] <- (beta.mntd.weighted[rows, columns] - mean(rand.vals)) / sd(rand.vals) 329 | rm("rand.vals") 330 | } 331 | } 332 | rownames(weighted.bNTI) <- colnames(phy_otu_m$data) 333 | colnames(weighted.bNTI) <- colnames(phy_otu_m$data) 334 | return(as.dist(weighted.bNTI)) 335 | } 336 | 337 | 338 | #' Calculate RCbray-curtis 339 | #' 340 | #' @param otutab otutab 341 | #' @param reps how many simulation performed? 342 | #' @param threads use how many threads to calculate (default:4) 343 | #' @param classic_metric standardizes the metric to range from -1 to 1 344 | #' @param split_ties adds half of the number of null observations that are equal to the observed number of shared species to the calculation- this is highly recommended 345 | #' 346 | #' @details Parallelized version of the Raup-Crick algorithm for "abundance" data (Stegen et al. 2013). 347 | #' @return a dist 348 | #' @export 349 | #' 350 | #' @examples 351 | #' \donttest{ 352 | #' if (requireNamespace("picante")) { 353 | #' data(otutab, package = "pcutils") 354 | #' df2tree(taxonomy) -> phylo 355 | #' b_NTI1(phylo, otutab) -> bnti_res 356 | #' RCbray1(otutab, reps = 9) -> rc_res 357 | #' 358 | #' data.frame( 359 | #' type = factor(c("Homo_S", "Heter_S", "Homo_D", "D_limit", "Undominated"), 360 | #' levels = c("Homo_S", "Heter_S", "Homo_D", "D_limit", "Undominated") 361 | #' ), 362 | #' number = c( 363 | #' sum(bnti_res < (-2)), sum(bnti_res > 2), 364 | #' sum((abs(bnti_res) < 2) & (abs(rc_res) < 0.95)), 365 | #' sum((abs(bnti_res) < 2) & (rc_res < (-0.95))), 366 | #' sum((abs(bnti_res) < 2) & (rc_res > 0.95)) 367 | #' ) 368 | #' ) -> com_pro 369 | #' pcutils::gghuan(com_pro, reorder = FALSE) 370 | #' } 371 | #' } 372 | RCbray1 <- function(otutab, reps = 9, threads = 1, classic_metric = TRUE, split_ties = TRUE) { 373 | com <- t(otutab) 374 | 375 | pcutils::lib_ps("foreach", "doSNOW", "snow") 376 | pb <- utils::txtProgressBar(max = reps, style = 3) 377 | opts <- list(progress = function(n) utils::setTxtProgressBar(pb, n)) 378 | cl <- snow::makeCluster(threads) 379 | doSNOW::registerDoSNOW(cl) 380 | 381 | bray.rand <- foreach::`%dopar%`(foreach::foreach(randomize = 1:reps, .options.snow = opts, .packages = c("vegan")), { 382 | null.dist <- com * 0 383 | for (i in 1:nrow(com)) { 384 | com.pa <- (com > 0) * 1 385 | gamma <- ncol(com) 386 | occur <- apply(com > 0, MARGIN = 2, FUN = sum) 387 | abundance <- apply(com, MARGIN = 2, FUN = sum) 388 | com1 <- rep(0, gamma) 389 | com1[sample(1:gamma, sum(com.pa[i, ]), replace = FALSE, prob = occur)] <- 1 390 | com1.samp.sp <- sample(which(com1 > 0), (sum(com[i, ]) - sum(com1)), 391 | replace = TRUE, prob = abundance[which(com1 > 0)] 392 | ) 393 | com1.samp.sp <- cbind(com1.samp.sp, 1) 394 | com1.sp.counts <- as.data.frame(tapply(com1.samp.sp[, 2], com1.samp.sp[, 1], FUN = sum)) 395 | colnames(com1.sp.counts) <- "counts" 396 | com1.sp.counts$sp <- as.numeric(rownames(com1.sp.counts)) 397 | com1[com1.sp.counts$sp] <- com1[com1.sp.counts$sp] + com1.sp.counts$counts 398 | x <- com1 399 | null.dist[i, ] <- x 400 | rm("com1.samp.sp", "com1.sp.counts") 401 | } 402 | as.matrix(vegan::vegdist(null.dist, "bray")) 403 | }) 404 | 405 | snow::stopCluster(cl) 406 | gc() 407 | pcutils::del_ps("doSNOW", "snow", "foreach") 408 | ## Calculate beta-diversity for obs metacommunity 409 | bray.obs <- as.matrix(vegan::vegdist(com, "bray")) 410 | 411 | ## how many null observations is the observed value tied with? 412 | null_bray_curtis <- bray.rand 413 | num_exact_matching_in_null <- lapply(null_bray_curtis, function(x) x == bray.obs) 414 | num_exact_matching_in_null <- apply(simplify2array(num_exact_matching_in_null), 1:2, sum) 415 | 416 | ## how many null values are smaller than the observed *dissimilarity*? 417 | num_less_than_in_null <- lapply(null_bray_curtis, function(x) (x < bray.obs) * 1) 418 | num_less_than_in_null <- apply(simplify2array(num_less_than_in_null), 1:2, sum) 419 | 420 | rc <- (num_less_than_in_null) / reps # rc; 421 | 422 | if (split_ties) { 423 | rc <- ((num_less_than_in_null + (num_exact_matching_in_null) / 2) / reps) 424 | } 425 | if (!classic_metric) { 426 | ## our modification of raup crick standardizes the metric to range from -1 to 1 instead of 0 to 1 427 | rc <- (rc - .5) * 2 428 | } 429 | return(as.dist(rc)) 430 | } 431 | 432 | # =========iCAMP==== 433 | 434 | # 里面有更多的写好的函数 435 | if (FALSE) { 436 | # https://mp.weixin.qq.com/s?__biz=MzI1OTk3NjEwMA==&mid=2247487267&idx=1&sn=6b776e219d1a782adc96b3f4937c2d7a&chksm=ea71f1e8dd0678fe0f5e3e03a06f98483cf2923c0df0f600c821dcda8c109217924afbe91ad4&scene=21#wechat_redirect 437 | # wd0=getwd() # please change to the folder you want to save the pd.big output. 438 | tax_tree <- df2tree(taxonomy) 439 | comm <- t(otutab) 440 | tree <- tax_tree 441 | picante::match.phylo.comm(tree, comm) 442 | # since need to save some output to a certain folder, 443 | # the following code is set as 'not test'. 444 | # but you may test the code on your computer 445 | # after change the folder path for 'pd.wd'. 446 | ## No test: 447 | wd0 <- getwd() # please change to the folder you want to save the pd.big output. 448 | pd.wd <- paste0(tempdir(), "/pdbig.icampbig") 449 | nworker <- 4 # parallel computing thread number 450 | rand.time <- 99 # usually use 1000 for real data. 451 | 452 | bin.size.limit <- 48 # for real data, usually use a proper number 453 | # according to phylogenetic signal test or try some settings 454 | # then choose the reasonable stochasticity level. 455 | # our experience is 12, or 24, or 48. 456 | # but for this example dataset which is too small, have to use 5. 457 | 458 | icamp.out <- iCAMP::icamp.big( 459 | comm = comm, tree = tree, pd.wd = pd.wd, 460 | rand = rand.time, nworker = nworker, 461 | bin.size.limit = bin.size.limit 462 | ) 463 | 464 | # 计算树的距离矩阵 465 | pd <- iCAMP::pdist.p(phylo, nworker = 4) 466 | pd.big <- iCAMP::pdist.big(phylo) 467 | # 计算bNTI 468 | iCAMP::bmpd(t(otutab), pd) 469 | bnti <- iCAMP::bNTIn.p(t(otutab), pd, rand = 20) 470 | # 计算RC 471 | iCAMP::RC.pc(t(otutab), rand = 20) 472 | # 计算生态位差异 473 | iCAMP::dniche() 474 | } 475 | 476 | 477 | # beta-diversity decomposition 478 | if (FALSE) { 479 | # #https://mp.weixin.qq.com/s?__biz=MzI1OTk3NjEwMA==&mid=2247484144&idx=1&sn=a181f9d6a5e47681727901b80907c24e&chksm=ea71fc3bdd06752df3cac0865ac94736138abde1d85741625e4ccbc3c6d19e34896dce06b6cd&scene=21#wechat_redirect 480 | # lib_ps("betapart",library = FALSE) 481 | # data(ceram.s) 482 | # #得到三个矩阵,两两成对比较。分别为总beta多样性jac or sor,物种转换jtu or sim,物种增减jne or sne. 483 | # ceram.core.s<-betapart::betapart.core(ceram.s) 484 | # ceram.dist.jac<-betapart::beta.pair(ceram.core.s, index.family="jac") 485 | # ceram.dist.sor<-betapart::beta.pair(ceram.core.s, index.family="sor") 486 | # ceram.multi.jac<-betapart::beta.multi(ceram.core.s, index.family="jac") 487 | # ceram.multi.sor<-betapart::beta.multi(ceram.core.s, index.family="sor") 488 | } 489 | 490 | 491 | # ======ecological niche===== 492 | ## https://mp.weixin.qq.com/s?__biz=MzI1OTk3NjEwMA==&mid=2247485757&idx=1&sn=88678b4be64ac8774f0672b0155c295c&chksm=ea71f7f6dd067ee0407c9ee894a1d6ba8e052fa8a66e0df31e950309093f4549b0e3a5aa6830&scene=21#wechat_redirect 493 | 494 | if (FALSE) { 495 | # lib_ps("spaa",library = FALSE) 496 | # #### Niche width and niche overlap 497 | # #niche.width计算生态位宽度 498 | # #mat:列为物种,行为样本 499 | # #method:计算方法 500 | # niche_w<-spaa::niche.width(t(otutab), method = "levins")%>%t%>%as.data.frame() 501 | # 502 | # #每两个物种之间生态位的重叠 503 | # spaa::niche.overlap(datasample[,1:3], method = "levins") 504 | # #https://mp.weixin.qq.com/s?__biz=MzI1OTk3NjEwMA==&mid=2247486549&idx=1&sn=05c283e4cf0afc4d338f1ca1c11dc5fc&chksm=ea71f29edd067b8829272ee565242ad9b08d18c4813d9e025144b41b9506c4cba40b754e2b6e&scene=21#wechat_redirect 505 | # lib_ps("MicroNiche",library = FALSE) 506 | # ?MicroNiche::levins.Bn() 507 | # ?MicroNiche::hurlberts.Bn() 508 | # MicroNiche::hurlberts.Bn(otutab) 509 | # ?MicroNiche::feinsingers.PS() 510 | # #https://mp.weixin.qq.com/s?__biz=MzI1OTk3NjEwMA==&mid=2247485087&idx=1&sn=95d121fc4080aa9af2a61de22694093d&chksm=ea71f854dd067142c0b201657c57aeb6102cef5f738fc89380783efa8c413580515b8536e5a6&scene=21#wechat_redirect 511 | # lib_ps("indicspecies",library = FALSE) 512 | # 513 | # #labdsv包的indval函数可计算群落中的指示物种 514 | # lib_ps("labdsv",library = FALSE) 515 | # labdsv::indval() 516 | # #分析微生物群落和环境因子相关性的工具vegan::bioenv 517 | # vegan::bioenv() 518 | # #表征微生物抵抗性的一个指标(要有实验-对照的关系才能算) 519 | # #Rs=1-(2*|D0|/(C0+|D0|)) 520 | # #其中C0为对照组样本的α多样性指数,D0为对照组样本多样性和实验组样本α多样性之间的差值。 521 | # #Rs取值为-1到1。越接近1表明处理效应非常小,即微生物抵抗性很强;Rs越小表明处理效应越强,即微生物抵抗力越弱。 522 | # 523 | # #DNCI群落构建 524 | # lib_ps("DNCImper",library = FALSE) 525 | # ?PerSIMPER 526 | # #用法 527 | # DNCImper:::PerSIMPER( 528 | # matrixSIMP, #行为样本,列为物种 529 | # Groups, #只允许两个组 530 | # leg = FALSE, #是否添加图例 531 | # count = TRUE,#显示完成置换的次数 532 | # dataTYPE = "prab", #prab为0-1数据;count为丰度数据 533 | # Nperm = 1000,#置换次数 534 | # plotSIMPER = TRUE 535 | # ) 536 | # A <- DNCImper:::PerSIMPER(Matrix, Group,Nperm = 100,count = FALSE) 537 | # #上述只能针对两个组,若有更多的组,可用PerSIMPER_overall,计算整体的PerSIMPER 538 | # #DNCI.ese 计算DNCI效应量,只允许两组。 539 | # #三组或以上用DNCI.ses_overall,计算整体的DNCI。 540 | # #三组或以上若用DNCI_multigroup,计算两两成对DNCI。 541 | } 542 | --------------------------------------------------------------------------------