| 1 |
#!/usr/bin/perl |
| 2 |
use strict; |
| 3 |
use Getopt::Long; |
| 4 |
|
| 5 |
# Author: Erik Aronesty ([email protected]) |
| 6 |
# Outputs a random sampled fastq or fasta file, from an input fastq |
| 7 |
# Copyright (c) 2011 Expression Analysis |
| 8 |
|
| 9 |
|
| 10 |
# This program is free software: you can redistribute it and/or modify |
| 11 |
# it under the terms of the GNU General Public License as published by |
| 12 |
# the Free Software Foundation, either version 3 of the License, or |
| 13 |
# (at your option) any later version. |
| 14 |
# |
| 15 |
# This program is distributed in the hope that it will be useful, |
| 16 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
# GNU General Public License for more details. |
| 19 |
# |
| 20 |
# You should have received a copy of the GNU General Public License |
| 21 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
|
| 23 |
|
| 24 |
my $cnt; |
| 25 |
my $fasta; |
| 26 |
my $out; |
| 27 |
my $seed=1; |
| 28 |
my $window; |
| 29 |
my $always; |
| 30 |
my $append; |
| 31 |
my $pct; |
| 32 |
GetOptions("count=i"=>\$cnt, "pct=f"=>\$pct, "fasta"=>\$fasta, "out=s"=>\$out, "seed=i"=>\$seed, "window=i"=>\$window, "always|a"=>\$always, "append|A"=>\$append) || die usage(); |
| 33 |
if ($window < $cnt) { |
| 34 |
$window = $cnt * 5; |
| 35 |
$window = 100000 if $window < 100000; |
| 36 |
} |
| 37 |
|
| 38 |
if ($seed) { |
| 39 |
srand($seed); |
| 40 |
} |
| 41 |
|
| 42 |
die usage() unless $cnt>0||$pct>0; |
| 43 |
|
| 44 |
my $in = shift; |
| 45 |
my $mate = shift; |
| 46 |
my $mate2 = shift; |
| 47 |
|
| 48 |
die "Can't see $in\n" unless -e $in; |
| 49 |
die "Can't see $mate\n" unless !$mate || -e $mate; |
| 50 |
die "Can't see $mate2\n" unless !$mate2 || -e $mate2; |
| 51 |
|
| 52 |
my $s = -s $in; |
| 53 |
my $sm = -s $mate; |
| 54 |
my $sm2 = -s $mate2; |
| 55 |
|
| 56 |
die "Need -out <prefix> for paired-end" if $mate && !$out; |
| 57 |
|
| 58 |
my $suff; |
| 59 |
if ($out =~ s/\%(.*)//) { |
| 60 |
$suff = $1; |
| 61 |
} |
| 62 |
$out =~ s/_$//; |
| 63 |
|
| 64 |
my $gzmeth = $suff =~ /\.gz/ ? "|gzip -c " : ""; |
| 65 |
|
| 66 |
open(IN, $in=~/\.gz$/?"gunzip -c $in|":$in) || die; |
| 67 |
|
| 68 |
$append = $append ? ">>" : ">"; |
| 69 |
|
| 70 |
if ($mate) { |
| 71 |
open(MI, $mate=~/\.gz$/?"gunzip -c $mate|":$mate) || die; |
| 72 |
open(MI2, $mate2=~/\.gz$/?"gunzip -c $mate2|":$mate2) if $mate2; |
| 73 |
open(O1, "$gzmeth$append${out}_1$suff") || die; |
| 74 |
open(O2, "$gzmeth$append${out}_2$suff") || die; |
| 75 |
if ($mate2) { |
| 76 |
open(O3, "$gzmeth$append${out}_3$suff") || die; |
| 77 |
} |
| 78 |
} else { |
| 79 |
my $gzmeth = $out =~ /\.gz/ ? "|gzip -c " : ""; |
| 80 |
if ($out) { |
| 81 |
open(O1, "$gzmeth$append$out"); |
| 82 |
} else { |
| 83 |
open(O1, ">&STDOUT"); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| •87 |
my $lc = 0+`ea-alc -o $in`; |
| 88 |
my $stats = $in; $stats =~ s/\.gz$//; $stats .= ".stats"; |
| 89 |
if (-e $stats) { |
| 90 |
my $rlc = `grep ^reads $stats | cut -f 2`+0; |
| 91 |
$lc = $rlc if $rlc; |
| 92 |
} else { |
| 93 |
if ($in =~ /gz$/) { |
| 94 |
$lc *= .90; # lower guess |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if (!$always && ($cnt > $lc/2)) { |
| 99 |
$cnt *= 4; |
| 100 |
warn "Source is too small relative to count requested, just returning tail -$cnt\n"; |
| 101 |
if ($mate) { |
| 102 |
if ($in=~/gz$/) { |
| 103 |
system("gunzip -c $in | head -$cnt $gzmeth $append ${out}_1$suff"); |
| 104 |
} else { |
| 105 |
system("tail -$cnt $in $gzmeth $append ${out}_1$suff"); |
| 106 |
} |
| 107 |
if ($mate=~/gz$/) { |
| 108 |
system("gunzip -c $mate | head -$cnt $gzmeth $append ${out}_2$suff"); |
| 109 |
} else { |
| 110 |
system("tail -$cnt $mate $gzmeth $append ${out}_2$suff"); |
| 111 |
} |
| 112 |
if ($mate2=~/gz$/) { |
| 113 |
system("gunzip -c $mate2 | head -$cnt $gzmeth $append ${out}_3$suff"); |
| 114 |
} else { |
| 115 |
system("tail -$cnt $mate2 $gzmeth $append ${out}_3$suff") if ($mate2); |
| 116 |
} |
| 117 |
exit 0; |
| 118 |
} else { |
| 119 |
if ($out) { |
| 120 |
$gzmeth = $out =~ /\.gz/ ? "|gzip -c " : ""; |
| 121 |
$out = "$gzmeth $append $out"; |
| 122 |
} |
| 123 |
if ($in=~/gz$/) { |
| 124 |
exec("gunzip -c $in | head -$cnt $out"); |
| 125 |
die("Exec failed : $!\n"); |
| 126 |
} else { |
| 127 |
exec("tail -$cnt $in $out"); |
| 128 |
die("Exec failed : $!\n"); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
# reads to sample from....whole file or a smaller part? |
| 134 |
$window = $lc if $window > $lc; |
| 135 |
|
| 136 |
my $fudge = 1.5; # top weighted |
| 137 |
$fudge = 1.2 if $always; # less top-weighted |
| 138 |
|
| 139 |
# larger = more chance to keep read |
| 140 |
my $prob; |
| 141 |
|
| 142 |
if ($pct) { |
| 143 |
$prob = $pct/100; |
| 144 |
$cnt = 10000000000; |
| 145 |
} else { |
| 146 |
$prob = ($cnt*$fudge)/$window; |
| 147 |
} |
| 148 |
|
| 149 |
# we used to seek ... but this broke too much ... if you want to fix... fix randomFQ-broken ... |
| 150 |
if (!$always && !$pct) { |
| 151 |
# skip some reads at the beginning |
| 152 |
my $skip = ($lc-$window) / 10; |
| 153 |
$skip = 200000 if $skip > 200000; |
| 154 |
for (my $i=0;$i<$skip;++$i) { |
| 155 |
scalar <IN>; scalar <IN>; scalar <IN>; scalar <IN>; |
| 156 |
scalar <MI>; scalar <MI>; scalar <MI>; scalar <MI>; |
| 157 |
scalar <MI2>; scalar <MI2>; scalar <MI2>; scalar <MI2>; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
while ($cnt > 0) { |
| 162 |
if (rand() > $prob) { |
| 163 |
# discard |
| 164 |
<IN>; <IN>; <IN>; <IN>; |
| 165 |
<MI>; <MI>; <MI>; <MI>; |
| 166 |
<MI2>; <MI2>; <MI2>; <MI2>; |
| 167 |
next; |
| 168 |
} |
| 169 |
|
| 170 |
# id |
| 171 |
my $i = <IN>; |
| 172 |
if (!$i) { |
| 173 |
$cnt = 0; |
| 174 |
last; |
| 175 |
} |
| 176 |
my $i2 = <MI> if $mate; |
| 177 |
my $i3 = <MI2> if $mate2; |
| 178 |
|
| 179 |
# read |
| 180 |
my $r = <IN>; |
| 181 |
my $r2 = <MI> if $mate; |
| 182 |
my $r3 = <MI2> if $mate2; |
| 183 |
|
| 184 |
if ($fasta) { |
| 185 |
# only need id and read |
| 186 |
$i=~ s/^\@/>/; |
| 187 |
$i2=~ s/^\@/>/; |
| 188 |
$i3=~ s/^\@/>/; |
| 189 |
print O1 "$i$r"; |
| 190 |
print O2 "$i2$r2" if $mate; |
| 191 |
print O3 "$i3$r3" if $mate2; |
| 192 |
<IN>;<IN>; |
| 193 |
<MI>;<MI>; |
| 194 |
} else { |
| 195 |
# print id and read |
| 196 |
print O1 "$i$r"; |
| 197 |
print O2 "$i2$r2"; |
| 198 |
print O3 "$i3$r3"; |
| 199 |
# copy comment and quality |
| 200 |
print O1 scalar <IN>; |
| 201 |
print O2 scalar <MI> if $mate; |
| 202 |
print O3 scalar <MI2> if $mate2; |
| 203 |
print O1 scalar <IN>; |
| 204 |
print O2 scalar <MI> if $mate; |
| 205 |
print O3 scalar <MI2> if $mate2; |
| 206 |
} |
| 207 |
--$cnt; |
| 208 |
} |
| 209 |
|
| 210 |
close IN; |
| 211 |
|
| 212 |
sub usage() { |
| 213 |
return <<EOF |
| 214 |
usage: $0 (-c <count> | -p <pct>) [-fasta] [-out <prefix>] [-seed <int>] <input-fastq> [<input-2> [<index-3>] ] |
| 215 |
|
| 216 |
Returns <count> number of random entries from the input fastq. |
| 217 |
|
| 218 |
Output is fastq, unless you specify -fasta |
| 219 |
|
| 220 |
If the -out parameter ends in .gz, the result is gzipped in-place. |
| 221 |
|
| 222 |
-p returns a % of total reads, -c returns a fixed count. |
| 223 |
|
| 224 |
SINGLE END: |
| 225 |
|
| 226 |
Outputs to standard output, unless -out <file> is specified. |
| 227 |
|
| 228 |
PAIRED END: |
| 229 |
|
| 230 |
Pass 2 (or 3) files as input, -out is required. |
| 231 |
|
| 232 |
If the paired-end output contains a "%" sign, it is replaced with the 1 & 2 for paired-end. |
| 233 |
|
| 234 |
IE: -o output_%.fastq.gz |
| 235 |
|
| 236 |
Otherwise it's jsut output_1 and output_2 |
| 237 |
|
| 238 |
*** If one file is an indexed read, it has to be the 3rd file (for now). |
| 239 |
EOF |
| 240 |
} |
| 241 |
|
| 242 |
sub max { |
| 243 |
return $_[0] > $_[1] ? $_[0] : $_[1]; |
| 244 |
} |
| 245 |
|