-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfstep_to_bed.pl
executable file
·65 lines (58 loc) · 1.82 KB
/
fstep_to_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
#!/usr/bin/env perl
# Description: - read fixedStep or variableStep wiggle input data,
# output four column bedGraph format data
# Author: Vipin
use warnings;
use strict;
my ($position, $chr, $step, $span) = (0, "", 1, 1);
my $usage = q(
fstep_to_bed.pl - Program to convert a valid fixedStep or variableStep wiggle input data to BED format.
USAGE: fstep_to_bed.pl <fixedStep/variableStep Wiggle format> +/- > <output file name>
);
if (scalar(@ARGV) != 2) {
print $usage;
exit
}
my $inFile = $ARGV[0];
my $strand = $ARGV[1];
open WIG, "<$inFile" || die "Can't open the Wiggle file \n";
while (my $dataValue = <WIG>)
#while (my $dataValue = <STDIN>)
{
chomp $dataValue;
if ( $dataValue =~ m/^track /) {
print STDERR "Skipping track line\n";
next;
}
if ( $dataValue =~ m/^fixedStep/ || $dataValue =~ m/^variableStep/ ) {
$position = 0;
$chr = "";
$step = 1;
$span = 1;
my @a = split /\s/, $dataValue;
for (my $i = 1; $i < scalar(@a); ++$i) {
my ($ident, $value) = split('=',$a[$i]);
if ($ident =~ m/chrom/) { $chr = $value; }
elsif ($ident =~ m/start/) { $position = $value-1; }
elsif ($ident =~ m/step/) { $step = $value; }
elsif ($ident =~ m/span/) { $span = $value; }
else {
print STDERR "ERROR: unrecognized fixedStep line: $dataValue\n";
exit 255;
}
}
open WIG, ">$chr" || die "Can't open for writing\n";
}
else {
my @b = split('\s', $dataValue);
if (scalar(@b)>1) {
$position = $b[0];
$dataValue = $b[1];
}
my $loc_pos = $position+$span;
print "$chr\t$position\t$loc_pos\t$dataValue\t$strand\n";
$position = $position + $step;
}
}
close WIG;
exit;