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
|
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long qw(:config no_ignore_case bundling);
my $usage = <<_EOUSAGE_;
##########################################################
#
# -I <string> input.fq
#
# --ignore_dirty ignores poorly formed entries
#
# -a <int> append "/num" to the accession name.
#
# -v verbose
#
###########################################################
_EOUSAGE_
;
my $inputFile;
my $ignore_dirty = 0;
my $append_num;
my $VERBOSE = 0;
&GetOptions( 'I=s' => \$inputFile,
'ignore_dirty' => \$ignore_dirty,
'a=i' => \$append_num,
'v' => \$VERBOSE,
);
unless ($inputFile) {
die $usage;
}
my $fh;
if ($inputFile =~ /\.gz$/) {
open ($fh, "gunzip -c $inputFile | ") or die $!;
}
else {
open ($fh, $inputFile) or die "Error, cannot open $inputFile";
}
my $counter = 0;
my $num_clean = 0;
my $num_dirty = 0;
my @rec;
my $line = <$fh>;
while ($line) {
if ($line =~ /^\@/) {
$counter++;
print STDERR "\r[$counter] [$num_clean clean] [$num_dirty dirty] " if ($counter % 10000 == 0 && $VERBOSE);
push (@rec, $line);
$line = <$fh>;
for (1..3) {
push (@rec, $line);
$line = <$fh>;
}
my $record_text = join("", @rec);
my $header = shift @rec;
my $seq = shift @rec;
my $qual_header = shift @rec;
my $qual_line = shift @rec;
chomp $header;
chomp $seq if $seq;
chomp $qual_header if $qual_header;
chomp $qual_line if $qual_line;
my @header_pts = split(/\s+/, $header);
if (scalar @header_pts > 1) {
$header = shift @header_pts;
}
if ($header && $seq && $qual_header && $qual_line &&
$qual_header =~ /^\+/ && length($seq) == length($qual_line)) {
# can do some more checks here if needed to be sure that the lines are formatted as expected.
$header =~ s/^\@//;
## convert casava format over
my @pts = split(/\s+/, $header);
if (scalar @pts > 1 && $pts[1] =~ /^([12]):/) {
my $val = $1;
$header = $pts[0] . "/$val";
}
elsif ($append_num) {
$header .= "/$append_num";
}
print "$header\t$seq\t$qual_line\n";
$num_clean++;
}
else {
$num_dirty++;
unless ($ignore_dirty) {
die "Error, improperly formatted entry:\n\n$record_text ";
}
}
@rec = ();
} else {
$line = <$fh>;
}
}
exit(0);
|