forked from rebeccaknowlton/drosophila-longevity
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_int_model_results.R
306 lines (261 loc) · 15 KB
/
plot_int_model_results.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# this script reads in the TACC output for the interaction model and creates
# several plots comparing these results to the original author classifications
library(data.table)
library(ggplot2)
library(gridExtra)
# read in coefs, pvals, se from the 272 output files
int_model_coefs <- matrix(nrow = 271536, ncol = 6)
int_model_pvals <- matrix(nrow = 271536, ncol = 6)
int_model_se <- matrix(nrow = 271536, ncol = 6)
for (i in 1:272) {
coefs_tmp <- as.matrix(suppressWarnings(fread(input = paste0("int_model_coefs_",i,".txt"))[,2:7]))
pvals_tmp <- as.matrix(suppressWarnings(fread(input = paste0("int_model_pvals_",i,".txt"))[,2:7]))
se_tmp <- as.matrix(suppressWarnings(fread(input = paste0("int_model_se_",i,".txt"))[,2:7]))
int_model_coefs[(1000 * (i - 1) + 1):(1000 * (i - 1) + nrow(coefs_tmp)),] <- coefs_tmp
int_model_pvals[(1000 * (i - 1) + 1):(1000 * (i - 1) + nrow(coefs_tmp)),] <- pvals_tmp
int_model_se[(1000 * (i - 1) + 1):(1000 * (i - 1) + nrow(coefs_tmp)),] <- se_tmp
}
# save full version (all SNPs) of output files
write.table(int_model_coefs, file = "ALL_int_model_coefs.txt")
write.table(int_model_pvals, file = "ALL_int_model_pvals.txt")
write.table(int_model_se, file = "ALL_int_model_se.txt")
# look at -log10 of pvals from the interaction model
my_pvals <- -log10(int_model_pvals[,6])
my_pvals[which(is.na(my_pvals))] <- 0
min(my_pvals)
max(my_pvals)
# iterate through 10,000 possible pval significance thresholds
pval_threshold <- seq(from = 2, to = 15.2, length = 10000)
# for each pval threshold, count how many SNPs had a significant interaction term
num_int_sig <- rep(NA, 10000)
for (i in 1:length(num_int_sig)) {
num_int_sig[i] <- length(which(my_pvals > pval_threshold[i]))
}
plot(x = pval_threshold, y = num_int_sig)
# read in author classifications
author_classifications <- rep(NA, length = nrow(alt))
summary_info <- read.delim('SummaryTable_allsites_12Nov20.txt')
alt <- fread('29Jun20_merged_alt_counts_allCHR.txt')
for (i in 1:nrow(alt)) {
summary_idx <- which(summary_info$site == alt$site[i], arr.ind = TRUE)
if (length(summary_idx) != 0 ) {
author_classifications[i] <- summary_info$sig_cat[summary_idx]
}
}
# find how many of the significant interaction terms at each pval threshold
# were originally classified as HS, both, or CTRL
num_HS_sig <- rep(0, length(pval_threshold))
num_both_sig <- rep(0, length(pval_threshold))
num_CTRL_sig <- rep(0, length(pval_threshold))
for (i in 1:length(pval_threshold)) {
num_HS_sig[i] = length(which(author_classifications[which(my_pvals > pval_threshold[i])] == "HS"))
num_both_sig[i] = length(which(author_classifications[which(my_pvals > pval_threshold[i])] == "both"))
num_both_sig[i] = length(which(author_classifications[which(my_pvals > pval_threshold[i])] == "CTRL"))
}
# what proportion of the significant interaction terms fell into each category?
prop_HS_sig <- num_HS_sig / num_int_sig
prop_both_sig <- num_both_sig / num_int_sig
prop_CTRL_sig <- num_CTRL_sig / num_int_sig
# create dataframe for plotting
plot_data = data.frame(pval_threshold = pval_threshold,
num_int_sig = num_int_sig,
num_HS_sig = num_HS_sig,
num_both_sig = num_both_sig,
num_CTRL_sig = num_CTRL_sig,
prop_HS_sig = prop_HS_sig,
prop_both_sig = prop_both_sig,
prop_CTRL_sig = prop_CTRL_sig)
# plot proportion classified as CTRL, HS, or shared at each significance threshold
ggplot(data = plot_data) +
ylab("Proportion") +
xlab("-log10(p) Significance Threshold")+
geom_line(aes(x = pval_threshold, y = prop_HS_sig, color = "HS"), lwd = 1.5) +
geom_line(aes(x = pval_threshold, y = prop_both_sig, color = "shared"), lwd = 1.5) +
geom_line(aes(x = pval_threshold, y = prop_CTRL_sig, color = "CTRL"), lwd = 1.5) +
theme_bw() +
scale_color_manual(name = "Author Classification",
values = c("HS" = "cornflowerblue",
"shared" = "deeppink2",
"CTRL" = "goldenrod1"))
# what do the interaction model p values look like for sites the authors classified as significant?
log10_int_pvals <- -log10(int_model_pvals[,6])
log10_time_pvals <- -log10(int_model_pvals[,2])
log10_int_pvals[which(is.na(log10_int_pvals))] <- 0
log10_time_pvals[which(is.na(log10_time_pvals))] <- 0
plot_data2 <- data.frame(log10_int_pvals = log10_int_pvals,
log10_time_pvals = log10_time_pvals,
author_classifications = author_classifications)
ggplot(data = plot_data2, aes(x = log10_int_pvals, y = log10_time_pvals,
color = author_classifications, shape = author_classifications)) +
geom_point() +
scale_color_manual(values = c("NS" = "lightgray",
"shared" = "red",
"HS" = "purple",
"CTRL" = "green3")) +
scale_shape_manual(values = c("NS" = 1,
"shared" = 16,
"HS" = 16,
"CTRL" = 16)) +
theme_bw()
# want to change order of levels for author_classifications so that NS is plotted first
plot_data3 <- data.frame(log10_int_pvals = log10_int_pvals,
log10_time_pvals = log10_time_pvals,
author_classifications = author_classifications)
plot_data3$author_classifications[which(plot_data3$author_classifications == "NS")] <- "a NS"
plot_data3$author_classifications[which(plot_data3$author_classifications == "shared")] <- "b shared"
plot_data3$author_classifications[which(plot_data3$author_classifications == "HS")] <- "c HS"
plot_data3$author_classifications[which(plot_data3$author_classifications == "CTRL")] <- "d CTRL"
plot_data3 <- plot_data3[order(plot_data3$author_classifications),]
plot_data3$author_classifications[which(plot_data3$author_classifications == "a NS")] <- "NS"
plot_data3$author_classifications[which(plot_data3$author_classifications == "b shared")] <- "shared"
plot_data3$author_classifications[which(plot_data3$author_classifications == "c HS")] <- "HS"
plot_data3$author_classifications[which(plot_data3$author_classifications == "d CTRL")] <- "CTRL"
ggplot(data = plot_data3, aes(x = log10_int_pvals, y = log10_time_pvals,
color = author_classifications, shape = author_classifications)) +
geom_point(alpha = 0.35) +
scale_color_manual(values = c("NS" = "lightgray",
"shared" = "deeppink2",
"HS" = "cornflowerblue",
"CTRL" = "goldenrod2")) +
scale_shape_manual(values = c("NS" = 1,
"shared" = 16,
"HS" = 16,
"CTRL" = 16)) +
theme_bw() +
ylab("-log10(p) [time]") +
xlab("-log10(p) [time:environment]") +
labs(color = "Author Classification", shape = "Author Classification")
# plot coefficients
int_coefs <- int_model_coefs[,6]
time_coefs <- int_model_coefs[,2]
plot_data4 <- data.frame(int_coefs = int_coefs,
time_coefs = time_coefs,
author_classifications = author_classifications)
plot_data4$author_classifications[which(plot_data4$author_classifications == "NS")] <- "a NS"
plot_data4$author_classifications[which(plot_data4$author_classifications == "shared")] <- "b shared"
plot_data4$author_classifications[which(plot_data4$author_classifications == "HS")] <- "c HS"
plot_data4$author_classifications[which(plot_data4$author_classifications == "CTRL")] <- "d CTRL"
plot_data4 <- plot_data4[order(plot_data4$author_classifications),]
plot_data4$author_classifications[which(plot_data4$author_classifications == "a NS")] <- "NS"
plot_data4$author_classifications[which(plot_data4$author_classifications == "b shared")] <- "shared"
plot_data4$author_classifications[which(plot_data4$author_classifications == "c HS")] <- "HS"
plot_data4$author_classifications[which(plot_data4$author_classifications == "d CTRL")] <- "CTRL"
ggplot(data = plot_data4, aes(x = int_coefs, y = time_coefs,
color = author_classifications, shape = author_classifications)) +
geom_point(alpha = 0.35) +
scale_color_manual(values = c("NS" = "lightgray",
"shared" = "deeppink2",
"HS" = "cornflowerblue",
"CTRL" = "goldenrod2")) +
scale_shape_manual(values = c("NS" = 1,
"shared" = 16,
"HS" = 16,
"CTRL" = 16)) +
geom_hline(yintercept = 0, linetype = "dashed") +
geom_vline(xintercept = 0, linetype = "dashed") +
theme_bw() +
xlab("time:environment coefficient") +
ylab("time coefficient")+
labs(color = "Author Classification", shape = "Author Classification")
# look at int_coef + time_coef for the HS sites
plot_data5 <- data.frame(author_classifications = author_classifications,
idx = 1:length(author_classifications),
time_coefs = time_coefs,
int_coefs = int_coefs,
sum_coefs = int_coefs + time_coefs)
plot_data5$author_classifications[which(plot_data5$author_classifications == "NS")] <- "a NS"
plot_data5$author_classifications[which(plot_data5$author_classifications == "shared")] <- "b shared"
plot_data5$author_classifications[which(plot_data5$author_classifications == "HS")] <- "c HS"
plot_data5$author_classifications[which(plot_data5$author_classifications == "CTRL")] <- "d CTRL"
plot_data5 <- plot_data5[order(plot_data5$author_classifications),]
plot_data5$author_classifications[which(plot_data5$author_classifications == "a NS")] <- "NS"
plot_data5$author_classifications[which(plot_data5$author_classifications == "b shared")] <- "shared"
plot_data5$author_classifications[which(plot_data5$author_classifications == "c HS")] <- "HS"
plot_data5$author_classifications[which(plot_data5$author_classifications == "d CTRL")] <- "CTRL"
ggplot(plot_data5, aes(x = time_coefs, y = sum_coefs, color = author_classifications, shape = author_classifications)) +
geom_point(alpha = 0.35) +
theme_bw() +
scale_color_manual(values = c("NS" = "lightgray",
"shared" = "deeppink2",
"HS" = "cornflowerblue",
"CTRL" = "goldenrod2")) +
geom_hline(yintercept = 0, linetype = "dashed") +
geom_vline(xintercept = 0, linetype = "dashed") +
scale_shape_manual(values = c("NS" = 1,
"shared" = 16,
"HS" = 16,
"CTRL" = 16)) +
labs(color = "Author Classification", shape = "Author Classification")
# want to split plot into version where they had same signs, and where they had opposite signs
plot_data5_samesigns <- plot_data5[which(plot_data5$time_coefs * plot_data5$int_coefs > 0),]
plot_data5_diffsigns <- plot_data5[which(plot_data5$time_coefs * plot_data5$int_coefs < 0),]
ggplot(plot_data5_samesigns, aes(x = idx, y = sum_coefs, color = author_classifications, shape = author_classifications)) +
geom_point(alpha = 0.35) +
theme_bw() +
scale_color_manual(values = c("NS" = "lightgray",
"shared" = "deeppink2",
"HS" = "cornflowerblue",
"CTRL" = "goldenrod2")) +
geom_hline(yintercept = 0, linetype = "dashed") +
scale_shape_manual(values = c("NS" = 1,
"shared" = 16,
"HS" = 16,
"CTRL" = 16)) +
labs(color = "Author Classification", shape = "Author Classification") +
ylab("beta_t + beta_te") +
xlab("") +
ggtitle("Same Signs") +
theme(axis.text.x = element_blank())
ggplot(plot_data5_diffsigns, aes(x = idx, y = sum_coefs, color = author_classifications, shape = author_classifications)) +
geom_point(alpha = 0.35) +
theme_bw() +
scale_color_manual(values = c("NS" = "lightgray",
"shared" = "deeppink2",
"HS" = "cornflowerblue",
"CTRL" = "goldenrod2")) +
geom_hline(yintercept = 0, linetype = "dashed") +
scale_shape_manual(values = c("NS" = 1,
"shared" = 16,
"HS" = 16,
"CTRL" = 16)) +
labs(color = "Author Classification", shape = "Author Classification") +
ylab("beta_t + beta_te") +
xlab("") +
ggtitle("Opposite Signs") +
theme(axis.text.x = element_blank())
# z scores for coefs
int_z_scores <- int_model_coefs[,6] / int_model_se[,6]
time_z_scores <- int_model_coefs[,2] / int_model_se[,2]
mean(int_z_scores)
int_z_scores[which(is.na(int_z_scores))] <- 0
mean(time_z_scores)
time_z_scores[which(is.na(time_z_scores))] <- 0
plot_data6 <- data.frame(int_z_scores = int_z_scores,
time_z_scores = time_z_scores,
author_classifications = author_classifications)
plot_data6$author_classifications[which(plot_data6$author_classifications == "NS")] <- "a NS"
plot_data6$author_classifications[which(plot_data6$author_classifications == "shared")] <- "b shared"
plot_data6$author_classifications[which(plot_data6$author_classifications == "HS")] <- "c HS"
plot_data6$author_classifications[which(plot_data6$author_classifications == "CTRL")] <- "d CTRL"
plot_data6 <- plot_data6[order(plot_data6$author_classifications),]
plot_data6$author_classifications[which(plot_data6$author_classifications == "a NS")] <- "NS"
plot_data6$author_classifications[which(plot_data6$author_classifications == "b shared")] <- "shared"
plot_data6$author_classifications[which(plot_data6$author_classifications == "c HS")] <- "HS"
plot_data6$author_classifications[which(plot_data6$author_classifications == "d CTRL")] <- "CTRL"
ggplot(data = plot_data6, aes(x = int_z_scores, y = time_z_scores,
color = author_classifications, shape = author_classifications)) +
geom_point(alpha = 0.35) +
scale_color_manual(values = c("NS" = "lightgray",
"shared" = "deeppink2",
"HS" = "cornflowerblue",
"CTRL" = "goldenrod2")) +
scale_shape_manual(values = c("NS" = 1,
"shared" = 16,
"HS" = 16,
"CTRL" = 16)) +
geom_hline(yintercept = 0, linetype = "dashed") +
geom_vline(xintercept = 0, linetype = "dashed") +
theme_bw() +
labs(color = "Author Classification", shape = "Author Classification") +
xlab("time:environment coef/se") +
ylab("time coef/se")