Skip to content

Commit

Permalink
fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
evanbiederstedt committed Sep 26, 2022
1 parent a379ea2 commit fb81144
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 7 deletions.
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,7 @@ spcov <- function(m, cm) {
.Call('_conos_spcov', PACKAGE = 'conos', m, cm)
}

arma_mat_cor <- function(m) {
.Call('_conos_arma_mat_cor', PACKAGE = 'conos', m)
}

31 changes: 24 additions & 7 deletions R/integrations.R
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ p2app4conos <- function(conos, cdl=NULL, metadata=NULL, filename='conos_app.bin'
}



#' Filter genes by requiring minimum average expression within at least one of the provided cell clusters
#'
#' @param emat spliced (exonic) count matrix
Expand All @@ -539,6 +540,19 @@ filter.genes.by.cluster.expression <- function(emat, clusters, min.max.cluster.a
}



#' A slightly faster way of calculating column correlation matrix
#' @param mat matrix whose columns will be correlated
#' @param nthreads number of threads to use
#' @return correlation matrix
#' @keywords internal
armaCor <- function(mat,nthreads=1) {
cd <- arma_mat_cor(mat);
rownames(cd) <- colnames(cd) <- colnames(mat);
return(cd)
}


#' RNA velocity analysis on samples integrated with conos
#' Create a list of objects to pass into gene.relative.velocity.estimates function from the velocyto.R package
#'
Expand All @@ -548,6 +562,9 @@ filter.genes.by.cluster.expression <- function(emat, clusters, min.max.cluster.a
#' @param groups set of clusters to use (default=NULL). Ignored if 'clustering' is not NULL.
#' @param n.odgenes numeric Number of overdispersed genes to use for PCA (default=2000).
#' @param verbose boolean Whether to use verbose mode (default=TRUE)
#' @param min.max.cluster.average.emat Required minimum average expression count for emat, the spliced (exonic) count matrix (default=0.2). Note: no normalization is perfomed. See the parameter 'min.max.cluster.average' in the function 'filter.genes.by.cluster.expression.'
#' @param min.max.cluster.average.nmat Required minimum average expression count for nmat, the unspliced (nascent) count matrix (default=0.05). Note: no normalization is perfomed. See the parameter 'min.max.cluster.average' in the function 'filter.genes.by.cluster.expression.'
#' @param min.max.cluster.average.smat Required minimum average expression count for smat, the spanning read matrix (used in offset calculations) (default=0.01). Note: no normalization is perfomed. See the parameter 'min.max.cluster.average' in the function 'filter.genes.by.cluster.expression.'
#' @return List with cell distances, combined spliced expression matrix, combined unspliced expression matrix, combined matrix of spanning reads, cell colors for clusters and embedding (taken from conos)
#' @export
velocityInfoConos <- function(cms.list, con, clustering=NULL, groups=NULL, n.odgenes=2e3, verbose=TRUE, min.max.cluster.average.emat=0.2, min.max.cluster.average.nmat=0.05, min.max.cluster.average.smat=0.01) {
Expand All @@ -573,9 +590,9 @@ velocityInfoConos <- function(cms.list, con, clustering=NULL, groups=NULL, n.odg
cms.list <- lapply(cms.list, function(x) {lapply(x, function(y) {y[row.names(y) %in% common.genes,]} )} )

# Merge velocity files from different samples
emat <- do.call(cbind, lapply(cms.list, function(x) {x[[1]]}))
nmat <- do.call(cbind, lapply(cms.list, function(x) {x[[2]]}))
smat <- do.call(cbind, lapply(cms.list, function(x) {x[[3]]}))
emat <- do.call(cbind, lapply(cms.list, function(x) {x[[1]]})) ## emat - spliced (exonic) count matrix
nmat <- do.call(cbind, lapply(cms.list, function(x) {x[[2]]})) ## nmat - unspliced (nascent) count matrix
smat <- do.call(cbind, lapply(cms.list, function(x) {x[[3]]})) ## smat - optional spanning read matrix (used in offset calculations)

# Keep the order of cells consistent between velocity matrices and the embedding (not really sure whether it's necessary...)
emat <- emat[,order(match(colnames(emat), rownames(emb)))]
Expand All @@ -588,12 +605,12 @@ velocityInfoConos <- function(cms.list, con, clustering=NULL, groups=NULL, n.odg
# Again, keep the order of cells consistent
pcs <- pcs[order(match(rownames(pcs), rownames(emb))),]
# Calculate the cell distances based on correlation
cell.dist <- as.dist(1 - velocyto.R::armaCor(t(pcs)))
cell.dist <- as.dist(1 - armaCor(t(pcs)))

if (verbose) message("Filtering velocity...\n")
emat %<>% velocyto.R::filter.genes.by.cluster.expression(groups, min.max.cluster.average=min.max.cluster.average.emat)
nmat %<>% velocyto.R::filter.genes.by.cluster.expression(groups, min.max.cluster.average=min.max.cluster.average.nmat)
smat %<>% velocyto.R::filter.genes.by.cluster.expression(groups, min.max.cluster.average=min.max.cluster.average.smat)
emat %<>% filter.genes.by.cluster.expression(groups, min.max.cluster.average=min.max.cluster.average.emat)
nmat %<>% filter.genes.by.cluster.expression(groups, min.max.cluster.average=min.max.cluster.average.nmat)
smat %<>% filter.genes.by.cluster.expression(groups, min.max.cluster.average=min.max.cluster.average.smat)

if (verbose) message("All Done!")
return(list(cell.dist=cell.dist, emat=emat, nmat=nmat, smat=smat, cell.colors=cell.colors, emb=emb))
Expand Down
20 changes: 20 additions & 0 deletions man/armaCor.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions man/velocityInfoConos.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#include <RcppArmadillo.h>
#define NDEBUG 1
#include <RcppEigen.h>
#include <Rcpp.h>

Expand Down Expand Up @@ -333,6 +334,17 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// arma_mat_cor
arma::mat arma_mat_cor(const arma::mat& m);
RcppExport SEXP _conos_arma_mat_cor(SEXP mSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< const arma::mat& >::type m(mSEXP);
rcpp_result_gen = Rcpp::wrap(arma_mat_cor(m));
return rcpp_result_gen;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"_conos_RjnmfC", (DL_FUNC) &_conos_RjnmfC, 8},
Expand All @@ -356,6 +368,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_conos_adjacent_vertices", (DL_FUNC) &_conos_adjacent_vertices, 1},
{"_conos_adjacent_vertex_weights", (DL_FUNC) &_conos_adjacent_vertex_weights, 2},
{"_conos_spcov", (DL_FUNC) &_conos_spcov, 2},
{"_conos_arma_mat_cor", (DL_FUNC) &_conos_arma_mat_cor, 1},
{NULL, NULL, 0}
};

Expand Down
17 changes: 17 additions & 0 deletions src/spcov.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@


// [[Rcpp::depends(RcppEigen)]]
// [[Rcpp::depends(RcppArmadillo)]]


#include <RcppArmadillo.h>
#define NDEBUG 1
#include <RcppEigen.h>
#include <Rcpp.h>


// [[Rcpp::export]]
Eigen::MatrixXd spcov(const Eigen::SparseMatrix<double>& m,Eigen::VectorXd cm) {
Eigen::MatrixXd v=m.transpose()*m;
v-=(cm* cm.transpose())*((double)m.rows());
v/=((double) m.rows()-1);
return(v);
}


// quick matrix correlation function
// [[Rcpp::export]]
arma::mat arma_mat_cor(const arma::mat& m) {
return(cor(m));
}

0 comments on commit fb81144

Please sign in to comment.