annotate MatrixEQTL/demo/b.cvrt.r @ 3:ae74f8fb3aef draft

Uploaded
author jasonxu
date Fri, 12 Mar 2021 08:20:57 +0000
parents cd4c8e4a4b5b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
1 library("MatrixEQTL");
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
2
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
3 # Number of columns (samples)
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
4 n = 100;
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
5
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
6 # Number of covariates
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
7 nc = 10;
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
8
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
9
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
10
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
11
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
12 # Generate the covariates
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
13 cvrt.mat = 2 + matrix(rnorm(n*nc), ncol = nc);
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
14
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
15 # Generate the vectors with genotype and expression variables
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
16 snps.mat = cvrt.mat %*% rnorm(nc) + rnorm(n);
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
17 gene.mat = cvrt.mat %*% rnorm(nc) + rnorm(n) + 0.5 * snps.mat + 1;
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
18
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
19 # Create 3 SlicedData objects for the analysis
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
20 snps1 = SlicedData$new( matrix( snps.mat, nrow = 1 ) );
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
21 gene1 = SlicedData$new( matrix( gene.mat, nrow = 1 ) );
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
22 cvrt1 = SlicedData$new( t(cvrt.mat) );
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
23
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
24 # Produce no output files
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
25 filename = NULL; # tempfile()
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
26
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
27 # Call the main analysis function
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
28 me = Matrix_eQTL_main(
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
29 snps = snps1,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
30 gene = gene1,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
31 cvrt = cvrt1,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
32 output_file_name = filename,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
33 pvOutputThreshold = 1,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
34 useModel = modelLINEAR,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
35 errorCovariance = numeric(),
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
36 verbose = TRUE,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
37 pvalue.hist = FALSE );
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
38
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
39 # Pull Matrix eQTL results - t-statistic and p-value
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
40 beta = me$all$eqtls$beta;
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
41 tstat = me$all$eqtls$statistic;
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
42 pvalue = me$all$eqtls$pvalue;
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
43 rez = c(beta = beta, tstat = tstat, pvalue = pvalue);
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
44 # And compare to those from the linear regression in R
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
45 {
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
46 cat("\n\n Matrix eQTL: \n");
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
47 print(rez);
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
48 cat("\n R summary(lm()) output: \n");
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
49 lmdl = lm( gene.mat ~ snps.mat + cvrt.mat );
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
50
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
51 lmout = summary(lmdl)$coefficients[2,c("Estimate","t value","Pr(>|t|)")];
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
52 print( lmout );
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
53 }
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
54
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
55 # Results from Matrix eQTL and "lm" must agree
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
56 stopifnot(all.equal(lmout, rez, check.attributes=FALSE));