| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use lib '../lib'; | ||
| 6 | use Bio::CUA::Summarizer; | ||
| 7 | use Bio::CUA::SeqIO; | ||
| 8 | use Getopt::Long; | ||
| 9 | |||
| 10 | my $sep = "\t"; | ||
| 11 | my @args = @ARGV; | ||
| 12 | my $seqFile; | ||
| 13 | my $idFile; | ||
| 14 | my $outFile; | ||
| 15 | my $help; | ||
| 16 | my $each; | ||
| 17 | my $gcId = 1; | ||
| 18 | |||
| 19 | GetOptions( | ||
| 20 | 's|seq-file=s' => \$seqFile, | ||
| 21 | 'i|id-file:s' => \$idFile, | ||
| 22 | 'o|out-file:s' => \$outFile, | ||
| 23 | 'h|help!' => \$help, | ||
| 24 | "e|each!" => \$each | ||
| 25 | ); | ||
| 26 | |||
| 27 | &usage() if($help or !defined($seqFile)); | ||
| 28 | |||
| 29 | $outFile ||= '-'; | ||
| 30 | |||
| 31 | my $idsRef = _read_ids($idFile) if($idFile); | ||
| 32 | |||
| 33 | my $sum = Bio::CUA::Summarizer->new(-codon_table => $gcId); | ||
| 34 | # get codon order | ||
| 35 | my $table = $sum->codon_table; | ||
| 36 | my @codons = $table->all_codons or die $!; | ||
| 37 | # get AA order | ||
| 38 | my @AAs = map {$table->translate($_)} @codons; | ||
| 39 | |||
| 40 | my $outFh; | ||
| 41 | open($outFh, "> $outFile") or die "Cannot open $outFile for write:$!"; | ||
| 42 | print $outFh "# Produced by $0 @args\n"; | ||
| 43 | |||
| 44 | warn "# Counting codons from $seqFile ...\n"; | ||
| 45 | if($each) # we have process each sequence separately | ||
| 46 | { | ||
| 47 | # print outfile headers | ||
| 48 | print $outFh "# each row contains the counts of codons for each". | ||
| 49 | " sequence and the last comment line lists the amino acids coded". | ||
| 50 | " by codons\n"; | ||
| 51 | print $outFh "#", join($sep,"seqId", @AAs),"\n"; | ||
| 52 | print $outFh join($sep,"seqId", @codons),"\n"; | ||
| 53 | my $counter = 0; | ||
| 54 | # process each sequence separately | ||
| 55 | my $io = Bio::CUA::SeqIO->new(-file => $seqFile, -format => 'fasta') or | ||
| 56 | die "Cannot open '$seqFile' for codons:$!"; | ||
| 57 | while(my $seq = $io->next_seq) | ||
| 58 | { | ||
| 59 | my $id = $seq->id; | ||
| 60 | next unless(!$idsRef or exists($idsRef->{$id})); | ||
| 61 | my $localCodons = $sum->get_codon_list($seq) or | ||
| 62 | (warn "Counting codons in $id failed\n" and next); | ||
| 63 | &output_codon_row($id,$localCodons); | ||
| 64 | warn "# $counter sequences have been processed\n" | ||
| 65 | if(++$counter % 2000 == 0); | ||
| 66 | } | ||
| 67 | }else | ||
| 68 | { | ||
| 69 | # print outfile headers | ||
| 70 | print $outFh "# each row is the codon, its count, and the coded amino acid\n"; | ||
| 71 | print $outFh join($sep, qw/codon count AA/),"\n"; | ||
| 72 | my $codonList; | ||
| 73 | if($idsRef) # sequences have to be filtered | ||
| 74 | { | ||
| 75 | my $io = Bio::CUA::SeqIO->new(-file => $seqFile, -format => | ||
| 76 | 'fasta') or die "Cannot open '$seqFile' for codons:$!"; | ||
| 77 | while(my $seq = $io->next_seq) | ||
| 78 | { | ||
| 79 | my $id = $seq->id; | ||
| 80 | next unless(exists($idsRef->{$id})); | ||
| 81 | my $localCodons = $sum->get_codon_list($seq) or | ||
| 82 | (warn "Counting codons in $id failed\n" and next); | ||
| 83 | while(my ($codon, $cnt) = each %$localCodons) | ||
| 84 | { | ||
| 85 | $codonList->{$codon} += $cnt; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | }else # no filtering | ||
| 89 | { | ||
| 90 | $codonList = $sum->get_codon_list($seqFile) or | ||
| 91 | die "Get codons from '$seqFile' failed:$!"; | ||
| 92 | } | ||
| 93 | &output_codon_column($codonList); | ||
| 94 | } | ||
| 95 | |||
| 96 | close $outFh; | ||
| 97 | warn "Work done!!\n"; | ||
| 98 | |||
| 99 | exit 0; | ||
| 100 | |||
| 101 | # write out codons by column | ||
| 102 | sub output_codon_column | ||
| 103 | { | ||
| 104 | my $codonHash = shift; | ||
| 105 | |||
| 106 | for(my $i = 0; $i <= $#codons; $i++) | ||
| 107 | { | ||
| 108 | my $codon = $codons[$i]; | ||
| 109 | print $outFh join($sep, $codon, $codonHash->{$codon},$AAs[$i]),"\n"; | ||
| 110 | } | ||
| 111 | |||
| 112 | return 1; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub output_codon_row | ||
| 116 | { | ||
| 117 | my ($id, $codonHash) = @_; | ||
| 118 | |||
| 119 | my @orderedCnts = map {$codonHash->{$_} || 0} @codons; | ||
| 120 | |||
| 121 | print $outFh join($sep, $id, @orderedCnts), "\n"; | ||
| 122 | |||
| 123 | return 1; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub _read_ids | ||
| 127 | { | ||
| 128 | my $file = shift; | ||
| 129 | |||
| 130 | my $fh; | ||
| 131 | open($fh, "< $file") or die "Can not open $file:$!"; | ||
| 132 | my %ids; | ||
| 133 | while(<$fh>) | ||
| 134 | { | ||
| 135 | next if /^#/ or /^\s*$/; | ||
| 136 | chomp; | ||
| 137 | my ($id) = split $sep; | ||
| 138 | $ids{$id}++; | ||
| 139 | } | ||
| 140 | close $fh; | ||
| 141 | |||
| 142 | return \%ids; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub usage | ||
| 146 | { | ||
| 147 | print <<USAGE; | ||
| 148 | |||
| 149 | Usage: $0 [options] | ||
| 150 | |||
| 151 | This program reads fasta-formated seqences and counts codons in them. | ||
| 152 | |||
| 153 | Options: | ||
| 154 | |||
| 155 | Mandatory options: | ||
| 156 | |||
| 157 | -s/--seq-file: the file containing fasta-formated sequences. | ||
| 158 | |||
| 159 | Auxiliary options: | ||
| 160 | |||
| 161 | -i/--id-file: a file containing sequence IDs (should match those in the | ||
| 162 | sequence file by '--seq-file'), one ID per line. If this file is | ||
| 163 | provided, only sequences matching the contained IDs will be analyzed. | ||
| 164 | Otherwise all the sequences in the input sequence file. | ||
| 165 | |||
| 166 | -e/--each: a switch option. If provided, codons are counted for each | ||
| 167 | sequence. Default is summing up all the codons in all the sequences. | ||
| 168 | |||
| 169 | -o/--out-file: the file to store the result. Default is standard | ||
| 170 | output, usually the screen. | ||
| 171 | |||
| 172 | -h/--help: show this help message. For more detailed information, run | ||
| 173 | 'perldoc tabulate_codons.pl' | ||
| 174 | |||
| 175 | Author: Zhenguo Zhang | ||
| 176 | Contact: zhangz.sci\@gmail.com | ||
| 177 | Created: | ||
| 178 | Sun Jul 12 13:09:56 EDT 2015 | ||
| 179 | |||
| 180 | USAGE | ||
| 181 | |||
| 182 | exit 1; | ||
| 183 | } | ||
| 184 | |||
| 185 | =pod | ||
| 186 | |||
| 187 | =head1 NAME | ||
| 188 | |||
| 189 | tabulate_codons.pl - a program to output codons in a sequence file as | ||
| 190 | a table | ||
| 191 | |||
| 192 | =head1 VERSION | ||
| 193 | |||
| 194 | VERSION: 0.01 | ||
| 195 | |||
| 196 | =head1 SYNOPSIS | ||
| 197 | |||
| 198 | This progran reads fasta-formated sequences and output codon counts in | ||
| 199 | the sequence as a table. | ||
| 200 | |||
| 201 | # count the codons of all sequences in longest_cds.dmel_5_57.fa | ||
| 202 | tabulate_codons.pl -s longest_cds.dmel_5_57.fa -o codon_counts.tsv | ||
| 203 | |||
| 204 | # the same as above, but output counts for each sequence | ||
| 205 | tabulate_codons.pl -s longest_cds.dmel_5_57.fa -o codon_counts_each.tsv --each | ||
| 206 | |||
| 207 | # you can also count for a subset of sequences, like | ||
| 208 | tabulate_codons.pl -s longest_cds.dmel_5_57.fa -o \ | ||
| 209 | codon_counts_sub.tsv --id-file subset_ids.tsv | ||
| 210 | |||
| 211 | # subset_ids.tsv contains sequence IDs, one ID per line | ||
| 212 | |||
| 213 | =head1 OPTIONS | ||
| 214 | |||
| 215 | =head3 Mandatory options: | ||
| 216 | |||
| 217 | =over | ||
| 218 | |||
| 219 | =item -s/--seq-file | ||
| 220 | |||
| 221 | the file containing fasta-formated sequences. | ||
| 222 | |||
| 223 | =back | ||
| 224 | |||
| 225 | =head3 Auxiliary options: | ||
| 226 | |||
| 227 | =over | ||
| 228 | |||
| 229 | =item -i/--id-file | ||
| 230 | |||
| 231 | a file containing sequence IDs. This option is used to filter the | ||
| 232 | sequences in the file by option L</-s>, so that only sequences listed | ||
| 233 | in this id-file are parsed. Note, the IDs should match those in the | ||
| 234 | sequence file by L</-s>, one ID per line. | ||
| 235 | In default, all the sequences in the input file are analyzed. | ||
| 236 | |||
| 237 | =item -e/--each | ||
| 238 | |||
| 239 | a switch option. If provided, codons are counted for each | ||
| 240 | sequence. Default is summing up all the codons in all the sequences. | ||
| 241 | |||
| 242 | =item -o/--out-file | ||
| 243 | |||
| 244 | the file to store the result. Default is standard output. | ||
| 245 | |||
| 246 | =item -h/--help | ||
| 247 | |||
| 248 | show this help message. For more detailed information, run | ||
| 249 | 'perldoc tabulate_codons.pl' | ||
| 250 | |||
| 251 | =back | ||
| 252 | |||
| 253 | =head1 AUTHOR | ||
| 254 | |||
| 255 | Zhenguo Zhang, C<< <zhangz.sci at gmail.com> >> | ||
| 256 | |||
| 257 | =head1 BUGS | ||
| 258 | |||
| 259 | Please report any bugs or feature requests to C<bug-bio-cua at | ||
| 260 | rt.cpan.org> or through the web interface at | ||
| 261 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Bio-CUA>. I will be | ||
| 262 | notified, and then you'll automatically be notified of progress on | ||
| 263 | your bug as I make changes. | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | =head1 SUPPORT | ||
| 268 | |||
| 269 | You can find documentation for this class with the perldoc command. | ||
| 270 | |||
| 271 | perldoc Bio::CUA | ||
| 272 | |||
| 273 | You can also look for information at: | ||
| 274 | |||
| 275 | =over 4 | ||
| 276 | |||
| 277 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 278 | |||
| 279 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Bio-CUA> | ||
| 280 | |||
| 281 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 282 | |||
| 283 | L<http://annocpan.org/dist/Bio-CUA> | ||
| 284 | |||
| 285 | =item * CPAN Ratings | ||
| 286 | |||
| 287 | L<http://cpanratings.perl.org/d/Bio-CUA> | ||
| 288 | |||
| 289 | =item * Search CPAN | ||
| 290 | |||
| 291 | L<http://search.cpan.org/dist/Bio-CUA/> | ||
| 292 | |||
| 293 | =back | ||
| 294 | |||
| 295 | |||
| 296 | =head1 ACKNOWLEDGEMENTS | ||
| 297 | |||
| 298 | |||
| 299 | =head1 LICENSE AND COPYRIGHT | ||
| 300 | |||
| 301 | Copyright 2015 Zhenguo Zhang. | ||
| 302 | |||
| 303 | This program is free software: you can redistribute it and/or modify | ||
| 304 | it under the terms of the GNU General Public License as published by | ||
| 305 | the Free Software Foundation, either version 3 of the License, or | ||
| 306 | (at your option) any later version. | ||
| 307 | |||
| 308 | This program is distributed in the hope that it will be useful, | ||
| 309 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 310 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 311 | GNU General Public License for more details. | ||
| 312 | |||
| 313 | You should have received a copy of the GNU General Public License | ||
| 314 | along with this program. If not, see L<http://www.gnu.org/licenses/>. | ||
| 315 | |||
| 316 | =cut | ||
| 317 |