Skip to content

Commit e6af172

Browse files
ppavlidisarteymix
authored andcommitted
more heavy test of eBayes
1 parent 717fb9f commit e6af172

4 files changed

Lines changed: 99 additions & 7 deletions

File tree

test/data/test-squeezevar.R

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# GSE112792; we're working with log2 transformed values so we skip the weighted regression steps
2+
3+
library(gemma.R)
4+
library(SummarizedExperiment)
5+
library(limma)
6+
library(dplyr)
7+
dat <- getDataset("GSE112792")
8+
9+
e = assay(dat)
10+
gt = factor(colData(dat)$genotype)
11+
12+
# some basic data filtering, not exactly the same as in Gemma but close
13+
e<-e[apply(e, 1, var) > 1e-5,]
14+
e<-e[apply(e, 1, function(x) {
15+
a<-length(unique(x))>5;
16+
}),]
17+
18+
19+
# test code for interpolation
20+
x = c(9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0)
21+
y = c(1.0, 0.25, 0.1111111111111111, 0.0625, 0.04, 0.027777777777777776, 0.02040816326530612, 0.015625, 0.012345679012345678, 0.01)
22+
interpolate = c(9.5, 8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5)
23+
f<-approxfun(x,y,rule=2)
24+
f(c(interpolate))
25+
26+
27+
28+
f<-lmFit(e, model.matrix(~gt))
29+
f<-eBayes(f)
30+
write.table(f$sigma^2, "/Users/pzoot/test-vars.txt", quote=F, sep="\t")
31+
limma::fitFDist(f$sigma^2, f$df.residual)
32+
33+
sv<-limma::squeezeVar(f$sigma^2, 6)
34+
write.table(sv$var.post, "/Users/pzoot/test-var.post.txt", quote=F, sep='\t')
35+
36+
# raw count data from RNA seq pipeline, not via gemma
37+
counts<-read.delim("./GSE112792_counts.genes", row.names=1, header=T)
38+
# not quite equivalent to Gemma filters
39+
counts<-counts[apply(counts,1,var)> 1e-5,]
40+
counts<-counts[apply(counts, 1, function(x) {
41+
a<-length(unique(x))>5;
42+
}),]
43+
44+
# fixme: sample and row order might not be right ...can work out from library sizes
45+
vv<-voom(as.matrix(counts), model.matrix(~gt), plot=T)
46+
gmv<-read.delim("mv-1885318782381326010.txt", header=F, sep='\t')
47+
glo<-read.delim("loess-fit-943568072532947709.txt", header=F, sep='\t')
48+
plot(gmv, pch='.', ylim=c(0,5))
49+
lines(glo, col='red')
50+
51+
td<-read.delim("lmtest11.dat.txt", header=T, row.names=1, sep='\t')
52+
tdes<-read.delim("lmtest11.des.txt", header=T, row.names=1, sep='\t')
53+
des<-model.matrix(~tdes$targets.TreatmentDHT);
54+
lib.size<-colSums(td)
55+
tdlogcpm<-t(log2(t(td+0.5)/(lib.size+1)*1e6))
56+
Amean<-rowMeans(tdlogcpm)
57+
sx <- Amean+mean(log2(lib.size+1))-log2(1e6)
58+
fit <- lmFit(tdlogcpm, des)
59+
sy <- sqrt(fit$sigma)
60+
l <- lowess(sx,sy,f=0.5)
61+
write.table(l, "lmtest11.lowess.txt", quote=F, sep='\t')
62+
write.table(tdlogcpm, "lmtest11.log2cpm.txt", quote=F, sep='\t')
63+
tv<-voom(as.matrix(td), des, plot=T)
64+
write.table(tv$weights, "lmtest11.voomweights.txt", sep='\t', quote=F)
65+
fitted.values <- fit$coef %*% t(fit$design)
66+
fitted.cpm <- 2^fitted.values
67+
fitted.count <- 1e-6 * t(t(fitted.cpm)*(lib.size+1))
68+
fitted.logcount <- log2(fitted.count)
69+
f <- approxfun(l, rule=2)
70+
w <- 1/f(fitted.logcount)^4
71+
dim(w) <- dim(fitted.logcount)
72+

test/data/test-var.post.gz

207 KB
Binary file not shown.

test/data/test-vars.gz

208 KB
Binary file not shown.

test/ubic/basecode/math/linearmodels/ModeratedTstatTest.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.junit.Assert.*;
2323

2424
import java.util.Map;
25+
import java.util.zip.GZIPInputStream;
2526

2627
import org.apache.commons.lang3.ArrayUtils;
2728
import org.junit.Test;
@@ -32,11 +33,6 @@
3233
import ubic.basecode.dataStructure.matrix.StringMatrix;
3334
import ubic.basecode.io.reader.DoubleMatrixReader;
3435
import ubic.basecode.io.reader.StringMatrixReader;
35-
import ubic.basecode.math.linearmodels.DesignMatrix;
36-
import ubic.basecode.math.linearmodels.GenericAnovaResult;
37-
import ubic.basecode.math.linearmodels.LeastSquaresFit;
38-
import ubic.basecode.math.linearmodels.LinearModelSummary;
39-
import ubic.basecode.math.linearmodels.ModeratedTstat;
4036
import ubic.basecode.util.RegressionTesting;
4137

4238
/**
@@ -146,7 +142,7 @@ public void testLimmaMissing() throws Exception {
146142
sums = fit.summarizeByKeys(true);
147143

148144
}
149-
145+
150146
@Test
151147
public void testFdist() {
152148
double[] x = new double[]{0.30232520254346584299,
@@ -255,7 +251,7 @@ public void testFdist() {
255251
DenseDoubleMatrix1D dfs = new DenseDoubleMatrix1D(x.length);
256252
dfs.assign(8);
257253
double[] expected = new double[]{1.1056298866365792399, 14.544682227013733922};
258-
// double[] actualold = ModeratedTstat.fitFDistNoMissing(new DenseDoubleMatrix1D(x), 8);
254+
// double[] actualold = ModeratedTstat.fitFDistNoMissing(new DenseDoubleMatrix1D(x), 8);
259255
double[] actual = ModeratedTstat.fitFDist(new DenseDoubleMatrix1D(x), dfs);
260256
assertTrue(RegressionTesting.closeEnough(actual, expected, 1e-10));
261257

@@ -357,5 +353,29 @@ public void testSqueezeVarWMissing() {
357353

358354
}
359355

356+
/**
357+
* Test based on GSE112792; see test-squeezevar.R
358+
*
359+
* @throws Exception
360+
*/
361+
@Test
362+
public void testSqueezeVarB() throws Exception {
363+
DoubleMatrixReader f = new DoubleMatrixReader();
364+
DoubleMatrix<String, String> testMatrix = f.read(new GZIPInputStream(this.getClass().getResourceAsStream(
365+
"/data/test-vars.gz")));
366+
DoubleMatrix1D vars = new DenseDoubleMatrix1D(testMatrix.getColumn(0));
367+
DoubleMatrix1D df1s = new DenseDoubleMatrix1D(vars.size());
368+
df1s.assign(a -> 6);
369+
double[] actual = ModeratedTstat.fitFDist(vars, df1s);
370+
double[] expected = new double[]{0.031699809959242764013,1.5383043501108832896};
371+
assertTrue(RegressionTesting.closeEnough(expected, actual, 1e-10));
372+
373+
DoubleMatrix1D squeezedActual = ModeratedTstat.squeezeVariances(vars, df1s, actual);
374+
375+
DoubleMatrix<String, String> testSqueezed = f.read(new GZIPInputStream(this.getClass().getResourceAsStream(
376+
"/data/test-var.post.gz")));
377+
378+
assertTrue(RegressionTesting.closeEnough(testSqueezed.getColumn(0), squeezedActual.toArray(), 1e-10));
379+
}
360380

361381
}

0 commit comments

Comments
 (0)