upstream-extended/iuc/tools/windowmasker/windowmasker_to_bed.pl
1#!/usr/bin/env perl
2use warnings;
3use strict;
4
5=head1 NAME
6
7windowmasker_to_bed.pl - Convert WindowMasker output to BED format
8
9=head1 SYNOPSIS
10
11 windowmasker_to_bed.pl < ${input.interval} > ${output.bed}
12
13=head1 DESCRIPTION
14
15This scripts converts the interval output from WindowMasker ustat
16to bed 4 format.
17
18=head1 VERSION
19
20Last update: 2017-04-28
21
22=cut
23
24
25#====================
26# Libraries
27#====================
28use Getopt::Long;
29use Pod::Usage;
30use Carp;
31
32sub main {
33 parse_arguments();
34
35 my $chrom;
36 my $idx = 0;
37
38 while ( defined(my $line = <>) ) {
39 next if ($line =~ /^\s*$/x);
40
41 if ($line =~ /^>(\S+)/x) {
42 $chrom = $1;
43
44 } elsif ($line =~ /(\d+)\s+\-\s+(\d+)/x) {
45 # interval coordinates are 0-based
46 my $start = $1;
47 my $end = $2 + 1;
48 my $name = sprintf("wm_%d", $idx++);
49
50 print join("\t", $chrom, $start, $end, $name), "\n";
51
52 } else {
53 croak("Invalid line: ${line}");
54 }
55 }
56
57 return;
58}
59
60main();
61
62
63
64#====================
65# Parse command-line arguments
66#====================
67sub parse_arguments {
68 my $help = 0;
69
70 GetOptions('help|?' => \$help) or usage();
71
72 pod2usage({ verbose => 2 }) if ($help);
73
74 return;
75}
76
77sub usage {
78 my $msg = shift;
79
80 pod2usage({ verbose => 1, message => $msg || "" });
81 exit 1;
82}