diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b6a065 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata diff --git a/DESCRIPTION b/DESCRIPTION new file mode 100644 index 0000000..09d2f5f --- /dev/null +++ b/DESCRIPTION @@ -0,0 +1,27 @@ +Package: microsig +Type: Package +Title: Calculates Microbe Signature Score +Version: 0.0.0.9000 +Authors@R: + c(person(given = "Caroline", + family = "Wheeler", + role = c("aut"), + comment = c(ORCID = "0000-0002-3374-4909")), + person(given = "Rebecca", + family = "Hoyd", + role = c("aut"), + comment = c(ORCID = "0000-0003-1210-4491")), + person(given = "Daniel", + family = "Spakowicz", + role = c("aut", "cre"), + email = "daniel.spakowicz@osumc.edu", + comment = c(ORCID = "0000-0003-2314-6435")) +Maintainer: Daniel Spakowicz +Description: This package contains functions to calculate microbe signatures, particularly from bugsigdb inputs. +License: MIT +Encoding: UTF-8 +Roxygen: list(markdown = TRUE) +RoxygenNote: 7.2.3 +Imports: + dplyr, + tidyr diff --git a/NAMESPACE b/NAMESPACE new file mode 100644 index 0000000..d0bd7d7 --- /dev/null +++ b/NAMESPACE @@ -0,0 +1,7 @@ +# Generated by roxygen2: do not edit by hand + +export(calcMicroSigScore) +export(getSigList) +export(microbeEntries) +import(dplyr) +import(tidyr) diff --git a/R/calcMicroSigScore.R b/R/calcMicroSigScore.R new file mode 100644 index 0000000..c7142a3 --- /dev/null +++ b/R/calcMicroSigScore.R @@ -0,0 +1,58 @@ +#' Function to calculate microbe signature scores given a set of up and down +#' regulated microbes and the relative abundances of microbe per sample +#' +#' @param taxa a data frame where columns are microbes and rows are samples, first +#' column named 'sample' +#' @param sig.up a character vector listing microbes in given signature that are +#' up-regulated, empty if none +#' @param sig.down a character vector listing microbes in given signature that are +#' down-regulated, empty if none +#' +#' @return a data frame with samples and corresponding microbe signature scores +#' +#' @import dplyr +#' @import tidyr +#' +#' @export +calcMicroSigScore <- function(taxa, sig.up, sig.down){ + df <- taxa %>% + tidyr::gather(microbe, exo.ra, -sample) %>% + dplyr::group_by(sample) %>% + dplyr::mutate(z_score = scale(exo.ra)) + + upMics <- df %>% + dplyr::filter(microbe %in% sig.up) + + upTotal <- length(unique(upMics$microbe)) + + upMics <- upMics %>% + dplyr::group_by(sample) %>% + dplyr::summarize(tempUp = sum(exo.ra)) + + downMics <- df %>% + dplyr::filter(microbe %in% sig.down) + + downTotal <- length(unique(downMics$microbe)) + + downMics <- downMics %>% + dplyr::group_by(sample) %>% + dplyr::summarize(tempDown = sum(exo.ra)) + + if(upTotal == 0 && downTotal == 0){ + df <- data.frame(matrix(ncol = 0, nrow = 0)) + }else if(upTotal == 0){ + df <- downMics %>% + dplyr::mutate(score = tempDown*(1/downTotal)) %>% + dplyr::select(sample,score) + }else if(downTotal == 0){ + df <- upMics %>% + dplyr::mutate(score = tempUp*(1/upTotal)) %>% + dplyr::select(sample, score) + }else{ + df <- merge(upMics, downMics, by = "sample") %>% + dplyr::mutate(score = (tempUp - tempDown)*(1/(upTotal + downTotal))) %>% + dplyr::select(sample, score) + } + + return(df) +} diff --git a/R/getSigList.R b/R/getSigList.R new file mode 100644 index 0000000..37a1e5d --- /dev/null +++ b/R/getSigList.R @@ -0,0 +1,14 @@ +#' Given a single cell value from bigsigDB output in the column 'MetaPhlAn.taxon.names', +#' returns a list of the microbes +#' +#' @param sig character string from bugsigDB output containing comma separated +#' Metaphlan taxonomic names +#' +#' @return sig.list , a character list of containing the string after the last +#' "|" in each comma separated Metaphlan taxonomic name +#' +#' @export +getSigList <- function(sig){ + sig.list <- sub("^.+\\|", "", unlist(strsplit(sig ,","))) + return(sig.list) +} diff --git a/R/microbeEntries.R b/R/microbeEntries.R new file mode 100644 index 0000000..384d9ba --- /dev/null +++ b/R/microbeEntries.R @@ -0,0 +1,32 @@ +#' Microbe Entries +#' A function that can be used to call of specific microbe signatures. +#' @param query Name of the signature group. If query is passed "all" function will return all the signature groups +#' +#' @return The microbes within a desired signature group in the format "x__microbe name" with x indicating taxonomic level +#' @export +#' +#' @examples microbeEntries("paa_up") +microbeEntries <- function(query){ + + paa_up <- c("s__Clostridium sp. UNK-MGS-6", "s__Firmicutes bacterium CAG 41", + "s__Coprobacillus cateniformis", "s__Clostridia bacterium UC5-1-2G4", + "s__Streptococcus dysgalactiae", "s__Acidaminococcus sp. HPA0509", + "s__Roseburia sp.CAG-303", "s__Butyrivibrio_sp. AE2032", + "s__Coprobacillus sp. 29-1", "s__Coprobacillus sp. D6") + paa_down <- c("s__Alistipes sp. CAG-435", "s__Fusobacterium hwasookii", + "s__Leuconostoc sp. DORA-2", "s__Paenibacillus lentimorbus", + "s__Fusobacterium sp. oral taxon 370", "s__Alcanivorax hongdengensis", + "s__Arthrobacter sp. LS16", "s__Bacillus sp. 5B6", "s__Bacillus velezensis", + "s__Fusobacterium sp. OBRC1") + + mic_list <- list(paa_up, paa_down) + + names(mic_list) <- c("paa_up", "paa_down") + + if(query == "all"){ + return(mic_list) + } + + mics_queried <- mic_list[[query]] + return(mics_queried) +} diff --git a/README.md b/README.md index 6b527ab..0799258 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # microsig -This package contains functions to calculate microbe signatures, particularly from bugsigdb inputs. +This package contains functions to calculate microbe signature scores, particularly from bugsigdb inputs. diff --git a/man/calcMicroSigScore.Rd b/man/calcMicroSigScore.Rd new file mode 100644 index 0000000..cedfaed --- /dev/null +++ b/man/calcMicroSigScore.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/calcMicroSigScore.R +\name{calcMicroSigScore} +\alias{calcMicroSigScore} +\title{Function to calculate microbe signature scores given a set of up and down +regulated microbes and the relative abundances of microbe per sample} +\usage{ +calcMicroSigScore(taxa, sig.up, sig.down) +} +\arguments{ +\item{taxa}{a data frame where columns are microbes and rows are samples, first +column named 'sample'} + +\item{sig.up}{a character vector listing microbes in given signature that are +up-regulated, empty if none} + +\item{sig.down}{a character vector listing microbes in given signature that are +down-regulated, empty if none} +} +\value{ +a data frame with samples and corresponding microbe signature scores +} +\description{ +Function to calculate microbe signature scores given a set of up and down +regulated microbes and the relative abundances of microbe per sample +} diff --git a/man/getSigList.Rd b/man/getSigList.Rd new file mode 100644 index 0000000..fdedaa2 --- /dev/null +++ b/man/getSigList.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/getSigList.R +\name{getSigList} +\alias{getSigList} +\title{Given a single cell value from bigsigDB output in the column 'MetaPhlAn.taxon.names', +returns a list of the microbes} +\usage{ +getSigList(sig) +} +\arguments{ +\item{sig}{character string from bugsigDB output containing comma separated +Metaphlan taxonomic names} +} +\value{ +sig.list , a character list of containing the string after the last +"|" in each comma separated Metaphlan taxonomic name +} +\description{ +Given a single cell value from bigsigDB output in the column 'MetaPhlAn.taxon.names', +returns a list of the microbes +} diff --git a/man/microbeEntries.Rd b/man/microbeEntries.Rd new file mode 100644 index 0000000..d7ff5bf --- /dev/null +++ b/man/microbeEntries.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/microbeEntries.R +\name{microbeEntries} +\alias{microbeEntries} +\title{Microbe Entries +A function that can be used to call of specific microbe signatures.} +\usage{ +microbeEntries(query) +} +\arguments{ +\item{query}{Name of the signature group. If query is passed "all" function will return all the signature groups} +} +\value{ +The microbes within a desired signature group in the format "x__microbe name" with x indicating taxonomic level +} +\description{ +Microbe Entries +A function that can be used to call of specific microbe signatures. +} +\examples{ +microbeEntries("paa_up") +} diff --git a/microsig.Rproj b/microsig.Rproj new file mode 100644 index 0000000..e83436a --- /dev/null +++ b/microsig.Rproj @@ -0,0 +1,16 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX + +AutoAppendNewline: Yes +StripTrailingWhitespace: Yes