-
Notifications
You must be signed in to change notification settings - Fork 6
/
MultimodalMappingPDA-scRNASeq.R
2712 lines (2128 loc) · 122 KB
/
MultimodalMappingPDA-scRNASeq.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
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
# This script reproduces the single cell RNAseq analysis of human pancreatic
# samples and PBMCs in Figures 2A, 2B, 2C, 2D, 2E, 2F, 3A, 3B, 3C, 3D, 3E, 3F,
# 4A, 4B, 4C, 4D, 4E, 4F, 4G, 5A, 5B, 5C, 5D, 5E, 5F, 6A, 6B, 6C, 6D, 6E, 6F, 6G
# and Supplementary Figures S2B, S2C, S2D, S3A, S3B, S3C, S3D, S3E, S4A, S4B, S4C,
# S5A, S5B, S5C, S5D, S5E of the paper "Multimodal Mapping of the Tumor
# and Peripheral Blood Immune Landscape in Human Pancreatic Cancer"
# The raw data was processed in line with the
# Seurat workflow outlined on the Satija Lab website
# (https://satijalab.org/seurat/v3.2/pbmc3k_tutorial.html) as well as in the
# following reference:
#
# Stuart T, Butler A, Hoffman P, Hafemeister C, Papalexi E, III WMM, Hao Y,
# Stoeckius M, Smibert P, Satija R (2019). "Comprehensive Integration of
# Single-Cell Data." Cell, 177, 1888-1902. doi: 10.1016/j.cell.2019.05.031,
# https://doi.org/10.1016/j.cell.2019.05.031.
#Load required packages
library(Seurat)
library(dplyr)
library(magrittr)
library(data.table)
library(Matrix)
library(devtools)
library(RcppArmadillo)
library(Rcpp)
library(scales)
library(pheatmap)
library(gplots)
library(ggplot2)
library(cowplot)
library(tibble)
library(xlsx)
library(data.table)
#Install batch correction package harmony
install_github("immunogenomics/harmony")
devtools::install_github("immunogenomics/harmony")
library(harmony)
#If using processed data from GEO/dbGaP, skip ahead to section Figures/Data Analysis
# Table of Contents
# 1. Functions
# 2. Data Pre-processing
# 3. Interactome Analysis
# 4. Figures/Data Analysis
#____________________________________________________________________________________________________________________________________________________________________#
# 1. Functions
#Interactome Code
check_genes <- function(genes, database, object_genes) {
'Check to make sure that wanted genes are in reference you provide and in object
Args:
genes (chr vector): list of potential wanted genes
database (data.frame): table with reference ligand/receptor pairs
object_genes (chr vector): list of genes present in Seurat object
Returns:
not_found_list (chr list): list of genes not found in database and/or object
'
database_genes <- as.vector(unlist(database))
not_found_database <- c()
not_found_object <- c()
for (i in 1:length(genes)) {
if (!(genes[i] %in% database_genes)) {
not_found_database <- c(not_found_database, genes[i])
}
if (!(genes[i] %in% object_genes)) {
not_found_object <- c(not_found_object, genes[i])
}
}
not_found_list <- list(database=not_found_database, object=not_found_object)
return(not_found_list)
}
make_LR_pairs <- function(ligands, receptors, database) {
'Make all LR pairs based on wanted ligands and/or receptors and database provided
Args:
ligands (chr vector): list of wanted ligands
receptors (chr vector): list of wanted receptors
database (data.frame): table with reference ligand/receptor pairs
Returns:
wanted_LR (data.frame): data.frame with ligand/receptor pairs from wanted ligands and receptors
'
wanted_LR <- data.frame(Ligand = character(0), Receptor = character(0))
for (ligand in ligands){
# list of corresponding receptors
corresponding_receptors <- unique(as.character(database[,2][grep(ligand, database[,1])]))
for (receptor in corresponding_receptors) {
LR_row <- data.frame(Ligand = ligand, Receptor = receptor)
wanted_LR <- rbind(wanted_LR, LR_row)
}
}
# filter out unwanted receptors
wanted_LR <- wanted_LR[which(wanted_LR$Receptor %in% wanted_receptors),]
return(wanted_LR)
}
create_LR_table <- function(ligands, receptors, cell_types, LRs, avg0, avg1) {
'Create table w/ ligand, receptor, source and target cells, average expressions, and IDs
Args:
ligands (chr vector): list of wanted ligands
receptors (chr vector): list of wanted receptors
cell_types (chr vector): list of common cell types between two objects
LRs (data.frame): table with with wanted ligand/receptor pairs (from make_LR_pairs function)
avg0 (list of num data.frame): average expression table of object 0
avg1 (list of num data.frame): average expression table of object 1
Returns:
LR_table (data.frame): table of potential ligand/receptor pairs
Contains ligand, receptor, source, target, average expression of ligand and receptors from
source and target cells. Also contains IDs (important for Cytoscape).
'
LR_table <- data.frame()
count <- 0
for (ligand in ligands) {
known_receptors <- LRs$Receptor[which(LRs$Ligand == ligand)]
for (receptor in known_receptors) {
for (i in c(1:length(cell_types))) {
for (j in c(1:length(cell_types))) {
LR_table <- rbind(LR_table,data.frame(ligands = ligand, receptors = receptor,
source = cell_types[i], target = cell_types[j],
avg_lig_0 = avg0[ligand, cell_types[i]],
avg_rec_0 = avg0[receptor, cell_types[j]],
avg_lig_1 = avg1[ligand, cell_types[i]],
avg_rec_1 = avg1[receptor, cell_types[j]]))
}
}
}
cat(count, ' ')
count <- count+1
}
# Create IDs
source_ID <- c()
target_ID <- c()
# Find optimal cell type IDs
ids_key <- c()
for (i in 1:length(cell_types)) {
for (j in 1:min(nchar(cell_types))) {
name_1 <- substring(cell_types[i], 1, j)
name_list <- c()
name_list <- c(sapply(cell_types[-i], function(x) name_list <- c(name_list, substring(x, 1, j))))
if(name_1 %in% name_list) {
next
}
else {
ids_key <- c(ids_key, name_1)
break
}
}
}
names(ids_key) <- cell_types
for (i in c(1:length(LR_table[,1]))) {
letter1 <- as.character(ids_key[LR_table$source[i]])
letter2 <- as.character(ids_key[LR_table$target[i]])
n1 <- which(ligands == LR_table$ligands[i])
n2 <- which(receptors == LR_table$receptors[i])
source_ID <- c(source_ID, paste(letter1, 'L', n1, sep = ''))
target_ID <- c(target_ID, paste(letter2, 'R', n2, sep = ''))
}
LR_table <- cbind(LR_table, data.frame(source_ID = source_ID, target_ID = target_ID))
return(LR_table)
}
avg_LR_filt <- function(table, threshold) {
'Calculates states (ON/OFF --> 1/0) and filter if there is no expression in both groups
Args:
table (data.frame): table with potential ligand/receptors (from create_LR_table)
threshold (num): average expression threshold
Returns:
table (data.frame): filtered table based on average expression
'
# Find states of pairs in each group
LR_states <- data.frame(lig_0 = character(0), rec_0 = character(0),
lig_1 = character(0), rec_1 = character(0),
is_on = logical(0))
for (i in c(1:length(LR_table$avg_lig_0))) {
row_states <- data.frame(lig_0 = table$avg_lig_0[i] > threshold,
rec_0 = table$avg_rec_0[i] > threshold,
lig_1 = table$avg_lig_1[i] > threshold,
rec_1 = table$avg_rec_1[i] > threshold)
row_states$is_on <- (row_states$lig_0 & row_states$rec_0) | (row_states$lig_1 & row_states$rec_1)
LR_states <- rbind(LR_states, row_states)
}
table <- cbind(table, ifelse((LR_states$lig_0 & LR_states$rec_0), 1, 0))
table <- cbind(table, ifelse((LR_states$lig_1 & LR_states$rec_1), 1, 0))
colnames(table)[11] <- 'is_on_0'
colnames(table)[12] <- 'is_on_1'
# Filter out pairs if pairs in both group are less than threshold
table <- table[which(LR_states$is_on == TRUE),]
return(table)
}
LR_diff <- function(table, data_0, data_1, genes, label, alpha = 0.05) {
'Calculate Wilcoxon-rank test on ligands and receptors (separately) between groups
Order of test: data_0 --> data_1
Args:
table (data.frame): table with potential ligand/receptors (from create_LR_table)
data_0 (data.frame): table of gene expression from object 0
data_1 (data.frame): table of gene expression from object 1
genes (chr vector): list of wanted genes
label (chr): name of meta.data slot in Seurat object
alpha (num): alpha level for Wilcoxon-rank test
Returns:
table (data.frame): filtered table based on Wilcoxon-rank test
'
table$lig_diff <- rep(0, length(table[,1]))
table$rec_diff <- rep(0, length(table[,1]))
table$lig_diff_p <- rep(0, length(table[,1]))
table$rec_diff_p <- rep(0, length(table[,1]))
for (i in 1:length(table[,1])) {
ligand <- table$ligands[i]
receptor <- table$receptors[i]
source <- table$source[i]
target <- table$target[i]
lig_0_data <- data_0[which(data_0[,label] == source), ligand]
rec_0_data <- data_0[which(data_0[,label] == target), receptor]
lig_1_data <- data_1[which(data_1[,label] == source), ligand]
rec_1_data <- data_1[which(data_1[,label] == target), receptor]
lig_wilcox <- wilcox.test(lig_0_data, lig_1_data, exact = F, paired = F)
rec_wilcox <- wilcox.test(rec_0_data, rec_1_data, exact = F, paired = F)
table$lig_diff_p[i] <- lig_wilcox$p.value
table$rec_diff_p[i] <- rec_wilcox$p.value
}
table$lig_diff_p_adj <- p.adjust(table$lig_diff_p, method = 'bonferroni')
table$rec_diff_p_adj <- p.adjust(table$rec_diff_p, method = 'bonferroni')
# If not significant, then 0
# If significant, then 1
for (i in 1:length(table[,1])) {
table$lig_diff[i] <- ifelse(table$lig_diff_p_adj[i] < alpha, 1, 0)
table$rec_diff[i] <- ifelse(table$rec_diff_p_adj[i] < alpha, 1, 0)
}
# # If there is difference, then find if increase/decrease
# # for ligands
# for (i in 1:length(table[,1])) {
# if (table$lig_diff[i] == 1) {
# table$lig_diff[i] <- ifelse(table$avg_lig_0[i] > table$avg_lig_1[i], yes = -1, no = 1)
# }
#
# if (table$rec_diff[i] == 1) {
# table$rec_diff[i] <- ifelse(table$avg_rec_0[i] > table$avg_rec_1[i], yes = -1, no = 1)
# }
# }
return(table)
}
generate_supplement <- function(LR_table, cytoscape_nodes) {
'Generate supplemental information for nodes to input back into Cytoscape
1) ligand/receptor 2) ligand/receptor name
Args:
LR_table (data.frame): table with ligand/receptor pairs
cytoscape_ids (data.frame): exported cytoscape node table
Returns:
node_names_ids (data.frame): table with gene names matching IDs and if ligand/receptor
'
# List of node names
ids <- c(as.character(LR_table$source_ID), as.character(LR_table$target_ID))
gene_names <- c(as.character(LR_table$ligands), as.character(LR_table$receptors))
node_names_ids <- data.frame(ID = ids, names = gene_names, LR = rep(c('L', 'R'),each=length(LR_table$source_ID)),stringsAsFactors = F)
node_names_ids <- unique(node_names_ids)
# sort supplemental names in order of what is in cytoscape
cytoscape_ids <- gsub('""', '', cytoscape_nodes$`shared name`)
order <- match(cytoscape_ids, node_names_ids$ID)
node_names_ids <- node_names_ids[order,]
node_names_ids$ID <- paste('"', node_names_ids$ID, '"', sep = '')
return(node_names_ids)
}
# AutomatedClusterMarkerTable returns FindAllMarkers table with extra bits of useful information
# and an educated guess about cluster identity
AutomatedClusterMarkerTable <- function(Seurat_Object){
library(dplyr)
library(tibble)
library(Seurat)
ClusterList <- list()
Idents(object = Seurat_Object) <- "seurat_clusters"
current.cluster.ids <- sort(as.numeric(levels(Seurat_Object@active.ident)))
new.cluster.ids <- c()
for(i in current.cluster.ids){
List_Position <- i + 1
ClusterList[[List_Position]] <- FindMarkers(object = Seurat_Object, ident.1 = i, min.pct = 0.25, only.pos = TRUE)
Positive_Genes <- rownames(ClusterList[[List_Position]])
Num_Positive_Genes <- length(Positive_Genes)
RPS_Num <- length(grep(pattern = "^RPS", x = Positive_Genes))
RPL_Num <- length(grep(pattern = "^RPL", x = Positive_Genes))
RP_Percent <- sum(RPS_Num, RPL_Num)/length(Positive_Genes)*100
RP_Label <- paste("RP%:", RP_Percent, sep = " ")
Mito_Num <- length(grep(pattern = "^MT-", x = Positive_Genes))
Mito_Percent <- Mito_Num/length(Positive_Genes)*100
Mito_Label <- paste("Mito%:", RP_Percent, sep = " ")
ClusterCells <- WhichCells(object = Seurat_Object, idents = i)
Cell_Barcodes <- unlist(Seurat_Object@assays$RNA@counts@Dimnames[2])
Cell_Number <- c()
for(k in 1:length(ClusterCells)){
Cell_Position <- grep(pattern = ClusterCells[k],x = Cell_Barcodes, value = FALSE)
Cell_Number <- c(Cell_Number,Cell_Position)
}
S_Score <- Seurat_Object@meta.data$S.Score
G2M_Score <- Seurat_Object@meta.data$G2M.Score
Cluster_S_Score <- S_Score[Cell_Number]
Cluster_G2M_Score <- G2M_Score[Cell_Number]
Avg_Cluster_S_Score <- mean(Cluster_S_Score)
Avg_Cluster_G2M_Score <- mean(Cluster_G2M_Score)
Cluster_S_Score_Range <- range(Cluster_S_Score)
Cluster_G2M_Score_Range <- range(Cluster_G2M_Score)
nFeature <- Seurat_Object@meta.data$nFeature_RNA
nCount <- Seurat_Object@meta.data$nCount_RNA
Mito <- Seurat_Object@meta.data$percent.mt
Cluster_nFeature <- nFeature[Cell_Number]
Cluster_nCount <- nCount[Cell_Number]
Cluster_Mito <- Mito[Cell_Number]
Avg_Cluster_nFeature <- as.integer(mean(Cluster_nFeature))
Avg_Cluster_nCount <- as.integer(mean(Cluster_nCount))
Max_Cluster_Mito <- max(Cluster_Mito)
Cell_Types <- c("Epi","T Cell","Myeloid","B Cell","Fibroblast","RBC","NK", "Endo","Acinar")
Epi_Markers <- c("KRT7","KRT8","KRT18","KRT19","EPCAM","CDH1")
T_Cell_Markers <- c("CD3E","CD3G","CD3D","CD4","IL7R","CD8A","LEF1")
Myeloid_Markers <- c("CD14","ITGAM","MNDA","MPEG1","ITGAX")
B_Cell_Markers <- c("CD79A","MS4A1","CD19")
Fibroblast_Markers <- c("CDH11","PDGFRA","PDGFRB","ACTA2")
RBC_Markers <- c("HBA1","HBB","HBA2")
NK_Markers <- c("NCR3","FCGR3A","NCAM1","KLRF1","KLRC1","CD38","KLRC1")
Endo_Markers <- c("CDH5","PECAM1")
Acinar_Markers <- c("TRY4","SPINK1","AMY2A")
All_Markers <- list(Epi_Markers,T_Cell_Markers,Myeloid_Markers,B_Cell_Markers,Fibroblast_Markers,RBC_Markers,NK_Markers,Endo_Markers,Acinar_Markers)
Epi_Score <- 0
T_Cell_Score <- 0
Myeloid_Score <- 0
B_Cell_Score <- 0
Fibroblast_Score <- 0
RBC_Score <- 0
NK_Score <- 0
Endo_Score <- 0
Acinar_Score <- 0
All_Scores <- list(Epi_Score,T_Cell_Score,Myeloid_Score,B_Cell_Score,Fibroblast_Score,RBC_Score,NK_Score,Endo_Score,Acinar_Score)
Weighted_Scores <- c()
Score_Weights <- c(1.85,1.85,2.22,3.7,2.78,3.7,1.85,5.56,3.7)
for(h in 1:length(All_Markers)){
Markers_to_Test<- All_Markers[[h]]
Marker_Row <- h
for(j in 1:length(Markers_to_Test)){
Gene_Found <- 0
Gene_Found <- length(grep(pattern = Markers_to_Test[j], x = Positive_Genes))
if(Gene_Found > 0 ){
All_Scores[[Marker_Row]] <- All_Scores[[Marker_Row]]+1
}
}
Weighted_Scores[Marker_Row] <- All_Scores[[Marker_Row]]*Score_Weights[Marker_Row]
}
ClusterID <- which(Weighted_Scores >= 5.5)
if(length(ClusterID) > 0){
if(length(ClusterID) > 1){
ID <- "Multiple"
}else{
ID <- Cell_Types[ClusterID]
}
}else{
ID <- i
}
if(RP_Percent > 30){
ID <- paste("RP_",ID,sep = "")
}
if(Avg_Cluster_S_Score > 0.01 | Avg_Cluster_G2M_Score > 0.01){
CellCycleID <- "Cycling"
ID <- paste("Cycling_",ID,sep = "")
}else{
CellCycleID <- "N/A"
}
if(Avg_Cluster_nCount < 700){
ID <- paste("G_",ID,sep = "")
}
new.cluster.ids <- c(new.cluster.ids,ID)
Label_Row <- length(Positive_Genes) + 1
Label_Row2 <- length(Positive_Genes) + 2
Label_Row3 <- length(Positive_Genes) + 3
Label_Row4 <- length(Positive_Genes) + 4
Label_Row5 <- length(Positive_Genes) + 5
Label1 <- c("Summary:",paste("Cluster",i, sep = " "), paste("ID:",ID, sep = " "),paste("Mito%:",Mito_Percent, sep = " "),paste("RP%:",RP_Percent, sep = " "))
Label2 <- c("Immune Summary",paste("T Cell Score:",All_Scores[[2]], sep = " "),paste("Myeloid Score:",All_Scores[[3]], sep = " "),paste("B Cell Score:",All_Scores[[4]], sep = " "),
paste("NK Score:",All_Scores[[7]], sep = " "))
Label3 <- c(paste("Epi Score:",All_Scores[[1]], sep = " "),paste("Fib Score:",All_Scores[[5]], sep = " "),paste("Acinar Score:",All_Scores[[9]], sep = " "),
paste("Endo Score:",All_Scores[[8]], sep = " "),paste("RBC Score:",All_Scores[[6]], sep = " "))
Label4 <- c("Avg S Score:", Avg_Cluster_S_Score, "Avg G2M Score:", Avg_Cluster_G2M_Score, CellCycleID)
Label5 <- c("Filter Info",paste("Avg. nGene:", Avg_Cluster_nFeature, sep = " "),paste("Avg. nCounts:", Avg_Cluster_nCount, sep = " ")
, paste("Highest Mito:",Max_Cluster_Mito, sep = " "), paste("# Cells:",length(Cell_Number), sep = " "))
ClusterList[[List_Position]][Label_Row,] <- Label1
ClusterList[[List_Position]][Label_Row2,] <- Label2
ClusterList[[List_Position]][Label_Row3,] <- Label3
ClusterList[[List_Position]][Label_Row4,] <- Label4
ClusterList[[List_Position]][Label_Row5,] <- Label5
ClusterList[[List_Position]] <- rownames_to_column(.data = ClusterList[[List_Position]],var = "Gene")
ClusterList[[List_Position]][Label_Row,"Gene"] <- "Summary1"
ClusterList[[List_Position]][Label_Row2,"Gene"] <- "Summary2"
ClusterList[[List_Position]][Label_Row3,"Gene"] <- "Summary3"
ClusterList[[List_Position]][Label_Row4,"Gene"] <- "Summary4"
ClusterList[[List_Position]][Label_Row5,"Gene"] <- "Summary5"
}
ClusterDataFrame <- bind_rows(ClusterList, .id = "column_label")
ClusterDataFrame <- ClusterDataFrame[,-1]
ClusterPackage <- list(ClusterDataFrame, new.cluster.ids)
return(ClusterPackage)
}
#Circos Code
# CircosFunctions will output the text files required to run Circos
#SetIdents to Circos Labels Prior to Starting
CircosFunctions <- function(InteractomeData, SeuratObject, CellTypeVector, POI, Lig_or_Rec, LR_List, Species, Cutoff_Lig, Cutoff_Rec){
#InteractomeData: Filtered Interactome Data
#SeuratObject: Object used to make interactome
#CellTypeVector: Vector of cell types to be included in interactome
#POI: A number representing the cell population of interest to single out as the Ligand in a receptor plot or the receptor in a ligand plot
#Lig_or_Rec: T = POI as Ligand in receptor plot, F = POI as receptor in ligand plot
#LR_List: A dataframe of ligands and paired receptors, ex. The Ramilowski List (organized human, mouse, suborganized ligand-receptor)
#Species: T = Human, F = Mouse
#Cutoff_Lig: Cutoff ligand expression value for the interactome
#Cutoff_Rec: Cutoff receptor expression value for the interactome
#Cutoff Values:
#Positive = Cutoff determined by summary function ex Min, 1st Q, Median, Mean, 3rd Q, Max (1-6)
#Negative = Arbitrary cutoff (set -0.05 for a 0.05 cutoff)
#POI Numbers:
#1 - CD8 T Cells
#2 - CD4 T Cells
#3 - T Cells
#4 - Pericytess
#5 - iCAF Fibroblasts
#6 - Fibroblasts
#7 - Epithelial
#8 - Acinar
#9 - Endothelial
#10 - Mast Cells
#11 - Granulocytes
#12 - Macrophages
#13 - MDSCs
#14 - Myeloid
#15 - NK Cells
#16 - Dendritic Cells
#17 - Endocrine
#18 - Perivascular
#19 - B Cells
CircosFiles <- list()
CellTypes <- c("CD8TCells","CD4TCells","TCells","myCAFFibroblast","iCAFFibroblast","Fibroblasts","Epithelial","Acinar","Endothelial","MastCells",
"Granulocytes","Macrophages","MDSCs","Myeloid","NKCells","DendriticCells","Endocrine","Perivascular","BCells")
CellTypeColors <- c("darkgreen", "limegreen","forestgreen","darkslategray3","darkcyan","darkcyan","red3","deeppink",
"mediumvioletred","gold","orange1","darkorange2", "tan2","darkorange2","darkorchid","chocolate4","darkred","lightskyblue","gold1")
#Karyotype Function
Karyotype_File <- data.frame(
"Chr" = "chr -",
"Name" = "Epithelial",
"Label" = "Epithelial",
"Start" = 0,
"End" = 0,
"Color" = "chr0"
)
LigList_File <- data.frame(
"Chr" = "A",
"Start" = 0,
"End" = 0,
"Name" = "A"
)
RecList_File <- data.frame(
"Chr" = "A",
"Start" = 0,
"End" = 0,
"Name" = "A"
)
DataVec_Source <- c()
for(i in 1:length(CellTypeVector)){
CellType <- CellTypeVector[i]
DataVec_Source <- c(DataVec_Source, which(x = InteractomeData$source == CellType))
}
Data_Source <- InteractomeData[DataVec_Source,]
DataVec_Target <- c()
for(i in 1:length(CellTypeVector)){
CellType <- CellTypeVector[i]
DataVec_Target <- c(DataVec_Target, which(x = Data_Source$target == CellType))
}
Data_Target <- Data_Source[DataVec_Target,]
Data <- Data_Target[which(Data_Target$source != Data_Target$target),]
if(Cutoff_Lig > 0 & Cutoff_Rec > 0){
Data <- Data[which(Data$avg_lig_0 > summary(Data$avg_lig_0)[1] | Data$avg_rec_0 > summary(Data$avg_rec_0)[Cutoff_Rec]),]
}else{
if(Cutoff_Lig < 0 & Cutoff_Lig < 0){
Data <- Data[which(Data$avg_lig_0 > -1*-.01),]
Data <- Data[which(Data$avg_rec_0 > -1*Cutoff_Rec),]
}else{
if(Cutoff_Lig > 0){
Data <- Data[which(Data$avg_lig_0 > summary(Data$avg_lig_0)[Cutoff_Lig]),]
Data <- Data[which(Data$avg_rec_0 > -1*Cutoff_Rec),]
}else{
Data <- Data[which(Data$avg_rec_0 > summary(Data$avg_rec_0)[Cutoff_Rec]),]
Data <- Data[which(Data$avg_lig_0 > -1*Cutoff_Lig),]
}
}
}
for (i in 1:length(table(Data$source))) {
#Determine if population is present after filtering by significance
if( length(which(Data$source == rownames(table(Data$source))[i])) > 0 ){
#Subset Population of Interest
POI_Lig_Data <- Data[which(Data$source == rownames(table(Data$source))[i]),]
POI_Rec_Data <- Data[which(Data$target == rownames(table(Data$source))[i]),]
POI_Lig <- unique(as.character(POI_Lig_Data$ligands))
POI_Rec <- unique(as.character(POI_Rec_Data$receptors))
POI_Lig_Num <- length(POI_Lig)
POI_Rec_Num <- length(POI_Rec)
if(POI_Lig_Num > POI_Rec_Num){
BandSize <- POI_Lig_Num*10-1
}else{
BandSize <- POI_Rec_Num*10-1
}
InsertDF <- data.frame(
"Chr" = "chr -",
"Name" = as.character(rownames(table(Data$source))[i]),
"Label" = as.character(rownames(table(Data$source))[i]),
"Start" = 0,
"End" = BandSize,
"Color" = paste("chr",i,sep = "")
)
Karyotype_File <- merge(x = Karyotype_File, y = InsertDF, by = c("Chr","Name","Label","Start","End","Color"), all.y = T, all.x = T, sort = F)
if(POI_Lig_Num > POI_Rec_Num){
InsertDF_LigList <- data.frame(
"Chr" = rep(x = as.character(rownames(table(Data$source))[i]), times = POI_Lig_Num),
"Start" = seq(from = 0, to = POI_Lig_Num*10-10, by = 10),
"End" = seq(from = 9, to = POI_Lig_Num*10-1, by = 10),
"Name" = POI_Lig
)
InsertDF_RecList <- data.frame(
"Chr" = rep(x = as.character(rownames(table(Data$target))[i]), times = POI_Rec_Num),
"Start" = ceiling(seq(from = 0, to = POI_Lig_Num*10-(POI_Lig_Num*10-10-0)/POI_Rec_Num, by = (POI_Lig_Num*10-1)/POI_Rec_Num)),
"End" = floor(seq(from = (POI_Lig_Num*10-1)/POI_Rec_Num, to = POI_Lig_Num*10-1, by = (POI_Lig_Num*10-1)/POI_Rec_Num)),
"Name" = POI_Rec
)
}else{
InsertDF_LigList <- data.frame(
"Chr" = rep(x = as.character(rownames(table(Data$source))[i]), times = POI_Lig_Num),
"Start" = ceiling(seq(from = 0, to = POI_Rec_Num*10-(POI_Rec_Num*10-10-0)/POI_Lig_Num, by = (POI_Rec_Num*10-1)/POI_Lig_Num)),
"End" = floor(seq(from = (POI_Rec_Num*10-1)/POI_Lig_Num, to = POI_Rec_Num*10-1, by = (POI_Rec_Num*10-1)/POI_Lig_Num)),
"Name" = POI_Lig
)
InsertDF_RecList <- data.frame(
"Chr" = rep(x = as.character(rownames(table(Data$target))[i]), times = POI_Rec_Num),
"Start" = seq(from = 0, to = POI_Rec_Num*10-10, by = 10),
"End" = seq(from = 9, to = POI_Rec_Num*10-1, by = 10),
"Name" = POI_Rec
)
}
Karyotype_File <- merge(x = Karyotype_File, y = InsertDF, by = c("Chr","Name","Label","Start","End","Color"), all.y = T, all.x = T)
LigList_File <- merge(x = LigList_File, y = InsertDF_LigList, by = c("Chr","Start","End","Name"), all.y = T, all.x = T, sort = F)
RecList_File <- merge(x = RecList_File, y = InsertDF_RecList, by = c("Chr","Start","End","Name"), all.y = T, all.x = T, sort = F)
}
}
Karyotype_File <- Karyotype_File[-1,]
LigList_File <- LigList_File[-1,]
RecList_File <- RecList_File[-1,]
sentenceString <- Karyotype_File$Name
searchString <- ' '
replacementString <- ''
sentenceString = sub(searchString,replacementString,sentenceString)
sentenceString = sub(searchString,replacementString,sentenceString)
Karyotype_File$Name <- sentenceString
Karyotype_File$Label <- sentenceString
sentenceString <- LigList_File$Chr
searchString <- ' '
replacementString <- ''
sentenceString = sub(searchString,replacementString,sentenceString)
sentenceString = sub(searchString,replacementString,sentenceString)
LigList_File$Chr <- sentenceString
sentenceString <- RecList_File$Chr
searchString <- ' '
replacementString <- ''
sentenceString = sub(searchString,replacementString,sentenceString)
sentenceString = sub(searchString,replacementString,sentenceString)
RecList_File$Chr <- sentenceString
Band_Color <- c()
for (i in 1:dim(Karyotype_File)[1]) {
CellType <- as.character(Karyotype_File[i,"Name"])
Band_Color <- c(Band_Color, CellTypeColors[which(CellTypes == CellType)])
}
Karyotype_File[,"Color"] <- Band_Color
LigList <- LigList_File
RecList <- RecList_File
if(Species == T){
LigRecList <- LR_List[,1:2]
}else{
LigRecList <- LR_List[,3:4]
}
#Expression Function
LigExpression <- data.frame(
"Chr" = "A",
"Name" = "A",
"Expression" = 0
)
RecExpression <- data.frame(
"Chr" = "A",
"Name" = "A",
"Expression" = 0
)
Avg_Expression <- AverageExpression(object = SeuratObject, features = c(LigList$Name,RecList$Name))
Avg_Expression <- Avg_Expression[[1]]
for(i in 1:length(CellTypeVector)){
CellSearch <- CellTypeVector[i]
sentenceString <- CellSearch
searchString <- ' '
replacementString <- ''
sentenceString = sub(searchString,replacementString,sentenceString)
sentenceString = sub(searchString,replacementString,sentenceString)
CellSearch <- sentenceString
ident1 <- CellTypeVector[i]
CellExpression <- subset.data.frame(x = Avg_Expression, select = ident1)
LigFeatures <- as.character(LigList[which(LigList$Chr == CellSearch),"Name"])
RecFeatures <- as.character(RecList[which(RecList$Chr == CellSearch),"Name"])
Lig_DE <- data.frame(
"Exp" = 0,
"Name" = "a"
)
Rec_DE <- data.frame(
"Exp" = 0,
"Name" = "a"
)
for(k in 1:length(LigFeatures)){
Lig_Exp <- subset.data.frame(x = CellExpression, subset = rownames(Avg_Expression) == LigFeatures[k])
Lig_Exp[1,2] <- LigFeatures[k]
colnames(Lig_Exp)[1] <- "Exp"
colnames(Lig_Exp)[2] <- "Name"
Lig_DE <- merge(x = Lig_DE, y = Lig_Exp, by = c("Exp","Name"), all.y = T, all.x = T, sort = F)
}
Lig_DE <- Lig_DE[-1,]
for(t in 1:length(RecFeatures)){
Rec_Exp <- subset.data.frame(x = CellExpression, subset = rownames(Avg_Expression) == RecFeatures[t])
Rec_Exp[1,2] <- RecFeatures[t]
colnames(Rec_Exp)[1] <- "Exp"
colnames(Rec_Exp)[2] <- "Name"
Rec_DE <- merge(x = Rec_DE, y = Rec_Exp, by = c("Exp","Name"), all.y = T, all.x = T, sort = F)
}
Rec_DE <- Rec_DE[-1,]
Lig_DF <- data.frame(
"Chr" = rep(x = CellTypeVector[i], times = dim(Lig_DE)[1]),
"Name" = Lig_DE$Name,
"Expression" = Lig_DE$Exp
)
Rec_DF <- data.frame(
"Chr" = rep(x = CellTypeVector[i], times = dim(Rec_DE)[1]),
"Name" = Rec_DE$Name,
"Expression" = Rec_DE$Exp
)
LigExpression <- merge(x = LigExpression, y = Lig_DF, by = c("Chr","Name","Expression"), all.y = T, all.x = T, sort = F)
RecExpression <- merge(x = RecExpression, y = Rec_DF, by = c("Chr","Name","Expression"), all.y = T, all.x = T, sort = F)
}
LigExpression <- LigExpression[-1,]
RecExpression <- RecExpression[-1,]
sentenceString <- LigExpression$Chr
searchString <- ' '
replacementString <- ''
sentenceString = sub(searchString,replacementString,sentenceString)
sentenceString = sub(searchString,replacementString,sentenceString)
LigExpression$Chr <- sentenceString
sentenceString <- RecExpression$Chr
searchString <- ' '
replacementString <- ''
sentenceString = sub(searchString,replacementString,sentenceString)
sentenceString = sub(searchString,replacementString,sentenceString)
RecExpression$Chr <- sentenceString
ExpressionOutput <- merge(x = LigExpression, y = RecExpression, by = c("Chr","Name","Expression"), all.y = T, all.x = T, sort = F)
#Squeeze expression values
ExpressionInput <- ExpressionOutput
Normalized_Expression <- log(ExpressionOutput$Expression*1000)
ScaledExpression <- ((Normalized_Expression - range(Normalized_Expression)[1])/
(range(Normalized_Expression)[2]- range(Normalized_Expression)[1]))*4
ExpressionInput$Expression <- ScaledExpression
#Text and Link Function
CellNumCounter <- 1
if(Lig_or_Rec == T){
if(POI == 1){
CellType <- CellTypes[1]
CellTypeColor <- CellTypeColors[1]
}else{
while (POI != CellNumCounter) {
CellNumCounter <- CellNumCounter + 1
}
CellType <- CellTypes[CellNumCounter]
CellTypeColor <- CellTypeColors[CellNumCounter]
}
POI_Lig_Data <- LigList[which(LigList$Chr == CellType),]
POI_Rec_Data <- RecList[which(RecList$Chr != CellType),]
Text_File <- merge(x = POI_Lig_Data, y = POI_Rec_Data, by = c("Chr","Start","End","Name"), all.y = T, all.x = T, sort = F)
Fil_LigRecList_Vec <- c()
for(i in 1:dim(LigRecList)[1]){
LigRecList_Lig <- as.character(LigRecList[i,1])
LigRecList_Rec <- as.character(LigRecList[i,2])
Lig_Present <- grep(pattern = paste("^", LigRecList_Lig,"$", sep = ""), x = POI_Lig_Data$Name)
Rec_Present <- grep(pattern = paste("^", LigRecList_Rec,"$", sep = ""), x = POI_Rec_Data$Name)
LigList_Row <- which(LigRecList[,1] == LigRecList_Lig)
RecList_Row <- which(LigRecList[,2] == LigRecList_Rec)
if(length(Lig_Present) > 0 & length(Rec_Present) > 0){
Fil_LigRecList_Vec <- c(Fil_LigRecList_Vec, intersect(LigList_Row,RecList_Row))
}
}
LigRecList_Fil <- LigRecList[Fil_LigRecList_Vec,]
Link_File <- data.frame(
"Chr" = "A",
"Start" = 0,
"End" = 0,
"Chr1" = "A",
"Start1" = 0,
"End1" = 0
)
for (i in 1:length(POI_Lig_Data$Name)) {
Rec_Data <- data.frame(
"Chr" = "A",
"Start" = 0,
"End" = 0
)
Lig <- as.character(POI_Lig_Data$Name[i])
Receptors <- as.character(LigRecList_Fil[,2][grep(x = LigRecList_Fil[,1], pattern = paste("^", Lig,"$", sep = ""),value = F)])
if(length(Receptors) == 1){
Rec_Data_Insert <- POI_Rec_Data[grep(x = POI_Rec_Data$Name, pattern = paste("^", Receptors,"$", sep = ""),value = F),1:3]
Rec_Data <- merge(x = Rec_Data, y = Rec_Data_Insert, by = c("Chr","Start","End"), all.y = T, all.x = T, sort = F)
}else{
for (l in 1:length(Receptors)) {
Receptor <- Receptors[l]
Rec_Data_Insert <- POI_Rec_Data[grep(x = POI_Rec_Data$Name, pattern = paste("^", Receptor,"$", sep = ""),value = F),1:3]
Rec_Data <- merge(x = Rec_Data, y = Rec_Data_Insert, by = c("Chr","Start","End"), all.y = T, all.x = T, sort = F)
}
}
Rec_Data <- Rec_Data[-1,]
Lig_Data <- POI_Lig_Data[i,1:3]
if(length(Rec_Data$Chr) > 1){
Lig_Data <- rbind(Lig_Data, Lig_Data[rep(1, length(Rec_Data$Chr)-1), ])
}
Lig_Rec_Merge <- bind_cols(x = Lig_Data, y = Rec_Data)
Link_File <- rbind(x = Link_File, y = Lig_Rec_Merge)
}
Link_File <- Link_File[-1,]
Link_File[,(dim(Link_File)[2]+1)] <-rep(x = paste("color=",CellTypeColor,sep = ""), times = dim(Link_File)[1])
Expression_File <- Text_File
Expression_File[,5] <- rep(0, times = dim(Expression_File)[1])
for(k in 1:dim(Expression_File)[1]){
GeneCell <- Expression_File[k,"Chr"]
Gene <- as.character(Expression_File[k,"Name"])
ExpressionPull <- ExpressionInput[which(ExpressionInput$Chr == GeneCell & ExpressionInput$Name == Gene),]
Expression_File[k,5] <- ExpressionPull$Expression
}
Expression_File <- Expression_File[,-4]
}else{
if(POI == 1){
CellType <- CellTypes[1]
}else{
while (POI != CellNumCounter) {
CellNumCounter <- CellNumCounter + 1
}
CellType <- CellTypes[CellNumCounter]
CellTypeColor <- CellTypeColors[CellNumCounter]
}
POI_Lig_Data <- LigList[which(LigList$Chr != CellType),]
POI_Rec_Data <- RecList[which(RecList$Chr == CellType),]
Text_File <- merge(x = POI_Rec_Data, y = POI_Lig_Data, by = c("Chr","Start","End","Name"), all.y = T, all.x = T, sort = F)
Fil_LigRecList_Vec <- c()
for(i in 1:dim(LigRecList)[1]){
LigRecList_Lig <- as.character(LigRecList[i,1])
LigRecList_Rec <- as.character(LigRecList[i,2])
Lig_Present <- grep(pattern = paste("^", LigRecList_Lig,"$", sep = ""), x = POI_Lig_Data$Name)
Rec_Present <- grep(pattern = paste("^", LigRecList_Rec,"$", sep = ""), x = POI_Rec_Data$Name)
LigList_Row <- which(LigRecList[,1] == LigRecList_Lig)
RecList_Row <- which(LigRecList[,2] == LigRecList_Rec)
if(length(Lig_Present) > 0 & length(Rec_Present) > 0){
Fil_LigRecList_Vec <- c(Fil_LigRecList_Vec, intersect(LigList_Row,RecList_Row))
}
}
LigRecList_Fil <- LigRecList[Fil_LigRecList_Vec,]
Link_File <- data.frame(
"Chr" = "A",
"Start" = 0,
"End" = 0,
"Chr1" = "A",
"Start1" = 0,
"End1" = 0
)
for (i in 1:length(POI_Rec_Data$Name)) {
Lig_Data <- data.frame(
"Chr" = "A",
"Start" = 0,
"End" = 0
)
Rec <- as.character(POI_Rec_Data$Name[i])
Ligands <- as.character(LigRecList_Fil[,1][grep(x = LigRecList_Fil[,2], pattern = paste("^", Rec,"$", sep = ""),value = F)])
if(length(Ligands == 1)){
Lig_Data_Insert <- POI_Lig_Data[grep(x = POI_Lig_Data$Name, pattern = paste("^", Ligands,"$", sep = ""),value = F),1:3]
Lig_Data <- merge(x = Lig_Data, y = Lig_Data_Insert, by = c("Chr","Start","End"), all.y = T, all.x = T, sort = F)
}else{
for (l in 1:length(Ligands)) {
Ligand <- Ligands[l]
Lig_Data_Insert <- POI_Lig_Data[grep(x = POI_Lig_Data$Name, pattern = paste("^", Ligand,"$", sep = ""),value = F),1:3]
Lig_Data <- merge(x = Lig_Data, y = Lig_Data_Insert, by = c("Chr","Start","End"), all.y = T, all.x = T, sort = F)
}
}
Lig_Data <- Lig_Data[-1,]
Rec_Data <- POI_Rec_Data[i,1:3]
if(length(Lig_Data$Chr) > 1){
Rec_Data <- rbind(Rec_Data, Rec_Data[rep(1, length(Lig_Data$Chr)-1), ])
}
Lig_Rec_Merge <- bind_cols(x = Rec_Data, y = Lig_Data)
Link_File <- rbind(x = Link_File, y = Lig_Rec_Merge)
}
Link_File <- Link_File[-1,]
Link_Color <- c()
for (i in 1:dim(Link_File)[1]) {
CellType <- as.character(Link_File[i,"Chr1"])
Link_Color <- c(Link_Color, CellTypeColors[which(CellTypes == CellType)])
}
Link_Color <- paste("color=",Link_Color,"_a4",sep = "")
Link_File[,(dim(Link_File)[2]+1)] <- Link_Color