-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheatmap.R
177 lines (159 loc) · 5.8 KB
/
heatmap.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
source("blackflies.R")
here::i_am("heatmap.R")
# Prep
clean_viruses <- taxonomy_df$rowname
clean_OTU <- OTU %>%
filter(rownames(.) %in% clean_viruses) %>%
select(-contains("NC"))
joined_df <- left_join(
clean_OTU %>% rownames_to_column(),
taxonomy_df
)
tpm_scaled <- joined_df %>%
mutate(length = as.numeric(str_extract(rowname, "(?<=length_)\\d+"))) %>%
mutate(
across(starts_with("BlackFly"), ~ .x / (length / 1000)),
across(starts_with("BlackFly"), ~ .x / sum(.x))
)
# Raw counts
ra_p <- joined_df %>%
mutate(across(starts_with("BlackFly"), ~ .x / sum(.x))) %>%
select(starts_with("BlackFly"), Realm, rowname) %>%
pivot_longer(cols = starts_with("Blackfly"), names_to = "Sample", values_to = "Read_Count") %>%
filter(Read_Count > 0) %>%
mutate(Realm = if_else(is.na(Realm), "Unclassified", Realm)) %>%
ggplot(aes(x = Realm, y = Read_Count, color = Realm, fill = Realm)) +
ggdist::stat_halfeye(
## custom bandwidth
adjust = .5,
## adjust height
width = .2,
## move geom to the right
justification = -.6,
## remove slab interval
.width = 0,
point_colour = NA,
show.legend = F
) +
geom_boxplot(
width = .15,
## remove outliers
outlier.shape = NA, ## `outlier.shape = NA` or `outlier.alpha = 0` works as well
) +
## add dot plots from {ggdist} package
ggdist::stat_dots(
## orientation to the left
side = "left",
## move geom to the left
justification = 1.12,
## adjust grouping (binning) of observations
binwidth = 0.03,
show.legend = F
) +
scale_y_log10(breaks = c(0, 0.000001, 0.00001, 0.0001, 0.001, 0.01, 0.1, 1),
labels = c(0, 0.0001, 0.001, 0.01, 0.1, 1, 10, 100)) +
labs(y = "Relative abundance (%)") +
# ggpubr::stat_pwc(hide.ns = T)+
scale_fill_manual(values = alpha(realm_col, .3)) +
scale_color_manual(values = realm_col) +
theme_classic() +
coord_cartesian(xlim = c(1.2, 3.9), clip = "off")
# TPM scaled
tpm_p <- tpm_scaled %>%
select(starts_with("BlackFly"), Realm, rowname) %>%
pivot_longer(cols = starts_with("Blackfly"), names_to = "Sample", values_to = "TPM") %>%
# group_by(rowname) %>%
# filter(TPM == max(TPM)) %>%
# ungroup() %>%
filter(TPM > 0) %>%
mutate(Realm = if_else(is.na(Realm), "Unclassified", Realm)) %>%
ggplot(aes(x = Realm, y = TPM, color = Realm, fill = Realm)) +
ggdist::stat_halfeye(
## custom bandwidth
adjust = .5,
## adjust height
width = .2,
## move geom to the right
justification = -.6,
## remove slab interval
.width = 0,
point_colour = NA,
show.legend = F
) +
geom_boxplot(
width = .15,
## remove outliers
outlier.shape = NA, ## `outlier.shape = NA` or `outlier.alpha = 0` works as well
) +
## add dot plots from {ggdist} package
ggdist::stat_dots(
## orientation to the left
side = "left",
## move geom to the left
justification = 1.12,
## adjust grouping (binning) of observations
binwidth = 0.025,
show.legend = F
) +
scale_y_log10(breaks = c(0, 0.00001, 0.0001, 0.001, 0.01, 0.1, 1),
labels = c(0, 0.001, 0.01, 0.1, 1, 10, 100)) +
labs(y = "Length normalized\nrelative abundance (%)") +
# ggpubr::stat_pwc(hide.ns = T, method = "wilcox.test", p.adjust.method = "BH") +
scale_fill_manual(values = alpha(realm_col, .3)) +
scale_color_manual(values = realm_col) +
theme_classic() +
coord_cartesian(xlim = c(1.2, 3.9), clip = "off")
tpm_p / ra_p
ggsave("figures/raincloud.pdf", dpi=300, height = 5, width = 10)
tpm_p
ggsave(plot = tpm_p, filename = "figures/tpm_realm.pdf", dpi = 300, height = 5, width = 10)
# Heatmap
source("draw_blackfly_heatmap.R")
# Raw counts
grouped_order_df <- joined_df %>%
select(starts_with("BlackFly"), Realm, Kingdom, Phylum, Class, Order, Family) %>%
group_by(Realm, Kingdom, Phylum, Class, Order, Family) %>%
summarize(across(all_of(starts_with("BlackFly")), ~sum(.)),
n=n()) %>%
ungroup() %>%
mutate(across(!starts_with("BlackFly"), ~replace_na(., "unclassified")))
draw_blackfly_heatmap(grouped_order_df)
# TPM
tpm_order_df <- tpm_scaled %>%
select(starts_with("BlackFly"), Realm, Kingdom, Phylum, Class, Order, Family) %>%
group_by(Realm, Kingdom, Phylum, Class, Order, Family) %>%
summarize(across(all_of(starts_with("BlackFly")), ~ sum(.)),
n = n()
) %>%
ungroup() %>%
mutate(across(!starts_with("BlackFly"), ~ replace_na(., "unclassified")))
draw_blackfly_heatmap(tpm_order_df, log2=F, title="Length normalized RA", legend_side = "bottom")
pdf("figures/blackflies_heatmap_bottom_legend.pdf", width = 16.6, height = 17)
draw_blackfly_heatmap(grouped_order_df, legend_side = "bottom")
dev.off()
tiff("figures/tiff/figure3.tiff", width = 16.6, height = 17, units = "in", res = 300)
draw(hm,
annotation_legend_list = pd,
annotation_legend_side = "bottom", heatmap_legend_side = "bottom",
merge_legend = T, legend_grouping = "original"
)
dev.off()
# Eukaryotic viral families
hm_anno_df %>%
filter(Family != "unclassified", Realm != "Duplodnaviria", Class != "Leviviricetes", !Family %in% c("Picobirnaviridae", "Cystoviridae")) %>%
count()
# Eukaryotic viruses not classified onto Family level
hm_anno_df %>%
filter(Family == "unclassified", Realm != "Duplodnaviria", Class != "Leviviricetes", !Family %in% c("Picobirnaviridae", "Cystoviridae")) %>%
count()
# Prokaryotic viruses
hm_anno_df %>%
filter(Realm == "Duplodnaviria" | Class == "Leviviricetes" | Family %in% c("Picobirnaviridae", "Cystoviridae")) %>%
count()
# Classified eukaryotic RNA viruses
hm_anno_df %>%
filter(Realm == "Riboviria", Family != "unclassified", Class != "Leviviricetes", !Family %in% c("Picobirnaviridae", "Cystoviridae")) %>%
count()
hm_anno_df %>%
filter(Realm == "Riboviria", Family != "unclassified", Class != "Leviviricetes", !Family %in% c("Picobirnaviridae", "Cystoviridae")) %>%
count()