-
Notifications
You must be signed in to change notification settings - Fork 1
/
fastq_split.pl
executable file
·77 lines (64 loc) · 1.67 KB
/
fastq_split.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
#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use File::Spec;
my %uncompress = ('bz2' => 'bzcat',
'gz' => 'zcat');
# assume short read fastq with 4 lines per record!
my $size = 1_000_000;
my $outdir;
if (!defined @ARGV){
print "Please Provide
-s int The number of sequences per file [1_000_000]
-o dir The name of the directory to output the files
followed by a list of files to be split
Usage:
./fastq_split -o split_fq ~/somedir/someRandom.fq
./fastq_split -o split_fq ~/somedir/*fq
";
exit;
}
GetOptions('s|size:i' => \$size,
'o|outdir:s' => \$outdir,
);
my @files = @ARGV;
if (!-e $outdir){
system ("mkdir -p $outdir");
}
for my $file ( @files ) {
$file = File::Spec->rel2abs($file);
my ($vol,$dir,$fname) = File::Spec->splitpath($file);
my $odir = $outdir || $dir;
my $fh;
if( $fname =~ /(\S+)\.(gz|bz2)$/) {
open($fh => "$uncompress{$2} $file |") ||die $!;
$fname = $1;
} else {
open($fh => $file) || die "$file $!";
}
my @name = split(/\./,$fname);
my ($ext) = pop @name;
my $f = join(".",@name);
my $i = 0;
#open(my $ofh => sprintf(">$odir/%s.p%02d.%s",$f,$i++,$ext)) || die $!;
open(my $ofh => sprintf(">$odir/p%02d.%s.%s",$i++,$f,$ext)) || die $!;
my $n = 0;
while(<$fh>) {
unless( /^@/ ) {
chomp;
die("out of register, got line:\n$_\n but expected line to start with '\@'\n");
}
print $ofh $_;
for ( 0..2) {
$_ = <$fh>;
print $ofh $_;
}
if( ++$n >= $size ) {
close($ofh);
#open($ofh => sprintf(">$odir/%s.p%02d.%s",$f,$i++,$ext)) || die $!;
open($ofh => sprintf(">$odir/p%02d.%s.%s",$i++,$f,$ext)) || die $!;
$n= 0;
}
}
close($ofh);
}