-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafeQuant.R
1082 lines (860 loc) · 40.5 KB
/
safeQuant.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
#!/usr/bin/Rscript
# Author: ahrnee-adm
# Adaptation: Georgia Angelidou (v. )
###############################################################
############################################################### INIT ###############################################################
#### DEPENDANCIES
suppressWarnings(suppressPackageStartupMessages(library(stringr, quiet=T)))
suppressWarnings(suppressPackageStartupMessages(library("affy", quiet=T)))
suppressWarnings(suppressPackageStartupMessages(library("limma", quiet=T)))
suppressWarnings(suppressPackageStartupMessages(library(gplots, quiet=T)))
suppressWarnings(suppressPackageStartupMessages(library(seqinr, quiet=T)))
suppressWarnings(suppressPackageStartupMessages(library(corrplot, quiet=T)))
suppressWarnings(suppressPackageStartupMessages(library(optparse, quiet=T)))
suppressWarnings(suppressPackageStartupMessages(library(data.table, quiet=T)))
suppressWarnings(suppressPackageStartupMessages(library(magrittr, quiet=T)))
suppressWarnings(suppressPackageStartupMessages(library(ggplot2, quiet=T)))
suppressWarnings(suppressPackageStartupMessages(library(ggrepel, quiet=T)))
suppressWarnings(suppressPackageStartupMessages(library(dplyr, quiet=T)))
suppressWarnings(suppressPackageStartupMessages(library(readxl, quiet=T)))
# TODO: Change the working directory to the one were the files are located in my PC
# Note: Don't add the / or \ after the R folder name. Otherwise it will not access the correct path
#sourceDirOSX <- ""
sourceDirOSX <- "D:/proteomics/proteomics/SafeQuant/SafeQuant-GitHubVersion/SafeQuant-master_Geo/R"
sourceDirTPP <- "C:/Proteomics/SafeQuant-master_Geo/R"
# first check if dev or tpp mode
if(file.exists(sourceDirOSX) | file.exists(sourceDirTPP)){
sourceDir <- ifelse(file.exists(sourceDirOSX),sourceDirOSX,sourceDirTPP)
source(paste(sourceDir,"/ExpressionAnalysis.R",sep=""))
source(paste(sourceDir,"/SafeQuantAnalysis.R",sep=""))
source(paste(sourceDir,"/Graphics.R",sep=""))
source(paste(sourceDir,"/GGGraphics.R",sep=""))
source(paste(sourceDir,"/IdentificationAnalysis.R",sep=""))
source(paste(sourceDir,"/Parser.R",sep=""))
source(paste(sourceDir,"/TMT.R",sep=""))
source(paste(sourceDir,"/UserOptions.R",sep=""))
}else if("SafeQuant" %in% installed.packages()[,1]){ # used installed SafeQuant
cat("Loading SafeQuant Library \n")
library("SafeQuant")
}else{
stop("SafeQuant Package not installed\n")
}
rm(sourceDirOSX, sourceDirTPP)
VERSION <- "2.3.5"
### USER CMD LINE OPTIONS
userOptions <- getUserOptions(version=VERSION)
### USER CMD LINE OPTIONS END
if(userOptions$verbose) print(userOptions$proteinQuant)
### SUPRESS WARNINGS
if(!userOptions$verbose){
options(warn=-1)
}
#### DEPENDENCIES
if(userOptions$verbose) print(userOptions)
############################################################### PARSING ###############################################################
if(userOptions$verbose) cat("PARSING INPUT FILE \n")
# get file type
fileType <- .getFileType(userOptions$inputFile)
### Progenesis Export
if(fileType %in% c("ProgenesisProtein","ProgenesisFeature","ProgenesisPeptide")){
# default
expDesign <- getExpDesignProgenesisCsv(userOptions$inputFile)
# get user specified experimental design
if(!is.na(userOptions$expDesignTag)){
# user specified
expDesign <- expDesignTagToExpDesign(userOptions$expDesignTag,expDesign)
}
if(fileType == "ProgenesisProtein"){
cat("INFO: PARSING PROGENESIS PROTEIN EXPORT FILE ",userOptions$inputFile, "\n" )
eset <- parseProgenesisProteinCsv(file=userOptions$inputFile,expDesign=expDesign)
}else if(fileType == "ProgenesisPeptide"){
#"ProgenesisFeature"
cat("INFO: PARSING PROGENESIS PEPTIDE EXPORT FILE ",userOptions$inputFile, "\n" )
# gives some general information about the different files all together
eset <- parseProgenesisPeptideMeasurementCsv(file=userOptions$inputFile,expDesign=expDesign, exclusivePeptides=userOptions$FExclusivePeptides)
}else{ #"ProgenesisFeature"
cat("INFO: PARSING PROGENESIS FEATURE EXPORT FILE ",userOptions$inputFile, "\n" )
eset <- parseProgenesisFeatureCsv(file=userOptions$inputFile,expDesign=expDesign)
}
# Scaffold Export (TMT data)
}else if(fileType == "ScaffoldTMT"){
cat("INFO: PARSING SCAFFOLD RAW EXPORT FILE ",userOptions$inputFile, "\n" )
# get default experimental design
# six plex or ten plex ?
# use default experimental design unless specified by the user
nbPlex <- .getNbPlex(userOptions$inputFile)
if(nbPlex == 6){
# 6-plex default: 1,2,3:4,5,6
expDesign <- data.frame(condition=paste("Condition",sort(rep(c(1,2),3)),sep=""),isControl=sort(rep(c(T,F),3),decreasing=T) )
}else{
# 10-plex default is "1,4,7,10:2,5,8:3,6,9"
expDesign <- data.frame(condition=paste("Condition",c(1,2,3,1,2,3,1,2,3,1),sep=""),isControl=c(T,F,F,T,F,F,T,F,F,T) )
}
eset <- parseScaffoldRawFile(file=userOptions$inputFile,expDesign=expDesign)
if(userOptions$TAdjustRatios){
#if((nbPlex == 10)){ # only possible for tmt-10 plex
nbCalMixSpectra <- sum( (fData(eset)$proteinName %in% names(CALIBMIXRATIOS)))
if(nbCalMixSpectra < 100) stop("Not enough Calibration Mix spectra were found: ",nbCalMixSpectra, "\n ")
cat("INFO: FOUND ", nbCalMixSpectra ," Calibration Mix spectra\n")
esetCalibMix <- .getCalibMixEset(eset)
# discard calibration mix proteins
eset <- eset[!(fData(eset)$proteinName %in% names(CALIBMIXRATIOS)),]
intAdjObj <- .intensityAdjustment(eset, esetCalibMix)
#}else{
# stop("Ratio Correction Not implemented for TMT 6-plex")
#}
}
# get user specified experimental design
if(!is.na(userOptions$expDesignTag)){
# user specified
expDesign <- expDesignTagToExpDesign(userOptions$expDesignTag,expDesign)
}
# eset <- parseScaffoldRawFile(file=userOptions$inputFile,expDesign=expDesign)
# apply specified experimental design
eset <- createExpressionDataset(expressionMatrix=exprs(eset)[,rownames(expDesign)],expDesign=expDesign,featureAnnotations=fData(eset))
if(!is.na(userOptions$scaffoldPTMSpectrumReportFile)){
cat("INFO: ADDING SCAFFOLD PTM ANNOTATIONS \n")
eset <- addScaffoldPTMFAnnotations(eset,userOptions$scaffoldPTMSpectrumReportFile)
}
}else if(fileType == "MaxQuantProteinGroup"){
# get user specified experimental design
if(!is.na(userOptions$expDesignTag)){
# user specified
expDesign <- expDesignTagToExpDesign(userOptions$expDesignTag,data.frame(condition=paste("Condition",1:1000),isControl=c(F,1000)), file=userOptions$inputFile, fileT =fileType)
}else{
stop("Please Specify Experimental Design")
}
cat("INFO: PARSING MaxQuant PROTEIN EXPORT FILE ",userOptions$inputFile, "\n" )
eset <- parseMaxQuantProteinGroupTxt(userOptions$inputFile,expDesign=expDesign, method="auc")
}else if(fileType == "GenericCSV"){
}else if (fileType == "SpectronantProteinGroup"){
expDesign <- expDesignTagToExpDesign(userOptions$expDesignTag,data.frame(condition=paste("Condition",1:1000),isControl=c(F,1000)), file=userOptions$inputFile, fileT=fileType)
cat("INFO: PARSING Spectronant PROTEIN EXPORT FILE ",userOptions$inputFile, "\n" )
eset <- parseSpectronantProteinGroupTxt(userOptions$inputFile,expDesign=expDesign, method="auc")
}else if (fileType == "DiaNNProteinGroup"){
expDesign <- expDesignTagToExpDesign(userOptions$expDesignTag,data.frame(condition=paste("Condition",1:1000),isControl=c(F,1000)), file=userOptions$inputFile, fileT=fileType)
#expDesign <- expDesignTagToExpDesign("D:\\proteomics\\proteomics\\SafeQuant\\SafeQuant-GitHubVersion\\SafeQuant-master_Geo\\Workflow_Info\\Example-experimentalDesignTemplate-EPS-7.txt",data.frame(condition=paste("Condition",1:1000),isControl=c(F,1000)), file="D:\\proteomics\\proteomics\\SafeQuant\\SafeQuant-GitHubVersion\\SafeQuant-master_Geo\\Workflow_Info\\report-SafeQuant_processed.tsv", fileT="DiaNNProteinGroup")
cat("INFO: PARSING Dia-NN PROTEIN EXPORT FILE ",userOptions$inputFile, "\n" )
eset <- parseDiaNNProteinGroupTxt(userOptions$inputFile,expDesign=expDesign, method="auc")
#eset <- parseDiaNNProteinGroupTxt("D:\\proteomics\\proteomics\\SafeQuant\\SafeQuant-GitHubVersion\\SafeQuant-master_Geo\\Workflow_Info\\report-SafeQuant_processed.tsv",expDesign=expDesign, method="auc")
}else{
stop("Unknown File Type", userOptions$inputFile)
}
# test option, limit number of entries
if(userOptions$test){
eset <- eset[1:nrow(eset) %in% sample(1:min(300,c(nrow(eset))),replace=F),]
}
# parse .fasta file
if(!is.na(userOptions$proteinFastaFile)){
cat("INFO: PARSING PROTEIN SEQUENCE DB ",userOptions$proteinFastaFile, "\n" )
### read protein db
proteinDB <- read.fasta(userOptions$proteinFastaFile,seqtype = "AA",as.string = TRUE, set.attributes = FALSE)
# dirty fix check if ACs in Progenesis file are stripped
if(isStrippedACs(sample(fData(eset)$proteinName,100))){
cat("INFO: RE-FORMATTING ACCESSION NUMBERS\n")
names(proteinDB) <- stripACs(names(proteinDB))
}
}
############################################################### CREATE DATA MODEL ###############################################################
if(userOptions$verbose) print(eset)
if(userOptions$verbose) print(pData(eset))
if(userOptions$verbose) print(names(fData(eset)))
#### CREATE FEATURE DATA AND FILTER (pre-rollup)
# Since Spectronant Files are already filter we can skip this area
# Point we still need the filter column in the final table
# generic
if (fileType == "DiaNNProteinGroup"){
filter <- data.frame(
con=isCon(fData(eset)$ac) # contaminants
,ac = !(grepl(userOptions$selectedProteinName,fData(eset)$proteinName,ignore.case=T)) # protein ac
)
}else{
filter <- data.frame(
con=isCon(fData(eset)$proteinName) # contaminants
,ac = !(grepl(userOptions$selectedProteinName,fData(eset)$proteinName,ignore.case=T)) # protein ac
)
}
# do not filter TMT data
if("pMassError" %in% names(fData(eset)) && (fileType != "ScaffoldTMT") ){
### applicable to Progenesis feature Exports
if(is.na(userOptions$precursorMassFilter)){ # if not user specified
# automatically get precursor limits, X * sd of 50% top scoring
userOptions$precursorMassFilter <- getMeanCenteredRange(fData(eset)$pMassError[fData(eset)$idScore > quantile(fData(eset)$idScore,na.rm=T)[3]],nbSd = 3)
filter <- cbind(filter, pMassError=
(fData(eset)$pMassError < userOptions$precursorMassFilter[1])
| (fData(eset)$pMassError > userOptions$precursorMassFilter[2]) # precursor mass tolerance
)
}
}
if("ptm" %in% names(fData(eset))){
# add motif-X and ptm coordinates
if(exists("proteinDB")){
cat("INFO: EXTRACTING PTM COORDINATES AND MOTIFS\n")
#format 1) progensis 2) scaffold
eset <- .addPTMCoord(eset,proteinDB,motifLength=6, isProgressBar=T,format= (fileType == "ScaffoldTMT") +1)
}
filter <- cbind(filter
, ptm = !(grepl(userOptions$selectedModifName,as.character(fData(eset)$ptm),ignore.case=T))
, nbPtmsPerPeptide = (fData(eset)$nbPtmsPerPeptide > userOptions$maxNbPtmsPerPeptide) )
}
if("peptide" %in% names(fData(eset))){
filter <- cbind(filter
, peptideLength =nchar(as.character(fData(eset)$peptide)) < userOptions$minPeptideLength
, charge = fData(eset)$charge == 1 # discard singly charged
)
}
# TODO: need to fix for Spectronant
#if(!("nbPeptides" %in% names(fData(eset)))){
### set nb peptides per protein
# eset <- setNbPeptidesPerProtein(eset)
#}
if(("nbPeptides" %in% names(fData(eset)))){
filter <- cbind(filter,nbPeptides=(fData(eset)$nbPeptides < userOptions$minNbPeptidesPerProt))
}
# do not filter TMT data
#if(("idScore" %in% names(fData(eset))) && (fileType != "ScaffoldTMT")){
if(("idScore" %in% names(fData(eset)))){
eset <- addIdQvalues(eset)
filter <- cbind(filter,qvalue=fData(eset)$idQValue > userOptions$fdrCutoff)
}
# set pre-rollup filters
eset <- .setFilter(eset,filter=filter)
rm(filter)
### make sure at least 1 feature pass the filter
if(sum(!fData(eset)$isFiltered,na.rm=T) == 0){
stop("CHECK FILTER SETTINGS. ALL FEATURES WERE FILTERED OUT")
}
#### CREATE FEATURE DATA AND FILTER END
### SET ANCHOR PROTEINS
fData(eset)$isNormAnchor <- grepl(userOptions$normAC,fData(eset)$proteinName)
if(userOptions$verbose){
cat("\nNB. ANCHOR PROTEINS: ")
cat(sum(fData(eset)$isNormAnchor))
cat("\n")
print(fData(eset)$proteinName[fData(eset)$isNormAnchor])
cat("\n")
}
### SET ANCHOR PROTEINS END
############################################################### EXPRESSION ANALYSIS ###############################################################
# create paired experiemntal design()
if(userOptions$ECorrelatedSamples){
eset <- createPairedExpDesign(eset)
}
# add filters etc to adjusted expressionSet
# update expDesign of intAdjObj$esetAd
if(exists("intAdjObj")){
fData(intAdjObj$esetAdj) <- fData(eset)
pData(intAdjObj$esetAdj) <- pData(eset)
exprs(intAdjObj$esetAdj) <- exprs(intAdjObj$esetAdj)[,colnames(exprs(eset))]
}
### non-pairwise stat test
statMethod <- c("")
if(userOptions$SNonPairWiseStatTest) statMethod <- c("all") #@TODO what about 'naRep'
if(userOptions$SRawDataAnalysis){ # No Normalization
esetNorm <- eset
if(exists("intAdjObj")) intAdjObj$esetAdjNorm <- intAdjObj$esetAdj
}else{ # NORMALIZE
method <- c("global","median")
# norm based on sum if norm anchor is specified
if(sum(fData(eset)$isNormAnchor) < nrow(eset)) method <- c("global","sum")
esetNorm <- sqNormalize(eset, method=method)
if(exists("intAdjObj")) intAdjObj$esetAdjNorm <- sqNormalize(intAdjObj$esetAdj, method=method )
}
### MISSING VALUES IMPUTATION
# baselineIntensity <- getBaselineIntensity(as.vector(unlist(exprs(esetNorm)[,1])),promille=5)
# exprs(esetNorm)[ is.na(exprs(esetNorm)) | (exprs(esetNorm) <= 0) ] <- 0
# exprs(esetNorm) <- exprs(esetNorm) + baselineIntensity
# if(exists("intAdjObj")){
# baselineIntensity <- getBaselineIntensity(as.vector(unlist(exprs(intAdjObj$esetAdjNorm)[,1])),promille=5)
# exprs(intAdjObj$esetAdjNorm)[ is.na(exprs(intAdjObj$esetAdjNorm)) | (exprs(intAdjObj$esetAdjNorm) <= 0) ] <- 0
# exprs(intAdjObj$esetAdjNorm) <- exprs(intAdjObj$esetAdjNorm) + baselineIntensity
# }
# print (userOptions$SMissingValuesImutationMethod)
# quit()
esetNorm = sqImpute(esetNorm,method=userOptions$SMissingValuesImutationMethod)
if(exists("intAdjObj")){
intAdjObj$esetAdjNorm = sqImpute(intAdjObj$esetAdjNorm,method=userOptions$SMissingValuesImutationMethod )
}
# Note: this overwrites the option when the SNonPairWiseStatTest acivated and sets it as all
if (userOptions$medianInfo){
statMethod <- c(statMethod, "median")
}else{
statMethod <- c(statMethod, 'mean')
}
if((fileType == "ProgenesisProtein") | (fileType == "MaxQuantProteinGroup") |
(fileType == "SpectronantProteinGroup") | (fileType == "DiaNNProteinGroup")){
fData(esetNorm)$isFiltered <- fData(esetNorm)$isFiltered | isDecoy(fData(esetNorm)$proteinName)
if (userOptions$log2_i){
sqaProtein <- safeQuantAnalysis(esetNorm, method=statMethod, fcThrs=userOptions$ratioCutOff, log2v = FALSE)
}else{
sqaProtein <- safeQuantAnalysis(esetNorm, method=statMethod, fcThrs=userOptions$ratioCutOff)
}
sqaProtein <- safeQuantAnalysis(esetNorm, method=statMethod, fcThrs=userOptions$ratioCutOff)
}else if((fileType == "ScaffoldTMT") && is.na(userOptions$scaffoldPTMSpectrumReportFile)){
# roll-up protein level
cat("INFO: ROLL-UP PROTEIN LEVEL\n")
fData(esetNorm)$isFiltered <- fData(esetNorm)$isFiltered | isDecoy(fData(esetNorm)$proteinName)
# correct TMT ratios
if(userOptions$TAdjustRatios){
fData(intAdjObj$esetAdjNorm)$isFiltered <- fData(esetNorm)$isFiltered
intAdjObjProt <- intAdjObj
intAdjObjProt$esetAdjNorm <- rollUp(intAdjObj$esetAdjNorm,featureDataColumnName= c("proteinName"))
sqaProtein <- safeQuantAnalysis(rollUp(esetNorm,featureDataColumnName= c("proteinName")), method=statMethod,intensityAdjustmentObj=intAdjObjProt, fcThrs=userOptions$ratioCutOff )
}else{
sqaProtein <- safeQuantAnalysis(rollUp(esetNorm,featureDataColumnName= c("proteinName")), method=statMethod , fcThrs=userOptions$ratioCutOff)
}
fData(sqaProtein$eset)$isFiltered <- fData(sqaProtein$eset)$isFiltered | isDecoy(fData(sqaProtein$eset)$proteinName) | (fData(sqaProtein$eset)$nbPeptides < userOptions$minNbPeptidesPerProt)
}else{
# roll-up peptide level
cat("INFO: ROLL-UP PEPTIDE LEVEL\n")
# correct TMT ratios
if(userOptions$TAdjustRatios){
cat("WARN: Ratio correction not yet implemented in this anlysis mode \n")
}
# Geo: here should add the method options and change from median to mean
esetPeptide <- rollUp(esetNorm,featureDataColumnName= c("peptide","ptm"))
# fdr filter
# replace qValues by rollUp level qValues ()
esetPeptide <- addIdQvalues(esetPeptide)
if(fileType == "ScaffoldTMT"){
fData(esetPeptide)$isFiltered <- fData(esetPeptide)$isFiltered | (fData(esetPeptide)$nbPeptides < userOptions$minNbPeptidesPerProt)
}else{
# update filter to exclude peptide level hight qValues
fData(esetPeptide)$isFiltered <- fData(esetPeptide)$isFiltered | (fData(esetPeptide)$idQValue > userOptions$fdrCutoff) | (fData(esetPeptide)$nbPeptides < userOptions$minNbPeptidesPerProt)
}
if(userOptions$proteinQuant){
cat("INFO: ROLL-UP PROTEIN LEVEL\n")
esetProtein <- rollUp(esetPeptide,featureDataColumnName= c("proteinName"))
esetProtein <- addIdQvalues(esetProtein)
if(fileType == "ScaffoldTMT"){
fData(esetProtein)$isFiltered <- fData(esetProtein)$isFiltered | isDecoy(fData(esetProtein)$proteinName) | (fData(esetProtein)$nbPeptides < userOptions$minNbPeptidesPerProt)
}else{
fData(esetProtein)$isFiltered <- fData(esetProtein)$isFiltered | (fData(esetProtein)$idQValue > userOptions$fdrCutoff) | isDecoy(fData(esetProtein)$proteinName) | (fData(esetProtein)$nbPeptides < userOptions$minNbPeptidesPerProt)
}
sqaProtein <- safeQuantAnalysis(esetProtein, method=statMethod, fcThrs=userOptions$ratioCutOff)
}
fData(esetPeptide)$isFiltered <- fData(esetPeptide)$isFiltered | isDecoy(fData(esetPeptide)$proteinName)
sqaPeptide <- safeQuantAnalysis(esetPeptide, method=statMethod, fcThrs=userOptions$ratioCutOff)
fData(esetNorm)$isFiltered <- fData(esetNorm)$isFiltered | isDecoy(fData(esetNorm)$proteinName) | (fData(esetNorm)$nbPeptides < userOptions$minNbPeptidesPerProt)
if(userOptions$top3 & userOptions$proteinQuant){
cat("INFO: ROLL-UP TOP3\n")
esetTop3 <- rollUp(esetPeptide,featureDataColumnName= c("proteinName"), method="top3")
}
}
### IBAQ
if(userOptions$iBAQ & userOptions$proteinQuant){
cat("INFO: CALCULATING IBAQ VALUES\n")
if(exists("proteinDB")){
esetIBAQ <- getIBAQEset(sqaProtein$eset, proteinDB=proteinDB)
}else{
cat("ERROR: proteinDB NOT FOUND NO iBAQ VALUES CALCULATED\n")
}
}
### EXPRESSION ANALYSIS END
#
# ls()
# quit()
# all globalEnvironment
rm(addIdQvalues, addScaffoldPTMFAnnotations, createExpDesign, createExpressionDataset)
############################################################### EXPORTS ###############################################################
cat("INFO: PREPARING EXPORTS","\n")
#### SET WORKING DIR
if(!file.exists(userOptions$outputDir)) dir.create(userOptions$outputDir)
if(userOptions$verbose) cat("INFO: CREATED DIRECTORY", userOptions$outputDir,"\n")
#### SET WORKING DIR
##I/O: set export file paths
userOptions$pdfFilePath <- file.path(userOptions$outputDir, paste(userOptions$resultsFileLabel,".pdf",sep=""))
userOptions$peptideReportFilePath <- file.path(userOptions$outputDir, paste0(userOptions$resultsFileLabel,"_PEPTIDE.",userOptions$sSheetExtension))
userOptions$proteinReportFilePath <- file.path(userOptions$outputDir, paste0(userOptions$resultsFileLabel,"_PROTEIN.",userOptions$sSheetExtension))
userOptions$paramsFilePath <- file.path(userOptions$outputDir, paste(userOptions$resultsFileLabel,"_SQ_PARAMS.TXT",sep=""))
userOptions$rDataFilePath <- file.path(userOptions$outputDir, paste(userOptions$resultsFileLabel,"_SQ.rData",sep=""))
############################################################### GRAPHICS ###############################################################
# plot protein or peptide level results
if(exists("sqaProtein")){
sqaDisp <- sqaProtein
lab <- "Protein"
}else{
sqaDisp <- sqaPeptide
lab <- "Peptide"
}
### only disp. a subset for some plots
rowSelEset <- 1:nrow(eset) %in% sample(nrow(eset),min(c(2000,nrow(eset))) ,replace=F)
rowSelSqaDisp <- 1:nrow(sqaDisp$eset) %in% sample(nrow(sqaDisp$eset),min(c(2000,nrow(sqaDisp$eset))) ,replace=F)
pdf(userOptions$pdfFile)
parDefault <- par()
CONDITIONCOLORS <- .getConditionColors(esetNorm)
### EXPDESIGN PLOT
# Note: visualize the information pData(esetNorm)
plotExpDesign(esetNorm, version=VERSION)
### EXPDESIGN PLOT END
### IDENTIFICATION PLOTS
if(userOptions$verbose) cat("INFO: IDENTIFICATION PLOTS \n")
par(mfrow=c(2,2))
#.idOverviewPlots()
#@ NOT CRAN COMPATIBLE
.idOverviewPlots(userOptions=userOptions
,esetNorm=esetNorm
,fileType=fileType
,sqaPeptide= ifelse(exists("sqaPeptide"),list(sqaPeptide),list(NA))[[1]]# HACK to pass check
,sqaProtein= ifelse(exists("sqaProtein"),list(sqaProtein),list(NA))[[1]] # HACK to pass check
)
if(fileType %in% c("ProgenesisFeature","ProgenesisPeptide")){
par(mfrow=c(3,2))
.idPlots(eset, selection=c(1,3), main="Feature Level", qvalueThrs=userOptions$fdrCutoff, userOptions=userOptions)
if(exists("sqaPeptide")) .idPlots(sqaPeptide$eset, selection=c(1,3), main="Peptide Level", qvalueThrs=userOptions$fdrCutoff)
if(exists("sqaProtein")) .idPlots(sqaProtein$eset, selection=c(1,3), main="Protein Level", qvalueThrs=userOptions$fdrCutoff)
}
par(parDefault)
### IDENTIFICATIONS PLOTS END
### QUANT. QC PLOTS
if(userOptions$verbose) cat("INFO: QUANT QC. PLOTS \n")
### MASS ERROR
par(parDefault)
#if("pMassError" %in% names(fData(eset))){
if(fileType %in% c("ProgenesisFeature","ProgenesisPeptide")){
par(mfrow=c(2,1), mar=c(4.5,6.1,4.1,6.1))
# Note: Visualize the data fData(eset)
plotPrecMassErrorDistrib(eset, pMassTolWindow=userOptions$precursorMassFilter)
plotPrecMassErrorVsScore(eset[rowSelEset,], pMassTolWindow=userOptions$precursorMassFilter)
par(parDefault)
}
layout(rbind(c(1,2), c(3,3)))
### missing values
missinValueBarplot(eset)
### total intensity sum
barplotMSSignal(eset)
rm(barplotMSSignal)
par( mar=c(6.5,5.1,2.5,3.1))
cvBoxplot(sqaDisp$eset)
par(parDefault)
### CORRELATION PLOTS
### COR OR PAIRS PLOT. IF FEWER THAN X SAMPLES
if(ncol(sqaDisp$eset) < 8){
pairsAnnot(log10(exprs(sqaDisp$eset))[rowSelSqaDisp & !fData(sqaDisp$eset)$isFiltered ,],textCol=as.character(CONDITIONCOLORS[pData(sqaDisp$eset)$condition,]))
}else{
d_c <- log10(exprs(sqaDisp$eset))[rowSelSqaDisp & !fData(sqaDisp$eset)$isFiltered,]
colnames(d_c) <- expDesign$file
#.correlationPlot(log10(exprs(sqaDisp$eset))[rowSelSqaDisp & !fData(sqaDisp$eset)$isFiltered,], labels=as.character(unique(pData(sqaDisp$eset)$condition)), textCol=as.character(CONDITIONCOLORS[pData(sqaDisp$eset)$condition,]))
.correlationPlot(d_c, labels=as.character(unique(pData(sqaDisp$eset)$condition)), textCol=as.character(CONDITIONCOLORS[pData(sqaDisp$eset)$condition,]))
}
### COR OR PAIRS PLOT. IF FEWER THAN X CONDITIONS
if(length(unique(pData(sqaDisp$eset)$condition)) < 8){
pairsAnnot(log10(getSignalPerCondition(sqaDisp$eset[rowSelSqaDisp & !fData(sqaDisp$eset)$isFiltered,]))[,as.character(unique(pData(sqaDisp$eset)$condition)) ],textCol=as.character(CONDITIONCOLORS[as.character(unique(pData(sqaDisp$eset)$condition)),]))
}else{
.correlationPlot(log10(getSignalPerCondition(sqaDisp$eset[rowSelSqaDisp & !fData(sqaDisp$eset)$isFiltered,]))[,as.character(unique(pData(sqaDisp$eset)$condition)) ],textCol=as.character(CONDITIONCOLORS[as.character(unique(pData(sqaDisp$eset)$condition)),]))
}
par(parDefault)
### TMT calibration mix
if(exists("intAdjObj")){
# plot adjusted ratios vs org ratio
# boxplot noise fraction
#if(ncol(sqaDisp$ratio) > 1) par(mfrow=c(2,2))
boxplot(intAdjObj$noiseFraction*100, border=ifelse(intAdjObj$selectedPairs,"blue","black")
, ylab="Noise Fraction (%)",xlab="Calibration Mix Pair", cex.axis=1.5,cex.lab=1.5)
if(ncol(sqaDisp$ratio) > 1) par(mfrow=c(2,2))
plotAdjustedVsNonAdjustedRatio(sqaDisp$ratio,sqaDisp$unAdjustedRatio)
par(parDefault)
}
### QUANT. QC PLOTS END
par(parDefault)
if(userOptions$verbose) cat("INFO: HEAT MAP \n")
#hClustHeatMapOld(sqaDisp$eset[(1:nrow(sqaDisp$eset) %in% sample(nrow(sqaDisp$eset),min(c(nrow(sqaDisp$eset),10000)))) & !fData(sqaDisp$eset)$isFiltered,],main= paste(lab,"Level"))
# set smaller range if TMT
breaks=seq(-2,2,length=20)
if(fileType == "ScaffoldTMT" ){
breaks=seq(-1.5,1.5,length=20)
}
hClustHeatMap(sqaDisp$eset[(1:nrow(sqaDisp$eset) %in% sample(nrow(sqaDisp$eset),min(c(nrow(sqaDisp$eset),10000)))) & !fData(sqaDisp$eset)$isFiltered,]
,main= paste(lab,"Level")
,breaks = breaks)
### QUANT. STAT. PLOTS
### VAILD FEATURES VS. pValue/qValue
if(userOptions$verbose) cat("INFO: QUANT RES. PLOTS \n")
par(mfrow=c(1,2))
plotNbValidDeFeaturesPerFDR(sqaDisp,
upRegulated=F
,log2RatioCufOff=log2(userOptions$ratioCutOff)
,pvalRange=c(0,0.15)
,pvalCutOff=userOptions$deFdrCutoff
,isLegend=T
,isAdjusted=T
,ylab=paste(lab, "Counts")
,main="DOWN REGULATION"
)
plotNbValidDeFeaturesPerFDR(sqaDisp,
upRegulated=T
,log2RatioCufOff=log2(userOptions$ratioCutOff)
,pvalRange=c(0,0.15)
,pvalCutOff=userOptions$deFdrCutoff
,isLegend=F
,isAdjusted=T
,ylab=paste(lab, "Counts")
,main="UP REGULATION"
)
par(parDefault)
# plotVolcano(sqaDisp
# , main=paste(lab,"Level")
# , ratioThrs= userOptions$ratioCutOff
# , pValueThreshold= userOptions$deFdrCutoff
# , adjusted = T)
# Note Check sqaDisp
plotAllGGVolcanoes(sqaDisp
,log2RatioThrs =userOptions$ratioCutOff %>% log2
,pValueThrs= userOptions$deFdrCutoff
,ylab = "log10 Adj. P-Value"
,title = paste(lab,"Level")
,textSize = 15
)
###### Here is were need to chnge the plot legend for ebayes
if(userOptions$eBayes){
par(mfrow=c(1,2))
plotNbValidDeFeaturesPerFDR(sqaDisp,
upRegulated=F
,log2RatioCufOff=log2(userOptions$ratioCutOff)
,pvalRange=c(0,0.15)
,pvalCutOff=userOptions$deFdrCutoff
,isLegend=T
,isAdjusted=F
,ylab=paste(lab, "Counts")
,main="DOWN REGULATION"
)
plotNbValidDeFeaturesPerFDR(sqaDisp,
upRegulated=T
,log2RatioCufOff=log2(userOptions$ratioCutOff)
,pvalRange=c(0,0.15)
,pvalCutOff=userOptions$deFdrCutoff
,isLegend=F
,isAdjusted=F
,ylab=paste(lab, "Counts")
,main="UP REGULATION"
)
par(parDefault)
# plotVolcano(sqaDisp
# , main=paste(lab,"Level")
# , ratioThrs= userOptions$ratioCutOff
# , pValueThreshold= userOptions$deFdrCutoff
# , adjusted = F)
plotAllGGVolcanoes(sqaDisp
,log2RatioThrs =userOptions$ratioCutOff %>% log2
,pValueThrs= userOptions$deFdrCutoff
,ylab = "log10 P-Value"
,title = paste(lab,"Level")
,textSize = 15
,isAdjusted = F
)
par(mfrow=c(2,2))
if(nrow(CONDITIONCOLORS) > 4) par(mfrow=c(3,3))
.allpValueHist(sqaDisp)
plotQValueVsPValue(sqaDisp, lim=c(0,1))
par(parDefault)
}
### QUANT. STAT. PLOTS END
par(parDefault)
### SOME ADDITIONAL QC PLOTS
if(userOptions$addQC){
# if(exists("sqaPeptide")){
# plotXYDensity(fData(sqaPeptide$eset)$retentionTime,fData(sqaPeptide$eset)$pMassError, disp=c("")
# , xlab="Retention time (min)"
# , ylab="Precursor Mass Error (ppm)"
# , cex.axis=1.5
# , cex.lab=1.5)
if( all(c("retentionTime","pMassError") %in% names(fData(eset)) )){
plotXYDensity(fData(eset)$retentionTime,fData(eset)$pMassError, disp=c("")
, xlab="Retention time (min)"
, ylab="Precursor Mass Error (ppm)"
, cex.axis=1.5
, cex.lab=1.5)
abline(h=c(userOptions$precursorMassFilter[1],0,userOptions$precursorMassFilter[2]),lty=2, lwd=2)
# rt vs signal
sel <- 1:nrow(esetNorm) %in% sample(nrow(esetNorm),min(c(4000,nrow(esetNorm))) ,replace=F) & (!(fData(esetNorm)$proteinName %in% names(CALIBMIXRATIOS)))
plotRTNormSummary(esetNorm[sel,])
par(mfrow=c(2,2))
plotRTNorm(getRTNormFactors(esetNorm[sel,], minFeaturesPerBin=100),esetNorm[sel,])
par(parDefault)
}
par(mfrow=c(2,2))
#all ma plots
for(s in colnames(exprs(esetNorm))){
sel <- 1:nrow(esetNorm) %in% sample(nrow(esetNorm),min(c(4000,nrow(esetNorm))) ,replace=F) & (!(fData(esetNorm)$proteinName %in% names(CALIBMIXRATIOS)))
maPlotSQ(esetNorm[sel,],sample=s)
}
par(parDefault)
}
rm(esetNorm)
cat("INFO: CREATED FILE ", userOptions$pdfFile,"\n")
graphics.off()
############################### GRAPHICS END
############################################################### SPREADSHEET EXPORT ###############################################################
if(exists("sqaPeptide")){
selFDataCol <- c("peptide","proteinName", "ac","geneName", "proteinDescription", "idScore","idQValue"
,"retentionTime", "ptm", "nbPtmsPerPeptide", "nbRolledFeatures" )
selFDataCol <- selFDataCol[selFDataCol %in% names(fData(sqaPeptide$eset))]
### add modif coord
if("motifX" %in% names(fData(sqaPeptide$eset))){
selFDataCol <- c(selFDataCol,"motifX","modifCoord")
}
### add allAccessions
if("allAccessions" %in% names(fData(sqaPeptide$eset))){
selFDataCol <- c(selFDataCol,"allAccessions")
}
### add ptmPeptide
if("ptmPeptide" %in% names(fData(sqaPeptide$eset))){
selFDataCol <- c(selFDataCol,"ptmPeptide")
}
### add ptmLocProb
if("ptmLocProb" %in% names(fData(sqaPeptide$eset))){
selFDataCol <- c(selFDataCol,"ptmLocProb")
}
### add ptmLocMascotConfidence
if("ptmLocMascotConfidence" %in% names(fData(sqaPeptide$eset))){
selFDataCol <- c(selFDataCol,"ptmLocMascotConfidence")
}
# add sqa object data
cv <- sqaPeptide$cv
names(cv) <- paste("cv",names(cv),sep="_")
ratio <- sqaPeptide$ratio
if(ncol(ratio) > 0 ) names(ratio) <- paste("log2ratio",names(ratio),sep="_")
pValue <- sqaPeptide$pValue
if(ncol(pValue) > 0 ) names(pValue) <- paste("pValue",names(pValue),sep="_")
log10_pValue <- -log10(sqaPeptide$pValue) # Added: 26072021
if(ncol(log10_pValue) > 0 ) names(log10_pValue) <- paste(" -log10_pValue",names(log10_pValue),sep="_") # Added: 26072021
qValue <- sqaPeptide$qValue
if(ncol(qValue) > 0 ) names(qValue) <- paste("qValue",names(qValue),sep="_")
log10_qValue <- -log10(sqaPeptide$qValue) # Added: 26072021
if(ncol(log10_qValue) > 0 ) names(log10_qValue) <- paste(" -log10_qValue",names(log10_qValue),sep="_") # Added: 26072021
if (userOptions$medianInfo){
medianSignalDf <- getSignalPerCondition(sqaPeptide$eset)
#names(medianSignalDf) <- paste("medianInt",names(medianSignalDf),sep="_")
names(medianSignalDf) <- paste("medianInt",names(medianSignalDf),sep="_")
}else{
medianSignalDf <- getSignalPerCondition(sqaPeptide$eset, method='mean')
#names(medianSignalDf) <- paste("medianInt",names(medianSignalDf),sep="_")
names(medianSignalDf) <- paste("meanInt",names(medianSignalDf),sep="_")
}
expset <- exprs(sqaPeptide$eset)
if ("file" %in% colnames(expDesign)){
colnames(expset) <- expDesign$file
}else{
colnames(expset) <- rownames(expDesign)
}
out <- cbind(
fData(sqaPeptide$eset)[,selFDataCol]
, expset
, medianSignalDf
, cv
, ratio
, fracNAFeatures = getNAFraction(sqaPeptide$eset,method=c("cond","count"))
, pValue
, log10_pValue
, qValue
, log10_qValue
, FTestPValue = sqaPeptide$FPValue
, FTestQValue = sqaPeptide$FQValue
)[!fData(sqaPeptide$eset)$isFiltered,]
### add unadjusted ratios if TMT ratio correction
if(userOptions$TAdjustRatios){
unadjPeptideRatios <- sqaPeptide$unAdjustedRatio[!fData(sqaPeptide$eset)$isFiltered,]
names(unadjPeptideRatios) <- paste("log2_unadjRatio",names(sqaPeptide$ratio),sep="_")
out <- cbind(out,unadjPeptideRatios)
}
### paired expDesign ratio export
if("subject" %in% names(pData(sqaPeptide$eset))){
allRatios <- getRatios(sqaPeptide$eset,method="paired")[!fData(sqaPeptide$eset)$isFiltered,]
names(allRatios) <- paste("log2_pairedRatio",names(allRatios),sep="_")
out <- cbind(out,allRatios)
}
write.table(out
, file=userOptions$peptideReportFilePath
, sep=userOptions$sSheetExportDelimiter
, row.names=F
)
cat("INFO: CREATED FILE ", userOptions$peptideReportFilePath,"\n")
}
if(exists("sqaProtein")){
if ("idScore" %in% colnames(fData(sqaProtein$eset))){
selFDataCol <- c("proteinName","ac","geneName","proteinDescription","idScore")
}else if ("Global.PG.Q.Value" %in% colnames(fData(sqaProtein$eset))){
selFDataCol <- c("proteinName","ac","geneName","proteinDescription","Global.PG.Q.Value")
}else if("Protein.Q.Value" %in% colnames(fData(sqaProtein$eset))){
selFDataCol <- c("proteinName","ac","geneName","proteinDescription","Protein.Q.Value")
}
#selFDataCol <- c("proteinName","ac","geneName","proteinDescription","idScore","idQValue")
if("nbPeptides" %in% names(fData(sqaProtein$eset))){
selFDataCol <- c(selFDataCol, 'nbPeptides')
}
selFDataCol <- selFDataCol[selFDataCol %in% names(fData(sqaProtein$eset))]
### add allAccessions
if("allAccessions" %in% names(fData(sqaProtein$eset))){
selFDataCol <- c(selFDataCol,"allAccessions")
}
# add sqa object data
cv <- sqaProtein$cv
names(cv) <- paste("Imp_cv",names(cv),sep=".")
ratio <- sqaProtein$ratio
if(ncol(ratio) > 0 ) names(ratio) <- paste("log2ratio",names(ratio),sep=".")
pValue <- sqaProtein$pValue
if(ncol(pValue) > 0 ) names(pValue) <- paste("pValue",names(pValue),sep=".")
log10_pValue <- -log10(sqaProtein$pValue) # Added: 26072021
if(ncol(log10_pValue) > 0 ) names(log10_pValue) <- paste(" -log10_pValue",names(log10_pValue),sep=".") # Added: 26072021
qValue <- sqaProtein$qValue
if(ncol(qValue) > 0 ) names(qValue) <- paste("qValue",names(qValue),sep="_")
log10_qValue <- -log10(sqaProtein$qValue) # Added: 26072021
if(ncol(log10_qValue) > 0 ) names(log10_qValue) <- paste(" -log10_qValue",names(log10_qValue),sep=".") # Added: 26072021
if (userOptions$medianInfo){
medianSignalDf <- log2(getSignalPerCondition(sqaProtein$eset))
if (userOptions$log2_i){
medianSignalDf <- 2^medianSignalDf
}
names(medianSignalDf) <- paste("Imp_Log2_medianInt",names(medianSignalDf),sep=".")
}else{
medianSignalDf <- log2(getSignalPerCondition(sqaProtein$eset, method="mean"))
if (userOptions$log2_i){
medianSignalDf <- 2^medianSignalDf
}
names(medianSignalDf) <- paste("Imp_Log2_meanInt",names(medianSignalDf),sep=".")
}
expset <- exprs(sqaProtein$eset)
expset <- log2(expset)
if ("file" %in% colnames(expDesign)){
colnames(expset) <- expDesign$file
}else{
colnames(expset) <- rownames(expDesign)
}
if (userOptions$log2_i){
expset <- 2^expset
colnames(expset) <- paste("Log2.", colnames(expset), sep="")
}else{
colnames(expset) <- paste("Imp_Log2.", colnames(expset), sep="")
}
if (fileType == "SpectronantProteinGroup"){
ext_add_col <- names(fData(sqaProtein$eset))[grepl("NrOfPrecursorsMeasured$",names(fData(sqaProtein$eset)))]
selFDataCol <- c(selFDataCol, ext_add_col)
}else if (fileType == "DiaNNProteinGroup"){
ext_add_col <- names(fData(sqaProtein$eset))[grepl("^NrOfPrecursorsMeasured\\.",names(fData(sqaProtein$eset)))]
ext_add_col_cnv <- sub('NrOfPrecursorsMeasured\\.', '', ext_add_col)
expdDesign_file_name_cnv <- gsub("-", '.', sub('Quantity\\.', '', expDesign$file))
# One option but the problem it doesn't give the correct order of the data according to the quantity values when the C is not the first samples
# for (name_i in ext_add_col_cnv){
# print (name_i)
# if (name_i %in% expdDesign_file_name_cnv){
# next
# }else{
# ch_str <- paste('NrOfPrecursorsMeasured.', name_i, sep = "")
# ext_add_col <- ext_add_col[ext_add_col != ch_str]
# }
# }
# Alternative solution
ext_add_col <- c()
for ( name_i in expdDesign_file_name_cnv){
ext_add_col <- c(ext_add_col, paste('NrOfPrecursorsMeasured.', name_i, sep = ""))
}
selFDataCol <- c(selFDataCol, ext_add_col)
}
ext_add_imp_col <- names(fData(sqaProtein$eset))[grepl("^NA_IMP_CNT",names(fData(sqaProtein$eset)))]
ext_add_imp_col_repl <- sub('NA_IMP_CNT\\.', '', ext_add_imp_col)
ext_add_imp_col_repl <- expDesign[ext_add_imp_col_repl, 'file']
ext_add_imp_col_repl <- paste("Log2.", ext_add_imp_col_repl, sep="")
selFDataCol <- c(selFDataCol, ext_add_imp_col)
ext_add_imp_col2 <- names(fData(sqaProtein$eset))[grepl("^NA_IMP_CNT",names(fData(sqaProtein$eset)))]
# I replace the 0 and 1 of the original store NA_IMP_CNT to the the actually values with NA when there was an imputated value
# Note: In the futere maybe this values should come after the original setting.
# I should also change the name but for know I will leave it like this
na_cnt_info <- fData(sqaProtein$eset)[,ext_add_imp_col2]
na_cnt_info[na_cnt_info >= 1 ] <- -1
na_cnt_info[na_cnt_info == 0 ] <- NA
na_cnt_info[is.na(na_cnt_info)] <-expset[is.na(na_cnt_info)]
na_cnt_info[na_cnt_info == -1 ] <- NA
fData(sqaProtein$eset)[,ext_add_imp_col] <- na_cnt_info
if (userOptions$extra_info){
out <- cbind(
fData(sqaProtein$eset)[,selFDataCol]
, expset
, medianSignalDf
, cv
, ratio
, fracNAFeatures = getNAFraction(sqaProtein$eset,method=c("cond","count"))
, pValue
, log10_pValue
, qValue
, log10_qValue
, FTestPValue = sqaProtein$FPValue
, FTestQValue = sqaProtein$FQValue
)[!fData(sqaProtein$eset)$isFiltered,]
}else{
out <- cbind(
fData(sqaProtein$eset)[,selFDataCol]
, expset
, medianSignalDf
, cv
, ratio
#, fracNAFeatures = getNAFraction(sqaProtein$eset,method=c("cond","count"))
, pValue
, log10_pValue
, qValue
, log10_qValue
#, FTestPValue = sqaProtein$FPValue
#, FTestQValue = sqaProtein$FQValue
)[!fData(sqaProtein$eset)$isFiltered,]
}