-
Notifications
You must be signed in to change notification settings - Fork 0
/
differentialInteraction.R
130 lines (97 loc) · 4.02 KB
/
differentialInteraction.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
# Function to perform multiple t-tests to compare interaction scores between clusters
# Create function
differentialInteraction <- function(InteractionMatList,
MetaList,
Attribute,
Comparison) {
# Make a list for results to go in:
diffIntRes <- list()
# Perform for RECEIVERS first ----
message('RECEIVERS first...')
# Transpose the interaction mat
interactionDF <- as.data.frame(t(InteractionMatList$ReceiverMat))
# Add cell as column in meta and count mat
interactionDF <- interactionDF %>%
rownames_to_column(var = 'cell')
diffIntMeta2 <- MetaList$ReceiverMeta %>%
rownames_to_column(var = 'cell')
# Now join meta and column
tTestDF <- interactionDF %>%
left_join(diffIntMeta2, by = 'cell')
# Subset down to clusters we want to compare
tTestDFFilt <- tTestDF %>%
filter(!!as.symbol(Attribute) %in% Comparison)
# Check if there are enough replicates of groups
if (sum(tTestDFFilt$Cluster == Comparison[1]) > 3 &
sum(tTestDFFilt$Cluster == Comparison[2]) > 3) {
message('Starting statistical test for RECEIVERS.')
# Make dataframe for results
wilcoxRes <- data.frame()
# For loop to perform test on all columns
for (x in c(2:(ncol(tTestDFFilt) - ncol(MetaList$ReceiverMeta)))) {
# As data is not normally distributed - use wilcoxon
res <-
wilcox.test(tTestDFFilt[, x] ~ tTestDFFilt[, Attribute],
data = tTestDFFilt,
paired = F)
# Adjust the p-value
FDR <-
p.adjust(res$p.value, method = 'fdr', n = length(c(2:(
ncol(tTestDFFilt) - ncol(MetaList$ReceiverMeta)
))))
# Put result into dataframe
wilcoxRes[colnames(tTestDFFilt)[x], 'Receptor-Ligand'] <-
colnames(tTestDFFilt)[x]
wilcoxRes[colnames(tTestDFFilt)[x], 'P-Value'] <- res$p.value
wilcoxRes[colnames(tTestDFFilt)[x], 'FDR'] <- FDR
}
diffIntRes$ReceiverResults <- wilcoxRes
} else {
message('Not enough replicates in groups for statistical test. Skipping RECEIVERS.')
}
# Now perform for SENDERS ----
message('SENDERS second...')
# Transpose the interaction mat
interactionDF <- as.data.frame(t(InteractionMatList$SenderMat))
# Add cell as column in meta and count mat
interactionDF <- interactionDF %>%
rownames_to_column(var = 'cell')
diffIntMeta2 <- MetaList$SenderMeta %>%
rownames_to_column(var = 'cell')
# Now join meta and column
tTestDF <- interactionDF %>%
left_join(diffIntMeta2, by = 'cell')
# Subset down to clusters we want to compare
tTestDFFilt <- tTestDF %>%
filter(!!as.symbol(Attribute) %in% Comparison)
# Check if there are enough replicates of groups
if (sum(tTestDFFilt$Cluster == Comparison[1]) > 3 &
sum(tTestDFFilt$Cluster == Comparison[2]) > 3) {
message('Starting statistical test for SENDERS.')
# Make dataframe for results
wilcoxRes <- data.frame()
# For loop to perform test on all columns
for (x in c(2:(ncol(tTestDFFilt) - ncol(MetaList$SenderMeta)))) {
# As data is not normally distributed - use wilcoxon
res <-
wilcox.test(tTestDFFilt[, x] ~ tTestDFFilt[, Attribute],
data = tTestDFFilt,
paired = F)
# Adjust the p-value
FDR <-
p.adjust(res$p.value, method = 'fdr', n = length(c(2:(
ncol(tTestDFFilt) - ncol(MetaList$SenderMeta)
))))
# Put result into dataframe
wilcoxRes[colnames(tTestDFFilt)[x], 'Receptor-Ligand'] <-
colnames(tTestDFFilt)[x]
wilcoxRes[colnames(tTestDFFilt)[x], 'P-Value'] <- res$p.value
wilcoxRes[colnames(tTestDFFilt)[x], 'FDR'] <- FDR
}
diffIntRes$SenderResults <- wilcoxRes
} else {
message('Not enough replicates in groups for statistical test. Skipping SENDERS.')
}
# Now return list
return(diffIntRes)
}