| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use lib '../lib'; | ||
| 6 | use Bio::CUA::CUB::Builder; | ||
| 7 | use Bio::CUA::CodonTable; | ||
| 8 | use Getopt::Long; | ||
| 9 | use Fcntl qw/:seek/; | ||
| 10 | use File::Sort qw/sort_file/; | ||
| 11 | use File::Temp qw/ tempfile tempdir /; | ||
| 12 | our $VERSION = 0.01; | ||
| 13 | |||
| 14 | my $seqIO_pkg; | ||
| 15 | |||
| 16 | BEGIN{ | ||
| 17 | eval { require Bio::SeqIO; }; | ||
| 18 | |||
| 19 | if($@) # bioperl is not installed | ||
| 20 | { | ||
| 21 | require Bio::CUA::SeqIO; | ||
| 22 | $seqIO_pkg = 'Bio::CUA::SeqIO'; | ||
| 23 | }else | ||
| 24 | { | ||
| 25 | $seqIO_pkg = 'Bio::SeqIO'; | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | my $sep = "\t"; # field separator | ||
| 30 | my $seqFile; | ||
| 31 | my $expFile; | ||
| 32 | my $gcId; | ||
| 33 | my $outFile; | ||
| 34 | my $select; | ||
| 35 | my $background; | ||
| 36 | my $normMethod; | ||
| 37 | my $minTotal; # the minimal count of an amino acid for being | ||
| 38 | # considered to calculate CAI parameters | ||
| 39 | my $help; | ||
| 40 | |||
| 41 | GetOptions( | ||
| 42 | 'i|seq-file=s' => \$seqFile, | ||
| 43 | 'e|exp-file:s' => \$expFile, | ||
| 44 | 's|select:s' => \$select, | ||
| 45 | 'b|background:s' => \$background, | ||
| 46 | 'o|out-file:s' => \$outFile, | ||
| 47 | 'g|gc-id:i' => \$gcId, | ||
| 48 | 'm|method:s' => \$normMethod, | ||
| 49 | 'h|help!' => \$help | ||
| 50 | ); | ||
| 51 | |||
| 52 | &usage() if($help or !defined($seqFile)); | ||
| 53 | |||
| 54 | if($select) | ||
| 55 | { | ||
| 56 | die "option '--exp-file' is needed as option '--select' is provided:$!" | ||
| 57 | unless($expFile); | ||
| 58 | } | ||
| 59 | |||
| 60 | $gcId ||= 1; | ||
| 61 | $outFile ||= '-'; | ||
| 62 | $select ||= 'all'; | ||
| 63 | $select = lc($select); | ||
| 64 | $normMethod ||= 'max'; | ||
| 65 | |||
| 66 | warn "# Step 1: tabulate codons from sequences\n"; | ||
| 67 | my $table = Bio::CUA::CodonTable->new(-id => $gcId) | ||
| 68 | or die "Creating codon table failed:$!"; | ||
| 69 | my $builder = Bio::CUA::CUB::Builder->new( | ||
| 70 | -codon_table => $table | ||
| 71 | ) | ||
| 72 | or die "Creating analyzer failed:$!"; | ||
| 73 | |||
| 74 | my $codonListRef; | ||
| 75 | my ($sortedFh, $sortedExpFile); | ||
| 76 | if($expFile) | ||
| 77 | { | ||
| 78 | # sort this $expFile first | ||
| 79 | ($sortedFh, $sortedExpFile) = tempfile(); | ||
| 80 | #warn "$sortedExpFile\n"; | ||
| 81 | sort_file({ | ||
| 82 | I => $expFile, | ||
| 83 | o => $sortedExpFile, | ||
| 84 | k => '2,2n', | ||
| 85 | t => "\t" # field separator | ||
| 86 | }); | ||
| 87 | my $idsRef; | ||
| 88 | if($select eq 'all') | ||
| 89 | { | ||
| 90 | $idsRef = choose_ids($sortedFh, 'all'); | ||
| 91 | }elsif($select < 1) # a fraction | ||
| 92 | { | ||
| 93 | my $total = num_of_lines($expFile); | ||
| 94 | $select = int($total * $select); | ||
| 95 | # largest is at the end, -r option for sort does not work | ||
| 96 | $idsRef = choose_ids($sortedFh, 'high', $select); | ||
| 97 | }elsif($select > 1) # number of genes | ||
| 98 | { | ||
| 99 | $select = int($select); | ||
| 100 | $idsRef = choose_ids($sortedFh, 'high', $select); | ||
| 101 | }else | ||
| 102 | { | ||
| 103 | die "Unknown option '$select' for --select:$!"; | ||
| 104 | } | ||
| 105 | |||
| 106 | $codonListRef = &read_codons($seqFile,$idsRef); | ||
| 107 | # output_ids($idsRef,'positive'); | ||
| 108 | }else # consider codons in all input sequences | ||
| 109 | { | ||
| 110 | $codonListRef = &read_codons($seqFile); | ||
| 111 | } | ||
| 112 | |||
| 113 | # now consider background data | ||
| 114 | |||
| 115 | my $backCodonListRef; | ||
| 116 | if($background) | ||
| 117 | { | ||
| 118 | if(is_numeric($background)) | ||
| 119 | { | ||
| 120 | die "option '--exp-file' is needed for selecting background data:$!" | ||
| 121 | unless($sortedFh); | ||
| 122 | |||
| 123 | my $idsRef; | ||
| 124 | if($background < 1) # a fraction | ||
| 125 | { | ||
| 126 | my $total = num_of_lines($expFile); | ||
| 127 | $background = int($total * $background); | ||
| 128 | # largest is at the end, -r option for sort does not work | ||
| 129 | $idsRef = choose_ids($sortedFh, 'low', $background); | ||
| 130 | }elsif($background > 1) # number of genes | ||
| 131 | { | ||
| 132 | $background = int($background); | ||
| 133 | $idsRef = choose_ids($sortedFh, 'low', $background); | ||
| 134 | }else | ||
| 135 | { | ||
| 136 | die "Unknown option '$background' for --background:$!"; | ||
| 137 | } | ||
| 138 | |||
| 139 | $backCodonListRef = &read_codons($seqFile,$idsRef); | ||
| 140 | |||
| 141 | #output_ids($idsRef,'negative'); | ||
| 142 | }elsif(-f $background) # a sequence file | ||
| 143 | { | ||
| 144 | $backCodonListRef = &read_codons($background) or | ||
| 145 | die "reading codons from $background failed:$!"; | ||
| 146 | }else | ||
| 147 | { | ||
| 148 | die "Unknown data '$background' for option --background:$!"; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | warn "# Step 2: calculate CAIs for codons\n"; | ||
| 153 | if($background) | ||
| 154 | { | ||
| 155 | $builder->build_b_cai($codonListRef, $backCodonListRef, | ||
| 156 | $minTotal, $outFile) or die "building codons' b_CAI failed:$!"; | ||
| 157 | }else | ||
| 158 | { | ||
| 159 | $builder->build_cai($codonListRef, $minTotal, $normMethod, | ||
| 160 | $outFile) or | ||
| 161 | die "building codons' CAI failed:$!"; | ||
| 162 | } | ||
| 163 | |||
| 164 | # remove temporary files | ||
| 165 | if($sortedExpFile) | ||
| 166 | { | ||
| 167 | close $sortedFh; | ||
| 168 | unlink $sortedExpFile; | ||
| 169 | } | ||
| 170 | |||
| 171 | warn "Work done!!\n"; | ||
| 172 | |||
| 173 | exit 0; | ||
| 174 | |||
| 175 | sub output_ids | ||
| 176 | { | ||
| 177 | my ($hash, $name) = @_; | ||
| 178 | |||
| 179 | open(O,"> tmp_$name") or die $!; | ||
| 180 | print O join("\n", keys %$hash), "\n"; | ||
| 181 | close O; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub is_numeric | ||
| 185 | { | ||
| 186 | my $num = shift; | ||
| 187 | if($num =~ /^[+-]?[eE\d\.\-]+$/) | ||
| 188 | { | ||
| 189 | return 1; | ||
| 190 | }else | ||
| 191 | { | ||
| 192 | return 0; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | # get the number of lines in a file | ||
| 197 | sub num_of_lines | ||
| 198 | { | ||
| 199 | my ($file) = @_; | ||
| 200 | my $totalNum; | ||
| 201 | if(ref($file) eq 'GLOB') | ||
| 202 | { | ||
| 203 | $. = 0; # reset number to 0 | ||
| 204 | my $currPos = tell($file); | ||
| 205 | seek($file, 0, SEEK_SET); # set to beginning | ||
| 206 | 1 while(<$file>); | ||
| 207 | $totalNum = $.; # a special variable | ||
| 208 | seek($file,$currPos,SEEK_SET); # set back the pointer | ||
| 209 | }else | ||
| 210 | { | ||
| 211 | my $fh; | ||
| 212 | open($fh, "< $file") or die "Can not open $file:$!"; | ||
| 213 | 1 while(<$fh>); | ||
| 214 | $totalNum = $.; # a special variable | ||
| 215 | close $fh; | ||
| 216 | } | ||
| 217 | return $totalNum; | ||
| 218 | } | ||
| 219 | |||
| 220 | # read codons from given sequence IDs | ||
| 221 | sub read_codons | ||
| 222 | { | ||
| 223 | my ($seqFile,$idsRef) = @_; | ||
| 224 | # if $idsRef is not a hash reference, all the sequences in the | ||
| 225 | # input file will be scanned | ||
| 226 | |||
| 227 | if(!defined($idsRef)) # no selected IDs | ||
| 228 | { | ||
| 229 | return $builder->get_codon_list($seqFile); | ||
| 230 | } | ||
| 231 | |||
| 232 | my $io = $seqIO_pkg->new(-file => $seqFile, -format => 'fasta'); | ||
| 233 | my %codonList; # store all the codons of selected sequences | ||
| 234 | my $seqCnt = 0; | ||
| 235 | |||
| 236 | while(my $seq = $io->next_seq) | ||
| 237 | { | ||
| 238 | next unless($idsRef->{$seq->id}); | ||
| 239 | my $localCodons = $builder->get_codon_list($seq) or | ||
| 240 | (warn "Counting codons in ".$seq->id." failed\n" and next); | ||
| 241 | while(my ($codon, $cnt) = each %$localCodons) | ||
| 242 | { | ||
| 243 | $codonList{$codon} += $cnt; | ||
| 244 | } | ||
| 245 | $seqCnt++; | ||
| 246 | } | ||
| 247 | |||
| 248 | return \%codonList; | ||
| 249 | } | ||
| 250 | |||
| 251 | # choose IDs from an input file | ||
| 252 | sub choose_ids | ||
| 253 | { | ||
| 254 | my ($fh, $method, $cnt) = @_; | ||
| 255 | $method = lc($method); | ||
| 256 | # set the filehandle to the beginning | ||
| 257 | seek($fh,0,SEEK_SET); | ||
| 258 | my %data; | ||
| 259 | my $counter = 0; | ||
| 260 | if($method eq 'all') | ||
| 261 | { | ||
| 262 | while(<$fh>) | ||
| 263 | { | ||
| 264 | chomp; | ||
| 265 | my @fields = split $sep; | ||
| 266 | $data{$fields[0]}++; | ||
| 267 | } | ||
| 268 | # return _read_column($fh, 1); | ||
| 269 | }elsif($method eq 'low') | ||
| 270 | { | ||
| 271 | while(<$fh>) | ||
| 272 | { | ||
| 273 | chomp; | ||
| 274 | my @fields = split $sep; | ||
| 275 | $data{$fields[0]}++; | ||
| 276 | last unless(++$counter < $cnt); | ||
| 277 | } | ||
| 278 | }elsif($method eq 'high') | ||
| 279 | { | ||
| 280 | my $toSkip = num_of_lines($fh) - $cnt; | ||
| 281 | while(<$fh>) | ||
| 282 | { | ||
| 283 | next unless(++$counter > $toSkip); | ||
| 284 | chomp; | ||
| 285 | my @fields = split $sep; | ||
| 286 | $data{$fields[0]}++; | ||
| 287 | } | ||
| 288 | }else | ||
| 289 | { | ||
| 290 | die "Unknown method $method for choosing IDs in 'choose_ids':$!"; | ||
| 291 | } | ||
| 292 | |||
| 293 | return \%data; | ||
| 294 | } | ||
| 295 | |||
| 296 | sub _read_column | ||
| 297 | { | ||
| 298 | # the $fh may not be at the beginning | ||
| 299 | my ($fh, $colNum, $cnt) = @_; | ||
| 300 | my %data; | ||
| 301 | while(<$fh>) | ||
| 302 | { | ||
| 303 | last if(defined($cnt) and $cnt == 0); | ||
| 304 | chomp; | ||
| 305 | my @fields = split $sep; | ||
| 306 | $data{$fields[$colNum - 1]}++; | ||
| 307 | } | ||
| 308 | |||
| 309 | return \%data; | ||
| 310 | } | ||
| 311 | |||
| 312 | sub usage | ||
| 313 | { | ||
| 314 | print <<USAGE; | ||
| 315 | Usage: $0 [options] | ||
| 316 | |||
| 317 | This program reads into fasta-formatted sequences and ouput CAI values | ||
| 318 | for each codon. | ||
| 319 | |||
| 320 | Mandatory options: | ||
| 321 | |||
| 322 | -i/--seq-file: a file containing protein-coding sequences in fasta | ||
| 323 | format | ||
| 324 | |||
| 325 | Auxiliary options: | ||
| 326 | |||
| 327 | -e/--exp-file: a file containing sequence IDs and their expression | ||
| 328 | |||
| 329 | -s/--select: how many sequences are chosen for CAI calculation. | ||
| 330 | all = all IDs in the expression file given by --exp-file | ||
| 331 | 0.## = the top fraction of highly expressed genes, say 0.30, then top | ||
| 332 | 30% highly expressed genes. | ||
| 333 | ### = an integer such as 200, then the top 200 highly expressed | ||
| 334 | genes. | ||
| 335 | Default is 'all'. | ||
| 336 | |||
| 337 | -b/--background: data for background codon usage estimation. | ||
| 338 | 0.## = a fraction (e.g.,0.30) of most lowly expressed genes in the | ||
| 339 | expression file by --exp-file | ||
| 340 | ### = a number (say 200) of most lowly expressed genes in the | ||
| 341 | expression file. | ||
| 342 | filename = a file containing protein-coding sequences which will be | ||
| 343 | anlyzed for background codon usage. | ||
| 344 | |||
| 345 | -g/--gc-id: ID of genetic code table. | ||
| 346 | |||
| 347 | -m/--method: method to calculate CAI. Available values are 'max' | ||
| 348 | (default) and 'mean'. | ||
| 349 | |||
| 350 | -o/--out-file: the file to store the result. Default is standard output. | ||
| 351 | |||
| 352 | -h/--help: show this help message. For more detailed information, run | ||
| 353 | 'perldoc build_cai_param.pl' | ||
| 354 | |||
| 355 | Author: Zhenguo Zhang | ||
| 356 | Contact: zhangz.sci\@gmail.com | ||
| 357 | Created: | ||
| 358 | Wed Mar 4 16:42:24 EST 2015 | ||
| 359 | |||
| 360 | USAGE | ||
| 361 | |||
| 362 | exit 1; | ||
| 363 | } | ||
| 364 | |||
| 365 | =pod | ||
| 366 | |||
| 367 | =head1 NAME | ||
| 368 | |||
| 369 | build_cai_param.pl - a program to calculate CAI for each codon | ||
| 370 | |||
| 371 | =head1 VERSION | ||
| 372 | |||
| 373 | VERSION = 0.01 | ||
| 374 | |||
| 375 | =head1 SYNOPSIS | ||
| 376 | |||
| 377 | This is a program to compute CAI at codon level with different | ||
| 378 | methods. It is part of distribution | ||
| 379 | L<http://search.cpan.org/dist/Bio-CUA/> | ||
| 380 | |||
| 381 | # calculate codon CAI by choosing the top 200 highly expressed genes | ||
| 382 | build_cai_param.pl -i seqs.fasta -e gene_expression.tsv -s 200 -o CAI_top200 | ||
| 383 | |||
| 384 | # the same as above but normalize RSCUs with expected RSCUs under even | ||
| 385 | # codon usage | ||
| 386 | build_cai_param.pl -i seqs.fasta -e gene_expression.tsv -s 200 -o CAI_top200.by_mean -m mean | ||
| 387 | |||
| 388 | # normalize RSCUs by RSCUs derived from bottom 1000 lowely expressed genes | ||
| 389 | build_cai_param.pl -i seqs.fasta -e gene_expression.tsv -s 200 -o CAI_top200.b1000 -b 1000 | ||
| 390 | |||
| 391 | =head1 OPTIONS | ||
| 392 | |||
| 393 | All options have a short and a long forms, e.g., -i and --seq-file for | ||
| 394 | first option. | ||
| 395 | |||
| 396 | In the following text, RSCU stands for relative synonymous codon | ||
| 397 | usage. | ||
| 398 | |||
| 399 | =head3 Mandatory options | ||
| 400 | |||
| 401 | =over | ||
| 402 | |||
| 403 | =item -i/--seq-file | ||
| 404 | |||
| 405 | a file containing protein-coding sequences in fasta format. | ||
| 406 | |||
| 407 | =back | ||
| 408 | |||
| 409 | =head3 Auxiliary options | ||
| 410 | |||
| 411 | =over | ||
| 412 | |||
| 413 | =item -e/--exp-file | ||
| 414 | |||
| 415 | a file containing sequence IDs and their expression in the forllowing | ||
| 416 | format: | ||
| 417 | |||
| 418 | seq-id1E<lt>tabE<gt>0.67 | ||
| 419 | seq-id2E<lt>tabE<gt>2.57 | ||
| 420 | ... ... | ||
| 421 | |||
| 422 | each line contains one sequence ID and the sequence's gene expression | ||
| 423 | level (RNA, protein, or else), separated by tab. The sequence IDs | ||
| 424 | must match the IDs in the sequence file specified above. | ||
| 425 | |||
| 426 | From this file, highly expressed genes will be selected according to | ||
| 427 | the gene expression rank. See below options. | ||
| 428 | |||
| 429 | If this option is omitted, all the sequences in the above sequence | ||
| 430 | file would be used for calculating CAIs. | ||
| 431 | |||
| 432 | =item -s/--select | ||
| 433 | |||
| 434 | determine how many sequences are chosen from the above expression | ||
| 435 | file (by option --exp-file). Available formats are: | ||
| 436 | |||
| 437 | I<all>, all IDs in the expression file are chosen. | ||
| 438 | |||
| 439 | I<0.##>, a fraction of top highly expressed genes, say 0.30, then top | ||
| 440 | 30% highly expressed genes are chosen. | ||
| 441 | |||
| 442 | I<###>, an integer, say 200, then the top 200 highly expressed genes | ||
| 443 | are chosen. | ||
| 444 | |||
| 445 | Default is I<all>. If the option --exp-file is omitted, this option | ||
| 446 | has no effect. | ||
| 447 | |||
| 448 | =item -b/--background | ||
| 449 | |||
| 450 | specify background data (e.g., lowly expressed | ||
| 451 | genes) from which the background codon usage is derived. Then each | ||
| 452 | codon's RSCU from highly expressed genes is divided by the codon's | ||
| 453 | RSCU from the background data; these normalized RSCUs are used for CAI | ||
| 454 | calculation. This method is termed 'background-normalization'. | ||
| 455 | |||
| 456 | How to specify background data: I<0.##>, I<###>, or I<filename>, the | ||
| 457 | former two formats choose a fraction | ||
| 458 | of or a number of genes from the most lowly expressed genes specified | ||
| 459 | in the expression file by --exp-file. See option --select for details | ||
| 460 | of the two specification formats. The last format specifies a | ||
| 461 | fasta-formatted sequence file from which background codon usage is | ||
| 462 | calculated. | ||
| 463 | |||
| 464 | =item -g/--gc-id | ||
| 465 | |||
| 466 | ID of genetic code table. See L<NCBI genetic code| | ||
| 467 | http://www.ncbi.nlm.nih.gov/Taxonomy/taxonomyhome.html/index.cgi?chapter=cgencodes> | ||
| 468 | for valid IDs. Default is 1, i.e., standard genetic code. | ||
| 469 | |||
| 470 | =item -m/--method | ||
| 471 | |||
| 472 | method to calculated CAI: I<max> or I<mean>. | ||
| 473 | The former is used by <Sharp and Li, 1987, NAR>, in which each codon's | ||
| 474 | RSCU is divided by the maximum of all synonymous codons to derive CAI. | ||
| 475 | The 'mean' method divides each codon's RSCU by the expected RSCU under | ||
| 476 | even codon usage to get CAI. For example, for an amino acid with four synonymous | ||
| 477 | codons, the expected RSCU is 0.25 for each codon, so all observed | ||
| 478 | RSCUs of this amino acid's codons are divided by 0.25. | ||
| 479 | |||
| 480 | If option C<--background> is activated, the 'background-normalization' | ||
| 481 | method always uses the I<max> method to get final CAIs. | ||
| 482 | |||
| 483 | =item -o/--out-file | ||
| 484 | |||
| 485 | file to store the result. Default is standard output, usually screen. | ||
| 486 | |||
| 487 | =back | ||
| 488 | |||
| 489 | =head1 AUTHOR | ||
| 490 | |||
| 491 | Zhenguo Zhang, C<< <zhangz.sci at gmail.com> >> | ||
| 492 | |||
| 493 | =head1 BUGS | ||
| 494 | |||
| 495 | Please report any bugs or feature requests to C<bug-bio-cua at | ||
| 496 | rt.cpan.org> or through the web interface at | ||
| 497 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Bio-CUA>. I will be | ||
| 498 | notified, and then you'll automatically be notified of progress on | ||
| 499 | your bug as I make changes. | ||
| 500 | |||
| 501 | =cut | ||
| 502 | |||
| 503 | =head1 SUPPORT | ||
| 504 | |||
| 505 | You can find documentation for this class with the perldoc command. | ||
| 506 | |||
| 507 | perldoc Bio::CUA | ||
| 508 | |||
| 509 | You can also look for information at: | ||
| 510 | |||
| 511 | =over 4 | ||
| 512 | |||
| 513 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 514 | |||
| 515 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Bio-CUA> | ||
| 516 | |||
| 517 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 518 | |||
| 519 | L<http://annocpan.org/dist/Bio-CUA> | ||
| 520 | |||
| 521 | =item * CPAN Ratings | ||
| 522 | |||
| 523 | L<http://cpanratings.perl.org/d/Bio-CUA> | ||
| 524 | |||
| 525 | =item * Search CPAN | ||
| 526 | |||
| 527 | L<http://search.cpan.org/dist/Bio-CUA/> | ||
| 528 | |||
| 529 | =back | ||
| 530 | |||
| 531 | |||
| 532 | =head1 ACKNOWLEDGEMENTS | ||
| 533 | |||
| 534 | |||
| 535 | =head1 LICENSE AND COPYRIGHT | ||
| 536 | |||
| 537 | Copyright 2015 Zhenguo Zhang. | ||
| 538 | |||
| 539 | This program is free software: you can redistribute it and/or modify | ||
| 540 | it under the terms of the GNU General Public License as published by | ||
| 541 | the Free Software Foundation, either version 3 of the License, or | ||
| 542 | (at your option) any later version. | ||
| 543 | |||
| 544 | This program is distributed in the hope that it will be useful, | ||
| 545 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 546 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 547 | GNU General Public License for more details. | ||
| 548 | |||
| 549 | You should have received a copy of the GNU General Public License | ||
| 550 | along with this program. If not, see L<http://www.gnu.org/licenses/>. | ||
| 551 | |||
| 552 | =cut | ||
| 553 |