-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMayu.pl
executable file
·1582 lines (1405 loc) · 51.3 KB
/
Mayu.pl
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/perl
use strict;
use warnings;
use Getopt::Long;
use File::Basename;
use FindBin;
use Cwd;
##################################################################
#
# Mayu is a software package to determine the protein
# identification false discovery rate (protFDR) additionally to
# the peptide-spectrum match false discovery rate (mFDR).
# For a comprehensive help about the currently implemented
# features type:
#
# perl Mayu.pl -manual
#
#
# Lukas Reiter
# Manfred Claassen
#
##################################################################
#
# SOFTWARE
#
# Lukas Reiter - Hengartner Laboratory
# lukas.reiter@molbio.uzh.ch
# Institute of Molecular Biology
# Winterthurerstrasse 190
# University of Zuerich - Irchel
# CH-8057 Zürich
# +++++++++++++++++++++++++++++++++++++++++
# Located at:
# Institute for Molecular Systems Biology
# Aebersold Laboratory
# Wolfgang-Pauli-Str. 16
# ETH Hönggerberg, HPT C 75
# CH-8093 Zürich
# Tel: +41 44 633 39 45
#
##################################################################
##################################################################
# INPUT AND OPTIONS
##################################################################
# no output buffering
$| = 1;
my $version = '1.08';
#-------------------------------------------------------------
# protein size estimation
#-------------------------------------------------------------
my $mirror_decoy_ids_from_target_ids = 1;
# gene group identification type
# 0: prefix
# 1: suffix
# 2: middle
my $gene_group_identification_type = 1;
my $gene_group_regex = '[a-z]';
# use a special bin for zero protein size
my $special_zero_bin = 1;
# peptides smaller than this size will never be identified with
# high confidence and are therefore neglected in the protein
# size estimation (additionally to the mass window)
my $general_min_pep_length = 4;
#-------------------------------------------------------------
# input PSM parsing
#-------------------------------------------------------------
# initial discriminant score cutoff
# (PeptideProphet probability cutoff)
my $initial_ds_cutoff = 0;
# used for printing out the number of identifications
my $status_fdr = 0.01;
# file ending for the table input files
my $table_file_ending = '.csv';
# separator for table input (and output)
my $table_separator = ',';
# 1: a header is assumed to be there
# 0: there is a check for a header
my $csv_header = 0;
# 1: filter the PSM for only the protein ids fround in the
# provided fasta database
# 0: alternatively use an average protein length for binning
# the protein identifications into protein search space
# size bins.
my $filter_ids = 0;
#-------------------------------------------------------------
# id selection
#-------------------------------------------------------------
# 0: all data and mFDR range (default)
# 1: cumulative input files and mFDR range
# 2: cumulative shuffled input files and mFDR range
# 3: cumulative runs and mFDR range
# 4: cumulative shuffled runs and mFDR range
# 5: cumulative runs (orthogonality sorted) and mFDR range
# (10: phospho peptides (orthogonality sorted) and mFDR range)
my $rh_id_selection_helper = {
'0' => 'all data and mFDR range (default)',
'1' => 'cumulative input files and mFDR range',
'2' => 'cumulative shuffled input files and mFDR range',
'3' => 'cumulative runs and mFDR range',
'4' => 'cumulative shuffled runs and mFDR range',
'5' => 'cumulative runs (orthogonality sorted) and mFDR range',
'10' => 'phospho peptides (orthogonality sorted) and mFDR range',
};
#-------------------------------------------------------------
# PSM filtering
#-------------------------------------------------------------
my $rh_psm_target_decoy = {
't' => 'target PSM',
'td' => 'target and decoy PSM',
'd' => 'decoy PSM'
};
#-------------------------------------------------------------
# variables for command line input
#-------------------------------------------------------------
my (
$pepxml_in, $csv_in, $db,
$min_pep_length, $decoy_id_prefix, $tar_dec_ratio,
$max_psm_fdr, $psm_fdr_steps, $nr_missed_cleavages,
$min_pep_mass, $max_pep_mass, $nr_prot_size_bins,
$file_name_base, $id_set_selection, $nr_runs_steps,
$ids_out_tag, $equidist_bins, $equicount_bins,
$dont_corr_id_seq, $run_r, $use_xml_parser,
$p_mfdr, $p_bin_prot, $p_prot_feat,
$print_input_output, $print_cumulative, $remove_ambiguous, $v,
$s, $help, $manual
);
# options are described in the usage() sub: perl Mayu.pl -h
GetOptions(
'A=s' => \$pepxml_in, # input pepxml.xml file or dir
'B=s' => \$csv_in, # input table.csv, comma sep
'C=s' => \$db, # target-decoy database
'D=i' => \$min_pep_length, # peptides have to be >= length
'E=s' => \$decoy_id_prefix, # id prefix for decoy hits
'F=f' => \$tar_dec_ratio, # target to decoy ratio (e.g. 1)
'G=f' => \$max_psm_fdr, # maximal PSM FDR
'H=i' => \$psm_fdr_steps, # number of analysis steps
'I=i' => \$nr_missed_cleavages, # number of missed cleavages
'J=f' => \$min_pep_mass, # minimal peptide mass
'K=f' => \$max_pep_mass, # maximal peptide mass
'L=i' => \$nr_prot_size_bins, # number of protein size bins
'M=s' => \$file_name_base, # use this as file name base
'N=i' => \$id_set_selection, # id set selection
'O=i' => \$nr_runs_steps, # cumulative runs
'P=s' => \$ids_out_tag, # output filtered ids
'equidist' => \$equidist_bins, # equicount / equidist
'dcis' => \$dont_corr_id_seq, # correct for identical seq
'xmlparser' => \$use_xml_parser, # use a proper xml parser
'runR' => \$run_r, # run R to finish analysis
'PmFDR' => \$p_mfdr, # print the mFDR files
'PbinProt' => \$p_bin_prot, # print bin protFDR files
'PprotFeat' => \$p_prot_feat, # print prot feature files
'Pio' => \$print_input_output, # print pepxml input to csv
'cumul' => \$print_cumulative, # print cumulative input
'remamb' => \$remove_ambiguous, # remove ambiguous PSMs
'verbose' => \$v,
'status' => \$s,
'help' => \$help,
'manual' => \$manual
);
##################################################################
# SET DEFAULT VALUES OR CHANGE IMPOSSIBLE VALUES
##################################################################
$pepxml_in = '' unless defined($pepxml_in);
$csv_in = '' unless defined($csv_in);
$db = '' unless defined($db);
$min_pep_length = 0 unless defined($min_pep_length);
$min_pep_length = 0 unless $min_pep_length > 0;
$decoy_id_prefix = 'rev_' unless defined($decoy_id_prefix);
$tar_dec_ratio = 1 unless defined($tar_dec_ratio);
$max_psm_fdr = 0.01 unless defined($max_psm_fdr);
$max_psm_fdr = 1 unless $max_psm_fdr <= 1;
$psm_fdr_steps = 11 unless defined($psm_fdr_steps);
$psm_fdr_steps = 11 unless $psm_fdr_steps >= 2;
$nr_missed_cleavages = 0 unless defined($nr_missed_cleavages);
$min_pep_mass = 400 unless defined($min_pep_mass);
$min_pep_mass = 0 unless $min_pep_mass >= 0;
$max_pep_mass = 6000 unless defined($max_pep_mass);
$max_pep_mass = 0 unless $max_pep_mass >= 0;
$nr_prot_size_bins = 10 unless defined($nr_prot_size_bins);
$nr_prot_size_bins = 10 unless $nr_prot_size_bins >= 1;
$id_set_selection = 0 unless defined($id_set_selection);
$nr_runs_steps = 2 unless defined($nr_runs_steps);
$nr_runs_steps = 2 unless $nr_runs_steps >= 2;
$ids_out_tag = '' unless defined($ids_out_tag);
$equidist_bins = 0 unless defined($equidist_bins);
$equicount_bins = 1 unless $equidist_bins == 1;
$equicount_bins = 0 unless $equidist_bins == 0;
$dont_corr_id_seq = 0 unless defined($dont_corr_id_seq);
$use_xml_parser = 0 unless defined($use_xml_parser);
$run_r = 0 unless defined($run_r);
$p_mfdr = 0 unless defined($p_mfdr);
$p_bin_prot = 0 unless defined($p_bin_prot);
$p_prot_feat = 0 unless defined($p_prot_feat);
$print_input_output = 0 unless defined($print_input_output);
$print_cumulative = 0 unless defined($print_cumulative);
$remove_ambiguous = 0 unless defined($remove_ambiguous);
$v = 0 unless defined($v);
$s = 0 unless defined($s);
$help = 0 unless defined($help);
$manual = 0 unless defined($manual);
##################################################################
# CHECK INPUT
##################################################################
if ($manual) {
manual();
exit;
}
elsif ($help) {
usage();
exit;
}
elsif ( ( -e $pepxml_in || -e $csv_in ) && -e $db ) {
main();
}
else {
print "\n\n need existing input for (-A or -B) and (-C)!\n\n";
print " -A: '$pepxml_in'\n";
print " -B: '$csv_in'\n";
print " -C: '$db'\n\n";
usage();
}
##################################################################
# MAIN
##################################################################
# Title : main()
# Usage :
# Function :
# Returns :
# Args :
sub main {
#-------------------------------------------------------------
# load modules and show used options
#-------------------------------------------------------------
# directory where the script is located
my $mayu_dir = dirname($0);
$mayu_dir =~ s/\\/\//g;
# current working directory
my $wd = getcwd();
# wd needs to be the Mayu directory for proper loading
# TPP installs in 2 different locations depending on OS
use lib "lib";
# load modules
use MayuTools;
use MascotCSV;
use MayuProteinSize;
use PSMSet;
# proper xml parser needs to be installed
if ($use_xml_parser) {
my $parser = "XML::Parser::PerlSAX";
eval("use $parser;");
die(" Could not find $parser!") if $@ ne "";
my $handler = "MayuPepXMLHandler"; # XML::SAX::Base
eval("use $handler;");
die(" Could not find $handler!") if $@ ne "";
}
# no installation required
else {
use MayuPepXMLParser;
}
use TandemMSIdSelectionScheme;
use MayuManager;
use PSMFDR;
use PeptideIdFDR;
use ProteinIdFDR;
use LocalProteinIdFDR;
use RPlots;
# helper subroutines
my $tools = MayuTools->new( 0, 0 );
$tools->set_time();
# output file name base
my $out_base = '';
my $timestamp = $tools->get_file_name_base();
if ( defined($file_name_base) ) {
$out_base = $file_name_base;
}
else {
$out_base = $timestamp;
}
# print all input and main options
print_options($out_base);
#-------------------------------------------------------------
# estimate the protein sizes that are accessible to the
# search engine. Return BinnedEntity object(s)
#-------------------------------------------------------------
print " ------------------------------------\n" if $v;
print " protein size\n" if $v;
print " ------------------------------------\n" if $v;
my $Mayu_prot_size = MayuProteinSize->new( $v, $s );
my ( $bin_prot, $bin_pep ) = $Mayu_prot_size->protein_size_analysis(
$tools,
$db,
$min_pep_length,
$decoy_id_prefix,
$nr_missed_cleavages,
$min_pep_mass,
$max_pep_mass,
$nr_prot_size_bins,
$dont_corr_id_seq,
$mirror_decoy_ids_from_target_ids,
$gene_group_identification_type,
$gene_group_regex,
$general_min_pep_length,
$special_zero_bin,
$equicount_bins,
$filter_ids
);
print "\n" if $v;
# TODO - add static modifications and types of modifications
# to the PSMSet class
# - only possible for pepxml, not for other input formats
# <aminoacid_modification aminoacid="M" massdiff="15.9994"
# mass="147.1920" variable="Y" symbol="*"/>
# <aminoacid_modification aminoacid="C" massdiff="8.9339"
# mass="339.3330" variable="Y" symbol="#"/>
# <aminoacid_modification aminoacid="C" massdiff="227.2603"
# mass="330.3991" variable="N"/>
#-------------------------------------------------------------
# get input PSM in the form of a PSMSet object
#-------------------------------------------------------------
print " ------------------------------------\n" if $v;
print " input PSM (mFDR)\n" if $v;
print " ------------------------------------\n" if $v;
my ( $psm_set, $ra_roc_files ) = get_psm_set(
$tools, $pepxml_in, $csv_in,
$initial_ds_cutoff, $decoy_id_prefix, $tar_dec_ratio,
$max_psm_fdr, $table_file_ending, $table_separator,
$out_base, $bin_prot, $db
);
print "\n" if $v;
if ( $psm_set->is_empty() ) {
print " no input data! "
. "(file not existing or data of low quality)\n";
exit;
}
#-------------------------------------------------------------
# MayuManager
# set the error models to the MayuManager and add binning
# entities if the model requires them.
# MayuManager will initialize the error models together with
# the PSMSets created by the TandemMSIdSelectionScheme.
# All the error models provide similar functionality for
# - error calculation
# - local error calculation
# - data querying
#
# some error models:
# - PSMFDR
# - PeptideIdFDR
# - ProteinIdFDR
# (- NaivePeptideIdFDR)
# (- NaiveProteinIdFDR)
#
# functions provided by all of the error models:
# - get_error_model_identifier()
# - create_error_model()
# - set_psm_set(), ( $ra_ra with 6 columns )
# - get_error_summary()
# functions provided by some of the error models:
# - get_error_bin_table()
#-------------------------------------------------------------
# TandemMSIdSelectionScheme
# selects the identifications and passes them to the
# MayuManager
#-------------------------------------------------------------
print " ------------------------------------\n" if $v;
print " protein identification FDR (protFDR)\n" if $v;
print " ------------------------------------\n" if $v;
my $p_manager = MayuManager->new( $v, $s );
$p_manager->set_protein_features($bin_prot);
my $feat_base = $out_base . '_feat_prot_' . $version;
$p_manager->set_protein_feature_output_base($feat_base)
if $p_prot_feat;
# set the error models
my $psm_em = PSMFDR->new( $v, $s, $tar_dec_ratio );
$p_manager->set_error_model($psm_em);
my $pep_em = PeptideIdFDR->new( $v, $s, $bin_pep, $tar_dec_ratio );
$p_manager->set_error_model($pep_em);
my $prot_em = ProteinIdFDR->new( $v, $s, $bin_prot, $tar_dec_ratio );
$p_manager->set_error_model($prot_em);
my $lp_em = LocalProteinIdFDR->new( $v, $s, $tar_dec_ratio );
$p_manager->set_error_model($lp_em);
# print out the used error models
$p_manager->print_registered_error_models() if $v;
# decide on the id selection scheme
my $sel_scheme = TandemMSIdSelectionScheme->new( $v, $s );
# 0: all data and mFDR range (default)
# 1: cumulative input files and mFDR range
# 2: cumulative shuffled input files and mFDR range
# 3: cumulative runs and mFDR range
# 4: cumulative shuffled runs and mFDR range
# 5: cumulative runs (orthogonality sorted) and mFDR range
# 10: phospho peptides (orthogonality sorted) and mFDR range
if ( $id_set_selection == 1 ) {
print "\n DATA SELECTION SCHEME:\n" if $v;
print " cumulative input files" . " -> set of mFDR\n\n"
if $v;
$sel_scheme->cumulative_file_mFDR_range( $p_manager, $psm_set,
$max_psm_fdr, $psm_fdr_steps );
}
elsif ( $id_set_selection == 2 ) {
print "\n DATA SELECTION SCHEME:\n" if $v;
print " cumulative shuffled input files" . " -> set of mFDR\n\n"
if $v;
$sel_scheme->cumulative_shuffled_file_mFDR_range( $p_manager,
$psm_set, $max_psm_fdr, $psm_fdr_steps );
}
elsif ( $id_set_selection == 3 ) {
print "\n DATA SELECTION SCHEME:\n" if $v;
print
" cumulative runs in $nr_runs_steps steps (sorted according "
. "to files and then runs) -> set of mFDR\n"
if $v;
$sel_scheme->cumulative_run_mFDR_range(
$p_manager, $psm_set, $max_psm_fdr,
$psm_fdr_steps, $nr_runs_steps
);
}
elsif ( $id_set_selection == 4 ) {
print "\n DATA SELECTION SCHEME:\n" if $v;
print " cumulative shuffled runs in $nr_runs_steps steps "
. " -> set of mFDR\n\n"
if $v;
$sel_scheme->cumulative_shuffled_run_mFDR_range(
$p_manager, $psm_set, $max_psm_fdr,
$psm_fdr_steps, $nr_runs_steps
);
}
elsif ( $id_set_selection == 5 ) {
print "\n DATA SELECTION SCHEME:\n" if $v;
print " cumulative runs in $nr_runs_steps steps (sorted according"
. " to orthogonality) -> set of mFDR\n\n"
if $v;
$sel_scheme->ortho_run_mFDR_range( $p_manager, $psm_set,
$max_psm_fdr, $psm_fdr_steps, $nr_runs_steps, $out_base,
$version );
}
# TODO implement
elsif ( $id_set_selection == 10 ) {
print "\n DATA SELECTION SCHEME:\n" if $v;
print " complete phospho data set -> set of mFDR\n\n" if $v;
$sel_scheme->phospho_id_mFDR_range( $p_manager, $psm_set,
$max_psm_fdr, $psm_fdr_steps );
}
# this is the default
else {
print "\n DATA SELECTION SCHEME:\n" if $v;
print " complete data set -> set of mFDR\n\n" if $v;
$sel_scheme->all_id_mFDR_range( $p_manager, $psm_set, $max_psm_fdr,
$psm_fdr_steps );
}
print " $feat_base\n" if $p_prot_feat && $v;
print "\n" if $v;
#-------------------------------------------------------------
# print out
#-------------------------------------------------------------
print " ------------------------------------\n" if $v;
print " printing files\n" if $v;
print " ------------------------------------\n" if $v;
# print out a file with local FDR of proteins with similar
# sizes if this error model was registered
if ( $p_manager->error_model_exists('ProteinIdFDR') && $p_bin_prot ) {
my $psize_locFDR_base = $out_base
. '_prot_size_local_FDR_' . $version;
my ( $prot_size_bin_csv, $prot_size_bin_txt ) =
print_prot_size_bin_file( $p_manager, $psize_locFDR_base, $tools );
print " $prot_size_bin_csv, $prot_size_bin_txt\n" if $v;
}
# print out main summary file
my $main_base = $out_base . '_main_' . $version;
my ( $fdr_csv, $fdr_txt ) =
print_main_file( $p_manager, $main_base, $tools );
print " main output files:\n" if $v;
print " $fdr_csv, $fdr_txt\n" if $v;
print "\n" if $v;
# print out a table of filtered psm
# e.g. mFDR=0.01:t or pepFDR=0.02:td or protFDR=0.05:t
unless ( $ids_out_tag =~ /^$/ ) {
my $psm_file_base = $out_base . '_psm_';
my (
$id_csv_file, $nr_psm, $fdr_type,
$fdr_value, $target_decoy, $mFDR
)
= print_psm_file( $psm_set, $ids_out_tag, $p_manager,
$psm_file_base, $max_psm_fdr, $tools );
print " $nr_psm PSM printed out\n" if $v;
print "\n" if $v;
}
#-------------------------------------------------------------
# R plots
#-------------------------------------------------------------
if ( $run_r ) {
print " ------------------------------------\n" if $v;
print " R plots\n" if $v;
print " ------------------------------------\n" if $v;
my $r_plots = RPlots->new( $v, $s );
my $main_template_in = $mayu_dir . '/templates/main.R';
# prepare the path of the r library
my $r_lib = $mayu_dir . '/r_lib/';
#-------------------------------------------------------------
# PmFDR
#-------------------------------------------------------------
if ( $p_mfdr ) {
my $roc_files_r_vector = 'c("' . join('","', @$ra_roc_files) . '")';
# prepare the file name of the r .pdf output file
my $r_out_base = $out_base . '_ds_vs_mFDR_' . $version;
my $r_out = $r_out_base . '.pdf';
# create replace hash
my $rh_replace = {
'<REPLACE_WITH_LIB_PATH>' => $r_lib,
'"<REPLACE_WITH_ROC_FILES>"' => $roc_files_r_vector,
'<REPLACE_WITH_OUT_PATH>' => getcwd(),
'<REPLACE_WITH_OUTFILE>' => $r_out
};
my $template_in = $mayu_dir . '/templates/PmFDR.R';
# if not defined then the new template is written to the cwd
my $template_out_dir = undef;
$r_plots->run_r_template( $template_in, $rh_replace,
$template_out_dir );
}
#-------------------------------------------------------------
# main
#-------------------------------------------------------------
# prepare the file name of the r .pdf output file
my $main_r_out_base = $out_base . '_main_' . $version;
my $main_r_out = $main_r_out_base . '.pdf';
# create replace hash
my $rh_replace = {
'<REPLACE_WITH_LIB_PATH>' => $r_lib,
'<REPLACE_WITH_INPUT_FILE>' => $fdr_csv,
'<REPLACE_WITH_OUT_PATH>' => getcwd(),
'<REPLACE_WITH_OUTFILE>' => $main_r_out
};
# if not defined then the new template is written to the cwd
my $template_out_dir = undef;
$r_plots->run_r_template( $main_template_in, $rh_replace,
$template_out_dir );
}
print "\n";
print " " . $tools->get_time() . " seconds run time\n";
print "\n";
}
# Title : print_psm_file()
# Usage :
# Function :
# Returns :
# Args :
sub print_psm_file {
my ( $psm_set, $ids_out_tag, $p_manager, $out_base, $max_psm_fdr,
$tools ) = @_;
# extract the type of filtering
my ( $fdr_type, $fdr_value, $target_decoy ) =
get_ids_filtering($ids_out_tag);
# output file name
my $id_csv_file = $out_base
. $fdr_type
. $fdr_value . '_'
. $target_decoy . '_'
. $version . '.csv';
# determine the corresponding mFDR cutoff
my $mFDR = 0.01; # default
if ( $fdr_type ne 'mFDR' ) {
my $header = 1;
my $ra_ra_fdr_table = $p_manager->get_table($header);
# remove the header and get index of columns
my $ra_header = shift @$ra_ra_fdr_table;
my ( $mfdr_col, $fdr_col );
for ( my $i = 0 ; $i < @$ra_header ; $i++ ) {
if ( $ra_header->[$i] =~ /^mFDR$/ ) {
$mfdr_col = $i;
}
if ( $ra_header->[$i] =~ /^$fdr_type$/ ) {
$fdr_col = $i;
}
}
my $ra_ra_FDR_mFDR = [];
foreach my $ra (@$ra_ra_fdr_table) {
push @$ra_ra_FDR_mFDR, [ $ra->[$fdr_col], $ra->[$mfdr_col] ];
}
# get an mFDR cutoff
my @mFDR =
$tools->linear_inter_or_extrapolate( $fdr_value,
$ra_ra_FDR_mFDR );
$mFDR = $mFDR[0];
}
else {
$mFDR = $fdr_value;
}
# check whether the selected PSM can be printed out
# but print out the PSM anyway
if ( $mFDR > $max_psm_fdr ) {
print
" your desired mFDR $mFDR for output (-P ...) is beyond the "
. "chosen mFDR $max_psm_fdr used for calculations (-G ...) !\n";
print " use a higher mFDR with -G for proper output "
. "(e.g. -G $mFDR)!\n";
print "\n";
}
# print out the selected printing of PSM
print " filtered data:\n" if $v;
print " printing a set of filtered PSM to $id_csv_file\n" if $v;
print " filtering PSM with $fdr_type = $fdr_value, "
. "corresponding mFDR = $mFDR\n"
if $v;
print " keeping " . $rh_psm_target_decoy->{$target_decoy} . "\n"
if $v;
# extract the data from $psm_set
my $ra_ra_psm = [];
my $nr_psm = 0;
if ( $target_decoy =~ /^t$/ ) {
$ra_ra_psm = $psm_set->get_target_psm_by_fdr($mFDR);
$nr_psm = $psm_set->get_nr_target_psm_by_fdr($mFDR);
}
elsif ( $target_decoy =~ /^td$/ ) {
$ra_ra_psm = $psm_set->get_psm_by_fdr($mFDR);
$nr_psm = $psm_set->get_nr_psm_by_fdr($mFDR);
}
else {
$ra_ra_psm = $psm_set->get_decoy_psm_by_fdr($mFDR);
$nr_psm = $psm_set->get_nr_decoy_psm_by_fdr($mFDR);
}
open( O, ">$id_csv_file" ) or warn $!;
print O join( $table_separator,
( 'scan', 'pep', 'prot', 'mod', 'score', 'decoy', 'mFDR' ) )
. "\n";
foreach my $ra_psm (@$ra_ra_psm) {
$ra_psm->[3] = get_string_from_mod( $ra_psm->[3] );
print O join( $table_separator, @$ra_psm ) . "\n";
}
close(O);
return (
$id_csv_file, $nr_psm, $fdr_type,
$fdr_value, $target_decoy, $mFDR
);
}
# Title : get_ids_filtering()
# Usage :
# Function :
# Returns :
# Args :
sub get_ids_filtering {
my ($ids_out_tag) = @_;
# default values
# filter with 0.05 on protFDR and report only target hits
my ( $fdr_type, $fdr_value, $target_decoy ) = ( 'protFDR', 0.05, 't' );
my @parts = split( ':', $ids_out_tag );
if ( @parts == 2 ) {
my @fdr_parts = split( '=', $parts[0] );
if ( @fdr_parts == 2 ) {
if ( $fdr_parts[0] =~ /^mFDR$/
|| $fdr_parts[0] =~ /^pepFDR$/
|| $fdr_parts[0] =~ /^protFDR$/ )
{
$fdr_type = $fdr_parts[0];
}
if ( $fdr_parts[1] =~ /^[\.\d]+$/ ) {
if ( $fdr_parts[1] >= 0 && $fdr_parts[1] <= 1 ) {
$fdr_value = $fdr_parts[1];
}
}
}
if ( $parts[1] =~ /^t$/
|| $parts[1] =~ /^d$/
|| $parts[1] =~ /^td$/ )
{
$target_decoy = $parts[1];
}
}
return ( $fdr_type, $fdr_value, $target_decoy );
}
# Title : print_prot_size_bin_file()
# Usage :
# Function :
# Returns :
# Args :
sub print_prot_size_bin_file {
my ( $p_manager, $out_base, $tools ) = @_;
my $csv_sep = ",";
my $prot_size_csv = $out_base . '.csv';
my $prot_size_txt = $out_base . '.txt';
my $fh_prot_size_bin_csv = FileHandle->new();
$fh_prot_size_bin_csv->open(">$prot_size_csv") or die $!;
my $header = 1;
my $ra_ra_prot_size_bin_table =
$p_manager->get_prot_bin_table($header);
foreach (@$ra_ra_prot_size_bin_table) {
print $fh_prot_size_bin_csv join( $csv_sep, @$_ ) . "\n";
}
$fh_prot_size_bin_csv->close();
# print files with equally spaced column
$tools->print_data_in_table_style( $ra_ra_prot_size_bin_table,
$prot_size_txt );
return ( $prot_size_csv, $prot_size_txt );
}
# Title : print_main_file()
# Usage :
# Function :
# Returns :
# Args :
sub print_main_file {
my ( $p_manager, $out_base, $tools ) = @_;
my $csv_sep = ",";
my $fdr_csv = $out_base . '.csv';
my $fdr_txt = $out_base . '.txt';
my $fh_fdr_csv = FileHandle->new();
$fh_fdr_csv->open(">$fdr_csv") or die $!;
my $header = 1;
my $ra_ra_fdr_table = $p_manager->get_table($header);
foreach (@$ra_ra_fdr_table) {
print $fh_fdr_csv join( $csv_sep, @$_ ) . "\n";
}
$fh_fdr_csv->close();
# print files with equally spaced column
$tools->print_data_in_table_style( $ra_ra_fdr_table, $fdr_txt );
return ( $fdr_csv, $fdr_txt );
}
# Title : get_psm_set()
# Usage :
# Function :
# Returns :
# Args :
sub get_psm_set {
my (
$tools, $pepxml_in, $csv_in,
$initial_ds_cutoff, $decoy_id_prefix, $tar_dec_ratio,
$max_psm_fdr, $table_file_ending, $table_separator,
$out_base, $bin_prot, $db
)
= @_;
# combine the PSM of all the input files after filtering
# and processing in this object
my $complete_psm_set = PSMSet->new( $v, $s );
my @roc_files = ();
if ( -e $pepxml_in ) {
my @pepxml = $tools->get_files( $pepxml_in, '.xml' );
my $nr_pepxml = @pepxml;
print " no xml input found!\n" if $nr_pepxml == 0;
foreach my $pepxml ( sort @pepxml ) {
my ( $psm_set, $roc_file ) =
get_psm_set_from_pepxml( $pepxml, $initial_ds_cutoff,
$decoy_id_prefix, $tar_dec_ratio, $max_psm_fdr, $tools,
$out_base, $db );
push @roc_files, $roc_file;
$complete_psm_set->add_psm( $psm_set->get_psm(), $pepxml,
$psm_set->get_runs() );
if ($print_cumulative) {
print " cumulative:\n";
$complete_psm_set->print_six_nr_by_fdr($status_fdr);
}
print "\n" if $v;
}
}
if ( -e $csv_in ) {
my @csv = $tools->get_files( $csv_in, $table_file_ending );
my $nr_csv = @csv;
print " no table input found!\n" if $nr_csv == 0;
foreach my $csv ( sort @csv ) {
my ( $psm_set, $roc_file ) =
get_psm_set_from_table( $csv, $initial_ds_cutoff,
$decoy_id_prefix, $tar_dec_ratio, $max_psm_fdr, $tools,
$table_separator, $out_base, $db );
push @roc_files, $roc_file;
$complete_psm_set->add_psm( $psm_set->get_psm(), $csv,
$psm_set->get_runs() );
if ($print_cumulative) {
print " cumulative:\n";
$complete_psm_set->print_six_nr_by_fdr($status_fdr);
}
print "\n" if $v;
}
}
# filter out PSM whose protein ids were not found in the protein
# fasta database
if ($filter_ids) {
print " filtering out protein ids that were not found in"
. " the target decoy protein database\n"
if $v;
my $rh_ids = $bin_prot->get_ids();
$complete_psm_set->filter_by_protein_id($rh_ids);
}
# print an overview of identifications for a given FDR
print " cumulative input data:\n" if $v;
$complete_psm_set->print_six_nr_by_fdr($status_fdr) if $v;
return ( $complete_psm_set, \@roc_files );
}
# Title : get_psm_set_from_pepxml()
# Usage :
# Function :
# Returns : a PSM set object
# Args :
sub get_psm_set_from_pepxml {
my ( $pepxml, $initial_pps_cutoff, $decoy_id_prefix, $tar_dec_ratio,
$max_psm_fdr, $tools, $out_base, $db )
= @_;
my $pepxml_file = basename($pepxml);
# extracted from pepxml
# 0 spectrum
# 1 peptide
# 2 main id (protein)
# 3 modification info
# 4 PeptideProphet probability (PPs)
my $ra_ra_psm = [];
my $status_prefix = "parsing $pepxml_file... ";
if ($use_xml_parser) {
my $Mayu_handler = MayuPepXMLHandler->new();
# create parser and set the handler
my $parser = XML::Parser::PerlSAX->new(
Handler => $Mayu_handler,
ErrorContext => 2,
);
my $status_prefix = "parsing $pepxml_file... ";
$Mayu_handler->set( $initial_pps_cutoff, $pepxml, $parser, $v,
$s, $status_prefix );
my %parser_args = ( Source => { SystemId => $pepxml } );
my $result = $parser->parse(%parser_args);
$ra_ra_psm = $Mayu_handler->results();
}
else {
my $Mayu_parser =
MayuPepXMLParser->new( $initial_pps_cutoff, $pepxml, $v, $s,
$status_prefix );
$ra_ra_psm = $Mayu_parser->parse();
}
print "\r $pepxml_file parsed \n" if $s;
print " $pepxml_file parsed\n" if $v && !$s;
# print the PSM to a csv file
if ($print_input_output) {
my $csv_file = $pepxml;
$csv_file =~ s/\.xml$/_mayu.csv/g;
open( O, ">$csv_file" ) or warn $!;
foreach my $ra_psm (@$ra_ra_psm) {
print O join( $table_separator,
( $ra_psm->[0], $ra_psm->[1], $ra_psm->[2],
get_string_from_mod( $ra_psm->[3] ), $ra_psm->[4] ) )
. "\n";
}
close(O);
}
# calculate the PSM FDR based on the target decoy approach,
# use the PeptideProphet score as discriminant score
my $psm_set = PSMSet->new( $v, $s, $ra_ra_psm, $pepxml );
my $roc_file = '';
( $psm_set, $roc_file ) = preprocess_psm_set(
$psm_set, $min_pep_length, $decoy_id_prefix,
$tar_dec_ratio, $status_fdr, $max_psm_fdr,
$pepxml_file, $out_base, $tools, $db
);
return ( $psm_set, $roc_file );
}
# Title : get_psm_set_from_table()
# Usage :
# Function :
# Returns : a PSM set object
# Args :
sub get_psm_set_from_table {
my ( $csv, $initial_ds_cutoff, $decoy_id_prefix, $tar_dec_ratio,
$max_psm_fdr, $tools, $table_separator, $out_base, $db )
= @_;
my $status_fdr = 0.01;
my $csv_file = basename($csv);
# extracted from table
# 0 spectrum
# 1 peptide
# 2 main id (protein)
# 3 modification info
# 4 PeptideProphet probability (PPs)
my $ra_ra_psm =
parse_csv( $csv, $initial_ds_cutoff, $table_separator, $csv_header );
print "\r $csv_file parsed \n" if $s;
print " $csv_file parsed\n" if $v && !$s;
# print the PSM to a csv file
if ($print_input_output) {
my $csv_file = $csv;
$csv_file =~ s/\.csv$/_mayu.csv/g;
open( O, ">$csv_file" ) or warn $!;
foreach my $ra_psm (@$ra_ra_psm) {
print O join( $table_separator,
( $ra_psm->[0], $ra_psm->[1], $ra_psm->[2],
get_string_from_mod( $ra_psm->[3] ), $ra_psm->[4] ) )
. "\n";
}
close(O);
}
my $psm_set = PSMSet->new( $v, $s, $ra_ra_psm, $csv );