-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
1687 lines (1323 loc) · 50.9 KB
/
server.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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
############ DosePredict
##############################################################################
########################################################Global variables ####
##############################################################################
rm(list=ls())
##### Load Packages
library(mrgsolve)
library(shiny)
library(deSolve)
library(magrittr)
library(ggplot2)
library(PKNCA)
library(data.table)
library(dplyr)
library(RColorBrewer)
library(shinythemes)
library(rmarkdown)
library(knitr)
library(plotly)
library(reshape2)
library(shinyBS)
#Misc
set.seed(1234)
cbPalette <- c(brewer.pal(8,"Set2"),brewer.pal(9,"Set1"),brewer.pal(12,"Set3"))
# please note in the blow models, PD indirect response model (with logistic growth model) is also incorporated for future development
# in this indirect response model, parameters includes as TVIC50, TVEMAX, TVKG, and Nmax.
# In this indirect response model, the PK is assumed to affect (inhibit) the PD output.
# under the current version of the software, the indirect response model is not outputed.
# However, the user can modify the model or output to utilize the PD indirect response model.
####################################################################################################
# 1 comp PK-PD
code1PK <- '
$PARAM TVCL = 17, TVVC = 4.12, TVKA = 0.5,
TVFBIO = 0.5,TVBP= 1,SCA= 1,
TVIC50=0.05, TVEMAX=.4, TVKG=0.076, Nmax=10000000
$CMT GUT CENT PD
$OMEGA @labels ECL EVC EKA EBV EIC EKmax
0.09 0.09 0.09 0.09 0.09 0.09
$MAIN
double CL = TVCL*exp(ECL);
double V = TVVC*exp(EVC);
double KA = TVKA*exp(EKA);;
double FBIO = TVFBIO*exp(EBV);
double IC50 = TVIC50*exp(EIC);
double EMAX = TVEMAX*exp(EKmax);
double KG = TVKG;
double S2 = V/SCA;
double BP = TVBP;
F_GUT = FBIO;
double KE = CL/V;
PD_0 = 100000000;
$ODE
double CP = CENT/S2;
double CPLASMA = CENT/S2;
double CBLOOD = BP*(CENT/S2);
double INH = (EMAX*CPLASMA)/(IC50+CPLASMA);
dxdt_GUT=-KA*GUT;
dxdt_CENT = KA*GUT -KE*CENT;
dxdt_PD = KG*PD*(1-PD/Nmax) - INH*PD;
$TABLE
double conc = CENT/V;
$CAPTURE CL V KA FBIO ECL EVC EKA EBV CP CPLASMA CBLOOD INH IC50
'
# 2 comp PKPD
code2PK <- '
$PARAM TVCL = 17, TVVC = 4.12, TVKA = 0.5,
TVQ1 = 2.9 , TVVP1 = 56.4,
TVFBIO = 0.5,TVBP= 1,SCA= 1,
TVIC50=0.05, TVEMAX=.4, TVKG=0.076, Nmax=10000000
$CMT GUT CENT PERI1 PD
$OMEGA @labels ECL EVC EKA EBV EIC EKmax
0.09 0.09 0.09 0.09 0.09 0.09
$MAIN
double CL = TVCL*exp(ECL);
double V = TVVC*exp(EVC);
double KA = TVKA*exp(EKA);
double Q1 = TVQ1;
double VP1 = TVVP1;
double FBIO = TVFBIO*exp(EBV);
double IC50 = TVIC50*exp(EIC);
double EMAX = TVEMAX*exp(EKmax);
double KG = TVKG;
double S2 = V/SCA;
double BP = TVBP;
F_GUT = FBIO;
double KE = CL/V;
double K23 = Q1/V;
double K32 = Q1/VP1;
PD_0=100000000;
$ODE
double CP = CENT/S2;
double CPLASMA = CENT/S2;
double CBLOOD = BP*(CENT/S2);
double INH = (EMAX*CPLASMA)/(IC50+CPLASMA);
dxdt_GUT=-KA*GUT;
dxdt_CENT = KA*GUT -KE*CENT+ K32*PERI1-K23*CENT;
dxdt_PERI1= - K32*PERI1+K23*CENT;
dxdt_PD = KG*PD*(1-PD/Nmax) - INH*PD;
$TABLE
double conc = CENT/V;
$CAPTURE CL V KA Q1 VP1 FBIO ECL EVC EKA EBV CP CPLASMA CBLOOD INH IC50
'
# 3 comp PDPK
code3PK <- '
$PARAM TVCL = 17, TVVC = 4.12, TVKA = 0.5,
TVQ1 = 2.9 , TVVP1 = 56.4,
TVQ2 = 3.52, TVVP2 = 9.9,
TVFBIO = 0.5,TVBP= 1,SCA= 1,
TVIC50=0.05, TVEMAX=.4, TVKG=0.076, Nmax=10000000
$CMT GUT CENT PERI1 PERI2 PD
$OMEGA @labels ECL EVC EKA EBV EIC EKmax
0.09 0.09 0.09 0.09 0.09 0.09
$MAIN
double CL = TVCL*exp(ECL);
double V = TVVC*exp(EVC);
double KA = TVKA*exp(EKA);
double Q1 = TVQ1;
double VP1 = TVVP1;
double Q2 = TVQ2;
double VP2 = TVVP2;
double FBIO = TVFBIO*exp(EBV);
double IC50 = TVIC50*exp(EIC);
double EMAX = TVEMAX*exp(EKmax);
double KG = TVKG;
double S2 = V/SCA;
double BP = TVBP;
F_GUT = FBIO;
double KE = CL/V;
double K23 = Q1/V;
double K32 = Q1/VP1;
double K24 = Q2/V;
double K42 = Q2/VP2;
PD_0=100000000;
$ODE
double CP = CENT/S2;
double CPLASMA = CENT/S2;
double CBLOOD = BP*(CENT/S2);
double INH = (EMAX*CPLASMA)/(IC50+CPLASMA);
dxdt_GUT=-KA*GUT;
dxdt_CENT = KA*GUT -KE*CENT+ K32*PERI1-K23*CENT + K42*PERI2-K24*CENT;
dxdt_PERI1= - K32*PERI1+K23*CENT;
dxdt_PERI2= - K42*PERI2+K24*CENT;
dxdt_PD = KG*PD*(1-PD/Nmax) - INH*PD;
$TABLE
double conc = CENT/V;
$CAPTURE CL V KA Q1 VP1 Q2 VP2 FBIO ECL EVC EKA EBV CP CPLASMA CBLOOD INH IC50
'
#####################################################################################################################
server = function(input, output) {
##############################################################################
################################################################## 1-Simulate ##
##############################################################################
sim.data <- eventReactive(input$go,{
# TEST input
IDMAXIN<-3
SimTime<-72
DOSEIN<- c(10,50,300)
IIIN<-12
ADDLIN<-2
CLIN <- 10
VCIN<- 50
KAIN<-0.5
FBIOIN<-80
FBIOIN <- (FBIOIN)/100
SCAIN<-1
BPIN<-1
IC50<-0.02
Kmax<-0.4
KG<-0.2
ECL<-0.24
EVC<-0.04
EKA<-0.24
EBV<-0.04
EIC<-0.04
EKmax<-0.04
Q1IN<-4
V2IN<-50
Q2IN<-2
V3IN<-11
IIIN<-24
ADDLIN<-2
################# SHINY INPUT:
# GENERAL:
IDMAXIN<-input$IDmax
SimTime<-(input$SimTime)
#DOSING:
DOSEIN <- as.numeric(unlist(strsplit(as.character(input$DOSE),",")))
IIIN<-input$II
ADDLIN<-input$ADDL
#PK:
CLIN<-input$CL
VCIN<- input$VC
KAIN<-input$KA
FBIOIN<-input$FBIO
FBIOIN <- (FBIOIN)/100
SCAIN<-input$SCALE
BPIN<-input$BP
#PD: fixed for now, in next version of the software is planned to incude PD models
IC50<-0.05
Kmax<-0.6
KG<-0.02
#EIC<-input$EIC
EIC<-0
#EKmax<-input$EKmax
EKmax<-0
#selected model:
selectPKmod <- input$PKmodel
# Simulations # KEEP amt the first column
if (selectPKmod == 1) {
#PK:
CLIN<-input$CL1
VCIN<- input$VC1
KAIN<-input$KA1
FBIOIN<-input$FBIO1
FBIOIN <- (FBIOIN)/100
SCAIN<-input$SCALE1
BPIN<-input$BP1
ECL<-input$ECL1
ECL<-(ECL/100)^2
EVC<-input$EVC1
EVC<-(EVC/100)^2
EKA<-input$EKA1
EKA<-(EKA/100)^2
EBV<-input$EBV1
EBV<-(EBV/100)^2
metadata <- expand.ev(amt=DOSEIN,ID=1:IDMAXIN,
TVCL = CLIN,TVVC=VCIN,TVKA=KAIN,
TVFBIO = FBIOIN,
TVIC50=IC50, TVEMAX=Kmax, TVKG=KG,
SCA =SCAIN,TVBP=BPIN,
ii=IIIN, addl=ADDLIN)
mod1PK <- mcode("pk1", code1PK)
out <-
mod1PK %>%
#drop.re %>%
omat(dmat(ECL,EVC,EKA,EBV,EIC,EKmax))%>%
data_set(metadata) %>%
carry.out(amt)%>%
mrgsim(end=SimTime,delta=.2)
plot(out)
}
if (selectPKmod == 2) {
#PK:
CLIN<-input$CL2
VCIN<- input$VC2
KAIN<-input$KA2
FBIOIN<-input$FBIO2
FBIOIN <- (FBIOIN)/100
SCAIN<-input$SCALE2
BPIN<-input$BP2
ECL<-input$ECL2
ECL<-(ECL/100)^2
EVC<-input$EVC2
EVC<-(EVC/100)^2
EKA<-input$EKA2
EKA<-(EKA/100)^2
EBV<-input$EBV2
EBV<-(EBV/100)^2
Q1IN<-input$Q12
V2IN<-input$V22
metadata <- expand.ev(amt=DOSEIN,ID=1:IDMAXIN,
TVCL = CLIN,TVVC=VCIN,TVKA=KAIN,
TVQ1 = Q1IN , TVVP1 = V2IN,
TVFBIO = FBIOIN,
TVIC50=IC50, TVEMAX=Kmax, TVKG=KG,
SCA =SCAIN,TVBP=BPIN,
ii=IIIN, addl=ADDLIN)
mod2PK <- mcode("pk2", code2PK)
out <-
mod2PK %>%
#drop.re %>%
omat(dmat(ECL,EVC,EKA,EBV,EIC,EKmax))%>%
data_set(metadata) %>%
carry.out(amt)%>%
mrgsim(end=SimTime,delta=.2)
}
if (selectPKmod == 3) {
#PK:
CLIN<-input$CL3
VCIN<- input$VC3
KAIN<-input$KA3
FBIOIN<-input$FBIO3
FBIOIN <- (FBIOIN)/100
SCAIN<-input$SCALE3
BPIN<-input$BP3
ECL<-input$ECL3
ECL<-(ECL/100)^2
EVC<-input$EVC3
EVC<-(EVC/100)^2
EKA<-input$EKA3
EKA<-(EKA/100)^2
EBV<-input$EBV3
EBV<-(EBV/100)^2
Q1IN<-input$Q13
V2IN<-input$V23
Q2IN<-input$Q23
V3IN<-input$V33
metadata <- expand.ev(amt=DOSEIN,ID=1:IDMAXIN,
TVCL = CLIN,TVVC=VCIN,TVKA=KAIN,
TVQ1 = Q1IN , TVVP1 = V2IN,
TVQ2 = Q2IN, TVVP2 = V3IN,
TVFBIO = FBIOIN,
TVIC50=IC50, TVEMAX=Kmax, TVKG=KG,
SCA =SCAIN,TVBP=BPIN,
ii=IIIN, addl=ADDLIN)
mod3PK <- mcode("pk3", code3PK)
out <-
mod3PK %>%
#drop.re %>%
omat(dmat(ECL,EVC,EKA,EBV,EIC,EKmax))%>%
data_set(metadata) %>%
carry.out(amt)%>%
mrgsim(end=SimTime,delta=.2)
}
if (selectPKmod == 4) {
#PK:
CLIN<-input$CL4
VCIN<- input$VC4
KAIN<-input$KA1
FBIOIN<-input$FBIO1
FBIOIN <- (FBIOIN)/100
SCAIN<-input$SCALE4
BPIN<-input$BP4
ECL<-input$ECL4
ECL<-(ECL/100)^2
EVC<-input$EVC4
EVC<-(EVC/100)^2
EKA<-input$EKA1
EKA<-(EKA/100)^2
EBV<-input$EBV1
EBV<-(EBV/100)^2
metadata <- expand.ev(amt=DOSEIN,ID=1:IDMAXIN,
TVCL = CLIN,TVVC=VCIN,TVKA=KAIN,
TVFBIO = FBIOIN,
TVIC50=IC50, TVEMAX=Kmax, TVKG=KG,
SCA =SCAIN,TVBP=BPIN,
ii=IIIN, addl=ADDLIN,cmt=2)
mod1PK <- mcode("pk1", code1PK)
out <-
mod1PK %>%
#drop.re %>%
omat(dmat(ECL,EVC,EKA,EBV,EIC,EKmax))%>%
data_set(metadata) %>%
carry.out(amt)%>%
mrgsim(end=SimTime,delta=.2)
}
if (selectPKmod == 5) {
#PK:
CLIN<-input$CL5
VCIN<- input$VC5
KAIN<-input$KA2
FBIOIN<-input$FBIO2
FBIOIN <- (FBIOIN)/100
SCAIN<-input$SCALE5
BPIN<-input$BP5
ECL<-input$ECL5
ECL<-(ECL/100)^2
EVC<-input$EVC5
EVC<-(EVC/100)^2
EKA<-input$EKA2
EKA<-(EKA/100)^2
EBV<-input$EBV2
EBV<-(EBV/100)^2
Q1IN<-input$Q15
V2IN<-input$V25
metadata <- expand.ev(amt=DOSEIN,ID=1:IDMAXIN,
TVCL = CLIN,TVVC=VCIN,TVKA=KAIN,
TVQ1 = Q1IN , TVVP1 = V2IN,
TVFBIO = FBIOIN,
TVIC50=IC50, TVEMAX=Kmax, TVKG=KG,
SCA =SCAIN,TVBP=BPIN,
ii=IIIN, addl=ADDLIN,cmt=2)
mod2PK <- mcode("pk2", code2PK)
out <-
mod2PK %>%
#drop.re %>%
omat(dmat(ECL,EVC,EKA,EBV,EIC,EKmax))%>%
data_set(metadata) %>%
carry.out(amt)%>%
mrgsim(end=SimTime,delta=.2)
}
if (selectPKmod == 6) {
#PK:
CLIN<-input$CL6
VCIN<- input$VC6
KAIN<-input$KA3
FBIOIN<-input$FBIO3
FBIOIN <- (FBIOIN)/100
SCAIN<-input$SCALE6
BPIN<-input$BP6
ECL<-input$ECL6
ECL<-(ECL/100)^2
EVC<-input$EVC6
EVC<-(EVC/100)^2
EKA<-input$EKA3
EKA<-(EKA/100)^2
EBV<-input$EBV3
EBV<-(EBV/100)^2
Q1IN<-input$Q16
V2IN<-input$V26
Q2IN<-input$Q26
V3IN<-input$V36
metadata <- expand.ev(amt=DOSEIN,ID=1:IDMAXIN,
TVCL = CLIN,TVVC=VCIN,TVKA=KAIN,
TVQ1 = Q1IN , TVVP1 = V2IN,
TVQ2 = Q2IN, TVVP2 = V3IN,
TVFBIO = FBIOIN,
TVIC50=IC50, TVEMAX=Kmax, TVKG=KG,
SCA =SCAIN,TVBP=BPIN,
ii=IIIN, addl=ADDLIN,cmt=2)
mod3PK <- mcode("pk3", code3PK)
out <-
mod3PK %>%
#drop.re %>%
omat(dmat(ECL,EVC,EKA,EBV,EIC,EKmax))%>%
data_set(metadata) %>%
carry.out(amt)%>%
mrgsim(end=SimTime,delta=.2)
}
#test plot
plot(out, CPLASMA+ CBLOOD~.)
# sim dataset
dataset<-as.data.frame(out)
#dataset<- dataset[ which(dataset$CPLASMA >=10E-7 ),] # to exclude very small simuliations
#dataset$CPLASMA[ which(dataset$CPLASMA <=10E-7 )] <-NA
})
############################################################################################
#### #### #### #### #### #### #### #### #### #### #### #########
#### #### 5555 #### #### #### PLOT: Generate the PK spaghetti plot #### #### #### ######
#### #### #### #### #### #### #### #### #### #### #### #########
############################################################################################
PLOTConc <- eventReactive(input$go, {
# SHINY INPUT:
# GENERAL:
IDMAXIN<-input$IDmax
SimTime<-(input$SimTime)
DOSEIN <- as.numeric(unlist(strsplit(as.character(input$DOSE),",")))
Xaxis<-input$Xaxis
effLimit<-input$effConcLim
CmaxLimit<-input$cmaxSafinput
# dataset PREP:
DoseData <- expand.ev(amt=DOSEIN, ID=1:IDMAXIN)
DoseData<-DoseData[,c(1,2)]
plot_data<-sim.data()
#plot_data<-dataset
plot_data<-plot_data[-3]
AllPlotData<-merge(plot_data,DoseData,by="ID",all.x=T)
# clean a bit for IV models
#selected model:
selectPKmod <- input$PKmodel
if (selectPKmod == 4|selectPKmod == 5|selectPKmod == 6) {
AllPlotData<-AllPlotData[-which(AllPlotData$time==0),]
}
# Geomtric Mean Function
gm_mean = function(x, na.rm=TRUE){
exp(sum(log(x[x > 0]), na.rm=na.rm) / length(x))
}
#probs <- c(0.025,0.05, 0.1, 0.25, 0.5, 0.75, 0.9,0.95,0.975)
probs <- c(0.05,0.95)
lowerLim<-probs[1]
upperLim<-probs[2]
AllPlotData%>%
group_by(amt,time)%>%
summarise(
Mean = mean(CPLASMA),
lowerQuant=quantile(CPLASMA,probs =lowerLim),
upperQuant=quantile(CPLASMA,probs =upperLim),
GeoMeanCP = gm_mean(CPLASMA),
meanCB = mean(CBLOOD),
GeoMeanCB = gm_mean(CBLOOD)
) -> SummaryConcData
SummaryConcData = SummaryConcData %>% rename( Dose=amt , Time=time, q5PI = lowerQuant ,q95PI = upperQuant )
SummaryConcData$Dose<-as.factor(SummaryConcData$Dose)
plotobj<-ggplot (SummaryConcData) +
geom_line (aes(x=Time, y=Mean, color=Dose),alpha = 1,size=1.5)+
xlab('Time') +
ylab('Concentrations')+
theme_bw()+
ggtitle("Means Only PK Plot") +
scale_color_manual(name="Doses:",values=cbPalette)+
theme( axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
# panel.border = element_blank(),
panel.background = element_blank(),
axis.text=element_text(size=12),
axis.title=element_text(size=14,face="bold"),
legend.position="top")+
guides(colour = guide_legend(override.aes = list(size=3)))+
scale_x_continuous(breaks=seq(0, SimTime, Xaxis))
plotobj
if (input$logPlot) {
plotobj<-plotobj+scale_y_log10()
plotobj
}
if (input$IndLines) {
AllPlotData2 = AllPlotData %>% rename( Time=time, Conc = CPLASMA , Dose=amt )
AllPlotData2$ID<-as.factor(AllPlotData2$ID)
AllPlotData2$Dose<-as.factor(AllPlotData2$Dose)
plotobj<-plotobj+
geom_line (data=AllPlotData2,aes(x=Time, y=Conc, group=ID,color=Dose),alpha = .2,size=1) +
ggtitle("Means and Individual Profiles PK Plot")
plotobj
}
if (input$PredInterval) {
plotobj<-plotobj+
geom_ribbon(data=SummaryConcData, aes(x=Time, ymin=q5PI, ymax=q95PI,color=Dose), linetype=2, alpha=0.1)+
ggtitle("Means and Prediction Intervals PK Plot")
plotobj
}
if (input$LimitsLines) {
plotobj<-plotobj+
geom_hline(aes(yintercept= effLimit), color="green", linetype="dashed",size=1 )+
geom_hline(aes(yintercept=CmaxLimit ), color="red",size=1.3 )+
annotate("text", x = (SimTime- (1/5)*SimTime), y = effLimit , label = paste0("Efficacy limit=",effLimit), colour = "black", size = 5)+
annotate("text", x = (SimTime- (1/5)*SimTime), y = CmaxLimit , label = paste0("Safety Cmax=",CmaxLimit), colour = "black", size = 5)
plotobj
}
plotobj
})
output$PLOTConc2 <-
renderPlot({
PLOTConc()
})
output$trendPlot <- renderPlotly({
ggplotly(PLOTConc()) %>%
layout(autosize=TRUE)
})
########################################################################################
#### #### #### #### #### #### #### #### #### #### #### ####
#### #### #### #### #### #### Generate the NCA data #### #### #### #### #### ####
#### #### #### #### #### #### #### #### #### #### #### ####
########################################################################################
NCA.data <- eventReactive(input$go, {
IDMAXIN<-input$IDmax
DOSEIN <- as.numeric(unlist(strsplit(as.character(input$DOSE),",")))
SimTime<-(input$SimTime)
NCAfrom<-input$NCAfrom
#NCAfrom<-0
NCAto<-input$NCAto
#NCAto<-12
NCAdata<-sim.data()
#1 Subset the concentration-time data
#NCAdata<-dataset
d.conc<-NCAdata
d.conc<-NCAdata[which(NCAdata$amt==0),]
# to remove all negative values!
d.conc<- d.conc[ which(d.conc$CPLASMA >=0 ),]
#2 create the dose data
IDMAXIN<-input$IDmax
#DOSEIN<- c(10,50,300)
DOSEIN <- as.numeric(unlist(strsplit(as.character(input$DOSE),",")))
d.dose<-expand.ev(Dose=DOSEIN, ID=1:IDMAXIN, time=0 )
#3 Put your concentration data into a PKNCAconc object
myconc <- PKNCAconc(data=d.conc,
formula=CP~time|ID)
#4 Put your dose data into a PKNCAdose object
mydose <- PKNCAdose(data=d.dose,
formula=Dose~time|ID)
#5 Specify the NCA params. start and end are for the general NCA, to the end of Simtime!!
m.intervals<-data.frame(
start=c(NCAfrom),
end= c(NCAto),
auclast=T,
cmax=T,
aucinf.obs=T,
tmax=T,
half.life=T)
# 6 calculate the NCA
my.data.manual<-PKNCAdata(myconc, mydose, intervals=m.intervals)
m.results<-pk.nca(my.data.manual)
results<-as.data.frame(m.results$result)
######7 cast results # may need them later
# casted <- cast(results, ID+start+end~PPTESTCD, mean)
#7 Merge with dosing
doseMerge<-d.dose[,c(1,2)]
all2<-merge(results,doseMerge,all=T)
sim.data.df <- all2
#ncadataset<-sim.data.df
})
########################################################################################
#### #### #### #### #### #### #### #### #### #### #### ####
#### #### #### #### #### #### Plot all the NCA data #### #### #### #### #### ####
#### #### #### #### #### #### #### #### #### #### #### ####
########################################################################################
plotallNCA1 <- eventReactive(input$go, {
# Geomtric Mean Function
gm_mean = function(x, na.rm=TRUE){
exp(sum(log(x[x > 0]), na.rm=na.rm) / length(x))
}
#NCAdata<-all2
NCAdata<-NCA.data()
#The plot
plotobj<-ggplot (NCAdata, aes(x=PPORRES)) +
geom_histogram (aes(group=as.factor(ID),fill=as.factor(Dose)),color="black") +
facet_wrap(~PPTESTCD, scales = "free") +
theme_bw()+
ylab("Count")+xlab("PK Parameter")+
scale_fill_manual(name="Doses",values=cbPalette)+
theme( axis.line = element_line(colour = "black"),
#panel.grid.major = element_blank(),
# panel.grid.minor = element_blank(),
#panel.border = element_blank(),
#panel.background = element_blank(),
axis.text=element_text(size=12),
axis.title=element_text(size=14,face="bold"))
plotobj
})
output$plotallNCA <-
renderPlot({
plotallNCA1()
})
########################################################################################
#### #### #### #### #### #### #### #### #### #### #### ####
#### #### #### #### #### #### Plot all the safety NCA data AUCinf+cmax #### #### #### #### #### ####
#### #### #### #### #### #### #### #### #### #### #### ####
########################################################################################
plotSAFNCA1 <- eventReactive(input$go, {
# input
effLimit<-input$efficacyLine
CmaxLimit<-input$cmaxSafinput
AUCinfSaf<- input$AUCinfSaf
#effLimit<-0.5
#CmaxLimit<-3.0
#AUCinfSaf<-4.0
# Geomtric Mean Function
gm_mean = function(x, na.rm=TRUE){
exp(sum(log(x[x > 0]), na.rm=na.rm) / length(x))
}
#NCAdata<-all2
NCAdata<-NCA.data()
# just keep AUC and Cmax:
NCAdata<-NCAdata[NCAdata$PPTESTCD %in% c("aucinf.obs","cmax" ,"auclast"),]
#summary of the NCA data
NCAdata%>%
group_by(PPTESTCD,ID,start,end,Dose)%>%
summarise(min = min(PPORRES),
max = max(PPORRES),
median=median(PPORRES),
mean=round(mean(PPORRES),2),
GeoMean=round(gm_mean(PPORRES),8))-> SumNCA
# Summaries related to safety AUC and Cmax:
AUC<- NCAdata[NCAdata$PPTESTCD %in% "aucinf.obs",]
Cmax<- NCAdata[NCAdata$PPTESTCD %in% "cmax",]
AUClast<- NCAdata[NCAdata$PPTESTCD %in% "auclast",]
AUC%>%
group_by(PPTESTCD)%>%
summarise(
Limitsafe = AUCinfSaf,
Nallsubjects = length(ID),
NsafeSubjects = length(PPORRES[PPORRES<AUCinfSaf ]),
Psafe=round(100*(NsafeSubjects/Nallsubjects),2),
GeoMean=round(gm_mean(PPORRES),4),
FoldCover=round((Limitsafe /GeoMean),1)
)-> SafAUC
Cmax%>%
group_by(PPTESTCD)%>%
summarise(
Limitsafe = CmaxLimit,
Nallsubjects = length(ID),
NsafeSubjects = length(PPORRES[PPORRES<CmaxLimit ]),
Psafe=round(100*(NsafeSubjects/Nallsubjects),2),
GeoMean=round(gm_mean(PPORRES),4),
FoldCover=round((Limitsafe /GeoMean),1))-> SafCmax
AUClast%>%
group_by(PPTESTCD)%>%
summarise(
Limitsafe = AUCinfSaf,
Nallsubjects = length(ID),
NsafeSubjects = length(PPORRES[PPORRES<AUCinfSaf ]),
Psafe=round(100*(NsafeSubjects/Nallsubjects),2),
GeoMean=round(gm_mean(PPORRES),4),
FoldCover=round((Limitsafe /GeoMean),1)
)-> SafAUClast
#SafSum<-rbind(SafAUC,SafCmax)
#Finding the Max count of the Histogram for plotting purposes
# AUC plot
plotobj<-ggplot (AUC, aes(x=PPORRES)) +
geom_histogram(colour="black")+ facet_wrap(~PPTESTCD, scales = "free")
HistoData<-ggplot_build(plotobj)$data[[1]]
maxCoountHisto<-max(ggplot_build(plotobj)$data[[1]]$count)
SafAUC<-cbind(SafAUC,maxCoountHisto)
# Cmax plot
plotobj<-ggplot (Cmax, aes(x=PPORRES)) +
geom_histogram(colour="black")+ facet_wrap(~PPTESTCD, scales = "free")
HistoData<-ggplot_build(plotobj)$data[[1]]
maxCoountHisto<-max(ggplot_build(plotobj)$data[[1]]$count)
SafCmax<-cbind(SafCmax,maxCoountHisto)
#auc last:
plotobj<-ggplot (AUClast, aes(x=PPORRES)) +
geom_histogram(colour="black")+ facet_wrap(~PPTESTCD, scales = "free")
HistoData<-ggplot_build(plotobj)$data[[1]]
maxCoountHisto<-max(ggplot_build(plotobj)$data[[1]]$count)
SafAUClast<-cbind(SafAUClast,maxCoountHisto)
# combine the Summary data with the histo count data:
AllSums<-rbind(SafAUC,SafCmax,SafAUClast)
#The plot
plotobj<-ggplot (NCAdata, aes(x=PPORRES)) +
geom_histogram (aes(group=as.factor(ID),fill=as.factor(Dose)),color="black") +
facet_wrap(~PPTESTCD, scales = "free", ncol=1) +
ylab("Count")+xlab("PK Parameter")+
theme_bw()+
scale_fill_manual(name="Doses",values=cbPalette)+
theme( axis.line = element_line(colour = "black"),
axis.text=element_text(size=12),
axis.title=element_text(size=14,face="bold"),
strip.text = element_text(size=20))
plotobj
if (input$LimitsLines) {
plotobj<-plotobj+
geom_vline(data= AllSums, aes(xintercept=Limitsafe ), linetype="dashed", size=1.5, colour="red")+
geom_label(data=AllSums, aes(x=Limitsafe,y=-0.15,
label=paste0("Safety Limit=",Limitsafe)))
plotobj
}
plotobj
})
output$plotSAFNCA <-
renderPlot({
plotSAFNCA1()
})
##############################################################################################
#### #### #### #### #### #### #### #### #### ####
#### #### #### #### #### ANALYSIS 3: Generate Time Above Efficacy limit #### #### #### #### ####
#### #### #### #### #### #### #### #### #### ####
##############################################################################################
TimeAboveefficacyLine <- eventReactive(input$go, {
data<-sim.data()
#data<-dataset
#effConcLimit<-1
effConcLimit<-input$effConcLim
#effTimeLimit<-24
SimTime<-(input$SimTime)
# Load your concentration-time data
d.conc<-data
d.conc<-data[which(data$amt==0),]
d.conc$ID<-as.factor(d.conc$ID)
# to cut time in pieces of 3 hours so that we can evaluate Time above Effline:
y<- seq(0,SimTime,3)
#y[1]
for(i in 1:length(y)) {
# i-th element of `u1` squared into `i`-th position of `usq`
d.conc$Seq[d.conc$time>=y[i] &d.conc$time<=y[i+1]] <- i
}
#Time Above per Seq#
TimeAbove<-setDT(d.conc)[CP > effConcLimit, diff((time)), by = .(ID,Seq)]
SumAllTime<-summarise(group_by(TimeAbove, ID),
sum=sum(V1))
IDMAXIN<-input$IDmax
DOSEIN <- as.numeric(unlist(strsplit(as.character(input$DOSE),",")))
d.dose<-expand.ev(Dose=DOSEIN, ID=1:IDMAXIN, time=0 )
# merge with dosing
doseMerge<-d.dose[,c(1,2)]
SumAllTime2<-merge(doseMerge, SumAllTime,all=T)
sim.data.df <- SumAllTime2
})
##############################################################################################
#### #### #### #### #### #### #### #### #### ####
#### #### #### #### #### Plot Time Above Efficacy limit #### #### #### #### ####
#### #### #### #### #### #### #### #### #### ####
##############################################################################################
plotTimeAbove1<- eventReactive(input$go, {
# Efficacy Time limit
effTimeLimit<-input$effTimeLim
#effTimeLimit<-24
effConcLimit<-input$effConcLim
#effConcLimit<-2
SimTime<-(input$SimTime)
# Geomtric Mean Function
gm_mean = function(x, na.rm=TRUE){
exp(sum(log(x[x > 0]), na.rm=na.rm) / length(x))
}
TimeAboveefficacyLine() %>%
#SumAllTime2 %>%
group_by(Dose)%>%
summarise(min = min(sum),
max = max(sum),
nSubjects=length(unique(ID)),
GeoMean=round(gm_mean(sum),4),
median=median(sum),
mean=round(mean(sum),2))-> SummaryTimeAboveEffiacy
#finding the Max count of the Histogram / it will be approximate as the bis width change when we add vline below
plotobj<-ggplot (TimeAboveefficacyLine(), aes(x=sum)) +
#plotobj<-ggplot (SumAllTime2, aes(x=sum)) +
geom_histogram(colour="black")
HistoData<-ggplot_build(plotobj)$data[[1]]
maxCoountHisto<-max(ggplot_build(plotobj)$data[[1]]$count)
# combine the Summary data with the histo count data:
AllSums<-cbind(SummaryTimeAboveEffiacy,maxCoountHisto)
plotobj<-ggplot (TimeAboveefficacyLine(), aes(x=sum)) +
#plotobj<-ggplot (SumAllTime2, aes(x=sum)) +
geom_histogram (aes(group=as.factor(ID),fill=as.factor(Dose)),color="black")+
theme_bw()+
scale_fill_manual(name="Doses",values=cbPalette)+
theme( axis.line = element_line(colour = "black"),
axis.text=element_text(size=12),
axis.title=element_text(size=14,face="bold"),
strip.text = element_text(size=20))+