-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneClusteringDjAndColonStrongEffects.R
147 lines (123 loc) · 4.26 KB
/
geneClusteringDjAndColonStrongEffects.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
library(tidyverse)
library(ComplexHeatmap)
fullgenesFit = read_tsv('fullGenesTab.tsv')
metadata = read_tsv('fullbarseqMeta.txt')
fitnessScoresMeans=fullgenesFit %>%
select(sample, locusId, fitnessScore, day, tissue) %>%
distinct()%>%
group_by(tissue, day, locusId)%>%
summarise('meanGeneFitness' = mean(fitnessScore))
strongEffects=fitnessScoresMeans %>%
filter(abs(meanGeneFitness) >= 5)%>%
ungroup() %>%
select(locusId)%>%
distinct()
nrow(strongEffects)
fitnessScoresMat=fullgenesFit %>%
filter(locusId %in% strongEffects$locusId)%>%
filter(tissue != 'T0')%>%
select(mouseDayTissue, locusId, fitnessScore) %>%
distinct()%>%
pivot_wider(names_from = 'mouseDayTissue',id_cols = 'locusId', values_from = 'fitnessScore')
functions =fullgenesFit %>%
select(locusId, name, desc,kofamAccession,kofamAccession, pfamAcession, pfamFunction) %>%
distinct()
fitnessScoresMat%>%
column_to_rownames('locusId')%>%
Heatmap(show_row_names = F)
wss <- (nrow(fitnessScoresMat) - 1) * sum(apply(column_to_rownames(fitnessScoresMat,'locusId'), 2, var))
# Loop through cluster numbers from 2 to 20 to calculate WSS for each k-means solution
for (i in 2:20) {
# Run k-means clustering with 'i' clusters and store the WSS for the solution
wss[i] <- sum(kmeans(column_to_rownames(fitnessScoresMat,'locusId'),
centers = i)$withinss)
}
plot(1:20, wss, type = "b", xlab = "Number of Clusters", ylab = "Within groups sum of squares")
clusters=fitnessScoresMat%>%
column_to_rownames('locusId')%>%
kmeans(centers = 4)
clusterMembership=clusters$cluster%>%
as.data.frame()
colnames(clusterMembership) = c('cluster')
clusterMembership=clusterMembership %>%
rownames_to_column('locusId')
clusterMembership %>%
group_by(cluster)%>%
summarise('clusterSize'= n())
cluster1=clusterMembership %>%
filter(cluster == 1) %>%
merge(functions, by = 'locusId')
fullgenesFit %>%
select(sample, locusId, fitnessScore, numericDay, tissue) %>%
filter(locusId %in% cluster1$locusId,
tissue != 'T0')%>%
distinct() %>%
group_by(tissue, locusId, numericDay)%>%
summarise('geneMeanFitScore' = mean(fitnessScore))%>%
ggplot(aes(x = numericDay,
col = locusId,
group = locusId,
y = geneMeanFitScore,))+
geom_point()+
geom_line()+
facet_wrap(~tissue)+
theme(legend.position = 'none')+
labs(title = 'Cluster 1 - Strong positive impact')
cluster2=clusterMembership %>%
filter(cluster == 2) %>%
merge(functions, by = 'locusId')
fullgenesFit %>%
select(sample, locusId, fitnessScore, numericDay, tissue) %>%
filter(locusId %in% cluster2$locusId,
tissue != 'T0')%>%
distinct() %>%
group_by(tissue, locusId, numericDay)%>%
summarise('geneMeanFitScore' = mean(fitnessScore))%>%
ggplot(aes(x = numericDay,
col = locusId,
group = locusId,
y = geneMeanFitScore,))+
geom_point()+
geom_line()+
facet_wrap(~tissue)+
theme(legend.position = 'none')+
labs(title = 'cluster 2 - weak fitness impacts')
cluster3=clusterMembership %>%
filter(cluster == 3) %>%
merge(functions, by = 'locusId')
fullgenesFit %>%
select(sample, locusId, fitnessScore, numericDay, tissue) %>%
filter(locusId %in% cluster3$locusId,
tissue != 'T0')%>%
distinct() %>%
group_by(tissue, locusId, numericDay)%>%
summarise('geneMeanFitScore' = mean(fitnessScore))%>%
ggplot(aes(x = numericDay,
col = locusId,
group = locusId,
alpha = .1,
y = geneMeanFitScore,))+
geom_point()+
geom_line()+
facet_wrap(~tissue)+
theme(legend.position = 'none')+
labs(title = 'Cluster 3 - Strong positive early, weakly negative late')
cluster4=clusterMembership %>%
filter(cluster == 4) %>%
merge(functions, by = 'locusId')
fullgenesFit %>%
select(sample, locusId, fitnessScore, numericDay, tissue) %>%
filter(locusId %in% cluster4$locusId,
tissue != 'T0')%>%
distinct() %>%
group_by(tissue, locusId, numericDay)%>%
summarise('geneMeanFitScore' = mean(fitnessScore))%>%
ggplot(aes(x = numericDay,
col = locusId,
group = locusId,
y = geneMeanFitScore,))+
geom_point()+
geom_line()+
facet_wrap(~tissue)+
theme(legend.position = 'none')+
labs('Cluster 4 - strong negative colonic')