| 1 | #!/usr/bin/env perl | ||
| 2 | use strict; | ||
| 3 | use Carp; | ||
| 4 | use Pod::Usage; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | fasta-decoy.pl - decoy input databanks following several moethods | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | Reads input fasta file and produce a decoyed databank with several methods: | ||
| 13 | |||
| 14 | =over 4 | ||
| 15 | |||
| 16 | =item reverse: simply reverse each sequence | ||
| 17 | |||
| 18 | =item shuffle: shuffle AA in each sequence | ||
| 19 | |||
| 20 | =item shuffle & avoid known cleaved peptides: shuffle sequence but avoid producing known tryptic peptides | ||
| 21 | |||
| 22 | =item Markov model: learn Markov model chain distribution of a given level, then produces entries corresponding to this distribution | ||
| 23 | |||
| 24 | =back | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | #reverse sequences for a local (optionaly compressed) file | ||
| 29 | fasta-decoy.pl --in=/tmp/uniprot_sprot.fasta.gz --method=reverse | ||
| 30 | |||
| 31 | #download databanks from the web | uncompress it and shuffle the sequence | ||
| 32 | wget -silent -O - ftp://ftp.expasy.org/databases/uniprot/current_release/knowledgebase/complete/uniprot_sprot.fasta.gz | zcat | databatanks-decoy.pl --method=shuffle | ||
| 33 | |||
| 34 | #use a .dat file (with splice forms) as an input | ||
| 35 | uniprotdat2fasta.pl --in=uniprot_sprot_human.dat | fasta-decoy.pl --method=markovmodel | ||
| 36 | |||
| 37 | #reversing each sequence | ||
| 38 | fasta-decoy.pl --ac-prefix=DECOY_ --in=mitoch.fasta --method=reverse --out=mitoch-reverse.fasta | ||
| 39 | |||
| 40 | #drawing amino acid following distribution in original fasta (end of sequence is considered as a learned random event) | ||
| 41 | fasta-decoy.pl --ac-prefix=DECOY_ --in=mitoch.fasta --method=markovmodel --markovmodel-level=0 --out=mitoch-markovmodel_0.fasta | ||
| 42 | |||
| 43 | #drawing amino acid with a markov model (here of length 3) | ||
| 44 | fasta-decoy.pl --ac-prefix=DECOY_ --in=mitoch.fasta --method=markovmodel --markovmodel-level=3 --out=mitoch-markovmodel_3.fasta | ||
| 45 | |||
| 46 | #each sequence is randomly shuffled | ||
| 47 | fasta-decoy.pl --ac-prefix=DECOY_ --in=mitoch.fasta --method=shuffle --out=mitoch-shuffle.fasta | ||
| 48 | |||
| 49 | #idem, but no tryptic peptide (of length>=6) from the original bank must be found in the random one; | ||
| 50 | => see script fasta-shuffle-notryptic.pl | ||
| 51 | |||
| 52 | |||
| 53 | =head1 ARGUMENTS | ||
| 54 | |||
| 55 | |||
| 56 | =head3 --in=infile.fasta | ||
| 57 | |||
| 58 | An input fasta file (will be uncompressed if ending with gz) | ||
| 59 | |||
| 60 | =head3 -out=outfile.fasta | ||
| 61 | |||
| 62 | A .fasta file [default is stdout] | ||
| 63 | |||
| 64 | =head3 --method=(reverse|shuffle|markovmodel) | ||
| 65 | |||
| 66 | Set the decoying method | ||
| 67 | |||
| 68 | =head1 OPTIONS | ||
| 69 | |||
| 70 | =head2 --ac-prefix=string | ||
| 71 | |||
| 72 | Set a key to be prepended before the AC in the randomized bank. By default, it will be dependent on the choosen method. | ||
| 73 | |||
| 74 | =head2 --method=shuffle options | ||
| 75 | |||
| 76 | |||
| 77 | =head2 --method=markovmodel options | ||
| 78 | |||
| 79 | =head3 --markovmodel-level=int [default 3] | ||
| 80 | |||
| 81 | Set length of the model (0 means only AA distrbution will be respected, 3 means chains of length 3 distribution etc.). Setting a length >3 can deal to memory burnout. | ||
| 82 | |||
| 83 | =head2 misc | ||
| 84 | |||
| 85 | =head3 --norandomseed | ||
| 86 | |||
| 87 | Random generator seed is set to 0, so 2 run on same data will produce the same result | ||
| 88 | |||
| 89 | =head3 --noprogressbar | ||
| 90 | |||
| 91 | do not display terminal progress bar (if possible) | ||
| 92 | |||
| 93 | =head3 --help | ||
| 94 | |||
| 95 | =head3 --man | ||
| 96 | |||
| 97 | =head3 --verbose | ||
| 98 | |||
| 99 | Setting an environment variable DO_NOT_DELETE_TEMP=1 will keep the temporay file after the script exit | ||
| 100 | |||
| 101 | =head1 EXAMPLE | ||
| 102 | |||
| 103 | |||
| 104 | =head1 COPYRIGHT | ||
| 105 | |||
| 106 | Copyright (C) 2004-2006 Geneva Bioinformatics www.genebio.com | ||
| 107 | |||
| 108 | This library is free software; you can redistribute it and/or | ||
| 109 | modify it under the terms of the GNU Lesser General Public | ||
| 110 | License as published by the Free Software Foundation; either | ||
| 111 | version 2.1 of the License, or (at your option) any later version. | ||
| 112 | |||
| 113 | This library is distributed in the hope that it will be useful, | ||
| 114 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 115 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 116 | Lesser General Public License for more details. | ||
| 117 | |||
| 118 | You should have received a copy of the GNU Lesser General Public | ||
| 119 | License along with this library; if not, write to the Free Software | ||
| 120 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 121 | |||
| 122 | =head1 AUTHORS | ||
| 123 | |||
| 124 | Alexandre Masselot, www.genebio.com | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | |||
| 129 | use SelectSaver; | ||
| 130 | use File::Basename; | ||
| 131 | use List::Util qw/shuffle/; | ||
| 132 | |||
| 133 | use Getopt::Long; | ||
| 134 | my ($acPrefix, $method, $noProgressBar); | ||
| 135 | |||
| 136 | |||
| 137 | my $markovmodel_level=2; | ||
| 138 | |||
| 139 | my $inFile='-'; | ||
| 140 | my $outFile='-'; | ||
| 141 | my $inFD; | ||
| 142 | |||
| 143 | my $outLength; | ||
| 144 | my $noSeed; | ||
| 145 | |||
| 146 | my($help, $man, $verbose); | ||
| 147 | |||
| 148 | if (!GetOptions( | ||
| 149 | "in=s"=>\$inFile, | ||
| 150 | "out=s" => \$outFile, | ||
| 151 | |||
| 152 | "method=s" => \$method, | ||
| 153 | "ac-prefix=s" =>\$acPrefix, | ||
| 154 | |||
| 155 | "markovmodel-level=i"=>\$markovmodel_level, | ||
| 156 | |||
| 157 | "outlength=i"=>\$outLength, | ||
| 158 | |||
| 159 | "norandomseed"=>\$noSeed, | ||
| 160 | "noprogressbar" => \$noProgressBar, | ||
| 161 | |||
| 162 | "help" => \$help, | ||
| 163 | "man" => \$man, | ||
| 164 | "verbose" => \$verbose, | ||
| 165 | ) | ||
| 166 | || $help || $man){ | ||
| 167 | |||
| 168 | |||
| 169 | pod2usage(-verbose=>2, -exitval=>2) if(defined $man); | ||
| 170 | pod2usage(-verbose=>1, -exitval=>2); | ||
| 171 | } | ||
| 172 | |||
| 173 | my $nbentries=0; | ||
| 174 | my $nbpept; | ||
| 175 | srand(1) if $noSeed; | ||
| 176 | |||
| 177 | die "no --method=methodname argument (see --help)" unless $method; | ||
| 178 | |||
| 179 | #init parsing progress bar | ||
| 180 | my ($pg, $size, $nextpgupdate, $readsize); | ||
| 181 | $readsize=0; | ||
| 182 | $nextpgupdate=0; | ||
| 183 | my $imaxreshuffle=1000; | ||
| 184 | my $imaxreshuffleFinal=10000; | ||
| 185 | |||
| 186 | __setInput(); | ||
| 187 | |||
| 188 | #set output on default | ||
| 189 | my $saver; | ||
| 190 | if ($outFile ne '-'){ | ||
| 191 | if($outFile=~/\.gz$/i){ | ||
| 192 | open FDOUT, ">:gzip", $outFile or die "cannot open for writing gziped [$outFile]: $!"; | ||
| 193 | $saver=new SelectSaver(\*FDOUT); | ||
| 194 | }else{ | ||
| 195 | open (FDOUT, ">$outFile") or die "could not open for writing [$outFile]:$!"; | ||
| 196 | $saver=new SelectSaver(\*FDOUT); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | if($method eq 'reverse'){ | ||
| 201 | $acPrefix ||='REV_'; | ||
| 202 | $nbentries=0; | ||
| 203 | while((my ($head, $seq)=__nextEntry())[0]){ | ||
| 204 | $nbentries++; | ||
| 205 | $head=~s/>/>$acPrefix/ or die "entry header does not start with '>': $head"; | ||
| 206 | $seq=reverse $seq; | ||
| 207 | print "$head\n"; | ||
| 208 | print __prettySeq($seq)."\n"; | ||
| 209 | } | ||
| 210 | print STDERR "reverted $nbentries\n" if $verbose; | ||
| 211 | } elsif ($method eq 'markovmodel') { | ||
| 212 | $acPrefix ||='MARKOVMODEL_'; | ||
| 213 | $nbentries=0; | ||
| 214 | die "--markovmodel-level=int [$markovmodel_level] should be between 0 and 3" unless $markovmodel_level>=0 && $markovmodel_level<=3; | ||
| 215 | |||
| 216 | my %chain; #count and prob | ||
| 217 | #counting | ||
| 218 | while ((my ($head, $seq)=__nextEntry())[0]) { | ||
| 219 | $nbentries++; | ||
| 220 | $seq.='*'; | ||
| 221 | my $buf=''; | ||
| 222 | while ($seq=~/(.)/g) { | ||
| 223 | my $p=pos $seq; | ||
| 224 | my $q=$p-$markovmodel_level-1; | ||
| 225 | $q=0 if $q<0; | ||
| 226 | my $prec=substr $seq, $q, $p-$q-1; | ||
| 227 | $chain{$prec || '^'}{$1}++; | ||
| 228 | } | ||
| 229 | } | ||
| 230 | #normalize; | ||
| 231 | foreach my $h (values %chain) { | ||
| 232 | my $tot=0; | ||
| 233 | $tot+=$_ foreach values %$h; | ||
| 234 | $h->{$_}=$h->{$_}*1.0/$tot foreach keys %$h; | ||
| 235 | } | ||
| 236 | #generate: | ||
| 237 | eval{ | ||
| 238 | if ( -t STDIN && -t STDOUT){ | ||
| 239 | require Term::ProgressBar; | ||
| 240 | $size=(stat $inFile)[7]; | ||
| 241 | $pg=Term::ProgressBar->new ({name=> "markovmodel generation", | ||
| 242 | count=>$nbentries, | ||
| 243 | ETA=>'linear', | ||
| 244 | remove=>1 | ||
| 245 | }); | ||
| 246 | } | ||
| 247 | $nextpgupdate=0; | ||
| 248 | $readsize=0; | ||
| 249 | }; | ||
| 250 | |||
| 251 | |||
| 252 | foreach (1..$nbentries) { | ||
| 253 | $readsize++; | ||
| 254 | $nextpgupdate=$pg->update($readsize) if $pg && $readsize>=$nextpgupdate; | ||
| 255 | |||
| 256 | print ">$acPrefix$_\n"; | ||
| 257 | my $seq=''; | ||
| 258 | my $prec=''; | ||
| 259 | while (1) { | ||
| 260 | my $hNext; | ||
| 261 | $hNext=$prec?$chain{$prec}:$chain{'^'}; | ||
| 262 | my $crand=rand; | ||
| 263 | my $nextAA='?'; | ||
| 264 | foreach $_ (keys %$hNext) { | ||
| 265 | $crand-=$hNext->{$_}; | ||
| 266 | if ($crand<=0) { | ||
| 267 | $nextAA=$_; | ||
| 268 | last; | ||
| 269 | } | ||
| 270 | } | ||
| 271 | last if $nextAA eq '*'; | ||
| 272 | if ($markovmodel_level>0) { | ||
| 273 | $prec.=$nextAA; | ||
| 274 | $prec=~s/.+(?=.{$markovmodel_level})//; | ||
| 275 | } | ||
| 276 | die "NO next AA generated" if $nextAA eq '?'; | ||
| 277 | $seq.=$nextAA; | ||
| 278 | } | ||
| 279 | print __prettySeq($seq)."\n"; | ||
| 280 | } | ||
| 281 | } elsif ($method eq 'shuffle') { | ||
| 282 | $acPrefix ||='SHFL_'; | ||
| 283 | $nbentries=0; | ||
| 284 | while ((my ($head, $seq)=__nextEntry())[0]) { | ||
| 285 | $nbentries++; | ||
| 286 | $head=~s/>/>$acPrefix/ or die "entry header does not start with '>': $head"; | ||
| 287 | $seq=join ('', shuffle split(//, $seq)); | ||
| 288 | print "$head\n"; | ||
| 289 | print __prettySeq($seq)."\n"; | ||
| 290 | } | ||
| 291 | print STDERR "shuffled $nbentries\n" if $verbose; | ||
| 292 | } else { | ||
| 293 | die "unimplemented method [$method]"; | ||
| 294 | } | ||
| 295 | |||
| 296 | |||
| 297 | sub __nextEntry{ | ||
| 298 | lock ($inFD); | ||
| 299 | local $/="\n>"; | ||
| 300 | my $contents=<$inFD>; | ||
| 301 | return undef unless $contents; | ||
| 302 | |||
| 303 | $readsize+=length $contents; | ||
| 304 | $nextpgupdate=$pg->update($readsize) if $pg && $readsize>=$nextpgupdate; | ||
| 305 | |||
| 306 | chomp $contents; | ||
| 307 | $contents=">$contents" unless $contents=~/^>/; | ||
| 308 | my ($head, $seq)=split /\n/,$contents,2; | ||
| 309 | $seq=~s/\s+//g; | ||
| 310 | return ($head, $seq); | ||
| 311 | } | ||
| 312 | |||
| 313 | sub __prettySeq{ | ||
| 314 | $_=$_[0]; | ||
| 315 | s/(\S{60})(?=\S)/$1\n/g; | ||
| 316 | return $_; | ||
| 317 | } | ||
| 318 | |||
| 319 | sub __setInput{ | ||
| 320 | eval{ | ||
| 321 | if ((!$noProgressBar) && ($inFile !~ /gz$/i) && ($inFile ne '-')&& -t STDIN && -t STDOUT){ | ||
| 322 | require Term::ProgressBar; | ||
| 323 | $size=(stat $inFile)[7]; | ||
| 324 | $pg=Term::ProgressBar->new ({name=> "parsing ".basename($inFile), | ||
| 325 | count=>$size, | ||
| 326 | ETA=>'linear', | ||
| 327 | remove=>1 | ||
| 328 | }); | ||
| 329 | } | ||
| 330 | $nextpgupdate=0; | ||
| 331 | $readsize=0; | ||
| 332 | }; | ||
| 333 | if ($@){ | ||
| 334 | warn "could not use Term::ProgressBar (consider installing the module for more interactive use"; | ||
| 335 | } | ||
| 336 | #set input | ||
| 337 | if ($inFile eq '-'){ | ||
| 338 | $inFD=\*STDIN; | ||
| 339 | }elsif($inFile=~/\.gz$/i){ | ||
| 340 | require PerlIO::gzip; | ||
| 341 | open $inFD, "<:gzip", "$inFile" or die $!; | ||
| 342 | }else{ | ||
| 343 | open ($inFD, "<$inFile") or die "cannot open for reading [$inFile]: $!"; | ||
| 344 | } | ||
| 345 | } | ||
| 346 |