| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use lib '../lib'; | ||
| 6 | use Bio::CUA::CUB::Calculator; | ||
| 7 | use Bio::CUA::CodonTable; | ||
| 8 | use Getopt::Long; | ||
| 9 | |||
| 10 | our $VERSION = 0.11; | ||
| 11 | my @args = @ARGV; | ||
| 12 | my $sep = "\t"; | ||
| 13 | my $seqIO_pkg; | ||
| 14 | |||
| 15 | BEGIN{ | ||
| 16 | eval { require Bio::SeqIO; }; | ||
| 17 | |||
| 18 | if($@) | ||
| 19 | { | ||
| 20 | require Bio::CUA::SeqIO; | ||
| 21 | $seqIO_pkg = 'Bio::CUA::SeqIO'; | ||
| 22 | }else | ||
| 23 | { | ||
| 24 | $seqIO_pkg = 'Bio::SeqIO'; | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | my $seqFile; | ||
| 29 | my $gcId; | ||
| 30 | my $outFile; | ||
| 31 | my $taiFile; | ||
| 32 | my $caiFile; | ||
| 33 | my $fopFile; | ||
| 34 | my $encMethods; | ||
| 35 | my $baseCompFile; | ||
| 36 | my $help; | ||
| 37 | my $lite; | ||
| 38 | |||
| 39 | GetOptions( | ||
| 40 | 's|seq-file=s' => \$seqFile, | ||
| 41 | 'g|gc-id:i' => \$gcId, | ||
| 42 | 't|tai-param:s' => \$taiFile, | ||
| 43 | 'f|fop-param:s' => \$fopFile, | ||
| 44 | 'c|cai-param:s' => \$caiFile, | ||
| 45 | 'e|enc-methods:s' => \$encMethods, | ||
| 46 | 'l|lite!' => \$lite, | ||
| 47 | 'o|out-file:s' => \$outFile, | ||
| 48 | 'b|base-comp:s' => \$baseCompFile, | ||
| 49 | 'h|help!' => \$help | ||
| 50 | ); | ||
| 51 | |||
| 52 | &usage() if($help or !defined($seqFile)); | ||
| 53 | |||
| 54 | $gcId ||= 1; | ||
| 55 | $outFile ||= '-'; | ||
| 56 | #$encMethods ||= 'enc'; | ||
| 57 | |||
| 58 | my @encs = split ',', $encMethods if($encMethods); | ||
| 59 | |||
| 60 | # set up parameters for calculating CUB metrics | ||
| 61 | my %params; # to CUB analyzer | ||
| 62 | my %baseCompositions; | ||
| 63 | if(grep /^encp/,@encs) # there are methods needing background data | ||
| 64 | { | ||
| 65 | die "option --base-comp is missing for encp and its related methods:$!" | ||
| 66 | unless($baseCompFile); | ||
| 67 | if(-f $baseCompFile) # this is a file | ||
| 68 | { | ||
| 69 | open(B,"< $baseCompFile") or die "Can not open $baseCompFile:$!"; | ||
| 70 | while(<B>) | ||
| 71 | { | ||
| 72 | next if /^#/; | ||
| 73 | chomp; | ||
| 74 | my @fields = split "\t"; | ||
| 75 | # we may implement checking the validity of the data here in | ||
| 76 | # future | ||
| 77 | $baseCompositions{$fields[0]} = [@fields[1..4]]; | ||
| 78 | } | ||
| 79 | close B; | ||
| 80 | }else # just a comma-separated 4 values | ||
| 81 | { | ||
| 82 | my @baseFreq = split ',', $baseCompFile; | ||
| 83 | die "The length of specified base frequency '$baseCompFile'". | ||
| 84 | " is not 4:$!" unless($#baseFreq == 3); | ||
| 85 | $params{'-base_background'} = \@baseFreq; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | warn "Step 1: build analyzer\n"; | ||
| 90 | |||
| 91 | my $table = Bio::CUA::CodonTable->new(-id => $gcId) or die $!; | ||
| 92 | $params{'-codon_table'}= $table; | ||
| 93 | $params{'-tai_values'} = $taiFile if($taiFile); | ||
| 94 | $params{'-cai_values'} = $caiFile if($caiFile); | ||
| 95 | $params{'-optimal_codons'} = $fopFile if($fopFile); | ||
| 96 | |||
| 97 | my $cub = Bio::CUA::CUB::Calculator->new(%params) or die $!; | ||
| 98 | |||
| 99 | warn "Step 2: calculating parameters\n"; | ||
| 100 | my $io = $seqIO_pkg->new(-file => $seqFile, -format => 'fasta') or die | ||
| 101 | "Can't create sequence IO on $seqFile:$!"; | ||
| 102 | |||
| 103 | open(O, "> $outFile") or die "Can not open $outFile:$!"; | ||
| 104 | my @header = qw/seq_id/; | ||
| 105 | push @header, qw/length AAs GC GC3/ unless($lite); | ||
| 106 | push @header, @encs if(@encs); | ||
| 107 | push @header, 'tai' if($taiFile); | ||
| 108 | push @header, 'cai' if($caiFile); | ||
| 109 | push @header, 'fop' if($fopFile); | ||
| 110 | |||
| 111 | my @allAAtypes = sort($table->all_amino_acids); # make the amino acids | ||
| 112 | # always in alphabet order | ||
| 113 | print O "# produced by $0 @args\n"; | ||
| 114 | print O "# amino acid order for AAs: ",join(',',@allAAtypes),"\n" | ||
| 115 | unless($lite); | ||
| 116 | print O '#', join($sep, @header),"\n"; | ||
| 117 | my $counter = 0; | ||
| 118 | while(my $seq = $io->next_seq) | ||
| 119 | { | ||
| 120 | my $codonList = $cub->get_codon_list($seq) or | ||
| 121 | (warn "Get codons for ".$seq->id." failed\n" and next); | ||
| 122 | |||
| 123 | my @result = ($seq->id); | ||
| 124 | unless($lite) | ||
| 125 | { | ||
| 126 | # count amino acids | ||
| 127 | my %AAs; | ||
| 128 | my $totalPepLen = 0; | ||
| 129 | while(my ($codon, $cnt) = each %$codonList) | ||
| 130 | { | ||
| 131 | next if($table->is_stop_codon($codon)); | ||
| 132 | my $AA = $table->translate($codon) or | ||
| 133 | die "Can not translate $codon:$!"; | ||
| 134 | $AAs{$AA} += $cnt; | ||
| 135 | $totalPepLen += $cnt; | ||
| 136 | } | ||
| 137 | my $aaCounts = join(',', map { $AAs{$_} || 0 } @allAAtypes); | ||
| 138 | |||
| 139 | # now GC content | ||
| 140 | my $gcFrac = $cub->gc_frac($codonList); | ||
| 141 | my $gc3Frac = $cub->gc_frac($codonList,3); | ||
| 142 | |||
| 143 | # store results | ||
| 144 | push @result, $totalPepLen, $aaCounts; | ||
| 145 | push @result, map { _format_num($_) } ($gcFrac, $gc3Frac); | ||
| 146 | } | ||
| 147 | |||
| 148 | # get base composition for this sequence if available | ||
| 149 | my $baseComp = $baseCompositions{$seq->id} | ||
| 150 | if(%baseCompositions); | ||
| 151 | # call (codons, minTotal, base composition) | ||
| 152 | # set undef value to NA | ||
| 153 | my @encResult = @encs? | ||
| 154 | map { $cub->$_($codonList,undef,$baseComp) || 'NA' } @encs : (); | ||
| 155 | # set ENC which is > 62 to 62 | ||
| 156 | push @result, map { ($_ ne 'NA' and $_ > 62)? 62 : _format_num($_) } @encResult; | ||
| 157 | |||
| 158 | my $tai = $cub->tai($codonList) or die $! if($taiFile); | ||
| 159 | my $cai = $cub->cai($codonList) or die $! if($caiFile); | ||
| 160 | my $fop = $cub->fop($codonList) or die $! if($fopFile); | ||
| 161 | push @result, _format_num($tai) if(defined $tai); | ||
| 162 | push @result, _format_num($cai) if(defined $cai); | ||
| 163 | push @result, _format_num($fop) if(defined $fop); | ||
| 164 | |||
| 165 | print O join($sep, @result),"\n"; | ||
| 166 | |||
| 167 | warn "# $counter sequences have been analyzed\n" | ||
| 168 | if(++$counter % 500 == 0); | ||
| 169 | } | ||
| 170 | |||
| 171 | close O; | ||
| 172 | |||
| 173 | warn "Work [$counter sequences] is done!!\n"; | ||
| 174 | |||
| 175 | exit 0; | ||
| 176 | |||
| 177 | sub _format_num | ||
| 178 | { | ||
| 179 | my ($num) = @_; | ||
| 180 | return $num if($num eq 'NA'); | ||
| 181 | my $field = 7; | ||
| 182 | return sprintf("%.*f",, $field, $num); | ||
| 183 | } | ||
| 184 | |||
| 185 | sub usage | ||
| 186 | { | ||
| 187 | print <<USAGE; | ||
| 188 | Usage: $0 [options] | ||
| 189 | |||
| 190 | This program computes CUB indices and additional sequence statistics | ||
| 191 | for each sequence. Available CUB indices are ENC, CAI, tAI, Fop. | ||
| 192 | |||
| 193 | Options: | ||
| 194 | |||
| 195 | -s/--seq-file: file containing sequences in fasta format, from which | ||
| 196 | CUB indices are computed. | ||
| 197 | |||
| 198 | -g/--gc-id: genetic code table ID used for deriving amino acids for | ||
| 199 | sequences in input file. Default is 1, i.e., standard table | ||
| 200 | |||
| 201 | -t/--tai-param: the file containing tAI values for each codon in the | ||
| 202 | format 'codon<tab>tAI_value'. If not provided, tAI values would not be | ||
| 203 | computed. | ||
| 204 | |||
| 205 | -c/--cai-param: similar to --tai-param, except for CAI values. | ||
| 206 | |||
| 207 | -f/--fop-param: a file containing pre-defined optimal codons, one | ||
| 208 | codon per line. | ||
| 209 | |||
| 210 | -e/--enc-methods: methods for ENC calculation. Available values are | ||
| 211 | enc, enc_r, encp, and encp_r. | ||
| 212 | |||
| 213 | -b/--base-comp: a file or four numbers separated by comma. When | ||
| 214 | provided is a file, it is assumed that sequence-specific background | ||
| 215 | base compositions are given in the format like: | ||
| 216 | seq_id1 #A #T #C #G | ||
| 217 | seq_id2 #A #T #C #G | ||
| 218 | ... ... | ||
| 219 | When provided are numbers, it should be like | ||
| 220 | 0.2,0.3,0.3,0.2 | ||
| 221 | giving the frequency of A/T/C/G in order. | ||
| 222 | This option is needed when computing encp* versions of ENC. | ||
| 223 | |||
| 224 | -l/--lite: in default, the program outputs counts of amino acids, GC | ||
| 225 | content, and protein lengths. If this switch option is given, these | ||
| 226 | parameters will not be output. | ||
| 227 | |||
| 228 | -o/--out: the file to store the results. Default is to standard output. | ||
| 229 | |||
| 230 | -h/--help: show this message. For more details, run | ||
| 231 | 'perldoc calculate_CUB.pl' | ||
| 232 | |||
| 233 | Author: Zhenguo Zhang | ||
| 234 | Contact: zhangz.sci\@gmail.com | ||
| 235 | Created: | ||
| 236 | Sat May 2 22:22:07 EDT 2015 | ||
| 237 | |||
| 238 | |||
| 239 | USAGE | ||
| 240 | |||
| 241 | exit 1; | ||
| 242 | } | ||
| 243 | |||
| 244 | =pod | ||
| 245 | |||
| 246 | =head1 NAME | ||
| 247 | |||
| 248 | calculate_CUB.pl - a program to calculate sequence codon usage bias | ||
| 249 | metrics and other sequence parameters. | ||
| 250 | |||
| 251 | =head1 VERSION | ||
| 252 | |||
| 253 | VERSION: 0.11 | ||
| 254 | |||
| 255 | =head1 SYNOPSIS | ||
| 256 | |||
| 257 | This program computes CUB indices for each sequence; the types of | ||
| 258 | computed CUB indices depend on the provided options (see below). | ||
| 259 | |||
| 260 | In addition to CUB indices, the program also computes some other | ||
| 261 | features such as counts of amino acids, GC-content of the whole | ||
| 262 | sequence and the 3rd codon positions. | ||
| 263 | |||
| 264 | # compute ENC, ENC_r, CAI, and tAI for each sequence in file cds.fa | ||
| 265 | summarize_cds_stat.pl --cai CAI_param.top_200 --tai tAI_param \ | ||
| 266 | --enc enc,enc_r --seq cds.fa -o CUB_indice.tsv | ||
| 267 | |||
| 268 | # the same as above but not output GC content, AA counts and protein | ||
| 269 | # lengths | ||
| 270 | summarize_cds_stat.pl --cai CAI_param.top_200 --tai tAI_param \ | ||
| 271 | --enc enc,enc_r --seq cds.fa -o CUB_indice.tsv --lite | ||
| 272 | |||
| 273 | =head1 OPTIONS | ||
| 274 | |||
| 275 | =head3 Mandatory options | ||
| 276 | |||
| 277 | =over | ||
| 278 | |||
| 279 | =item -s/--seq-file | ||
| 280 | |||
| 281 | file containing sequences in fasta format, from which each sequence's | ||
| 282 | CUB indices are computed. | ||
| 283 | |||
| 284 | =back | ||
| 285 | |||
| 286 | =head3 Auxiliary options | ||
| 287 | |||
| 288 | =over | ||
| 289 | |||
| 290 | =item -g/--gc-id | ||
| 291 | |||
| 292 | ID of genetic code table used for identifying amino acid encoded by | ||
| 293 | each codon. Default is 1, i.e., standard code. See L<NCBI Genetic | ||
| 294 | Code|http://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi?mode=t> | ||
| 295 | for valid IDs. | ||
| 296 | |||
| 297 | =item -t/--tai-param | ||
| 298 | |||
| 299 | file containing tAI value for each codon in the | ||
| 300 | format 'codon<tab>tAI_value', which can be produced by | ||
| 301 | L<build_tai_param.pl>. If not given, tAI values would not be | ||
| 302 | computed. | ||
| 303 | |||
| 304 | =item -c/--cai-param | ||
| 305 | |||
| 306 | similar to --tai-param, except that CAI values are | ||
| 307 | provided in the same format. This file may be produced by | ||
| 308 | L<build_cai_param.pl>. If not given, CAI values would not be computed. | ||
| 309 | |||
| 310 | =item -f/--fop-param | ||
| 311 | |||
| 312 | a file containing pre-defined optimal codons, one codon per line. | ||
| 313 | Optimal codons can be selected using different ways, such as selecting | ||
| 314 | high-tAI codons or those preferred in highly expressed genes. | ||
| 315 | |||
| 316 | =item -e/--enc-methods | ||
| 317 | |||
| 318 | methods for ENC calculations. Available values are I<enc>, I<enc_r>, | ||
| 319 | I<encp>, and I<encp_r>. I<encp*> versions corrects background | ||
| 320 | GC-content in calculations. I<*_r> versions uses a new method to | ||
| 321 | estimate missing F values. Check module L<Bio::CUA::CUB::Calculator> | ||
| 322 | to see details of these methods. Default is I<enc>. Multiple methods | ||
| 323 | can be specified as comma-separated string such as 'enc,encp,enc_r'. | ||
| 324 | |||
| 325 | =item -b/--base-comp | ||
| 326 | |||
| 327 | This option is needed when computing encp* versions of ENC. The base | ||
| 328 | compositions are used as background base compositions to calculate | ||
| 329 | expected codon frequency in the sequences. It may be helpful for one | ||
| 330 | to exclude the effect of mutational bias on codon usage. | ||
| 331 | This option has no effect unless I<encp*> version | ||
| 332 | methods are specified in I<--enc-methods>. | ||
| 333 | |||
| 334 | Acceptable values are either a file or four numbers separated by comma. | ||
| 335 | When provided is a file, it is assumed that sequence-specific background | ||
| 336 | base compositions are given in the format like: | ||
| 337 | |||
| 338 | seq_id1 #A #T #C #G | ||
| 339 | seq_id2 #A #T #C #G | ||
| 340 | ... ... | ||
| 341 | |||
| 342 | where #A/#T/#C/#G are counts or fractions of each base type in background data | ||
| 343 | (e.g., introns) for each sequence. For sequences without background | ||
| 344 | base composition information, 'NA' will be returned from I<encp*> | ||
| 345 | methods. | ||
| 346 | |||
| 347 | When provided are numbers, it should be like | ||
| 348 | |||
| 349 | 0.2,0.3,0.3,0.2 | ||
| 350 | |||
| 351 | giving the frequency of A/T/C/G in order. | ||
| 352 | |||
| 353 | =item -l/--lite | ||
| 354 | |||
| 355 | A switch option. In default, the program outputs counts of amino acids, GC | ||
| 356 | content, and protein lengths. If this option is set these parameters will | ||
| 357 | not be output. | ||
| 358 | |||
| 359 | =item -o/--out-file | ||
| 360 | |||
| 361 | the file to store the results. Default is to standard output, usually | ||
| 362 | screen. | ||
| 363 | |||
| 364 | =item -h/--help | ||
| 365 | |||
| 366 | show the brief help message. | ||
| 367 | |||
| 368 | =back | ||
| 369 | |||
| 370 | =head1 AUTHOR | ||
| 371 | |||
| 372 | Zhenguo Zhang, C<< <zhangz.sci at gmail.com> >> | ||
| 373 | |||
| 374 | =head1 BUGS | ||
| 375 | |||
| 376 | Please report any bugs or feature requests to C<bug-bio-cua at | ||
| 377 | rt.cpan.org> or through the web interface at | ||
| 378 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Bio-CUA>. I will be | ||
| 379 | notified, and then you'll automatically be notified of progress on | ||
| 380 | your bug as I make changes. | ||
| 381 | |||
| 382 | =cut | ||
| 383 | |||
| 384 | =head1 SUPPORT | ||
| 385 | |||
| 386 | You can find documentation for this class with the perldoc command. | ||
| 387 | |||
| 388 | perldoc Bio::CUA | ||
| 389 | |||
| 390 | You can also look for information at: | ||
| 391 | |||
| 392 | =over 4 | ||
| 393 | |||
| 394 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 395 | |||
| 396 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Bio-CUA> | ||
| 397 | |||
| 398 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 399 | |||
| 400 | L<http://annocpan.org/dist/Bio-CUA> | ||
| 401 | |||
| 402 | =item * CPAN Ratings | ||
| 403 | |||
| 404 | L<http://cpanratings.perl.org/d/Bio-CUA> | ||
| 405 | |||
| 406 | =item * Search CPAN | ||
| 407 | |||
| 408 | L<http://search.cpan.org/dist/Bio-CUA/> | ||
| 409 | |||
| 410 | =back | ||
| 411 | |||
| 412 | |||
| 413 | =head1 ACKNOWLEDGEMENTS | ||
| 414 | |||
| 415 | =head1 UPDATES | ||
| 416 | |||
| 417 | 0.11 - Thu May 21 16:00:28 EDT 2015 | ||
| 418 | |||
| 419 | 1. modify/add option --base-comp and --lite. | ||
| 420 | |||
| 421 | =head1 LICENSE AND COPYRIGHT | ||
| 422 | |||
| 423 | Copyright 2015 Zhenguo Zhang. | ||
| 424 | |||
| 425 | This program is free software: you can redistribute it and/or modify | ||
| 426 | it under the terms of the GNU General Public License as published by | ||
| 427 | the Free Software Foundation, either version 3 of the License, or | ||
| 428 | (at your option) any later version. | ||
| 429 | |||
| 430 | This program is distributed in the hope that it will be useful, | ||
| 431 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 432 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 433 | GNU General Public License for more details. | ||
| 434 | |||
| 435 | You should have received a copy of the GNU General Public License | ||
| 436 | along with this program. If not, see L<http://www.gnu.org/licenses/>. | ||
| 437 | |||
| 438 | =cut | ||
| 439 |