forked from molgenis/ngs-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_per_base_bed.pl
executable file
·76 lines (68 loc) · 2.31 KB
/
create_per_base_bed.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
use Getopt::Long;
use List::Util qw(first);
use POSIX;
my ($help, $inputbed, $output, $outputfolder);
#### get options
GetOptions(
"h" => \$help,
"input=s" => \$inputbed,
"output=s" => \$output,
"outputfolder=s" => \$outputfolder,
);
usage() and exit(1) if $help;
# mandatory args
usage() and exit(1) unless $inputbed;
usage() and exit(1) unless $output;
usage() and exit(1) unless $outputfolder;
chomp $inputbed;
chomp $output;
chomp $outputfolder;
#Open input and output files
open (INPUT, "<", $inputbed ) or die $!;
#Read bed file
my $number=0;
my $regnum = 1;
my $binSize= 1;
while (my $lines=<INPUT>){
chomp $lines;
if ($lines !~ m/^track.+/gs) {
#print $lines . "\n";
#Remove chr before chrNumber and substitute M with MT
$lines =~ s/^chr//i;
$lines =~ s/^M\t/MT\t/i;
#Split line
my @array = split("\t", $lines);
my $chr = $array[0];
my $start = $array[1];
my $stop = $array[2];
my $gene = $array[3];
my $region = ($stop-$start);
#Iterate over region and create bins
open (OUTPUT, ">>", "$outputfolder/$output.per_base.bed" ) or die $!;
for (my $i=($start+1); $i<=$stop; $i=($i+$binSize)){
print OUTPUT "$chr\t" . $i . "\t" . ($i+1) . "\t$gene\n";
}
close(OUTPUT);
$regnum++;
}else{
#Negative check
#print "$lines\n";
}
}
sub usage {
print <<EOF;
#########################################################################################
This script splits a bed file in regions of a length specified by the user.
#########################################################################################
Usage: ./create_per_base_intervals.pl
\t-input\t\t\tInput bed file.
\t-output\t\t\tOutput prefix
\t-outputfolder\t\tOutputfolder
Example usage: perl create_per_base_intervals.pl -input target_exons.bed -output exonIntervals
#########################################################################################
EOF
}