-
Notifications
You must be signed in to change notification settings - Fork 1
/
.Rhistory
512 lines (512 loc) · 31.3 KB
/
.Rhistory
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
facil$Rush_Sample_ID
sapply(facil$Rush_Sample_ID, function(x) lookup$`Genome ID`[lookup$`Sample ID` == x])
samp_names
sapply(facil$Rush_Sample_ID, function(x) samp_names$`OpGen Sample Accession ID #`[samp_names$`Rush Sample ID` == x])
facil
length(facil$patient)
length(unique(facil$patient))
lookup
sapply(st$`Sample name`, function(x) lookup$`Sample ID`[lookup$`Genome ID` == x])
lookup
st
sapply(st$`Sample name`, function(x) print(x) lookup$`Sample ID`[lookup$`Genome ID` == x])
sapply(st$`Sample name`, function(x) lookup$`Sample ID`[lookup$`Genome ID` == x])
lookup$`Genome ID` %in% st$`Sample name`
lookup$`Genome ID` == st$`Sample name`
lookup$`Genome ID` == st$`Sample name`[1]
st$`Sample name`[1]
lookup$`Genome ID`
lookup$`Sample ID`
st$`Sample name`
st$`Sample name` %in% lookup$`Genome ID`
lookup
lookup$`Sample ID`
lookup$`Genome ID`
unlist()
unlist(sapply(st$`Sample name`, function(x) lookup$`Sample ID`[lookup$`Genome ID` == x])
)
sapply(facil$Rush_Sample_ID, function(x) samp_names$`OpGen Sample Accession ID #`[samp_names$`Rush Sample ID` == x])
lookup <- read_tsv('data/2020-02-19_REALM_genome_sample_lookup.xlsx')
lookup
lookup <- read_excel('data/2020-02-19_REALM_genome_sample_lookup.xlsx')
lookup
opgen <- read_csv('data/Manifest_OpGen_noPHI_11_6_20_CSV.csv')
protect <- read_csv('data/Manifest_PROTECT_noPHI_11_6_20_CSV.csv')
library(tidyverse)
library(readxl)
st <- read_excel('../2020-09-01_ST_KPC_NDM_analysis/data/2020-08-10_REALM_QC_ST.xlsx', sheet = 4)
opgen <- read_csv('data/Manifest_OpGen_noPHI_11_6_20_CSV.csv')
protect <- read_csv('data/Manifest_PROTECT_noPHI_11_6_20_CSV.csv')
lookup <- read_excel('data/2020-02-19_REALM_genome_sample_lookup.xlsx')
samp_names <- read_excel('data/191122-WGS_Manifest _KPC_NDM_Samples_Sent_to_Michigan.xlsx')
opgen
protect
lookup
lookup$`Sample ID`
sapply(facil$Rush_Sample_ID, function(x) samp_names$`OpGen Sample Accession ID #`[samp_names$`Rush Sample ID` == x])
sapply(st$`Sample name`, function(x) lookup$`Sample ID`[lookup$`Genome ID` == x])
table([grepl('A000',sapply(st$`Sample name`, function(x) lookup$`Sample ID`[lookup$`Genome ID` == x])))
table(grepl('A000',sapply(st$`Sample name`, function(x) lookup$`Sample ID`[lookup$`Genome ID` == x])))
all_samp_ids <- sapply(st$`Sample name`, function(x) lookup$`Sample ID`[lookup$`Genome ID` == x])
all_samp_ids[grepl('A000',all_samp_ids)]
unlist(all_samp_ids[grepl('A000',all_samp_ids)])
realm_samp_ids <- unlist(all_samp_ids[grepl('A000',all_samp_ids)])
sapply(realm_samp_ids, function(x) samp_names$`Rush Sample ID`[samp_names$`OpGen Sample Accession ID #` == x])
facil_samp_ids <- sapply(realm_samp_ids, function(x) samp_names$`Rush Sample ID`[samp_names$`OpGen Sample Accession ID #` == x])
all_samp_ids
all_samp_ids <- unlist(sapply(st$`Sample name`, function(x) lookup$`Sample ID`[lookup$`Genome ID` == x]))
all_samp_ids
facil_samp_ids <- sapply(realm_samp_ids, function(x) samp_names$`Rush Sample ID`[samp_names$`OpGen Sample Accession ID #` == x])
facil_samp_ids
facil_samp_ids <- sapply(all_samp_ids, function(x) samp_names$`Rush Sample ID`[samp_names$`OpGen Sample Accession ID #` == x])
facil_samp_ids
head(all_samp_ids)
all_samp_ids[1:10]
all_samp_ids[1:20]
all_samp_ids[1:30]
facil_samp_ids[28]
samp_names$`Rush Sample ID`
samp_names$`OpGen Sample Accession ID #`
opgen
protect
all_samp_ids[1:30]
opgen
protect
unlist(facil_samp_ids)
lookup$`Sample ID`
protect$RUSH_Patient_ID
facil_samp_ids
all_samp_ids
samp_names
protect
opgen
protect
facil_samp_ids <- sapply(all_samp_ids, function(x){
nam <- samp_names$`Rush Sample ID`[samp_names$`OpGen Sample Accession ID #` == x]
if(length(nam) == 0){
nam <- gsub(' ORG.*','',x)
}
return(nam)
})
facil_samp_ids
facil_samp_ids <- sapply(all_samp_ids, function(x){
nam <- samp_names$`Rush Sample ID`[samp_names$`OpGen Sample Accession ID #` == x]
if(length(nam) == 0){
nam <- gsub(' ORG.*','',x) %>% gsub(' ','',x)
}
return(nam)
})
facil_samp_ids
facil_samp_ids <- sapply(all_samp_ids, function(x){
nam <- samp_names$`Rush Sample ID`[samp_names$`OpGen Sample Accession ID #` == x]
if(length(nam) == 0){
nam <- gsub(' ORG.*','',x) #%>% gsub(' ','',x)
}
return(nam)
})
facil_samp_ids
facil_samp_ids <- sapply(all_samp_ids, function(x){
nam <- samp_names$`Rush Sample ID`[samp_names$`OpGen Sample Accession ID #` == x]
if(length(nam) == 0){
nam <- gsub(' ORG.*','',x) %>% gsub(' P','',x)
}
return(nam)
})
facil_samp_ids
facil_samp_ids <- sapply(all_samp_ids, function(x){
nam <- samp_names$`Rush Sample ID`[samp_names$`OpGen Sample Accession ID #` == x]
if(length(nam) == 0){
nam <- gsub(' ORG.*','',x) %>% gsub(' P','P',x)
}
return(nam)
})
facil_samp_ids
facil_samp_ids <- sapply(all_samp_ids, function(x){
nam <- samp_names$`Rush Sample ID`[samp_names$`OpGen Sample Accession ID #` == x]
if(length(nam) == 0){
nam <- gsub(' ORG.*','',x)# %>% gsub('[] P','P',x)
}
return(nam)
})
facil_samp_ids
facil_samp_ids <- sapply(all_samp_ids, function(x){
nam <- samp_names$`Rush Sample ID`[samp_names$`OpGen Sample Accession ID #` == x]
if(length(nam) == 0){
nam <- gsub(' ORG.*','',x) %>% gsub('[A-Z] P','P',x)
}
return(nam)
})
facil_samp_ids
facil_samp_ids <- sapply(all_samp_ids, function(x){
nam <- samp_names$`Rush Sample ID`[samp_names$`OpGen Sample Accession ID #` == x]
if(length(nam) == 0){
nam <- gsub(' ORG.*','',x) %>% gsub('\sP','P',x)
nam <- gsub(' ORG.*','',x) %>% gsub('\\sP','P',x)
facil_samp_ids <- sapply(all_samp_ids, function(x){
nam <- samp_names$`Rush Sample ID`[samp_names$`OpGen Sample Accession ID #` == x]
if(length(nam) == 0){
nam <- gsub(' ORG.*','',x) %>% gsub('\\sP','P',x)
}
return(nam)
})
facil_samp_ids
x<-"ab b cde"
gsub(" b| c", " ",x)
x<-"ab b cde"
gsub(" b| c", " ",x)
x<-"ab b cde"
gsub(" b| c", "",x)
facil_samp_ids <- sapply(all_samp_ids, function(x){
nam <- samp_names$`Rush Sample ID`[samp_names$`OpGen Sample Accession ID #` == x]
if(length(nam) == 0){
nam <- gsub(' ORG.*','',x)
nam <- gsub(' P','P',x)
}
return(nam)
})
facil_samp_ids
facil_samp_ids <- sapply(all_samp_ids, function(x){
nam <- samp_names$`Rush Sample ID`[samp_names$`OpGen Sample Accession ID #` == x]
if(length(nam) == 0){
nam <- gsub(' ORG.*','',x)
nam <- gsub(' P','.P',x)
}
return(nam)
})
facil_samp_ids
facil_samp_ids <- sapply(all_samp_ids, function(x){
nam <- samp_names$`Rush Sample ID`[samp_names$`OpGen Sample Accession ID #` == x]
if(length(nam) == 0){
nam <- gsub(' ORG.*','',x)
nam <- gsub(' P','.P',nam)
}
return(nam)
})
facil_samp_ids
sum(facil_samp_ids %in% protect$RUSH_Patient_ID)
sum(facil_samp_ids %in% opgen$Rush_Sample_ID)
sum(facil_samp_ids %in% protect$RUSH_Patient_ID)
length(facil_samp_ids)
sum(facil_samp_ids %in% opgen$Rush_Sample_ID)
sum(facil_samp_ids %in% protect$RUSH_Patient_ID)
length(facil_samp_ids)
sum(facil_samp_ids %in% opgen$Rush_Sample_ID) + sum(facil_samp_ids %in% protect$RUSH_Patient_ID)
sum(facil_samp_ids %in% opgen$Rush_Sample_ID) + sum(facil_samp_ids %in% protect$RUSH_Patient_ID)/length(facil_samp_ids)
(sum(facil_samp_ids %in% opgen$Rush_Sample_ID) + sum(facil_samp_ids %in% protect$RUSH_Patient_ID))/length(facil_samp_ids)
length(facil_samp_ids)
sum(facil_samp_ids %in% opgen$Rush_Sample_ID) + sum(facil_samp_ids %in% protect$RUSH_Patient_ID)
(sum(facil_samp_ids %in% opgen$Rush_Sample_ID) + sum(facil_samp_ids %in% protect$RUSH_Patient_ID))/length(facil_samp_ids)
setwd('~')
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(readxl)
library(pheatmap)
library(ape)
library(ggtree)
species <- read_excel('../2020-09-01_ST_KPC_NDM_analysis/data/2020-08-10_REALM_QC_ST.xlsx', sheet = 4) %>% rename(name = `Sample name`)
kraken <- read_excel('data/2020-08-10_QC_analysis.xlsx', sheet = 6) %>% rename(name = `Sample name`)
card <- read_csv('../../Sequence_data/Reports/2020_08_26_Final_QC_analysis_AMR_minimal.csv') %>%
mutate(name=gsub('_/report.tsv','',name))
plasmidfinder <- read_csv('../../Sequence_data/Reports/plasmidfinder/results/ariba_minimal_results.csv') %>%
mutate(name=gsub('ariba/|/report.tsv','',name))
metadat <- read_tsv('../2020-11-10_facilities/data/realm_metadata.tsv') %>% rename(name = gID)
metadat <- metadat %>% select_if(names(metadat) != 'ST')
dat <- full_join(metadat, species) %>% full_join(., kraken) %>% full_join(.,card) %>% full_join(., plasmidfinder) %>% filter(!grepl('NEG_CTL',name)) %>% mutate(ST=gsub('\\*','',ST)) %>% filter(!is.na(id)) %>% mutate(bla=gsub('NDMKPC','NDM & KPC',paste0(ifelse(`NDM_-.match`=='yes','NDM',''),ifelse(`KPC_-.match`=='yes','KPC',''))))
st147 <- read.tree('../../Sequence_data/output_files/2020_10_06_17_18_52_core_results_with_public_genomes_and_outgroup/gubbins/iqtree_masked_wga/2020_10_06_17_18_52_KP_ST147_genome_aln_w_alt_allele_unmapped.treefile')
st147 <- drop.tip(st147, st147$tip.label[!grepl('REALM_CRE_',st147$tip.label)])
st147$tip.label <- gsub('_$','',st147$tip.label)
st147 <- root(st147,'REALM_CRE_125')
st147 <- drop.tip(st147,'REALM_CRE_125')
dna147 <- read.dna('../../Sequence_data/output_files/2020_10_06_17_18_52_core_results_with_public_genomes_and_outgroup/gubbins/2020_10_06_17_18_52_KP_ST147_genome_aln_w_alt_allele_unmapped.filtered_polymorphic_sites.fasta',format='fasta')
rownames(dna147) <- gsub('_$','',rownames(dna147))
dna147 <- dna147[st147$tip.label,]
all_dna <- read.dna('../../Sequence_data/output_files/2020_10_16_17_17_17_core_results_all_against_KPNIH1/gubbins/2020_10_16_17_17_17_KPNIH1_genome_aln_w_alt_allele_unmapped.filtered_polymorphic_sites.fasta', format = 'fasta')
# dat %>% ggplot(aes(x=`Raw Coverage`,fill=Species)) + geom_histogram(bins=100) + geom_vline(aes(xintercept=150,col='black'))
# hist(dat$`Raw Coverage`, breaks = 100)
# abline(v=150,col='red')
# dat %>% ggplot(aes(x=`Percentage of reads for Species`,fill=Species)) + geom_histogram(bins=100) + geom_vline(aes(xintercept=35,col='black'))
raw_cov_thresh <- 20
perc_spec_thresh <- 37
tit <- paste0('Raw coverage threshold = ', raw_cov_thresh, '; Percent species threshold = ', perc_spec_thresh)
# dat %>% ggplot(aes(x=`Raw Coverage`,y=`Percentage of reads for Species`,col=Species)) + geom_point() + geom_vline(aes(xintercept=raw_cov_thresh),col='black') + geom_hline(aes(yintercept=perc_spec_thresh),col='black') + ggtitle(tit)
dat %>% ggplot(aes(x=`Raw Coverage`,y=`Percentage of reads for Species`,col=Species,size=ifelse(`NDM_-.match` == 'yes',1,0.5))) + geom_point(alpha=0.5) + geom_vline(aes(xintercept=raw_cov_thresh),col='black') + geom_hline(aes(yintercept=perc_spec_thresh),col='black') + ggtitle(tit)
dat %>% ggplot(aes(x=`Raw Coverage`,y=`Percentage of reads for Species`,col=Comments)) + geom_point() + geom_vline(aes(xintercept=raw_cov_thresh),col='black') + geom_hline(aes(yintercept=perc_spec_thresh),col='black') + ggtitle(tit)
# dat %>% ggplot(aes(x=`Raw Coverage`,y=`Mean Read Depth`,col=Comments)) + geom_point() #+ geom_vline(aes(xintercept=35,col='black'))
# hist(dat$`Percentage of reads for Species`, breaks = 100)
# abline(v=35,col='red')
kp <- dat %>% filter(Species == 'Klebsiella pneumoniae' & !(`Raw Coverage` < raw_cov_thresh | `Percentage of reads for Species` < perc_spec_thresh))
# kp %>% select('ST','NDM_-.match') %>% table(.,useNA = 'always')
ndm_sts <- kp %>% filter(`NDM_-.match` == 'yes') %>% select(ST) %>% filter(!ST %in% c('ND','Novel')) %>% unique() %>% unlist() %>% c() %>% unname()
kp_ndm <- kp %>% filter(ST %in% ndm_sts)
# kp_ndm %>% select('ST','survey','NDM_-.match') %>% table()
# kp_ndm %>% select('ST','f_id','NDM_-.match') %>% table()
accessory <- kp_ndm %>% select_if(grepl('match',names(kp_ndm))) %>% select_if(apply(.,2,function(x) length(unique(x))) == 2) %>% data.frame()
# accessory[accessory == 'yes'] = 1
# accessory[accessory == 'no'] = 0
accessory <- sapply(accessory, function(x){
ifelse(x == 'yes',1,0)
})
rownames(accessory) <- kp_ndm$name
mdat <- data.frame(kp_ndm[,c('ST','NDM_-.match','KPC_-.match'),drop=F])
rownames(mdat) <- kp_ndm$name
cols <- list(`NDM_..match` = c(yes='orange',no='white'),`KPC_..match` = c(yes='blue',no='white'))
pheatmap(accessory,annotation_row = mdat, show_rownames = F, color = colorRampPalette(c('white','black'))(100), annotation_colors = cols)
gheatmap(ggtree(st147), mdat[,c('NDM_..match','KPC_..match')])
mdat <- data.frame(kp_ndm[,c('ST','NDM_-.match','KPC_-.match','bla'),drop=F])
rownames(mdat) <- kp_ndm$name
bla <- as.character(dat$bla)
names(bla) <- dat$name
bla <- c(bla[st147$tip.label], rep(NA,Nnode(st147)))
# ggtree(st147) + geom_tippoint(aes(color=sy))
st147_plot <- st147
st147_plot$tip.label <- gsub('REALM_CRE_','',st147_plot$tip.label)
# ggtree(st147_plot, layout = 'fan') + geom_tippoint(aes(color=bla)) + geom_tiplab(size=3)
ggtree(st147_plot) + geom_tippoint(aes(color=bla)) + geom_tiplab(size=2)
dists147 <- dist.dna(dna147,model = 'N',pairwise.deletion = T, as.matrix = T)
acc_cor <- cor(t(accessory))
acc_cor <- acc_cor[st147$tip.label[st147$tip.label %in% rownames(acc_cor)],st147$tip.label[st147$tip.label %in% colnames(acc_cor)]]
dists147 <- dists147[rownames(acc_cor),colnames(acc_cor)]
carb <- sapply(rownames(dists147), function(x){
sapply(colnames(dists147), function(y){
c1 <- dat$bla[dat$name==x]
c2 <- dat$bla[dat$name==y]
paste0(sort(c(c1,c2)),collapse=' / ')
})
pairs <- sapply(rownames(dists147), function(x){
sapply(colnames(dists147), function(y){
c1 <- dat$name[dat$name==x]
c2 <- dat$name[dat$name==y]
paste0(sort(c(c1,c2)),collapse=' / ')
})
cors <- bind_rows(pair=pairs[lower.tri(pairs)],carb=carb[lower.tri(carb)],snp_dists=dists147[lower.tri(dists147)],acc_cor=acc_cor[lower.tri(acc_cor)])
cors %>% ggplot(aes(x=snp_dists,y=acc_cor,col=carb)) + geom_point(alpha=0.5) + facet_wrap(~carb)
# Get pairs for NDM&KPC isolates
both <- dat$name[dat$bla == 'NDM & KPC']
# cors %>% filter(grepl('NDM & KPC',carb))
both_pair <- sapply(both, function(x){
colnames(dists147)[colnames(dists147) != x][which.min(dists147[x,colnames(dists147) != x])]
})
# Get pairs for KPC isolates
kpc <- dat$name[dat$bla == 'KPC' & dat$ST == 147]
# remove 241 b/c same as 212 and accessory genomes similar
# remove 409, 386, 330 b/c more outgroup than 502
kpc <- kpc[!kpc %in% c('REALM_CRE_241','REALM_CRE_409','REALM_CRE_386','REALM_CRE_330')]
kpc_pair <- sapply(kpc, function(x){
colnames(dists147)[colnames(dists147) != x & !colnames(dists147) %in%
c('REALM_CRE_241')][which.min(dists147[x,colnames(dists147) != x &
!colnames(dists147) %in% c('REALM_CRE_241')])]
})
# Get NDM representatives for non-ST147 K. pneumo. (Looked at accessory genomes to choose)
not_147 <- dat$name[dat$bla == 'NDM' & dat$ST != 147]
# remove duplicate STs: 607, 379, 356, 394, 403
not_147 <- not_147[!not_147 %in% paste0('REALM_CRE_',c(607, 379, 356, 394, 403))]
# prep both pairs for plotting
samps_both <- cbind(c(sapply(1:length(both_pair), function(x){
bp <- unname(c(both_pair[x],names(both_pair)[x]))
})),
c(sapply(1:length(both_pair), function(x){
bp <- unname(c(dat$bla[dat$name == both_pair[x]],dat$bla[dat$name == names(both_pair)[x]]))
})),
c(sapply(1:length(both_pair), function(x) rep(paste0('both_',x),2))))
rownames(samps_both) <- samps_both[,1]
colnames(samps_both) <- c('name','bla','pair')
acc_pairs_both <- accessory[samps_both[,1],]
# prep kpc pairs for plotting
samps_kpc <- cbind(c(sapply(1:length(kpc_pair), function(x){
bp <- unname(c(kpc_pair[x],names(kpc_pair)[x]))
})),
c(sapply(1:length(kpc_pair), function(x){
bp <- unname(c(dat$bla[dat$name == kpc_pair[x]],dat$bla[dat$name == names(kpc_pair)[x]]))
})),
c(sapply(1:length(kpc_pair), function(x) rep(paste0('kpc_',x),2))))
rownames(samps_kpc) <- samps_kpc[,1]
colnames(samps_kpc) <- c('name','bla','pair')
acc_pairs_kpc <- accessory[samps_kpc[,1],]
# acc_pairs <- acc_pairs[,colSums(acc_pairs)]
md_not <- data.frame(ST=sapply(not_147, function(x) dat$ST[dat$name == x]))
rownames(md_not) <- not_147
acc_non147 <- accessory[rownames(accessory) %in% not_147,]
acc_all <- rbind(acc_pairs_both,acc_pairs_kpc,acc_non147)
ann_all <- data.frame(rbind(samps_kpc[,2:3,drop=F],samps_both[,2:3,drop=F]))
# ann_all$ST <- sapply(samps_)
pheatmap(acc_all,annotation_row = ann_all,color = colorRampPalette(c('white','black'))(100))
# pheatmap(accessory[samps_both[,1],],annotation_row = data.frame(samps_both[,2:3,drop=F]),color = colorRampPalette(c('white','black'))(100))
# pheatmap(accessory[samps_kpc[,1],],annotation_row = data.frame(samps_kpc[,2:3,drop=F]),color = colorRampPalette(c('white','black'))(100))
# pheatmap(acc_non147, annotation_row = md_not,color = colorRampPalette(c('white','black'))(100))
print(rownames(acc_all))
chosen <- ifelse(st147$tip.label %in% rownames(acc_all),'cadetblue','black')
bla <- as.character(dat$bla)
names(bla) <- dat$name
bla <- c(bla[st147$tip.label], rep(NA,Nnode(st147)))
# ggtree(st147) + geom_tippoint(aes(color=sy))
st147_plot <- st147
st147_plot$tip.label <- gsub('REALM_CRE_','',st147_plot$tip.label)
# ggtree(st147_plot, layout = 'fan') + geom_tippoint(aes(color=bla)) + geom_tiplab(size=3)
ggtree(st147_plot) + geom_tippoint(aes(color=bla)) + geom_tiplab(size=2,col=chosen)
colSums(accessory[,colnames(acc_all)[colSums(acc_all) == 0]])
non_ndm_147s <- kp_ndm$name[kp_ndm$ST == 147 & kp_ndm$bla != 'NDM']
non_ndm_147s_pair <- sapply(non_ndm_147s, function(x){
colnames(dists147)[colnames(dists147) != x][which.min(dists147[x,colnames(dists147) != x])]
})
non_ndm_147s_pairs_isolates <- unique(c(names(non_ndm_147s_pair),non_ndm_147s_pair))
non_ndm_147s_pairs_isolates
chosen_mic <- ifelse(st147$tip.label %in% non_ndm_147s_pairs_isolates,'cadetblue','black')
# also include
ggtree(st147_plot) + geom_tippoint(aes(color=bla)) + geom_tiplab(size=2,col=chosen_mic)
read.dna('../../Sequence_data/output_files/2020_10_16_17_17_17_core_results_all_against_KPNIH1/gubbins/')
kp_ndm %>% filter(ST != 147) %>% select(bla,ST) %>% table()
# singletons
kp_ndm$name[kp_ndm$ST %in% c(234,327)]
# others - pick closest pairwise distance
kp_ndm$name[kp_ndm$ST %in% c(15,307,874)]
setwd('~/Desktop/gl_mount/Project_REALM/Analysis/2020-11-23_long-read/')
setwd('~/Desktop/a_mount/Project_CRE_EIP/Analysis/Regional_CRE_transmission/2020-10-09_match-metadata-genomes/')
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(readxl)
st_species <- read_csv('../2020-02-13_species-st-descriptive-analysis/data/species_st.csv')
mn_metadat <- sapply(list.files('../../data/sample_information/MN/', full.names = T), read_excel)
mn_metadat$`../../data/sample_information/MN//FINAL_CRE_18_Results_MN_01302020.xlsx`
mn_metadat
mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx`$SPHLID
gsub('^C','', mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx`$SPHLID)
mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`
st_species$Sample
st_species$Sample_IDs
st_species$Sample
gsub('_R1_001.fastq.gz','',st_species$Sample)
gsub('_R1_001.fastq.gz','',st_species$Sample) %in% gsub('^C','', mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx`$SPHLID)
gsub('_R1_001.fastq.gz','',st_species$Sample)
gsub('^C','', mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx`$SPHLID)
st_species
species_st <- read_csv('../2020-02-13_species-st-descriptive-analysis/data/species_st.csv')
gsub('_R1_001.fastq.gz','',species_st$Sample)[species_st$state == 'MN']
gsub('_R1_001.fastq.gz','',species_st$Sample)[species_st$state == 'MN'] %in% gsub('^C','', mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx`$SPHLID)
gsub('_R1_001.fastq.gz','',species_st$Sample)[species_st$state == 'MN']
View(species_st)
gsub('_R1_001.fastq.gz|-.*','',species_st$Sample)[species_st$state == 'MN']
gsub('_R1_001.fastq.gz|-.*','',species_st$Sample)[species_st$state == 'MN'] %in%
gsub('^C','', mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx`$SPHLID)
gsub('_R1_001.fastq.gz|-.*','',species_st$Sample)[species_st$state == 'MN']# %in%
mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec
gsub('_R1_001.fastq.gz|-.*','',species_st$Sample)[species_st$state == 'MN']# %in%
mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec %in% gsub('^C','', mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx`$SPHLID)
mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec
mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec %in% gsub('^C','', mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx`$SPHLID) %>% table()
mn_metadat
mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec %in% gsub('^C','', mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx`$SPHLID) %>% table()
mn_metadat$`../../data/sample_information/MN//FINAL_CRE_18_Results_MN_01302020.xlsx`
length(mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$MN_Patient_ID)
length(unique(mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$MN_Patient_ID))
mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec
mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec %in% gsub('^C','', mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx`$SPHLID)
gsub('_R1_001.fastq.gz|-.*','',species_st$Sample)[species_st$state == 'MN']# %in%
gsub('^C','', mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx`$SPHLID)
gsub('_R1_001.fastq.gz|-.*','',species_st$Sample)[species_st$state == 'MN'] %in%
gsub('^C','', mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx`$SPHLID)
gsub('_R1_001.fastq.gz|-.*','',species_st$Sample)[species_st$state == 'MN'] %in% mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec
mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec %in% gsub('_R1_001.fastq.gz|-.*','',species_st$Sample)[species_st$state == 'MN']
mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec[!mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec %in% gsub('_R1_001.fastq.gz|-.*','',species_st$Sample)[species_st$state == 'MN']]
gsub('_R1_001.fastq.gz|-.*','',species_st$Sample)[species_st$state == 'MN'][!gsub('_R1_001.fastq.gz|-.*','',species_st$Sample)[species_st$state == 'MN'] %in% mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec]
species_st$Sample
gsub('_R1_001.fastq.gz','',species_st$Sample)[species_st$state == 'MN']
gsub('_R1_001.fastq.gz|-MN.*','',species_st$Sample)[species_st$state == 'MN'][!gsub('_R1_001.fastq.gz|-.*','',species_st$Sample)[species_st$state == 'MN'] %in% mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec]
gsub('_R1_001.fastq.gz|-MN.*','',species_st$Sample)[species_st$state == 'MN'][!gsub('_R1_001.fastq.gz|-MN.*|_S.*','',species_st$Sample)[species_st$state == 'MN'] %in% mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec]
gsub('_R1_001.fastq.gz|-MN.*','',species_st$Sample)[species_st$state == 'MN'][!gsub('_R1_001.fastq.gz|-MN.*','',species_st$Sample)[species_st$state == 'MN'] %in% mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec]
gsub('_R1_001.fastq.gz|-MN.*','',species_st$Sample)[species_st$state == 'MN'][!gsub('_R1_001.fastq.gz|-MN.*|-CRE.*','',species_st$Sample)[species_st$state == 'MN'] %in% mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec]
gsub('_R1_001.fastq.gz|-MN.*','',species_st$Sample)[species_st$state == 'MN'][!gsub('_R1_001.fastq.gz|-MN.*|-CRE.*|-.$','',species_st$Sample)[species_st$state == 'MN'] %in% mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec]
gsub('_R1_001.fastq.gz|-MN.*','',species_st$Sample)[species_st$state == 'MN'][!gsub('_R1_001.fastq.gz|-MN.*|-CRE.*|-*$','',species_st$Sample)[species_st$state == 'MN'] %in% mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec]
gsub('_R1_001.fastq.gz|-MN.*','',species_st$Sample)[species_st$state == 'MN'][!gsub('_R1_001.fastq.gz|-MN.*|-CRE.*|-[0-9]$','',species_st$Sample)[species_st$state == 'MN'] %in% mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec]
gsub('_R1_001.fastq.gz|-MN.*','',species_st$Sample)[species_st$state == 'MN'][!gsub('_R1_001.fastq.gz|-','',species_st$Sample)[species_st$state == 'MN'] %in% mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec]
gsub('_R1_001.fastq.gz','',species_st$Sample)[species_st$state == 'MN'][!gsub('_R1_001.fastq.gz|-','',species_st$Sample)[species_st$state == 'MN'] %in% mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec]
gsub('_R1_001.fastq.gz','',species_st$Sample)[species_st$state == 'MN'][!gsub('_R1_001.fastq.gz|-.*','',species_st$Sample)[species_st$state == 'MN'] %in% mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec]
gsub('_R1_001.fastq.gz','',species_st$Sample)[species_st$state == 'MN'][!gsub('_R1_001.fastq.gz|-.*|S11_L001|S4_L001','',species_st$Sample)[species_st$state == 'MN'] %in% mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec]
gsub('_R1_001.fastq.gz|-.*|S11_L001|S4_L001','',species_st$Sample)[species_st$state == 'MN']
gsub('_R1_001.fastq.gz','',species_st$Sample)[species_st$state == 'MN'][!gsub('_R1_001.fastq.gz|-.*|S11_L001|S4_L001','',species_st$Sample)[species_st$state == 'MN'] %in% mn_metadat$`../../data/sample_information/MN//WGS Isolates for Transmission Network_FINAL-1-21-2020.xlsx`$Spec]
getwd()
setwd('~/Desktop/a_mount/Project_CRE_EIP/Analysis/Regional_CRE_transmission/2020-10-09_match-metadata-genomes/')
getwd()
setwd('~/Desktop/a_mount/Project_CRE_EIP/Analysis/Regional_CRE_transmission/2020-10-09_match-metadata-genomes/')
getwd()
setwd('~/Desktop/a_mount/Project_CRE_EIP/Analysis/Regional_CRE_transmission/2020-10-09_match-metadata-genomes/')
getwd()
mn_metadat <- sapply(list.files('../../data/sample_information/MN/', full.names = T), read_excel)
list.files('../../data/sample_information/MN/', full.names = T)
library(tidyverse)
library(readxl)
species_st <- read_csv('../2020-02-13_species-st-descriptive-analysis/data/species_st.csv')
mn_metadat <- sapply(list.files('../../data/sample_information/MN/', full.names = T), read_excel)
list.files('../../data/sample_information/MN/', full.names = T)
mn_metadat <- sapply(list.files('../../data/sample_information/MN/', full.names = T), function(x){
if(grepl('xlsx',x)) x <- read_excel(x)
else x <- read_tsv(x)
} )
mn_metadat
length(mn_metadat)
species_st$Sample %in% mn_metadat$`../../data/sample_information/MN//2020-12-09_MN_genome_lookup_table.txt`$Original_fastq
species_st$Sample[species_st$state == 'MN'] %in% mn_metadat$`../../data/sample_information/MN//2020-12-09_MN_genome_lookup_table.txt`$Original_fastq
species_st %>% rename(Original_fastq=Sample)
mn_metadat$`../../data/sample_information/MN//2020-12-09_MN_genome_lookup_table.txt`
species_st
species_st %>% rename(Original_fastq=Sample, Project_fastq=Sample_IDs)
species_st <- species_st %>% rename(Original_fastq=Sample, Project_fastq=Sample_IDs)
full_join(species_st, mn_metadat$`../../data/sample_information/MN//2020-12-09_MN_genome_lookup_table.txt`)
full_join(mn_metadat$`../../data/sample_information/MN//2020-12-09_MN_genome_lookup_table.txt`, species_st)
# species/st and lookup table
dat <- full_join(mn_metadat$`../../data/sample_information/MN//2020-12-09_MN_genome_lookup_table.txt`, species_st)
View(dat)
mn_metadat$`../../data/sample_information/MN//CRE Transmission Network Project_MN-UMich_CDC-WGS_Crosswalk.xlsx`
mn_metadat$`../../data/sample_information/MN//FINAL_CRE_18_Results_MN_01302020.xlsx`
mn_metadat$`../../data/sample_information/MN//MN_EIP_metadata with db_Nov2020.xlsx`
dat
mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx`
species_st <- species_st %>% rename(Original_fastq=Sample, Project_fastq=Sample_IDs, Spec_ID=SpecID)
species_st <- species_st %>% rename(Original_fastq=Sample, Project_fastq=Sample_IDs)
species_st
dat %>% rename(Spec_ID=SpecID)
# species/st and lookup table
dat <- full_join(mn_metadat$`../../data/sample_information/MN//2020-12-09_MN_genome_lookup_table.txt`, species_st) %>% rename(Spec_ID=SpecID)
dim(dat)
full_join(dat, mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx`)
full_join(dat, mn_metadat$`../../data/sample_information/MN//MuGSI CRE Transmission Network Metadata_2012-2018.xlsx`)
mn_metadat$`../../data/sample_information/MN//MuGSI CRE Transmission Network Metadata_2012-2018.xlsx`
mn_metadat$`../../data/sample_information/MN//FINAL_CRE_18_Results_MN_01302020.xlsx`
mn_metadat$`../../data/sample_information/MN//MN_EIP_metadata with db_Nov2020.xlsx`
full_join(dat, mn_metadat$`../../data/sample_information/MN//MN_EIP_metadata with db_Nov2020.xlsx`)
mn_metadat$`../../data/sample_information/MN//MN_EIP_metadata with db_Nov2020.xlsx`
mn_metadat$`../../data/sample_information/MN//MN_EIP_metadata with db_Nov2020.xlsx` %>% mutate(Spec_ID=as.numeric(Spec_ID))
mn_metadat$`../../data/sample_information/MN//MN_EIP_metadata with db_Nov2020.xlsx` <- mn_metadat$`../../data/sample_information/MN//MN_EIP_metadata with db_Nov2020.xlsx` %>% mutate(Spec_ID=as.numeric(Spec_ID))
full_join(dat, mn_metadat$`../../data/sample_information/MN//MN_EIP_metadata with db_Nov2020.xlsx`)
dim(mn_metadat$`../../data/sample_information/MN//MN_EIP_metadata with db_Nov2020.xlsx`)
mn_metadat$`../../data/sample_information/MN//MN_EIP_metadata with db_Nov2020.xlsx`$Spec_ID %in% dat$Spec_ID
dat$Spec_ID %in% mn_metadat$`../../data/sample_information/MN//MN_EIP_metadata with db_Nov2020.xlsx`$Spec_ID
dat$Spec_ID[dat$state=='MN'] %in% mn_metadat$`../../data/sample_information/MN//MN_EIP_metadata with db_Nov2020.xlsx`$Spec_ID
dat$Spec_ID[dat$state=='MN']
dat$Spec_ID[dat$state=='MN'] %in% mn_metadat$`../../data/sample_information/MN//MN_EIP_metadata with db_Nov2020.xlsx`$Spec_ID
full_join(dat, mn_metadat$`../../data/sample_information/MN//MN_EIP_metadata with db_Nov2020.xlsx`)
View(full_join(dat, mn_metadat$`../../data/sample_information/MN//MN_EIP_metadata with db_Nov2020.xlsx`)
)
dim(dat)
dim(mn_metadat$`../../data/sample_information/MN//MN_EIP_metadata with db_Nov2020.xlsx`)
?full_join
mn_metadat$`../../data/sample_information/MN//MN_EIP_metadata with db_Nov2020.xlsx`
mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx`
# species/st and lookup table
dat <- full_join(mn_metadat$`../../data/sample_information/MN//2020-12-09_MN_genome_lookup_table.txt`, species_st)
mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx` %>% mutate(Spec_ID=gsub('C','',SPHLID))
mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx` <- mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx` %>% mutate(Spec_ID=gsub('C','',SPHLID))
dat$SpecID %in% mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx`$Spec_ID
dat$SpecID[dat$state=='MN'] %in% mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx`$Spec_ID
setwd('~')
dat$SpecID[dat$state=='MN'] %in% mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx`$Spec_ID
table(dat$SpecID[dat$state=='MN'] %in% mn_metadat$`../../data/sample_information/MN//MuGSI_CMCRF.xlsx`$Spec_ID)
setwd('Desktop/snitkitr/')
devtools::document()
View(parse_indels())
View(parse_indels)
View(parse_snps)
test = "514.3"
grep('^[0-9]',test)
View(get_anc_alleles)
View(get_major_alleles)
View(get_anc_alleles)