forked from homeveg/nuctools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_chr_bed.pl
363 lines (290 loc) · 11.6 KB
/
extract_chr_bed.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/perl
=head1 NAME
extract_chr_bed.pl - Splits a standard bed file with mapped reads into smaller bed files per each chromosome
=head1 SYNOPSIS
perl -w extract_chr_bed.pl -in all_data.bed.gz -out output_name_template -p [<pattern>] [--help]
Required arguments:
--input | -in input BED or compressed BED.GZ file
--output | -out output BED file name template (each newly created bed file will have following name: chrN.output_name.bed.gz)
--pattern | -p chromosome name pattern (For example: chr|chromosome e.t.c.).
Options:
--chromosomes | -chrNr set the number of chromosomes to extract (default: all - extract all chromosomes)
--dir | -d output folder (default: script working directory)
--gzip | -z compress the output
--help | -h Help
define column numbers in the input BED file (Nr. of the very first column is 0):
-chr chromosome column Nr. (default: -chr 0)
Example usage:
extract 22 chromosomes from whole-genome compressed bed file. Chromosome ID contains "chr":
extract_chr_bed.pl --input=all_data.bed.gz --output=output_name --pattern="chr" --chromosomes=22
OR
extract_chr_bed.pl -in all_data.bed.gz -out output_name -p "chr" -chrNr 22
extract chromosome X only from uncompressed whole-genome BED file.
extract_chr_bed.pl --input=all_data.bed --output=output_name --pattern="chrX" --chromosomes=1
OR
extract_chr_bed.pl -in all_data.bed -out output_name -p "chrX" -chrNr 1
extract all chromosomes from uncompressed whole-genome BED file.
extract_chr_bed.pl --input=all_data.bed --output=output_name --pattern="chr" [--chromosomes=all]
OR
extract_chr_bed.pl -in all_data.bed -out output_name -p "chr"
=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_chr_bed.pl
extract_chr_bed.pl reads standard BED file and split it by chromosomes using specified pattern. Each newly generated file for each chromosome saved using output filename template provided as one of the required parameters. Input bed file can be compressed with gzip and have *.bed.gz extension. Output file automatically saved compressed as *.bed.gz. If no gzip found files will be saved in flat text format
=head1 AUTHORS
=over
=item
Yevhen Vainshtein <[email protected]>
=item
Vladimir Teif
=back
=head2 Last modified
14 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"; }
# Variables set in response to command line arguments
# (with defaults)
my $infile;
my $infile_name;
my $outfile;
my $pattern="chr";
my $needsHelp;
my $output_dir="./";
# columns (default columns for *.ext.bed files)
my $chromosome_col=0;
my $chrNr="all";
my $useGZ;
my $options_okay = &Getopt::Long::GetOptions(
'input|in=s' => \$infile_name,
'output|out=s' => \$outfile,
'dir|d=s' => \$output_dir,
'pattern|p=s' => \$pattern,
'chromosomes|chrNr=s' => \$chrNr,
# bed file columns
'chromosome_col|chrC=s' => \$chromosome_col,
'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";
}
$output_dir =~ s/(.*)\/$/$1/;
#create doutput dir if not found
if (! -d $output_dir) {
print STDERR "directory $output_dir not found. Creating...";
mkdir $output_dir or die "Error creating directory: $output_dir";
print STDERR "done\n";
}
print STDERR "input BED file: $infile_name\n";
print STDERR "output BED file: ",$output_dir,"/", $outfile,"\n";
print STDERR "chromosome ID template: $pattern\n";
print STDERR "total number of chromosome to extract: $chrNr\n";
print STDERR "chromosome column Nr.: $chromosome_col\n";
my @chromosomes;
my @FHs;
my (@OUT_FHs, %OUT_FHs);
if ( $infile_name =~ (/.*\.gz$/) ) {
$infile = IO::Uncompress::Gunzip->new( $infile_name )
or die "IO::Uncompress::Gunzip failed: $IO::Uncompress::Gunzip::GunzipError\n";
}
else { open( $infile, "<", $infile_name ) or die "error: $infile_name 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_tab='.*\t(.*)';
my $regex_split_newline='\n';
my $filesize = -s $infile_name; #determine file size in bytes
my $size_counter_step=int($filesize/100);
$filesize = int($filesize/1048576); # filesize in megabytes
print STDERR "Reading $infile_name file of $filesize MBs. Please wait...\n";
my $processed_memory_size = 0;
my $offset=0;
my $not_zero_counter=0;
my $string_counter=0;
my $BUFFER_SIZE = 1024*4;
if ($chrNr eq "all") {
print STDERR "extracting all chromosomes...\n";
while ((my $n = read($infile, $buffer, $BUFFER_SIZE)) !=0) {
if ($n >= $BUFFER_SIZE) {
$buffer .= <$infile>;
}
my @lines = split(/$regex_split_newline/o, $buffer);
# process each line in zone file
foreach my $line (@lines) {
my @newline1=split(/\t/, $line);
my $chr_name=$newline1[$chromosome_col];
#initialize file handle
if (not defined $OUT_FHs{$chr_name} ){
my $suffics;
if ($outfile) { $suffics = ".".$outfile.".bed"; }
else { $suffics = ".bed";}
my $out_filename_gz= $output_dir."/".$chr_name.$suffics.".gz";
my $out_filename= $output_dir."/".$chr_name.$suffics;
if ($useGZ) {
print STDERR "start writing data to file $out_filename_gz...\n";
$OUT_FHs{$chr_name} = new IO::Compress::Gzip ($out_filename_gz) or open ">$out_filename" or die "Can't open $out_filename for writing: $!\n";
}
else {
print STDERR "start writing data to file $out_filename...\n";
open $OUT_FHs{$chr_name}, '>', $out_filename or die "Can't open $out_filename for writing; $!\n";
}
}
my $OUT_FH = $OUT_FHs{$chr_name};
print $OUT_FH "$line\n";
}
$processed_memory_size += $n;
$offset += $n;
if(int($processed_memory_size/1048576)>= $filesize/10) {
print STDERR "."; $processed_memory_size=0;
}
undef @lines;
$buffer = "";
}
foreach my $OUTFH (%OUT_FHs) { close($OUTFH); } # close output files
print STDERR "done in ", time()-$timer2, " seconds\n";
}
elsif ($chrNr > 1) {
print STDERR "extracting $chrNr chromosomes...\n";
for (my $chrID=1; $chrID <= $chrNr; $chrID++ ) {
my $chrname = $pattern.$chrID;
my $FH = uc($chrname);
push (@chromosomes, $chrname);
push (@FHs, $FH);
}
for (my $i=0; $i<=$#chromosomes; $i++ ) {
push(@OUT_FHs, "*$FHs[$i]");
my $suffics;
if ($outfile) { $suffics = ".".$outfile.".bed"; }
else { $suffics = ".bed.gz";}
my $out_filename_gz= $output_dir.$chromosomes[$i].$suffics.".gz";
my $out_filename= $output_dir.$chromosomes[$i].$suffics;
if ($useGZ) {
print STDERR "start writing data to file $out_filename_gz...\n";
$OUT_FHs[$i] = new IO::Compress::Gzip ($out_filename_gz) or open ">$out_filename" or die "Can't open $out_filename for writing: $!\n";
}
else {
print STDERR "start writing data to file $out_filename...\n";
open $OUT_FHs[$i], '>', $out_filename or die "Can't open $out_filename for writing; $!\n";
}
}
while ((my $n = read($infile, $buffer, $BUFFER_SIZE)) !=0) {
if ($n >= $BUFFER_SIZE) {
$buffer .= <$infile>;
}
my @lines = split(/$regex_split_newline/o, $buffer);
# process each line in zone file
foreach my $line (@lines) {
for (my $i=0; $i<=$#chromosomes; $i++ ) {
my $chromosome_id = $chromosomes[$i];
my $OUTFH = $OUT_FHs[$i];
if ($line =~ m/$chromosome_id\s/) { print $OUTFH "$line\n"; last; }
}
}
$processed_memory_size += $n;
$offset += $n;
if(int($processed_memory_size/1048576)>= $filesize/10) {
print STDERR "."; $processed_memory_size=0;
}
undef @lines;
$buffer = "";
}
foreach my $OUTFH (@OUT_FHs) { close($OUTFH); } # close output files
print STDERR "done in ", time()-$timer2, " seconds\n";
}
else {
my $suffics;
if ($outfile) { $suffics = ".".$outfile.".bed"; }
else { $suffics = ".bed.gz";}
my $out_filename_gz= $pattern.$suffics.".gz";
my $out_filename= $pattern.$suffics;
my $OUT_FHs;
if ($useGZ) {
print STDERR "start writing data to file $out_filename_gz...\n";
$OUT_FHs = new IO::Compress::Gzip ($out_filename_gz) or open ">$out_filename" or die "Can't open $out_filename for writing: $!\n";
}
else {
print STDERR "start writing data to file $out_filename...\n";
open $OUT_FHs, '>', $out_filename or die "Can't open $out_filename for writing; $!\n";
}
while ((my $n = read($infile, $buffer, $BUFFER_SIZE)) !=0) {
if ($n >= $BUFFER_SIZE) {
$buffer .= <$infile>;
}
my @lines = split(/$regex_split_newline/o, $buffer);
# process each line in zone file
foreach my $line (@lines) {
if ($line =~ m/$pattern\s/) { print $OUT_FHs "$line\n"; }
}
$processed_memory_size += $n;
$offset += $n;
if(int($processed_memory_size/1048576)>= $filesize/10) {
print STDERR "."; $processed_memory_size=0;
}
undef @lines;
$buffer = "";
}
print STDERR "done in ", time()-$timer2, " seconds\n";
close($OUT_FHs);
}
close($infile);
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_name ) {
pod2usage(
-exitval => 2,
-verbose => 1,
-message => "Cannot find input BED file $infile_name: $!\n"
);
}
if ( ! $pattern ) {
pod2usage(
-exitval => 2,
-verbose => 1,
-message => "please specify chromosome name template \n"
);
}
}