forked from PASApipeline/PASApipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWigParser.pm
executable file
·197 lines (130 loc) · 3.56 KB
/
WigParser.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
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
package WigParser;
use strict;
use warnings;
use Carp;
####
sub new {
my $packagename = shift;
my ($wig_file) = @_;
unless ($wig_file) {
confess "Error, need wig_filename as parameter to constructor";
}
my $self = { wig_file => $wig_file,
wig_file_fh => undef,
contig_to_seek_pos_href => {},
};
bless ($self, $packagename);
$self->_init_wig_info();
return($self);
}
####
sub _init_wig_info {
my $self = shift;
my $wig_file = $self->{wig_file};
open (my $fh, $wig_file) or die "Error, cannot open file $wig_file";
$self->{wig_file_fh} = $fh;
while (<$fh>) {
if (/chrom=(\S+)/) {
my $scaff = $1;
my $filepos = tell($fh);
$self->{contig_to_seek_pos_href}->{$scaff} = $filepos;
}
}
return;
}
####
sub get_contig_list {
my $self = shift;
my @contigs = keys %{$self->{contig_to_seek_pos_href}};
return(@contigs);
}
####
sub get_wig_array {
my $self = shift;
my ($contig) = @_;
my $seekpos = $self->{contig_to_seek_pos_href}->{$contig};
unless (defined $seekpos) {
confess "Error, cannot find seek position entry for contig: $contig";
}
my $fh = $self->{wig_file_fh};
seek($fh, $seekpos, 0);
#print STDERR "-retrieving wig array for $contig at pos: $seekpos\n";
my @wig_array;
while (<$fh>) {
if (/chrom=/) { last; }
chomp;
my ($pos, $val, @rest) = split(/\s+/);
if ($pos > 1e12) {
confess "Error, position $pos is out of range of max contig position value set at 1e12";
}
$wig_array[$pos] = $val;
}
# fill in missing entries
foreach my $val (@wig_array) {
if (! defined ($val)) {
$val = 0;
}
}
return(@wig_array);
}
#####################################################################
## Static method that uses LOTS of memory (original implementation)
sub parse_wig {
my ($wig_file) = @_;
my %scaff_to_coverage;
print STDERR "-retrieving max positions per scaffold\n";
my %max_vals_for_scaffs = &_parse_max_scaff_lengths($wig_file);
print STDERR "-preallocating memory for coverage\n";
## preallocate arrays for coverage
my $sum_pos = 0;
foreach my $scaff (keys %max_vals_for_scaffs) {
my $max_val = $max_vals_for_scaffs{$scaff};
my @cov_vals = ();
$#cov_vals = $max_val;
for my $index (0..$max_val) {
$cov_vals[$index] = int(0);
}
$scaff_to_coverage{$scaff} = \@cov_vals;
$sum_pos += $max_val;
}
print STDERR "- $sum_pos bases represented\n";
print STDERR "-populating coverage data into memory.\n";
my $scaff = undef;
open (my $fh, $wig_file) or die "Error, cannot open file $wig_file";
while (<$fh>) {
if (/^track/) { next; };
if (/chrom=(\S+)/) {
$scaff = $1;
next;
}
chomp;
my ($pos, $val) = split(/\s+/);
$scaff_to_coverage{$scaff}->[$pos] = $val;
}
close $fh;
return(%scaff_to_coverage);
}
### Private
sub _parse_max_scaff_lengths {
my ($wig_file) = @_;
my %scaff_to_max_vals;
my $scaff = "";
my $counter = 0;
open (my $fh, $wig_file) or die "Error, cannot open file $wig_file";
while (<$fh>) {
if (/^track/) { next; };
if (/chrom=(\S+)/) {
$scaff = $1;
next;
}
chomp;
my ($pos, $val) = split(/\s+/);
$scaff_to_max_vals{$scaff} = $pos;
#print "scaff: $scaff\t$pos=> $val\n";
$counter++;
#if ($counter > 100000) { last; }
}
close $fh;
return(%scaff_to_max_vals);
}
1; #EOM