| 1 | #!/usr/bin/env perl | ||
| 2 | use strict; | ||
| 3 | use Pod::Usage; | ||
| 4 | use Getopt::Long; | ||
| 5 | use File::Basename; | ||
| 6 | use File::Temp qw(tempfile); | ||
| 7 | |||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | ncbinr2phenyxfasta.pl - tranfoorm ncbirn bank into fasta with annotated headers | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | ncbinr2phenyxfasta.pl --in=EST_others.fasta --out=/tmp/est_others.fasta --defaulttaxid=1 --taxo=/tmp/gi_taxid_nucl.dmp --sorted --norewind | ||
| 18 | |||
| 19 | =head1 ARGUMENTS | ||
| 20 | |||
| 21 | |||
| 22 | =head3 --in=infile.fasta | ||
| 23 | |||
| 24 | An input fasta file | ||
| 25 | |||
| 26 | =head3 --taxo=file | ||
| 27 | |||
| 28 | =head3 --out=outfile.fasta | ||
| 29 | |||
| 30 | A .fasta file [default is stdout] | ||
| 31 | |||
| 32 | =head1 OPTIONS | ||
| 33 | |||
| 34 | =head3 --sorted | ||
| 35 | |||
| 36 | The entries both in fasta and taxo file are sorted (there can be (few) miss sorting in the taxonomy file) | ||
| 37 | |||
| 38 | =head3 --defaulttaxid=int | ||
| 39 | |||
| 40 | A taxonomy to batttribute to entries where there is nor registrated taxonomy | ||
| 41 | |||
| 42 | =head3 --norewind | ||
| 43 | |||
| 44 | Disallow rewind of taxonomy file when a taxid is missing | ||
| 45 | |||
| 46 | =head3 --help | ||
| 47 | |||
| 48 | =head3 --man | ||
| 49 | |||
| 50 | =head3 --verbose | ||
| 51 | |||
| 52 | Setting an environment vartiable DO_NOT_DELETE_TEMP=1 will keep the temporay file after the script exit | ||
| 53 | |||
| 54 | =head1 EXAMPLE | ||
| 55 | |||
| 56 | |||
| 57 | =head1 COPYRIGHT | ||
| 58 | |||
| 59 | Copyright (C) 2004-2006 Geneva Bioinformatics www.genebio.com | ||
| 60 | |||
| 61 | This library is free software; you can redistribute it and/or | ||
| 62 | modify it under the terms of the GNU Lesser General Public | ||
| 63 | License as published by the Free Software Foundation; either | ||
| 64 | version 2.1 of the License, or (at your option) any later version. | ||
| 65 | |||
| 66 | This library is distributed in the hope that it will be useful, | ||
| 67 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 68 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 69 | Lesser General Public License for more details. | ||
| 70 | |||
| 71 | You should have received a copy of the GNU Lesser General Public | ||
| 72 | License along with this library; if not, write to the Free Software | ||
| 73 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 74 | |||
| 75 | =head1 AUTHORS | ||
| 76 | |||
| 77 | Djibril Ousmanou, | ||
| 78 | |||
| 79 | Alexandre Masselot, www.genebio.com | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | |||
| 84 | my $nrFile; | ||
| 85 | my $outFile='-'; | ||
| 86 | my ($nrFile, $taxodmpFile, $defaultTaxid, $sortedData, $isNoRewind, $noProgressBar, $help, $man, $verbose); | ||
| 87 | |||
| 88 | |||
| 89 | ############### read command line options | ||
| 90 | if (!GetOptions( | ||
| 91 | "in=s"=>\$nrFile, | ||
| 92 | "taxo=s"=>\$taxodmpFile, | ||
| 93 | "defaulttaxid=i"=>\$defaultTaxid, | ||
| 94 | "out=s"=>\$outFile, | ||
| 95 | "sorted"=>\$sortedData, | ||
| 96 | "norewind"=>\$isNoRewind, | ||
| 97 | |||
| 98 | "noprogressbar"=>\$noProgressBar, | ||
| 99 | |||
| 100 | "help"=>\$help, | ||
| 101 | "man" => \$man, | ||
| 102 | "verbose"=>\$verbose, | ||
| 103 | ) | ||
| 104 | || $help || $man | ||
| 105 | ){ | ||
| 106 | pod2usage(-verbose=>2, -exitval=>2) if(defined $man); | ||
| 107 | pod2usage(-verbose=>1, -exitval=>2); | ||
| 108 | } | ||
| 109 | |||
| 110 | die "no nr fasta (--in=file)" unless $nrFile; | ||
| 111 | die "notaxo.dmp (--taxo=file)" unless $taxodmpFile; | ||
| 112 | |||
| 113 | my ($nextpgupdate, $readsize, $pg); | ||
| 114 | |||
| 115 | |||
| 116 | unless($sortedData){ | ||
| 117 | |||
| 118 | setupPG($nrFile); | ||
| 119 | |||
| 120 | my %ac2taxid; | ||
| 121 | open (FD,"<$nrFile") or die "cannot open for reading [$nrFile]:$!"; | ||
| 122 | while (<FD>) { | ||
| 123 | $readsize+=length $_; | ||
| 124 | $nextpgupdate=$pg->update($readsize) if $pg && $readsize>=$nextpgupdate; | ||
| 125 | next unless /^>gi\|(\w+)/; | ||
| 126 | my $gi=$1; | ||
| 127 | $gi=~s/_.*//; | ||
| 128 | $ac2taxid{$gi}=undef; | ||
| 129 | } | ||
| 130 | warn "nb of fasta entries=".scalar (keys %ac2taxid)."\n"; | ||
| 131 | close FD; | ||
| 132 | |||
| 133 | setupPG($taxodmpFile); | ||
| 134 | |||
| 135 | open (FD,"<$taxodmpFile") or die "cannot open for reading [$taxodmpFile]:$!"; | ||
| 136 | while (<FD>) { | ||
| 137 | $readsize+=length $_; | ||
| 138 | $nextpgupdate=$pg->update($readsize) if $pg && $readsize>=$nextpgupdate; | ||
| 139 | chomp; | ||
| 140 | my ($gi, $taxo)=split; | ||
| 141 | next unless exists $ac2taxid{$gi}; | ||
| 142 | $ac2taxid{$gi}=$taxo; | ||
| 143 | } | ||
| 144 | close FD; | ||
| 145 | |||
| 146 | my $nbnotaxid=0; | ||
| 147 | foreach (keys %ac2taxid) { | ||
| 148 | unless ($ac2taxid{$_}) { | ||
| 149 | $nbnotaxid++; | ||
| 150 | $ac2taxid{$_}=1; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | warn "no taxid found for $nbnotaxid/".scalar (keys %ac2taxid)." entries\n"; | ||
| 155 | warn "producing output file [$outFile]\n"; | ||
| 156 | setupPG($nrFile); | ||
| 157 | open (FD, "<$nrFile") or die "cannot open for reading [$nrFile]:$!"; | ||
| 158 | open (OUT, ">$outFile") or die "cannot open for writing: [$outFile]: $!"; | ||
| 159 | local $/="\n>"; | ||
| 160 | while (<FD>) { | ||
| 161 | $readsize+=length $_; | ||
| 162 | $nextpgupdate=$pg->update($readsize) if $pg && $readsize>=$nextpgupdate; | ||
| 163 | chomp; | ||
| 164 | my ($head, $seq)=split /\n/, $_, 2; | ||
| 165 | $head=~s/^>//; | ||
| 166 | |||
| 167 | my $gi; | ||
| 168 | |||
| 169 | |||
| 170 | if ($head=~s/^((?:\w+)\|(?:(\d+)\w*))\|(\S+)\|\s*(.*)/>$1 \\ID=$3 \\NCBITAXID=$ac2taxid{$2} \\DE=$4/) { | ||
| 171 | $gi=$2; | ||
| 172 | $gi=~s/_.*//; | ||
| 173 | s/\cA/;/g; | ||
| 174 | } | ||
| 175 | |||
| 176 | $seq=~s/[^A-Z]//ig; | ||
| 177 | next unless $seq=~/\S/; | ||
| 178 | |||
| 179 | print OUT "$head\n$seq\n"; | ||
| 180 | #$ac2taxid{$gi}=undef if defined $gi; | ||
| 181 | } | ||
| 182 | close FD; | ||
| 183 | close OUT; | ||
| 184 | } else { | ||
| 185 | if($isNoRewind){ | ||
| 186 | my ($FDTMP, $ftmp)=tempfile(UNLINK=>1); | ||
| 187 | my $cmd="sort -n $taxodmpFile"; | ||
| 188 | warn "sorting taxonomy: $cmd >$ftmp\n"; | ||
| 189 | unless (`sort --version` && open (FDSORTED, "$cmd|")){ | ||
| 190 | die "ask --rewind but cannot find 'sort command or execute '$cmd'"; | ||
| 191 | } | ||
| 192 | while(<FDSORTED>){ | ||
| 193 | print $FDTMP $_; | ||
| 194 | } | ||
| 195 | close $FDTMP; | ||
| 196 | close FDSORTED; | ||
| 197 | $taxodmpFile=$ftmp; | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | setupPG($nrFile); | ||
| 202 | open (FD, "<$nrFile") or die "cannot open for reading [$nrFile]:$!"; | ||
| 203 | open (FDTAXO,"<$taxodmpFile") or die "cannot open for reading [$taxodmpFile]:$!"; | ||
| 204 | |||
| 205 | open (OUT, ">$outFile") or die "cannot open for writing: [$outFile]: $!"; | ||
| 206 | local $/="\n>"; | ||
| 207 | my $hasRewindTaxo; | ||
| 208 | my ($nbentries, $nbmisstaxo)=(0,0); | ||
| 209 | while (<FD>) { | ||
| 210 | $nbentries++; | ||
| 211 | $hasRewindTaxo=$isNoRewind; | ||
| 212 | $readsize+=length $_; | ||
| 213 | $nextpgupdate=$pg->update($readsize) if $pg && $readsize>=$nextpgupdate; | ||
| 214 | |||
| 215 | s/^>//; | ||
| 216 | my ($line, $seq)=split /\n/, $_, 2; | ||
| 217 | |||
| 218 | my $gi; | ||
| 219 | if ($line=~s/^((?:\w+)\|(\w+))\|(\S+)\|\s*(.*)/>$1 \\ID=$3 \\NCBITAXID=__TAXID__ \\DE=$4/) { | ||
| 220 | $gi=$2; | ||
| 221 | $gi=~s/_.*//; | ||
| 222 | $line=~s/\cA/;/g; | ||
| 223 | #locate gi | ||
| 224 | my $taxid; | ||
| 225 | local $/="\n"; | ||
| 226 | while(<FDTAXO>){ | ||
| 227 | my ($gitmp, $taxo)=split; | ||
| 228 | if(($gitmp+0)>($gi+0)){ | ||
| 229 | last if($hasRewindTaxo); | ||
| 230 | $hasRewindTaxo=1; | ||
| 231 | unless ($isNoRewind){ | ||
| 232 | warn "rewind at line $line\n" ; | ||
| 233 | close FDTAXO; | ||
| 234 | open (FDTAXO,"<$taxodmpFile") or die "cannot open for reading [$taxodmpFile]:$!"; | ||
| 235 | } | ||
| 236 | next; | ||
| 237 | } | ||
| 238 | if($gitmp eq $gi){ | ||
| 239 | $taxid=$taxo; | ||
| 240 | last; | ||
| 241 | } | ||
| 242 | } | ||
| 243 | unless(defined $taxid){ | ||
| 244 | if(defined $defaultTaxid){ | ||
| 245 | $nbmisstaxo++; | ||
| 246 | $taxid=$defaultTaxid; | ||
| 247 | }else{ | ||
| 248 | die "no taxid for gi=[$gi] in $taxodmpFile (force using --defaulttaxid=1)"; | ||
| 249 | } | ||
| 250 | } | ||
| 251 | $line=~s/NCBITAXID=__TAXID__/NCBITAXID=$taxid/; | ||
| 252 | } | ||
| 253 | $seq=~s/[^A-Z]//ig; | ||
| 254 | next unless $seq=~/\S/; | ||
| 255 | |||
| 256 | print OUT "$line\n$seq\n"; | ||
| 257 | } | ||
| 258 | warn "nb miss taxo:$nbmisstaxo/$nbentries\n"; | ||
| 259 | } | ||
| 260 | warn "completed\n"; | ||
| 261 | |||
| 262 | sub setupPG{ | ||
| 263 | my $file=shift or die "no arg to setupPG"; | ||
| 264 | die "$file does not exist" unless -f $file; | ||
| 265 | if ((!$noProgressBar) && ($file ne '-')&& -t STDIN && -t STDOUT){ | ||
| 266 | require Term::ProgressBar; | ||
| 267 | my $size=(stat $file)[7]; | ||
| 268 | $pg=Term::ProgressBar->new ({name=> "parsing ".basename($file), | ||
| 269 | count=>$size, | ||
| 270 | ETA=>'linear', | ||
| 271 | remove=>1 | ||
| 272 | }); | ||
| 273 | $nextpgupdate=0; | ||
| 274 | $readsize=0; | ||
| 275 | } | ||
| 276 | } |