-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathewas-annotate.R
executable file
·107 lines (95 loc) · 5.12 KB
/
ewas-annotate.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/local/bin/Rscript --vanilla
#' EWAS-fusion Annotator -- A tool for EWAS-fusion annotation
#' Usage: ewas-annotate.R <<EWAS-fusion working directory> [manifest_location=/at/different/location]
#' @author Alexia Cardona, \email{alexia.cardona@@mrc-epid.cam.ac.uk}
#' @date May 2017
infinium_humanmethylation450_beadchip_download <- function()
{
download.file("ftp://webdata2:webdata2@ussd-ftp.illumina.com/downloads/ProductFiles/HumanMethylation450/HumanMethylation450_15017482_v1-2.csv","HumanMethylation450_15017482_v1-2.csv")
download.file("https://support.illumina.com/content/dam/illumina-support/documents/myillumina/4214cc3b-e52e-4ab4-a4d7-b956f62ed208/450k_manifest_header_descriptions.xlsx","450k_manifest_header_descriptions.xlsx")
}
args <- commandArgs(trailingOnly = FALSE)
manifest_location <- dirname(sub("--file=", "", args[grep("--file", args)]))
args <- strsplit(commandArgs(TRUE), split='=')
keys <- vector("character")
if (length(args) > 0) for (i in 1:length(args)) {
key <- args[[i]][1]
value <- args[[i]][2]
keys <- c(keys, key)
if (exists(key)) assign(key, value)
}
args <- commandArgs(trailingOnly=TRUE)
prefix <- args[1]
prefix <- ifelse(substring(prefix,nchar(prefix)) != "/", paste0(prefix, "/"), prefix)
if(is.na(prefix)) {
cat("\n")
cat("Usage: ewas-annotate.R directory.name\n")
cat("where directory.name contains results from ewas-fusion.sh\n\n")
quit("yes")
}
# User messages
options(echo=FALSE)
cat("EWAS-fusion Annotator -- A tool for EWAS-fusion annotation
(based on Illumina Infinium HumanMethylation450 Manifest file version 1.2)\n
Alexia Carona, PhD\nEmail: alexia.cardona@@mrc-epid.cam.ac.uk\n
Annotating files in", prefix, "...\n\n")
# Manifest information
anno <- read.csv(paste0(manifest_location,"/HumanMethylation450_15017482_v1-2.csv"),as.is=TRUE, skip=7)
anno <- subset(anno, CHR!="X"&CHR!="Y")
anno <- within(anno, {CHR=as.numeric(CHR)})
# Results from all chromosomes
temp <- read.table(paste0(prefix, 6, ".dat.MHC"), as.is=TRUE, header=TRUE)
nMHC <- nrow(temp)
for(i in 1:22) temp <- rbind(temp, read.table(paste0(prefix, i, ".dat"), as.is=TRUE, header=TRUE))
library(reshape)
temp <- temp[setdiff(names(temp),c("FILE","CHR"))]
temp <- rename(temp, c("ID"="Name",
"EQTL.ID"="MEQTL.ID",
"EQTL.R2"="MEQTL.R2",
"EQTL.Z"="MEQTL.Z",
"EQTL.GWAS.Z"="MEQTL.GWAS.Z",
"TWAS.Z"="EWAS.Z",
"TWAS.P"="EWAS.P"))
# Annotation
temp <- within(temp, {MHC <- 0})
temp[1:nMHC, "MHC"] <- 1
annotated.data <- merge(temp, anno, by="Name")
N <- nrow(annotated.data)
# P-values, Bonferroni-corrected significant list and joint/conditional analysis
sorted.data <- annotated.data[with(annotated.data,order(EWAS.P)),]
write.csv(sorted.data, file=paste0(prefix, "annotatedSorted.csv"), quote=FALSE, row.names=FALSE)
cat(paste0("All annotation: ", prefix, "annotatedSorted.csv\n"))
sig.data <- subset(sorted.data, EWAS.P <= 0.05/N)
write.csv(sig.data, file=paste0(prefix, "annotatedSortedSignificant.csv"), quote=FALSE, row.names=FALSE)
cat(paste0("Bonferroni-corrected significant list: ", prefix, "annotatedSortedSignificant.csv\n"))
included <- dropped <- NULL
for (i in 1:22)
{
taji <- paste0(prefix, i, ".top.analysis.joint_included.dat")
if (file.exists(taji)) included <- rbind(included, read.table(taji, as.is=TRUE, header=TRUE))
tajd <- paste0(prefix, i, ".top.analysis.joint_dropped.dat")
if (file.exists(tajd)) dropped <- rbind(dropped, read.table(tajd, as.is=TRUE, header=TRUE))
}
included <- rename(included, c("ID"="Name", "TWAS.Z"="EWAS.Z", "TWAS.P"="EWAS.P"))
j <- merge(included, anno, by="Name")
sorted.data <- j[with(j,order(JOINT.P)),]
write.csv(sorted.data[setdiff(names(sorted.data),"FILE")], file=paste0(prefix, "annotatedJoint_included.csv"), quote=FALSE, row.names=FALSE)
cat(paste0("Joint annotation: ", prefix, "annotatedJoint_included.csv\n"))
dropped <- rename(dropped, c("ID"="Name", "TWAS.Z"="EWAS.Z", "TWAS.P"="EWAS.P"))
c <- merge(dropped, anno, by="Name")
sorted.data <- c[with(c,order(COND.P)),]
write.csv(sorted.data[setdiff(names(sorted.data),"FILE")], file=paste0(prefix, "annotatedJoint_dropped.csv"), quote=FALSE, row.names=FALSE)
cat(paste0("Conditional annotation: ", prefix, "annotatedJoint_dropped.csv\n"))
annotated.data <- within(annotated.data, {EWAS.P.Bonferroni <- 0.05/N})
included <- included[setdiff(names(included),c("FILE","EWAS.Z","EWAS.P"))]
aj <- merge(annotated.data, included, by="Name", all=TRUE)
dropped <- dropped[setdiff(names(dropped),c("FILE","EWAS.Z","EWAS.P"))]
ajc <- merge(aj, dropped, by="Name", all=TRUE)
ajc <- ajc[with(ajc,order(CHR,MAPINFO)),]
write.csv(ajc,file=paste0(prefix,"ajc.csv"),quote=FALSE, row.names=FALSE)
cat(paste0("Association + Joint/conditional annotation: ", prefix, "ajc.csv\n"))
cat("\nThe annotation is done.\n\n")
rm(prefix, temp, included, dropped, anno, annotated.data, sorted.data, sig.data, j, c, aj, ajc)
cat("Further information about FUSION and annotation is available from\n
http://gusevlab.org/projects/fusion/
https://support.illumina.com/array/array_kits/infinium_humanmethylation450_beadchip_kit/downloads.html\n\n")