forked from homeveg/nuctools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_rows_occup.pl
231 lines (185 loc) · 6.53 KB
/
extract_rows_occup.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
#!/usr/bin/perl
=head1 NAME
extract_rows_occup.pl - Extracts occupancy values from *.OCC.GZ file for a given genomic interval and save it as a compressed *.OCC.GZ file
=head1 SYNOPSIS
perl -w extract_rows_occup.pl --input=<in.bed> --output=<out.bed> --start=<selected region start> --end=<selected region end> --coordsCol=<coordinates Col.Nr.> --help [--help]
Required arguments:
--input | -in path to directory with aggregate profiles
--output | -out output table file name
--start | -s chromosomal coordinate of selected region start
--end | -e chromosomal coordinate of selected region end
Options:
--gzip | -z compress the output
--coordsCol | -cC chromosomal coordinate column Nr.(default: 0)
--help | h Help
Example usage:
perl -w extract_rows_occup.pl --input=in.bed.gz --output=out.bed.gz
OR
perl -w extract_rows_occup.pl -in in.bed.gz -out out.bed.gz
=head1 DESCRIPTION
=head2 NucTools 1.0 package.
NucTools is a software package for analysis of chromatin feature occupancy profiles from high-throughput sequencing data
=head2 extract_rows_occup.pl
extract_rows_occup.pl extracts occupancy values from *.OCC.GZ file for a given genomic interval and save it as a compressed *.OCC.GZ file
=head1 AUTHORS
=over
=item
Yevhen Vainshtein <[email protected]>
=item
Vladimir Teif
=back
=head2 Last modified
18 October 2016
=head1 LICENSE
Copyright (C) 2012-2016 Yevhen Vainshtein, Vladimir Teif
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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
=cut
use strict;
use Getopt::Long;
use Pod::Usage;
# optional gzip support if modules are installed
my ($ModuleGzipIsLoaded, $ModuleGunzipIsLoaded);
BEGIN { $ModuleGunzipIsLoaded = eval "require IO::Uncompress::Gunzip; 1"; }
BEGIN { $ModuleGzipIsLoaded = eval "require IO::Compress::Gzip; IO::Compress::Gzip->import( qw[gzip] );1"; }
my $infile;
my $outfile;
my $start_interval;
my $end_interval;
my $coordsCol=0;
my $useGZ;
my $needsHelp;
my $options_okay = &Getopt::Long::GetOptions(
'input|in=s' => \$infile,
'output|out=s' => \$outfile,
'start|s=s' => \$start_interval,
'end|e=s' => \$end_interval,
'coordsCol|cC=s' => \$coordsCol,
'gzip|z' => \$useGZ,
'help|h' => \$needsHelp
);
# Check to make sure options are specified correctly and files exist
&check_opts();
# check if GZIP is loaded
if ( ((!$ModuleGzipIsLoaded) or (!$ModuleGunzipIsLoaded)) and ($useGZ) ) {
print STDERR "Can't work with GZIP: IO::Compress::Gzip is not on PATH\n";
exit;
}
elsif ( (($ModuleGzipIsLoaded) and ($ModuleGunzipIsLoaded)) and ($useGZ) ) {
print STDERR "ZGIP support enabled\n";
}
else {
print STDERR "ZGIP support disabled\n";
}
# open pipe to Gzip or open text file for writing
my ($gz_out_file,$out_file,$OUT_FHs);
$out_file = $outfile;
if ($useGZ) {
$out_file =~ s/(.*)\.gz$/$1/;
$gz_out_file = $out_file.".gz";
$OUT_FHs = new IO::Compress::Gzip ($gz_out_file) or open ">$out_file" or die "Can't open $out_file for writing: $!\n";
}
else {
open $OUT_FHs, '>', $outfile or die "Can't open $outfile for writing; $!\n";
}
# open occupancy file
my $inFH;
if ( $infile =~ (/.*\.gz$/) ) {
$inFH = IO::Uncompress::Gunzip->new( $infile )
or die "IO::Uncompress::Gunzip failed: $IO::Uncompress::Gunzip::GunzipError\n";
}
else { open( $inFH, "<", $infile ) or die "error: $infile cannot be opened:$!"; }
my $buffer = "";
my $sz_buffer = 0;
my $timer2 = time();
# counter for the markers we see
my $marker_count = 0;
my $regex_split_newline='\n';
my $filesize_in_bytes = -s $infile; #determine file size in bytes
my $size_counter_step=int($filesize_in_bytes/10);
my $filesize = int($filesize_in_bytes/1048576); # filesize in megabytes
print STDERR "Reading $infile file of $filesize MBs. Please wait...\n";
my $processed_memory_size = 0;
my $offset=0;
my $BUFFER_SIZE = 1024;
my $old_coordinate=1;
while ((my $n = read($inFH, $buffer, $BUFFER_SIZE)) !=0) {
if (($n >= $BUFFER_SIZE) or (($n == $filesize_in_bytes))) {
$buffer .= <$inFH>;
}
my @lines = split(/$regex_split_newline/o, $buffer);
# process each line in zone file
foreach my $line (@lines) {
chomp($line);
my @newline=split(/\s/, $line);
if (($newline[$coordsCol] >= $start_interval) and ($newline[$coordsCol] <= $end_interval)) {
print $OUT_FHs join("\t",@newline), "\n";
#print join("\t",@newline), "\n";
}
elsif ($newline[$coordsCol] > $end_interval) {
last;
}
}
$processed_memory_size += $n;
$offset += $n;
if(int($processed_memory_size/1048576)>= $size_counter_step) {
print STDERR "."; $processed_memory_size=0;
}
undef @lines;
$buffer = "";
}
close($inFH);
close($OUT_FHs);
print STDERR "done!\nJob finished in ", time()-$timer2, " seconds.\n";
exit;
#--------------------------------------------------------------------------
# Check for problem with the options or if user requests help
sub check_opts {
if ($needsHelp) {
pod2usage( -verbose => 2 );
}
if ( !$options_okay ) {
pod2usage(
-exitval => 2,
-verbose => 1,
-message => "Error specifying options."
);
}
if ( ! -e $infile ) {
pod2usage(
-exitval => 2,
-verbose => 1,
-message => "Cannot find input BED file $infile: $!\n"
);
}
if (!$outfile ) {
pod2usage(
-exitval => 2,
-verbose => 1,
-message => "please specify output BED file name\n"
);
}
if (!$start_interval ) {
pod2usage(
-exitval => 2,
-verbose => 1,
-message => "please specify starting coordinate of the interval\n"
);
}
if (!$end_interval ) {
pod2usage(
-exitval => 2,
-verbose => 1,
-message => "please specify end coordinate of the interval\n"
);
}
}