-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathscafe.tool.cm.filter
2416 lines (2074 loc) · 121 KB
/
scafe.tool.cm.filter
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/env perl
# This chunk of stuff was generated by App::FatPacker. To find the original
# file's code, look for the end of this BEGIN block or the string 'FATPACK'
BEGIN {
my %fatpacked;
s/^ //mg for values %fatpacked;
my $class = 'FatPacked::'.(0+\%fatpacked);
no strict 'refs';
*{"${class}::files"} = sub { keys %{$_[0]} };
if ($] < 5.008) {
*{"${class}::INC"} = sub {
if (my $fat = $_[0]{$_[1]}) {
my $pos = 0;
my $last = length $fat;
return (sub {
return 0 if $pos == $last;
my $next = (1 + index $fat, "\n", $pos) || $last;
$_ .= substr $fat, $pos, $next - $pos;
$pos = $next;
return 1;
});
}
};
}
else {
*{"${class}::INC"} = sub {
if (my $fat = $_[0]{$_[1]}) {
open my $fh, '<', \$fat
or die "FatPacker error loading $_[1] (could be a perl installation issue?)";
return $fh;
}
return;
};
}
unshift @INC, bless \%fatpacked, $class;
} # END OF FATPACK CODE
#====================================================================================================================================================#
#<use>
$|++; #---turn on the auto flush for the progress bar
no warnings 'utf8';
use warnings;
use strict;
use File::Path;
use File::Copy;
use File::Basename;
use List::Util qw (shuffle);
use File::Spec::Functions qw(rel2abs abs2rel);
use Time::HiRes qw( time );
use Getopt::Long 'HelpMessage';
use Cwd 'abs_path';
use AutoLoader qw/AUTOLOAD/;
#<\use>
#====================================================================================================================================================#
#====================================================================================================================================================#
#<doc>
=head1 SYNOPSIS
5'-O~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~AAA-3'
O~~~AA O~~ O~ O~~~~~~~AO~~~~~~~~A
O~~ O~~ O~~ O~~ O~O~~ O~~ O~~
O~~ O~~ O~ O~~ O~~ O~~
O~~ O~~ O~~ O~~ O~~~~~AA O~~~~~~A
O~~ O~~ O~~~~~A O~~ O~~ O~~
O~~ O~~ O~~ O~~ O~~ O~~ O~~ O~~
O~~~~A O~~~ O~~ O~~O~~ O~~~~~~~AA
┌─ᐅ 5'-O~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-3'
...===┴========================================================================================...
Single Cell Analysis of Five-prime End (SCAFE) Tool Suite
---> scafe.tool.cm.filter <---
<--- tool, common mode, filter for genuine TSS clusters --->
Description:
This tool filter for genuine TSS clusters based on a logistic regression model on TSS cluster properties.
Usage:
scafe.tool.cm.filter [options] --ctss_bed_path --ung_ctss_bed_path --tssCluster_bed_path --genome --outputPrefix --outDir
--ctss_bed_path <required> [string] ctss file contains all ctss,
*.collapse.ctss.bed.gz from scafe.tool.sc.bam_to_ctss.pl,
5th column is number reads/UMI
--ung_ctss_bed_path <required> [string] ctss file contains only ctss with unencoded G,
*.unencoded_G.collapse.ctss.bed.gz from scafe.tool.sc.bam_to_ctss.pl,
5th column is number reads/UMI
--tssCluster_bed_path <required> [string] bed file contains all TSS clusters,
*.tssCluster.bed.gz from scafe.tool.cm.cluster.pl
--genome <required> [string] name of genome reference, e.g. hg19.gencode_v32lift37
--outputPrefix <required> [string] prefix for the output files
--outDir <required> [string] directory for the output files
--tssCluster_flank_size (optional) [integer] size of regions (each side) flanking a TSS cluster summit for
counting UMI/reads for expression levels calculation (default = 75)
--local_bkgd_extend_size (optional) [integer] size of regions (each side) flanking a TSS cluster summit for
defining the scope for calculating local background (default = 500)
--min_gold_num (optional) [integer] minimum number of gold standard regions for training and testing the
logical regression model (default = 100)
--training_pct (optional) [float] top and bottom percentage of the TSS clusters, ranked by signal in
$training_signal_path, used for training of logical regression model
(default = 5)
--training_signal_path (optional) [string] quantitative signal (e.g. ATAC -logP, in bigwig format), or binary genomic
regions (e.g. annotated CRE, in bed format) used for training of logical
regression model If null, $usr_glm_model_path must be supplied for
pre-built logical regression model. It overrides usr_glm_model_path
(default=null)
--testing_signal_path (optional) [string] quantitative signal (e.g. ATAC -logP, in bigwig format), or binary genomic
regions (e.g. annotated CRE, in bed format) used for testing the performance
of the logical regression model. If null, annotated TSS from $genome will be
used as binary genomic regions. (default=null)
--usr_glm_model_path (optional) [string] pre-built logical regression model from the Caret package in R. Used only if
training_signal_path is not supplied. Models were pre-built for each genome
and used as default.
--Rscript_bin (optional) [string] path to the Rscript bin, aim to allow users to supply an R version other the
system wide R version. Package Caret must be installed. (Defaul = Rscript)
--default_cutoff (optional) [integer] logistic probablity cutoffs for the "default" stringency (Default = 0.5)
--exclude_chrom_list (optional) [string] a list of comma delimited chromosome to be excluded in the training and
testing of the logical regression model (Default = chrM)
--overwrite (optional) [yes/no] erase outDir/outputPrefix before running (default=no)
Dependencies:
R packages: 'ROCR','PRROC', 'caret', 'e1071', 'ggplot2', 'scales', 'reshape2'
bedtools
bigWigAverageOverBed
For demo, cd to SCAFE dir and run,
scafe.tool.cm.filter \
--overwrite=yes \
--ctss_bed_path=./demo/output/sc.solo/bam_to_ctss/demo/bed/demo.collapse.ctss.bed.gz \
--ung_ctss_bed_path=./demo/output/sc.solo/bam_to_ctss/demo/bed/demo.unencoded_G.collapse.ctss.bed.gz \
--tssCluster_bed_path=./demo/output/sc.solo/cluster/demo/bed/demo.tssCluster.bed.gz \
--training_signal_path=./demo/input/atac/demo.atac.bw \
--testing_signal_path=./demo/input/atac/demo.atac.bw \
--genome=hg19.gencode_v32lift37 \
--outputPrefix=demo \
--outDir=./demo/output/sc.solo/filter/
=head1 VERSION
v0.9.0-beta [March 20, 2021]
-Initial pre-release
v1.0.0 [June 6, 2022]
-No update
=cut#
#<\doc>
#====================================================================================================================================================#
#====================================================================================================================================================#
#<lastCmdCalled>
#
# notCalledBefore
#
# notCalledBefore
#
#<\lastCmdCalled>
#====================================================================================================================================================#
#====================================================================================================================================================#
#<global>
my $scriptDirPath = dirname(rel2abs($0));
my $scriptAbsPath = abs_path($0);
my ($curntTimeStamp) = &timeStamp();#->2306
my $ARGVStr = join "\n", (¤tTime(), $scriptAbsPath, @ARGV);#->740
my $globalReadmeHsh_ref = {};
our $tmplog_fh;
#<\global>
#====================================================================================================================================================#
#====================================================================================================================================================#
{ #Main sections lexical scope starts
#====================================================================================================================================================#
#====================================================================================================================================================#
# section 0_startingTasks
#
#<section ID="startingTasks" num="0">
my ($ctss_bed_path, $ung_ctss_bed_path, $tssCluster_bed_path, $training_signal_path, $testing_signal_path, $genome, $tssCluster_flank_size, $local_bkgd_extend_size, $min_gold_num, $training_pct, $Rscript_bin, $usr_glm_model_path, $default_cutoff, $exclude_chrom_list, $outputPrefix, $outDir, $overwrite) = &readParameters();#->2201
#<\section>
#====================================================================================================================================================#
#====================================================================================================================================================#
# section 1_defineHardCodedParam
#
#<section ID="defineHardCodedParam" num="1">
my $paramTag = "$outputPrefix";
my $anno_reg_assignment_flank_size = $tssCluster_flank_size;
my $pseudocount = 1;
my $training_pct_hsh_ref = {
'pos' => $training_pct,
'neg' => $training_pct,
};
my $testing_pct_hsh_ref = {
'pos' => 10,
'neg' => 10,
};
my $trnscpt_density_pct = 0.5;
my $min_density_pct = 0.05;
#<\section>
#====================================================================================================================================================#
#====================================================================================================================================================#
# section 2_defineOutDirPath
#
#<section ID="defineOutDirPath" num="2">
my @mkDirAry;
my $result_dir = "$outDir/$paramTag/"; push @mkDirAry, $result_dir;
system "rm -rf $result_dir" if ($overwrite eq 'yes');
my $result_bed_dir = "$result_dir/bed/"; push @mkDirAry, $result_bed_dir;
my $result_log_dir = "$result_dir/log/"; push @mkDirAry, $result_log_dir;
my $result_glm_dir = "$result_dir/glm/"; push @mkDirAry, $result_glm_dir;
my $result_script_dir = "$result_dir/script/"; push @mkDirAry, $result_script_dir;
my $result_tmp_dir = "$result_dir/tmp/"; push @mkDirAry, $result_tmp_dir;
foreach my $dir (@mkDirAry) {system ("mkdir -pm 755 $dir");}
open $tmplog_fh, ">", "$result_dir/00_screen_log.$curntTimeStamp.log.txt";
&printStartOrFinishMessage("startMessage");#->1964
&logCalledCMDAndScript($ARGVStr, $result_script_dir, $scriptAbsPath);#->1534
&reportAndLogStatus("Running with following parameters", 10, "\n");#->2284
&reportAndLogStatus("tssCluster_flank_size=$tssCluster_flank_size", 10, "\n");#->2284
&reportAndLogStatus("local_bkgd_extend_size=$local_bkgd_extend_size", 10, "\n");#->2284
&reportAndLogStatus("min_gold_num=$min_gold_num", 10, "\n");#->2284
&reportAndLogStatus("training_pct=$training_pct", 10, "\n");#->2284
#<\section>
#====================================================================================================================================================#
#====================================================================================================================================================#
# section 3_getInfoTasks
#
#<section ID="getInfoTasks" num="3">
my ($tabix_bin, $bgzip_bin, $bedtools_bin, $samtools_bin, $paraclu_bin, $cut_sh_path, $bedGraphToBigWig_bin, $bigWigAverageOverBed_bin) = &checkAllExecutable();#->529
my ($build_glm_R, $predict_prob_R, $benchmark_roc_R) = &checkRScriptVersion($Rscript_bin);#->654
my ($mask_tss_region_bed_path, $exon_region_bed_path, $intron_region_bed_path, $background_exon_bed_path, $background_intron_bed_path, $gene_bed_path, $mask_bed_path, $chrom_size_path, $gene_info_path, $tss_pos_bed_path) = &checkRegionBedChromSizeGeneInfoPath($genome);#->701
my ($exclude_chrom_hsh_ref) = &generateExcludeChrHsh($exclude_chrom_list);#->878
my ($chrom_size_hsh_ref) = &getChromSize($chrom_size_path);#->1052
my ($gene_info_hsh_ref) = &readGeneInfo($gene_info_path);#->2139
#<\section>
#====================================================================================================================================================#
#====================================================================================================================================================#
# section 4_backgroundDensityTasks
#
#<section ID="backgroundDensityTasks" num="4">
my ($trnscpt_info_hsh_ref, $trnscpt_bkgd_hsh_ref) = &getTranscriptRegionSize($background_exon_bed_path, $background_intron_bed_path, $gene_info_hsh_ref);#->1306
my ($min_density) = &getBackgroundDensity($trnscpt_info_hsh_ref, $gene_info_hsh_ref, $trnscpt_bkgd_hsh_ref, $ctss_bed_path, $trnscpt_density_pct, $min_density_pct, $bedtools_bin);#->902
&printGeneDensityLog($result_log_dir, $gene_info_hsh_ref, $paramTag);#->1807
#<\section>
#====================================================================================================================================================#
#====================================================================================================================================================#
# section 5_tssClusterPropertiesTasks
#
#<section ID="tssClusterPropertiesTasks" num="5">
my ($tssCluster_summit_bed_path, $tssCluster_summit_anno_flank_bed_path, $tssCluster_info_hsh_ref) = &getTssClusterProperties($tssCluster_bed_path, $ctss_bed_path, $result_bed_dir, $bedtools_bin, $chrom_size_path, $tssCluster_flank_size, $chrom_size_hsh_ref, $anno_reg_assignment_flank_size, $ung_ctss_bed_path, $local_bkgd_extend_size, $mask_tss_region_bed_path, $pseudocount);#->1346
&getGeneTssClusterRanking($tssCluster_summit_bed_path, $tss_pos_bed_path, $tssCluster_info_hsh_ref, $bedtools_bin, $gene_info_hsh_ref, $chrom_size_path);#->1151
&getDistToTSS($tssCluster_summit_bed_path, $tss_pos_bed_path, $tssCluster_info_hsh_ref, $bedtools_bin);#->1117
&getDistToCRE($tssCluster_summit_bed_path, $mask_bed_path, $mask_tss_region_bed_path, $tssCluster_info_hsh_ref, $bedtools_bin);#->1079
#<\section>
#====================================================================================================================================================#
#====================================================================================================================================================#
# section 6_backgroundRelativeExpressionTasks
#
#<section ID="backgroundRelativeExpressionTasks" num="6">
&assignTssClusterToBackgroundRegions($tssCluster_summit_anno_flank_bed_path, $bedtools_bin, $tssCluster_info_hsh_ref, $gene_info_hsh_ref, $exon_region_bed_path, $intron_region_bed_path);#->406
&assignTssClusterToAnnnotationRegions($tssCluster_summit_anno_flank_bed_path, $bedtools_bin, $tssCluster_info_hsh_ref, $gene_info_hsh_ref, $exon_region_bed_path, $intron_region_bed_path, $mask_tss_region_bed_path);#->312
&getBackgroundRelativeExpression($tssCluster_info_hsh_ref, $gene_info_hsh_ref, $min_density, $tssCluster_flank_size, $local_bkgd_extend_size);#->1002
#<\section>
#====================================================================================================================================================#
#====================================================================================================================================================#
# section 7_BenchmarkTasks
#
#<section ID="BenchmarkTasks" num="7">
my ($tssCluster_glm_predictors_path, $glm_model_path) = &performGlmTraining($result_tmp_dir, $training_signal_path, $tssCluster_summit_bed_path, $tssCluster_info_hsh_ref, $bigWigAverageOverBed_bin, $bedtools_bin, $training_pct_hsh_ref, $min_gold_num, $result_bed_dir, $mask_tss_region_bed_path, $mask_bed_path, $result_glm_dir, $exclude_chrom_hsh_ref, $build_glm_R, $genome, $Rscript_bin, $testing_signal_path, $testing_pct_hsh_ref, $usr_glm_model_path);#->1642
&predictLogisticProbability($tssCluster_glm_predictors_path, $glm_model_path, $predict_prob_R, $tssCluster_info_hsh_ref, $result_glm_dir, $Rscript_bin, $default_cutoff);#->1690
&performAnnoRegionBenchmark($testing_signal_path, $tssCluster_info_hsh_ref, $result_glm_dir, $exclude_chrom_hsh_ref, $Rscript_bin, $benchmark_roc_R);#->1559
#<\section>
#====================================================================================================================================================#
#====================================================================================================================================================#
# section 8_count
#
#<section ID="count" num="8">
&printTssClusterLog($result_log_dir, $tssCluster_info_hsh_ref, $paramTag);#->2058
&printFilteredBed($tssCluster_bed_path, $tssCluster_info_hsh_ref, $result_bed_dir, $paramTag);#->1751
#<\section>
#====================================================================================================================================================#
#====================================================================================================================================================#
# section 9_finishingTasks
#
#<section ID="finishingTasks" num="9">
&printOutputFileListAndReadme($ARGVStr, $paramTag, $outDir);#->1844
&printStartOrFinishMessage("finishMessage");#->1964
#<\section>
#====================================================================================================================================================#
#====================================================================================================================================================#
} #Main sections lexical scope ends
#====================================================================================================================================================#
#====================================================================================================================================================#
#List of subroutines by category
#
# checkTools [n=1]:
# checkRScriptVersion
#
# general [n=6]:
# checkRScriptVersion, currentTime, logCalledCMDAndScript
# printStartOrFinishMessage, readParameters, timeStamp
#
# getTextInfo [n=1]:
# readGeneInfo
#
# log [n=1]:
# reportAndLogStatus
#
# output [n=1]:
# printOutputFileListAndReadme
#
# time [n=1]:
# timeStamp
#
# unassigned [n=26]:
# assignTssClusterToAnnnotationRegions, assignTssClusterToBackgroundRegions, buildGlm
# checkAllExecutable, checkGlmModelRDS, checkRegionBedChromSizeGeneInfoPath
# defineGoldenSetForTesting, defineGoldenSetForTraining, generateExcludeChrHsh
# getBackgroundDensity, getBackgroundRelativeExpression, getChromSize
# getDistToCRE, getDistToTSS, getGeneTssClusterRanking
# getGoldenSetBed, getGoldenSetBigwig, getTranscriptRegionSize
# getTssClusterProperties, performAnnoRegionBenchmark, performGlmTraining
# predictLogisticProbability, printFilteredBed, printGeneDensityLog
# printTrainingInput, printTssClusterLog
#
#====================================================================================================================================================#
sub assignTssClusterToAnnnotationRegions {
#....................................................................................................................................................#
# subroutineCategory: unassigned
# dependOnSub: reportAndLogStatus|2284
# appearInSub: >none
# primaryAppearInSection: 6_backgroundRelativeExpressionTasks|235
# secondaryAppearInSection: >none
# input: $bedtools_bin, $exon_region_bed_path, $gene_info_hsh_ref, $intron_region_bed_path, $mask_tss_region_bed_path, $tssCluster_info_hsh_ref, $tssCluster_summit_anno_flank_bed_path
# output:
# toCall: &assignTssClusterToAnnnotationRegions($tssCluster_summit_anno_flank_bed_path, $bedtools_bin, $tssCluster_info_hsh_ref, $gene_info_hsh_ref, $exon_region_bed_path, $intron_region_bed_path, $mask_tss_region_bed_path);
# calledInLine: 239
#....................................................................................................................................................#
my ($tssCluster_summit_anno_flank_bed_path, $bedtools_bin, $tssCluster_info_hsh_ref, $gene_info_hsh_ref, $exon_region_bed_path, $intron_region_bed_path, $mask_tss_region_bed_path) = @_;
&reportAndLogStatus("Assigning background regions to tssClusters by summit location", 10, "\n");#->2284
my $anno_info_hsh_ref = {};
$anno_info_hsh_ref->{'end5'}{'bed_path'} = $mask_tss_region_bed_path;
$anno_info_hsh_ref->{'end5'}{'priority'} = 0;
$anno_info_hsh_ref->{'exon'}{'bed_path'} = $exon_region_bed_path;
$anno_info_hsh_ref->{'exon'}{'priority'} = 1;
$anno_info_hsh_ref->{'intron'}{'bed_path'} = $intron_region_bed_path;
$anno_info_hsh_ref->{'intron'}{'priority'} = 2;
my $check_tssClusterID_hsh_ref = {};
my $anno_orientation_hsh_ref = {
'ss' => '-s',
'as' => '-S',
};
#---[2020/05/03 17:22] intersect annotation in order of ss, annotation priority then as, annotation priority
foreach my $anno_orientation (qw/ss as/) {
my $anno_orientation_opt = $anno_orientation_hsh_ref->{$anno_orientation};
foreach my $anno_type (sort {$anno_info_hsh_ref->{$a}{'priority'} <=> $anno_info_hsh_ref->{$b}{'priority'}} keys %{$anno_info_hsh_ref}) {
&reportAndLogStatus("Intersecting tssClusters with $anno_type in $anno_orientation anno_orientation.", 10, "\n");#->2284
#---[2019/12/22 23:10] to hold the tssClusterID within this round
my $anno_bed_path = $anno_info_hsh_ref->{$anno_type}{'bed_path'};
my $within_region_tssClusterID_hsh_ref = {};
open BEDTOOLSINTERSECT, "$bedtools_bin intersect $anno_orientation_opt -wo -a $tssCluster_summit_anno_flank_bed_path -b $anno_bed_path |";
while (<BEDTOOLSINTERSECT>) {
#chr1 11888638 11888649 chr1_11888638_11888649_+ 25 + chr1 11888514 11888681 ENSG00000011021.17;ENSG00000242349.1 2 + 11
chomp;
my @splt = split /\t/;
my $tssClusterID = $splt[3];
my $geneID_str = $splt[-4];
#---[2019/12/22 23:11] skip if exists in previous round
next if exists $check_tssClusterID_hsh_ref->{$tssClusterID};
foreach my $geneID ((split /;/, $geneID_str)) {
$tssCluster_info_hsh_ref->{$tssClusterID}{'anno_type'} = $anno_type;
$tssCluster_info_hsh_ref->{$tssClusterID}{'anno_orientation'} = $anno_orientation;
$tssCluster_info_hsh_ref->{$tssClusterID}{'anno_geneID'}{$geneID}++;
$within_region_tssClusterID_hsh_ref->{$tssClusterID}++;
}
}
close BEDTOOLSINTERSECT;
my $num_tssCluster_within_region = keys %{$within_region_tssClusterID_hsh_ref};
&reportAndLogStatus("$num_tssCluster_within_region tssClusters assigned to $anno_type in $anno_orientation anno_orientation.", 10, "\n");#->2284
#---[2019/12/22 23:10] bookmark the added tssClusterID
foreach my $tssClusterID (keys %{$within_region_tssClusterID_hsh_ref}) {
$check_tssClusterID_hsh_ref->{$tssClusterID}++;
}
}
}
{
&reportAndLogStatus("adding intergenic annotaton", 10, "\n");#->2284
my $anno_type = 'intergenic';
my $anno_orientation = 'ns';
my $geneID = 'nongenic';
my $num_tssCluster_within_region = 0;
foreach my $tssClusterID (keys %{$tssCluster_info_hsh_ref}) {
if (not exists $tssCluster_info_hsh_ref->{$tssClusterID}{'anno_type'}) {
$num_tssCluster_within_region++;
$tssCluster_info_hsh_ref->{$tssClusterID}{'anno_type'} = $anno_type;
$tssCluster_info_hsh_ref->{$tssClusterID}{'anno_orientation'} = $anno_orientation;
$tssCluster_info_hsh_ref->{$tssClusterID}{'anno_geneID'}{$geneID}++;
}
}
&reportAndLogStatus("$num_tssCluster_within_region tssClusters assigned to $anno_type in $anno_orientation anno_orientation.", 10, "\n");#->2284
}
foreach my $tssClusterID (keys %{$tssCluster_info_hsh_ref}) {
my $anno_type = $tssCluster_info_hsh_ref->{$tssClusterID}{'anno_type'};
my $anno_orientation = $tssCluster_info_hsh_ref->{$tssClusterID}{'anno_orientation'};
$tssCluster_info_hsh_ref->{$tssClusterID}{'anno_region'} = join ":", ($anno_type,$anno_orientation);
}
return ();
}
sub assignTssClusterToBackgroundRegions {
#....................................................................................................................................................#
# subroutineCategory: unassigned
# dependOnSub: reportAndLogStatus|2284
# appearInSub: >none
# primaryAppearInSection: 6_backgroundRelativeExpressionTasks|235
# secondaryAppearInSection: >none
# input: $bedtools_bin, $exon_region_bed_path, $gene_info_hsh_ref, $intron_region_bed_path, $tssCluster_info_hsh_ref, $tssCluster_summit_anno_flank_bed_path
# output:
# toCall: &assignTssClusterToBackgroundRegions($tssCluster_summit_anno_flank_bed_path, $bedtools_bin, $tssCluster_info_hsh_ref, $gene_info_hsh_ref, $exon_region_bed_path, $intron_region_bed_path);
# calledInLine: 238
#....................................................................................................................................................#
my ($tssCluster_summit_anno_flank_bed_path, $bedtools_bin, $tssCluster_info_hsh_ref, $gene_info_hsh_ref, $exon_region_bed_path, $intron_region_bed_path) = @_;
&reportAndLogStatus("Assigning background regions to tssClusters by summit location", 10, "\n");#->2284
my $anno_info_hsh_ref = {};
$anno_info_hsh_ref->{'exon'}{'bed_path'} = $exon_region_bed_path;
$anno_info_hsh_ref->{'exon'}{'priority'} = 1;
$anno_info_hsh_ref->{'intron'}{'bed_path'} = $intron_region_bed_path;
$anno_info_hsh_ref->{'intron'}{'priority'} = 2;
my $check_tssClusterID_hsh_ref = {};
my $bkgd_orientation_hsh_ref = {
'ss' => '-s',
'as' => '-S',
};
#---[2020/05/03 17:22] intersect annotation in order of ss, annotation priority then as, annotation priority
foreach my $bkgd_orientation (qw/ss as/) {
my $bkgd_orientation_opt = $bkgd_orientation_hsh_ref->{$bkgd_orientation};
foreach my $bkgd_type (sort {$anno_info_hsh_ref->{$a}{'priority'} <=> $anno_info_hsh_ref->{$b}{'priority'}} keys %{$anno_info_hsh_ref}) {
&reportAndLogStatus("Intersecting tssClusters with $bkgd_type in $bkgd_orientation bkgd_orientation.", 10, "\n");#->2284
#---[2019/12/22 23:10] to hold the tssClusterID within this round
my $anno_bed_path = $anno_info_hsh_ref->{$bkgd_type}{'bed_path'};
my $within_region_tssClusterID_hsh_ref = {};
open BEDTOOLSINTERSECT, "$bedtools_bin intersect $bkgd_orientation_opt -wo -a $tssCluster_summit_anno_flank_bed_path -b $anno_bed_path |";
while (<BEDTOOLSINTERSECT>) {
#chr1 11888638 11888649 chr1_11888638_11888649_+ 25 + chr1 11888514 11888681 ENSG00000011021.17;ENSG00000242349.1 2 + 11
chomp;
my @splt = split /\t/;
my $tssClusterID = $splt[3];
my $geneID_str = $splt[-4];
#---[2019/12/22 23:11] skip if exists in previous round
next if exists $check_tssClusterID_hsh_ref->{$tssClusterID};
foreach my $geneID ((split /;/, $geneID_str)) {
$tssCluster_info_hsh_ref->{$tssClusterID}{'bkgd_type'} = $bkgd_type;
$tssCluster_info_hsh_ref->{$tssClusterID}{'bkgd_orientation'} = $bkgd_orientation;
$tssCluster_info_hsh_ref->{$tssClusterID}{'bkgd_geneID'}{$geneID}++;
$within_region_tssClusterID_hsh_ref->{$tssClusterID}++;
}
}
close BEDTOOLSINTERSECT;
my $num_tssCluster_within_region = keys %{$within_region_tssClusterID_hsh_ref};
&reportAndLogStatus("$num_tssCluster_within_region tssClusters assigned to $bkgd_type in $bkgd_orientation bkgd_orientation.", 10, "\n");#->2284
#---[2019/12/22 23:10] bookmark the added tssClusterID
foreach my $tssClusterID (keys %{$within_region_tssClusterID_hsh_ref}) {
$check_tssClusterID_hsh_ref->{$tssClusterID}++;
}
}
}
{
&reportAndLogStatus("adding intergenic annotaton", 10, "\n");#->2284
my $bkgd_type = 'intergenic';
my $bkgd_orientation = 'ns';
my $geneID = 'nongenic';
my $num_tssCluster_within_region = 0;
foreach my $tssClusterID (keys %{$tssCluster_info_hsh_ref}) {
if (not exists $tssCluster_info_hsh_ref->{$tssClusterID}{'bkgd_type'}) {
$num_tssCluster_within_region++;
$tssCluster_info_hsh_ref->{$tssClusterID}{'bkgd_type'} = $bkgd_type;
$tssCluster_info_hsh_ref->{$tssClusterID}{'bkgd_orientation'} = $bkgd_orientation;
$tssCluster_info_hsh_ref->{$tssClusterID}{'bkgd_geneID'}{$geneID}++;
}
}
&reportAndLogStatus("$num_tssCluster_within_region tssClusters assigned to $bkgd_type in $bkgd_orientation bkgd_orientation.", 10, "\n");#->2284
}
foreach my $tssClusterID (keys %{$tssCluster_info_hsh_ref}) {
my $bkgd_type = $tssCluster_info_hsh_ref->{$tssClusterID}{'bkgd_type'};
my $bkgd_orientation = $tssCluster_info_hsh_ref->{$tssClusterID}{'bkgd_orientation'};
$tssCluster_info_hsh_ref->{$tssClusterID}{'bkgd_region'} = join ":", ($bkgd_type,$bkgd_orientation);
}
return ();
}
sub buildGlm {
#....................................................................................................................................................#
# subroutineCategory: unassigned
# dependOnSub: reportAndLogStatus|2284
# appearInSub: performGlmTraining|1642
# primaryAppearInSection: >none
# secondaryAppearInSection: 7_BenchmarkTasks|245
# input: $Rscript_bin, $build_glm_R, $result_glm_dir, $tssCluster_glm_predictors_path
# output: $glm_model_path
# toCall: my ($glm_model_path) = &buildGlm($tssCluster_glm_predictors_path, $build_glm_R, $result_glm_dir, $Rscript_bin);
# calledInLine: 1680
#....................................................................................................................................................#
my ($tssCluster_glm_predictors_path, $build_glm_R, $result_glm_dir, $Rscript_bin) = @_;
my $build_glm_dir = "$result_glm_dir/build_glm";
#system ("rm -Rf $build_glm_dir");
system ("mkdir -pm 755 $build_glm_dir");
my $glm_model_path = "$build_glm_dir/combined.predictors.glm.model.RDS";
my $glm_model_stdout_path = "$build_glm_dir/00_run.stdout.log.txt";
my $glm_model_stderr_path = "$build_glm_dir/00_run.stderr.log.txt";
&reportAndLogStatus("building glm_model_path", 10, "\n");#->2284
system "$Rscript_bin $build_glm_R $tssCluster_glm_predictors_path $build_glm_dir 1>$glm_model_stdout_path 2>$glm_model_stderr_path";
if (-s $glm_model_path) {
&reportAndLogStatus("glm_model_path is generated", 10, "\n");#->2284
} else {
die "glm_model_path is not generated. Quitting\n";
}
return ($glm_model_path);
}
sub checkAllExecutable {
#....................................................................................................................................................#
# subroutineCategory: unassigned
# dependOnSub: reportAndLogStatus|2284
# appearInSub: >none
# primaryAppearInSection: 3_getInfoTasks|201
# secondaryAppearInSection: >none
# input: none
# output: $bedGraphToBigWig_bin, $bedtools_bin, $bgzip_bin, $bigWigAverageOverBed_bin, $cut_sh_path, $paraclu_bin, $samtools_bin, $tabix_bin
# toCall: my ($tabix_bin, $bgzip_bin, $bedtools_bin, $samtools_bin, $paraclu_bin, $cut_sh_path, $bedGraphToBigWig_bin, $bigWigAverageOverBed_bin) = &checkAllExecutable();
# calledInLine: 204
#....................................................................................................................................................#
my $dirPath = dirname(rel2abs($0));
my $tabix_bin = "$dirPath/../resources/bin/tabix/tabix";
my $bgzip_bin = "$dirPath/../resources/bin/bgzip/bgzip";
my $bedtools_bin = "$dirPath/../resources/bin/bedtools/bedtools";
my $samtools_bin = "$dirPath/../resources/bin/samtools/samtools";
my $paraclu_bin = "$dirPath/../resources/bin/paraclu/paraclu";
my $cut_sh_path = "$dirPath/../resources/bin/paraclu/paraclu-cut.sh";
my $bedGraphToBigWig_bin = "$dirPath/../resources/bin/bedGraphToBigWig/bedGraphToBigWig";
my $bigWigAverageOverBed_bin = "$dirPath/../resources/bin/bigWigAverageOverBed/bigWigAverageOverBed";
&reportAndLogStatus("Checking all SCAFE executables", 10, "\n");#->2284
{
my $stdOut = `$tabix_bin --version 2>&1`;
if ($stdOut =~ m/tabix \(htslib\) (\S+)/) {
&reportAndLogStatus("Checking: tabix version: $1", 0, "\n");#->2284
} else {
die "tabix is not installed properly. Quitting.\n";
}
}
{
my $stdOut = `$bgzip_bin --version 2>&1`;
if ($stdOut =~ m/bgzip \(htslib\) (\S+)/) {
&reportAndLogStatus("Checking: bgzip version: $1", 0, "\n");#->2284
} else {
die "bgzip is not installed properly. Quitting.\n";
}
}
{
my $stdOut = `$bedtools_bin --version 2>&1`;
if ($stdOut =~ m/bedtools v(\S+)/) {
&reportAndLogStatus("Checking: bedtools version: $1", 0, "\n");#->2284
} else {
die "bedtools is not installed properly. Quitting.\n";
}
}
{
my $stdOut = `$samtools_bin 2>&1`;
if ($stdOut =~ m/\s+(Version: \S+)\s+/) {
&reportAndLogStatus("Checking: samtools version: $1", 0, "\n");#->2284
} else {
die "samtools is not installed properly. Quitting.\n";
}
}
{
my $stdOut = `$paraclu_bin 2>&1`;
if ($stdOut =~ m/paraclu: I need a minValue and a fileName/) {
&reportAndLogStatus("Checking: paraclu found.", 0, "\n");#->2284
} else {
die "paraclu is not installed properly. Quitting.\n";
}
}
{
my $stdOut = `$cut_sh_path 55C7128A 2>&1`;
if ($stdOut =~ m/awk:/) {
&reportAndLogStatus("Checking: paraclu-cut found.", 0, "\n");#->2284
} else {
die "paraclu-cut is not installed properly. Quitting.\n";
}
}
{
my $stdOut = `$bedGraphToBigWig_bin 2>&1`;
if ($stdOut =~ m/bedGraphToBigWig v (\S+)/) {
&reportAndLogStatus("Checking: bedGraphToBigWig version: $1", 0, "\n");#->2284
} else {
die "bedGraphToBigWig not installed properly. Quitting.\n";
}
}
{
my $stdOut = `$bigWigAverageOverBed_bin 2>&1`;
if ($stdOut =~ m/bigWigAverageOverBed v(\S+)/) {
&reportAndLogStatus("Checking: bigWigAverageOverBed version: $1", 0, "\n");#->2284
} else {
die "bigWigAverageOverBed is not installed properly. Quitting.\n";
}
}
return ($tabix_bin, $bgzip_bin, $bedtools_bin, $samtools_bin, $paraclu_bin, $cut_sh_path, $bedGraphToBigWig_bin, $bigWigAverageOverBed_bin);
}
sub checkGlmModelRDS {
#....................................................................................................................................................#
# subroutineCategory: unassigned
# dependOnSub: reportAndLogStatus|2284
# appearInSub: performGlmTraining|1642
# primaryAppearInSection: >none
# secondaryAppearInSection: 7_BenchmarkTasks|245
# input: $genome
# output: $glm_model_path
# toCall: my ($glm_model_path) = &checkGlmModelRDS($genome);
# calledInLine: 1684
#....................................................................................................................................................#
my ($genome) = @_;
my $dirPath = dirname(rel2abs($0));
my $glm_model_path = "$dirPath/../resources/genome/$genome/glm/combined.predictors.glm.model.RDS";
if (-s $glm_model_path) {
&reportAndLogStatus("Checking: glm_model_path found", 0, "\n");#->2284
} else {
die "glm_model_path not found. Quitting.\n";
}
return ($glm_model_path);
}
sub checkRScriptVersion {
#....................................................................................................................................................#
# subroutineCategory: general, checkTools
# dependOnSub: reportAndLogStatus|2284
# appearInSub: >none
# primaryAppearInSection: 3_getInfoTasks|201
# secondaryAppearInSection: >none
# input: $Rscript_bin
# output: $benchmark_roc_R, $build_glm_R, $predict_prob_R
# toCall: my ($build_glm_R, $predict_prob_R, $benchmark_roc_R) = &checkRScriptVersion($Rscript_bin);
# calledInLine: 205
#....................................................................................................................................................#
my ($Rscript_bin) = @_;
my $dirPath = dirname(rel2abs($0));
my $build_glm_R = "$dirPath/../resources/R/build_glm.R";
my $predict_prob_R = "$dirPath/../resources/R/predict_prob.R";
my $benchmark_roc_R = "$dirPath/../resources/R/benchmark_roc.R";
my $stdOut = `$Rscript_bin --version 2>&1`;
if ($stdOut =~ m/R scripting front-end version (\S+)/) {
&reportAndLogStatus("Checking: Rscript version: $1", 0, "\n");#->2284
} else {
die "Rscript is not installed properly. Quitting.\n";
}
if (-s $build_glm_R) {
&reportAndLogStatus("build_glm_R found.", 0, "\n");#->2284
} else {
die "Rscript build_glm_R is not found. Quitting.\n";
}
if (-s $predict_prob_R) {
&reportAndLogStatus("predict_prob_R found.", 0, "\n");#->2284
} else {
die "Rscript predict_prob_R is not found. Quitting.\n";
}
if (-s $benchmark_roc_R) {
&reportAndLogStatus("benchmark_roc_R found.", 0, "\n");#->2284
} else {
die "Rscript benchmark_roc_R is not found. Quitting.\n";
}
return($build_glm_R, $predict_prob_R, $benchmark_roc_R);
}
sub checkRegionBedChromSizeGeneInfoPath {
#....................................................................................................................................................#
# subroutineCategory: unassigned
# dependOnSub: >none
# appearInSub: >none
# primaryAppearInSection: 3_getInfoTasks|201
# secondaryAppearInSection: >none
# input: $genome
# output: $background_exon_bed_path, $background_intron_bed_path, $chrom_size_path, $exon_region_bed_path, $gene_bed_path, $gene_info_path, $intron_region_bed_path, $mask_bed_path, $mask_tss_region_bed_path, $tss_pos_bed_path
# toCall: my ($mask_tss_region_bed_path, $exon_region_bed_path, $intron_region_bed_path, $background_exon_bed_path, $background_intron_bed_path, $gene_bed_path, $mask_bed_path, $chrom_size_path, $gene_info_path, $tss_pos_bed_path) = &checkRegionBedChromSizeGeneInfoPath($genome);
# calledInLine: 206
#....................................................................................................................................................#
my ($genome) = @_;
my $dirPath = dirname(rel2abs($0));
my $mask_tss_region_bed_path = "$dirPath/../resources/genome/$genome/bed/TSS_flank_250.bed.gz";
my $exon_region_bed_path = "$dirPath/../resources/genome/$genome/bed/exon.bed.gz";
my $intron_region_bed_path = "$dirPath/../resources/genome/$genome/bed/intron.bed.gz";
my $gene_bed_path = "$dirPath/../resources/genome/$genome/bed/gene.bed.gz";
my $background_exon_bed_path = "$dirPath/../resources/genome/$genome/bed/exon.trnscpt_based.subtract_TSS_flank_250.bed.gz";
my $background_intron_bed_path = "$dirPath/../resources/genome/$genome/bed/intron.trnscpt_based.subtract_TSS_flank_250.bed.gz";
my $mask_bed_path = "$dirPath/../resources/genome/$genome/bed/mask.bed.gz";
my $gene_info_path = "$dirPath/../resources/genome/$genome/tsv//gene.info.tsv";
my $chrom_size_path = "$dirPath/../resources/genome/$genome/tsv/chrom.sizes.tsv";
my $tss_pos_bed_path = "$dirPath/../resources/genome/$genome/bed/TSS_flank_0.bed.gz";
die "genome $genome does not have mask_tss_region_bed_path. Please rerun prep_genome step\n" if not -s $mask_tss_region_bed_path;
die "genome $genome does not have exon_region_bed_path. Please rerun prep_genome step\n" if not -s $exon_region_bed_path;
die "genome $genome does not have intron_region_bed_path. Please rerun prep_genome step\n" if not -s $intron_region_bed_path;
die "genome $genome does not have background_exon_bed_path. Please rerun prep_genome step\n" if not -s $background_exon_bed_path;
die "genome $genome does not have background_intron_bed_path. Please rerun prep_genome step\n" if not -s $background_intron_bed_path;
die "genome $genome does not have mask_bed_path. Please rerun prep_genome step\n" if not -s $mask_bed_path;
die "genome $genome does not have gene_info_path. Please rerun prep_genome step\n" if not -s $gene_info_path;
die "genome $genome does not have chrom_size_path. Please rerun prep_genome step\n" if not -s $chrom_size_path;
die "genome $genome does not have tss_pos_bed_path. Please rerun prep_genome step\n" if not -s $tss_pos_bed_path;
die "genome $genome does not have gene_bed_path. Please rerun prep_genome step\n" if not -s $gene_bed_path;
return ($mask_tss_region_bed_path, $exon_region_bed_path, $intron_region_bed_path, $background_exon_bed_path, $background_intron_bed_path, $gene_bed_path, $mask_bed_path, $chrom_size_path, $gene_info_path, $tss_pos_bed_path);
}
sub currentTime {
#....................................................................................................................................................#
# subroutineCategory: general
# dependOnSub: >none
# appearInSub: printStartOrFinishMessage|1964, reportAndLogStatus|2284
# primaryAppearInSection: >none
# secondaryAppearInSection: 2_defineOutDirPath|172, 9_finishingTasks|264
# input: none
# output: $runTime
# toCall: my ($runTime) = ¤tTime();
# calledInLine: 130, 1980, 1984, 1989, 1993, 2300, 2301
#....................................................................................................................................................#
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
my $runTime = sprintf "%04d-%02d-%02d %02d:%02d", $year+1900, $mon+1,$mday,$hour,$min;
return $runTime;
}
sub defineGoldenSetForTesting {
#....................................................................................................................................................#
# subroutineCategory: unassigned
# dependOnSub: getGoldenSetBed|1202, getGoldenSetBigwig|1249, reportAndLogStatus|2284
# appearInSub: performGlmTraining|1642
# primaryAppearInSection: >none
# secondaryAppearInSection: 7_BenchmarkTasks|245
# input: $bedtools_bin, $bigWigAverageOverBed_bin, $exclude_chrom_hsh_ref, $mask_bed_path, $mask_tss_region_bed_path, $min_gold_num, $result_tmp_dir, $testing_pct_hsh_ref, $testing_signal_path, $training_pct_hsh_ref, $tssCluster_info_hsh_ref, $tssCluster_summit_bed_path
# output:
# toCall: &defineGoldenSetForTesting($tssCluster_summit_bed_path, $tssCluster_info_hsh_ref, $training_pct_hsh_ref, $mask_tss_region_bed_path, $mask_bed_path, $testing_signal_path, $result_tmp_dir, $bedtools_bin, $bigWigAverageOverBed_bin, $testing_pct_hsh_ref, $min_gold_num, $exclude_chrom_hsh_ref);
# calledInLine: 1664
#....................................................................................................................................................#
my ($tssCluster_summit_bed_path, $tssCluster_info_hsh_ref, $training_pct_hsh_ref, $mask_tss_region_bed_path, $mask_bed_path, $testing_signal_path, $result_tmp_dir, $bedtools_bin, $bigWigAverageOverBed_bin, $testing_pct_hsh_ref, $min_gold_num, $exclude_chrom_hsh_ref) = @_;
if (-s $testing_signal_path) {
my $train_test = 'test';
if ($testing_signal_path =~ m/bw$/ or $testing_signal_path =~ m/bigwig$/) {
my $num_tssCluster = keys %{$tssCluster_info_hsh_ref};
my $pos_gold_target_num = int($num_tssCluster*($testing_pct_hsh_ref->{'pos'}/100));
my $neg_gold_target_num = int($num_tssCluster*($testing_pct_hsh_ref->{'neg'}/100));
$pos_gold_target_num = $min_gold_num if $min_gold_num > $pos_gold_target_num;
$neg_gold_target_num = $min_gold_num if $min_gold_num > $neg_gold_target_num;
my $benchmark_signal_score_path = "$result_tmp_dir/benchmark_signal_score.txt";
my $bw_signal_path = $testing_signal_path;
my $tssCluster_intersect_reg_bed_path = $tssCluster_summit_bed_path;
&getGoldenSetBigwig($bigWigAverageOverBed_bin, $bw_signal_path, $tssCluster_intersect_reg_bed_path, $benchmark_signal_score_path, $tssCluster_info_hsh_ref, $train_test, $exclude_chrom_hsh_ref, $pos_gold_target_num, $neg_gold_target_num);#->1249
} elsif ($testing_signal_path =~ m/bed$/ or $testing_signal_path =~ m/bed.gz$/) {
my $max_dist_to_bed_region = 0;
my $min_dist_to_bed_region = 100;
my $bed_signal_path = $testing_signal_path;
&getGoldenSetBed($bed_signal_path, $exclude_chrom_hsh_ref, $tssCluster_info_hsh_ref, $max_dist_to_bed_region, $min_dist_to_bed_region, $tssCluster_summit_bed_path, $train_test, $bedtools_bin);#->1202
} else {
die "testing_signal_path must be bed or bigwig\n";
}
} else {
&reportAndLogStatus("defining goldenset by annotated TSS and CRE for testing", 10, "\n");#->2284
my $max_dist_to_anno_tss = 250;
my $min_dist_to_anno_cre = 10;
my $num_gold_pos = 0;
my $num_gold_neg = 0;
foreach my $tssClusterID (sort {$tssCluster_info_hsh_ref->{$a}{'dist_to_anno_tss'} <=> $tssCluster_info_hsh_ref->{$b}{'dist_to_anno_tss'}} keys %{$tssCluster_info_hsh_ref}) {
$tssCluster_info_hsh_ref->{$tssClusterID}{'test_in_gold_pos'} = 'no';
my $dist_to_anno_tss = $tssCluster_info_hsh_ref->{$tssClusterID}{'dist_to_anno_tss'};
my $max_gene_rank = $tssCluster_info_hsh_ref->{$tssClusterID}{'max_gene_rank'};
next if $dist_to_anno_tss == -1;
if ($dist_to_anno_tss <= $max_dist_to_anno_tss and $max_gene_rank == 1) {
#---[2020/09/29 4:31] take only the cluster that is max_gene_rank is 1
$tssCluster_info_hsh_ref->{$tssClusterID}{'test_binary'} = 1;
$tssCluster_info_hsh_ref->{$tssClusterID}{'test_in_gold_value'} = $dist_to_anno_tss;
$num_gold_pos++;
}
}
foreach my $tssClusterID (sort {$tssCluster_info_hsh_ref->{$b}{'dist_to_anno_cre'} <=> $tssCluster_info_hsh_ref->{$a}{'dist_to_anno_cre'}} keys %{$tssCluster_info_hsh_ref}) {
$tssCluster_info_hsh_ref->{$tssClusterID}{'test_in_gold_neg'} = 'no';
my $dist_to_anno_cre = $tssCluster_info_hsh_ref->{$tssClusterID}{'dist_to_anno_cre'};
next if $dist_to_anno_cre == -1;
#last if $num_tssCluster_in_golden_negative_region >= $neg_gold_target_num;
if ($dist_to_anno_cre >= $min_dist_to_anno_cre) {
$tssCluster_info_hsh_ref->{$tssClusterID}{'test_binary'} = 0;
$tssCluster_info_hsh_ref->{$tssClusterID}{'test_in_gold_value'} = $dist_to_anno_cre;
$num_gold_neg++;
}
}
&reportAndLogStatus("num_gold_pos for testing =$num_gold_pos", 10, "\n");#->2284
&reportAndLogStatus("num_gold_neg for testing =$num_gold_neg", 10, "\n");#->2618 #->2284
}
return ();
}
sub defineGoldenSetForTraining {
#....................................................................................................................................................#
# subroutineCategory: unassigned
# dependOnSub: getGoldenSetBed|1202, getGoldenSetBigwig|1249
# appearInSub: performGlmTraining|1642
# primaryAppearInSection: >none
# secondaryAppearInSection: 7_BenchmarkTasks|245
# input: $bedtools_bin, $bigWigAverageOverBed_bin, $exclude_chrom_hsh_ref, $min_gold_num, $result_tmp_dir, $training_pct_hsh_ref, $training_signal_path, $tssCluster_info_hsh_ref, $tssCluster_summit_bed_path
# output:
# toCall: &defineGoldenSetForTraining($result_tmp_dir, $training_signal_path, $tssCluster_summit_bed_path, $tssCluster_info_hsh_ref, $bigWigAverageOverBed_bin, $bedtools_bin, $training_pct_hsh_ref, $min_gold_num, $exclude_chrom_hsh_ref);
# calledInLine: 1667
#....................................................................................................................................................#
my ($result_tmp_dir, $training_signal_path, $tssCluster_summit_bed_path, $tssCluster_info_hsh_ref, $bigWigAverageOverBed_bin, $bedtools_bin, $training_pct_hsh_ref, $min_gold_num, $exclude_chrom_hsh_ref) = @_;
my $train_test = 'train';
if ($training_signal_path =~ m/bw$/ or $training_signal_path =~ m/bigwig$/) {
my $num_tssCluster = keys %{$tssCluster_info_hsh_ref};
my $pos_gold_target_num = int($num_tssCluster*($training_pct_hsh_ref->{'pos'}/100));
my $neg_gold_target_num = int($num_tssCluster*($training_pct_hsh_ref->{'neg'}/100));
$pos_gold_target_num = $min_gold_num if $min_gold_num > $pos_gold_target_num;
$neg_gold_target_num = $min_gold_num if $min_gold_num > $neg_gold_target_num;
my $benchmark_signal_score_path = "$result_tmp_dir/benchmark_signal_score.txt";
my $bw_signal_path = $training_signal_path;
&getGoldenSetBigwig($bigWigAverageOverBed_bin, $bw_signal_path, $tssCluster_summit_bed_path, $benchmark_signal_score_path, $tssCluster_info_hsh_ref, $train_test, $exclude_chrom_hsh_ref, $pos_gold_target_num, $neg_gold_target_num);#->1249
} elsif ($training_signal_path =~ m/bed$/ or $training_signal_path =~ m/bed.gz$/) {
my $max_dist_to_bed_region = 0;
my $min_dist_to_bed_region = 100;
my $bed_signal_path = $training_signal_path;
&getGoldenSetBed($bed_signal_path, $exclude_chrom_hsh_ref, $tssCluster_info_hsh_ref, $max_dist_to_bed_region, $min_dist_to_bed_region, $tssCluster_summit_bed_path, $train_test, $bedtools_bin);#->1202
} else {
die "training_signal_path must be bed or bigwig\n";
}
return ();
}
sub generateExcludeChrHsh {
#....................................................................................................................................................#
# subroutineCategory: unassigned
# dependOnSub: reportAndLogStatus|2284
# appearInSub: >none
# primaryAppearInSection: 3_getInfoTasks|201
# secondaryAppearInSection: >none
# input: $exclude_chrom_list
# output: $exclude_chrom_hsh_ref
# toCall: my ($exclude_chrom_hsh_ref) = &generateExcludeChrHsh($exclude_chrom_list);
# calledInLine: 207
#....................................................................................................................................................#
my ($exclude_chrom_list) = @_;
my $exclude_chrom_hsh_ref = {};
if ($exclude_chrom_list ne 'null') {
foreach my $exclu_chrom (split /\,/, $exclude_chrom_list) {
&reportAndLogStatus("$exclu_chrom will be excluded from training and testing", 10, "\n");#->2284
$exclude_chrom_hsh_ref->{$exclu_chrom} = 1;
}
}
return ($exclude_chrom_hsh_ref);
}
sub getBackgroundDensity {
#....................................................................................................................................................#
# subroutineCategory: unassigned
# dependOnSub: reportAndLogStatus|2284
# appearInSub: >none
# primaryAppearInSection: 4_backgroundDensityTasks|214
# secondaryAppearInSection: >none
# input: $bedtools_bin, $ctss_bed_path, $gene_info_hsh_ref, $min_density_pct, $trnscpt_bkgd_hsh_ref, $trnscpt_density_pct, $trnscpt_info_hsh_ref
# output: $min_density
# toCall: my ($min_density) = &getBackgroundDensity($trnscpt_info_hsh_ref, $gene_info_hsh_ref, $trnscpt_bkgd_hsh_ref, $ctss_bed_path, $trnscpt_density_pct, $min_density_pct, $bedtools_bin);
# calledInLine: 218
#....................................................................................................................................................#
my ($trnscpt_info_hsh_ref, $gene_info_hsh_ref, $trnscpt_bkgd_hsh_ref, $ctss_bed_path, $trnscpt_density_pct, $min_density_pct, $bedtools_bin) = @_;
my @all_non_zero_density_array = ();
foreach my $exon_intron (keys %{$trnscpt_bkgd_hsh_ref}) {
foreach my $trnscptID (keys %{$trnscpt_info_hsh_ref}) {
foreach my $bkgd_orientation (qw/ss as/) {
$trnscpt_info_hsh_ref->{$trnscptID}{'count'}{$exon_intron}{$bkgd_orientation} = 1;
}
}
&reportAndLogStatus("getting $exon_intron background count", 10, "\n");#->2284
my $exon_intron_bed_path = $trnscpt_bkgd_hsh_ref->{$exon_intron} ;
open BEDTOOLS, "$bedtools_bin intersect -wo -sorted -a $exon_intron_bed_path -b $ctss_bed_path | cut -f 4,6,11,12 |";
while (<BEDTOOLS>) {
chomp;
#chr10 76184022 76284872 ENSG00000156110.14_4|ENST00000672920.1_1 1 + chr10 76211833 76211834 1 1 + 1
#chr10 76184022 76284872 ENSG00000156110.14_4|ENST00000672920.1_1 1 + chr10 76213630 76213631 1 1 + 1
#chr10 76184022 76284872 ENSG00000156110.14_4|ENST00000672920.1_1 1 + chr10 76215039 76215040 1 1 + 1
#chr10 76184022 76284872 ENSG00000156110.14_4|ENST00000672920.1_1 1 + chr10 76215901 76215902 1 1 + 1
my ($IDstr, $gene_strand, $count, $ctss_strand) = split /\t/;
my $bkgd_orientation = 'ss';
$bkgd_orientation = 'as' if $gene_strand ne $ctss_strand;
my ($geneID, $trnscptID_str) = split /\|/, $IDstr;
foreach my $trnscptID (split /;/, $trnscptID_str) {
$trnscpt_info_hsh_ref->{$trnscptID}{'count'}{$exon_intron}{$bkgd_orientation} += $count;
}
}
close BEDTOOLS;
foreach my $trnscptID (keys %{$trnscpt_info_hsh_ref}) {
if (exists $trnscpt_info_hsh_ref->{$trnscptID}{'size'}{$exon_intron}) {
my $size = $trnscpt_info_hsh_ref->{$trnscptID}{'size'}{$exon_intron};
foreach my $bkgd_orientation (qw/ss as/) {
my $count = $trnscpt_info_hsh_ref->{$trnscptID}{'count'}{$exon_intron}{$bkgd_orientation};
$trnscpt_info_hsh_ref->{$trnscptID}{'density'}{$exon_intron}{$bkgd_orientation} = sprintf "%.2e", $count/$size;
}
}
}
foreach my $geneID (keys %{$gene_info_hsh_ref}) {
foreach my $bkgd_orientation (qw/ss as/) {
my @density_ary = ();
foreach my $trnscptID (keys %{$gene_info_hsh_ref->{$geneID}{'trnscptID'}}) {
if (exists $trnscpt_info_hsh_ref->{$trnscptID}{'density'}{$exon_intron}) {
my $density = $trnscpt_info_hsh_ref->{$trnscptID}{'density'}{$exon_intron}{$bkgd_orientation};