-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathscafe.tool.bk.count
736 lines (595 loc) · 29.2 KB
/
scafe.tool.bk.count
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
#!/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 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.bk.count <---
<--- tool, bulk mode, count ctss within tCREs --->
Description:
This tool counts the CAGE reads within a set of user-defined regions, e.g. tCRE, and
returns the reads per regions
Usage:
scafe.tool.bk.count [options] --countRegion_bed_path --ctss_bed_path --outputPrefix --outDir
--countRegion_bed_path <required> [string] bed file contains the regions for counting CTSS, e.g. tCRE ranges,
*.CRE.coord.bed.gz from scafe.tool.cm.annotate
--ctss_bed_path <required> [string] ctss file for counting,
*collapse.ctss.bed.gz from scafe.tool.bk.bam_to_ctss,
5th column is number of read
--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
For demo, cd to SCAFE dir and run,
scafe.tool.bk.count \
--overwrite=yes \
--countRegion_bed_path=./demo/output/bk.solo/annotate/demo/bed/demo.CRE.annot.bed.gz \
--ctss_bed_path=./demo/output/bk.solo/bam_to_ctss/demo/bed/demo.collapse.ctss.bed.gz \
--outputPrefix=demo \
--outDir=./demo/output/bk.solo/count/
=head1 VERSION
v0.9.0-beta [March 20, 2021]
-Initial pre-release
v1.0.0 [June 6, 2022]
-No major update
=cut
#<\doc>
#====================================================================================================================================================#
#====================================================================================================================================================#
#<lastCmdCalled>
#
# notCalledBefore
#
# notCalledBefore
#
#<\lastCmdCalled>
#====================================================================================================================================================#
#====================================================================================================================================================#
#<global>
my $scriptDirPath = dirname(rel2abs($0));
my $scriptAbsPath = abs_path($0);
my ($curntTimeStamp) = &timeStamp();#->624
my $ARGVStr = join "\n", (¤tTime(), $scriptAbsPath, @ARGV);#->264
my $globalReadmeHsh_ref = {};
our $tmplog_fh;
#<\global>
#====================================================================================================================================================#
#====================================================================================================================================================#
{ #Main sections lexical scope starts
#====================================================================================================================================================#
#====================================================================================================================================================#
# section 0_startingTasks
#
#<section ID="startingTasks" num="0">
my ($countRegion_bed_path, $ctss_bed_path, $outputPrefix, $outDir, $overwrite) = &readParameters();#->521
#<\section>
#====================================================================================================================================================#
#====================================================================================================================================================#
# section 1_defineHardCodedParam
#
#<section ID="defineHardCodedParam" num="1">
my $paramTag = "$outputPrefix";
#<\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_log_dir = "$result_dir/log/"; push @mkDirAry, $result_log_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);#->282
&printStartOrFinishMessage("startMessage");#->448
my ($bedtools_bin, $samtools_bin, $paraclu_bin, $cut_sh_path, $bedGraphToBigWig_bin, $bigWigAverageOverBed_bin) = &checkAllExecutable();#->184
#<\section>
#====================================================================================================================================================#
#====================================================================================================================================================#
# section 3_output
#
#<section ID="output" num="3">
my ($countRegion_info_hsh_ref) = &readCountRegionInfo($countRegion_bed_path);#->482
&runBedToolsIntersect($countRegion_bed_path, $ctss_bed_path, $countRegion_info_hsh_ref, $bedtools_bin);#->587
&printCount($countRegion_info_hsh_ref, $result_log_dir);#->307
#<\section>
#====================================================================================================================================================#
#====================================================================================================================================================#
# section 4_finishingTasks
#
#<section ID="finishingTasks" num="4">
&printOutputFileListAndReadme($ARGVStr, $paramTag, $outDir);#->333
&printStartOrFinishMessage("finishMessage");#->448
#<\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
#
# range [n=1]:
# runBedToolsIntersect
#
# time [n=1]:
# timeStamp
#
# unassigned [n=3]:
# checkAllExecutable, printCount, readCountRegionInfo
#
#====================================================================================================================================================#
sub checkAllExecutable {
#....................................................................................................................................................#
# subroutineCategory: unassigned
# dependOnSub: reportAndLogStatus|565
# appearInSub: >none
# primaryAppearInSection: 2_defineOutDirPath|120
# secondaryAppearInSection: >none
# input: none
# output: $bedGraphToBigWig_bin, $bedtools_bin, $bigWigAverageOverBed_bin, $cut_sh_path, $paraclu_bin, $samtools_bin
# toCall: my ($bedtools_bin, $samtools_bin, $paraclu_bin, $cut_sh_path, $bedGraphToBigWig_bin, $bigWigAverageOverBed_bin) = &checkAllExecutable();
# calledInLine: 132
#....................................................................................................................................................#
my $dirPath = dirname(rel2abs($0));
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 = `$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 ($bedtools_bin, $samtools_bin, $paraclu_bin, $cut_sh_path, $bedGraphToBigWig_bin, $bigWigAverageOverBed_bin);
}
sub currentTime {
#....................................................................................................................................................#
# subroutineCategory: general
# dependOnSub: >none
# appearInSub: printStartOrFinishMessage|448, reportAndLogStatus|565
# primaryAppearInSection: >none
# secondaryAppearInSection: 2_defineOutDirPath|120, 4_finishingTasks|147
# input: none
# output: $runTime
# toCall: my ($runTime) = ¤tTime();
# calledInLine: 93, 464, 468, 473, 477, 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 logCalledCMDAndScript {
#....................................................................................................................................................#
# subroutineCategory: general
# dependOnSub: >none
# appearInSub: >none
# primaryAppearInSection: 2_defineOutDirPath|120
# secondaryAppearInSection: >none
# input: $ARGVStr, $result_script_dir, $scriptAbsPath
# output:
# toCall: &logCalledCMDAndScript($ARGVStr, $result_script_dir, $scriptAbsPath);
# calledInLine: 130
#....................................................................................................................................................#
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 printCount {
#....................................................................................................................................................#
# subroutineCategory: unassigned
# dependOnSub: reportAndLogStatus|565
# appearInSub: >none
# primaryAppearInSection: 3_output|137
# secondaryAppearInSection: >none
# input: $countRegion_info_hsh_ref, $result_log_dir
# output:
# toCall: &printCount($countRegion_info_hsh_ref, $result_log_dir);
# calledInLine: 142
#....................................................................................................................................................#
my ($countRegion_info_hsh_ref, $result_log_dir) = @_;
&reportAndLogStatus("printing count", 0, "\n");#->565
my $count_path = "$result_log_dir/count.log.tsv";
open (COUNTLOG, ">", "$count_path");
$globalReadmeHsh_ref->{$count_path}{'description'} = "tsv file contains read counts of each CRE";
print COUNTLOG join "", ((join "\t", ('countRegion_ID', 'count')), "\n");
foreach my $countRegion_ID (sort {$countRegion_info_hsh_ref->{$a}{'count'} <=> $countRegion_info_hsh_ref->{$b}{'count'}} keys %{$countRegion_info_hsh_ref}) {
my $count = $countRegion_info_hsh_ref->{$countRegion_ID}{'count'};
print COUNTLOG join "", ((join "\t", ($countRegion_ID, $count)), "\n");
}
close COUNTLOG;
return ();
}
sub printOutputFileListAndReadme {
#....................................................................................................................................................#
# subroutineCategory: output
# dependOnSub: >none
# appearInSub: >none
# primaryAppearInSection: 4_finishingTasks|147
# secondaryAppearInSection: >none
# input: $ARGVStr, $outDir, $paramTag
# output:
# toCall: &printOutputFileListAndReadme($ARGVStr, $paramTag, $outDir);
# calledInLine: 150
#....................................................................................................................................................#
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|264
# appearInSub: >none
# primaryAppearInSection: 2_defineOutDirPath|120, 4_finishingTasks|147
# secondaryAppearInSection: >none
# input: $StartOrFinishMessage
# output: none
# toCall: &printStartOrFinishMessage($StartOrFinishMessage);
# calledInLine: 131, 151
#....................................................................................................................................................#
my ($StartOrFinishMessage) = @_;
if ($StartOrFinishMessage eq "startMessage") {
print "\n=========================================================================\n";
print "[".¤tTime()."] starts running ...... \n";#->264
print "=========================================================================\n\n";
print $tmplog_fh "\n=========================================================================\n";
print $tmplog_fh "[".¤tTime()."] starts running ...... \n";#->264
print $tmplog_fh "=========================================================================\n\n";
} elsif ($StartOrFinishMessage eq "finishMessage") {
print "\n=========================================================================\n";
print "[".¤tTime()."] finished running .......\n";#->264
print "=========================================================================\n\n";
print $tmplog_fh "\n=========================================================================\n";
print $tmplog_fh "[".¤tTime()."] finished running .......\n";#->264
print $tmplog_fh "=========================================================================\n\n";
}
}
sub readCountRegionInfo {
#....................................................................................................................................................#
# subroutineCategory: unassigned
# dependOnSub: reportAndLogStatus|565
# appearInSub: >none
# primaryAppearInSection: 3_output|137
# secondaryAppearInSection: >none
# input: $countRegion_bed_path
# output: $countRegion_info_hsh_ref
# toCall: my ($countRegion_info_hsh_ref) = &readCountRegionInfo($countRegion_bed_path);
# calledInLine: 140
#....................................................................................................................................................#
my ($countRegion_bed_path) = @_;
my $countRegion_info_hsh_ref = {};
&reportAndLogStatus("reading count region info", 0, "\n");#->565
if ($countRegion_bed_path =~ m/\.gz$/) {
open (COUNTREGION, " gzip -dc $countRegion_bed_path |");
} else {
open (COUNTREGION, "<", $countRegion_bed_path);
}
my $countRegion_index = 1;
while (<COUNTREGION>) {
chomp;
my ($chrom, $chromStart, $chromEnd, $countRegion_ID) = split /\t/;
my $countRegion_name = $countRegion_ID;
die "$countRegion_ID is duplicated\n" if exists $countRegion_info_hsh_ref->{$countRegion_ID};
$countRegion_info_hsh_ref->{$countRegion_ID}{'count'} = 0;
$countRegion_index++;
}
close COUNTREGION;
my $num_countRegion = keys %{$countRegion_info_hsh_ref};
&reportAndLogStatus("info of $num_countRegion countRegion stored", 0, "\n");#->565
return ($countRegion_info_hsh_ref);
}
sub readParameters {
#....................................................................................................................................................#
# subroutineCategory: general
# dependOnSub: >none
# appearInSub: >none
# primaryAppearInSection: 0_startingTasks|104
# secondaryAppearInSection: >none
# input: none
# output: $countRegion_bed_path, $ctss_bed_path, $outDir, $outputPrefix, $overwrite
# toCall: my ($countRegion_bed_path, $ctss_bed_path, $outputPrefix, $outDir, $overwrite) = &readParameters();
# calledInLine: 107
#....................................................................................................................................................#
my ($countRegion_bed_path, $ctss_bed_path, $outputPrefix, $outDir, $overwrite);
$overwrite = 'no';
GetOptions (
"countRegion_bed_path=s" => \$countRegion_bed_path,
"ctss_bed_path=s" => \$ctss_bed_path,
"outputPrefix=s" => \$outputPrefix,
"outDir:s" => \$outDir,
"overwrite:s" => \$overwrite,
'help' => sub { HelpMessage(0) },
) or HelpMessage(1);
HelpMessage(1) unless $countRegion_bed_path;
#---check file
my $file_check_hsh_ref = {
'countRegion_bed_path' => $countRegion_bed_path,
'ctss_bed_path' => $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 744 $outDir/";
return($countRegion_bed_path, $ctss_bed_path, $outputPrefix, $outDir, $overwrite);
}
sub reportAndLogStatus {
#....................................................................................................................................................#
# subroutineCategory: log
# dependOnSub: currentTime|264
# appearInSub: checkAllExecutable|184, printCount|307, readCountRegionInfo|482, runBedToolsIntersect|587
# primaryAppearInSection: >none
# secondaryAppearInSection: 2_defineOutDirPath|120, 3_output|137
# input: $lineEnd, $message, $numTrailingSpace
# output:
# toCall: &reportAndLogStatus($message, $numTrailingSpace, $lineEnd);
# calledInLine: 205, 210, 219, 228, 237, 246, 255, 320, 497, 516, 601
#....................................................................................................................................................#
my ($message, $numTrailingSpace, $lineEnd) = @_;
my $trailingSpaces = '';
$trailingSpaces .= " " for (1..$numTrailingSpace);
print "[".¤tTime()."] ".$message.$trailingSpaces.$lineEnd;#->264
print $tmplog_fh "[".¤tTime()."] ".$message.$lineEnd if $lineEnd ne "\r";#->264
return ();
}
sub runBedToolsIntersect {
#....................................................................................................................................................#
# subroutineCategory: range
# dependOnSub: reportAndLogStatus|565
# appearInSub: >none
# primaryAppearInSection: 3_output|137
# secondaryAppearInSection: >none
# input: $bedtools_bin, $countRegion_bed_path, $countRegion_info_hsh_ref, $ctss_bed_path
# output:
# toCall: &runBedToolsIntersect($countRegion_bed_path, $ctss_bed_path, $countRegion_info_hsh_ref, $bedtools_bin);
# calledInLine: 141
#....................................................................................................................................................#
my ($countRegion_bed_path, $ctss_bed_path, $countRegion_info_hsh_ref, $bedtools_bin) = @_;
&reportAndLogStatus("Start running bedtools intersect. Be patient.", 10, "\n");#->565
if ($countRegion_bed_path =~ m/\.gz$/) {
#---[2/18/15 19:54] sometimes will crash on ac2 if read gz directly
open BEDTOOLS, "gzip -dc $countRegion_bed_path | cut -f 1-6 | $bedtools_bin intersect -sorted -wo -a stdin -b $ctss_bed_path |";
} else {
open BEDTOOLS, "cut -f 1-6 $countRegion_bed_path | $bedtools_bin intersect -sorted -wo -a stdin -b $ctss_bed_path |";
}
while (<BEDTOOLS>) {
chomp;
#chr1 19485502 19485554 p1@CAPZB 639956 - chr1 19485525 19485526 TGCGCAGAGGCACATG-1 2 - 1
my ($chrom, $countRegion_start, $countRegion_end, $countRegion_ID, undef, $countRegion_strand, undef, undef, undef, undef, $count, $CTSS_strand) = split /\t/;
if (($countRegion_strand eq '.' or $countRegion_strand eq $CTSS_strand) and (exists $countRegion_info_hsh_ref->{$countRegion_ID})) {
$countRegion_info_hsh_ref->{$countRegion_ID}{'count'} += $count;
}
}
close BEDTOOLS;
return ();
}
sub timeStamp {
#....................................................................................................................................................#
# subroutineCategory: time, general
# dependOnSub: >none
# appearInSub: >none
# primaryAppearInSection: >none
# secondaryAppearInSection: >none
# input: none
# output: $curntTimeStamp
# toCall: my ($curntTimeStamp) = &timeStamp();
# calledInLine: 92
#....................................................................................................................................................#
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;