-
Notifications
You must be signed in to change notification settings - Fork 0
/
targets.Rmd
1378 lines (1227 loc) · 48.4 KB
/
targets.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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Figures for Bidirectional crosstalk between epithelial-mesenchymal plasticity and IFNγ-induced PD-L1 expression promotes tumor progression"
author: "Gerhard Burger & Carlijn Lems"
output: html_document
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
knitr::opts_chunk$set(tar_interactive = F)
options(scipen=999)
```
# Packages
```{r, eval = FALSE}
# DON'T USE THIS use renv::restore
# also DESCRIPTION now handles dependencies for renv
install.packages("remotes")
remotes::install_github("ropensci/targets")
remotes::install_github("clauswilke/colorblindr")
remotes::install_cran(c("drc", "colorspace", "grid", "cowplot", "magick", "targets", "tarchetypes", "scales", "glue", "colorblindr", "fs", "rootSolve", 'tidyverse','interp'))
source("R/functions.R")
source("R/common_plotstuff.R")
```
# Setup
First, load `targets` to activate the specialized `knitr` engine for Target Markdown.
```{r}
library(targets)
# my custom target utils
tar_mload <- function(names) {
tar_make({{names}})
tar_load({{names}}, envir = parent.frame(n = 2))
}
tar_mread <- function(name) {
tar_mload({{name}})
name
}
```
```{r}
tar_unscript()
```
# Globals
We first define some global options/functions common to all targets. The function below plots a histogram of ozone concentrations, and our histogram target will need it.
```{targets 00globals, tar_globals = TRUE}
options(tidyverse.quiet = TRUE)
options(scipen=999)
source("R/functions.R")
source("R/common_plotstuff.R")
tar_option_set(packages = c("drc", "cowplot", "magick", "grid", "colorspace", "tarchetypes", "scales", "glue", "magrittr", "colorblindr", "fs", "rootSolve", "tidyverse", "interp"))
library(tarchetypes)
text_size <- 15
label_size <- round(text_size *1.5)
save_device <- "pdf"
```
# Targets
## Figures
### 1. Model construction
```{targets figure1_pre}
list(
# biorender svg is imported pdf in inkscape with poppler
tar_target(jakstat_file, "data/biorender/biorender_jakstatpdl.svg", format = "file"),
tar_target(model_file, "data/inkscape/TCSandPDL1.svg", format = "file"),
tar_target(jakstat_bior, ggdraw() + draw_image(image_read(jakstat_file, density = 300))),
tar_target(model_plot, ggdraw() + draw_image(image_read(model_file, density = 300)))
)
```
#### Time course data
```{targets figure1_timecourse}
list(
tar_target(ifnstat_file, 'data/time_courses_sub/jakstat_time_course_ifn1.txt', format = "file"),
tar_target(ifnpdl_file, 'data/time_courses_sub/ifn_jak_stat_pdl_timecourse_ifn1.txt', format = "file"),
tar_target(ifnstat_timecourse, read_copasi(ifnstat_file)),
tar_target(ifnpdl_timecourse, read_copasi(ifnpdl_file)),
tar_target(stcs_timecourse, read_copasi(emt_no_int_file)),
tar_target(ifnstat_tc_plot,
ggplot(ifnstat_timecourse) +
geom_line(aes(x = `# Time` / 60, y = `[x10]`), size = 1.1) +
labs(x = "Time (minutes)", y = "STAT1p_2\n(nM)") +
theme_cowplot()),
tar_target(ifnpdl_tc_plot,
ggplot(ifnpdl_timecourse) +
geom_line(aes(x = `# Time`, y = `[PM]`), size = 1.1) +
scale_x_continuous(breaks = seq(0,48, by = 12), limits = c(0,48)) +
labs(x = "Time (hours)", y = "PD-L1 membr.\n(molecules)") +
theme_cowplot()),
tar_target(stcs_tc_plot,
ggplot(stcs_timecourse) +
geom_line(aes(x = `# Time` / 24, y = `[mZ]`), size = 1.1) +
labs(x = "Time (days)", y = "ZEB1 mRNA\n(molecules)") +
theme_cowplot()),
tar_target(tc_aligned, cowplot::align_plots(ifnstat_tc_plot, ifnpdl_tc_plot, stcs_tc_plot, align = "v", axis = 'l')),
tar_target(tc_combined, plot_grid(tc_aligned[[1]], tc_aligned[[2]], tc_aligned[[3]], nrow = 1))
)
```
#### TCS model
```{targets tcs_bif}
list(
tar_target(labels_p1, tribble(
~text, ~x, ~y,
"E", 187000, 110,
"E/M", 205000, 420,
"M", 205000, 920
) %>% mutate(x = x / 1e5)),
tar_target(stcs_data_wbp,
core %>%
nest(data = everything()) %>%
rowwise() %>%
mutate(
bp = list(determine_BP(data, "# Values[S].InitialValue", "[mZ]")),
data = list(data %>% mutate(interval = findInterval(`[mZ]`, bp)))
) %>%
unnest(cols = data) %>%
mutate(stable = (interval %% 2) == 0)),
tar_target(stcs_plot,
stcs_data_wbp %>%
ggplot(aes(`# Values[S].InitialValue`/1e5, `[mZ]`, group = interval,
linetype = stable)) +
geom_line(size = 1.2) +
scale_linetype_discrete(guide = "none", limits = c(T,F)) +
labs(x = bquote('SNAIL1 ('*10^5~'molecules)'), y = 'ZEB1 mRNA (molecules)') +
geom_text(data = labels_p1, aes(x,y, label = text),
fontface = "bold", inherit.aes = F, show.legend = FALSE) +
coord_cartesian(xlim = c(1.8, 2.3), ylim = c(0, 1000)) +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = expansion(mult = c(0,0.05))) +
expand_limits(y = 0) +
theme_cowplot() +
theme(legend.position = "top"))
)
```
```{targets tcs_tc}
tcs_tc_path <- "data/time_courses_tcs"
list(
tar_target(E_tc_file, path(tcs_tc_path, "core_emt_E200000_to_S215000.txt"), format = "file"),
tar_target(EM_tc_file, path(tcs_tc_path, "core_emt_EM200000_to_S215000.txt"), format = "file"),
tar_target(M_tc_file, path(tcs_tc_path, "core_emt_M200000_to_S215000.txt"), format = "file"),
tar_target(E_ss_file, path(tcs_tc_path, "core_emt_E200000_to_S215000_SS.txt"), format = "file"),
tar_target(EM_ss_file, path(tcs_tc_path, "core_emt_EM200000_to_S215000_SS.txt"), format = "file"),
tar_target(M_ss_file, path(tcs_tc_path, "core_emt_M200000_to_S215000_SS.txt"), format = "file"),
tar_target(tc_data, tibble(emt = c("E", "E/M", "M"),
file = c(E_tc_file, EM_tc_file, M_tc_file)) %>%
mutate(emt = factor(emt, levels = c("M", "E/M", "E"))) %>%
group_by(emt) %>%
rowwise() %>%
summarise(data = list(read_copasi(file)), .groups = "drop") %>%
unnest(data)),
tar_target(ss_data, tibble(emt = c("E", "E/M", "M"),
file = c(E_ss_file, EM_ss_file, M_ss_file)) %>%
mutate(emt = factor(emt, levels = c("M", "E/M", "E"))) %>%
group_by(emt) %>%
rowwise() %>%
summarise(data = list(read_copasi(file) %>%
arrange(`# Values[S].InitialValue`)),
.groups = "drop") %>%
unnest(data)),
tar_target(ss_data_min, ss_data %>% group_by(emt) %>%
filter(`# Values[S].InitialValue` == min(`# Values[S].InitialValue`))),
tar_target(ss_data_max, ss_data %>% group_by(emt) %>%
filter(`# Values[S].InitialValue` == max(`# Values[S].InitialValue`))),
tar_target(tc_plot,
ggplot(tc_data, aes(`# Time` / 24, `[mZ]`, color = emt)) +
geom_line(size = 1.2, show.legend = F) +
labs(x = "Time (days)", y = "ZEB mRNA (molecules)") +
coord_cartesian(ylim = c(0, 1000)) +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = expansion(mult = c(0,0.05))) +
expand_limits(y = 0) +
theme_cowplot()),
tar_target(tc_ss_plot,
stcs_plot +
geom_point(data = ss_data_min, aes(`# Values[S].InitialValue`, `[mZ]`, color = emt),
size = 3, inherit.aes = F, show.legend = F) +
geom_line(data = ss_data, aes(`# Values[S].InitialValue`, `[mZ]`, color = emt),
size = 1.2, inherit.aes = F, show.legend = F,
arrow = arrow(length = unit(0.1, "inches"), type = "closed"))),
tar_target(tc_tot_aligned, cowplot::align_plots(stcs_plot, tc_ss_plot,
align = "v", axis = 'l'))
)
```
```{targets tc_ss_alt_plot, tar_simple = T}
ss_data_minmax <- left_join(
ss_data_max %>% mutate(S = 215000, mZ_high = `[mZ]`) %>% select(emt, starts_with("mZ")),
ss_data_min %>% mutate(S = 200000, mZ_low = `[mZ]`) %>% select(emt, starts_with("mZ")),
by = "emt"
) %>%
mutate(emt = factor(emt, levels = c("M", "E/M", "E")))
stcs_plot +
geom_point(data = ss_data_min, aes(2, `[mZ]`, color = emt),
size = 2.5, inherit.aes = F, show.legend = F) +
geom_point(data = ss_data_min, aes(2.15, `[mZ]`, color = emt),
size = 2.5, inherit.aes = F, show.legend = F) +
geom_segment(data = ss_data_min, aes(x = 2, y = `[mZ]`, color = emt,
xend = 2.15, yend = `[mZ]`),
size = 1.2, inherit.aes = F, show.legend = F, linetype = "dashed") +
geom_segment(data = ss_data_minmax, aes(x = 2.15, y = mZ_low, color = emt,
xend = 2.15, yend = mZ_high),
size = 1.2, arrow = arrow(length = unit(0.1, "inches"), type = "closed"),
inherit.aes = F, show.legend = F) +
geom_text(data = labels_p1, aes(x,y, label = text),
fontface = "bold", inherit.aes = F, show.legend = FALSE)
```
#### Combined
```{targets figure1}
list(
tar_target(figure1, {
plot_grid(
plot_grid(jakstat_bior, model_plot,
nrow = 1, rel_widths = c(1.1,2),
labels = c("A", "B"), label_size = label_size),
NULL, NULL,
plot_grid(tc_aligned[[1]], NULL, tc_aligned[[2]], tc_aligned[[3]],
nrow = 1, rel_widths = c(1,0.05,1,1),
labels = c("C", "D","", "F"), label_size = label_size),
plot_grid(NULL,
tc_ss_alt_plot,
NULL,
tc_plot,
NULL,
nrow = 1, align = "h", rel_widths = c(0.05, 1,0.05,1,1),
labels = c("E","", "", "", ""), label_size = label_size),
ncol = 1, rel_heights = c(2.5, 0.05, 0.1, 1,1.7)
)
}),
tar_target(figure1_file, {
path <- "output/figure1.pdf"
ggsave(path, plot = figure1, width = 12, height = 9, device = save_device)
})
)
```
### 2. IFNy -> EMT, EMT -> PD-L1
```{targets figure2-source-files}
bifs_path <- "data/bifs"
list(
tar_target(twod_copasi_file, path(bifs_path, "twod_kmu_mod.txt.xz"), format = "file"),
tar_target(core_emt_file, path(bifs_path, "core_emt.txt.xz"), format = "file"),
tar_target(ifn_bif_file, path(bifs_path, "full_model_ifn_bif_kmu_mod.txt.xz"), format = "file"),
tar_target(twod_copasi, {
read_copasi(twod_copasi_file) %>%
filter(`# Values[S].InitialValue` >= 170000,
`# Values[S].InitialValue` <= 230001)
}),
tar_target(core, read_copasi(core_emt_file)),
tar_target(g_I_cont, read_copasi(ifn_bif_file))
)
```
#### Snail bifurcation
```{targets bif_globals}
list(
tar_target(xmin, 170000 / 1e5),
tar_target(xmax, (230000+1) / 1e5),
tar_target(snail_for_ifn_bif, 195000 / 1e5),
tar_target(exp_order, c("no_int", "ifn0", "ifn1")),
tar_target(exp_labels, c("TCS",
expression(paste('IFN', gamma, ' = 0 nM')),
expression(paste('IFN', gamma, ' = 0.1 nM'))
) %>% setNames(exp_order))
)
```
```{targets bifs_snail_data, tar_simple = T}
data <- tribble(
~experiment, ~data,
"no_int", core,
"ifn0", twod_copasi %>% filter(`Values[I (global)].InitialValue` == 0),
"ifn1", twod_copasi %>% filter(`Values[I (global)].InitialValue` == 0.1),
) %>%
rowwise() %>%
mutate(
bp = list(determine_BP(data, "# Values[S].InitialValue", "[mZ]")),
data = list(data %>% mutate(interval = findInterval(`[mZ]`, bp)))
) %>%
unnest(cols = data) %>%
mutate(stable = (interval %% 2) == 0)
data %>% mutate(experiment = factor(experiment, levels = exp_order))
```
```{targets bif_snail_zeb, tar_simple = T}
labels_p1 <- tribble(
~text, ~x, ~y,
"E", 177000, 90,
"E/M", 200000, 430,
"M", 205000, 920
) %>% mutate(x = x / 1e5)
bifs_snail_data %>%
ggplot(aes(`# Values[S].InitialValue` / 1e5, `[mZ]`, group = interaction(experiment, interval), linetype = stable, color = experiment)) +
geom_line(size = 1.2) +
geom_vline(xintercept = snail_for_ifn_bif, linetype = "dashed") +
scale_linetype_discrete(guide = "none", limits = c(T,F)) +
scale_color_OkabeIto(order = c(8,2,1), labels = exp_labels, name = NULL) +
labs(x = bquote('SNAIL1 ('*10^5~'molecules)'), y = 'ZEB1 mRNA (molecules)') +
geom_text(data = labels_p1, aes(x,y, label = text), fontface = "bold", inherit.aes = F, show.legend = FALSE) +
coord_cartesian(xlim = c(xmin,xmax)) +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
expand_limits(y = 0) +
theme(legend.position = "top")
```
```{targets bif_snail_pdl, tar_simple = T}
labels_p2 <- tribble(
~experiment, ~text, ~x, ~y,
"ifn0", "E", 180000, 24000,
"ifn0", "E/M", 211000, 16000,
"ifn0", "M", 225000, 67000,
"ifn1", "E", 175000, 65000,
"ifn1", "E/M", 200000, 115000,
"ifn1", "M", 210000, 310000
) %>% mutate(x = x / 1e5)
bifs_snail_data %>%
filter(experiment != "no_int") %>%
ggplot(aes(`# Values[S].InitialValue`/ 1e5, `[PM]`, group = interaction(experiment, interval), linetype = stable, color = experiment)) +
geom_line(size = 1.2) +
geom_vline(xintercept = snail_for_ifn_bif, linetype = "dashed") +
scale_linetype_discrete(guide = "none", limits = c(T,F)) +
scale_color_OkabeIto(order = c(2,1), labels = exp_labels, name = NULL) +
labs(x = bquote('SNAIL1 ('*10^5~'molecules)'), y = 'PD-L1 membrane (molecules)') +
geom_text(data = labels_p2, aes(x,y, label = text, color = experiment), fontface = "bold", inherit.aes = F, show.legend = FALSE) +
coord_cartesian(xlim = c(xmin,xmax), ylim = c(0, 3.2e5)) +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
theme(legend.position = "top")
```
#### IFN bifurcation
```{targets bif_I_P, tar_simple = T}
mZ_breaks_g_I_cont <- determine_BP(g_I_cont, "# Values[I (global)].InitialValue", "[mZ]")
g_I_cont %>% mutate(
interval = findInterval(`[mZ]`, mZ_breaks_g_I_cont),
stable = (interval %% 2) == 0
) -> g_I_cont_pd
labels_bifIP <- tribble(
~text, ~x, ~y,
"E", 0.075, 0.45e5,
"E/M", 0.075, 1.05e5,
"M", 0.075, 2.65e5
)
g_I_cont_pd %>%
ggplot(aes(`# Values[I (global)].InitialValue`, `[PM]`, group = interval, linetype = stable)) +
geom_line(size = 1.2) +
scale_linetype_discrete(guide = "none", limits = c(T,F)) +
coord_cartesian(xlim = c(0, 0.1), ylim = c(0, 3.2e5)) +
geom_text(data = labels_bifIP, aes(x,y, label = text), fontface = "bold",
inherit.aes = F, show.legend = FALSE) +
xlab(expression(paste('IFN', gamma, ' (nM)'))) +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
ylab('PD-L1 membrane (molecules)')
```
#### 2D bif
```{targets fixed_bps_twod_copasi, tar_simple = T}
twod_copasi %>%
drop_na() %>%
nest_by(`Values[I (global)].InitialValue`) -> nested
# Find bifurcations x and y points
nested <- nested %>%
mutate(
bpx = list(determine_BP(data, "# Values[S].InitialValue", "[mZ]", res = "x")),
bpy = list(determine_BP(data, "# Values[S].InitialValue", "[mZ]", res = "y")),
)
# sometimes too many bps are detected
wonky <- nested %>% filter(length(bpx) != 4)
## Inspect the wonky bps
# wonky %>% mutate(
# data = list(data %>% mutate(interval = findInterval(`[mZ]`, bpy)))
# ) %>%
# select(-bpx, -bpy) %>%
# unnest(data) %>%
# ggplot(aes(`# Values[S].InitialValue`,`[mZ]`, color = as.factor(interval))) +
# geom_point() +
# facet_wrap(vars(`Values[I (global)].InitialValue`))
correct_bps <- tibble(
`Values[I (global)].InitialValue` = wonky$`Values[I (global)].InitialValue`,
bps = list(
c(1,2,5,6),
c(1,2,5,6),
c(1:4),
c(1:4),
c(1:4)
)
)
fixed <- wonky %>%
left_join(correct_bps, by = "Values[I (global)].InitialValue") %>%
rowwise() %>%
mutate(
bpx = list(bpx[bps]),
bpy = list(bpy[bps])
) %>%
select(-bps)
## check fixed bps
# fixed %>% mutate(
# data = list(data %>% mutate(interval = findInterval(`[mZ]`, bpy)))
# ) %>%
# select(-bpx, -bpy) %>%
# unnest(data) %>%
# ggplot(aes(`# Values[S].InitialValue`,`[mZ]`, color = as.factor(interval))) +
# geom_point() +
# facet_wrap(vars(`Values[I (global)].InitialValue`))
nested %>%
rows_update(fixed, by = "Values[I (global)].InitialValue")
# TEMP solution
# nested %>% filter(length(bpx) == 4)
```
```{targets twod_copasi_bps_res}
list(
tar_target(twod_bps_raw, {
fixed_bps_twod_copasi %>%
summarize(bp_to_tibble(bpx), .groups = "drop") %>%
rename(IFN = `Values[I (global)].InitialValue`)
}),
tar_target(twod_bps_smooth, {
BP1_sp <- spline(twod_bps_raw$IFN, twod_bps_raw$BP1, xmax = 0.1, n = 10*length(twod_bps_raw$IFN))
BP2_sp <- spline(twod_bps_raw$IFN, twod_bps_raw$BP2, xmax = 0.1, n = 10*length(twod_bps_raw$IFN))
BP3_sp <- spline(twod_bps_raw$IFN, twod_bps_raw$BP3, xmax = 0.1, n = 10*length(twod_bps_raw$IFN))
BP4_sp <- spline(twod_bps_raw$IFN, twod_bps_raw$BP4, xmax = 0.1, n = 10*length(twod_bps_raw$IFN))
BP_smooth <- tibble(IFN = BP1_sp$x, BP1 = BP1_sp$y, BP2 = BP2_sp$y, BP3 = BP3_sp$y, BP4 = BP4_sp$y)
})
)
```
```{targets init_model_pd, tar_simple = T}
ggplot(twod_bps_smooth) +
geom_ribbon(aes(y = IFN, xmin = xmin, xmax = xmax, fill = '{E}')) +
geom_ribbon(aes(y = IFN, xmin = BP4 / 1e5, xmax = xmax, fill = '{E, M}')) +
geom_ribbon(aes(y = IFN, xmin = BP2 / 1e5, xmax = xmax, fill = '{E, E/M, M}')) +
geom_ribbon(aes(y = IFN, xmin = BP1 / 1e5, xmax = xmax, fill = '{E/M, M}')) +
geom_ribbon(aes(y = IFN, xmin = BP3 / 1e5, xmax = xmax, fill = '{M}')) +
geom_vline(xintercept = snail_for_ifn_bif, linetype = "dashed") +
coord_cartesian(ylim = c(0,0.1), expand = F) +
labs(x = bquote('SNAIL1 ('*10^5~'molecules)'), y = expression(paste('IFN', gamma, ' (nM)')), fill = NULL) +
#scale_fill_manual(name = "", values = c('{E, E/M, M}' = custom.col[15], '{E, M}' = custom.col[4], '{E/M, M}' = custom.col[13], '{E}' = custom.col[6], '{M}' = custom.col[24]), limits = c('{E}', '{E, M}', '{E, E/M, M}', '{E/M, M}', '{M}')) +
scale_fill_OkabeIto(order = 3:7, limits = c('{E}','{E, M}', '{E, E/M, M}', '{E/M, M}', '{M}')) +
scale_y_continuous(breaks = seq(0, 0.1, by =0.02))
```
#### Combined
```{targets figure2}
list(
tar_target(figure2, {
plot_grid(bif_snail_pdl + theme_cowplot(text_size)+ theme(legend.position="top", legend.justification = "right"),
bif_I_P + theme_cowplot(text_size)+ theme(legend.position="top"),
bif_snail_zeb + theme_cowplot(text_size)+ theme(legend.position="top", legend.justification = "right"),
init_model_pd + theme_cowplot(text_size) + theme(legend.position="top", legend.justification = "center"),
labels = "AUTO", label_size = label_size, ncol = 2, align = "vh", axis = "tlbr",
scale = 0.99) #< allow spacing for labels
}),
tar_target(figure2_file, {
path <- "output/figure2.pdf"
ggsave(path, plot = figure2, width = 12, height = 10, device = save_device)
})
)
```
### 3. Temporal dynamics
```{targets temp_dyn_files}
emt_td_path <- "data/emt_td"
list(
tar_target(emt_no_int_file, path(emt_td_path, "EMT no interaction fluxes.txt"), format = "file"),
tar_target(emt_ifn0_file, path(emt_td_path, "EMT no IFN fluxes kmu.txt"), format = "file"),
tar_target(emt_ifn1_file, path(emt_td_path, "EMT IFN fluxes kmu.txt"), format = "file"),
tar_target(met_no_int_file, path(emt_td_path, "MET no interaction fluxes.txt"), format = "file"),
tar_target(met_ifn0_file, path(emt_td_path, "MET no IFN fluxes kmu.txt"), format = "file"),
tar_target(met_ifn1_file, path(emt_td_path, "MET IFN fluxes kmu.txt"), format = "file")
)
```
```{targets time_dynamics_long, tar_simple = T}
time_dynamics <- tribble(
~type, ~experiment, ~file,
"EMT", "no_int", emt_no_int_file,
"EMT", "ifn0", emt_ifn0_file,
"EMT", "ifn1", emt_ifn1_file,
"MET", "no_int", met_no_int_file,
"MET", "ifn0", met_ifn0_file,
"MET", "ifn1", met_ifn1_file,
) %>%
rowwise() %>%
mutate(data = list(read_copasi(file))) %>%
select(-file) %>% unnest(data)
time_dynamics <- time_dynamics %>%
mutate(
across(all_of(c("[mP]", "[PM]","(R5).Flux")), ~ ifelse(experiment == "no_int", NA, .x)),
time = `# Time`/24
) %>% select(-`# Time`)
time_dynamics %>%
pivot_longer(cols = c(where(is.numeric), -time))
```
```{targets td_gvars}
list(
tar_target(td_vars, c("[mu2]", "[mZ]", "[Z]", "[mP]", "[PE]", "[PG]", "[PM]",
"[mF]", "[F]")),
tar_target(td_labels, c(
"miR-200",
"ZEB1 mRNA",
"ZEB1",
"PD-L1 mRNA",
"PD-L1 ER",
"PD-L1 Golgi",
"PD-L1 membrane",
"IRF1 mRNA",
"IRF1"
) %>% setNames(td_vars)),
tar_target(td_vars_to_show, c("[mu2]", "[mZ]", "[Z]", "[mP]", "[PM]"))
)
```
```{targets figure3, tar_simple = T}
time_dynamics_long %>%
filter(name %in% td_vars_to_show) %>%
mutate(experiment = factor(experiment, levels = exp_order)) %>%
ggplot(aes(time,value, color = experiment)) +
geom_line(aes(linetype = experiment), size = 1.2) +
facet_grid(rows = vars(factor(name, levels = td_vars)), cols = vars(type),
scales = "free_y", switch = "y",
labeller = labeller(.rows = as_labeller(td_labels))) +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = expansion(mult = c(0,0.05))) +
expand_limits(y = 0) +
scale_color_OkabeIto(order = c(8,2,1), labels = exp_labels, name = NULL) +
scale_linetype_manual(values = c("dashed", "solid", "solid"), labels = exp_labels, name = NULL) +
labs(x = "Time (days)", y = "Regulator (molecules)") +
theme_cowplot(text_size) +
panel_border() +
#background_grid() +
theme(strip.placement = 'outside',
strip.background = element_blank(),
strip.text.x = element_text(size = 1.2*text_size, face = "bold"),
panel.spacing.x = unit(1, "lines"),
legend.position = "top",
legend.justification = "right")
```
#### Combined
```{targets fig3}
tar_target(figure3_file, {
path <- "output/figure3.pdf"
ggsave(path, plot = figure3, width = 12, height = 10, device = save_device)
})
```
## SI
### S1. STAT-PDL model
```{targets statpdl_model}
list(
tar_target(statpdl_model_file, "data/inkscape/statpdl1.svg", format = "file"),
tar_target(statpdl_model_fig,
ggdraw() + draw_image(image_read(statpdl_model_file, density = 300)))
)
```
```{targets statpdl_tc_plot, tar_simple = T}
vars <- c( "[mP]", "[PE]", "[PG]", "[PM]", "[mF]", "[F]")
labels <- c(
"PD-L1 mRNA",
"PD-L1 ER",
"PD-L1 Golgi",
"PD-L1 membrane",
"IRF1 mRNA",
"IRF1 protein"
) %>% setNames(vars)
var_order <- c("[mF]", "[F]", "[mP]", "[PE]", "[PG]", "[PM]")
ifnpdl_timecourse %>%
select(-`[STAT]`) %>%
mutate(time = `# Time`) %>%
select(-`# Time`) %>%
pivot_longer(cols = c(where(is.numeric), -time)) %>%
ggplot(aes(time,value)) +
geom_line(size = 1.2) +
facet_wrap(vars(factor(name, levels = var_order)), ncol = 2,
scales = "free_y",
labeller = as_labeller(labels)) +
scale_x_continuous(breaks = seq(0,48, by = 12), limits = c(0,48), expand = c(0,0)) +
scale_y_continuous(expand = expansion(mult = c(0,0.05))) +
expand_limits(y = 0) +
labs(x = "Time (hours)", y = "(molecules)") +
theme_cowplot(text_size) +
panel_border() +
#background_grid() +
theme(strip.background = element_blank())
```
#### Combined
```{targets si_statpdl_model, tar_simple = T}
plot_grid(statpdl_model_fig, statpdl_tc_plot,
nrow = 1, rel_widths = c(1, 2),
labels = "AUTO", label_size = label_size)
```
```{targets si_statpdl_model_file, tar_simple = T}
path <- "output/si_statpdl_model.pdf"
ggsave(path, plot = si_statpdl_model, width = 8, height = 4, device = save_device)
```
### S2. PD-L1 for different EMT states
Smooth and interpolate data to be able to use geom_raster
```{targets interped_data, tar_simple = T}
# Add EMT status to data based on BP interval
fixed_bps_twod_copasi %>%
mutate(
data = list(data %>% mutate(interval = findInterval(`[mZ]`, bpy)))
) %>%
select(-bpx,-bpy) %>%
unnest(data) %>%
filter((interval %% 2) == 0 ) %>%
left_join(tibble(interval = c(0,2,4), emt = c("E", "E/M", "M")), by = "interval") %>%
select(-interval) %>%
rename(
IFN = `Values[I (global)].InitialValue`,
S = `# Values[S].InitialValue`
) -> sumtest
# take average for duplicates
sumtest %>%
select(IFN, S, emt, `[PM]`) %>%
group_by(IFN, S, emt) %>%
summarise(across(where(is.numeric), mean), .groups = "drop") -> simple_sum
# reduce x resolution to solve missing values and make suitable for interp
xintervals <- with(simple_sum, seq(min(S), max(S), length = 101))
xmidinterval <- xintervals + diff(xintervals)[1]
simple_sum %>%
ungroup() %>%
mutate(s_bin = findInterval(S, xintervals)) %>%
group_by(IFN, s_bin, emt) %>%
summarize(`[PM]` = mean(`[PM]`), .groups = "drop") %>%
left_join(tibble(s_bin = seq_along(xintervals), s_bin_mid = xmidinterval), by = "s_bin") %>%
mutate(S = s_bin_mid) %>%
select(-starts_with("s_bin")) -> simple_sum2
simple_sum2 %>%
nest_by(emt) %>%
mutate(
data = list({
interp(data$S / 1e5, data$IFN, data$`[PM]`,
xo = xmidinterval / 1e5, yo = unique(data$IFN)) %>%
interp2xyz(data.frame = T) %>%
rename(S = x, IFN = y, `[PM]` = z) %>%
as_tibble() %>%
drop_na()
}))-> interped_data_full
bounds <- tibble(
emt = c("E", "E/M", "M"),
bounds = list(
twod_bps_raw %>% select(IFN, s_max = BP1) %>% add_column(s_min = 0),
twod_bps_raw %>% select(IFN, s_min = BP2, s_max = BP3),
twod_bps_raw %>% select(IFN, s_min = BP4) %>% add_column(s_max = Inf)
)
)
# remove interpolation outside hull
interped_data_full %>%
left_join(bounds, by = "emt") %>%
mutate(
data= list(data %>%
left_join(bounds, by = "IFN") %>%
filter(S > s_min / 1e5, S < s_max / 1e5) %>%
select(-s_min, -s_max)
)
) %>%
select(-bounds) %>%
unnest(data)
```
```{targets si_pdl_emt_plot_data, tar_simple = T}
interped_data %>%
group_by(IFN, S) %>%
summarize(`[PM]` = mean(`[PM]`, na.rm =T), .groups = "drop") -> averaged
bind_rows(interped_data, averaged %>% add_column(emt = "Average")) %>%
mutate(emt = factor(emt, levels = c("E", "E/M", "M", "Average")))
```
```{targets si_pdl_emt_plot, tar_simple=T}
si_pdl_emt_plot_data %>%
ggplot(aes(S,IFN)) +
geom_raster(aes(fill = `[PM]` / 1e5)) +
facet_wrap(vars(emt), nrow = 1) +
scale_fill_viridis_c(limits = function(x) {x[1] <- 0; x}) +
labs(x = bquote('SNAIL1 ('*10^5~'molecules)'),
y = expression(paste('IFN', gamma, ' (nM)')),
fill = bquote('PD-L1 membrane ('*10^5~'molecules)')) +
geom_path(data = twod_bps_smooth %>% pivot_longer(cols = starts_with("BP")),
aes(x = value / 1e5, y = IFN, group = name),
inherit.aes = F, size = 1, color = "white") +
coord_cartesian(expand = F) +
scale_y_continuous(breaks = seq(0, 0.1, by =0.02)) +
theme_cowplot(text_size) +
panel_border() +
theme(strip.background = element_blank()) +
theme(legend.position="top",
legend.justification = "right")
```
#### Combined
```{targets si_pdl_emt_output}
list(
tar_target(si_pdl_emt_file, {
path <- "output/si_pdl_emt.pdf"
ggsave(path, plot = si_pdl_emt_plot, width = 12, height = 4, device = save_device)
})
)
```
### S3. Simplified model
```{targets si_simplified}
list(
tar_target(simp_model_file, "data/inkscape/TCSandPDL1_wo_jakstat.svg", format = "file"),
tar_target(simple_model_2d_bif_file, "data/bifs/core_emt_plus_pdl_2d_bif.txt.xz", format = "file"),
tar_target(simple_model_2d_bif, read_copasi(simple_model_2d_bif_file))
)
```
```{targets si_simp_2d_bif, tar_simple = T}
simple_model_2d_bif %>%
drop_na() %>%
nest_by(`Values[F].InitialValue`) -> nested
nested <- nested %>%
mutate(
bps = list(determine_BP(data, "# Values[S].InitialValue", "[mZ]", res = "x"))
)
nested %>%
filter(length(bps) <= 4) %>% # sometimes too many bps are detected
summarize(bp_to_tibble(bps), .groups = "drop") %>%
rename(FF = `Values[F].InitialValue`) -> sum2
spline_xmax <- 3e5
BP1_sp <- spline(sum2$FF, sum2$BP1, xmax = spline_xmax, n = 10*length(sum2$FF))
BP2_sp <- spline(sum2$FF, sum2$BP2, xmax = spline_xmax, n = 10*length(sum2$FF))
BP3_sp <- spline(sum2$FF, sum2$BP3, xmax = spline_xmax, n = 10*length(sum2$FF))
BP4_sp <- spline(sum2$FF, sum2$BP4, xmax = spline_xmax, n = 10*length(sum2$FF))
BP_smooth <- tibble(FF = BP1_sp$x, BP1 = BP1_sp$y, BP2 = BP2_sp$y, BP3 = BP3_sp$y, BP4 = BP4_sp$y)
p2 <- ggplot(BP_smooth) +
geom_ribbon(aes(y = FF, xmin = xmin, xmax = xmax, fill = '{E}')) +
geom_ribbon(aes(y = FF, xmin = BP4 / 1e5, xmax = xmax, fill = '{E, M}')) +
geom_ribbon(aes(y = FF, xmin = BP2 / 1e5, xmax = xmax, fill = '{E, E/M, M}')) +
geom_ribbon(aes(y = FF, xmin = BP1 / 1e5, xmax = xmax, fill = '{E/M, M}')) +
geom_ribbon(aes(y = FF, xmin = BP3 / 1e5, xmax = xmax, fill = '{M}')) +
coord_cartesian(ylim = c(0,300000)) +
labs(x = bquote('SNAIL1 ('*10^5~'molecules)'), y = "I (molecules)", fill = NULL) +
#scale_fill_manual(name = "", values = c('{E, E/M, M}' = custom.col[15], '{E, M}' = custom.col[4], '{E/M, M}' = custom.col[13], '{E}' = custom.col[6], '{M}' = custom.col[24]), limits = c('{E}', '{E, M}', '{E, E/M, M}', '{E/M, M}', '{M}')) +
scale_fill_OkabeIto(order = 3:7, limits = c('{E}','{E, M}', '{E, E/M, M}', '{E/M, M}', '{M}')) +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
theme_cowplot(text_size)
```
#### Combined
```{targets si_simp_output}
list(
tar_target(si_simp_output,
plot_grid(ggdraw() + draw_image(image_read(simp_model_file, density = 300)),
si_simp_2d_bif + theme(legend.position="top", legend.justification = "center"),
nrow = 1, rel_widths = c(1,1),
labels = c("A", "B"), label_size = label_size, scale = 0.90)
),
tar_target(si_simp_file, {
path <- "output/si_simplified_model.pdf"
ggsave(path, plot = si_simp_output, width = 12, height = 4, device = save_device)
})
)
```
### S4. Sensitivity
```{targets sens_files}
list(
tar_files_input(sens_files_sep,
fs::path("data/sensitivity/",
c("lmp2 0.16.txt.xz",
"lmp2 0.18.txt.xz",
"lmp2 0.22.txt.xz",
"lmp2 0.24.txt.xz",
"lmp3 0.8.txt.xz",
"lmp3 0.9.txt.xz",
"lmp3 1.1.txt.xz",
"lmp3 1.2.txt.xz",
"lmump2 0.04.txt.xz",
"lmump2 0.045.txt.xz",
"lmump2 0.055.txt.xz",
"lmump2 0.06.txt.xz",
"lmump3 0.8.txt.xz",
"lmump3 0.9.txt.xz",
"lmump3 1.1.txt.xz",
"lmump3 1.2.txt.xz",
"ltp1 0.8.txt.xz",
"ltp1 0.9.txt.xz",
"ltp1 1.1.txt.xz",
"ltp1 1.2.txt.xz",
"ltp2 0.24.txt.xz",
"ltp2 0.27.txt.xz",
"ltp2 0.33.txt.xz",
"ltp2 0.36.txt.xz",
"ltp3 0.04.txt.xz",
"ltp3 0.045.txt.xz",
"ltp3 0.055.txt.xz",
"ltp3 0.06.txt.xz",
"mu2_0 lmp 11000.txt.xz",
"mu2_0 lmp 12000.txt.xz",
"mu2_0 lmp 8000.txt.xz",
"mu2_0 lmp 9000.txt.xz",
"mu2_0 lmump 11000.txt.xz",
"mu2_0 lmump 12000.txt.xz",
"mu2_0 lmump 8000.txt.xz",
"mu2_0 lmump 9000.txt.xz",
"mu2_0 ltp 11000.txt.xz",
"mu2_0 ltp 12000.txt.xz",
"mu2_0 ltp 8000.txt.xz",
"mu2_0 ltp 9000.txt.xz")
)
),
tar_target(sens_file_unchanged, "data/sensitivity/unchanged.txt.xz", format = "file")
)
```
```{targets sens_tibble}
list(
tar_target(meta, {
meta <- tibble(file = sens_files_sep) %>%
# extract parameter and value from file name
mutate(str_match(path_file(file), "(.*) ([\\d\\.]+[\\d])(\\.txt)?")[,c(2,3)] %>%
`colnames<-`(c("par", "val")) %>%
as_tibble()) %>%
mutate(val = as.numeric(val)) %>%
# sort before adding change
group_by(par) %>%
arrange(val, .by_group = T) %>%
# add change (every parameter has -20%, -10%, +10%, +20%)
mutate(change = rep_along(val, c(-0.2, -0.1, 0.1, 0.2))) %>%
# add unchanged.txt
ungroup() %>%
add_row(tibble_row(file = sens_file_unchanged, par = "base", change = 0))
}),
tar_target(all_nested, {
meta %>%
# read data
rowwise() %>%
mutate(data = list(read_copasi(file))) %>%
select(-file) %>%
# computate bifurcation
mutate(bp = list(determine_BP(data, "# Values[S].InitialValue", "[mZ]"))) %>%
# include bp info in data
mutate(data = list(data %>% mutate(interval = findInterval(`[mZ]`, bp)))) %>%
select(-bp)
}),
tar_target(par_meta, {
par_meta <- tribble(
~par, ~col, ~row,
"lmp2", "mRNA_PD-L1", "l2",
"lmp3", "mRNA_PD-L1", "l3",
"lmump2", "miR-200", "l2",
"lmump3", "miR-200", "l3",
"ltp1", "PD-L1_ER", "l1",
"ltp2", "PD-L1_ER", "l2",
"ltp3", "PD-L1_ER", "l3",
"mu2_0 lmp", "mRNA_PD-L1", "mu0",
"mu2_0 lmump", "miR-200" ,"mu0",
"mu2_0 ltp", "PD-L1_ER", "mu0"
)
# l1, l2, and l3, are really l0, l1, and l2
row_names <- c(paste0("l", 1:3), "mu0")
row_labels <- c(
expression(gamma[mu*0]/gamma["m"*0]/"l"[0]),
expression(gamma[mu*1]/gamma["m"*1]/"l"[1]),
expression(gamma[mu*2]/gamma["m"*2]/"l"[2]),
expression(mu[0])
) %>% setNames(row_names)
col_names <- c("miR-200", "mRNA_PD-L1", "PD-L1_ER")
col_labels <- c(
expression("miR-200 (Y"[mu]*")"),
expression("mRNA PD-L1 (Y"["m"]*")"),
expression("PD-L1 in ER (L)"[])
) %>% setNames(col_names)
par_meta %>% mutate(
row = factor(row, labels = row_labels),
col = factor(col, labels = col_labels)
)
}),
tar_target(sens_labels, {
vars <- c("lmp2", "lmp3", "lmump2", "lmump3", "ltp1", "ltp2", "ltp3", "mu2_0 lmp", "mu2_0 lmump", "mu2_0 ltp")
c(expression('l'[mP2]),
expression('l'[mP3]),
expression('l'[paste(mu,'mP2')]),
expression('l'[paste(mu,'mP3')]),
expression('l'[tP1]),
expression('l'[tP2]),
expression('l'[tP3]),
expression(paste(mu['0'], ' ', '(L'['p'['mP']], ')')),
expression(paste(mu['0'], ' ', '(L'['p'[paste(mu,'mP')]], ')')),
expression(paste(mu['0'], ' ', '(L'['p'['tP']], ')'))
) %>% setNames(vars)
}),
tar_target(sens_xmin, 170000 / 1e5),
tar_target(sens_xmax, 220000 / 1e5)
)
```
```{targets sens_plot}
list(
tar_target(sens_all, {
# add model value to all pars (plotting from 2 different data frames I didn't get working)
sens <- all_nested %>% filter(par != "base")
base_add <- sens %>% expand(par) %>% add_column(all_nested %>% filter(par == "base") %>% select(-par))
all <- bind_rows(sens, base_add) %>%
unnest(cols = data) %>%
mutate(stable = (interval %% 2) == 0) %>%
mutate(change = factor(change,
levels = c(-0.2, -0.1, 0, 0.1, 0.2),
labels = c("-20%", "-10%", "Model value", "+10%", "+20%"),
ordered = T))
}),
tar_target(sens_pdl_plot, {
sens_all %>%
left_join(par_meta, by = "par") %>%
mutate(par = factor(par, labels = sens_labels)) %>%
filter(row != "mu[0]") %>%
ggplot(aes(`# Values[S].InitialValue` / 1e5, `[PM]`, linetype = stable,
color = change, group = interaction(interval, change)))+
geom_line(size = 1) +
facet_grid(rows = vars(row), cols = vars(col), labeller = label_parsed) +
scale_linetype_discrete(guide = "none", limits = c(T,F)) +
scale_color_discrete_diverging(palette = "Blue-Red") +
labs(color = NULL,
x = bquote('SNAIL1 ('*10^5~'molecules)'),