Skip to content

Commit

Permalink
version 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tpq authored and cran-robot committed Jul 6, 2016
0 parents commit 87c5062
Show file tree
Hide file tree
Showing 32 changed files with 2,457 additions and 0 deletions.
35 changes: 35 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Package: propr
Title: Calculating Proportionality Between Vectors of Compositional
Data
Version: 1.1.0
URL: http://github.com/tpq/propr
BugReports: http://github.com/tpq/propr/issues
Authors@R: c(
person("Thomas", "Quinn", email = "contacttomquinn@gmail.com", role = c("aut", "cre")),
person("David", "Lovell", email = "david.lovell@qut.edu.au", role = "aut")
)
Description: The bioinformatic evaluation of gene co-expression often begins with
correlation-based analyses. However, this approach lacks statistical validity
when applied to relative data, including those biological count data produced by
microarray assays or high-throughput RNA-sequencing. As an alternative, Lovell
et al propose a proportionality metric, phi, derived from compositional data
analysis, a branch of math dealing specifically with relative data. In a
subsequent publication, Erb and Nicodemus expounded these efforts by elaborating
on another proportionality metric, rho. This package introduces a programmatic
framework for the calculation of feature dependence using proportionality and
other compositional data methods discussed in the cited publications.
License: GPL-2
LazyData: true
VignetteBuilder: knitr
RoxygenNote: 5.0.1
Depends: R (>= 3.2.2)
Imports: methods
Suggests: compositions, dendextend, ggplot2, ggthemes, knitr,
rmarkdown, testthat
NeedsCompilation: no
Packaged: 2016-07-06 00:08:59 UTC; thom
Author: Thomas Quinn [aut, cre],
David Lovell [aut]
Maintainer: Thomas Quinn <contacttomquinn@gmail.com>
Repository: CRAN
Date/Publication: 2016-07-06 09:44:00
31 changes: 31 additions & 0 deletions MD5
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
455c771cf8481b26401d90907353a3aa *DESCRIPTION
156ec54e98b9e35ed47a671d223edf1b *NAMESPACE
d42464af95b40021761560662d6f709d *NEWS.md
202017784e399813d15f595fff4440e4 *R/propr-backend.R
a50b04d7046c0b703e4e7098de5f3ddf *R/propr-classes.R
9c4cd2f34759be4bea23c8eed7d7bb7f *R/propr-methods.R
e71cf145895d640e635811964126b17c *R/propr-perb.R
628ec584b94b4b595b5f3b7c9aae07ba *R/propr-phit.R
eb65f255609f369fa209912ea0065afb *README.md
79086a6786543eda37d50ee59b2d9da5 *build/vignette.rds
7e8b32c5ecd2d10e82aed71f928be93a *inst/doc/propr-vignette.R
d4bfa5c80c697c188330a0c205b86d13 *inst/doc/propr-vignette.Rmd
2cdfc1f722d89face3c159481581484a *inst/doc/propr-vignette.html
3a1924d0443f34ddfc7e8a051dea4b37 *man/perb.Rd
e82dfb7b8d74048aa755b946858288ff *man/phit.Rd
c1f20b8969faf5bbd5a0399e54ab615d *man/propr-class.Rd
d5ccc56298a3b1d5e04968cfc4d8d74e *man/propr.Rd
0fe59d89c9611cf4301cbc8f43cb2fa3 *man/proprALR.Rd
b3c307949b6b75260172ed7ef0997fdd *man/proprCLR.Rd
f3ca7b8b03b6b44ee558ae5c00010048 *man/proprPairs.Rd
2287bdb852156bbe3d98830c561dc115 *man/proprPerb.Rd
18fb6daf85bb1e408a53dde4b679a392 *man/proprPhit.Rd
6082a9b422e214509bfac10bd6f93a2c *man/proprSym.Rd
d2e912419a153a83fb9753300bfd7d9d *man/proprTri.Rd
d3b180f9c085eb5ee6cb51db23dbe723 *man/proprVLR.Rd
38a416384180a93d31020bdffb37ccee *tests/testthat.R
fc05d9b4d260a59e5ee9868520801bd6 *tests/testthat/test-logratio.R
18bea904004f38c52e8236832c08f21a *tests/testthat/test-perb.R
fb995286047319340ea951b7fb50b49a *tests/testthat/test-phit.R
23378d943d79a9bf3fb14ed100337e57 *tests/testthat/test-vlr.R
d4bfa5c80c697c188330a0c205b86d13 *vignettes/propr-vignette.Rmd
21 changes: 21 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by roxygen2: do not edit by hand

export(dendrogram)
export(perb)
export(phit)
exportClasses(propr)
exportMethods("$")
exportMethods("[")
exportMethods(image)
exportMethods(plot)
exportMethods(show)
exportMethods(subset)
importFrom(grDevices,rainbow)
importFrom(methods,new)
importFrom(methods,show)
importFrom(stats,as.dendrogram)
importFrom(stats,as.dist)
importFrom(stats,ecdf)
importFrom(stats,hclust)
importFrom(stats,order.dendrogram)
importFrom(stats,p.adjust)
27 changes: 27 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## propr 1.1.0
---------------------
* New orientation expected for input data
* Updated back-end and vignette accordingly
* Removed redundant transpositions
* Fixed rare subsetting errors
* Tweaked plot methods

## propr 1.0.0
---------------------
* Introduced `phit` function
* Implements Lovell's \phi proportionality metric
* Returns object of class `propr`
* Introduced `perb` function
* Implements Erb's \rho proportionality metric
* Returns object of class `propr`
* Introduced `propr` Class
* `show` method
* Subsets `propr` based on `@pairs` slot
* `subset` method
* Subsets `propr` based on `@matrix` slot
* `plot` method
* Plots pairwise *lr proportionality
* `dendrogram` method
* Plots clusters of *lr-transformed data
* `image` method
* Plots heatmap of *lr-transformed data
213 changes: 213 additions & 0 deletions R/propr-backend.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
#' Calculate proportionality metric phi (Lovell 2015).
#'
#' Provided for backend use.
#'
#' @inheritParams phit
#' @return Returns proportionality matrix.
proprPhit <- function(counts, symmetrize = TRUE){

# Replace zeroes with next smallest number
counts[counts == 0] <- unique(sort(as.matrix(counts)))[2]

# Calculate the variance of the log-ratio ("variation array")
counts.vlr <- proprVLR(counts)
colnames(counts.vlr) <- colnames(counts)
rownames(counts.vlr) <- colnames(counts)

# Calculate feature variance across clr transformed treatments
counts.clr <- proprCLR(counts)
counts.clr.var <- apply(counts.clr, 2, stats::var)

# Sweep out feature clr variance from the variation array
phi <- sweep(counts.vlr, 2, counts.clr.var, FUN = "/")

# Symmetrize matrix if symmetrize = TRUE
if(symmetrize) phi <- proprSym(phi)

return(phi)
}

#' Calculate proportionality metric rho (Erb 2016).
#'
#' Provided for backend use.
#'
#' @inheritParams phit
#' @inheritParams perb
#' @return Returns proportionality matrix.
proprPerb <- function(counts, ivar = 0){

# Replace zeroes with next smallest number
counts[counts == 0] <- unique(sort(as.matrix(counts)))[2]

# Calculate the variance of the log-ratio ("variation array")
counts.vlr <- proprVLR(counts)
colnames(counts.vlr) <- colnames(counts)
rownames(counts.vlr) <- colnames(counts)

if(ivar != 0){

# Calculate feature variance across alr transformed treatments
counts.vlr <- counts.vlr[-ivar, -ivar] # returns one less dimension
counts.alr <- proprALR(counts, ivar = ivar) # returns one less dimension
counts.var <- apply(counts.alr, 2, stats::var)

}else{

# Calculate feature variance across clr transformed treatments
counts.clr <- proprCLR(counts)
counts.var <- apply(counts.clr, 2, stats::var)
}

# Divide variation array by sum of feature variances
for(i in 1:ncol(counts.vlr)){
for(j in 1:nrow(counts.vlr)){
counts.vlr[i, j] <- counts.vlr[i, j] / (counts.var[i] + counts.var[j])
}
}

# Calculate: p = 1 - (var(x - y))/(var(x) + var(y))
rho <- 1 - counts.vlr

return(rho)
}

#' Calculates the variance of the log of the ratios.
#'
#' Provided for backend use.
#'
#' @param X A data.frame or matrix. A "count matrix" with subjects as rows and features as columns.
#' @param check A logical. If TRUE, function first checks for negative and NA values.
#' @return Returns a matrix containing the variance of the log of the ratios.
proprVLR <- function(X, check = FALSE){

if(check){

if(any(X < 0)) stop("negative values found")
if(any(is.na(X))) stop("NA values found")
}

logX <- log(X)
Cov <- stats::var(logX) ## Note the avoidance of compositions::var
D <- ncol(logX)
VarCol <- matrix(rep(diag(Cov), D), ncol = D)
return(-2 * Cov + VarCol + t(VarCol))
}

#' Calculates the centered log-ratio transformation.
#'
#' Provided for backend use.
#'
#' @inheritParams proprVLR
#' @return A matrix. Returns the centered log-ratio transformation of \code{X}.
proprCLR <- function(X, check = FALSE){

if(check){

if(any(X < 0)) stop("negative values found")
if(any(is.na(X))) stop("NA values found")
}

logX <- log(X)
return(sweep(logX, 1, rowMeans(logX), "-")) # subtract out the means
}

#' Calculates the additive log-ratio transformation.
#'
#' Provided for backend use.
#'
#' @param ivar A numeric scalar. Specificies feature to use as reference for additive log-ratio transformation.
#' @inheritParams proprVLR
#' @return A matrix. Returns the additive log-ratio transformation of \code{X}.
proprALR <- function(X, ivar, check = FALSE){

if(check){

if(any(X < 0)) stop("negative values found")
if(any(is.na(X))) stop("NA values found")
}

logX <- log(X[, -ivar])
return(sweep(logX, 1, log(X[, ivar]), "-")) # subtract out the ivar
}

#' Recasts proportionality matrix as a table of feature pairs.
#'
#' Provided for backend use.
#'
#' @param prop A data.frame or matrix. A proportionality matrix.
#' @return A data.frame. Returns a table of feature pairs.
proprPairs <- function(prop){

if(identical(dim(prop), as.integer(c(1, 1)))){

return(data.frame())
}

index.i <- vector("numeric", length = (nrow(prop) - 1)*nrow(prop)/2)
index.j <- vector("numeric", length = (nrow(prop) - 1)*nrow(prop)/2)
index.prop <- vector("numeric", length = (nrow(prop) - 1)*nrow(prop)/2)
counter <- 1

for(j in 2:nrow(prop)){

for(i in 1:(j-1)){

index.i[counter] <- i
index.j[counter] <- j
index.prop[counter] <- prop[j, i]
counter <- counter + 1
}
}

result <- data.frame("feature1" = rownames(prop)[index.i],
"feature2" = rownames(prop)[index.j],
"prop" = index.prop,
stringsAsFactors = FALSE)

final <- result[order(result$prop), ]
rownames(final) <- 1:nrow(final)

return(final)
}

#' Retrieve the lower left triangle of a proportionality matrix.
#'
#' Provided for backend use.
#'
#' @inheritParams proprPairs
#' @return A vector. Returns the lower left triangle of a proportionality matrix.
proprTri <- function(prop){

result <- vector("numeric", length = (nrow(prop) - 1)*nrow(prop)/2)
counter <- 1

for(j in 2:nrow(prop)){

for(i in 1:(j-1)){

result[counter] <- prop[j, i]
counter <- counter + 1
}
}

return(result)
}

#' Symmetrizes a proportionality matrix.
#'
#' Provided for backend use.
#'
#' @inheritParams proprPairs
#' @return A matrix. Returns a symmetrized proportionality matrix.
proprSym <- function(prop){

for(j in 2:nrow(prop)){

for(i in 1:(j-1)){

prop[i, j] <- prop[j, i]
}
}

return(prop)
}
46 changes: 46 additions & 0 deletions R/propr-classes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#' The propr Package
#'
#' @description
#' Welcome to the \code{propr} package!
#'
#' To learn more about how to calculate metrics of proportionality,
#' see the help file for the method definitions \code{\link{phit}}
#' and \code{\link{perb}}.
#'
#' To learn more about the resultant \code{propr} class object, see
#' the help file for the class definition \code{\link{propr-class}}.
#'
#' To learn more about compositional data analysis, and its relevance
#' to biological count data, see the attached vignette.
#'
#' To learn more about \code{propr} class methods, see below.
#'
#' @name propr
NULL

#' An S4 class to hold results from proportionality analysis.
#'
#' @slot counts A data.frame. Stores the original "count matrix" input.
#' @slot logratio A data.frame. Stores the log-ratio transformed "count matrix".
#' @slot matrix A matrix. Stores the proportionality matrix calculated by \code{phit} or \code{perb}.
#' @slot pairs A data.frame. Projects the proportionality matrix pairwise.
#'
#' @seealso \code{\link{propr}}, \code{\link{phit}}, \code{\link{perb}}
#'
#' @examples
#' randomNum <- sample(1:1000, size = 25 * 10, replace = TRUE)
#' counts <- matrix(randomNum, nrow = 25, ncol = 10)
#' prop <- perb(counts, ivar = 0, iter = 0)
#' prop[1:5, ]
#' prop$prop
#' prop[1:5, "prop"]
#' subset(prop, 1:5)
#' @export
setClass("propr",
slots = c(
counts = "data.frame",
logratio = "data.frame",
matrix = "matrix",
pairs = "data.frame"
)
)

0 comments on commit 87c5062

Please sign in to comment.