-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDEG_plotting.Rmd
1556 lines (1182 loc) · 50.1 KB
/
DEG_plotting.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
```{r eval=FALSE}
# conda activate voyager
library(Seurat)
library(tidyverse)
library(RColorBrewer)
library(viridis)
library(cowplot)
library(ggrepel)
library(ggpubr)
library(ggrastr)
library(patchwork)
theme_set(theme_cowplot())
# set random seed for reproducibility
set.seed(12345)
setwd('/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/')
fig_dir <- 'figures/'
data_dir <- 'data/'
# load seurat object
seurat_obj <- readRDS(file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/data/harmony_annotated_integration.rds')
# load the color scheme!!
load('/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/data/color_scheme.rda')
```
Combine the individual DEG tables
```{r eval=FALSE}
###############################################################################
# Cluster markers
###############################################################################
DEG_dir <- "/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/cluster_markers/"
DEG_tests <- dir(DEG_dir)
combined <- Reduce(rbind, lapply(DEG_tests, function(file){
read.csv(paste0(DEG_dir, '/', file))
}))
combined$Nr4a2 <- ifelse(combined$gene %in% Nr4a2_targets, TRUE, FALSE)
write.csv(
combined,
file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/cluster_marker_DEGs.csv',
quote=FALSE, row.names=FALSE
)
###############################################################################
# celltype markers
###############################################################################
DEG_dir <- "/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/celltype_markers/"
DEG_tests <- dir(DEG_dir)
combined <- Reduce(rbind, lapply(DEG_tests, function(file){
read.csv(paste0(DEG_dir, '/', file))
}))
combined$Nr4a2 <- ifelse(combined$gene %in% Nr4a2_targets, TRUE, FALSE)
write.csv(
combined,
file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/celltype_marker_DEGs.csv',
quote=FALSE, row.names=FALSE
)
###############################################################################
# Cluster Nurr2c vs GFP
###############################################################################
DEG_dir <- "/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/cluster_Nurr2c_vs_GFP/"
DEG_tests <- dir(DEG_dir)
combined <- Reduce(rbind, lapply(DEG_tests, function(file){
read.csv(paste0(DEG_dir, '/', file))
}))
combined$Nr4a2 <- ifelse(combined$gene %in% Nr4a2_targets, TRUE, FALSE)
write.csv(
combined,
file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/cluster_Nurr2c_vs_GFP_DEGs.csv',
quote=FALSE, row.names=FALSE
)
###############################################################################
# Cluster Naive Nurr2c vs GFP
###############################################################################
DEG_dir <- "/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/cluster_Naive_Nurr2c_vs_GFP/"
DEG_tests <- dir(DEG_dir)
combined <- Reduce(rbind, lapply(DEG_tests, function(file){
read.csv(paste0(DEG_dir, '/', file))
}))
combined$Nr4a2 <- ifelse(combined$gene %in% Nr4a2_targets, TRUE, FALSE)
write.csv(
combined,
file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/cluster_Naive_Nurr2c_vs_GFP_DEGs.csv',
quote=FALSE, row.names=FALSE
)
###############################################################################
# Celltype Nurr2c vs GFP
###############################################################################
DEG_dir <- "/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/celltype_Nurr2c_vs_GFP/"
DEG_tests <- dir(DEG_dir)
combined <- Reduce(rbind, lapply(DEG_tests, function(file){
read.csv(paste0(DEG_dir, '/', file))
}))
combined$Nr4a2 <- ifelse(combined$gene %in% Nr4a2_targets, TRUE, FALSE)
write.csv(
combined,
file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/celltype_Nurr2c_vs_GFP_DEGs.csv',
quote=FALSE, row.names=FALSE
)
###############################################################################
# Celltype Naive Nurr2c vs GFP
###############################################################################
DEG_dir <- "/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/celltype_Naive_Nurr2c_vs_GFP/"
DEG_tests <- dir(DEG_dir)
combined <- Reduce(rbind, lapply(DEG_tests, function(file){
read.csv(paste0(DEG_dir, '/', file))
}))
combined$Nr4a2 <- ifelse(combined$gene %in% Nr4a2_targets, TRUE, FALSE)
write.csv(
combined,
file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/celltype_Naive_Nurr2c_vs_GFP_DEGs.csv',
quote=FALSE, row.names=FALSE
)
```
Write the significant DEGs for the Supplementary Tables
```{r eval=FALSE}
# cluster DEGs
degs_cl <- read.csv(file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/cluster_Nurr2c_vs_GFP_DEGs.csv')
degs_ct <- read.csv(file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/celltype_Nurr2c_vs_GFP_DEGs.csv')
keep_groups <- setdiff(unique(degs_cl$group), unique(degs_ct$group))
degs_cl <- subset(degs_cl, p_val_adj < 0.05 & group %in% keep_groups) %>%
dplyr::select(-Nr4a2)
degs_ct <- subset(degs_ct, p_val_adj < 0.05) %>%
dplyr::select(-Nr4a2)
degs <- rbind(degs_cl, degs_ct)
write.csv(
degs,
file=paste0(data_dir, 'experienced_DEGs_signif.csv'),
quote=FALSE, row.names=FALSE
)
# celltype DEGs
# Naive cluster DEGs
degs_cl <- read.csv(file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/cluster_Naive_Nurr2c_vs_GFP_DEGs.csv')
degs_ct <- read.csv(file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/celltype_Naive_Nurr2c_vs_GFP_DEGs.csv')
keep_groups <- setdiff(unique(degs_cl$group), unique(degs_ct$group))
degs_cl <- subset(degs_cl, p_val_adj < 0.05 & group %in% keep_groups) %>%
dplyr::select(-Nr4a2)
degs_ct <- subset(degs_ct, p_val_adj < 0.05) %>%
dplyr::select(-Nr4a2)
degs <- rbind(degs_cl, degs_ct)
write.csv(
degs,
file=paste0(data_dir, 'naive_DEGs_signif.csv'),
quote=FALSE, row.names=FALSE
)
```
Plot the Marker gene heatmap (Figure 2B)
```{r eval=FALSE}
# read combined
degs <- read.csv('/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/cluster_marker_DEGs.csv')
group_levels <- levels(seurat_obj$cell_identity)
degs$group <- factor(as.character(degs$group), levels=group_levels)
n_degs <- 5
plot_genes <- degs %>%
arrange(group) %>%
subset(p_val_adj <= 0.05) %>%
group_by(group) %>%
top_n(n_degs, wt=avg_log2FC) %>%
.$gene
seurat_obj$barcode <- colnames(seurat_obj)
temp <- table(seurat_obj@meta.data$cell_identity)
df <- data.frame()
for(i in 1:length(temp)){
if(temp[[i]] < 1000){
cur_df <- seurat_obj@meta.data %>% subset(cell_identity == names(temp)[i])
} else{
cur_df <- seurat_obj@meta.data %>% subset(cell_identity == names(temp)[i]) %>% sample_n(1000)
}
df <- rbind(df, cur_df)
}
seurat_obj <- ScaleData(seurat_obj, features=plot_genes)
p <- DoHeatmap(
seurat_obj %>% subset(barcode %in% df$barcode),
features=unlist(plot_genes),
group.by='cell_identity',
group.colors = cell_group_colors,
raster=TRUE, slot='scale.data'
) + theme(
axis.text.y = element_text(face='italic', size=3)
)
pdf(paste0(fig_dir, 'cluster_marker_gene_heatmap.pdf'), width=14, height=8, useDingbats=FALSE)
p
dev.off()
```
Neuronal marker gene analysis (Figure S2)
```{r eval=FALSE}
#--------------------------------------------------------------------------------
# MHb markers
#--------------------------------------------------------------------------------
seurat_mhb <- subset(seurat_obj, cell_type == 'MHb-Neuron')
seurat_mhb$annotation <- droplevels(seurat_mhb$annotation)
Idents(seurat_mhb) <- seurat_mhb$annotation
markers <- FindAllMarkers(
seurat_mhb,
test.use = "MAST",
min.pct = 0.2,
logfc.threshold = 0.5,
only.pos =TRUE
)
write.csv(markers, quote=FALSE, file=paste0(data_dir, 'mhb_markers.csv'))
markers <- read.csv(file=paste0(data_dir, 'mhb_markers.csv'))
# exclude mitochondrial genes from the plot
markers <- markers[!grepl("^mt-", markers$gene),]
n_degs <- 15
plot_genes <- markers %>%
arrange(cluster) %>%
subset(p_val_adj <= 0.05) %>%
group_by(cluster) %>%
top_n(n_degs, wt=avg_log2FC) %>%
.$gene
# set random seed
set.seed(42)
seurat_mhb$barcode <- colnames(seurat_mhb)
temp <- seurat_mhb@meta.data %>% group_by(annotation) %>% sample_n(300)
seurat_mhb <- ScaleData(seurat_mhb, features=plot_genes)
seurat_mhb$ordered_clusters <- fct_rev(seurat_mhb$annotation)
p <- DoHeatmap(
seurat_mhb %>% subset(barcode %in% temp$barcode),
features=unlist(plot_genes),
group.by='annotation',
raster=TRUE, slot='scale.data',
group.colors=cluster_colors
)+ theme(axis.text.y = element_text(face='italic'))
pdf(paste0(fig_dir, 'mhb_marker_gene_heatmap.pdf'), width=7, height=12, useDingbats=FALSE)
p
dev.off()
subset(markers, cluster == 'MHb-5') %>% arrange(avg_log2FC)
#--------------------------------------------------------------------------------
# LHb markers
#--------------------------------------------------------------------------------
seurat_lhb <- subset(seurat_obj, cell_type == 'LHb-Neuron')
seurat_lhb$annotation <- droplevels(seurat_lhb$annotation)
Idents(seurat_lhb) <- seurat_lhb$annotation
markers <- FindAllMarkers(
seurat_lhb,
test.use = "MAST",
min.pct = 0.2,
logfc.threshold = 0.5,
only.pos =TRUE
)
write.csv(markers, quote=FALSE, file=paste0(data_dir, 'lhb_markers.csv'))
markers <- read.csv(file=paste0(data_dir, 'lhb_markers.csv'))
markers <- markers[!grepl("^mt-", markers$gene),]
n_degs <- 10
plot_genes <- markers %>%
arrange(cluster) %>%
subset(p_val_adj <= 0.05) %>%
group_by(cluster) %>%
top_n(n_degs, wt=avg_log2FC) %>%
.$gene
# set random seed
set.seed(42)
seurat_lhb$barcode <- colnames(seurat_lhb)
temp <- seurat_lhb@meta.data %>% group_by(annotation) %>% sample_n(300)
seurat_lhb <- ScaleData(seurat_lhb, features=plot_genes)
seurat_lhb$ordered_clusters <- fct_rev(seurat_lhb$annotation)
p <- DoHeatmap(
seurat_lhb %>% subset(barcode %in% temp$barcode),
features=unlist(plot_genes),
group.by='annotation',
raster=TRUE, slot='scale.data',
group.colors=cluster_colors
)+ theme(axis.text.y = element_text(face='italic'))
pdf(paste0(fig_dir, 'lhb_marker_gene_heatmap.pdf'), width=7, height=12, useDingbats=FALSE)
p
dev.off()
subset(markers, cluster == 'MHb-5') %>% arrange(avg_log2FC)
#--------------------------------------------------------------------------------
# PHb markers
#--------------------------------------------------------------------------------
seurat_phb <- subset(seurat_obj, cell_type == 'PHb-Neuron')
seurat_phb$annotation <- droplevels(seurat_phb$annotation)
Idents(seurat_phb) <- seurat_phb$annotation
markers <- FindAllMarkers(
seurat_phb,
test.use = "MAST",
min.pct = 0.2,
logfc.threshold = 0.5,
only.pos =TRUE
)
write.csv(markers, quote=FALSE, file=paste0(data_dir, 'phb_markers.csv'))
markers <- read.csv(file=paste0(data_dir, 'phb_markers.csv'))
markers <- markers[!grepl("^mt-", markers$gene),]
n_degs <- 15
plot_genes <- markers %>%
arrange(cluster) %>%
subset(p_val_adj <= 0.05) %>%
group_by(cluster) %>%
top_n(n_degs, wt=avg_log2FC) %>%
.$gene
# set random seed
set.seed(42)
seurat_phb$barcode <- colnames(seurat_phb)
temp <- seurat_phb@meta.data %>% group_by(annotation) %>% sample_n(300)
seurat_phb <- ScaleData(seurat_phb, features=plot_genes)
seurat_phb$ordered_clusters <- fct_rev(seurat_phb$annotation)
p <- DoHeatmap(
seurat_phb %>% subset(barcode %in% temp$barcode),
features=unlist(plot_genes),
group.by='annotation',
raster=TRUE, slot='scale.data',
group.colors=cluster_colors
) + theme(axis.text.y = element_text(face='italic'))
pdf(paste0(fig_dir, 'phb_marker_gene_heatmap.pdf'), width=7, height=12, useDingbats=FALSE)
p
dev.off()
```
Make a table of primary and secondary Nr4a2 target genes using the
TF network analysis.
```{r eval=FALSE}
cur_tf <- 'Nr4a2'
tf_nets <- dir('../tf_net/data/tf_nets/')
tf_nets <- tf_nets[grepl('importance', tf_nets)]
network_df <- data.frame()
for(cur_net_file in tf_nets){
print(cur_net_file)
tmp <- strsplit(cur_net_file, '_')[[1]]
cur_celltype <- tmp[2]
cur_group <- tmp[3]
importance_df <- read.csv(paste0('../tf_net/data/tf_nets/', cur_net_file ))
#---------------------------------------------------------------------------#
# Define the TF regulons
#---------------------------------------------------------------------------#
n_tfs <- 5
importance_thresh <- 0.001
regulons <- importance_df %>%
subset(Gain > importance_thresh) %>%
group_by(gene) %>%
slice_max(order_by=Gain, n=n_tfs) %>%
ungroup()
# compute the degree for each TF:
tf_degrees <- table(regulons$tf)
#---------------------------------------------------------------------------#
# Get the primary & secondary targets of Nr4a2
#---------------------------------------------------------------------------#
# primary target genes
cur_primary<- regulons %>%
subset(tf == cur_tf)
# which of these pimary target genes are tfs?
cur_primary_tfs <- cur_primary %>%
subset(gene %in% unique(regulons$tf)) %>% .$gene
cur_tfs <- unique(c(cur_tf, cur_primary_tfs))
# get the regulons for these TFs:
cur_secondary <- subset(regulons, tf %in% cur_primary_tfs)
cur_secondary_tfs <- cur_primary %>%
subset(gene %in% unique(regulons$tf)) %>% .$gene
# combine the primary and secondary into one table
cur_network <- rbind(cur_primary, cur_secondary)
cur_network$Gain <- cur_network$Gain * sign(cur_network$Cor)
cur_genes <- unique(cur_network$gene)
length(cur_genes)
# make an igraph network from the nr4a2 regulon:
cur_network <- cur_network %>%
dplyr::rename(c(source=tf, target=gene)) %>%
mutate(Score = sign(Cor) * Gain)
primary_genes <- unique(cur_primary$gene)
secondary_genes <- unique(cur_network$target[! cur_network$target %in% primary_genes])
cur_network$target_type <- ifelse(cur_network$target %in% primary_genes, 'primary', 'secondary')
cur_network$cell_group <- cur_celltype
cur_network$group <- cur_group
network_df <- rbind(network_df, cur_network)
}
# save this as a supp table!
df <- network_df %>% subset(group != 'PHb-Neuron')
df <- df %>% dplyr::select(-c(Cover, Frequency, Cor, Score))
df <- df %>% dplyr::rename(regulatory_score=Gain)
write.csv(df, file=paste0(data_dir, 'Nr4a2_regulons.csv'), quote=FALSE, row.names=FALSE)
```
Nurr2c vs GFP with updated Nr4a2 targets (Neurons only)
Volcano plots in Figures 4 and 5
```{r eval=FALSE}
#---------------------------------------------------------------------------#
# First select one of the following groups of DEGs for plotting
#---------------------------------------------------------------------------#
# cluster DEGs
degs <- read.csv(file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/cluster_Nurr2c_vs_GFP_DEGs.csv')
degs <- subset(degs, group %in% seurat_obj$cell_identity)
degs$group <- factor(
degs$group,
levels = levels(seurat_obj$cell_identity)
)
name <- 'cluster'
Nr4a2 <- FALSE
nlabel <- 5
color1 <- 'darkgoldenrod3'; color2 <- 'hotpink3'
# celltype DEGs
degs <- read.csv(file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/celltype_Nurr2c_vs_GFP_DEGs.csv')
degs <- subset(degs, group %in% seurat_obj$cell_type)
degs$group <- factor(
degs$group,
levels = levels(seurat_obj$cell_type)
)
name <- 'celltype'
Nr4a2 <- FALSE
nlabel <- 5
color1 <- 'darkgoldenrod3'; color2 <- 'hotpink3'
# Naive cluster DEGs
degs <- read.csv(file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/cluster_Naive_Nurr2c_vs_GFP_DEGs.csv')
degs <- subset(degs, group %in% seurat_obj$cell_identity)
degs$group <- factor(
degs$group,
levels = levels(seurat_obj$cell_identity)
)
name <- 'cluster_naive'
Nr4a2 <- TRUE
nlabel <- 5
color2 <- "#643D78"; color1 <- "#14703F"
# Naive celltype DEGs
degs <- read.csv(file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/celltype_Naive_Nurr2c_vs_GFP_DEGs.csv')
degs <- subset(degs, group %in% seurat_obj$cell_type)
degs$group <- factor(
degs$group,
levels = levels(seurat_obj$cell_type)
)
name <- 'celltype_naive'
Nr4a2 <- TRUE
nlabel <- 5
color2 <- "#643D78"; color1 <- "#14703F"
# subset the neurons (this doesn't work for cluster level info)
degs <- subset(degs, group %in% c('MHb-Neuron', 'LHb-Neuron', 'PHb-Neuron'))
degs$group <- droplevels(degs$group)
#---------------------------------------------------------------------------#
# Add the TF information to the deg table
#---------------------------------------------------------------------------#
clusters <- unique(degs$group)
tmp <- data.frame()
for(cur_celltype in clusters){
print(cur_celltype)
group1 <- unique(degs$ident1)
group2 <- unique(degs$ident2)
# get the DEGs for this cluster
cur_degs <- subset(degs, group == cur_celltype)
# for this cluster, get the set of primary and secondary Nr4a2 targets:
cur_network <- subset(network_df, cell_group == cur_celltype & group %in% c(group1, group2))
primary_genes <- subset(cur_network, target_type == 'primary') %>% .$target
secondary_genes <- subset(cur_network, target_type == 'secondary') %>% .$target
# add info to the deg table:
cur_degs$target_type <- ifelse(cur_degs$gene %in% secondary_genes, 'secondary', 'other')
cur_degs$target_type <- ifelse(cur_degs$gene %in% primary_genes, 'primary', cur_degs$target_type)
tmp <- rbind(tmp, cur_degs)
}
degs <- tmp
#---------------------------------------------------------------------------#
# subset by Nr4a2 targets?
#---------------------------------------------------------------------------#
cur_target_type <- 'primary'
cur_target_type <- 'secondary'
cur_target_type <- 'other'
degs <- subset(degs, target_type == cur_target_type)
#---------------------------------------------------------------------------#
# setup table for volcano plot
#---------------------------------------------------------------------------#
# remove the mt genes from the volcano plot
degs <- degs[!grepl('mt-', degs$gene),]
# lowest non-zero value
lowest <- degs %>% subset(p_val_adj != 0) %>% top_n(-1, wt=p_val_adj) %>% .$p_val_adj
degs$p_val_adj <- ifelse(degs$p_val_adj == 0, lowest, degs$p_val_adj)
# label the top and bottom significant genes by log fold change
cur_degs <- Reduce(rbind, lapply(unique(degs$group), function(x){
cur <- subset(degs, group == x)
top_thresh <- cur %>% subset(p_val_adj <= 0.05 & avg_log2FC > 0) %>% top_n(nlabel, wt=avg_log2FC) %>% .$avg_log2FC %>% min
bottom_thresh <- cur %>% subset(p_val_adj <= 0.05 & avg_log2FC < 0) %>% top_n(-1*nlabel, wt=avg_log2FC) %>% .$avg_log2FC %>% max
cur$anno <- ifelse(cur$p_val_adj <= 0.05 & cur$avg_log2FC >= top_thresh, cur$gene, NA)
cur$anno <- ifelse(cur$p_val_adj <= 0.05 & cur$avg_log2FC <= bottom_thresh, cur$gene, cur$anno)
cur$anno <- ifelse(cur$gene == 'Nr4a2', cur$gene, cur$anno)
cur$color <- ifelse(cur$p_val_adj > 0.05, 'gray', ifelse(cur$avg_log2FC > 0, color1, color2))
cur
}))
groups <- levels(degs$group)
plot_list <- list()
for(cluster in groups){
print(cluster)
plot_degs <- cur_degs %>% subset(group == cluster)
p <- plot_degs %>%
ggplot(aes(x=avg_log2FC, y=-log10(p_val_adj))) +
geom_hline(yintercept=-log10(0.05), linetype='dashed')
# plot genes that are Nr4a2 targets
p <- p + ggrastr::rasterise(geom_point(
alpha=0.5,
color=plot_degs %>% .$color
), dpi=500)
p <- p +
geom_point(
inherit.aes=FALSE,
data=subset(plot_degs, !is.na(anno)),
aes(avg_log2FC, -log10(p_val_adj)),
fill=subset(plot_degs, !is.na(anno)) %>% .$color,
shape=21, size=3, color='black'
) +
geom_text_repel(aes(label=anno), color='black', fontface='italic', min.segment.length=0) +
xlim(-1*max(abs(plot_degs$avg_log2FC))-0.1, max(abs(plot_degs$avg_log2FC))+0.1) +
ggtitle(paste0(cluster)) +
xlab(bquote("Average log"[2]~"(Fold Change)")) +
ylab(bquote("-log"[10]~"(Adj. P-value)")) +
theme(
panel.border = element_rect(color='black', fill=NA, size=1),
panel.grid.major = element_blank(),
axis.line = element_blank(),
plot.title = element_text(hjust = 0.5),
legend.position='bottom'
)
plot_list[[cluster]] <- p
}
out <- paste0(fig_dir, 'volcano_', name, '_', cur_target_type, '.pdf')
plot_list <- lapply(plot_list, function(x){
x + theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
plot.margin = margin(0,0,0,0),
plot.title = element_text(vjust=-0.2)
)
})
# cluster
# pdf(out, width=18, height=12, useDingbats=FALSE)
# wrap_plots(plot_list, ncol=6)
# dev.off()
# celltype
pdf(out, width=9, height=3, useDingbats=FALSE)
wrap_plots(plot_list, ncol=3)
dev.off()
```
Volcano plots for all cell types, not split by TF target type
(Figure S6)
```{r eval=FALSE}
# cluster DEGs
degs <- read.csv(file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/cluster_Nurr2c_vs_GFP_DEGs.csv')
degs <- subset(degs, group %in% seurat_obj$cell_identity)
degs$group <- factor(
degs$group,
levels = levels(seurat_obj$cell_identity)
)
name <- 'cluster'
Nr4a2 <- FALSE
nlabel <- 5
color1 <- 'darkgoldenrod3'; color2 <- 'hotpink3'
# celltype DEGs
degs <- read.csv(file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/celltype_Nurr2c_vs_GFP_DEGs.csv')
degs <- subset(degs, group %in% seurat_obj$cell_type)
degs$group <- factor(
degs$group,
levels = levels(seurat_obj$cell_type)
)
name <- 'celltype'
Nr4a2 <- FALSE
nlabel <- 5
color1 <- 'darkgoldenrod3'; color2 <- 'hotpink3'
# Naive cluster DEGs
degs <- read.csv(file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/cluster_Naive_Nurr2c_vs_GFP_DEGs.csv')
degs <- subset(degs, group %in% seurat_obj$cell_identity)
degs$group <- factor(
degs$group,
levels = levels(seurat_obj$cell_identity)
)
name <- 'cluster_naive'
Nr4a2 <- TRUE
nlabel <- 5
color2 <- "#643D78"; color1 <- "#14703F"
# Naive celltype DEGs
degs <- read.csv(file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/celltype_Naive_Nurr2c_vs_GFP_DEGs.csv')
degs <- subset(degs, group %in% seurat_obj$cell_type)
degs$group <- factor(
degs$group,
levels = levels(seurat_obj$cell_type)
)
name <- 'celltype_naive'
Nr4a2 <- TRUE
nlabel <- 5
color2 <- "#643D78"; color1 <- "#14703F"
# remove the mt genes from the plot
degs <- degs[!grepl('mt-', degs$gene),]
# lowest non-zero value
lowest <- degs %>% subset(p_val_adj != 0) %>% top_n(-1, wt=p_val_adj) %>% .$p_val_adj
degs$p_val_adj <- ifelse(degs$p_val_adj == 0, lowest, degs$p_val_adj)
# label the top and bottom significant genes by log fold change
cur_degs <- Reduce(rbind, lapply(unique(degs$group), function(x){
cur <- subset(degs, group == x)
top_thresh <- cur %>% subset(p_val_adj <= 0.05 & avg_log2FC > 0) %>% top_n(nlabel, wt=avg_log2FC) %>% .$avg_log2FC %>% min
bottom_thresh <- cur %>% subset(p_val_adj <= 0.05 & avg_log2FC < 0) %>% top_n(-1*nlabel, wt=avg_log2FC) %>% .$avg_log2FC %>% max
cur$anno <- ifelse(cur$p_val_adj <= 0.05 & cur$avg_log2FC >= top_thresh, cur$gene, NA)
cur$anno <- ifelse(cur$p_val_adj <= 0.05 & cur$avg_log2FC <= bottom_thresh, cur$gene, cur$anno)
cur$anno <- ifelse(cur$gene == 'Nr4a2', cur$gene, cur$anno)
cur$color <- ifelse(cur$p_val_adj > 0.05, 'gray', ifelse(cur$avg_log2FC > 0, color1, color2))
cur
}))
groups <- levels(degs$group)
plot_list <- list()
for(cluster in groups){
print(cluster)
plot_degs <- cur_degs %>% subset(group == cluster)
p <- plot_degs %>%
ggplot(aes(x=avg_log2FC, y=-log10(p_val_adj))) +
geom_hline(yintercept=-log10(0.05), linetype='dashed')
# plot genes that are Nr4a2 targets
p <- p + ggrastr::rasterise(geom_point(
alpha=0.5,
color=plot_degs %>% .$color
), dpi=500)
p <- p +
geom_point(
inherit.aes=FALSE,
data=subset(plot_degs, !is.na(anno)),
aes(avg_log2FC, -log10(p_val_adj)),
fill=subset(plot_degs, !is.na(anno)) %>% .$color,
shape=21, size=3, color='black'
) +
geom_text_repel(aes(label=anno), color='black', fontface='italic', min.segment.length=0) +
xlim(-1*max(abs(plot_degs$avg_log2FC))-0.1, max(abs(plot_degs$avg_log2FC))+0.1) +
ggtitle(paste0(cluster)) +
xlab(bquote("Average log"[2]~"(Fold Change)")) +
ylab(bquote("-log"[10]~"(Adj. P-value)")) +
theme(
panel.border = element_rect(color='black', fill=NA, size=1),
panel.grid.major = element_blank(),
axis.line = element_blank(),
plot.title = element_text(hjust = 0.5),
legend.position='bottom'
)
plot_list[[cluster]] <- p
}
out <- paste0(fig_dir, 'volcano_', name, '.pdf')
plot_list <- lapply(plot_list, function(x){
x + theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
plot.margin = margin(0,0,0,0),
plot.title = element_text(vjust=-0.2)
)
})
# cluster
pdf(out, width=18, height=12, useDingbats=FALSE)
wrap_plots(plot_list, ncol=6)
dev.off()
# celltype
pdf(out, width=18, height=6.5, useDingbats=FALSE)
wrap_plots(plot_list, ncol=6)
dev.off()
```
RRHO plots to compare DEGs between MHb and LHb
Figures 4C and 5D
```{r eval=FALSE}
library(RRHO)
library(viridis)
library(ggpubr)
degs <- read.csv(file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/DEGs/data/celltype_Nurr2c_vs_GFP.csv')
degs$group <- factor(
degs$group,
levels = levels(seurat_obj$cell_type)
)
name <- 'celltype'
cur_target_type <- 'primary'
cur_target_type <- 'secondary'
cur_target_type <- 'other'
degs <- subset(degs, target_type == cur_target_type)
# lowest non-zero value
lowest <- degs %>% subset(p_val_adj != 0) %>% top_n(-1, wt=p_val_adj) %>% .$p_val_adj
degs$p_val_adj <- ifelse(degs$p_val_adj == 0, lowest, degs$p_val_adj)
groups <- c('MHb-Neuron','LHb-Neuron', 'PHb-Neuron')
pairs <- combn(groups, 2)
# plot settings
rrho_plot_list <- list()
cor_list <- c()
NR4A2 <- TRUE
#rrho_maxval <- 500
rrho_maxval <- 350
colfunc <- inferno
for(i in 1:ncol(pairs)){
cur_pair <- pairs[,i]
name <- paste(cur_pair, collapse='_')
print(name)
cur_x <- cur_pair[1]; cur_y <- cur_pair[2]
cur_degs_x <- subset(degs, group == cur_x & Nr4a2 == NR4A2 )
cur_degs_y <- subset(degs, group == cur_y & Nr4a2 == NR4A2 )
# make sure they are in the same order:
rownames(cur_degs_x) <- cur_degs_x$gene
rownames(cur_degs_y) <- cur_degs_y$gene
cur_degs_y <- cur_degs_y[cur_degs_x$gene,]
# join the two dataframes
plot_df <- dplyr::inner_join(cur_degs_x, cur_degs_y, by = 'gene')
cur_cor <- cor(x=as.numeric(plot_df$avg_log2FC.x), y=as.numeric(plot_df$avg_log2FC.y))
cor_list <- c(cor_list, cur_cor)
# set up gene lists
gl1 <- plot_df[,c('gene', 'avg_log2FC.x')]
gl2 <- plot_df[,c('gene', 'avg_log2FC.y')]
# run rrho
test <- RRHO(gl1, gl2, alternative='enrichment', BY=TRUE)
overlap_df <- reshape2::melt(test$hypermat.by)
#subset(overlap_df, value != Inf) %>% .$value %>% max
overlap_df$value <- ifelse(overlap_df$value > rrho_maxval, rrho_maxval, overlap_df$value)
# plot rrho heatmap
p <- ggplot(overlap_df, aes(x=Var1, y=Var2, fill=value, color=value)) +
ggrastr::rasterise(geom_tile(), dpi=500) +
scale_fill_gradientn(colors=colfunc(256), limits=c(0, rrho_maxval)) +
scale_color_gradientn(colors=colfunc(256), limits=c(0, rrho_maxval)) +
theme(
plot.title=element_text(hjust=0.5, size=5, face='plain'),
axis.line=element_blank(),
axis.ticks=element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title = element_blank(),
plot.margin=margin(0,0,0,0)
) + coord_equal() + ggtitle(paste0(name, '\nR=', signif(as.numeric(cur_cor),3)))
rrho_plot_list[[name]] <- p
}
pdf(paste0(fig_dir, 'deg_rrho_combined_', cur_target_type, '.pdf'), width=3, height=7)
wrap_plots(rrho_plot_list, ncol=1) + plot_layout(guides='collect')
dev.off()
```
Quantify overlap between the Naive & Behavior DEGs
Euler Diagrams in Figures 4 and 5
```{r eval=FALSE}
library(GeneOverlap)
# cluster DEGs
degs <- read.csv(file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/cluster_Nurr2c_vs_GFP_DEGs.csv')
degs <- subset(degs, group %in% seurat_obj$cell_identity)
name <- 'cluster'
degs$group <- factor(
degs$group,
levels = levels(seurat_obj$cell_identity)
)
degs_nurr2c <- degs
# Naive cluster DEGs
degs <- read.csv(file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/cluster_Naive_Nurr2c_vs_GFP_DEGs.csv')
degs <- subset(degs, group %in% seurat_obj$cell_identity)
degs$group <- factor(
degs$group,
levels = levels(seurat_obj$cell_identity)
)
Nr4a2 <- TRUE
degs_naive <- degs
# celltype DEGs
degs <- read.csv(file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/celltype_Nurr2c_vs_GFP_DEGs.csv')
degs <- subset(degs, group %in% seurat_obj$cell_type)
degs$group <- factor(
degs$group,
levels = levels(seurat_obj$cell_type)
)
name <- 'celltype'
degs_nurr2c <- degs
# Naive celltype DEGs
degs <- read.csv(file='/dfs7/swaruplab/smorabit/collab/woodlab/cocaine_mouse_2021/Nurr2c_vs_GFP/revision/DEGs/data/celltype_Naive_Nurr2c_vs_GFP_DEGs.csv')
degs <- subset(degs, group %in% seurat_obj$cell_type)
degs$group <- factor(
degs$group,
levels = levels(seurat_obj$cell_type)
)
Nr4a2 <- FALSE
degs_naive <- degs
#--------------------------------------------------------------
# Compute gene overlaps
#--------------------------------------------------------------
groups <- levels(degs_nurr2c$group)
fc_cutoff <- 0.25
genome.size <- nrow(seurat_obj)
overlap_df <- do.call(rbind, lapply(groups, function(cur_group){
cur_nurr2c_up <- degs_nurr2c %>% subset(group == cur_group & p_val_adj < 0.05 & avg_log2FC >= fc_cutoff) %>% .$gene
cur_naive_up <- degs_naive %>% subset(group == cur_group & p_val_adj < 0.05 & avg_log2FC >= fc_cutoff) %>% .$gene
#up_setsize <- length(intersect)
cur_nurr2c_down <- degs_nurr2c %>% subset(group == cur_group & p_val_adj < 0.05 & avg_log2FC <= -1* fc_cutoff) %>% .$gene
cur_naive_down <- degs_naive %>% subset(group == cur_group & p_val_adj < 0.05 & avg_log2FC <= -1* fc_cutoff) %>% .$gene
cur_overlap_up <- testGeneOverlap(newGeneOverlap(
cur_nurr2c_up,
cur_naive_up,
genome.size=genome.size
))