-
Notifications
You must be signed in to change notification settings - Fork 1
/
genotype_twas.R
executable file
·148 lines (132 loc) · 5.32 KB
/
genotype_twas.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env Rscript
# genotype_twas.R
#
# Perform the genotype-level TWAS.
#
# Federico Marotta ([email protected])
# Feb,Jun,Aug-Sep 2020
suppressPackageStartupMessages({
library(docopt)
library(fplyr)
})
"genotype_twas
Associate the predicted expression to the phenotype. Use characters to force
factors. The first column must contain the individual ID, the second the
phenotype, and from the third onwards there must be the covariates.
Usage:
genotype_twas [options] <PHENO_FILE> <TREX_FILE>
genotype_twas (-h | --help)
Arguments:
TREX_FILE The predicted TBA-regulated expression
PHENO_FILE A file with phenotypes
Options:
-t --twas-samples=<FILE> [default: all of them]
-c --covar=<FILE> Path to the covariates (optional)
-q --pheno-quantile-normalize Apply an inverse normal transformation to the
quantitative phenotypes
-o --outdir=<DIR> Path to the output file [default: ./]
-j --threads=<N> Number of cores for parallel computations [default: 1]
-v --verbose=<N> Level of verbosity (0, 1, or 2) [default: 1]
-h --help Show this message
" -> doc
argv <- docopt(gsub(" \n\\s+", " ", x = doc, perl = T))
pheno <- fread(argv$PHENO_FILE,
stringsAsFactors = TRUE,
na.strings = c(getOption("datatable.na.strings", "NA"), ""))
w <- sapply(pheno, function(x) length(unique(na.omit(x))) > 1)
if (length(which(!w))) {
warning("Removing ", paste(names(which(!w)), collapse = ", "),
" from the phenotypes (all values are constant).")
pheno <- pheno[, ..w]
}
names(pheno)[1] <- "IID"
pheno$IID <- as.character(pheno$IID)
if (argv$`--twas-samples` != "all of them") {
samples <- readLines(argv$`--twas-samples`)
pheno <- pheno[IID %in% samples]
}
d <- pheno
if (!is.null(argv$covar)) {
covar <- fread(argv$covar,
stringsAsFactors = TRUE,
na.strings = c(getOption("datatable.na.strings", "NA"), ""))
w <- sapply(covar, function(x) length(unique(na.omit(x))) > 1)
if (length(which(!w))) {
warning("removing ", paste(names(which(!w)), collapse = ", "),
" from the covariates (all values are constant).")
covar <- covar[, ..w]
}
names(covar)[1] <- "IID"
covar$IID <- as.character(covar$IID)
d <- merge(pheno, covar, by = "IID")
}
if (!dir.exists(argv$outdir))
if (!dir.create(argv$outdir, recursive = T))
stop("Could not create output directory.")
out <- paste0(argv$outdir, "/", names(pheno)[-1], ".trex_itwas.tsv")
fmply(argv$TREX_FILE, out, parallel = as.integer(argv$threads), function(expr) {
d <- merge(expr[, c("IID", "TREX")], d, by = "IID")
if (argv$verbose > 0)
message("Considering gene ", expr$GENE[1], "...")
r <- lapply(seq_len(length(pheno) - 1), function(ph) {
ph <- ph + 1 # skip the first field
if (!is.null(argv$covar)) {
f <- as.formula(paste(names(pheno)[ph],
paste("TREX",
paste(names(covar)[-1], collapse = " + "),
sep = " + "),
sep = " ~ "))
} else {
f <- as.formula(paste(names(pheno)[ph], "TREX", sep = " ~ "))
}
if (argv$verbose > 1)
message("Considering phenotype ", names(pheno)[ph], "...")
if (is.factor(pheno[[ph]])) {
tryCatch({
l <- glm(f, family = binomial, d)
allcoef <- coef(summary(l))
excoef <- allcoef[rownames(allcoef) == "TREX"]
if (length(excoef) == 0)
return(NULL)
names(excoef) <- colnames(allcoef)
t <- data.table(Gene = expr$GENE[1], t(excoef))
t$Deviance <- l$deviance
t$`DF Residual` <- l$df.residual
t
}, error = function(e) {
message(e$message)
return(NULL)
}, warning = function(w) {
message(w$message)
return(NULL)
})
} else if (is.numeric(pheno[[ph]])) {
tryCatch({
if (argv$`--pheno-quantile-normalize`) {
d[[names(pheno)[ph]]] <- qnorm(
(rank(d[[names(pheno)[ph]]], na.last = "keep") - 0.5) / sum(!is.na(d[[names(pheno)[ph]]]))
)
}
l <- lm(f, d)
allcoef <- coef(summary(l))
excoef <- allcoef[rownames(allcoef) == "TREX"]
if (length(excoef) == 0)
return(NULL)
names(excoef) <- colnames(allcoef)
t <- data.table(Gene = expr$GENE[1], t(excoef))
t$`R squared` <- summary(l)$r.squared
t$`DF Residual` <- l$df.residual
t
}, error = function(e) {
message(e$message)
return(NULL)
}, warning = function(w) {
message(w$message)
return(NULL)
})
} else {
stop("Problem with PHENO_FILE: the type of the phenotype could not be determined.")
}
})
list(r)
})