-
Notifications
You must be signed in to change notification settings - Fork 7
/
Wiggle.pm
114 lines (82 loc) · 1.97 KB
/
Wiggle.pm
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
#
#===============================================================================
#
# FILE: Wiggle.pm
#
# DESCRIPTION: Package to handle Wiggle file
# BUGS: ---
# NOTES: The package is spun off from the AnnotationIO.pm
# AUTHOR: Chaolin Zhang (cz), czhang@rockefeller.edu
# COMPANY: Rockefeller University
# VERSION: 1.0
# CREATED: 12/17/10
# REVISION: ---
#===============================================================================
package Wiggle;
require Exporter;
our $VERSION = 1.01;
@ISA = qw (Exporter);
@EXPORT = qw (
readBedGraphFile
writeBedGraphFile
);
=head1 NAME
Wiggle - read and write wiggle like files
subroutines starting with a hyphen should not be called outside
=cut
use strict;
use warnings;
use Data::Dumper;
use Carp ();
=head2 readBedGraphFile
Note: original name: readWigFile
my $ret = readBedGraphFile ($inFile)
=cut
sub readWigFile
{
Carp::croak "obsolete subroutine, call readBedGraphFile\n";
}
sub readBedGraphFile
{
my $in = $_[0];
my $fin;
my @ret;
open ($fin, "<$in") || Carp::croak "can not open file $in to read\n";
while (my $line = <$fin>)
{
chomp $line;
next if $line =~/^\s*$/;
next if $line =~/^\#/;
my ($chrom, $chromStart, $chromEnd, $score) = split (/\s/, $line);
push @ret, {chrom=>$chrom, chromStart=>$chromStart, chromEnd=>$chromEnd-1, score=>$score};
}
close ($fin);
return \@ret;
}
=head2 writeBedGraphFile
Note: original name: writeWigFile
writeBedGraphFile (\@regions, $outFile, $headerLine);
=cut
sub writeWigFile
{
Carp::croak "obsolete subroutine, call writeBedGraphFile\n";
}
sub writeBedGraphFile
{
my ($regions, $out, $header) = @_;
my $fout;
open ($fout, ">$out") || Carp::croak "cannot open file $out to write\n";
if ($header ne '')
{
print $fout $header, "\n";
}
foreach my $r (@$regions)
{
print $fout join ("\t", $r->{"chrom"},
$r->{"chromStart"},
$r->{"chromEnd"} + 1,
$r->{"score"}), "\n";
}
close ($fout);
}
1;