forked from Deredge-Lab-UMB/HExD-IPA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HExD.R
4472 lines (4076 loc) · 212 KB
/
HExD.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
{
#install.packages('mixtools')
#install.packages('ggpmisc')
#install.packages('ggplot2')
#install.packages('splus2R')
#install.packages('openxlsx')
#install.packages('data.table')
#install.packages('zoo')
#install.packages('pracma')
#install.packages('flextable')
#install.packages('officer')
#install.packages('webshot')
#install.packages('plyr')
#install.packages('dplyr')
#install.packages('magick')
#install.packages('reprex')
#install.packages('ggformula')
#install.packages('ggthemes')
#install.packages('jpeg')
#install.packages('grid')
#install.packages('gridExtra')
#install.packages('minpack.lm')
#install.packages('readr')
#install.packages('qcpR')
#webshot::install_phantomjs()
} #FIRST TIME ONLY
{
library(mixtools)
library(ggpmisc)
library(ggplot2)
library(splus2R)
library(openxlsx)
library(data.table)
library(zoo)
library(pracma)
library(flextable)
library(officer)
library(webshot)
library(plyr)
library(dplyr)
library(magick)
library(reprex)
library(ggformula)
library(ggthemes)
library(jpeg)
library(grid)
library(gridExtra)
library(minpack.lm)
library(readr)
show_col_types = FALSE
} #Call Packages Needed
###Span good range 0.3 to 0.8, maxit good range 90 to 150, epsilon good range 1e-03 to 1e-08.
### Initial should be set to span = 0.5, Maxit=150, Epsiol=1e-06. If the code goes infinate set span = 0.7, and epsilon=1e-03.
{
span=0.5
maxit=150
epsilon = 1e-06
}
#Data Setup
{
setwd("~/Documents/University of Maryland, Baltimore/Deredge Lab/Wintrode Lab Rotation/AbTIR 2020/AbTIR_HExD_EX1 Deconvolution/Peptide 58 to 69_Apo")
data <- read_csv("58-69_Apo.csv")
show_col_types = FALSE
num_peptime <- c (1, 2, 3 ,4 ,5 ,6,7,8,9,10,11)
charge_list <- c(2)
Pep_Name <- c("AbTIR_58-69_Apo")
}
#Plot Setup
{
lwd_mz <- 0.8 #line width of mass spec data graph
lwd_env <-1.3 #line width of all envelopes
axis_font <- 15 #axis font size
border_width <- 1.1 #width of black border
}
#Select Analysis Mode
{
time <- c(1,10,11,60,61,600,660,3600,3960,7200,7560) #time series data, if none leave as c()
mutant <- c() #time series data with, if none leave as c()
temp <- c() #temperature change data, if none leave as c()
conc <- c() #Concentration change data, if none leave as c()
}
#Analysis Graph Modes
{
Log=TRUE #used to graph the X-axis as a log of the value = TRUE if log, = FALSE if linear
Linear=FALSE #in reference to a linear graphing of x-values (not taking the log of it). = TRUE if linear, = FALSE if log
Line = TRUE #Plot a line graph
Bar = FALSE #plot a bar graph
}
###Code Cleaning (Once after IPA, then comment out again):
#XUndeut <- Undeut
#XTD <- TD
#If your on a Mac running Mac OS, you will need Xquartz to run the following step. This can be installed from here: https://www.xquartz.org/
data_list <- qpcR:::cbind.na(XUndeut,X10.sec,X11.sec,X60.sec,X61.sec,X10.min,X11.min,X1.hr,X1.1.hr,X2.hr,X2.1.hr) #Load in IPA data
#Initial Values Call, No need to change ever
{
Peptide_Names <- c(colnames(data))
output <- list()
c.fit_tot <- list()
x_max <- max(data[grep(pattern = "x|X", x=colnames(data))])
x_min <- min(data[grep(pattern = "x|X", x=colnames(data))])
}
#Important notes:
#1a) A good number of iterations is between 15 to 60 Iterations for a simple system
#1b) If the system shows unimodality and a bad fit/if the system goes infinate within NLSLM fitting then aim for 3 iterations
#2) The first thing to change with any errors is the Span. A good test is to go up and down within the listed range
#3) If the span doesn't work, change the epsilon
#4) If nothing works, check the IPA to ensure the proper peaks were selected
#5) If the timepoint flags a false bimodal at a later timepoint, change Num_envelops to =1 and color_1 = 4
#6) if the colors of the two envelops are switched, change color_1 to =4 and color_3 to =3 and rerun just the plot section
#7) If there is a Chi squared error it means that the fit is too far from the original data and should be rerun
#8) When running the code, be sure to have the plots tab open to visually catch any fitting issues
#9) If a block in a timepoint section fails, address the error if needed and then rerun startng from the data assignment block of that section
#10) Each block under each timepiont section is the same, so the naming is only applied to the first 4 blocks for reference.
###First Time Point
Color_1=3
Color_2=4
Num_Envelops=2
Replicate=FALSE
{
k=1
if (length(charge_list)>1){
charge <- charge_list[k]
}else{
charge <- charge_list[1]
}
data_x <- na.omit(data_list[k+(k-1)])
data_y <- na.omit(data_list[k+1+(k-1)])
x_i <- c(data_x$x)
y_i <- c(data_y$y)
df <- data.frame(x_i,y_i)
charge_shift =1/charge
x <- data[print(Peptide_Names[k+(k-1)])]
x <- na.omit(x[[1]])
y.sg4 <- data[print(Peptide_Names[k+1+(k-1)])]
y.sg4 <- na.omit(y.sg4[[1]])
y.sg4 <- savgol(y.sg4, 3, 4, 0)
df_smooth <- data.frame(x, y.sg4)
} #Data Assignment and Initial Calls
{
mixture<-normalmixEM(x_i, sd.constr = c("a", "a"), arbmean = TRUE, arbvar = TRUE, maxit=maxit, epsilon = epsilon, ECM=TRUE)
ptest <- predict(loess(y_i ~ x_i, family="gaussian", span= span), df)
p_pred <- data.frame(x_i, ptest)
amp1 <- approx(p_pred$x_i, p_pred$ptest, xout=mixture$mu[1])
amp1_test <- amp1
amp2 <- approx(p_pred$x_i, p_pred$ptest, xout=mixture$mu[2])
amp2_test <-amp2
} #Mixed Effects Modeling and Amplitude Prediction via regression (finds peak at non-discrete datapoint)
{
if(mixture$mu[1] == mixture$mu[2] | (mixture$mu[2] - 1) < mixture$mu[1] | Num_Envelops == 1){
x <- x_i
amp1 <- max(c(amp1$y, amp2$y))
fit <- nlsLM(y_i ~ (C1*exp(-(x-mean1)**2/(2 * sigma1**2))),
data = df,
start = list(mean1 = mixture$mu[1],
sigma1 = mixture$sigma[1],
C1 = amp1),
lower =c(0,mixture$sigma[1],0),
upper=c(Inf, mixture$sigma[1], 100),
algorithm = "port",
)
dffit <- data.frame(x=x_i)
dffit$y <- predict(fit, newdata=dffit)
fit.sum <- summary(fit)
fit.sum
coef.fit <- fit.sum$coefficients
mu.fit <- coef.fit[1]
sigma.fit <- coef.fit[2]
c.fit <- coef.fit[3]
}else{
x <- x_i
amp1 <- approx(p_pred$x, p_pred$ptest, xout=mixture$mu[1])
amp2 <- approx(p_pred$x, p_pred$ptest, xout=mixture$mu[2])
if(length(time)!=0 && k!=1 | length(conc)!=0 && k!=1){
if(Replicate==FALSE){
c1_high <- output[[k-1]]$Height[1]
c2_low <- output[[k-1]]$Height[2]
}
if(Replicate==TRUE){
c1_high <- 100
c2_low <- 0
}
}else{
c1_high <- 100
c2_low <- 0
}
fit <- nlsLM(y_i ~ (C1*exp(-(x-mean1)**2/(2 * sigma1**2)) +
C2*exp(-(x-mean2)**2/(2 * sigma2**2))),
data = df,
start = list(mean1 = mixture$mu[1],
mean2 = mixture$mu[2],
sigma1 = mixture$sigma[1],
sigma2 = mixture$sigma[2],
C1 = amp1$y,
C2 = amp2$y),
lower=c(0,mixture$mu[1],mixture$sigma[1],mixture$sigma[2],0,c2_low),
upper=c(Inf, Inf, mixture$sigma[1], mixture$sigma[2], c1_high, 100),
algorithm = "port",
)
dffit <- data.frame(x=x_i)
dffit$y <- predict(fit, newdata=dffit)
fit.sum <- summary(fit)
fit.sum
coef.fit <- fit.sum$coefficients[,1]
mu.fit <- coef.fit[1:2]
sigma.fit <- coef.fit[3:4]
c.fit <- coef.fit[5:6]
}
} #NLSLM (Non-Linear Least Squares Levenberg-Marquardt) fitting. Fits the two bimodal envelops
{
plot_x <- x_i
plot_y <-y_i
plot_df <- data.frame(plot_x, plot_y)
smooth_x <- df_smooth$x
smooth_y <- df_smooth$y.sg4
#original
p1 <- ggplot(data=df_smooth, aes(smooth_x,smooth_y), colour="black") +
geom_line(lwd=lwd_mz)+
xlim(x_min,x_max)+
ggtitle(c(print(paste0(Peptide_Names[k+(k-1)]," ", paste0(Pep_Name))))) +
theme_tufte()+
theme(panel.border = element_rect(colour = "black", fill=NA, size=border_width),
plot.title = element_text(color="black", size=axis_font, face="bold"),
axis.title = element_blank(),
axis.text.x = element_text(face="bold", color="Black",size=axis_font),
axis.text.y = element_text(face="bold", color="Black",size=axis_font)
)
#overall fit
p1<- p1+geom_line(data=plot_df, aes(plot_x,plot_y), color="red", lwd=lwd_env)
#components of the fit
if(Num_Envelops==1){
x <- dffit$x
y <- (c.fit[1] *exp(-(x-mu.fit[1])**2/(2 * sigma.fit[1]**2)))
lo <- spline(x, y, method="natural")
p1 <- p1 + geom_line(data=data.frame(lo), aes(lo$x,lo$y),color=Color_1, lwd=lwd_env)
centroid <- ((sum(x_i*y_i))/sum(y_i))
plot_sum_y <- y
plot_sum_df <- data.frame(x,plot_sum_y)
if(length(time)!=0 | length(conc)!=0){
ADI_1 <- abs(0)
ADI_df <- c(ADI_1, 0)
}
FWHM_Sum = 2*sqrt(log(2)*2)*sigma.fit[1]
}else{
if(length(mu.fit)>1){
x <- dffit$x
y <- (c.fit[1] *exp(-(x-mu.fit[1])**2/(2 * sigma.fit[1]**2)))
lo <- spline(x, y, method="natural")
y2 <- (c.fit[2] *exp(-(x-mu.fit[2])**2/(2 * sigma.fit[2]**2)))
lo2 <- spline(x, y2, method="natural")
p1 <- p1 + geom_line(data=data.frame(lo), aes(lo$x,lo$y),color=Color_1, lwd=lwd_env)+
geom_line(data=data.frame(lo2), aes(lo2$x,lo2$y),color=Color_2, lwd=lwd_env)
plot_sum_y <- y+y2
plot_sum_df <- data.frame(x,plot_sum_y)
p1 <- p1 + geom_line(data=plot_sum_df, aes(x,plot_sum_y), color="#E69F00", lwd=lwd_env)
if(length(time)!=0 | length(conc)!=0){
ADI_1 <- 0
ADI_2 <- 0
ADI_df <- c(ADI_1, ADI_2)
}
FWHM_Sum = 2*sqrt(log(2)*2)*(sqrt((sigma.fit[1]^2)+(sigma.fit[2]^2)))
}else{
x <- dffit$x
y <- (c.fit[1] *exp(-(x-mu.fit[1])**2/(2 * sigma.fit[1]**2)))
lo <- spline(x, y, method="natural")
p1 <- p1 + geom_line(data=data.frame(lo), aes(lo$x,lo$y),color=Color_1, lwd=lwd_env)
plot_sum_y <- y
plot_sum_df <- data.frame(x,plot_sum_y)
centroid <- ((sum(x_i*y_i))/sum(y_i))
FWHM_Sum = 2*sqrt(log(2)*2)*sigma.fit[1]
if(length(time)!=0 | length(conc)!=0){
ADI_1 <- 0
ADI_df <- c(ADI_1, 0)
}
}
}
plot(p1)
ggsave(paste0('p',k,"_",Pep_Name,".jpeg"),p1,width=7,height=7)
FWHM_1 = 2*sqrt(log(2)*2)*sigma.fit[1]
FWHM_2 = 2*sqrt(log(2)*2)*sigma.fit[2]
FWHM <- FWHM_1
FWHM_Tot <- c(FWHM_Sum,NA)
y_AUC_1 <- c.fit[1] *exp(-(x-mu.fit[1])**2/(2 * sigma.fit[1]**2))
id <- order(dffit$x)
AUC_1 <- sum(diff(dffit$x[id])*rollmean(y_AUC_1[id],2))
AUC_1
y_AUC_2 <- c.fit[2] *exp(-(x-mu.fit[2])**2/(2 * sigma.fit[2]**2))
AUC_2 <- sum(diff(dffit$x[id])*rollmean(y_AUC_2[id],2))
AUC_2
AUC_tot_x <- dffit$x
AUC_tot_y <- plot_sum_y
id_2 <- order(dffit$x)
AUC_tot_dat <- sum(diff(AUC_tot_x[id_2])*rollmean(AUC_tot_y[id_2],2))
AUC_peaks_x <- plot_x
AUC_peaks_y <- plot_y
id_3 <- order(plot_x)
AUC_peaks_dat <- sum(diff(AUC_peaks_x[id_3])*rollmean(AUC_peaks_y[id_3],2))
AUC_Norm <- c((AUC_1/AUC_tot_dat),(AUC_2/AUC_tot_dat))
AUC_list <- c(AUC_1, AUC_2)
AUC_tot <- c(AUC_tot_dat, NA)
Chi_sqr <- (sum(plot_sum_y - plot_y)^2)/(sum(plot_y))
Chi_crit <- qchisq(p=0.95, df=(length(plot_y)-1))
if(Chi_sqr > Chi_crit){
stop("Chi Squared Value Past Critical Point")
}
centroid <- ((sum(x_i*y_i))/sum(y_i))
if(amp1_test$y<amp2_test$y){
c.fit_tot[[k]] <- c(0,c.fit)
}else{
c.fit_tot[[k]] <-c.fit
}
if(length(time)!=0 | length(conc)!=0){
if(Num_Envelops==1 && Color_1==3){
if(is.na(AUC_1) == FALSE && is.na(AUC_2)==FALSE){
if(AUC_Norm[1] >= 0.9 | AUC_Norm[2] >= 0.9 | AUC_Norm[1]<= 0.1 | AUC_Norm[2] <=0.1){
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1], Chi_sqr, c(1), ADI_df[1], AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area', 'Chi Squared', 'Normalized Area', 'Relative DA', 'Area Total', 'Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak")))
}
}else {
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1],Chi_sqr, c(1),ADI_df[1], AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area','Chi Squared', 'Normalized Area','Relative DA', 'Area Total','Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak")))
}
}
if(Num_Envelops==1 && Color_1==4){
if(is.na(AUC_1) == FALSE && is.na(AUC_2)==FALSE){
if(AUC_Norm[1] >= 0.9 | AUC_Norm[2] >= 0.9 | AUC_Norm[1]<= 0.1 | AUC_Norm[2] <=0.1){
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1], Chi_sqr, c(1), ADI_df[1], AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area', 'Chi Squared', 'Normalized Area', 'Relative DA', 'Area Total', 'Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak Env2")))
}
}else {
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1],Chi_sqr, c(1),ADI_df[1], AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area','Chi Squared', 'Normalized Area','Relative DA', 'Area Total','Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak Env2")))
}
}
if(Num_Envelops==2){
if(is.na(AUC_1) == FALSE && is.na(AUC_2)==FALSE){
if(AUC_Norm[1] >= 0.9 | AUC_Norm[2] >= 0.9 | AUC_Norm[1]<= 0.1 | AUC_Norm[2] <=0.1){
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1], Chi_sqr, c(1), ADI_df[1], AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area', 'Chi Squared', 'Normalized Area', 'Relative DA', 'Area Total', 'Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak")))
}else {
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit, centroid, c.fit, sigma.fit, FWHM, AUC_list, Chi_sqr, AUC_Norm, ADI_df, AUC_tot, FWHM_Tot))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area','Chi Squared', 'Normalized Area','Relative DA', 'Area Total', 'Total Width')
rownames(output[[k]]) <- c((print(paste0(Peptide_Names[k+(k-1)]," ", "Bimodal Peak 1"))), (print(paste0(Peptide_Names[k+(k-1)]," ", "Bimodal Peak 2"))))
}
}else {
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1],Chi_sqr, c(1),ADI_df[1], AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area','Chi Squared', 'Normalized Area','Relative DA', 'Area Total','Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak")))
}
}
}else{
if(Num_Envelops==1 && Color_1==3){
if(is.na(AUC_1) == FALSE && is.na(AUC_2)==FALSE){
if(AUC_Norm[1] >= 0.9 | AUC_Norm[2] >= 0.9 | AUC_Norm[1]<= 0.1 | AUC_Norm[2] <=0.1){
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1], Chi_sqr, c(1), AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area', 'Chi Squared', 'Normalized Area', 'Area Total', 'Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak")))
}
}else {
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1],Chi_sqr, c(1), AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area','Chi Squared', 'Normalized Area','Area Total','Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak")))
}
}
if(Num_Envelops==1 && Color_1==4){
if(is.na(AUC_1) == FALSE && is.na(AUC_2)==FALSE){
if(AUC_Norm[1] >= 0.9 | AUC_Norm[2] >= 0.9 | AUC_Norm[1]<= 0.1 | AUC_Norm[2] <=0.1){
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1], Chi_sqr, c(1), AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area', 'Chi Squared', 'Normalized Area', 'Area Total', 'Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak Env2")))
}
}else {
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1],Chi_sqr, c(1), AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area','Chi Squared', 'Normalized Area', 'Area Total','Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak Env2")))
}
}
if(Num_Envelops==2){
if(is.na(AUC_1) == FALSE && is.na(AUC_2)==FALSE){
if(AUC_Norm[1] >= 0.9 | AUC_Norm[2] >= 0.9 | AUC_Norm[1]<= 0.1 | AUC_Norm[2] <=0.1){
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1], Chi_sqr, c(1), AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area', 'Chi Squared', 'Normalized Area', 'Area Total', 'Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak")))
}else {
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit, centroid, c.fit, sigma.fit, FWHM, AUC_list, Chi_sqr, AUC_Norm, AUC_tot, FWHM_Tot))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area','Chi Squared', 'Normalized Area', 'Area Total', 'Total Width')
rownames(output[[k]]) <- c((print(paste0(Peptide_Names[k+(k-1)]," ", "Bimodal Peak 1"))), (print(paste0(Peptide_Names[k+(k-1)]," ", "Bimodal Peak 2"))))
}
}else {
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1],Chi_sqr, c(1), AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area','Chi Squared', 'Normalized Area', 'Area Total','Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak")))
}
}
}
} #Plot creation, Data table creation, and Chi Square Testing
###Second Time Point
Color_1=3
Color_2=4
Num_Envelops=2
Replicate=FALSE
{
k=2
if (length(charge_list)>1){
charge <- charge_list[k]
}else{
charge <- charge_list[1]
}
data_x <- na.omit(data_list[k+(k-1)])
data_y <- na.omit(data_list[k+1+(k-1)])
x_i <- c(data_x$x)
y_i <- c(data_y$y)
df <- data.frame(x_i,y_i)
charge_shift =1/charge
x <- data[print(Peptide_Names[k+(k-1)])]
x <- na.omit(x[[1]])
y.sg4 <- data[print(Peptide_Names[k+1+(k-1)])]
y.sg4 <- na.omit(y.sg4[[1]])
y.sg4 <- savgol(y.sg4, 3, 4, 0)
df_smooth <- data.frame(x, y.sg4)
}
{
mixture<-normalmixEM(x_i, sd.constr = c("a", "a"), arbmean = TRUE, arbvar = TRUE, maxit=maxit, epsilon = epsilon, ECM=TRUE)
ptest <- predict(loess(y_i ~ x_i, family="gaussian", span= span), df)
p_pred <- data.frame(x_i, ptest)
amp1 <- approx(p_pred$x_i, p_pred$ptest, xout=mixture$mu[1])
amp1_test <- amp1
amp2 <- approx(p_pred$x_i, p_pred$ptest, xout=mixture$mu[2])
amp2_test <-amp2
}
{
if(mixture$mu[1] == mixture$mu[2] | (mixture$mu[2] - 1) < mixture$mu[1] | Num_Envelops == 1){
x <- x_i
amp1 <- max(c(amp1$y, amp2$y))
fit <- nlsLM(y_i ~ (C1*exp(-(x-mean1)**2/(2 * sigma1**2))),
data = df,
start = list(mean1 = mixture$mu[1],
sigma1 = mixture$sigma[1],
C1 = amp1),
lower =c(0,mixture$sigma[1],0),
upper=c(Inf, mixture$sigma[1], 100),
algorithm = "port",
)
dffit <- data.frame(x=x_i)
dffit$y <- predict(fit, newdata=dffit)
fit.sum <- summary(fit)
fit.sum
coef.fit <- fit.sum$coefficients
mu.fit <- coef.fit[1]
sigma.fit <- coef.fit[2]
c.fit <- coef.fit[3]
}else{
x <- x_i
amp1 <- approx(p_pred$x, p_pred$ptest, xout=mixture$mu[1])
amp2 <- approx(p_pred$x, p_pred$ptest, xout=mixture$mu[2])
if(length(time)!=0 && k!=1 | length(conc)!=0 && k!=1){
if(Replicate==FALSE){
c1_high <- output[[k-1]]$Height[1]
c2_low <- output[[k-1]]$Height[2]
}
if(Replicate==TRUE){
c1_high <- 100
c2_low <- 0
}
}
fit <- nlsLM(y_i ~ (C1*exp(-(x-mean1)**2/(2 * sigma1**2)) +
C2*exp(-(x-mean2)**2/(2 * sigma2**2))),
data = df,
start = list(mean1 = mixture$mu[1],
mean2 = mixture$mu[2],
sigma1 = mixture$sigma[1],
sigma2 = mixture$sigma[2],
C1 = amp1$y,
C2 = amp2$y),
lower=c(0,mixture$mu[1],mixture$sigma[1],mixture$sigma[2],0,c2_low),
upper=c(Inf, Inf, mixture$sigma[1], mixture$sigma[2], c1_high, 100),
algorithm = "port",
)
dffit <- data.frame(x=x_i)
dffit$y <- predict(fit, newdata=dffit)
fit.sum <- summary(fit)
fit.sum
coef.fit <- fit.sum$coefficients[,1]
mu.fit <- coef.fit[1:2]
sigma.fit <- coef.fit[3:4]
c.fit <- coef.fit[5:6]
}
}
{
plot_x <- x_i
plot_y <-y_i
plot_df <- data.frame(plot_x, plot_y)
smooth_x <- df_smooth$x
smooth_y <- df_smooth$y.sg4
#original
p2 <- ggplot(data=df_smooth, aes(smooth_x,smooth_y), colour="black") +
geom_line(lwd=lwd_mz)+
xlim(x_min,x_max)+
ggtitle(c(print(paste0(Peptide_Names[k+(k-1)]," ", paste0(Pep_Name))))) +
theme_tufte()+
theme(panel.border = element_rect(colour = "black", fill=NA, size=border_width),
plot.title = element_text(color="black", size=axis_font, face="bold"),
axis.title = element_blank(),
axis.text.x = element_text(face="bold", color="Black",size=axis_font),
axis.text.y = element_text(face="bold", color="Black",size=axis_font)
)
#overall fit
p2<- p2+geom_line(data=plot_df, aes(plot_x,plot_y), color="red", lwd=lwd_env)
#components of the fit
if(Num_Envelops==1){
x <- dffit$x
y <- (c.fit[1] *exp(-(x-mu.fit[1])**2/(2 * sigma.fit[1]**2)))
lo <- spline(x, y, method="natural")
p2 <- p2 + geom_line(data=data.frame(lo), aes(lo$x,lo$y),color=Color_1, lwd=lwd_env)
centroid <- ((sum(x_i*y_i))/sum(y_i))
plot_sum_y <- y
plot_sum_df <- data.frame(x,plot_sum_y)
if(length(time)!=0 | length(conc)!=0){
ADI_1 <- abs(centroid-output[[1]]$Centroid)
ADI_df <- c(ADI_1, 0)
}
FWHM_Sum = 2*sqrt(log(2)*2)*sigma.fit[1]
}else{
if(length(mu.fit)>1){
x <- dffit$x
y <- (c.fit[1] *exp(-(x-mu.fit[1])**2/(2 * sigma.fit[1]**2)))
lo <- spline(x, y, method="natural")
y2 <- (c.fit[2] *exp(-(x-mu.fit[2])**2/(2 * sigma.fit[2]**2)))
lo2 <- spline(x, y2, method="natural")
p2 <- p2 + geom_line(data=data.frame(lo), aes(lo$x,lo$y),color=Color_1, lwd=lwd_env)+
geom_line(data=data.frame(lo2), aes(lo2$x,lo2$y),color=Color_2, lwd=lwd_env)
plot_sum_y <- y+y2
plot_sum_df <- data.frame(x,plot_sum_y)
p2 <- p2 + geom_line(data=plot_sum_df, aes(x,plot_sum_y), color="#E69F00", lwd=lwd_env)
if(length(time)!=0 | length(conc)!=0){
ADI_1 <- abs(mu.fit[1]-output[[1]]$Centroid)
ADI_2 <- abs(mu.fit[2]-output[[1]]$Centroid)
ADI_df <- c(ADI_1, ADI_2)
}
FWHM_Sum = 2*sqrt(log(2)*2)*(sqrt((sigma.fit[1]^2)+(sigma.fit[2]^2)))
}else{
x <- dffit$x
y <- (c.fit[1] *exp(-(x-mu.fit[1])**2/(2 * sigma.fit[1]**2)))
lo <- spline(x, y, method="natural")
p2 <- p2 + geom_line(data=data.frame(lo), aes(lo$x,lo$y),color=Color_1, lwd=lwd_env)
plot_sum_y <- y
plot_sum_df <- data.frame(x,plot_sum_y)
centroid <- ((sum(x_i*y_i))/sum(y_i))
if(length(time)!=0 | length(conc)!=0){
ADI_1 <- abs(centroid-output[[1]]$Centroid)
ADI_df <- c(ADI_1, 0)
}
FWHM_Sum = 2*sqrt(log(2)*2)*sigma.fit[1]
}
}
suppressWarnings(plot(p2))
suppressWarnings(ggsave(paste0('p',k,"_",Pep_Name,".jpeg"),p2,width=7,height=7))
FWHM_1 = 2*sqrt(log(2)*2)*sigma.fit[1]
FWHM_2 = 2*sqrt(log(2)*2)*sigma.fit[2]
FWHM <- FWHM_1
FWHM_Tot <- c(FWHM_Sum,NA)
y_AUC_1 <- c.fit[1] *exp(-(x-mu.fit[1])**2/(2 * sigma.fit[1]**2))
id <- order(dffit$x)
AUC_1 <- sum(diff(dffit$x[id])*rollmean(y_AUC_1[id],2))
AUC_1
y_AUC_2 <- c.fit[2] *exp(-(x-mu.fit[2])**2/(2 * sigma.fit[2]**2))
AUC_2 <- sum(diff(dffit$x[id])*rollmean(y_AUC_2[id],2))
AUC_2
AUC_tot_x <- dffit$x
AUC_tot_y <- plot_sum_y
id_2 <- order(dffit$x)
AUC_tot_dat <- sum(diff(AUC_tot_x[id_2])*rollmean(AUC_tot_y[id_2],2))
AUC_peaks_x <- plot_x
AUC_peaks_y <- plot_y
id_3 <- order(plot_x)
AUC_peaks_dat <- sum(diff(AUC_peaks_x[id_3])*rollmean(AUC_peaks_y[id_3],2))
AUC_Norm <- c((AUC_1/AUC_tot_dat),(AUC_2/AUC_tot_dat))
AUC_list <- c(AUC_1, AUC_2)
AUC_tot <- c(AUC_tot_dat, NA)
Chi_sqr <- (sum(plot_sum_y - plot_y)^2)/(sum(plot_y))
Chi_crit <- qchisq(p=0.95, df=(length(plot_y)-1))
if(Chi_sqr > Chi_crit){
stop("Chi Squared Value Past Critical Point")
}
centroid <- ((sum(x_i*y_i))/sum(y_i))
if(amp1_test$y<amp2_test$y){
c.fit_tot[[k]] <- c(0,c.fit)
}else{
c.fit_tot[[k]] <-c.fit
}
if(length(time)!=0 | length(conc)!=0){
if(Num_Envelops==1 && Color_1==3){
if(is.na(AUC_1) == FALSE && is.na(AUC_2)==FALSE){
if(AUC_Norm[1] >= 0.9 | AUC_Norm[2] >= 0.9 | AUC_Norm[1]<= 0.1 | AUC_Norm[2] <=0.1){
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1], Chi_sqr, c(1), ADI_df[1], AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area', 'Chi Squared', 'Normalized Area', 'Relative DA', 'Area Total', 'Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak")))
}
}else {
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1],Chi_sqr, c(1),ADI_df[1], AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area','Chi Squared', 'Normalized Area','Relative DA', 'Area Total','Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak")))
}
}
if(Num_Envelops==1 && Color_1==4){
if(is.na(AUC_1) == FALSE && is.na(AUC_2)==FALSE){
if(AUC_Norm[1] >= 0.9 | AUC_Norm[2] >= 0.9 | AUC_Norm[1]<= 0.1 | AUC_Norm[2] <=0.1){
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1], Chi_sqr, c(1), ADI_df[1], AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area', 'Chi Squared', 'Normalized Area', 'Relative DA', 'Area Total', 'Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak Env2")))
}
}else {
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1],Chi_sqr, c(1),ADI_df[1], AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area','Chi Squared', 'Normalized Area','Relative DA', 'Area Total','Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak Env2")))
}
}
if(Num_Envelops==2){
if(is.na(AUC_1) == FALSE && is.na(AUC_2)==FALSE){
if(AUC_Norm[1] >= 0.9 | AUC_Norm[2] >= 0.9 | AUC_Norm[1]<= 0.1 | AUC_Norm[2] <=0.1){
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1], Chi_sqr, c(1), ADI_df[1], AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area', 'Chi Squared', 'Normalized Area', 'Relative DA', 'Area Total', 'Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak")))
}else {
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit, centroid, c.fit, sigma.fit, FWHM, AUC_list, Chi_sqr, AUC_Norm, ADI_df, AUC_tot, FWHM_Tot))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area','Chi Squared', 'Normalized Area','Relative DA', 'Area Total', 'Total Width')
rownames(output[[k]]) <- c((print(paste0(Peptide_Names[k+(k-1)]," ", "Bimodal Peak 1"))), (print(paste0(Peptide_Names[k+(k-1)]," ", "Bimodal Peak 2"))))
}
}else {
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1],Chi_sqr, c(1),ADI_df[1], AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area','Chi Squared', 'Normalized Area','Relative DA', 'Area Total','Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak")))
}
}
}else{
if(Num_Envelops==1 && Color_1==3){
if(is.na(AUC_1) == FALSE && is.na(AUC_2)==FALSE){
if(AUC_Norm[1] >= 0.9 | AUC_Norm[2] >= 0.9 | AUC_Norm[1]<= 0.1 | AUC_Norm[2] <=0.1){
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1], Chi_sqr, c(1), AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area', 'Chi Squared', 'Normalized Area','Area Total', 'Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak")))
}
}else {
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1],Chi_sqr, c(1), AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area','Chi Squared', 'Normalized Area',, 'Area Total','Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak")))
}
}
if(Num_Envelops==1 && Color_1==4){
if(is.na(AUC_1) == FALSE && is.na(AUC_2)==FALSE){
if(AUC_Norm[1] >= 0.9 | AUC_Norm[2] >= 0.9 | AUC_Norm[1]<= 0.1 | AUC_Norm[2] <=0.1){
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1], Chi_sqr, c(1),AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area', 'Chi Squared', 'Normalized Area', 'Area Total', 'Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak Env2")))
}
}else {
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1],Chi_sqr, c(1), AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area','Chi Squared', 'Normalized Area', 'Area Total','Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak Env2")))
}
}
if(Num_Envelops==2){
if(is.na(AUC_1) == FALSE && is.na(AUC_2)==FALSE){
if(AUC_Norm[1] >= 0.9 | AUC_Norm[2] >= 0.9 | AUC_Norm[1]<= 0.1 | AUC_Norm[2] <=0.1){
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1], Chi_sqr, c(1), AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area', 'Chi Squared', 'Normalized Area', 'Area Total', 'Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak")))
}else {
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit, centroid, c.fit, sigma.fit, FWHM, AUC_list, Chi_sqr, AUC_Norm, AUC_tot, FWHM_Tot))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area','Chi Squared', 'Normalized Area', 'Area Total', 'Total Width')
rownames(output[[k]]) <- c((print(paste0(Peptide_Names[k+(k-1)]," ", "Bimodal Peak 1"))), (print(paste0(Peptide_Names[k+(k-1)]," ", "Bimodal Peak 2"))))
}
}else {
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1],Chi_sqr, c(1), AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area','Chi Squared', 'Normalized Area', 'Area Total','Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak")))
}
}
}
}
###Third Time Point
Color_1=3
Color_2=4
Num_Envelops=2
Replicate=TRUE
{
k=3
if (length(charge_list)>1){
charge <- charge_list[k]
}else{
charge <- charge_list[1]
}
data_x <- na.omit(data_list[k+(k-1)])
data_y <- na.omit(data_list[k+1+(k-1)])
x_i <- c(data_x$x)
y_i <- c(data_y$y)
df <- data.frame(x_i,y_i)
charge_shift =1/charge
x <- data[print(Peptide_Names[k+(k-1)])]
x <- na.omit(x[[1]])
y.sg4 <- data[print(Peptide_Names[k+1+(k-1)])]
y.sg4 <- na.omit(y.sg4[[1]])
y.sg4 <- savgol(y.sg4, 3, 4, 0)
df_smooth <- data.frame(x, y.sg4)
}
{
mixture<-normalmixEM(x_i, sd.constr = c("a", "a"), arbmean = TRUE, arbvar = TRUE, maxit=maxit, epsilon = epsilon, ECM=TRUE)
ptest <- predict(loess(y_i ~ x_i, family="gaussian", span= span), df)
p_pred <- data.frame(x_i, ptest)
amp1 <- approx(p_pred$x_i, p_pred$ptest, xout=mixture$mu[1])
amp1_test <- amp1
amp2 <- approx(p_pred$x_i, p_pred$ptest, xout=mixture$mu[2])
amp2_test <-amp2
}
{
if(mixture$mu[1] == mixture$mu[2] | (mixture$mu[2] - 1) < mixture$mu[1] | Num_Envelops == 1){
x <- x_i
amp1 <- max(c(amp1$y, amp2$y))
fit <- nlsLM(y_i ~ (C1*exp(-(x-mean1)**2/(2 * sigma1**2))),
data = df,
start = list(mean1 = mixture$mu[1],
sigma1 = mixture$sigma[1],
C1 = amp1),
lower =c(0,mixture$sigma[1],0),
upper=c(Inf, mixture$sigma[1], 100),
algorithm = "port",
)
dffit <- data.frame(x=x_i)
dffit$y <- predict(fit, newdata=dffit)
fit.sum <- summary(fit)
fit.sum
coef.fit <- fit.sum$coefficients
mu.fit <- coef.fit[1]
sigma.fit <- coef.fit[2]
c.fit <- coef.fit[3]
}else {
x <- x_i
amp1 <- approx(p_pred$x, p_pred$ptest, xout=mixture$mu[1])
amp2 <- approx(p_pred$x, p_pred$ptest, xout=mixture$mu[2])
if(length(time)!=0 && k!=1 | length(conc)!=0 && k!=1){
if(Replicate==FALSE){
c1_high <- output[[k-1]]$Height[1]
c2_low <- output[[k-1]]$Height[2]
}
if(Replicate==TRUE){
c1_high <- 100
c2_low <- 0
}
}
fit <- nlsLM(y_i ~ (C1*exp(-(x-mean1)**2/(2 * sigma1**2)) +
C2*exp(-(x-mean2)**2/(2 * sigma2**2))),
data = df,
start = list(mean1 = mixture$mu[1],
mean2 = mixture$mu[2],
sigma1 = mixture$sigma[1],
sigma2 = mixture$sigma[2],
C1 = amp1$y,
C2 = amp2$y),
lower=c(0,mixture$mu[1],mixture$sigma[1],mixture$sigma[2],0,c2_low),
upper=c(Inf, Inf, mixture$sigma[1], mixture$sigma[2], c1_high, 100),
algorithm = "port",
)
dffit <- data.frame(x=x_i)
dffit$y <- predict(fit, newdata=dffit)
fit.sum <- summary(fit)
fit.sum
coef.fit <- fit.sum$coefficients[,1]
mu.fit <- coef.fit[1:2]
sigma.fit <- coef.fit[3:4]
c.fit <- coef.fit[5:6]
}
}
{
plot_x <- x_i
plot_y <-y_i
plot_df <- data.frame(plot_x, plot_y)
smooth_x <- df_smooth$x
smooth_y <- df_smooth$y.sg4
#original
p3 <- ggplot(data=df_smooth, aes(smooth_x,smooth_y), colour="black") +
geom_line(lwd=lwd_mz)+
xlim(x_min,x_max)+
ggtitle(c(print(paste0(Peptide_Names[k+(k-1)]," ", paste0(Pep_Name))))) +
theme_tufte()+
theme(panel.border = element_rect(colour = "black", fill=NA, size=border_width),
plot.title = element_text(color="black", size=axis_font, face="bold"),
axis.title = element_blank(),
axis.text.x = element_text(face="bold", color="Black",size=axis_font),
axis.text.y = element_text(face="bold", color="Black",size=axis_font)
)
#overall fit
p3<- p3+geom_line(data=plot_df, aes(plot_x,plot_y), color="red", lwd=lwd_env)
#components of the fit
if(Num_Envelops==1){
x <- dffit$x
y <- (c.fit[1] *exp(-(x-mu.fit[1])**2/(2 * sigma.fit[1]**2)))
lo <- spline(x, y, method="natural")
p3 <- p3 + geom_line(data=data.frame(lo), aes(lo$x,lo$y),color=Color_1, lwd=lwd_env)
centroid <- ((sum(x_i*y_i))/sum(y_i))
plot_sum_y <- y
plot_sum_df <- data.frame(x,plot_sum_y)
if(length(time)!=0 | length(conc)!=0){
ADI_1 <- abs(centroid-output[[1]]$Centroid)
ADI_df <- c(ADI_1, 0)
}
FWHM_Sum = 2*sqrt(log(2)*2)*sigma.fit[1]
}else{
if(length(mu.fit)>1){
x <- dffit$x
y <- (c.fit[1] *exp(-(x-mu.fit[1])**2/(2 * sigma.fit[1]**2)))
lo <- spline(x, y, method="natural")
y2 <- (c.fit[2] *exp(-(x-mu.fit[2])**2/(2 * sigma.fit[2]**2)))
lo2 <- spline(x, y2, method="natural")
p3 <- p3 + geom_line(data=data.frame(lo), aes(lo$x,lo$y),color=Color_1, lwd=lwd_env)+
geom_line(data=data.frame(lo2), aes(lo2$x,lo2$y),color=Color_2, lwd=lwd_env)
plot_sum_y <- y+y2
plot_sum_df <- data.frame(x,plot_sum_y)
p3 <- p3 + geom_line(data=plot_sum_df, aes(x,plot_sum_y), color="#E69F00", lwd=lwd_env)
if(length(time)!=0 | length(conc)!=0){
ADI_1 <- abs(mu.fit[1]-output[[1]]$Centroid)
ADI_2 <- abs(mu.fit[2]-output[[1]]$Centroid)
ADI_df <- c(ADI_1, ADI_2)
}
FWHM_Sum = 2*sqrt(log(2)*2)*(sqrt((sigma.fit[1]^2)+(sigma.fit[2]^2)))
}else{
x <- dffit$x
y <- (c.fit[1] *exp(-(x-mu.fit[1])**2/(2 * sigma.fit[1]**2)))
lo <- spline(x, y, method="natural")
p3 <- p3 + geom_line(data=data.frame(lo), aes(lo$x,lo$y),color=Color_1, lwd=lwd_env)
plot_sum_y <- y
plot_sum_df <- data.frame(x,plot_sum_y)
centroid <- ((sum(x_i*y_i))/sum(y_i))
if(length(time)!=0 | length(conc)!=0){
ADI_1 <- abs(centroid-output[[1]]$Centroid)
ADI_df <- c(ADI_1, 0)
}
FWHM_Sum = 2*sqrt(log(2)*2)*sigma.fit[1]
}
}
suppressWarnings(plot(p3))
suppressWarnings(ggsave(paste0('p',k,"_",Pep_Name,".jpeg"),p3,width=7,height=7))
FWHM_1 = 2*sqrt(log(2)*2)*sigma.fit[1]
FWHM_2 = 2*sqrt(log(2)*2)*sigma.fit[2]
FWHM <- FWHM_1
FWHM_Tot <- c(FWHM_Sum,NA)
y_AUC_1 <- c.fit[1] *exp(-(x-mu.fit[1])**2/(2 * sigma.fit[1]**2))
id <- order(dffit$x)
AUC_1 <- sum(diff(dffit$x[id])*rollmean(y_AUC_1[id],2))
AUC_1
y_AUC_2 <- c.fit[2] *exp(-(x-mu.fit[2])**2/(2 * sigma.fit[2]**2))
AUC_2 <- sum(diff(dffit$x[id])*rollmean(y_AUC_2[id],2))
AUC_2
AUC_tot_x <- dffit$x
AUC_tot_y <- plot_sum_y
id_2 <- order(dffit$x)
AUC_tot_dat <- sum(diff(AUC_tot_x[id_2])*rollmean(AUC_tot_y[id_2],2))
AUC_peaks_x <- plot_x
AUC_peaks_y <- plot_y
id_3 <- order(plot_x)
AUC_peaks_dat <- sum(diff(AUC_peaks_x[id_3])*rollmean(AUC_peaks_y[id_3],2))
AUC_Norm <- c((AUC_1/AUC_tot_dat),(AUC_2/AUC_tot_dat))
AUC_list <- c(AUC_1, AUC_2)
AUC_tot <- c(AUC_tot_dat, NA)
Chi_sqr <- (sum(plot_sum_y - plot_y)^2)/(sum(plot_y))
Chi_crit <- qchisq(p=0.95, df=(length(plot_y)-1))
if(Chi_sqr > Chi_crit){
stop("Chi Squared Value Past Critical Point")
}
centroid <- ((sum(x_i*y_i))/sum(y_i))
if(amp1_test$y<amp2_test$y){
c.fit_tot[[k]] <- c(0,c.fit)
}else{
c.fit_tot[[k]] <-c.fit
}
if(length(time)!=0 | length(conc)!=0){
if(Num_Envelops==1 && Color_1==3){
if(is.na(AUC_1) == FALSE && is.na(AUC_2)==FALSE){
if(AUC_Norm[1] >= 0.9 | AUC_Norm[2] >= 0.9 | AUC_Norm[1]<= 0.1 | AUC_Norm[2] <=0.1){
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1], Chi_sqr, c(1), ADI_df[1], AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area', 'Chi Squared', 'Normalized Area', 'Relative DA', 'Area Total', 'Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak")))
}
}else {
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1],Chi_sqr, c(1),ADI_df[1], AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area','Chi Squared', 'Normalized Area','Relative DA', 'Area Total','Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak")))
}
}
if(Num_Envelops==1 && Color_1==4){
if(is.na(AUC_1) == FALSE && is.na(AUC_2)==FALSE){
if(AUC_Norm[1] >= 0.9 | AUC_Norm[2] >= 0.9 | AUC_Norm[1]<= 0.1 | AUC_Norm[2] <=0.1){
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1], Chi_sqr, c(1), ADI_df[1], AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area', 'Chi Squared', 'Normalized Area', 'Relative DA', 'Area Total', 'Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak Env2")))
}
}else {
output[[k]] <- assign(Peptide_Names[k+(k-1)], data.frame(mu.fit[1], centroid[1], c.fit[1], sigma.fit[1], FWHM[1], AUC_tot[1],Chi_sqr, c(1),ADI_df[1], AUC_tot[1], FWHM_Tot[1]))
colnames(output[[k]]) <- c('Mean Value','Centroid','Height','SD','FWHM', 'Area','Chi Squared', 'Normalized Area','Relative DA', 'Area Total','Total Width')
rownames(output[[k]]) <- c(print(paste0(Peptide_Names[k+(k-1)]," ", "Unimodal Peak Env2")))
}