utils.R 12.1 KB
Newer Older
1
2
#!/usr/bin/Rscript

Valentina Galata's avatar
Valentina Galata committed
3
## IMPORT
4
5
suppressMessages(library(ggsci)) # colors

Valentina Galata's avatar
Valentina Galata committed
6
7
8
##############################
# INPUT
read_nanostats <- function(fname){
9
    print(sprintf("Reading: %s", fname))
Valentina Galata's avatar
Valentina Galata committed
10
11
12
13
14
15
16
17
18
19
20
21
    df <- read.csv(
        file=fname,
        sep="\t",
        header=TRUE,
        check.names=FALSE,
        stringsAsFactors=FALSE
    )
    df_cols <- c("stat"="Statistic", "value"="Value")
    colnames(df) <- df_cols[colnames(df)]
    return(df)
}

22
read_quast <- function(fname){
23
    print(sprintf("Reading: %s", fname))
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    df <- read.csv(
        file=fname,
        sep="\t",
        header=TRUE,
        row.names=1,
        check.names=FALSE,
        stringsAsFactors=FALSE
    )
    testit::assert(all(colnames(df) %in% names(ASM_TOOL_NAMES)))
    colnames(df) <- ASM_TOOL_NAMES[colnames(df)]
    df <- df[QUAST_VARS, ASM_TOOL_NAMES]
    return(df)
}

read_plasflow <- function(fname){
39
    print(sprintf("Reading: %s", fname))
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    df <- read.csv(
        file=fname,
        sep="\t",
        header=TRUE,
        stringsAsFactors=FALSE,
        check.names=FALSE
    )
    testit::assert(all(df$tool %in% names(ASM_TOOL_NAMES)))
    df$tool <- ASM_TOOL_NAMES[df$tool]
    df <- df[df$label %in% names(PLASFLOW_NAMES$labels),]
    df$label <- PLASFLOW_NAMES$labels[df$label]
    return(df)
}

54
read_rgi <- function(fname){
55
    print(sprintf("Reading: %s", fname))
56
57
58
59
60
61
62
63
64
65
66
67
68
    df <- read.csv(
        file=fname,
        sep="\t",
        header=TRUE,
        stringsAsFactors=FALSE,
        check.names=FALSE
    )
    colnames(df) <- sapply(colnames(df), function(x){ ifelse(x %in% names(ASM_TOOL_NAMES), ASM_TOOL_NAMES[x], x) })
    testit::assert(all(df$col %in% names(RGI_NAMES$col)))
    df$col <- RGI_NAMES$col[df$col]
    return(df)
}

69
read_crispr <- function(fname){
70
    print(sprintf("Reading: %s", fname))
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    df <- read.csv(
        file=fname,
        sep="\t",
        header=TRUE,
        stringsAsFactors=FALSE,
        check.names=FALSE
    )
    testit::assert(all(df$crispr_tool %in% names(CRISPR_TOOL_NAMES)))
    testit::assert(all(df$asm_tool    %in% names(ASM_TOOL_NAMES)))
    df$crispr_tool <- CRISPR_TOOL_NAMES[df$crispr_tool]
    df$asm_tool    <- ASM_TOOL_NAMES[df$asm_tool]
    return(df)
}

85
read_mappability <- function(fname){
86
    print(sprintf("Reading: %s", fname))
87
88
89
90
91
92
93
    df <- read.csv(
        file=fname,
        sep="\t",
        header=TRUE,
        check.names=FALSE,
        stringsAsFactors=FALSE
    )
94
95
    testit::assert(all(df$tool %in% names(ASM_TOOL_NAMES)))
    df$tool <- ASM_TOOL_NAMES[df$tool]
96
97
98
    return(df)
}

99
read_diamond <- function(fname){
100
    print(sprintf("Reading: %s", fname))
101
102
103
104
105
106
107
108
109
110
111
112
    df <- read.csv(
        file=fname,
        sep="\t",
        header=TRUE,
        check.names=FALSE,
        stringsAsFactors=FALSE
    )
    testit::assert(all(df$tool %in% names(ASM_TOOL_NAMES)))
    df$tool <- ASM_TOOL_NAMES[df$tool]
    return(df)
}

113
read_prodigal <- function(fname){
114
    print(sprintf("Reading: %s", fname))
115
116
117
118
119
120
121
122
123
    df <- read.csv(
        file=fname,
        sep="\t",
        header=TRUE,
        check.names=FALSE,
        stringsAsFactors=FALSE
    )
    testit::assert(all(df$tool %in% names(ASM_TOOL_NAMES)))
    df$tool <- ASM_TOOL_NAMES[df$tool]
124
    df$partial_pct <- 100 * df$partial / df$total
125
126
127
    return(df)
}

128
read_cdhit <- function(fname){
129
    print(sprintf("Reading: %s", fname))
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    df <- read.csv(
        file=fname,
        sep="\t",
        header=TRUE,
        check.names=FALSE,
        stringsAsFactors=FALSE
    )
    df <- df[,c("tool1", "tool2", "unique")]
    testit::assert(all(df$tool1 %in% names(ASM_TOOL_NAMES)))
    df$tool1 <- ASM_TOOL_NAMES[df$tool1]
    testit::assert(all(df$tool2 %in% names(ASM_TOOL_NAMES)))
    df$tool2 <- ASM_TOOL_NAMES[df$tool2]
    return(df)
}

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
read_mash_dist <- function(fname){
    proc_name <- function(x){
        x <- dirname(x)
        x <- basename(x)
        return(x)
    }
    dm <- read.csv(file=fname, sep='\t', header=TRUE, row.names=1, check.names=FALSE)
    colnames(dm) <- sapply(colnames(dm), proc_name)
    rownames(dm) <- sapply(rownames(dm), proc_name)
    testit::assert(all(colnames(dm) %in% names(ASM_TOOL_NAMES)))
    colnames(dm) <- ASM_TOOL_NAMES[colnames(dm)]
    rownames(dm) <- ASM_TOOL_NAMES[rownames(dm)]
    return(dm)
}


161
##############################
162
163
164
165
166
167
168
169
170
171
172
# PLOTS

plot_rgi_overlap <- function(df, ctype, col){
    df <- df[df$type == ctype & df$col == col,]
    df_list <- lapply(ASM_TOOL_NAMES, function(x){ df[df[,x] > 0,"label"] })
    names(df_list) <- ASM_TOOL_NAMES[names(df_list)]
    UpSetR::upset(
        data=UpSetR::fromList(df_list),
        # overlap order
        order.by="degree",
        decreasing=FALSE,
173
174
        # number of sets to plot
        nsets=length(ASM_TOOL_NAMES),
175
176
177
        # y-label title
        mainbar.y.label=sprintf("Intersection size (%s hits, %s)", ctype, col),
        # text size
178
        text.scale = c(1.2, 1.2, 1.2, 1.2, 1.2, 1.2)#,
179
        # colors
180
181
182
183
184
185
186
187
        # set.metadata=list(
        #     data=data.frame(
        #         sets=names(df_list),
        #         Tool=names(df_list),
        #         stringsAsFactors=FALSE
        #     ), 
        #     plots=list(list(type="matrix_rows", column="Tool", colors=ASM_TOOL_COLORS, alpha=0.7))
        # )
188
189
190
    )
}

191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
plot_crispr_overlap <- function(df, asm_tool){
    asm_sets <- lapply(
        CRISPR_TOOL_NAMES,
        function(x){ unique(unlist(df[df$asm_tool==asm_tool & df$crispr_tool == x,"seq_id"])) }
    )
    names(asm_sets) <- CRISPR_TOOL_NAMES[names(asm_sets)]
    print(asm_sets)
    UpSetR::upset(
        data=UpSetR::fromList(asm_sets),
        order.by="degree",
        decreasing=FALSE,
        mainbar.y.label=sprintf("Contig intersection size (%s)", asm_tool),
        sets.x.label="Contigs w/ CRISPR array(s)"
    )
}

Valentina Galata's avatar
Valentina Galata committed
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
# THEMES
mappability_theme <-
    theme_bw() +
    theme(
        # grid
        panel.grid=element_blank(),
        # strip
        strip.background=element_rect(fill="white"),
        strip.text=element_text(size=12, color="black"),
        # axes
        axis.title=element_text(size=12, color="black"),
        axis.text.y=element_text(size=9, color="black"),
        axis.text.x=element_text(size=9, color="black", angle=90, vjust=0.5, hjust=1)
    )

crispr_theme <-
    theme_bw() +
    theme(
        # legend
        legend.title=element_blank(),
        # grid
        panel.grid=element_blank(),
        # strip
        strip.background=element_rect(fill="white"),
        strip.text=element_text(size=12, color="black"),
        # axes
        axis.title=element_text(size=12, color="black"),
        axis.text.y=element_text(size=9, color="black"),
        axis.text.x=element_text(size=9, color="black", angle=90, vjust=0.5, hjust=1)
    )

plasflow_theme <-
    theme_bw() +
    theme(
        # legend
        legend.title=element_blank(),
        # grid
        panel.grid=element_blank(),
        # axes
        axis.title=element_text(size=12, color="black"),
        axis.text.y=element_text(size=9, color="black"),
        axis.text.x=element_text(size=9, color="black", angle=90, vjust=0.5, hjust=1)
    )

prodigal_theme <-
    theme_bw() +
    theme(
        # legend
        legend.title=element_blank(),
        # grid
        panel.grid=element_blank(),
        # axes
        axis.title=element_text(size=12, color="black"),
        axis.text.y=element_text(size=9, color="black"),
        axis.text.x=element_text(size=9, color="black", angle=90, vjust=0.5, hjust=1)
    )

diamond_theme1 <-
    theme_bw() +
    theme(
        # legend
        legend.title=element_blank(),
        # grid
        panel.grid=element_blank(),
        # axes
        axis.title=element_text(size=12, color="black"),
        axis.text.y=element_text(size=9, color="black"),
        axis.text.x=element_text(size=9, color="black", angle=90, vjust=0.5, hjust=1)
    )

diamond_theme2 <-
    theme_bw() +
    theme(
        # legend
        legend.title=element_blank(),
        # grid
        panel.grid=element_blank(),
        # strip
        strip.background=element_rect(fill="white"),
        strip.text=element_text(size=9, color="black"),
        # axes
        axis.title=element_text(size=12, color="black"),
        axis.text.y=element_text(size=9, color="black"),
        axis.text.x=element_text(size=9, color="black", angle=90, vjust=0.5, hjust=1)
    )

rgi_theme <- 
    theme_bw() +
    theme(
        # grid
        panel.grid=element_blank(),
        # strip
        strip.background=element_rect(fill="white"),
        strip.text=element_text(size=9, color="black"),
        # axes
        axis.title=element_text(size=12, color="black"),
        axis.text.y=element_text(size=9, color="black"),
        axis.text.x=element_text(size=9, color="black", angle=90, vjust=0.5, hjust=1)
    )

quast_theme <-
    theme_bw() +
    theme(
        # grid
        panel.grid=element_blank(),
        # strip
        strip.background=element_rect(fill="white"),
        strip.text=element_text(size=12, color="black"),
        # axes
        axis.title=element_text(size=12, color="black"),
        axis.text.y=element_text(size=9, color="black"),
        axis.text.x=element_text(size=9, color="black", angle=90, vjust=0.5, hjust=1)
    )

cdhit_theme <-
    theme_bw() +
    theme(
        # grid
        panel.grid=element_blank(),
        # strip
        strip.background=element_rect(fill="white"),
        strip.text=element_text(size=9, color="black"),
        # axes
        axis.title=element_text(size=12, color="black"),
        axis.text.y=element_text(size=9, color="black"),
        axis.text.x=element_text(size=9, color="black", angle=90, vjust=0.5, hjust=1)
    )

335
336
337
338
##############################
# CONST

###############
339
# Assemblers
340
341
342

# names
ASM_TOOL_NAMES <- c(
343
    # LR, SR, hybrid
344
345
    "flye"="Flye",
    "megahit"="MEGAHIT",
346
    "metaspades"="metaSPAdes",
347
348
349
350
351
352
353
    "operams"="OPERA-MS",
    "metaspadeshybrid"="metaSPAdes (H)",
    # polishing w/ metaT (w/o LR)
    "megahitmetatracon"="Racon(MEGAHIT + metaT)",
    "metaspadesmetatracon"="Racon(metaSPAdes + metaT)",
    "operamsmetatracon"="Racon(OPERA-MS + metaT)",
    "metaspadeshybridmetatracon"="Racon(metaSPAdes (H) + metaT)"
354
)
Valentina Galata's avatar
Valentina Galata committed
355
356
357
358
359
ASM_TOOL_NAMES <- ASM_TOOL_NAMES[c(
    snakemake@config$assemblers$lr,
    snakemake@config$assemblers$sr,
    snakemake@config$assemblers$hy
)]
360
# colors
Valentina Galata's avatar
Valentina Galata committed
361
362
363
364
ASM_TOOL_PALETTE1 <- ggsci::pal_nejm("default", alpha=1)(5)
ASM_TOOL_PALETTE2 <- ggsci::pal_nejm("default", alpha=0.8)(5)
ASM_TOOL_PALETTE3 <- ggsci::pal_nejm("default", alpha=0.6)(5)
ASM_TOOL_PALETTE4 <- ggsci::pal_nejm("default", alpha=0.4)(5)
365
ASM_TOOL_COLORS <- c(
Valentina Galata's avatar
Valentina Galata committed
366
367
368
369
370
371
372
373
374
375
376
    # LR, SR, hybrid
    "Flye"=ASM_TOOL_PALETTE1[1],
    "MEGAHIT"=ASM_TOOL_PALETTE1[2],
    "metaSPAdes"=ASM_TOOL_PALETTE1[3],
    "OPERA-MS"=ASM_TOOL_PALETTE1[4],
    "metaSPAdes (H)"=ASM_TOOL_PALETTE1[5],
    # polishing w/ metaT (w/o LR)
    "Racon(MEGAHIT + metaT)"=ASM_TOOL_PALETTE2[2],
    "Racon(metaSPAdes + metaT)"=ASM_TOOL_PALETTE2[3],
    "Racon(OPERA-MS + metaT)"=ASM_TOOL_PALETTE2[4],
    "Racon(metaSPAdes (H) + metaT)"=ASM_TOOL_PALETTE2[5]
377
)
Valentina Galata's avatar
Valentina Galata committed
378
ASM_TOOL_COLORS <- ASM_TOOL_COLORS[ASM_TOOL_NAMES]
379

380
###############
381
# Gene tools
382
383
384
385
386
387
# GENE_TOOL_NAMES <- c(
#     "prodigal_partial"="Prodigal (partial)",
#     "prodigal_total"="Prodigal (total)",
#     "cdhit_unique"="CD-HIT (unique)",
#     "cdhit_total"="CD-HIT (total)"
# )
388

389
###############
390
# CRISPR tools
391

392
393
394
CRISPR_TOOL_NAMES <- c(
    "minced"="MinCED",
    "casc"="CasC"
395
396
)

397
###############
398
# PlasFlow
399
400

# names
401
402
403
404
405
406
PLASFLOW_NAMES <- list(
    statstype=c(
        count="Sequence count",
        sum="Cumulative sequence length [bp]",
        count_pct="Sequence count [%]",
        sum_pct="Cumulative sequence length [%]"
407
408
409
410
411
    ),
    labels=c(
        chromosome="Chromosome",
        plasmid="Plasmid",
        unclassified="Unclassified"
412
413
    )
)
414
# colors
415
PLASFLOW_COLORS <- list(
416
    labels=ggsci::pal_nejm("default", alpha=1)(4)[c(2,3,4)]
417
)
418
names(PLASFLOW_COLORS$labels) <- PLASFLOW_NAMES$labels
419

420
###############
421
422
# RGI
RGI_NAMES <- list(
423
    col=c(
424
425
426
427
428
429
        "Best_Hit_ARO"="ARO term",
        "ARO"="ARO",
        "Drug Class"="Drug class",       
        "Resistance Mechanism"="Resistance mechanism",
        "AMR Gene Family"="Gene family"
    )
430
431
)

432
###############
433
434
435
436
437
438
439
440
# QUAST
QUAST_VARS <- c(
    "# contigs",
    "Largest contig",            
    "Total length",
    "N50",
    "L50",                       
    "# N's per 100 kbp"
441
)