-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAMR_deseq.Rmd
385 lines (287 loc) · 15.1 KB
/
AMR_deseq.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
---
title: "AMR_deseq2"
author: "J. Meyer"
date: "2023-06-06"
output: html_document
---
Prepare data for DESeq2: 1. Count data should be formatted with genes in rows and samples in columns. 2. Metadata for the samples should be formatted with samples in rows and metadata in columns.
```{r, echo=FALSE}
counts <- read.table("20-samples-Bact-cds-counts.txt",sep="\t",header=TRUE)
# DeSeq is looking for only counts, without a first column of gene names, make the gene names row names.
mat <- counts[,-1]
rownames(mat) <- counts[,1]
# The Salmon counts have decimal places - round them up to prevent issues in deseq
counts2<-round(mat,digits=0)
# remove the disease samples from this analysis
counts2$AMR1D <- NULL
counts2$AMR2D <- NULL
counts2$AMR3D <- NULL
counts2$AMR5D <- NULL
counts2$AMR7D <- NULL
counts2$AMR8D <- NULL
counts2$AMR9D <- NULL
counts3 <-counts2[rowSums(counts2[])>0,] # remove empty rows
# use counts3 as input for DESeq2
counts4 <- counts3
counts4$row_names <- row.names(counts4) # add back transcript.id as a column to merge with metadata
colnames(counts4)[14] <- 'transcript_id'
rgi <- read.table("rgi_out.txt",sep="\t",header=TRUE,quote="")
rgi_counts <-merge(rgi,counts4,by="transcript_id")
write.table(rgi_counts,"Untreated-vs-antibiotic_RGI-counts.txt",sep="\t",col.names=NA)
meta <-read.table("metadata.txt",sep="\t",header=TRUE)
# remove disease samples to compare just before and after antibiotic treatment
meta2 <-meta[meta$type != "disease",]
write.table(meta2,"metadata_13.txt",sep="\t",col.names=NA)
```
Summarize RGI genes by AMR Gene Family, Drug Class, and Resistance Mechanism in untreated vs antibiotic colonies; prepare data for DESeq2 analysis
```{r, echo=FALSE}
library(dplyr)
rgi_counts <- read.table("Untreated-vs-antibiotic_RGI-counts.txt",sep="\t",header=TRUE)
rgi_counts[1] <- NULL
#combine individual gene counts to get counts by AMR.Gene.Family
amrfam <- rgi_counts %>% group_by(AMR.Gene.Family) %>% summarize_at(vars(AMR1A:AMR10A), sum)
write.table(amrfam,"Untreated-vs-antibiotic_AMRgenefamily-counts.txt",sep="\t",col.names=NA)
#format for DESeq2
amrfam2 <- as.data.frame(amrfam)
rownames(amrfam2) <- amrfam2[,1]
amrfam2$AMR.Gene.Family <- NULL
# use amrfam2 as input for DESeq2
#combine individual gene counts to get counts by Drug.Class
drug <- rgi_counts %>% group_by(Drug.Class) %>% summarize_at(vars(AMR1A:AMR10A), sum)
write.table(drug,"Untreated-vs-antibiotic_DrugClass-counts.txt",sep="\t",col.names=NA)
#format for DESeq2
drug2 <- as.data.frame(drug)
rownames(drug2) <- drug2[,1]
drug2$Drug.Class <- NULL
# use drug2 as input for DESeq2
#combine individual gene counts to get counts by Resistance.Mechanism
mech <- rgi_counts %>% group_by(Resistance.Mechanism) %>% summarize_at(vars(AMR1A:AMR10A), sum)
write.table(mech,"Untreated-vs-antibiotic_ResistanceMechanism-counts.txt",sep="\t",col.names=NA)
#format for DESeq2
mech2 <- as.data.frame(mech)
rownames(mech2) <- mech2[,1]
mech2$Resistance.Mechanism <- NULL
# use mech2 as input for DESeq2
```
Run DESeq2 to test for differentially expressed *individual RGI genes* between coral colonies immediately before treatment ("untreated") and one day after amoxicillin treatment ("antibiotic").
```{r, echo=FALSE}
library(DESeq2)
meta <-read.table("metadata_13.txt",sep="\t",header=TRUE)
meta$X <- NULL
meta$type <-factor(meta$type)
# DESeq2 is looking for only counts, without a first column of gene names, make the gene names row names.
# This is testing all individual RGI genes
#construct DESEQDataSet Object
dds <- DESeqDataSetFromMatrix(countData=counts3,
colData=meta,
design=~type)
#see what the object looks like
dds
#now run DESeq function
dds <- DESeq(dds)
resultsNames(dds) # lists the coefficients
res <- results(dds, name="type_before_vs_after")
summary(res)
sink("DESeq2_summary_transcriptid.txt")
print(summary(res))
sink()
sink("DESeq2_results_transcriptid.txt")
print(res)
sink()
```
Run DESeq2 to test for differentially expressed *AMR gene families* between coral colonies immediately before treatment ("untreated") and one day after amoxicillin treatment ("antibiotic").
```{r, echo=FALSE}
library(DESeq2)
meta <-read.table("metadata_13.txt",sep="\t",header=TRUE)
meta$X <- NULL
meta$type <-factor(meta$type)
# DESeq2 is looking for only counts, without a first column of gene names, make the gene names row names.
# This is testing differential expression of AMR gene families
#construct DESEQDataSet Object
dds <- DESeqDataSetFromMatrix(countData=amrfam2,
colData=meta,
design=~type)
#see what the object looks like
dds
#now run DESeq function
dds <- DESeq(dds)
resultsNames(dds) # lists the coefficients
res <- results(dds, name="type_before_vs_after")
summary(res)
sink("DESeq2_results_AMRgenefamily.txt")
print(res)
sink()
sink("DESeq2_summary_AMRgenefamily.txt")
print(summary(res))
sink()
```
Run DESeq2 to test for differentially expressed *Drug Class* between coral colonies immediately before treatment ("untreated") and one day after amoxicillin treatment ("antibiotic").
```{r, echo=FALSE}
library(DESeq2)
meta <-read.table("metadata_13.txt",sep="\t",header=TRUE)
meta$X <- NULL
meta$type <-factor(meta$type)
# DESeq2 is looking for only counts, without a first column of gene names, make the gene names row names.
# This is testing differential expression of AMR gene families
#construct DESEQDataSet Object
dds <- DESeqDataSetFromMatrix(countData=drug2,
colData=meta,
design=~type)
#see what the object looks like
dds
#now run DESeq function
dds <- DESeq(dds)
resultsNames(dds) # lists the coefficients
res <- results(dds, name="type_before_vs_after")
summary(res)
sink("DESeq2_results_DrugClass.txt")
print(res)
sink()
sink("DESeq2_summary_DrugClass.txt")
print(summary(res))
sink()
```
Run DESeq2 to test for differentially expressed *Resistance Mechanism* between coral colonies immediately before treatment ("untreated") and one day after amoxicillin treatment ("antibiotic").
```{r, echo=FALSE}
library(DESeq2)
meta <-read.table("metadata_13.txt",sep="\t",header=TRUE)
meta$X <- NULL
meta$type <-factor(meta$type)
# DESeq2 is looking for only counts, without a first column of gene names, make the gene names row names.
# This is testing differential expression of AMR gene families
#construct DESEQDataSet Object
dds <- DESeqDataSetFromMatrix(countData=mech2,
colData=meta,
design=~type)
#see what the object looks like
dds
#now run DESeq function
dds <- DESeq(dds)
resultsNames(dds) # lists the coefficients
res <- results(dds, name="type_before_vs_after")
summary(res)
sink("DESeq2_results_ResistanceMechanism.txt")
print(res)
sink()
sink("DESeq2_summary_ResistanceMechanism.txt")
print(summary(res))
sink()
```
Since nothing is differentially expressed between untreated and antibiotic treated colonies, plot summary of the types of AMR genes that were expressed. I will use Resistance Mechanism since there are 11 types in this category. (Drug Class = 140 types, AMR gene families = 371 types)
```{r, echo=FALSE}
library(ggplot2)
library(reshape2)
meta <-read.table("metadata_13.txt",sep="\t",header=TRUE)
meta$X <- NULL
mech <-read.table("Untreated-vs-antibiotic_ResistanceMechanism-counts.txt",sep="\t",header=TRUE)
mech$X <- NULL
#calculate relative abundance from counts
mech_RA <- mech[,-1]/colSums(mech[,-1]) * 100
mech_RA$Resistance.Mechanism <- mech$Resistance.Mechanism
#merge metadata and count dataframes - start by converting mech from wide to long format
mech_long <- melt(mech_RA, id.vars=c("Resistance.Mechanism"))
colnames(mech_long)[colnames(mech_long) == "variable"] <- "sample"
colnames(mech_long)[colnames(mech_long) == "value"] <- "proportion"
resist <-merge(meta, mech_long, "sample")
write.table(resist,"Untreated-vs-antibiotic_ResistanceMechanism-RA_combine_other.txt",sep="\t",col.names=NA)
#in excel, I changed categories with multiple mechanisms listed into "other" to reduce total number of displayed categories (and get rid of super long names in legend that prevented successful plotting)
other <-read.table("Untreated-vs-antibiotic_ResistanceMechanism-RA_combine_other.txt",sep="\t",header=TRUE)
other$type <-factor(other$type, levels=c("before","after"))
other$Resistance.Mechanism <-factor(other$Resistance.Mechanism, levels = c("antibiotic efflux","antibiotic inactivation","antibiotic target alteration","antibiotic target protection","antibiotic target replacement","reduced permeability to antibiotic","other"))
#color blind friendly palette from Cookbook for R: http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette
mycolors <-c("antibiotic efflux"="#E69F00","antibiotic inactivation"="#D55E00","antibiotic target alteration"="#56B4E9","antibiotic target protection"="#0072B2","antibiotic target replacement"="#009E73","reduced permeability to antibiotic"="#CC79A7","other"="#999999")
#plot as stacked bars
pdf("resistance_mechanisms.pdf", height=4)
p1<-ggplot(other, aes(fill=Resistance.Mechanism, x=colony, y=proportion))+
geom_bar(position="fill",stat="identity")+
facet_grid(. ~ type, scales="free", space="free")+
theme_bw()+
theme(strip.text.x=element_text(face="bold"))+
scale_fill_manual(values=mycolors)
p1
dev.off()
```
Plot the resistance mechanism "antibiotic inactivation" which includes beta-lactamases
```{r, echo=FALSE}
library(dplyr)
library(reshape2)
library(ggplot2)
rgi_counts <- read.table("Untreated-vs-antibiotic_RGI-counts.txt",sep="\t",header=TRUE)
rgi_counts[1] <- NULL
#subset = just "antibiotic inactivation" which includes beta-lactamases
inact <- rgi_counts[which(rgi_counts$Resistance.Mechanism=='antibiotic inactivation'),]
inact2 <- inact %>% group_by(AMR.Gene.Family) %>% summarize_at(vars(AMR1A:AMR10A), sum)
write.table(inact2,"Untreated-vs-antibiotic_AntibioticInactivation-counts.txt",sep="\t",col.names=NA)
#calculate relative abundance from counts
inact2_RA <- inact2[,-1]/colSums(inact2[,-1]) * 100
inact2_RA$AMR.Gene.Family <- inact2$AMR.Gene.Family
inact3 = inact2_RA[, c("AMR.Gene.Family", names(inact2_RA)[names(inact2_RA) != "AMR.Gene.Family"])]
write.table(inact3,"Untreated-vs-antibiotic_AntibioticInactivation-RA.txt",sep="\t",col.names=NA)
#find the top10 antibiotic inactivation gene families
inact3$total <- rowSums(inact3[,2:14])
inact3 <- inact3[order(inact3$total, decreasing = TRUE),]
top10 <-head(inact3, 10)
write.table(top10,"Untreated-vs-antibiotic_AntibioticInactivation-RA_top10_inactivation.txt",sep="\t",col.names=NA)
# R does not like the gene family name ANT(3") because of the quotation mark and I have not been able to escape the special character for plotting, so I will have to manually replace the name with ANT(3)
# I am also going to add a row to represent the proportion of gene families other than top10
top10<- read.table("Untreated-vs-antibiotic_AntibioticInactivation-RA_top10_inactivation.txt",sep="\t",header=TRUE)
#merge metadata and count dataframes - start by converting mech from wide to long format
top10_long <- melt(top10, id.vars=c("AMR.Gene.Family"))
colnames(top10_long)[colnames(top10_long) == "variable"] <- "sample"
colnames(top10_long)[colnames(top10_long) == "value"] <- "proportion"
meta <-read.table("metadata_13.txt",sep="\t",header=TRUE)
meta$X <- NULL
inact_meta <-merge(meta, top10_long, "sample")
#plot as stacked bars
inact_meta$type <-factor(inact_meta$type, levels=c("before","after"))
inact_meta$AMR.Gene.Family <-factor(inact_meta$AMR.Gene.Family, levels = c("OXA beta-lactamase","nitroimidazole reductase","tetracycline inactivation enzyme","chloramphenicol acetyltransferase (CAT)","streptogramin vgb lyase","ANT(3)","macrolide phosphotransferase (MPH)","ADC beta-lactamases pending classification for carbapenemase activity","CfiA beta-lactamase","MOX beta-lactamase","other"))
#colorblind friendly palette from ggpubfigs: https://github.com/JLSteenwyk/ggpubfigs/blob/master/R/colors.R
#muted_nine = c("#332288", "#117733", "#CC6677", "#88CCEE", "#999933", "#882255", "#44AA99", "#DDCC77", "#AA4499")
mycolors2 <-c("OXA beta-lactamase"="#332288","nitroimidazole reductase"="#117733","tetracycline inactivation enzyme"="#CC6677","chloramphenicol acetyltransferase (CAT)"="#88CCEE","streptogramin vgb lyase"="#999933","ANT(3)"="#882255","macrolide phosphotransferase (MPH)"="#44AA99","ADC beta-lactamases pending classification for carbapenemase activity"="#DDCC77","CfiA beta-lactamase"="#AA4499","MOX beta-lactamase"="#999999","other"="#000000")
pdf("inactivation_top10.pdf", height=4)
p2<-ggplot(inact_meta, aes(fill=AMR.Gene.Family, x=colony, y=proportion))+
geom_bar(position="fill",stat="identity")+
facet_grid(. ~ type, scales="free", space="free")+
theme(strip.text.x=element_text(face="bold"))+
scale_fill_manual(values = mycolors2)+
#theme(legend.position="bottom")+
#guides(fill = guide_legend(nrow = 2))+
theme_bw()
p2
dev.off()
```
Probably won't use.
```{r, echo=FALSE}
library(dplyr)
library(reshape2)
library(ggplot2)
#### look at only beta-lactamases
#in excel remove all rows that are not beta-lactamases 215 out of 260 inactivation gene families are beta-lactamases
beta <- read.table("Untreated-vs-antibiotic_AntibioticInactivation-RA.txt",sep="\t",header=TRUE)
#create a sum column to find most abundant beta-lactamase gene families then sort by abundance before plotting
beta$total <- rowSums(beta[,2:14])
beta <- beta[order(beta$total, decreasing = TRUE),]
top10 <-head(beta, 10)
write.table(top10,"Untreated-vs-antibiotic_AntibioticInactivation-RA_top10beta.txt",sep="\t",col.names=NA)
#merge metadata and count dataframes - start by converting mech from wide to long format
top10_long <- melt(top10, id.vars=c("AMR.Gene.Family"))
colnames(top10_long)[colnames(top10_long) == "variable"] <- "sample"
colnames(top10_long)[colnames(top10_long) == "value"] <- "proportion"
meta <-read.table("metadata_13.txt",sep="\t",header=TRUE)
meta$X <- NULL
beta10_meta <-merge(meta, top10_long, "sample")
#plot as stacked bars
beta10_meta$type <-factor(beta10_meta$type, levels=c("before","after"))
beta10_meta$AMR.Gene.Family <-factor(beta10_meta$AMR.Gene.Family, levels = c("OXA beta-lactamase","ADC beta-lactamases pending classification for carbapenemase activity","CfiA beta-lactamase","MOX beta-lactamase","SHV beta-lactamase","PER beta-lactamase","SPR beta-lactamase","subclass B3 PEDO beta-lactamase","GOB beta-lactamase","YRC Beta-lactamase"))
mycolors2 <-c("OXA beta-lactamase"="#332288","ADC beta-lactamases pending classification for carbapenemase activity"="#117733","CfiA beta-lactamase"="#CC6677","MOX beta-lactamase"="#88CCEE","SHV beta-lactamase"="#999933","PER beta-lactamase"="#882255","SPR beta-lactamase"="#44AA99","subclass B3 PEDO beta-lactamase"="#DDCC77","GOB beta-lactamase"="#AA4499","YRC Beta-lactamase"="#999999")
pdf("beta-lactamases_top10.pdf", height=4)
p2<-ggplot(beta10_meta, aes(fill=AMR.Gene.Family, x=sample, y=proportion))+
geom_bar(position="fill",stat="identity")+
facet_grid(. ~ type, scales="free", space="free")+
theme_bw()+
theme(axis.text.x=element_text(angle=90))+
theme(strip.text.x=element_text(face="bold"))+
scale_fill_manual(values = mycolors2)
p2
dev.off()
```