-
Notifications
You must be signed in to change notification settings - Fork 1
/
Fig_1f.Rmd
335 lines (262 loc) · 9.55 KB
/
Fig_1f.Rmd
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
---
title: "R Notebook"
output: html_notebook
---
This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Cmd+Shift+Enter*.
```{r}
#if (!require("BiocManager", quietly = TRUE))
# install.packages("BiocManager")
#BiocManager::install("TxDb.Mmusculus.UCSC.mm9.knownGene")
#BiocManager::install("TxDb.Mmusculus.UCSC.mm10.knownGene")
#BiocManager::install("TxDb.Mmusculus.UCSC.mm38.knownGene")
#BiocManager::install("org.Mm.eg.db")
#BiocManager::install("annotatr")
##browseVignettes("annotatr")
#BiocManager::install("viridis")
#BiocManager::install("clusterProfiler")
#BiocManager::install("ChIPseeker")
#BiocManager::install("ReactomePA")
library(dplyr)
library(tidyr)
library(annotatr)
library(readr)
library(ChIPseeker)
library(TxDb.Mmusculus.UCSC.mm10.knownGene)
library(clusterProfiler)
library(ReactomePA)
require(viridis)
library(ggplot2)
library(RColorBrewer)
library(colorspace)
```
```{r}
# Select annotations for intersection with regions
# Note inclusion of custom annotation, and use of shortcuts
setwd('/Users/tarkhov/Dropbox/Harvard/Science/R')
#dm_file = system.file('extdata', 'GSE80672_entropic_regions.csv', package = 'annotatr')
#dm_file = system.file('extdata', 'IDH2mut_v_NBM_multi_data_chr9.txt.gz', package = 'annotatr')
#dm_file = "/Users/tarkhov/Dropbox/Harvard/Science/Stochastic aging/data/GSE80672/GSE80672_regions.csv"
#dm_file = "/Users/tarkhov/Dropbox/Harvard/Science/Stochastic aging/data/GSE80672/GSE80672_regions_full.csv"
#dm_file = "/Users/tarkhov/Dropbox/Harvard/Science/Stochastic aging/data/GSE80672/GSE80672_Petkovich_regions.csv"
#dm_file = "/Users/tarkhov/Dropbox/Harvard/Science/scDNAm/data/GSE121436/GSE121436_b_up.csv"
#dm_file = "/Users/tarkhov/Dropbox/Harvard/Science/Stochastic aging/data/GSE80672/GSE80672_regions_7types.csv"
#dm_file = "/Users/tarkhov/Dropbox/Harvard/Science/Stochastic aging/data/GSE80672/scDNAm_aging_hypohyper_meth.csv"
dm_file = "/Users/tarkhov/Dropbox/Harvard/Science/Stochastic aging/data/GSE80672/scDNAm_aging_coreg_stoch_meth.csv"
extraCols = c()#r_age = 'numeric', p_age = 'numeric'
#, entropic_BA_x0 = 'numeric',
#antientropic_BA_b = 'numeric', antientropic_BA_x0 = 'numeric'
#)
dm_regions = read_regions(con = dm_file, genome = 'mm10', extraCols = extraCols,
format = 'bed',
rename_name = 'DM_status', rename_score = 'pval'
)
# Use less regions to speed things up
#dm_regions = dm_regions[1:2000]
print(dm_regions)
```
```{r}
only_mm10 <- builtin_annotations() %>%
as.data.frame %>%
filter(grepl("mm10", .))
annots <- only_mm10$"."
# Build the annotations (a single GRanges object)
annotations = build_annotations(genome = 'mm10', annotations = annots)
# Intersect the regions we read in with the annotations
dm_annotated = annotate_regions(
regions = dm_regions,
annotations = annotations,
ignore.strand = TRUE,
quiet = FALSE)
# A GRanges object is returned
print(dm_annotated)
```
```{r}
#covplot(dm_annotated, weightCol="r_age")
df_dump <- data.frame(annotations)
write.csv(df_dump, "mm10_annotation_dump.csv", row.names = rownames(df_dump))
```
```{r}
txdb <- TxDb.Mmusculus.UCSC.mm10.knownGene
gene <- seq2gene(dm_annotated, tssRegion = c(-1000, 1000), flankDistance = 3000, TxDb=txdb)
pathway2 <- enrichPathway(gene)
head(pathway2, 2)
```
```{r}
dotplot(pathway2)
```
```{r}
#covplot(dm_regions, weightCol="r_age")
# Randomize the input regions
dm_random_regions = randomize_regions(
regions = dm_regions,
allow.overlaps = TRUE,
per.chromosome = TRUE)
# Annotate the random regions using the same annotations as above
# These will be used in later functions
dm_random_annotated = annotate_regions(
regions = dm_random_regions,
annotations = annotations,
ignore.strand = TRUE,
quiet = TRUE)
# Find the number of regions per annotation type
dm_annsum = summarize_annotations(
annotated_regions = dm_annotated,
quiet = TRUE)
print(dm_annsum)
```
```{r}
# Find the number of regions per annotation type
# and the number of random regions per annotation type
dm_annsum_rnd = summarize_annotations(
annotated_regions = dm_annotated,
annotated_random = dm_random_annotated,
quiet = TRUE)
print(dm_annsum_rnd)
```
```{r}
# Take the mean of the diff_meth column across all regions
# occurring in an annotation.
dm_numsum = summarize_numerical(
annotated_regions = dm_annotated,
by = c('annot.type', 'annot.id'),
over = c('r_age'),
quiet = TRUE)
print(dm_numsum)
```
```{r}
# Count the occurrences of classifications in the DM_status
# column across the annotation types.
dm_catsum = summarize_categorical(
annotated_regions = dm_annotated,
by = c('annot.type', 'DM_status'),
quiet = TRUE)
print(dm_catsum)
```
```{r}
#dm_annotated <- data.frame(dm_annotated)
# The orders for the x-axis labels.
x_order = sort(annots)
#print(x_order)
#x_order = x_order[c(2,3,6,7,9,10,11, 13,15,17,18)]
#x_order = x_order[c(1,2,3)]
# The orders for the fill labels.
fill_order = c(
'hyper',
'none',
'hypo')
bioages_nonord = c('entropic_down_BA', 'entropic_up_BA', 'antientropic_down_BA', 'antientropic_up_BA',
'constant_BA', 'antient_insignificant_up_BA', 'antient_insignificant_down_BA',
'ent_insignificant_up_BA', 'ent_insignificant_down_BA')
bioages = c('constant_BA', 'ent_insignificant_up_BA', 'antient_insignificant_up_BA',
'entropic_up_BA', 'antientropic_up_BA',
'entropic_down_BA', 'antientropic_down_BA',
'ent_insignificant_down_BA', 'antient_insignificant_down_BA', 'petkovich_block')
bioages = c('constant_BA', 'ent_insignificant_up_BA', 'antient_insignificant_up_BA',
'entropic_up_BA', 'antientropic_up_BA',
'entropic_down_BA', 'antientropic_down_BA',
'ent_insignificant_down_BA', 'antient_insignificant_down_BA', 'petkovich_block')
#bioages = c('Background', 'Hypermethylated', 'Hypomethylated')
bioages = c('Background', 'Co-regulated', 'Stochastic')
#bioages = c('petkovich_clock')
#bioages = c('from Young ISO Det. to Young HET Det.')
#bioages = c('from Old ISO Det. to Old HET Det.')
dm_vs_kg_cat = plot_categorical(
annotated_regions = dm_annotated, x='annot.type', fill='DM_status',
x_order = x_order,
fill_order = bioages,
position='fill',
#position='dodge',
#position='stack',
legend_title = 'DM Status',
x_label = 'knownGene Annotations',
y_label = 'Proportion')
print(dm_vs_kg_cat)
```
```{r}
dm_vs_kg_cat = plot_categorical(
annotated_regions = dm_annotated, fill='annot.type',
x='DM_status',
fill_order = x_order,
x_order = bioages,
position='fill',
#position='dodge',
#position='stack',
legend_title = 'Genomic Annotation',
x_label = 'Epigenetic signatures',
y_label = 'Proportion')
#xcolors = c('black', '#fddbc7', '#b2182b', '#ef8a62', '#d1e5f0', '#2166ac', '#67a9cf', '#c7c7c7')
#ycolors = c('#67001f','#b2182b','#d6604d','#f4a582','#fddbc7','#f7f7f7','#d1e5f0','#92c5de','#4393c3','#2166ac','#053061')
dm_vs_kg_cat
```
```{r}
dm_vs_kg_cat = plot_categorical(
annotated_regions = dm_annotated, fill='annot.type',
x='DM_status',
fill_order = x_order,
x_order = bioages,
#position='fill',
#position='dodge',
position='stack',
legend_title = 'Genomic Annotation',
x_label = 'Epigenetic signatures',
y_label = 'Proportion')
print(dm_vs_kg_cat)
```
```{r}
dm_vs_kg_cat = plot_categorical(
annotated_regions = dm_annotated, fill='annot.type',
x='DM_status',
fill_order = x_order,
x_order = bioages,
#position='fill',
position='dodge',
#position='stack',
legend_title = 'DM Status',
x_label = 'knownGene Annotations',
y_label = 'Proportion')
print(dm_vs_kg_cat)
```
```{r}
dm_vs_kg_annotations = plot_annotation(
annotated_regions = dm_annotated,
#annotation_order = annots_order,
plot_title = '# of Sites Tested for DM annotated on chr9',
x_label = 'knownGene Annotations',
y_label = 'Count')
print(dm_vs_kg_annotations)
```
```{r}
dm_vs_kg_annotations_wrandom = plot_annotation(
annotated_regions = dm_annotated,
annotated_random = dm_random_annotated,
#annotation_order = annots_order,
plot_title = 'Dist. of Sites Tested for DM (with rndm.)',
x_label = 'Annotations',
y_label = 'Count')
print(dm_vs_kg_annotations_wrandom)
```
```{r}
dm_vs_coannotations = plot_coannotations(
annotated_regions = dm_annotated,
#annotation_order = annots_order,
axes_label = 'Annotations',
plot_title = 'Regions in Pairs of Annotations')
print(dm_vs_coannotations)
```
```{r}
dm_vs_regions_annot = plot_numerical(
annotated_regions = dm_annotated,
x = 'r_age',
facet = 'annot.type',
facet_order = annots,#c('hg19_genes_1to5kb','hg19_genes_promoters',
# #'hg19_genes_5UTRs','hg19_genes_3UTRs', 'hg19_custom_ezh2',
# #'hg19_genes_intergenic', 'hg19_cpg_islands'),
bin_width = 0.01,
plot_title = 'Group 0 Region Methylation In Genes',
x_label = 'Group 0')
print(dm_vs_regions_annot)
```
Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Cmd+Option+I*.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Cmd+Shift+K* to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.