From 538e1df74edac603168c200843e40cd6f19e63e9 Mon Sep 17 00:00:00 2001 From: noriakis <31095487+noriakis@users.noreply.github.com> Date: Sun, 10 Mar 2024 20:38:31 +0900 Subject: [PATCH] update NMF --- DESCRIPTION | 2 +- NAMESPACE | 5 ++++ R/NMF.R | 39 ++++++++++++++++++++++++++++++ R/stana.R | 34 ++++++++++++++++++++++++++ man/NMF.Rd | 33 +++++++++++++++++++++++++ man/alphaDiversityWithinSpecies.Rd | 11 +++++++++ man/gene.Rd | 14 +++++++++++ man/snv.Rd | 14 +++++++++++ 8 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 R/NMF.R create mode 100644 man/NMF.Rd create mode 100644 man/alphaDiversityWithinSpecies.Rd create mode 100644 man/gene.Rd create mode 100644 man/snv.Rd diff --git a/DESCRIPTION b/DESCRIPTION index ded00e0..3430644 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -6,7 +6,7 @@ License: GPL-3 Description: Analysis toolkit for intra-species diversity from metagenomics. Authors@R: person("Noriaki", "Sato", email = "nori@hgc.jp", role = c("cre", "aut")) Depends: ggplot2, ggstar, ggraph, igraph -Imports: GetoptLong, BiocFileCache, RCurl, igraph, vegan, methods, data.table, phangorn, RColorBrewer, ggtree, circlize, ComplexHeatmap, ggkegg, ape, dplyr, exactRankTests, ggblend, ggh4x, scales, tidygraph, ggplotify, ggtreeExtra, ggnewscale, scico, MKmisc +Imports: GetoptLong, BiocFileCache, RCurl, igraph, vegan, methods, data.table, phangorn, RColorBrewer, ggtree, circlize, ComplexHeatmap, ggkegg, ape, dplyr, exactRankTests, ggblend, ggh4x, scales, tidygraph, ggplotify, ggtreeExtra, ggnewscale, scico, MKmisc, NMF Suggests: simplifyEnrichment, stringr, tidyr, Boruta, knitr, ggrepel RoxygenNote: 7.3.1 VignetteBuilder: knitr diff --git a/NAMESPACE b/NAMESPACE index 27c8baf..ce9dfa3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,7 +1,9 @@ # Generated by roxygen2: do not edit by hand +export(NMF) export(addGeneAbundance) export(alleleStat) +export(alphaDiversityWithinSpecies) export(anno_PATRIC_keywords) export(anno_eggNOG_keywords) export(calcKO) @@ -25,6 +27,7 @@ export(doL0) export(drawEGGNOG) export(drawPATRIC) export(exportInteractive) +export(gene) export(genomeHeatmap) export(getAdonis) export(getCl) @@ -57,10 +60,12 @@ export(setGroup) export(setMap) export(setMetadata) export(setTree) +export(snv) export(strainClusterHeatmap) export(summariseAbundance) import(BiocFileCache) import(GetoptLong) +import(NMF) import(RColorBrewer) import(RCurl) import(ggplot2) diff --git a/R/NMF.R b/R/NMF.R new file mode 100644 index 0000000..fc76540 --- /dev/null +++ b/R/NMF.R @@ -0,0 +1,39 @@ + +#' NMF +#' @param stana stana object +#' @param species candidate species ID +#' @param rank rank of NMF +#' @param target KO, gene or snv, default to KO +#' @param seed random seed +#' @param method NMF method, default to snmf/r +#' @import NMF +#' @export +NMF <- function(stana, species, rank=10, target="KO", seed=53, method="snmf/r", + deleteZeroDepth=TRUE, beta=0.01) { + if (target=="KO") { + mat <- stana@kos[[species]] + } else if (target=="genes") { + mat <- stana@genes[[species]] + } else if (target=="snps") { + mat <- stana@snps[[species]] + if (deleteZeroDepth) { + mat <- mat[rowSums(mat == -1)==0,] + qqcat("After filtering `-1`, position numbers: @{dim(mat)[1]}\n") + } + } + res <- NMF::nmf(mat, rank = rank, seed = seed, method=method, beta=beta) + stana@NMF[[species]] <- res + return(stana) +} + +#' alphaDiversityWithinSpecies +#' @export +alphaDiversityWithinSpecies <- function(stana, species) { + if (is.null(stana@NMF[[species]])) { + stana <- NMF(stana, species) + } + res <- stana@NMF[[species]] + W <- coef(res) + div <- vegan::diversity(t(W)) + return(div) +} \ No newline at end of file diff --git a/R/stana.R b/R/stana.R index 442bb1c..91a168f 100644 --- a/R/stana.R +++ b/R/stana.R @@ -21,6 +21,7 @@ setClass("stana", slots=list( strainClusters="data.table", snps="list", eggNOG="list", + NMF = "list", kos="list", snpsInfo="list", snpsDepth="list", @@ -238,6 +239,39 @@ changeColors <- function(stana, colors) { return(stana) } +#' snv +#' @param dfs named list of dfs +#' @export +snv <- function(dfs) { + stana <- new("stana") + stana@type <- "manual" + + if (is.null(names(dfs))) { + ids <- paste0("species", seq_len(length(dfs))) + } else { + ids <- names(dfs) + } + stana@snps <- dfs + stana@ids <- ids + return(stana) +} + +#' gene +#' @param dfs named list of dfs +#' @export +gene <- function(dfs) { + stana <- new("stana") + stana@type <- "manual" + if (is.null(names(dfs))) { + ids <- paste0("species", seq_len(length(dfs))) + } else { + ids <- names(dfs) + } + stana@genes <- dfs + stana@ids <- ids + return(stana) +} + #' getGenes (concatenated to checkProfile) #' #' Obtain gene matrix from midas merge directory from MIDAS. diff --git a/man/NMF.Rd b/man/NMF.Rd new file mode 100644 index 0000000..2e398d5 --- /dev/null +++ b/man/NMF.Rd @@ -0,0 +1,33 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/NMF.R +\name{NMF} +\alias{NMF} +\title{NMF} +\usage{ +NMF( + stana, + species, + rank = 10, + target = "KO", + seed = 53, + method = "snmf/r", + deleteZeroDepth = TRUE, + beta = 0.01 +) +} +\arguments{ +\item{stana}{stana object} + +\item{species}{candidate species ID} + +\item{rank}{rank of NMF} + +\item{target}{KO, gene or snv, default to KO} + +\item{seed}{random seed} + +\item{method}{NMF method, default to snmf/r} +} +\description{ +NMF +} diff --git a/man/alphaDiversityWithinSpecies.Rd b/man/alphaDiversityWithinSpecies.Rd new file mode 100644 index 0000000..c14f05c --- /dev/null +++ b/man/alphaDiversityWithinSpecies.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/NMF.R +\name{alphaDiversityWithinSpecies} +\alias{alphaDiversityWithinSpecies} +\title{alphaDiversityWithinSpecies} +\usage{ +alphaDiversityWithinSpecies(stana, species) +} +\description{ +alphaDiversityWithinSpecies +} diff --git a/man/gene.Rd b/man/gene.Rd new file mode 100644 index 0000000..04720ff --- /dev/null +++ b/man/gene.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/stana.R +\name{gene} +\alias{gene} +\title{gene} +\usage{ +gene(dfs) +} +\arguments{ +\item{dfs}{named list of dfs} +} +\description{ +gene +} diff --git a/man/snv.Rd b/man/snv.Rd new file mode 100644 index 0000000..36fa123 --- /dev/null +++ b/man/snv.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/stana.R +\name{snv} +\alias{snv} +\title{snv} +\usage{ +snv(dfs) +} +\arguments{ +\item{dfs}{named list of dfs} +} +\description{ +snv +}