-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnormalize_gff.pl
executable file
·192 lines (146 loc) · 5.17 KB
/
normalize_gff.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
#!/usr/bin/env perl
use warnings;
use strict;
use Data::Dumper;
use Carp;
use Getopt::Long;
use Pod::Usage;
use version; our $VERSION = qv('0.0.1');
use FindBin;
use lib "$FindBin::Bin/DZLab-Tools/lib";
use DZLab::Tools::RunUtils;
GetOptions(
\%ARGV,
'input|i=s', 'output|o=s', 'error|e=s',
'gff-a|a=s', 'gff-b|b=s', 'scoring|c=s',
'feature|f=s', 'dot-as-zero|0',
_meta_options( \%ARGV ),
) and (@ARGV or $ARGV{input} or @ARGV{qw/gff-a gff-b/}) or pod2usage( -verbose => 1 );
my ( $INH, $OUTH, $ERRH ) = _prepare_io( \%ARGV, \@ARGV );
my %scoring_dispatch = (
log2_ratio => \&log2_ratio,
difference => sub {
(q{.} eq $_[0] or q{.} eq $_[1])
? q{.}
: ($_[0] - $_[1])
},
multiplication => sub {
(q{.} eq $_[0] or q{.} eq $_[1])
? q{.}
: ($_[0] * $_[1])
},
);
my $gff_a_iterator = make_gff_iterator( parser => \&gff_read, file => $ARGV{'gff-a'} );
my $gff_b_iterator = make_gff_iterator( parser => \&gff_read, file => $ARGV{'gff-b'} );
GFF:
while ( defined( my $gff_a = $gff_a_iterator->() )
and defined( my $gff_b = $gff_b_iterator->() ) ) {
next GFF unless 'HASH' eq ref $gff_a and 'HASH' eq ref $gff_b;
die "GFF record mismatch: ", Dumper [$gff_a, $gff_b]
unless @{$gff_a}{qw/seqname start end/} eq @{$gff_b}{qw/seqname start end/};
if ($ARGV{'dot-as-zero'}) {
$gff_a->{score} = 0 if $gff_a->{score} eq q{.};
$gff_b->{score} = 0 if $gff_b->{score} eq q{.};
}
print $OUTH join( "\t",
$gff_a->{seqname},
$gff_a->{source},
($ARGV{feature} // sprintf( "%s/%s", $gff_a->{feature}, $gff_b->{feature} )),
$gff_a->{start},
$gff_a->{end},
(sprintf( "%g" , $scoring_dispatch{$ARGV{scoring}}->( $gff_a->{score}, $gff_b->{score} ))),
$gff_a->{strand},
$gff_a->{frame},
(sprintf( "%s; %s", $gff_a->{attribute}, $gff_b->{attribute})),
), "\n";
}
sub log2_ratio {
my ($score_a, $score_b) = @_;
return q{.} if q{.} eq $score_a or q{.} eq $score_b;
return 'NaN' unless 0 < $score_a and 0 < $score_b;
return (log( $score_a/$score_b ) / log( 2 ));
}
sub make_gff_iterator {
my %options = @_;
my $parser = $options{parser};
my $file = $options{file};
croak
"Need parser function reference and file name or handle to build iterator"
unless $parser
and ref $parser eq 'CODE'
and (defined $file and -e $file);
open my $GFF_HANDLE, '<', $file
or croak "Can't read $file: $!";
return sub {
$parser->( scalar <$GFF_HANDLE> );
};
}
sub gff_read {
return [] if $_[0] =~ m/^
\s*
\#+
/mx;
my ($seqname, $source, $feature, $start, $end,
$score, $strand, $frame, $attribute
) = split m/\t/xm, shift || return;
$attribute =~ s/[\r\n]//mxg;
return {
'seqname' => lc $seqname,
'source' => $source,
'feature' => $feature,
'start' => $start,
'end' => $end,
'score' => $score,
'strand' => $strand,
'frame' => $frame,
'attribute' => $attribute
};
}
__DATA__
__END__
=head1 NAME
normalize_gff.pl - Normalize one GFF file by another
=head1 SYNOPSIS
normalize_gff.pl [OPTION]... [[-i] FILE]...
normalize_gff.pl -a exp.gff -b control.gff -c log2_ratio -f log2 -o norm_exp.gff
=head1 DESCRIPTION
Long description
=head1 OPTIONS
-a. gff-a
-b, gff-b
-c, scoring 'log2_ratio' or 'difference'
-f, feature
-o, --output <string> output filename (STDOUT)
-e, --error <string> output error filename (STDERR)
--verbose [integer] print increasingly verbose error messages
--quiet print no diagnostic or warning messages
--version print current version
--license print author's contact and copyright information
--help print this information
--manual print the plain old documentation page
=head1 VERSION
0.0.1
=head1 REVISION
$Rev: $:
$Author: $:
$Date: $:
$HeadURL: $:
$Id: $:
=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