Skip to content

Commit 6d56960

Browse files
committed
Merge branch 'net_norm' into devel
2 parents 55e4bdf + 0f600f7 commit 6d56960

File tree

6 files changed

+62
-1
lines changed

6 files changed

+62
-1
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ VignetteBuilder: knitr
1616
License: GPL (>= 2)
1717
BugReports: https://github.com/wulingyun/Corbi/issues
1818
URL: https://github.com/wulingyun/Corbi
19-
RoxygenNote: 7.0.2
19+
RoxygenNote: 7.1.0
2020
Encoding: UTF-8

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export(get_adjusted_deg_diff)
77
export(get_diff_ratio_net)
88
export(get_ratio_distribution)
99
export(get_ratio_distribution2)
10+
export(get_ratio_variance)
1011
export(get_shortest_distances)
1112
export(get_subnets)
1213
export(kappa_score)

R/deg_normalization.R

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
4+
#' @export
5+
get_ratio_variance <- function(expr.matrix, log.expr = FALSE)
6+
{
7+
if (!log.expr) expr.matrix <- log(expr.matrix)
8+
.Call(ND_RatioVariance, expr.matrix)
9+
}

src/Corbi.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const R_CallMethodDef callMethods[] = {
1313
{"ND_RatioDistributionParM", (DL_FUNC) &ND_RatioDistributionParM, 2},
1414
{"ND_RatioDistribution2", (DL_FUNC) &ND_RatioDistribution2, 3},
1515
{"ND_DiffRatioNet", (DL_FUNC) &ND_DiffRatioNet, 2},
16+
{"ND_RatioVariance", (DL_FUNC) &ND_RatioVariance, 1},
1617
{NULL, NULL, 0}
1718
};
1819

src/Corbi.h

+1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ extern "C" {
3030
SEXP ND_RatioDistributionParM(SEXP _DistI, SEXP _nGenes);
3131
SEXP ND_RatioDistribution2(SEXP _LogExprMatrix, SEXP _pEdge, SEXP _pTrim);
3232
SEXP ND_DiffRatioNet(SEXP _RatioLB, SEXP _LogExprVal);
33+
SEXP ND_RatioVariance(SEXP _LogExprMatrix);
3334
}

src/NetDEG.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,52 @@ SEXP ND_DiffRatioNet(SEXP _RatioLB, SEXP _LogExprVal)
310310
UNPROTECT(5);
311311
return(_M);
312312
}
313+
314+
SEXP ND_RatioVariance(SEXP _LogExprMatrix)
315+
{
316+
PROTECT(_LogExprMatrix = AS_NUMERIC(_LogExprMatrix));
317+
double *LogExprMatrix = NUMERIC_POINTER(_LogExprMatrix);
318+
int *dim = INTEGER_POINTER(AS_INTEGER(GET_DIM(_LogExprMatrix)));
319+
int nGenes = dim[0];
320+
int nSamples = dim[1];
321+
322+
SEXP _Var;
323+
PROTECT(_Var = NEW_NUMERIC(nGenes * nGenes));
324+
SetDim2(_Var, nGenes, nGenes);
325+
double *Var = NUMERIC_POINTER(_Var);
326+
SetValues(_Var, Var, 0.0);
327+
328+
double *r = (double *) R_alloc(nSamples, sizeof(double));
329+
double *e, m, v;
330+
int n;
331+
for (int i = 0; i < nGenes-1; i++)
332+
{
333+
for (int j = i+1; j < nGenes; j++)
334+
{
335+
e = LogExprMatrix;
336+
n = 0;
337+
m = v = 0;
338+
for (int k = 0; k < nSamples; k++)
339+
{
340+
if (R_finite(e[i]) && R_finite(e[j]))
341+
{
342+
r[n] = e[i] - e[j];
343+
m += r[n];
344+
v += r[n] * r[n];
345+
n++;
346+
}
347+
e += nGenes;
348+
}
349+
350+
if (n > 0)
351+
{
352+
v = (v - m * m / n) / (n - 1);
353+
Var[i+nGenes*j] = v;
354+
Var[j+nGenes*i] = v;
355+
}
356+
}
357+
}
358+
359+
UNPROTECT(2);
360+
return(_Var);
361+
}

0 commit comments

Comments
 (0)