annotate MatrixEQTL/demo/sample.cis.r @ 0:cd4c8e4a4b5b draft

Uploaded
author jasonxu
date Fri, 12 Mar 2021 08:12:46 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
1 # Matrix eQTL by Andrey A. Shabalin
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
2 # http://www.bios.unc.edu/research/genomic_software/Matrix_eQTL/
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
3 #
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
4 # Be sure to use an up to date version of R and Matrix eQTL.
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
5
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
6 # source("Matrix_eQTL_R/Matrix_eQTL_engine.r");
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
7 library(MatrixEQTL)
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
8
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
9 ## Location of the package with the data files.
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
10 base.dir = find.package('MatrixEQTL');
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
11 # base.dir = '.';
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
12
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
13 ## Settings
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
14
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
15 # Linear model to use, modelANOVA, modelLINEAR, or modelLINEAR_CROSS
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
16 useModel = modelLINEAR; # modelANOVA, modelLINEAR, or modelLINEAR_CROSS
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
17
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
18 # Genotype file name
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
19 SNP_file_name = paste(base.dir, "/data/SNP.txt", sep="");
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
20 snps_location_file_name = paste(base.dir, "/data/snpsloc.txt", sep="");
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
21
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
22 # Gene expression file name
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
23 expression_file_name = paste(base.dir, "/data/GE.txt", sep="");
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
24 gene_location_file_name = paste(base.dir, "/data/geneloc.txt", sep="");
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
25
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
26 # Covariates file name
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
27 # Set to character() for no covariates
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
28 covariates_file_name = paste(base.dir, "/data/Covariates.txt", sep="");
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
29
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
30 # Output file name
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
31 output_file_name_cis = tempfile();
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
32 output_file_name_tra = tempfile();
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
33
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
34 # Only associations significant at this level will be saved
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
35 pvOutputThreshold_cis = 2e-2;
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
36 pvOutputThreshold_tra = 1e-2;
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
37
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
38 # Error covariance matrix
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
39 # Set to numeric() for identity.
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
40 errorCovariance = numeric();
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
41 # errorCovariance = read.table("Sample_Data/errorCovariance.txt");
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
42
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
43 # Distance for local gene-SNP pairs
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
44 cisDist = 1e6;
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
45
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
46 ## Load genotype data
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
47
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
48 snps = SlicedData$new();
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
49 snps$fileDelimiter = "\t"; # the TAB character
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
50 snps$fileOmitCharacters = "NA"; # denote missing values;
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
51 snps$fileSkipRows = 1; # one row of column labels
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
52 snps$fileSkipColumns = 1; # one column of row labels
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
53 snps$fileSliceSize = 2000; # read file in slices of 2,000 rows
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
54 snps$LoadFile(SNP_file_name);
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
55
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
56 ## Load gene expression data
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
57
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
58 gene = SlicedData$new();
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
59 gene$fileDelimiter = "\t"; # the TAB character
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
60 gene$fileOmitCharacters = "NA"; # denote missing values;
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
61 gene$fileSkipRows = 1; # one row of column labels
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
62 gene$fileSkipColumns = 1; # one column of row labels
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
63 gene$fileSliceSize = 2000; # read file in slices of 2,000 rows
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
64 gene$LoadFile(expression_file_name);
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
65
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
66 ## Load covariates
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
67
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
68 cvrt = SlicedData$new();
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
69 cvrt$fileDelimiter = "\t"; # the TAB character
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
70 cvrt$fileOmitCharacters = "NA"; # denote missing values;
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
71 cvrt$fileSkipRows = 1; # one row of column labels
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
72 cvrt$fileSkipColumns = 1; # one column of row labels
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
73 if(length(covariates_file_name)>0) {
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
74 cvrt$LoadFile(covariates_file_name);
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
75 }
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
76
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
77 ## Run the analysis
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
78 snpspos = read.table(snps_location_file_name, header = TRUE, stringsAsFactors = FALSE);
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
79 genepos = read.table(gene_location_file_name, header = TRUE, stringsAsFactors = FALSE);
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
80
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
81 me = Matrix_eQTL_main(
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
82 snps = snps,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
83 gene = gene,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
84 cvrt = cvrt,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
85 output_file_name = output_file_name_tra,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
86 pvOutputThreshold = pvOutputThreshold_tra,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
87 useModel = useModel,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
88 errorCovariance = errorCovariance,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
89 verbose = TRUE,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
90 output_file_name.cis = output_file_name_cis,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
91 pvOutputThreshold.cis = pvOutputThreshold_cis,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
92 snpspos = snpspos,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
93 genepos = genepos,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
94 cisDist = cisDist,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
95 pvalue.hist = TRUE,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
96 min.pv.by.genesnp = TRUE,
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
97 noFDRsaveMemory = FALSE);
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
98
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
99 unlink(output_file_name_tra);
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
100 unlink(output_file_name_cis);
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
101
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
102 ## Results:
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
103
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
104 cat('Analysis done in: ', me$time.in.sec, ' seconds', '\n');
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
105 cat('Detected local eQTLs:', '\n');
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
106 show(me$cis$eqtls)
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
107 cat('Detected distant eQTLs:', '\n');
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
108 show(me$trans$eqtls)
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
109
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
110 ## Plot the histogram of local and distant p-values
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
111
cd4c8e4a4b5b Uploaded
jasonxu
parents:
diff changeset
112 plot(me)