Skip to content

Commit 0f600f7

Browse files
committed
Add function get_ratio_variance
1 parent 32b771a commit 0f600f7

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

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
@@ -11,6 +11,7 @@ const R_CallMethodDef callMethods[] = {
1111
{"ND_RatioDistribution", (DL_FUNC) &ND_RatioDistribution, 2},
1212
{"ND_RatioDistribution2", (DL_FUNC) &ND_RatioDistribution2, 3},
1313
{"ND_DiffRatioNet", (DL_FUNC) &ND_DiffRatioNet, 2},
14+
{"ND_RatioVariance", (DL_FUNC) &ND_RatioVariance, 1},
1415
{NULL, NULL, 0}
1516
};
1617

src/Corbi.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ extern "C" {
2626
SEXP ND_RatioDistribution(SEXP _LogExprMatrix, SEXP _pEdge);
2727
SEXP ND_RatioDistribution2(SEXP _LogExprMatrix, SEXP _pEdge, SEXP _pTrim);
2828
SEXP ND_DiffRatioNet(SEXP _RatioLB, SEXP _LogExprVal);
29+
SEXP ND_RatioVariance(SEXP _LogExprMatrix);
2930
}

src/NetDEG.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,52 @@ SEXP ND_DiffRatioNet(SEXP _RatioLB, SEXP _LogExprVal)
203203
UNPROTECT(5);
204204
return(_M);
205205
}
206+
207+
SEXP ND_RatioVariance(SEXP _LogExprMatrix)
208+
{
209+
PROTECT(_LogExprMatrix = AS_NUMERIC(_LogExprMatrix));
210+
double *LogExprMatrix = NUMERIC_POINTER(_LogExprMatrix);
211+
int *dim = INTEGER_POINTER(AS_INTEGER(GET_DIM(_LogExprMatrix)));
212+
int nGenes = dim[0];
213+
int nSamples = dim[1];
214+
215+
SEXP _Var;
216+
PROTECT(_Var = NEW_NUMERIC(nGenes * nGenes));
217+
SetDim2(_Var, nGenes, nGenes);
218+
double *Var = NUMERIC_POINTER(_Var);
219+
SetValues(_Var, Var, 0.0);
220+
221+
double *r = (double *) R_alloc(nSamples, sizeof(double));
222+
double *e, m, v;
223+
int n;
224+
for (int i = 0; i < nGenes-1; i++)
225+
{
226+
for (int j = i+1; j < nGenes; j++)
227+
{
228+
e = LogExprMatrix;
229+
n = 0;
230+
m = v = 0;
231+
for (int k = 0; k < nSamples; k++)
232+
{
233+
if (R_finite(e[i]) && R_finite(e[j]))
234+
{
235+
r[n] = e[i] - e[j];
236+
m += r[n];
237+
v += r[n] * r[n];
238+
n++;
239+
}
240+
e += nGenes;
241+
}
242+
243+
if (n > 0)
244+
{
245+
v = (v - m * m / n) / (n - 1);
246+
Var[i+nGenes*j] = v;
247+
Var[j+nGenes*i] = v;
248+
}
249+
}
250+
}
251+
252+
UNPROTECT(2);
253+
return(_Var);
254+
}

0 commit comments

Comments
 (0)