-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathparallel_codeml.pl
276 lines (187 loc) · 7.4 KB
/
parallel_codeml.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/perl -w
#
# parallel_codeml.pl -- A wrapper to run codeml in parallel.
#
#
# Author: Nowind
# Created: 2012-05-31
# Updated: 2021-04-07
# Version: 1.0.1
#
# Change logs:
# Version 1.0.0 18/03/30: The initial version.
# Version 1.0.1 21/04/07: Correct some descriptions.
=head1 NAME
parallel_codeml.pl
=head1 SYNOPSIS
parallel_codeml.pl --help/?
=head1 DESCRIPTION
A wrapper to run codeml in parallel.
=cut
use strict;
use Data::Dumper;
use Getopt::Long;
use File::Find::Rule;
use File::Basename;
use File::Temp;
use Parallel::ForkManager;
use MyPerl::FileIO qw(:all);
use MyPerl::Convert;
use MyPerl::Compare;
######################### Main #########################
my $CMDLINE = "perl $0 @ARGV";
my $VERSION = '1.0.1';
my $HEADER = "##$CMDLINE\n##Version: $VERSION\n";
my $SOURCE = (scalar localtime()) . " Version: $VERSION";
my $max_threads = 1;
my @models = ();
my $min_aln_len = 30;
my ($aln_file, $output, $out_directly, $show_help);
GetOptions(
"input=s" => \$aln_file,
"output=s" => \$output,
"threads=i" => \$max_threads,
"models=i{,}" => \@models,
"align-length=i" => \$min_aln_len,
"help|?" => \$show_help,
);
unless( !$show_help && $aln_file ) {
print <<EOF;
$0 -- Run codeml in parallel.
Version: $VERSION
Usage: perl $0 [options]
Options:
-i, --input <filename>
input alignment files in fasta format, required
-o, --output <dirname>
output filename, default to STDOUT
-m, --model <numbers>
currently not used
-a, --align-length <int>
minimum alignment length to process, [default: 30 (bp)]
-t, --threads <int>
how many data threads should be allocated to running this analysis
[default: 1]
-?, --help
show this help message
EOF
exit(1);
}
$|++;
print STDERR "# $0 v$VERSION\n# " . (scalar localtime()) . "\n";
if ($output) {
open (STDOUT, "> $output") || die $!;
}
#unless(@models > 0) {
@models = (0);
#}
##
## read reference sequences
##
my $pm = new Parallel::ForkManager($max_threads) if $max_threads > 1;
print STDOUT "$HEADER##" . (scalar localtime()) . "\n";
print "#Sample1\tSample2\taln_len\tref_len\tcmp_len\tt\tS\tN\tdNdS\tdN\tdS\n";
my $curr_job_num = 0;
my $aln_fh = getInputFilehandle($aln_file);
while (<$aln_fh>)
{
chomp(my $ref_id = $_);
chomp(my $ref_seq = <$aln_fh>);
chomp(my $cmp_id = <$aln_fh>);
chomp(my $cmp_seq = <$aln_fh>);
$ref_id =~ s/^\>//;
$cmp_id =~ s/^\>//;
my $aln_len = ($ref_seq =~ tr/ATGCatgc-/ATGCatgc-/);
my $ref_len = $aln_len - ($ref_seq =~ tr/-/-/);
my $cmp_len = $aln_len - ($cmp_seq =~ tr/-/-/);
next if ($min_aln_len && $aln_len < $min_aln_len);
$curr_job_num++;
my $pid = $pm->start and next if ($max_threads > 1);
print STDERR "\r>>Start calculating ... $curr_job_num";
my $tmp_dir = File::Temp->newdir(CLEANUP => 1);
my $tmp_in_fh = File::Temp->new(DIR => $tmp_dir, UNLINK => 1);
my $tmp_in = $tmp_in_fh->filename;
open (my $tmp_fh, "> $tmp_in") || die $!;
print {$tmp_fh} "2\t$aln_len\n";
print {$tmp_fh} "$ref_id\n $ref_seq\n";
print {$tmp_fh} "$cmp_id\n $cmp_seq\n";
my @results = ();
for my $model (@models)
{
write_codeml_ctl("$tmp_in.ctl", $tmp_in, $model);
my $run_codeml = "codeml $tmp_in.ctl";
my $return = system "$run_codeml >/dev/null 2>&1";
my $result_fh = getInputFilehandle("$tmp_in.out");
while (<$result_fh>)
{
##print STDOUT;
next unless(/t=\s+(.*?)\s+S=\s+(.*?)\s+N=\s+(.*?)\s+dN\/dS=\s+(.*?)\s+dN\s+=\s+(.*?)\s+dS\s+=\s+(.*?)\s+/);
my ($t, $S, $N, $dNdS, $dN, $dS) = ($1, $2, $3, $4, $5, $6);
push @results, $1;
print STDOUT "$ref_id\t$cmp_id\t$aln_len\t$ref_len\t$cmp_len\t$t\t$S\t$N\t$dNdS\t$dN\t$dS\n";
last;
}
}
if ($max_threads > 1) {
$pm->finish;
}
}
print STDERR "\tdone!\n";
######################### Sub #########################
sub write_codeml_ctl
{
my ($config_file, $seqfile, $select_model) = @_;
my $treefile = $seqfile . ".tree";
my $outfile = $seqfile . ".out";
open (OUT, "> $config_file") || die $!;
print OUT <<EOF;
seqfile = $seqfile * sequence data filename
treefile = $treefile * tree structure file name
outfile = $outfile * main result file name
noisy = 0 * 0,1,2,3,9: how much rubbish on the screen
verbose = 0 * 0: concise; 1: detailed, 2: too much
runmode = -2 * 0: user tree; 1: semi-automatic; 2: automatic
* 3: StepwiseAddition; (4,5):PerturbationNNI; -2: pairwise
seqtype = 1 * 1:codons; 2:AAs; 3:codons-->AAs
CodonFreq = 2 * 0:1/61 each, 1:F1X4, 2:F3X4, 3:codon table
* ndata = 10
clock = 0 * 0:no clock, 1:clock; 2:local clock; 3:CombinedAnalysis
aaDist = 0 * 0:equal, +:geometric; -:linear, 1-6:G1974,Miyata,c,p,v,a
aaRatefile = dat/jones.dat * only used for aa seqs with model=empirical(_F)
* dayhoff.dat, jones.dat, wag.dat, mtmam.dat, or your own
model = 2
* models for codons:
* 0:one, 1:b, 2:2 or more dN/dS ratios for branches
* models for AAs or codon-translated AAs:
* 0:poisson, 1:proportional, 2:Empirical, 3:Empirical+F
* 6:FromCodon, 7:AAClasses, 8:REVaa_0, 9:REVaa(nr=189)
NSsites = 0 * 0:one w;1:neutral;2:selection; 3:discrete;4:freqs;
* 5:gamma;6:2gamma;7:beta;8:beta&w;9:betaγ
* 10:beta&gamma+1; 11:beta&normal>1; 12:0&2normal>1;
* 13:3normal>0
icode = 0 * 0:universal code; 1:mammalian mt; 2-10:see below
Mgene = 0
* codon: 0:rates, 1:separate; 2:diff pi, 3:diff kapa, 4:all diff
* AA: 0:rates, 1:separate
fix_kappa = 0 * 1: kappa fixed, 0: kappa to be estimated
kappa = 2 * initial or fixed kappa
fix_omega = 0 * 1: omega or omega_1 fixed, 0: estimate
omega = .4 * initial or fixed omega, for codons or codon-based AAs
fix_alpha = 1 * 0: estimate gamma shape parameter; 1: fix it at alpha
alpha = 0. * initial or fixed alpha, 0:infinity (constant rate)
Malpha = 0 * different alphas for genes
ncatG = 8 * # of categories in dG of NSsites models
getSE = 0 * 0: don't want them, 1: want S.E.s of estimates
RateAncestor = 1 * (0,1,2): rates (alpha>0) or ancestral states (1 or 2)
Small_Diff = .5e-6
cleandata = 1 * remove sites with ambiguity data (1:yes, 0:no)?
* fix_blength = -1 * 0: ignore, -1: random, 1: initial, 2: fixed
method = 0 * Optimization method 0: simultaneous; 1: one branch a time
* Genetic codes: 0:universal, 1:mammalian mt., 2:yeast mt., 3:mold mt.,
* 4: invertebrate mt., 5: ciliate nuclear, 6: echinoderm mt.,
* 7: euplotid mt., 8: alternative yeast nu. 9: ascidian mt.,
* 10: blepharisma nu.
* These codes correspond to transl_table 1 to 11 of GENEBANK.
EOF
close OUT;
}