-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathscafe.tool.sc.subsample_ctss
846 lines (688 loc) · 32.7 KB
/
scafe.tool.sc.subsample_ctss
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
#!/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 File::Spec::Functions qw(rel2abs abs2rel);
use Time::HiRes qw( time );
use Getopt::Long 'HelpMessage';
use threads ('stack_size' => 64*4096);
use threads::shared;
use List::Util qw (sum shuffle min max);
use Cwd 'abs_path';
#<\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.sc.subsample_ctss <---
<--- tool, single-cell mode, subsample ctss --->
Description:
This tool subsample a ctss bed file and maintains the cellbarcode and UMI information
Usage:
scafe.tool.sc.subsample_ctss [options] --UMI_CB_ctss_bed_path --subsample_num --outputPrefix --outDir
--UMI_CB_ctss_bed_path <required> [string] ctss file for subsampling, one line one cellbarcode-UMI combination,
*UMI_CB.ctss.bed.gz from scafe.tool.sc.bam_to_ctss,
4th column cellbarcode-UMI and 5th column is number of unencoded-G
--subsample_num <required> [integer] number of UMI to be subsampled
--outputPrefix <required> [string] prefix for the output files
--outDir <required> [string] directory for the output files
--overwrite (optional) [yes/no] erase outDir/outputPrefix before running (default=no)
Dependencies:
bedtools
tabix
bgzip
For demo, cd to SCAFE dir and run,
scafe.tool.sc.subsample_ctss \
--overwrite=yes \
--UMI_CB_ctss_bed_path=./demo/output/sc.solo/bam_to_ctss/demo/bed/demo.UMI_CB.ctss.bed.gz \
--subsample_num=100000 \
--outputPrefix=demo \
--outDir=./demo/output/sc.subsample/subsample_ctss/
=head1 VERSION
v0.9.0-beta [March 20, 2021]
-Initial pre-release
v1.0.0 [June 6, 2022]
-output bgzip bed and indexed by tabix
=cut
#<\doc>
#====================================================================================================================================================#
#====================================================================================================================================================#
#<lastCmdCalled>
#
# notCalledBefore
#
# notCalledBefore
#
#<\lastCmdCalled>
#====================================================================================================================================================#
#====================================================================================================================================================#
#<global>
my $scriptDirPath = dirname(rel2abs($0));
my $scriptAbsPath = abs_path($0);
my ($curntTimeStamp) = &timeStamp();#->718
my $ARGVStr = join "\n", (¤tTime(), $scriptAbsPath, @ARGV);#->286
my $globalReadmeHsh_ref = {};
our $tmplog_fh;
#<\global>
#====================================================================================================================================================#
#====================================================================================================================================================#
{ #Main sections lexical scope starts
#====================================================================================================================================================#
#====================================================================================================================================================#
# section 0_startingTasks
#
#<section ID="startingTasks" num="0">
my ($UMI_CB_ctss_bed_path, $subsample_num, $outputPrefix, $outDir, $overwrite) = &readParameters();#->521
#<\section>
#====================================================================================================================================================#
#====================================================================================================================================================#
# section 1_defineHardCodedParam
#
#<section ID="defineHardCodedParam" num="1">
my $paramTag = "$outputPrefix";
my $max_thread = 5; #---[2022/06/06 15:34] num of thread for bgzip tabix
#<\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_script_dir = "$result_dir/script/"; push @mkDirAry, $result_script_dir;
foreach my $dir (@mkDirAry) {system ("mkdir -pm 777 $dir");}
open $tmplog_fh, ">", "$result_dir/00_screen_log.$curntTimeStamp.log.txt";
&logCalledCMDAndScript($ARGVStr, $result_script_dir, $scriptAbsPath);#->347
&printStartOrFinishMessage("startMessage");#->487
#<\section>
#====================================================================================================================================================#
#====================================================================================================================================================#
# section 3_subsample
#
#<section ID="subsample" num="3">
my ($tabix_bin, $bgzip_bin, $bedtools_bin, $samtools_bin, $paraclu_bin, $cut_sh_path, $bedGraphToBigWig_bin, $bigWigAverageOverBed_bin) = &checkAllExecutable();#->186
my ($target_fraction) = &getTargetFraction($UMI_CB_ctss_bed_path, $subsample_num);#->304
&subsampleCTSS($UMI_CB_ctss_bed_path, $result_bed_dir, $bedtools_bin, $target_fraction, $tabix_bin, $bgzip_bin, $max_thread, $outputPrefix);#->587
#<\section>
#====================================================================================================================================================#
#====================================================================================================================================================#
# section 4_convert
#
#<section ID="convert" num="4">
#<\section>
#====================================================================================================================================================#
#====================================================================================================================================================#
# section 5_finishingTasks
#
#<section ID="finishingTasks" num="5">
&printOutputFileListAndReadme($ARGVStr, $paramTag, $outDir);#->372
&printStartOrFinishMessage("finishMessage");#->487
#<\section>
#====================================================================================================================================================#
#====================================================================================================================================================#
} #Main sections lexical scope ends
#====================================================================================================================================================#
#====================================================================================================================================================#
#List of subroutines by category
#
# general [n=5]:
# currentTime, logCalledCMDAndScript, printStartOrFinishMessage
# readParameters, timeStamp
#
# log [n=1]:
# reportAndLogStatus
#
# output [n=1]:
# printOutputFileListAndReadme
#
# time [n=1]:
# timeStamp
#
# unassigned [n=3]:
# checkAllExecutable, getTargetFraction, subsampleCTSS
#
#====================================================================================================================================================#
sub checkAllExecutable {
#....................................................................................................................................................#
# subroutineCategory: unassigned
# dependOnSub: reportAndLogStatus|565
# appearInSub: >none
# primaryAppearInSection: 3_subsample|135
# 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: 138
#....................................................................................................................................................#
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");#->565
{
my $stdOut = `$tabix_bin --version 2>&1`;
if ($stdOut =~ m/tabix \(htslib\) (\S+)/) {
&reportAndLogStatus("Checking: tabix version: $1", 0, "\n");#->565
} 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");#->565
} 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");#->565
} 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");#->565
} 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");#->565
} 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");#->565
} 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");#->565
} 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");#->565
} 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 currentTime {
#....................................................................................................................................................#
# subroutineCategory: general
# dependOnSub: >none
# appearInSub: printStartOrFinishMessage|487, reportAndLogStatus|565
# primaryAppearInSection: >none
# secondaryAppearInSection: 2_defineOutDirPath|118, 5_finishingTasks|152
# input: none
# output: $runTime
# toCall: my ($runTime) = ¤tTime();
# calledInLine: 91, 503, 507, 512, 516, 581, 582
#....................................................................................................................................................#
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 getTargetFraction {
#....................................................................................................................................................#
# subroutineCategory: unassigned
# dependOnSub: reportAndLogStatus|565
# appearInSub: >none
# primaryAppearInSection: 3_subsample|135
# secondaryAppearInSection: >none
# input: $UMI_CB_ctss_bed_path, $subsample_num
# output: $target_fraction
# toCall: my ($target_fraction) = &getTargetFraction($UMI_CB_ctss_bed_path, $subsample_num);
# calledInLine: 139
#....................................................................................................................................................#
my ($UMI_CB_ctss_bed_path, $subsample_num) = @_;
&reportAndLogStatus("getting total num", 10, "\n");#->565
my $total_num = 0;
my $ung_num = 0;
if ($UMI_CB_ctss_bed_path =~ m/\.gz$/) {
open (FILEIN, " gzip -dc $UMI_CB_ctss_bed_path | cut -f 5 |");
} else {
open (FILEIN, "cut -f 5 $UMI_CB_ctss_bed_path |");
}
while (<FILEIN>) {
chomp (my $ung = $_);
$total_num++;
$ung_num++ if $ung > 0;
}
close FILEIN;
my $target_fraction = $subsample_num/$total_num;
if ($target_fraction > 1) {
$target_fraction = 1;
&reportAndLogStatus("subsample_num is greater than total_num", 10, "\n");#->565
&reportAndLogStatus("limiting target_fraction to 1", 10, "\n");#->565
}
&reportAndLogStatus("total_num=$total_num", 10, "\n");#->565
&reportAndLogStatus("unencoded_G_num=$ung_num", 10, "\n");#->565
&reportAndLogStatus("subsample_num=$subsample_num", 10, "\n");#->565
&reportAndLogStatus("target_fraction=$target_fraction", 10, "\n");#->565
return ($target_fraction);
}
sub logCalledCMDAndScript {
#....................................................................................................................................................#
# subroutineCategory: general
# dependOnSub: >none
# appearInSub: >none
# primaryAppearInSection: 2_defineOutDirPath|118
# secondaryAppearInSection: >none
# input: $ARGVStr, $result_script_dir, $scriptAbsPath
# output:
# toCall: &logCalledCMDAndScript($ARGVStr, $result_script_dir, $scriptAbsPath);
# calledInLine: 129
#....................................................................................................................................................#
my ($ARGVStr, $result_script_dir, $scriptAbsPath) = @_;
my $cpScriptPath = "$result_script_dir/script.ran.pl";
my $calledCMDPath = "$result_script_dir/called.cmd.txt";
system "cp -f $scriptAbsPath $cpScriptPath";
system "chmod 0444 $cpScriptPath"; #---[07/03/2014 18:02] make it read-only to make sure there'll be accodental change of parameters
open CALLEDCMD, ">", $calledCMDPath;
print CALLEDCMD join "", ($ARGVStr), "\n";
close CALLEDCMD;
return ();
}
sub printOutputFileListAndReadme {
#....................................................................................................................................................#
# subroutineCategory: output
# dependOnSub: >none
# appearInSub: >none
# primaryAppearInSection: 5_finishingTasks|152
# secondaryAppearInSection: >none
# input: $ARGVStr, $outDir, $paramTag
# output:
# toCall: &printOutputFileListAndReadme($ARGVStr, $paramTag, $outDir);
# calledInLine: 155
#....................................................................................................................................................#
my ($ARGVStr, $paramTag, $outDir) = @_;
my $outputFileListPath = "$outDir/$paramTag/output.file.list.txt";
open (OUTFILELIST, ">", $outputFileListPath);
my %dirHsh = ();
my %filelistLenCountHsh = ();
push @{$filelistLenCountHsh{'dir'}}, length 'Directory';
push @{$filelistLenCountHsh{'name'}}, length 'Name';
push @{$filelistLenCountHsh{'description'}}, length 'Description';
foreach my $outputFilePath (sort {$a cmp $b} keys %{$globalReadmeHsh_ref}) {
my $fileDescription = $globalReadmeHsh_ref->{$outputFilePath}{'description'};
my $cleandOutputFilePath = $outputFilePath;
$cleandOutputFilePath =~ s/\/+/\//g;
my ($filePrefix, $fileDir, $fileSuffix) = fileparse($cleandOutputFilePath, qr/\.[^.]*/);
$fileDir =~ s/^$outDir//;
my $fileName = $filePrefix.$fileSuffix;
$dirHsh{$fileDir}{$fileName} = $fileDescription;
push @{$filelistLenCountHsh{'dir'}}, length $fileDir;
push @{$filelistLenCountHsh{'name'}}, length $fileName;
push @{$filelistLenCountHsh{'description'}}, length $fileDescription;
open README, ">", "$outputFilePath.readme.txt";
print README "=================\n";
print README "File descriptions\n";
print README "=================\n";
print README "$fileDescription\n";
if (exists $globalReadmeHsh_ref->{$outputFilePath}{'headerAry'}) {
my @colLenCountHsh = (length 'column');
push @colLenCountHsh, length $_ foreach (@{$globalReadmeHsh_ref->{$outputFilePath}{'headerAry'}});
my $headerColLen = max(@colLenCountHsh)+2;
print README "\n";
print README "\n";
print README "===================\n";
print README "Column descriptions\n";
print README "===================\n";
print README "\n";
printf README "%-".$headerColLen."s", 'column';
print README "description\n";
printf README "%-".$headerColLen."s", '------';
print README "-----------\n";
foreach my $header (@{$globalReadmeHsh_ref->{$outputFilePath}{'headerAry'}}) {
my $columnDescription = 'self-explanatory';
$columnDescription = $globalReadmeHsh_ref->{$outputFilePath}{'header'}{$header} if exists $globalReadmeHsh_ref->{$outputFilePath}{'header'}{$header};
printf README "%-".$headerColLen."s", $header;
print README $columnDescription."\n";
}
}
if (exists $globalReadmeHsh_ref->{$outputFilePath}{'extra_info'}) {
print README "\n";
print README "\n";
print README "=================\n";
print README "Extra information\n";
print README "=================\n";
print README "\n";
foreach my $title (sort keys %{$globalReadmeHsh_ref->{$outputFilePath}{'extra_info'}}) {
print README "$title\n";
print README "-" foreach (1..length $title);
print README "\n";
print README "$_\n" foreach @{$globalReadmeHsh_ref->{$outputFilePath}{'extra_info'}{$title}};
}
}
print README "\n";
print README "\n";
print README "~" foreach (1..length "$fileName was created from running,");
print README "\n";
print README "$fileName was created from running,\n";
print README "\n";
print README "$ARGVStr\n";
print README "\n";
close README;
}
my $fileDir_colLen = max(@{$filelistLenCountHsh{'dir'}})+2;
my $fileName_colLen = max(@{$filelistLenCountHsh{'name'}})+2;
my $fileDescription_colLen = max(@{$filelistLenCountHsh{'description'}})+2;
printf OUTFILELIST ("%-".$fileDir_colLen."s %-".$fileName_colLen."s %-".$fileDescription_colLen."s\n", 'directory', 'name', 'description');
printf OUTFILELIST ("%-".$fileDir_colLen."s %-".$fileName_colLen."s %-".$fileDescription_colLen."s\n", '=========', '====', '===========');
foreach my $fileDir (sort {$a cmp $b} keys %dirHsh) {
foreach my $fileName (sort {$a cmp $b} keys %{$dirHsh{$fileDir}}) {
my $fileDescription = $dirHsh{$fileDir}{$fileName};
printf OUTFILELIST ("%-".$fileDir_colLen."s %-".$fileName_colLen."s %-".$fileDescription_colLen."s\n", $fileDir, $fileName, $fileDescription);
}
}
print OUTFILELIST "\n";
print OUTFILELIST "\n";
print OUTFILELIST "~" foreach (1..length "The above files were generated by running,");
print OUTFILELIST "\n";
print OUTFILELIST "The above files were generated by running,\n";
print OUTFILELIST "\n";
print OUTFILELIST "$ARGVStr\n";
print OUTFILELIST "\n";
close OUTFILELIST;
return ();
}
sub printStartOrFinishMessage {
#....................................................................................................................................................#
# subroutineCategory: general
# dependOnSub: currentTime|286
# appearInSub: >none
# primaryAppearInSection: 2_defineOutDirPath|118, 5_finishingTasks|152
# secondaryAppearInSection: >none
# input: $StartOrFinishMessage
# output: none
# toCall: &printStartOrFinishMessage($StartOrFinishMessage);
# calledInLine: 130, 156
#....................................................................................................................................................#
my ($StartOrFinishMessage) = @_;
if ($StartOrFinishMessage eq "startMessage") {
print "\n=========================================================================\n";
print "[".¤tTime()."] starts running ...... \n";#->286
print "=========================================================================\n\n";
print $tmplog_fh "\n=========================================================================\n";
print $tmplog_fh "[".¤tTime()."] starts running ...... \n";#->286
print $tmplog_fh "=========================================================================\n\n";
} elsif ($StartOrFinishMessage eq "finishMessage") {
print "\n=========================================================================\n";
print "[".¤tTime()."] finished running .......\n";#->286
print "=========================================================================\n\n";
print $tmplog_fh "\n=========================================================================\n";
print $tmplog_fh "[".¤tTime()."] finished running .......\n";#->286
print $tmplog_fh "=========================================================================\n\n";
}
}
sub readParameters {
#....................................................................................................................................................#
# subroutineCategory: general
# dependOnSub: >none
# appearInSub: >none
# primaryAppearInSection: 0_startingTasks|102
# secondaryAppearInSection: >none
# input: none
# output: $UMI_CB_ctss_bed_path, $outDir, $outputPrefix, $overwrite, $subsample_num
# toCall: my ($UMI_CB_ctss_bed_path, $subsample_num, $outputPrefix, $outDir, $overwrite) = &readParameters();
# calledInLine: 105
#....................................................................................................................................................#
my ($UMI_CB_ctss_bed_path, $subsample_num, $outputPrefix, $outDir, $overwrite);
$overwrite = 'no';
GetOptions (
"UMI_CB_ctss_bed_path=s" => \$UMI_CB_ctss_bed_path,
"subsample_num=s" => \$subsample_num,
"outputPrefix=s" => \$outputPrefix,
"outDir:s" => \$outDir,
"overwrite:s" => \$overwrite,
'help' => sub { HelpMessage(0) },
) or HelpMessage(1);
HelpMessage(1) unless $UMI_CB_ctss_bed_path;
#---check file
my $file_check_hsh_ref = {
'UMI_CB_ctss_bed_path' => $UMI_CB_ctss_bed_path,
};
foreach my $option_name (keys %{$file_check_hsh_ref}) {
my $file_path = $file_check_hsh_ref->{$option_name};
die "Quitting: File $option_name does not exists at $file_path" if not -s $file_path;
}
chop $outDir if ($outDir =~ m/\/$/); #---remove the last slash
system "mkdir -p -m 755 $outDir/";
return($UMI_CB_ctss_bed_path, $subsample_num, $outputPrefix, $outDir, $overwrite);
}
sub reportAndLogStatus {
#....................................................................................................................................................#
# subroutineCategory: log
# dependOnSub: currentTime|286
# appearInSub: checkAllExecutable|186, getTargetFraction|304, subsampleCTSS|587
# primaryAppearInSection: >none
# secondaryAppearInSection: 3_subsample|135
# input: $lineEnd, $message, $numTrailingSpace
# output:
# toCall: &reportAndLogStatus($message, $numTrailingSpace, $lineEnd);
# calledInLine: 209, 214, 223, 232, 241, 250, 259, 268, 277, 317, 335, 336, 339, 340, 341, 342, 626
#....................................................................................................................................................#
my ($message, $numTrailingSpace, $lineEnd) = @_;
my $trailingSpaces = '';
$trailingSpaces .= " " for (1..$numTrailingSpace);
print "[".¤tTime()."] ".$message.$trailingSpaces.$lineEnd;#->286
print $tmplog_fh "[".¤tTime()."] ".$message.$lineEnd if $lineEnd ne "\r";#->286
return ();
}
sub subsampleCTSS {
#....................................................................................................................................................#
# subroutineCategory: unassigned
# dependOnSub: reportAndLogStatus|565
# appearInSub: >none
# primaryAppearInSection: 3_subsample|135
# secondaryAppearInSection: >none
# input: $UMI_CB_ctss_bed_path, $bedtools_bin, $outputPrefix, $result_bed_dir, $target_fraction
# output:
# toCall: &subsampleCTSS($UMI_CB_ctss_bed_path, $result_bed_dir, $bedtools_bin, $target_fraction, $outputPrefix);
# calledInLine: 140
#....................................................................................................................................................#
my ($UMI_CB_ctss_bed_path, $result_bed_dir, $bedtools_bin, $target_fraction, $tabix_bin, $bgzip_bin, $max_thread, $outputPrefix) = @_;
my $sub_collapse_ctss_bed_path = "$result_bed_dir/$outputPrefix.subsample.collapse.ctss.bed";
my $sub_ung_collapse_ctss_bed_path = "$result_bed_dir/$outputPrefix.subsample.unencoded_G.collapse.ctss.bed";
my $sub_CB_ctss_bed_path = "$result_bed_dir/$outputPrefix.subsample.CB.ctss.bed";
my $num_proc = 0;
my $tmp_CTSS_hsh_ref = {};
my $last_chrom = 'initial';
my $last_start = 'initial';
my $last_strand = 'initial';
if ($UMI_CB_ctss_bed_path =~ m/\.gz$/) {
open (INCTSS, " gzip -dc $UMI_CB_ctss_bed_path|");
} else {
open (INCTSS, "<", $UMI_CB_ctss_bed_path);
}
open SUBCBCTSS, ">$sub_CB_ctss_bed_path";
open SUBCOLLAPSECTSS, ">$sub_collapse_ctss_bed_path";
open SUBUNGCOLLAPSECTSS, ">$sub_ung_collapse_ctss_bed_path";
while (<INCTSS>) {
chomp;
my ($chrom, $start, $end, $ID, $num_unencoded_G, $strand) = split /\t/;
next if (rand() > $target_fraction);
$num_proc++;
if ($num_proc%10000 == 0) {
&reportAndLogStatus("$num_proc CTSS subsampled", 10, "\n");#->565
}
{
my ($CB, $UMI) = split /_/, $ID;
$tmp_CTSS_hsh_ref->{$chrom}{$start}{$strand}{$CB}{'all'}{$UMI}++;
$tmp_CTSS_hsh_ref->{$chrom}{$start}{$strand}{$CB}{'ung'}{$UMI}++ if $num_unencoded_G > 0;
}
if ($last_chrom eq 'initial') {
$last_chrom = $chrom;
$last_start = $start;
$last_strand = $strand;
}
if (($chrom ne $last_chrom or $start ne $last_start or $strand ne $last_strand)) {
my $last_end = $last_start + 1;
my $CB_collapse_num_umi = 0;
my $num_CB = 0;
my $ung_CB_collapse_num_umi = 0;
my $ung_num_CB = 0;
foreach my $CB (sort keys %{$tmp_CTSS_hsh_ref->{$last_chrom}{$last_start}{$last_strand}}) {
my $num_umi = keys %{$tmp_CTSS_hsh_ref->{$last_chrom}{$last_start}{$last_strand}{$CB}{'all'}};
$CB_collapse_num_umi += $num_umi;
$num_CB++;
print SUBCBCTSS join "", (join "\t", ($last_chrom, $last_start, $last_end, $CB, $num_umi, $last_strand)), "\n";
if (exists $tmp_CTSS_hsh_ref->{$last_chrom}{$last_start}{$last_strand}{$CB}{'ung'}) {
my $ung_num_umi = keys %{$tmp_CTSS_hsh_ref->{$last_chrom}{$last_start}{$last_strand}{$CB}{'ung'}};
$ung_CB_collapse_num_umi += $ung_num_umi;
$ung_num_CB++;
}
}
print SUBCOLLAPSECTSS join "", (join "\t", ($last_chrom, $last_start, $last_end, $num_CB, $CB_collapse_num_umi, $last_strand)), "\n";
if ($ung_CB_collapse_num_umi > 0) {
print SUBUNGCOLLAPSECTSS join "", (join "\t", ($last_chrom, $last_start, $last_end, $ung_num_CB, $ung_CB_collapse_num_umi, $last_strand)), "\n";
}
delete $tmp_CTSS_hsh_ref->{$last_chrom}{$last_start}{$last_strand};
$last_chrom = $chrom;
$last_start = $start;
$last_strand = $strand;
}
if (eof(INCTSS)) {
foreach my $chrom (keys %{$tmp_CTSS_hsh_ref}) {
foreach my $start (keys %{$tmp_CTSS_hsh_ref->{$chrom}}) {
my $end = $start + 1;
foreach my $strand (keys %{$tmp_CTSS_hsh_ref->{$chrom}{$start}}) {
my $CB_collapse_num_umi = 0;
my $num_CB = 0;
my $ung_CB_collapse_num_umi = 0;
my $ung_num_CB = 0;
foreach my $CB (sort keys %{$tmp_CTSS_hsh_ref->{$chrom}{$start}{$strand}}) {
my $num_umi = keys %{$tmp_CTSS_hsh_ref->{$chrom}{$start}{$strand}{$CB}{'all'}};
$CB_collapse_num_umi += $num_umi;
$num_CB++;
print SUBCBCTSS join "", (join "\t", ($chrom, $start, $end, $CB, $num_umi, $strand)), "\n";
if (exists $tmp_CTSS_hsh_ref->{$chrom}{$start}{$strand}{$CB}{'ung'}) {
my $ung_num_umi = keys %{$tmp_CTSS_hsh_ref->{$last_chrom}{$last_start}{$last_strand}{$CB}{'ung'}};
$ung_CB_collapse_num_umi += $ung_num_umi;
$ung_num_CB++;
}
}
print SUBCOLLAPSECTSS join "", (join "\t", ($chrom, $start, $end, "$num_CB", $CB_collapse_num_umi, $strand)), "\n";
if ($ung_CB_collapse_num_umi > 0) {
print SUBUNGCOLLAPSECTSS join "", (join "\t", ($chrom, $start, $end, "$ung_num_CB", $ung_CB_collapse_num_umi, $strand)), "\n";
}
}
}
}
}
}
close INCTSS;
close SUBCBCTSS;
close SUBCOLLAPSECTSS;
close SUBUNGCOLLAPSECTSS;
my $ctss_hsh_ref = {
'sub_collapse_ctss_bed_path' => $sub_collapse_ctss_bed_path,
'sub_ung_collapse_ctss_bed_path' => $sub_ung_collapse_ctss_bed_path,
'sub_CB_ctss_bed_path' => $sub_CB_ctss_bed_path,
};
foreach my $ctss_name (sort keys %{$ctss_hsh_ref}) {
my $ctss_path = $ctss_hsh_ref->{$ctss_name};
my $tabix_ctss_path = "$ctss_path.gz";
&reportAndLogStatus("Compressing $ctss_name.", 10, "\n");#->1098
system "$bgzip_bin -@ $max_thread -c $ctss_path >$tabix_ctss_path";
system "rm -f $ctss_path";
&reportAndLogStatus("Tabix indexing $ctss_name.", 10, "\n");#->1098
system "$tabix_bin -p bed $tabix_ctss_path";
}
return ();
}
sub timeStamp {
#....................................................................................................................................................#
# subroutineCategory: time, general
# dependOnSub: >none
# appearInSub: >none
# primaryAppearInSection: >none
# secondaryAppearInSection: >none
# input: none
# output: $curntTimeStamp
# toCall: my ($curntTimeStamp) = &timeStamp();
# calledInLine: 90
#....................................................................................................................................................#
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
my $curntTimeStamp = sprintf "%04d.%02d.%02d.%02d.%02d.%02d", $year+1900,$mon+1,$mday,$hour,$min,$sec;
return ($curntTimeStamp);
}
exit;