-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunMK.pl
executable file
·383 lines (298 loc) · 13 KB
/
runMK.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env perl
########################
### Required Modules ###
use strict;
use warnings;
use Carp;
use Getopt::Long qw(:config no_ignore_case bundling pass_through);
use FindBin;
use File::Basename;
use Bio::Tools::Run::Alignment::Clustalw;
# for projecting alignments from protein to R/DNA space
use Bio::Align::Utilities qw(aa_to_dna_aln);
# for input of the sequence data
use Bio::SeqIO;
use Bio::AlignIO;
my $usage = <<__EOUSAGE__;
#######################################################################################################################################################################
#
# __ __ _ __ _
# | \/ | |/ / _ __ | |
# | |\/| | ' / | '_ \| |
# | | | | . \ _| |_) | |
# |_| |_|_|\_(_) .__/|_|
# |_|
#
# (MK.pl was written by Alisha Holloway)
#
#
# Requirements:
#
# 1. In addition to the perl modules required for this script to work (see code header), the following scripts are required:
#
# - Perl scripts:
#
# fastagrep.pl
# MK.pl
# MK_trim.pl
#
# 2. CDS file should contain all sequences to be analyzed. Transcript ID within species (among lines) should be the same. Header should have follwoing format:
# >transcript_id .......... species=<species_id> line=<line_id>
#
#
# Required arguments:
#
# --CDS_file|c <string> sequence file containing CDS from multiple species/lines.
#
# --gene_list|g <string> list file of genes to be analyzed.
# OR
# --gene_id|i <string> transcript/gene to be analyzed
#
# --output|o name of directory to place outputs
#
#
#
# Options:
#
# --show_samples display species/line IDs in CDS_file and exit (only requires "-c <CDS_file>" argument).
#
# --include_samples include specified species/lines
#
# --samples_file <string> (use line ID if present, otherwise use species ID).
#
# --include_outgroups (REQUIRED FOR MK TEST!!!)
#
# --orthology_file <string> file with orthology information:<transcript_id>\\t<orthologue_id>
#
#
# --view_DNA_alignment open DNA alignment in Geneious.
#
# --view_Protein_alignment open Protein alignment in Geneious.
#
# --save_alignments retain alignment files
#
#
# --MK_test
#
# --pol <string> [pol|unpol] specify polarized or unpolarized analysis.
# --outgroup <string> [speciesID] 1 for unpolarized; must have 2 for polarized analysis.
# (specificy two outgroups like so: --outgroup1 <string> --outgroup2 <string>)
# --ingroup <string> [speciesID] 1 species
#
#
# --help
#
#
#######################################################################################################################################################################
__EOUSAGE__
;
#####################################
## Set BioPerl alignment protocol ###
my $aln_factory = Bio::Tools::Run::Alignment::Clustalw->new;
###################################
### Define command line options ###
my $CDS_file;
my $gene_list;
my $gene_id;
my $output_dir;
my $show_samples_flag = 0;
my $include_samples_flag = 0;
my $exclude_samples_flag = 0;
my $samples_file;
my $include_outgroups_flag = 0;
my $orthology_file;
my $view_aln_dna_geneious_flag = 0;
my $view_aln_aa_geneious_flag = 0;
my $save_alignments_flag = 0;
my $mk_test_flag = 0;
my $pol;
my $outgroup;
my $outgroup1;
my $outgroup2;
my $ingroup;
my $help_flag = 0;
&GetOptions (
'CDS_file|c=s' => \$CDS_file,
'gene_list|g=s' => \$gene_list,
'gene_id|i=s' => \$gene_id,
'output|o=s' => \$output_dir,
'show_samples' => \$show_samples_flag,
'include_samples' => \$include_samples_flag,
'exclude_samples' => \$exclude_samples_flag,
'samples_file=s' => \$samples_file,
'include_outgroups' => \$include_outgroups_flag,
'orthology_file=s'=> \$orthology_file,
'view_DNA_alignment' => \$view_aln_dna_geneious_flag,
'view_Protein_alignment' => \$view_aln_aa_geneious_flag,
'save_alignments' => \$save_alignments_flag,
'MK_test' => \$mk_test_flag,
'pol=s' => \$pol,
'outgroup=s' => \$outgroup,
'outgroup1=s' => \$outgroup1,
'outgroup2=s' => \$outgroup2,
'ingroup=s' => \$ingroup,
'help' => \$help_flag,
);
#####################################################
### Check command line arguments and housekeeping ###
# if command line argument are not recognized
if (@ARGV) {
die "Error, don't understand parameters: @ARGV";
}
if ($help_flag) {
die $usage;
}
# only show samples in CDS_file
if ($show_samples_flag) {
system ("grep '>' $CDS_file | sed 's/.*species=/species=/g' | sort -u");
exit(0);
}
# if Mandatory arguments not present
unless ($gene_list || $gene_id || $output_dir) {
die "\nNo CDS file, gene ID or gene list file provided.\n\nAnd you need to specify output directory!\n $usage";
}
if (@ARGV) {
die "Error, do not recognize params: @ARGV ";
}
# specify output dir cname
if ($output_dir) {
mkdir($output_dir);
}
# if MK test, check polarization options given and make tmp dir
if ($mk_test_flag) {
unless ($pol =~ /^(pol|unpol)$/) {
die "\nError, do not recognize polarization option [$pol]\nShould be pol or unpol";
}
system ("mkdir MK.tmp");
}
######################################
###### Main execution of processes ###
main: {
my @gene_object;
###############################################################
##### Set the gene object to a single gene or list of genes. ##
if ($gene_id) {
push (@gene_object, $gene_id);
} elsif ($gene_list) {
open (FILE1, $gene_list) or die ("Could not open file \n");
while ($gene_list = <FILE1>){
chomp $gene_list;
my @file_bits = split(/\s+/,$gene_list);
my $gene_names = $file_bits[0];
push (@gene_object, $gene_names);
}
}
###############################################################
### Initiate the loop to analyze individual transcripts #######
foreach my $transcript (@gene_object){
###########################################################
### Add orthologous sequences to the analysis... #########
### Processes FASTA header to include species and #########
### line ID, if present. #########
if ($include_outgroups_flag) {
system ("grep $transcript $orthology_file | tr '\t' '\n' | sort -u > $transcript.list");
system ("fastagrep.pl -X -f $transcript.list $CDS_file > $transcript.fa");
#######################################################
### Restrict to a subset of samples ###################
if ($include_samples_flag) {
system ("fastagrep.pl -f $samples_file $transcript.fa > $transcript.trimmed.fa");
system ("rm $transcript.fa");
system ("mv $transcript.trimmed.fa $transcript.fa");
}
# header processing, contd.
system ("sed -i.bak '/>/ s/ line=/_/g' $transcript.fa");
system ("sed -i.bak '/>/ s/>.*species=/>/g' $transcript.fa");
system ("rm $transcript.list *.bak");
### This part will be temporarily edited to alow MK test without adding
##########################################################
### Don't add orthologous seqnuences to analysis. ########
} else {
system ("fastagrep.pl -X $transcript $CDS_file > $transcript.fa");
system ("sed -i.bak '/>/ s/ line=/_/g' $transcript.fa");
system ("sed -i.bak '/>/ s/>.*species=/>/' $transcript.fa");
}
##########################################################
### BioPerl process for sequence analysis ################
# read in sequence
my $seqio = Bio::SeqIO->new(-file => "$transcript.fa",
-format => 'fasta');
my %seqs;
my @prots;
# process each sequence
while ( my $seq = $seqio->next_seq ) {
$seqs{$seq->display_id} = $seq;
# translate them into protein
my $protein = $seq->translate();
my $pseq = $protein->seq();
if( $pseq =~ /\*/ &&
$pseq !~ /\*$/ ) {
warn("Oi! $transcript has a stop codon!!!");
}
# Tcoffee can't handle '*' even if it is trailing
$pseq =~ s/\*//g;
$protein->seq($pseq);
push @prots, $protein;
}
# Warn if only 1 sequence present
if( @prots < 2 ) {
warn("$transcript - Need at least 2 CDS sequences to proceed");
}
# Align the sequences with clustalw
my $aa_aln = $aln_factory->align(\@prots);
# project the protein alignment back to CDS coordinates
my $dna_aln = aa_to_dna_aln($aa_aln, \%seqs);
my @each = $dna_aln->each_seq();
# output alignments for downstream analysis
my $out_dna = Bio::AlignIO->new(-file => ">$transcript.aln_dna.afa" ,
-format => 'fasta');
$out_dna -> write_aln($dna_aln);
my $out_aa = Bio::AlignIO->new(-file => ">$transcript.aln_aa.afa" ,
-format => 'fasta');
$out_aa -> write_aln($aa_aln);
# clean-up alignment header files
system ("sed -i.bak '/>/ s/\\/.*//g' $transcript.*.afa");
system ("rm *bak");
############################################################
### Perform the analyses specified in the command line #####
# MK test
if ($mk_test_flag) {
# Edit/move alignment file to MK tmp dir
system ("sed '/>/ s/_/; /g' $transcript.aln_dna.afa > MK.tmp/$transcript.fa ");
system ("sed -i.bak '/>/ s/\$/;/g' MK.tmp/$transcript.fa ");
# run MK.pl with "pol" option
if ($pol eq "pol") {
system ("MK.pl -outfile MK.tmp/$transcript.MKout.tmp1.txt -pol pol -outgroup $outgroup1 -outgroup $outgroup2 -ingroup $ingroup -dir ./MK.tmp/");
system ("MK_trim.pl MK.tmp/$transcript.MKout.tmp1.txt > MK.tmp/$transcript.MKout.tmp2.txt");
# run MK.pl with "unpol" option
} elsif ($pol eq "unpol") {
system ("MK.pl -outfile MK.tmp/$transcript.MKout.tmp1.txt -pol unpol -outgroup $outgroup -ingroup $ingroup -dir ./MK.tmp/");
system ("MK_trim.pl MK.tmp/$transcript.MKout.tmp1.txt > MK.tmp/$transcript.MKout.tmp2.txt");
}
# Clean-up seqs from MK tmp directory for the next sequence
system ("rm MK.tmp/$transcript.fa ");
}
# option to save the alignment files
if ($save_alignments_flag || $view_aln_dna_geneious_flag || $view_aln_aa_geneious_flag) {
system ("mv $transcript.aln_dna.afa $output_dir/$transcript.DNA_alignment.afa");
system ("mv $transcript.aln_aa.afa $output_dir/$transcript.Protein_alignment.afa");
} else {
system ("rm $transcript.aln*.afa");
}
# option to view alignment files in Geneious
if ($view_aln_dna_geneious_flag) {
system ("open -a Geneious $output_dir/$transcript.DNA_alignment.afa");
}
if ($view_aln_aa_geneious_flag) {
system ("open -a Geneious $output_dir/$transcript.Protein_alignment.afa");
}
# clean-up
system ("rm $transcript.fa");
##########################################################
############### END OF LOOP ##############################
}
# Format final output files
if ($mk_test_flag) {
system ("echo 'TRANSCRIPT\tNS_POLY\tS_POLY\tNS_FIX\tS_FIX\tcodons\tfinal_NI\talpha\tfinal_FET' | cat - MK.tmp/*.MKout.tmp2.txt > $output_dir/MKout.txt");
system ("rm -r MK.tmp");
}
}