Debian Codegraph @

bin/bp_pairwise_kaks

raw
1 #!/usr/bin/env perl
2
3 use utf8;
4 use strict;
5 use warnings;
6
7 use Getopt::Long;
8
9 use Bio::Align::Utilities qw(aa_to_dna_aln);
10 use Bio::AlignIO;
11 use Bio::SeqIO;
12 use Bio::Tools::Run::Alignment::Clustalw;
13 use Bio::Tools::Run::Alignment::TCoffee;
14 use Bio::Tools::Run::Phylo::PAML::Codeml;
15 use Bio::Tools::Run::Phylo::PAML::Yn00;
16
17 # PODNAME: bp_pairwise_kaks
18 # ABSTRACT: calculate pairwise Ka,Ks for a set of sequences
19 # AUTHOR: Jason Stajich <[email protected]>
20 # OWNER: Jason Stajich <[email protected]>
21 # LICENSE: Perl_5
22
23
24 my $aln_prog = "clustalw";
25 my $kaks_prog = "codeml";
26 my $format = "fasta";
27 my $output;
28 my $cdna;
29 my $verbose;
30 my $help;
31
32 GetOptions(
33 'i|input:s' => \$cdna,
34 'o|output:s' => \$output,
35 'f|format:s' => \$format,
36 'msa:s' => \$aln_prog,
37 'kaks:s' => \$kaks_prog,
38 'v|verbose' => \$verbose,
39 'h|help' => \$help,
40 );
41
42 if( $help ) {
43 exec('perldoc',$0);
44 exit(0);
45 }
46 $verbose = -1 unless $verbose;
47 my ($aln_factory,$kaks_factory);
48 if( $aln_prog =~ /clus/i ) {
49 $aln_factory = Bio::Tools::Run::Alignment::Clustalw->new(-verbose => $verbose);
50 } elsif( $aln_prog =~ /t\_?cof/i ) {
51 $aln_factory = Bio::Tools::Run::Alignment::TCoffee->new(-verbose => $verbose);
52 } else {
53 warn("Did not provide either 'clustalw' or 'tcoffee' as alignment program names");
54 exit(0);
55 }
56 unless( $aln_factory->executable ) {
57 warn("Could not find the executable for $aln_prog, make sure you have installed it and have either set ".uc($aln_prog)."DIR or it is in your PATH");
58 exit(0);
59 }
60
61
62 if( $kaks_prog =~ /yn00/i ) {
63 $kaks_factory = Bio::Tools::Run::Phylo::PAML::Yn00->new(-verbose => $verbose);
64 } elsif( $kaks_prog =~ /codeml/i ) {
65 # change the parameters here if you want to tweak your Codeml running!
66 $kaks_factory = Bio::Tools::Run::Phylo::PAML::Codeml->new
67 (-verbose => $verbose,
68 -params => { 'runmode' => -2,
69 'seqtype' => 1,
70 }
71 );
72 }
73 unless ( $kaks_factory->executable ) {
74 warn("Could not find the executable for $kaks_prog, make sure you have installed it and you have defined PAMLDIR or it is in your PATH");
75 exit(0);
76 }
77
78 unless ( $cdna && -f $cdna && -r $cdna && ! -z $cdna ) {
79 warn("Did not specify a valid cDNA sequence file as input");
80 exit(0);
81 }
82
83 my $seqin = new Bio::SeqIO(-file => $cdna,
84 -format => $format);
85
86 my %seqs;
87 my @prots;
88 while( my $seq = $seqin->next_seq ) {
89 $seqs{$seq->display_id} = $seq;
90 my $protein = $seq->translate();
91 my $pseq = $protein->seq();
92
93 $pseq =~ s/\*$//;
94 if( $pseq =~ /\*/ ) {
95 warn("provided a cDNA (".$seq->display_id.") sequence with a stop codon, PAML will choke!");
96 exit(0);
97 }
98 # Tcoffee can't handle '*'
99 $pseq =~ s/\*//g;
100 $protein->seq($pseq);
101 push @prots, $protein;
102 }
103 if( @prots < 2 ) {
104 warn("Need at least 2 cDNA sequences to proceed");
105 exit(0);
106 }
107
108 local * OUT;
109 if( $output ) {
110 open(OUT, ">$output") || die("cannot open output $output for writing");
111 } else {
112 *OUT = *STDOUT;
113 }
114
115 my $aa_aln = $aln_factory->align(\@prots);
116 my $dna_aln = &aa_to_dna_aln($aa_aln, \%seqs);
117
118 my @each = $dna_aln->each_seq();
119
120
121 $kaks_factory->alignment($dna_aln);
122
123 my ($rc,$parser) = $kaks_factory->run();
124 if( $rc <= 0 ) {
125 warn($kaks_factory->error_string,"\n");
126 exit;
127 }
128 my $result = $parser->next_result;
129
130 if ($result->version =~ m/3\.15/) {
131 warn("This script does not work with v3.15 of PAML! Please use 3.14 instead.");
132 exit(0);
133 }
134
135 my $MLmatrix = $result->get_MLmatrix();
136
137 my @otus = $result->get_seqs();
138
139 my @pos = map {
140 my $c= 1;
141 foreach my $s ( @each ) {
142 last if( $s->display_id eq $_->display_id );
143 $c++;
144 }
145 $c;
146 } @otus;
147
148 print OUT join("\t", qw(SEQ1 SEQ2 Ka Ks Ka/Ks PROT_PERCENTID CDNA_PERCENTID)), "\n";
149 for( my $i = 0; $i < (scalar @otus -1) ; $i++) {
150 for( my $j = $i+1; $j < (scalar @otus); $j++ ) {
151 my $sub_aa_aln = $aa_aln->select_noncont($pos[$i],$pos[$j]);
152 my $sub_dna_aln = $dna_aln->select_noncont($pos[$i],$pos[$j]);
153 print OUT join("\t",
154 $otus[$i]->display_id,
155 $otus[$j]->display_id,$MLmatrix->[$i]->[$j]->{'dN'},
156 $MLmatrix->[$i]->[$j]->{'dS'},
157 $MLmatrix->[$i]->[$j]->{'omega'},
158 sprintf("%.2f",$sub_aa_aln->percentage_identity),
159 sprintf("%.2f",$sub_dna_aln->percentage_identity),
160 ), "\n";
161 }
162 }
163
164 __END__
165
166 =pod
167
168 =encoding UTF-8
169
170 =head1 NAME
171
172 bp_pairwise_kaks - calculate pairwise Ka,Ks for a set of sequences
173
174 =head1 VERSION
175
176 version 1.7.3
177
178 =head1 SYNOPSIS
179
180 bp_pairwise_kaks.PLS -i INPUT-cDNA [-f FORMAT] [-o OUTPUT] \
181 [--msa tcoffee|clustal] [--kaks yn00|codeml]
182
183 =head1 DESCRIPTION
184
185 This script will take as input a dataset of cDNA sequences, verify
186 that they contain no stop codons, align them in protein space, project
187 the alignment back into cDNA, and estimate the Ka (non-synonymous) and
188 Ks (synonymous) substitutions based on the Maximum Likelihood method
189 of Yang with the PAML package.
190
191 Often there are specific specific parameters you want to run when you
192 are computing Ka/Ks ratios so consider this script a starting point
193 and do not rely it on for every situation.
194
195 =head1 OPTIONS
196
197 =over 4
198
199 =item B<-i>, B<--input>
200
201 The input file with the cDNA sequences. Must have at least two
202 sequences, and be in a format that L<Bio::SeqIO> is capable to read.
203 In addition, if Bio::SeqIO is inable to automatically identify the
204 format, the B<-f> option should be specified.
205
206 Technically not an option, this is a required value.
207
208 =item B<-f>, B<--format>
209
210 Specify the format of INPUT-cDNA for L<Bio::SeqIO>.
211
212 =item B<-o>, B<--output>
213
214 Specify the file for output. Defaults to STDOUT.
215
216 =item B<--msa>
217
218 Program used for alignment, either clustalw or tcoffee. Defaults to
219 clustalw.
220
221 =item B<--kaks>
222
223 Program used for the Ka/Ks estimation, either codeml or yn00.
224 Defaults to codeml.
225
226 =item B<-v>, B<--verbose>
227
228 Be verbose.
229
230 =item B<-h>, B<--help>
231
232 Print help text.
233
234 =back
235
236 =head1 FEEDBACK
237
238 =head2 Mailing lists
239
240 User feedback is an integral part of the evolution of this and other
241 Bioperl modules. Send your comments and suggestions preferably to
242 the Bioperl mailing list. Your participation is much appreciated.
243
244 [email protected] - General discussion
245 http://bioperl.org/Support.html - About the mailing lists
246
247 =head2 Support
248
249 Please direct usage questions or support issues to the mailing list:
250 I<[email protected]>
251
252 rather than to the module maintainer directly. Many experienced and
253 reponsive experts will be able look at the problem and quickly
254 address it. Please include a thorough description of the problem
255 with code and data examples if at all possible.
256
257 =head2 Reporting bugs
258
259 Report bugs to the Bioperl bug tracking system to help us keep track
260 of the bugs and their resolution. Bug reports can be submitted via the
261 web:
262
263 https://github.com/bioperl/bio-tools-phylo-paml/issues
264
265 =head1 AUTHOR
266
267 Jason Stajich <[email protected]>
268
269 =head1 COPYRIGHT
270
271 This software is copyright (c) by Jason Stajich <[email protected]>.
272
273 This software is available under the same terms as the perl 5 programming language system itself.
274
275 =cut