| 1 | #!/usr/bin/env perl | ||
| 2 | use strict; | ||
| 3 | use Carp; | ||
| 4 | use Pod::Usage; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | fasta-shuffle-notryptic.pl - shuffle each sequence, without any original tryptic peptide | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | Reads input fasta file and produce a shuffle databank & avoid known cleaved peptides: shuffle sequence but avoid producing known tryptic peptides | ||
| 13 | |||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | #shuffle each sequence | ||
| 18 | fasta-shuffle-notryptic.pl --in=/tmp/uniprot_sprot.fasta | ||
| 19 | |||
| 20 | |||
| 21 | #to limit memory usage, one can use CRC code (--crcsize will | ||
| 22 | ./fasta-shuffle-notryptic.pl --ac-prefix=DECOY_ --in=/home/alex/tmp/a.fasta --out=/tmp/a.fasta --crcsize=33 -v --norandom | ||
| 23 | |||
| 24 | =head1 ARGUMENTS | ||
| 25 | |||
| 26 | |||
| 27 | =head3 --in=infile.fasta | ||
| 28 | |||
| 29 | An input fasta file (will be uncompressed if ending with gz) | ||
| 30 | |||
| 31 | =head3 -out=outfile.fasta | ||
| 32 | |||
| 33 | A .fasta file [default is stdout] | ||
| 34 | |||
| 35 | =head1 OPTIONS | ||
| 36 | |||
| 37 | =head2 --ac-prefix=string | ||
| 38 | |||
| 39 | Set a key to be prepended before the AC in the randomized bank. By default, it will be dependent on the choosen method. | ||
| 40 | |||
| 41 | |||
| 42 | =head3 --peptminlength [default 6] | ||
| 43 | |||
| 44 | Set the size of the peptide to be reshuffled if they already exist | ||
| 45 | |||
| 46 | =head3 --crcsize=int | ||
| 47 | |||
| 48 | Building a hash of known cleaved peptide can be quite demanding for memory (uniprot_trembl => ~4GB). Therefore solution is to make an array containing statements if or not a peptide with corresponding crc code was found. | ||
| 49 | |||
| 50 | The argument passed here is the number of bits use for the CRC coding: 33 means 2^33 bit of memory => 2^30 bytes => 1GB | ||
| 51 | |||
| 52 | =head3 --norandomseed | ||
| 53 | |||
| 54 | Random generator seed is set to 0, so 2 run on same data will produce the same result | ||
| 55 | |||
| 56 | =head2 misc | ||
| 57 | |||
| 58 | =head3 --noprogressbar | ||
| 59 | |||
| 60 | do not display terminal progress bar (if possible) | ||
| 61 | |||
| 62 | =head3 --help | ||
| 63 | |||
| 64 | =head3 --man | ||
| 65 | |||
| 66 | =head3 --verbose | ||
| 67 | |||
| 68 | Setting an environment variable DO_NOT_DELETE_TEMP=1 will keep the temporay file after the script exit | ||
| 69 | |||
| 70 | =head1 EXAMPLE | ||
| 71 | |||
| 72 | |||
| 73 | =head1 COPYRIGHT | ||
| 74 | |||
| 75 | Copyright (C) 2004-2006 Geneva Bioinformatics www.genebio.com | ||
| 76 | |||
| 77 | This library is free software; you can redistribute it and/or | ||
| 78 | modify it under the terms of the GNU Lesser General Public | ||
| 79 | License as published by the Free Software Foundation; either | ||
| 80 | version 2.1 of the License, or (at your option) any later version. | ||
| 81 | |||
| 82 | This library is distributed in the hope that it will be useful, | ||
| 83 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 84 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 85 | Lesser General Public License for more details. | ||
| 86 | |||
| 87 | You should have received a copy of the GNU Lesser General Public | ||
| 88 | License along with this library; if not, write to the Free Software | ||
| 89 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 90 | |||
| 91 | =head1 AUTHORS | ||
| 92 | |||
| 93 | Alexandre Masselot, www.genebio.com | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | use Inline 'C'; | ||
| 98 | use String::CRC; | ||
| 99 | use Bit::Vector; | ||
| 100 | use SelectSaver; | ||
| 101 | use File::Basename; | ||
| 102 | use List::Util qw/shuffle/; | ||
| 103 | use Log::StdLog; | ||
| 104 | |||
| 105 | |||
| 106 | use Getopt::Long; | ||
| 107 | my ($acPrefix, $noProgressBar); | ||
| 108 | |||
| 109 | my $shuffle_reshuffleCleavPept_minLength=6; | ||
| 110 | my $shuffle_reshuffleCleavPept_CRCLen; | ||
| 111 | |||
| 112 | |||
| 113 | my $inFile='-'; | ||
| 114 | my $outFile='-'; | ||
| 115 | my $inFD; | ||
| 116 | |||
| 117 | my $outLength; | ||
| 118 | my $noSeed; | ||
| 119 | |||
| 120 | my($help, $man, $verbose); | ||
| 121 | |||
| 122 | |||
| 123 | if (!GetOptions( | ||
| 124 | "in=s"=>\$inFile, | ||
| 125 | "out=s" => \$outFile, | ||
| 126 | |||
| 127 | "ac-prefix=s" =>\$acPrefix, | ||
| 128 | |||
| 129 | "peptminlength=i"=> \$shuffle_reshuffleCleavPept_minLength, | ||
| 130 | "crcsize=i"=> \$shuffle_reshuffleCleavPept_CRCLen, | ||
| 131 | |||
| 132 | "outlength=i"=>\$outLength, | ||
| 133 | "norandomseed"=>\$noSeed, | ||
| 134 | |||
| 135 | "noprogressbar" => \$noProgressBar, | ||
| 136 | |||
| 137 | "help" => \$help, | ||
| 138 | "man" => \$man, | ||
| 139 | "verbose" => \$verbose, | ||
| 140 | ) | ||
| 141 | || $help || $man) { | ||
| 142 | |||
| 143 | |||
| 144 | pod2usage(-verbose=>2, -exitval=>2) if(defined $man); | ||
| 145 | pod2usage(-verbose=>1, -exitval=>2); | ||
| 146 | } | ||
| 147 | |||
| 148 | Log::StdLog->import({level=>$verbose?'info':'warn', handle => \*STDERR}); | ||
| 149 | |||
| 150 | srand(1) if $noSeed; | ||
| 151 | |||
| 152 | my $trypsinQR=qr/(.*?[KR](?=[^P])|.+$)/; | ||
| 153 | |||
| 154 | my $nbentries=0; | ||
| 155 | my $nbpept; | ||
| 156 | |||
| 157 | my ($nbVCRC, $modVCRC, $maxBitCRC); | ||
| 158 | my @vcrc; | ||
| 159 | my %peptsDico; | ||
| 160 | |||
| 161 | if ($shuffle_reshuffleCleavPept_CRCLen>=32) { | ||
| 162 | $nbVCRC=1<<($shuffle_reshuffleCleavPept_CRCLen-32); | ||
| 163 | $maxBitCRC=2*(1<<31); #WARN 1<<32 => 1!!! | ||
| 164 | } else { | ||
| 165 | $nbVCRC=1; | ||
| 166 | $maxBitCRC=1<<$shuffle_reshuffleCleavPept_CRCLen; | ||
| 167 | } | ||
| 168 | print {*STDLOG} info=> "nb CRC vector=$nbVCRC\nmax bit 4 crc=$maxBitCRC\n"; | ||
| 169 | |||
| 170 | #init parsing progress bar | ||
| 171 | my ($pg, $size, $nextpgupdate, $readsize); | ||
| 172 | $readsize=0; | ||
| 173 | $nextpgupdate=0; | ||
| 174 | my $imaxreshuffle=1000; | ||
| 175 | my $imaxreshuffleFinal=10000; | ||
| 176 | |||
| 177 | __setInput(); | ||
| 178 | |||
| 179 | #set output on default | ||
| 180 | my $saver; | ||
| 181 | if ($outFile ne '-') { | ||
| 182 | if ($outFile=~/\.gz$/i) { | ||
| 183 | open FDOUT, ">:gzip", $outFile or die "cannot open for writing gziped [$outFile]: $!"; | ||
| 184 | $saver=new SelectSaver(\*FDOUT); | ||
| 185 | } else { | ||
| 186 | open (FDOUT, ">$outFile") or die "could not open for writing [$outFile]:$!"; | ||
| 187 | $saver=new SelectSaver(\*FDOUT); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | $acPrefix ||='SHFLPLUS_'; | ||
| 192 | @vcrc = $shuffle_reshuffleCleavPept_CRCLen && Bit::Vector->new($maxBitCRC, $nbVCRC||1); | ||
| 193 | $nbpept=0; | ||
| 194 | |||
| 195 | my ($cptbit, $size)=(0,0); | ||
| 196 | if($shuffle_reshuffleCleavPept_CRCLen){ | ||
| 197 | foreach my $v (@vcrc) { | ||
| 198 | $v->Empty; | ||
| 199 | $cptbit+=$v->Norm(); | ||
| 200 | } | ||
| 201 | $size=$vcrc[0]->Size(); | ||
| 202 | warn "cptbit=$cptbit"; | ||
| 203 | warn "size=".($maxBitCRC*$nbVCRC); | ||
| 204 | } | ||
| 205 | |||
| 206 | computeCRC(); | ||
| 207 | print {*STDLOG} info=> "nbentries=$nbentries\tnbpept=$nbpept\n"; | ||
| 208 | |||
| 209 | if ($shuffle_reshuffleCleavPept_CRCLen) { | ||
| 210 | my $cptbit=0; | ||
| 211 | my $size=0; | ||
| 212 | foreach my $v (@vcrc) { | ||
| 213 | $cptbit+=$v->Norm(); | ||
| 214 | } | ||
| 215 | print {*STDLOG} info=> "duplication rate in the CRC index=".sprintf("%2.2f", (1.0*$nbpept)/$cptbit)." ($nbpept/$cptbit) - this include 'natural' duplication when a cleaved peptide is seen more than once in the databank\n"; | ||
| 216 | print {*STDLOG} info => "crc vector fill rate=".sprintf("%3.3f", (1.0*$cptbit)/$nbVCRC/$maxBitCRC)." ($cptbit/($nbVCRC*$maxBitCRC))\n"; | ||
| 217 | }else{ | ||
| 218 | print {*STDLOG} info => "nb <> pept seq=".scalar(keys %peptsDico)."\n"; | ||
| 219 | } | ||
| 220 | __setInput(); | ||
| 221 | my @histoReshuffled; | ||
| 222 | my $nreshuffled=0; | ||
| 223 | my $nFinalReshuffle=0; | ||
| 224 | my $donotReadNext; | ||
| 225 | my ($head, $seq); | ||
| 226 | my $seqbak; | ||
| 227 | my $nreshuffperseq; | ||
| 228 | #donotReadNext is useed to resshuffle the whole sequence without reading the next one | ||
| 229 | my $iseq=0; | ||
| 230 | while ($donotReadNext || (($head, $seq)=__nextEntry())[0]) { | ||
| 231 | #print "(".__LINE__.") [$head]\n[$seq]\n"; | ||
| 232 | # if($head=~/^>A0BIX5\|A0BIX5_PARTE\b/){ | ||
| 233 | # Log::StdLog->import({level=>'debug', handle => \*STDERR}); | ||
| 234 | # my $dummy=<STDIN>; | ||
| 235 | # }else{ | ||
| 236 | # Log::StdLog->import({level=>$verbose?'info':'warn', handle => \*STDERR}); | ||
| 237 | # } | ||
| 238 | print {*STDLOG} debug=>$head; | ||
| 239 | print {*STDLOG} debug=>$seq; | ||
| 240 | |||
| 241 | if ($donotReadNext) { | ||
| 242 | print {*STDLOG} debug=>"donotReadNext: seq bak $seqbak"; | ||
| 243 | |||
| 244 | $seq=$seqbak; | ||
| 245 | undef $donotReadNext; | ||
| 246 | } else { | ||
| 247 | $nreshuffperseq=0 ; | ||
| 248 | $seqbak=$seq; | ||
| 249 | $iseq++; | ||
| 250 | last if $outLength && $iseq>$outLength; | ||
| 251 | } | ||
| 252 | #die"TEMP END" if $iseq>100; | ||
| 253 | print {*STDLOG} debug => __LINE__.": ($nreshuffperseq>$imaxreshuffleFinal)"; | ||
| 254 | if ($nreshuffperseq>$imaxreshuffleFinal) { | ||
| 255 | |||
| 256 | $head=~/>(\S+)/; | ||
| 257 | if ($pg) { | ||
| 258 | $pg->message("reshuffling the whole sequence without control [$1]"); | ||
| 259 | } else { | ||
| 260 | warn "reshuffling the whole sequence without control [$1]\n"; | ||
| 261 | } | ||
| 262 | $head=~s/>/>$acPrefix/ or die "entry header does not start with '>': $head"; | ||
| 263 | my $newseq=join ('', shuffle (split //, $seq)); | ||
| 264 | print "$head\n".__prettySeq($newseq)."\n"; | ||
| 265 | |||
| 266 | $histoReshuffled[$nreshuffperseq]++; | ||
| 267 | $nFinalReshuffle++; | ||
| 268 | undef $donotReadNext; | ||
| 269 | next; | ||
| 270 | } | ||
| 271 | $seq=join ('', shuffle (split //, $seq)); | ||
| 272 | my $newseq=""; | ||
| 273 | while ($seq && $seq=~ /$trypsinQR/) { | ||
| 274 | # warn "$seq"; | ||
| 275 | print {*STDLOG} debug => __LINE__.": $seq"; | ||
| 276 | if ($nreshuffperseq && (($nreshuffperseq % $imaxreshuffle) == 0)) { | ||
| 277 | $head=~/>(\S+)/; | ||
| 278 | print {*STDLOG} debug => __LINE__.": reshuffling the whole sequence [$1] ($nreshuffperseq/$imaxreshuffleFinal)"; | ||
| 279 | if ($pg) { | ||
| 280 | $pg->message("reshuffling the whole sequence [$1] ($nreshuffperseq/$imaxreshuffleFinal)"); | ||
| 281 | } else { | ||
| 282 | warn "reshuffling the whole sequence [$1] ($nreshuffperseq/$imaxreshuffleFinal)\n"; | ||
| 283 | } | ||
| 284 | $nreshuffperseq++; | ||
| 285 | $donotReadNext=1; | ||
| 286 | last; | ||
| 287 | } | ||
| 288 | my $pept=$1; | ||
| 289 | print {*STDLOG} debug=> "$pept"; | ||
| 290 | |||
| 291 | my $reshuffle; | ||
| 292 | |||
| 293 | if (length($pept)<$shuffle_reshuffleCleavPept_minLength) { | ||
| 294 | $newseq.=$pept; | ||
| 295 | $seq=substr($seq, length($pept)); | ||
| 296 | next; | ||
| 297 | } | ||
| 298 | if ($shuffle_reshuffleCleavPept_CRCLen) { | ||
| 299 | my ($i, $c)=crc($pept, $shuffle_reshuffleCleavPept_CRCLen); | ||
| 300 | if($nbVCRC>1){ | ||
| 301 | #$c%=$maxBitCRC; | ||
| 302 | }else{ | ||
| 303 | $c=$i; | ||
| 304 | $i=0; | ||
| 305 | } | ||
| 306 | $reshuffle = $vcrc[$i]->bit_test($c); | ||
| 307 | } else { | ||
| 308 | $reshuffle = exists $peptsDico{$pept}; | ||
| 309 | } | ||
| 310 | if ($reshuffle) { | ||
| 311 | print {*STDLOG} debug=>"($nreshuffperseq<$imaxreshuffle/2) reshuf\t$pept"; | ||
| 312 | while($nreshuffperseq<$imaxreshuffle/2) { | ||
| 313 | print {*STDLOG} debug=> "looping\t$pept"; | ||
| 314 | #warn "($nreshuffperseq<$imaxreshuffle/2)"; | ||
| 315 | $nreshuffled++; | ||
| 316 | $nreshuffperseq++; | ||
| 317 | my $lpept=length($pept); | ||
| 318 | my $lseq=length($seq); | ||
| 319 | last if $lpept== $lseq; | ||
| 320 | my ($l1, $l2, $lmax); | ||
| 321 | #well, the 2 numbers are not independent. et alors? | ||
| 322 | $l2=int(rand($lseq-2-$lpept)); | ||
| 323 | #warn "lpept=$lpept lseq=$lseq l2=$l2"; | ||
| 324 | $l1=$l2%($lpept-3)+1; | ||
| 325 | $l2+=$lpept; | ||
| 326 | #warn "l1=$l1 l2=$l2"; | ||
| 327 | c_swap($seq, $l1, $l2); | ||
| 328 | |||
| 329 | my $trialpept=substr($seq, 0, length($pept)); | ||
| 330 | print {*STDLOG} debug=>"trial\t$trialpept"; | ||
| 331 | if ($shuffle_reshuffleCleavPept_CRCLen) { | ||
| 332 | my ($i, $c)=crc($trialpept, $shuffle_reshuffleCleavPept_CRCLen); | ||
| 333 | $i%=$nbVCRC; | ||
| 334 | $c%=$maxBitCRC; | ||
| 335 | if(!($vcrc[$i]->bit_test($c))){ | ||
| 336 | #warn "undef reshuffle"; | ||
| 337 | undef $reshuffle; | ||
| 338 | $pept=$trialpept; | ||
| 339 | last; | ||
| 340 | } | ||
| 341 | }else{ | ||
| 342 | unless (exists $peptsDico{$trialpept}){ | ||
| 343 | undef $reshuffle; | ||
| 344 | $pept=$trialpept; | ||
| 345 | last; | ||
| 346 | } | ||
| 347 | } | ||
| 348 | } | ||
| 349 | if($reshuffle){ | ||
| 350 | $nreshuffled++; | ||
| 351 | $nreshuffperseq++; | ||
| 352 | |||
| 353 | if ($seq=~/(.{50})(.+)/) { | ||
| 354 | my ($s1, $s2)=($1, $2); | ||
| 355 | $seq=join ('', shuffle (split //, $s1)).$s2; | ||
| 356 | } else { | ||
| 357 | $seq=join ('', shuffle (split //, $seq)); | ||
| 358 | } | ||
| 359 | print {*STDLOG} debug=>"($nreshuffperseq<$imaxreshuffle/2) reshuffling whole seq $seq"; | ||
| 360 | next; | ||
| 361 | } | ||
| 362 | } | ||
| 363 | unless ($reshuffle){ | ||
| 364 | # warn "append newseq"; | ||
| 365 | $newseq.=$pept; | ||
| 366 | $seq=substr($seq, length($pept)); | ||
| 367 | $histoReshuffled[$nreshuffperseq]++; | ||
| 368 | $nreshuffperseq=0; | ||
| 369 | } | ||
| 370 | } | ||
| 371 | unless($donotReadNext){ | ||
| 372 | $head=~s/>/>$acPrefix/ or die "entry header does not start with '>': $head"; | ||
| 373 | print "$head\n".__prettySeq($newseq)."\n"; | ||
| 374 | $histoReshuffled[$nreshuffperseq]++; | ||
| 375 | #print "(".__LINE__.") [$head]\n[$newseq]\n"; | ||
| 376 | } | ||
| 377 | } | ||
| 378 | if ($verbose) { | ||
| 379 | print {*STDLOG} info=> "reshuffled pept/nb sequences: $nreshuffled/$nbentries\n"; | ||
| 380 | print {*STDLOG} info=> "nb final (no-check) seq reshuffling: $nFinalReshuffle\n"; | ||
| 381 | print {*STDLOG} info=> "#seq\t#nb reshuffled peptides\n"; | ||
| 382 | foreach (0..$#histoReshuffled) { | ||
| 383 | next unless $histoReshuffled[$_]; | ||
| 384 | print {*STDLOG} info=> "$_\t$histoReshuffled[$_]\n"; | ||
| 385 | } | ||
| 386 | } | ||
| 387 | |||
| 388 | |||
| 389 | sub __nextEntry{ | ||
| 390 | lock ($inFD); | ||
| 391 | local $/="\n>"; | ||
| 392 | my $contents=<$inFD>; | ||
| 393 | return undef unless $contents; | ||
| 394 | |||
| 395 | $readsize+=length $contents; | ||
| 396 | $nextpgupdate=$pg->update($readsize) if $pg && $readsize>=$nextpgupdate; | ||
| 397 | |||
| 398 | chomp $contents; | ||
| 399 | $contents=">$contents" unless $contents=~/^>/; | ||
| 400 | my ($head, $seq)=split /\n/,$contents,2; | ||
| 401 | $seq=~s/\s+//g; | ||
| 402 | return ($head, $seq); | ||
| 403 | } | ||
| 404 | |||
| 405 | sub __prettySeq{ | ||
| 406 | $_=$_[0]; | ||
| 407 | s/(\S{60})(?=\S)/$1\n/g; | ||
| 408 | return $_; | ||
| 409 | } | ||
| 410 | |||
| 411 | sub __setInput{ | ||
| 412 | eval{ | ||
| 413 | if ((!$noProgressBar) && ($inFile !~ /gz$/i) && ($inFile ne '-')&& -t STDIN && -t STDOUT) { | ||
| 414 | require Term::ProgressBar; | ||
| 415 | $size=(stat $inFile)[7]; | ||
| 416 | $pg=Term::ProgressBar->new ({name=> "parsing ".basename($inFile), | ||
| 417 | count=>$size, | ||
| 418 | ETA=>'linear', | ||
| 419 | remove=>1 | ||
| 420 | }); | ||
| 421 | } | ||
| 422 | $nextpgupdate=0; | ||
| 423 | $readsize=0; | ||
| 424 | }; | ||
| 425 | if ($@) { | ||
| 426 | warn "could not use Term::ProgressBar (consider installing the module for more interactive use"; | ||
| 427 | } | ||
| 428 | #set input | ||
| 429 | if ($inFile eq '-') { | ||
| 430 | $inFD=\*STDIN; | ||
| 431 | } elsif ($inFile=~/\.gz$/i) { | ||
| 432 | require PerlIO::gzip; | ||
| 433 | open $inFD, "<:gzip", "$inFile" or die $!; | ||
| 434 | } else { | ||
| 435 | open ($inFD, "<$inFile") or die "cannot open for reading [$inFile]: $!"; | ||
| 436 | } | ||
| 437 | } | ||
| 438 | |||
| 439 | sub computeCRC{ | ||
| 440 | while ((my ($head, $seq)=__nextEntry())[0]) { | ||
| 441 | { | ||
| 442 | $nbentries++; | ||
| 443 | } | ||
| 444 | my @tmpcrcs; | ||
| 445 | while ($seq=~/$trypsinQR/g) { | ||
| 446 | $_=$1; | ||
| 447 | my $l=length($_); | ||
| 448 | if ($l>=$shuffle_reshuffleCleavPept_minLength) { | ||
| 449 | if ($shuffle_reshuffleCleavPept_CRCLen) { | ||
| 450 | my ($i, $c)=crc($_, $shuffle_reshuffleCleavPept_CRCLen); | ||
| 451 | if($nbVCRC>1){ | ||
| 452 | #$c%=$maxBitCRC; | ||
| 453 | }else{ | ||
| 454 | $c=$i; | ||
| 455 | $i=0; | ||
| 456 | } | ||
| 457 | $vcrc[$i]->Bit_On($c); | ||
| 458 | } else { | ||
| 459 | $peptsDico{$_}=undef; | ||
| 460 | } | ||
| 461 | $nbpept++; | ||
| 462 | } | ||
| 463 | } | ||
| 464 | } | ||
| 465 | } | ||
| 466 | |||
| 467 | __END__ |