-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcount_adjacencies.pl
executable file
·252 lines (192 loc) · 6.67 KB
/
count_adjacencies.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env perl
# ___UNDOCUMENTED___
use warnings;
use strict;
use Data::Dumper;
use Carp;
use Getopt::Long;
use Pod::Usage;
use List::Util qw/sum/;
use FindBin;
use lib "$FindBin::Bin/DZLab-Tools/lib";
use DZLab::Tools::Fasta;
my $GFF_DATA = 'ARGV';
my $min_score = 0.10;
my $min_total = 10;
my $min_gap = 1;
my $normalize = q{};
my $sort = 0;
my $output;
# Grabs and parses command line options
my $result = GetOptions (
'min-score|s=f' => \$min_score,
'min-total|t=i' => \$min_total,
'min-gap|g=i' => \$min_gap,
'normalize|n=s' => \$normalize,
'sort|r' => \$sort,
'output|o=s' => \$output,
'verbose|v' => sub { use diagnostics; },
'quiet|q' => sub { no warnings; },
'help|h' => sub { pod2usage ( -verbose => 1 ); },
'manual|p' => sub { pod2usage ( -verbose => 2 ); }
);
# Check required command line parameters
pod2usage ( -verbose => 1 )
unless @ARGV and $result and $min_score > 0;
if ($output) {
open my $USER_OUT, '>', $output or croak "Can't open $output for writing: $!";
select $USER_OUT;
}
if ($sort) {
my $sorted_filename = sort_gff (@ARGV);
$GFF_DATA = undef;
open $GFF_DATA, '<', $sorted_filename;
}
my $reference = slurp_fasta ($normalize, {-l => 1});
my %buffer = ();
my %counts = ();
while (<$GFF_DATA>) {
next if m/^#.*$|^\s*$/;
chomp;
s/[\n\r]//;
my $current = gff_read ($_);
my $seqid = $current->{seqname};
my $score = $current->{score};
my $total = sum map { (split /=/, $_)[1] } split /;/, $current->{attribute};
# initialize buffer
if (!exists $buffer{$seqid} or !@{$buffer{$seqid}}) {
push @{$buffer{$seqid}}, [$score, $total, $current->{end}]
if $score ne q{.} and $score > $min_score and $total > $min_total;
}
# extend buffer if possible
elsif ($current->{start} - $buffer{$seqid}->[-1]->[2] <= $min_gap
and $score ne q{.} and $score > $min_score and $total > $min_total) {
push @{$buffer{$seqid}}, [$score, $total, $current->{end}];
}
# flush buffer
else {
flush_buffer (\%buffer, \%counts, $seqid);
}
}
my %full_counts = ();
# post-process histogram counts, one chromosome at a time
for my $seqid (keys %counts) {
# flush whatever's left over, if anything
flush_buffer (\%buffer, \%counts, $seqid)
if @{$buffer{$seqid}};
# slide through each bin size in ascending sorted order
for my $size (sort { $a <=> $b } keys %{$counts{$seqid}}) {
# find number of hits per bin, mean score and deviations
my $number = scalar @{$counts{$seqid}{$size}{mean}};
# my $mean = (sum @{$counts{$seqid}{$size}{mean}}) / $number;
# my $var = (sum map { ($_ - $mean) ** 2 }
# @{$counts{$seqid}{$size}{mean}}) / ($number - 1 || 1);
# my $std = $var ** 1/2;
# normalize number of hits with bulk size of genome
if ($normalize and $reference and exists $reference->{$seqid}) {
$number /= (length $reference->{$seqid});
$number = sprintf ("%g", $number * 1000000);
}
# print join ("\t",
# $size,
# $number,
# sprintf ("%g", $mean),
# sprintf ("%g", $var),
# sprintf ("%g", $std),
# ), "\n";
$full_counts{$size} += $number;
}
}
#print "#$seqid, counts normalized per 1000bp\n";
#print "#Window size\tWindow number\tMean score\tVariance\tStd\n";
for my $size (sort {$a <=> $b } keys %full_counts) {
print $size, "\t", $full_counts{$size}, "\n";
}
sub flush_buffer {
# alters $buffer and $counts in place
my ($buffer, $counts, $seqid) = @_;
my $size = scalar @{$buffer->{$seqid}};
my $mean = (sum map { $_->[0] } @{$buffer->{$seqid}}) / $size || 1;
# my $var = (sum map { ($_->[0] - $mean) ** 2 } @{$buffer->{$seqid}}) / ($size - 1 || 1);
push @{$counts->{$seqid}{$size}{mean}}, $mean;
# push @{$counts->{$seqid}{$size}{var }}, $var;
# push @{$counts->{$seqid}{$size}{std }}, $var ** 1/2;
@{$buffer->{$seqid}} = ();
}
sub gff_read {
my ($seqname, $source, $feature, $start, $end, $score, $strand, $frame, $attribute)
= split /\t/, shift;
return {
'seqname' => $seqname,
'source' => $source,
'feature' => $feature,
'start' => $start,
'end' => $end,
'score' => $score,
'strand' => $strand,
'frame' => $frame,
'attribute'=> $attribute
};
}
sub sort_gff {
use File::Temp qw/tempfile/;
my @gff_files = @_;
my @gff = ();
my ($tmp_fh, $tmp_filename) = tempfile();
BATCH:
for my $gff_file (@gff_files) {
open my $GFF, '<', $gff_file
or croak "Can't read $gff_file: $!";
while (<$GFF>) {
next if ($_ =~ m/^#.*$|^\s*$/);
chomp;
push @gff, $_;
}
close $GFF
or carp "Can't close $gff_file: $!";
}
map { print $tmp_fh $_, "\n" }
sort {
(split /\t/, $a)[0] cmp (split /\t/, $b)[0] or
(split /\t/, $a)[3] <=> (split /\t/, $b)[3]
}
@gff;
return $tmp_filename;
}
__END__
=head1 NAME
name.pl - Short description
=head1 SYNOPSIS
=head1 DESCRIPTION
=head1 OPTIONS
name.pl [OPTION]... [FILE]...
-o, --output filename to write results to (defaults to STDOUT)
-v, --verbose output perl's diagnostic and warning messages
-q, --quiet supress perl's diagnostic and warning messages
-h, --help print this information
-m, --manual print the plain old documentation page
=head1 REVISION
Version 0.0.1
$Rev: 249 $:
$Author: psilva $:
$Date: 2010-01-11 21:24:34 -0800 (Mon, 11 Jan 2010) $:
$HeadURL: http://dzlab.pmb.berkeley.edu/svn/bisulfite/trunk/count_adjacencies.pl $:
$Id: count_adjacencies.pl 249 2010-01-12 05:24:34Z psilva $:
=head1 AUTHOR
Pedro Silva <[email protected]/>
Zilberman Lab <http://dzlab.pmb.berkeley.edu/>
Plant and Microbial Biology Department
College of Natural Resources
University of California, Berkeley
=head1 COPYRIGHT
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=cut