3
|
1 #!/usr/bin/perl
|
|
2
|
|
3 #######
|
|
4 # POD #
|
|
5 #######
|
|
6
|
|
7 =pod
|
|
8
|
|
9 =head1 NAME
|
|
10
|
|
11 C<prot_finder.pl> - search for query protein homologs in annotated
|
|
12 bacterial genomes with BLASTP
|
|
13
|
|
14 =head1 SYNOPSIS
|
|
15
|
|
16 C<perl prot_finder.pl -r report.blastp -s subject.faa E<gt> blast_hits.tsv>
|
|
17
|
|
18 =head1 DESCRIPTION
|
|
19
|
|
20 This script is intended to search for homologous proteins in
|
|
21 annotated bacterial genomes. For this purpose, a previous
|
|
22 L<B<BLASTP>|http://blast.ncbi.nlm.nih.gov/Blast.cgi>), either
|
|
23 L<legacy or plus|https://blast.ncbi.nlm.nih.gov/Blast.cgi?PAGE_TYPE=BlastDocs&DOC_TYPE=Download>,
|
|
24 needs to be run with query protein sequences against
|
|
25 a B<BLASTP> database of subject proteins (e.g. all proteins from
|
|
26 several I<Escherichia coli> genomes).
|
|
27
|
|
28 The script L<C<cds_extractor.pl>|/cds_extractor> (with options B<-p
|
|
29 -f>) can be used to create multi-FASTA protein files of all
|
|
30 non-pseudo CDS from RichSeq genome files to create the needed
|
|
31 subject B<BLASTP> database. Present locus tags will be used as FASTA
|
|
32 IDs, but see L<C<cds_extractor.pl>|/cds_extractor> for a description
|
|
33 of the format. Query protein sequences for the B<BLASTP> need a
|
|
34 unique FASTA ID.
|
|
35
|
|
36 The B<BLASTP> report file (option B<-r>), the subject protein
|
|
37 multi-FASTA file (option B<-s>), and optionally the query protein
|
|
38 (multi-)FASTA file (option B<-q>) are then given to
|
|
39 C<prot_finder.pl>. Significant B<BLASTP> subject hits are filtered
|
|
40 according to the given cutoffs (options B<-i>, B<-cov_q>, and
|
|
41 B<-cov_s>) and the result is printed as an informative tab-separated
|
|
42 result table to C<STDOUT>. To apply global identity/coverage cutoffs
|
|
43 to subject hits high-scoring pairs (HSPs) are tiled (see
|
|
44 L<http://www.bioperl.org/wiki/HOWTO:Tiling> and
|
|
45 L<http://search.cpan.org/dist/BioPerl/Bio/Search/Hit/GenericHit.pm>).
|
|
46 Additionally, the subject protein sequences with significant query
|
|
47 hits are written to result multi-FASTA files, named according to the
|
|
48 respective query FASTA IDs (optionally including the query sequence
|
|
49 with option B<-q>).
|
|
50
|
|
51 Optionally, L<B<Clustal Omega>|http://www.clustal.org/omega/> can be
|
|
52 called (option B<-a> with optional B<-p>) to create multiple
|
|
53 alignments (FASTA format) for each of the resulting multi-FASTA
|
|
54 files. These alignments can be used to calculate phylogenies e.g.
|
|
55 with L<B<RAxML>|http://sco.h-its.org/exelixis/software.html> or
|
|
56 L<B<MEGA>|http://www.megasoftware.net/>.
|
|
57
|
|
58 Run the script L<C<cds_extractor.pl>|/cds_extractor> (with options
|
|
59 B<-p -f>) and the B<BLASTP> manually or use the bash shell wrapper
|
|
60 script C<prot_finder_pipe.sh> (see below L</"EXAMPLES">) to execute
|
|
61 the whole pipeline including C<prot_finder.pl> (with optional option
|
|
62 B<-q>). For a description of the pipeline and additional options see
|
|
63 option B<-h> of the shell script. Be aware that some options in
|
|
64 C<prot_finder_pipe.sh> corresponding to options in C<prot_finder.pl>
|
|
65 have different names. If L<C<cds_extractor.pl>|/cds_extractor> is
|
|
66 used in the pipeline (option B<-f> of the shell script) the working
|
|
67 folder has to contain the annotated bacterial genome subject files
|
|
68 (in RichSeq format, e.g. EMBL or GENBANK format).
|
|
69
|
|
70 At last, the resulting tab-separated table can be given to the
|
|
71 script C<prot_binary_matrix.pl> to create a presence/absence matrix
|
|
72 of the query proteins for each genome. Again see option B<-h> of
|
|
73 C<prot_binary_matrix.pl> for additional info. The presence/absence
|
|
74 matrix can also be transposed with script C<transpose_matrix.pl>
|
|
75 (see its help with B<-h>). These presence/absence matri(x|ces) can
|
|
76 e.g. be loaded into L<B<iTOL>|http://itol.embl.de/> to associate the
|
|
77 data with a phylogenetic tree. Also, you can use
|
|
78 C<binary_group_stats.pl> to calculate presence/absence statistics
|
|
79 for groups of columns and not simply single columns of the matrix.
|
|
80 C<binary_group_stats.pl> also has a comprehensive manual with its
|
|
81 option B<-h>.
|
|
82
|
|
83 =head1 OPTIONS
|
|
84
|
|
85 =head2 Mandatory options
|
|
86
|
|
87 =over 22
|
|
88
|
|
89 =item B<-r>=I<str>, B<-report>=I<str>
|
|
90
|
|
91 Path to B<BLASTP> report/output
|
|
92
|
|
93 =item B<-s>=I<str>, B<-subject>=I<str>
|
|
94
|
|
95 Path to subject multi-FASTA protein sequence file (*.faa) created
|
|
96 with L<C<cds_extractor.pl>|/cds_extractor> (and its options B<-p
|
|
97 -f>), which was used to create the B<BLASTP> database
|
|
98
|
|
99 =back
|
|
100
|
|
101 =head2 Optional options
|
|
102
|
|
103 =over 20
|
|
104
|
|
105 =item B<-h>, B<-help>
|
|
106
|
|
107 Help (perldoc POD)
|
|
108
|
|
109 =item B<-d>=I<str>, B<-dir_result>=I<str>
|
|
110
|
|
111 Path to result folder [default = query identity and coverage
|
|
112 cutoffs, './results_i#_cq#']
|
|
113
|
|
114 =item B<-f>, B<-force_dir>
|
|
115
|
|
116 Force output to an existing result folder, otherwise ask user to
|
|
117 remove content of existing folder. Careful, files from a previous
|
|
118 analysis might not be overwritten if different to current analysis.
|
|
119
|
|
120 =item B<-q>=I<str>, B<-query>=I<str>
|
|
121
|
|
122 Path to query (multi-)FASTA protein sequence file (*.faa) with
|
|
123 B<unique> FASTA IDs, which was used as query in the B<BLASTP>. Will
|
|
124 include each query protein sequence in the respective multi-FASTA
|
|
125 F<query-ID_hits.faa> result file.
|
|
126
|
|
127 =item B<-b>, B<-best_hit>
|
|
128
|
|
129 Give only the best hit (i.e. highest identity) for each subject
|
|
130 sequence if a subject has several hits with different queries
|
|
131
|
|
132 =item B<-i>=I<int>, B<-ident_cutoff>=I<int>
|
|
133
|
|
134 Query identity cutoff for significant hits (not including gaps), has
|
|
135 to be an integer number >= 0 and <= 100 [default = 70]
|
|
136
|
|
137 =item B<-cov_q>=I<int>, B<-cov_query_cutoff>=I<int>
|
|
138
|
|
139 Query coverage cutoff, has to be an integer >= 0 and <= 100 [default
|
|
140 = 70]
|
|
141
|
|
142 =item B<-cov_s>=I<int>, B<-cov_subject_cutoff>=I<int>
|
|
143
|
|
144 Subject/hit coverage cutoff, has to be an integer >= 0 and <= 100
|
|
145 [default = 0]
|
|
146
|
|
147 =item B<-a>, B<-align_clustalo>
|
|
148
|
|
149 Call L<B<Clustal Omega>|http://www.clustal.org/omega/> for multiple
|
|
150 alignment of each F<query-ID_hits.faa> result file
|
|
151
|
|
152 =item B<-p>=I<str>, B<-path_clustalo>=I<str>
|
|
153
|
|
154 Path to executable B<Clustal Omega> binary if not present in global
|
|
155 C<PATH> variable; requires option B<-a>
|
|
156
|
|
157 =item B<-t>=I<int>, B<-threads_clustalo>=I<int>
|
|
158
|
|
159 Number of threads for B<Clustal Omega> to use; requires option B<-a>
|
|
160 [default = all processors on system]
|
|
161
|
|
162 =item B<-v>, B<-version>
|
|
163
|
|
164 Print version number to C<STDERR>
|
|
165
|
|
166 =back
|
|
167
|
|
168 =head1 OUTPUT
|
|
169
|
|
170 =over 17
|
|
171
|
|
172 =item C<STDOUT>
|
|
173
|
|
174 The resulting tab-delimited output table with the significant
|
|
175 subject B<BLASTP> hits is printed to C<STDOUT>. Redirect (e.g. to a
|
|
176 file in the result directory, options B<-d -f>) or pipe into another
|
|
177 tool as needed (e.g. C<prot_binary_matrix.pl>).
|
|
178
|
|
179 =item F<./results_i#_cq#>
|
|
180
|
|
181 All result files are stored in a result folder
|
|
182
|
|
183 =item F<./results_i#_cq#/query-ID_hits.faa>
|
|
184
|
|
185 Multi-FASTA protein files of significant subject hits for each query
|
|
186 protein (named after the respective query FASTA ID), optionally
|
|
187 include the respective query protein sequence (with option B<-q>)
|
|
188
|
|
189 =item F<subject.faa.idx>
|
|
190
|
|
191 Index file of the subject protein file for fast sequence retrieval
|
|
192 (can be deleted if no further B<BLASTPs> are needed with these
|
|
193 subject sequences)
|
|
194
|
|
195 =item (F<./results_i#_cq#/queries_no_blastp-hits.txt>)
|
|
196
|
|
197 Lists all query sequence IDs without significant subject hits; with
|
|
198 option B<-b> includes also queries with significant hits but
|
|
199 I<without> a best blast hit for a subject
|
|
200
|
|
201 =item (F<./results_i#_cq#/clustal_omega.log>)
|
|
202
|
|
203 Optional log file of verbose B<Clustal Omega> C<STDOUT/STDERR> messages
|
|
204
|
|
205 =item (F<./results_i#_cq#/query-ID_aln.fasta>)
|
|
206
|
|
207 Optional B<Clustal Omega> multiple alignment of each
|
|
208 F<query-ID_hits.faa> result file in FASTA alignment format
|
|
209
|
|
210 =item (F<./results_i#_cq#/query-ID_tree.nwk>)
|
|
211
|
|
212 Optional B<Clustal Omega> NJ-guide tree in Newick format
|
|
213
|
|
214 =back
|
|
215
|
|
216 =head1 EXAMPLES
|
|
217
|
|
218 =head2 L<C<cds_extractor.pl>|/cds_extractor>
|
|
219
|
|
220 =over
|
|
221
|
|
222 =item C<for file in *.(gbk|embl); do perl cds_extractor.pl -i "$file" -p -f; done>
|
|
223
|
|
224 =item C<cat *.faa E<gt> subject.faa>
|
|
225
|
|
226 =item C<rm !(subject).faa>
|
|
227
|
|
228 =back
|
|
229
|
|
230 =head2 Legacy B<BLASTP>
|
|
231
|
|
232 =over
|
|
233
|
|
234 =item C<formatdb -p T -i subject.faa -n prot_finder_db>
|
|
235
|
|
236 =item C<blastall -p blastp -d prot_finder_db -i query.faa -o prot_finder.blastp -e 1e-10 -F F -s T -b 500>
|
|
237
|
|
238 =back
|
|
239
|
|
240 B<or>
|
|
241
|
|
242 =head2 B<BLASTP+>
|
|
243
|
|
244 =over
|
|
245
|
|
246 =item C<makeblastdb -dbtype prot -in subject.faa -out prot_finder_db>
|
|
247
|
|
248 =item C<blastp -db prot_finder_db -query query.faa -out prot_finder.blastp -evalue 1e-10 -seg no -use_sw_tback -num_alignments 500>
|
|
249
|
|
250 =back
|
|
251
|
|
252 =head2 C<prot_finder.pl>
|
|
253
|
|
254 =over
|
|
255
|
|
256 =item C<perl prot_finder.pl -r prot_finder.blastp -s subject.faa -cov_s 80 E<gt> blast_hits.tsv>
|
|
257
|
|
258 =back
|
|
259
|
|
260 B<or>
|
|
261
|
|
262 =over
|
|
263
|
|
264 =item C<perl prot_finder.pl -r prot_finder.blastp -s subject.faa -d result_dir -f -q query.faa -i 50 -cov_q 50 -b -a -p ~/bin/clustalo -t 6 E<gt> result_dir/blast_hits.tsv>
|
|
265
|
|
266 =back
|
|
267
|
|
268 =head2 All-in-one with bash script pipeline
|
|
269
|
|
270 =over
|
|
271
|
|
272 =item C<./prot_finder_pipe.sh -q query.faa -s subject.faa E<gt> blast_hits.tsv>
|
|
273
|
|
274 =back
|
|
275
|
|
276 B<or>
|
|
277
|
|
278 =over
|
|
279
|
|
280 =item C<./prot_finder_pipe.sh -q query.faa -f (embl|gbk) -d result_dir -p legacy -e 0 -t 12 -i 50 -c 50 -k 30 -b -a -o ~/bin/clustalo -m E<gt> result_dir/blast_hits.tsv>
|
|
281
|
|
282 =back
|
|
283
|
|
284 =head1 DEPENDENCIES
|
|
285
|
|
286 =over
|
|
287
|
|
288 =item L<B<BioPerl>|http://www.bioperl.org>>
|
|
289
|
|
290 Tested with B<BioPerl> version 1.006923
|
|
291
|
|
292 =item L<B<Clustal Omega>|http://www.clustal.org/omega/>
|
|
293
|
|
294 Tested with B<Clustal Omega> version 1.2.1
|
|
295
|
|
296 =back
|
|
297
|
|
298 =head1 VERSION
|
|
299
|
|
300 0.7.1 update: 05-04-2016
|
|
301 0.1 03-09-2012
|
|
302
|
|
303 =head1 AUTHOR
|
|
304
|
|
305 Andreas Leimbach aleimba[at]gmx[dot]de
|
|
306
|
|
307 =head1 LICENSE
|
|
308
|
|
309 This program is free software: you can redistribute it and/or modify
|
|
310 it under the terms of the GNU General Public License as published by
|
|
311 the Free Software Foundation; either version 3 (GPLv3) of the
|
|
312 License, or (at your option) any later version.
|
|
313
|
|
314 This program is distributed in the hope that it will be useful, but
|
|
315 WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
316 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
317 General Public License for more details.
|
|
318
|
|
319 You should have received a copy of the GNU General Public License
|
|
320 along with this program. If not, see L<http://www.gnu.org/licenses/>.
|
|
321
|
|
322 =cut
|
|
323
|
|
324
|
|
325 ########
|
|
326 # MAIN #
|
|
327 ########
|
|
328
|
|
329 use strict;
|
|
330 use warnings;
|
|
331 use autodie;
|
|
332 use Getopt::Long;
|
|
333 use Pod::Usage;
|
|
334 use Bio::SeqIO; # BioPerl module to handle sequence input/output
|
|
335 use Bio::SearchIO; # BioPerl module to handle BLAST reports
|
|
336 use Bio::Index::Fasta; # BioPerl module to create an index for a multi-FASTA file for faster sequence retrieval
|
|
337
|
|
338 my $Cmdline = "$0 @ARGV"; # used call command
|
|
339
|
|
340 ### Get the options with Getopt::Long
|
|
341 my $Blastp_Report_File; # path to BLASTP report/output file
|
|
342 my $Subject_File; # multi-FASTA protein file from 'cds_extractor.pl' (with options '-p -f') which was used to create the BLASTP DB (the subjects)
|
|
343 my $Result_Dir; # path to result folder; default is set below to '"./results_i".$Ident_Cutoff."_cq".$Cov_Query_Cutoff'
|
|
344 my $Opt_Force_Result_Dir; # force output to existing results folder
|
|
345 my $Query_File; # optionally, needed to include the query proteins in the result/hit multi-FASTA protein file for subsequent alignment
|
|
346 my $Opt_Best_Hit; # optionally, give only the best hit (i.e. highest identity) for each subject sequence, even if a subject sequence has several hits with different queries; if option not given report all subjects hits for each query
|
|
347 my $Ident_Cutoff = 70; # query identity cutoff (without gaps)
|
|
348 my $Cov_Query_Cutoff = 70; # query coverage cutoff
|
|
349 my $Cov_Subject_Cutoff = 0; # subject/hit coverage cutoff
|
|
350 my $Opt_Align_Clustal; # optionally, align the result sequences with Clustal Omega
|
|
351 my $Clustal_Path; # optionally, give path to the Clustal Omega binary
|
|
352 my $Clustal_Threads; # optionally, give number of threads Clustal Omega will use
|
|
353 my $VERSION = '0.7.1';
|
|
354 my ($Opt_Version, $Opt_Help);
|
|
355 GetOptions ('report=s' => \$Blastp_Report_File,
|
|
356 'subject=s' => \$Subject_File,
|
|
357 'dir_result=s' => \$Result_Dir,
|
|
358 'force_dir' => \$Opt_Force_Result_Dir,
|
|
359 'query=s' => \$Query_File,
|
|
360 'best_hit' => \$Opt_Best_Hit,
|
|
361 'ident_cutoff=i' => \$Ident_Cutoff,
|
|
362 'cov_query_cutoff=i' => \$Cov_Query_Cutoff,
|
|
363 'cov_subject_cutoff=i' => \$Cov_Subject_Cutoff,
|
|
364 'align_clustalo' => \$Opt_Align_Clustal,
|
|
365 'path_clustalo=s' => \$Clustal_Path,
|
|
366 'threads_clustalo=i' => \$Clustal_Threads,
|
|
367 'version' => \$Opt_Version,
|
|
368 'help|?' => \$Opt_Help)
|
|
369 or pod2usage(-verbose => 1, -exitval => 2);
|
|
370
|
|
371
|
|
372
|
|
373 ### Run perldoc on POD, enforce mandatory options, and check options
|
|
374 pod2usage(-verbose => 2) if ($Opt_Help);
|
|
375 die "$0 $VERSION\n" if ($Opt_Version);
|
|
376
|
|
377 if (!$Blastp_Report_File || !$Subject_File) {
|
|
378 my $warning = "\n### Fatal error: Mandatory options '-r' and '-s' or their arguments are missing!\n";
|
|
379 pod2usage(-verbose => 1, -message => $warning, -exitval => 2);
|
|
380 }
|
|
381 die "\n### Fatal error:\nBLASTP report file '$Blastp_Report_File' does not exist: $!\n" if (!-e $Blastp_Report_File);
|
|
382 die "\n### Fatal error:\nSubject multi-FASTA protein sequence file '$Subject_File' does not exist: $!\n" if (!-e $Subject_File);
|
|
383 die "\n### Fatal error:\nQuery multi-FASTA protein sequence file '$Query_File' does not exist: $!\n" if ($Query_File && !-e $Query_File);
|
|
384
|
|
385 if (($Ident_Cutoff < 0 || $Ident_Cutoff > 100) || ($Cov_Query_Cutoff < 0 || $Cov_Query_Cutoff > 100) || ($Cov_Subject_Cutoff < 0 || $Cov_Subject_Cutoff > 100)) {
|
|
386 my $warning = "\n### Fatal error:\nAll cutoff options ('-i', '-cov_q', and '-cov_s') require an integer number >= 0 and <= 100 as value!\n";
|
|
387 pod2usage(-verbose => 1, -message => $warning, -exitval => 2);
|
|
388 }
|
|
389
|
|
390 die "\n### Fatal error:\nOption '-p' requires the path to an executable Clustal Omega binary as value, not '$Clustal_Path'!\n" if ($Clustal_Path && !-x $Clustal_Path); # without '-p': presence of 'clustalo' in global PATH checked by system call at end of script
|
|
391 if (!$Opt_Align_Clustal && ($Clustal_Path || $Clustal_Threads)) {
|
|
392 warn "\n### Warning: Options '-p' and/or '-t' set without their required option '-a', forcing option '-a'!\n";
|
|
393 $Opt_Align_Clustal = 1;
|
|
394 }
|
|
395
|
|
396 if ($Clustal_Threads) {
|
|
397 my $max_cpus = qx{nproc --all 2> /dev/null} || die "\n### Fatal error:\nCouldn't run Unix GNU command 'nproc' to determine the overall number of processors on the local system!\n"; # get max number of processors on the system; '||' because success exit code is zero (in '$?')
|
|
398 chomp $max_cpus;
|
|
399 if ($Clustal_Threads < 0 || $Clustal_Threads > $max_cpus) {
|
|
400 my $warning = "\n### Fatal error: Option '-t' requires an integer number > 0 and <= $max_cpus as value, not '$Clustal_Threads'!\n";
|
|
401 pod2usage(-verbose => 1, -message => $warning, -exitval => 2);
|
|
402 }
|
|
403 }
|
|
404
|
|
405 print STDERR "\nScript call command: $Cmdline\n"; # print call command after '-h|-v'
|
|
406
|
|
407
|
|
408
|
|
409 ### Create result folder
|
|
410 if (!$Result_Dir) { # can't give default before 'GetOptions' in case cutoffs are set by the user
|
|
411 $Result_Dir = "./results_i".$Ident_Cutoff."_cq".$Cov_Query_Cutoff;
|
|
412 } else {
|
|
413 $Result_Dir =~ s/\/$//; # get rid of a potential '/' at the end of $Result_Dir path
|
|
414 }
|
|
415 if (-e $Result_Dir && !$Opt_Force_Result_Dir) {
|
|
416 empty_dir($Result_Dir); # subroutine to empty a directory with user interaction
|
|
417 } elsif (!$Opt_Force_Result_Dir) {
|
|
418 mkdir $Result_Dir;
|
|
419 }
|
|
420
|
|
421
|
|
422
|
|
423 ### Parse the BLASTP report/output file; print queries, subject ID (e.g. locus tag) hits, and stats; or store in hash for option '-b'
|
|
424 print STDERR "Parsing BLASTP report '$Blastp_Report_File' and looking for significant hits according to cutoffs ...\n"; # run status of script
|
|
425 print "# subject_organism\tsubject_ID\tsubject_gene\tsubject_protein_desc\tquery_ID\tquery_desc\tquery_coverage [%]\tquery_identities [%]\tsubject/hit_coverage [%]\te-value of best HSP\tbit-score of best HSP\n"; # header for output table with significant BLASTP hits
|
|
426
|
|
427 my %Query_Acc; # hash to check if query accessions/IDs are unique (which they have to be because of %Blast_Hits below) and to store all query_acc for '@Queries_No_Blasthit' below
|
|
428 my %Blast_Hits; # hash of array to store significant BLASTP hits for sequence retrieval afterwards; query_acc as key and subject ID hits as anonymous array
|
|
429 my %Best_Subject_Hit; # for option '-b'; hash of hash to store subject/query info/stats for only the best hit for each subject sequence ID (key)
|
|
430
|
|
431 my $Blast_Report = new Bio::SearchIO(-file => "<$Blastp_Report_File", -format => 'blast'); # Bio::SearchIO object
|
|
432 while (my $result = $Blast_Report->next_result) { # Bio::Search::Result::GenericResult object; several query sequences possible ($result = entire analysis for a single query seq)
|
|
433 my $hit_present = 0; # indicates if significant BLASTP hit found for a query
|
|
434 my @subject_ids; # array to store ALL significant subject ID hits for each query, for %Blast_Hits anonymous array
|
|
435
|
|
436 my $query_desc = $result->query_description;
|
|
437 my $query_acc = $result->query_accession =~ s/\.$//r; # = query ID; rm a '.' if present at the end of the string (for non-NCBI FASTA headers); non-destructive modifier causes the result of the substitution to be returned instead of modifying $_ (see http://perldoc.perl.org/perlrequick.html#Search-and-replace)
|
|
438 die "\n### Fatal error:\nQuery accession/ID '$query_acc' is not unique in the query file, but has to be. Please edit all repetitive occurences and rerun BLASTP or the bash script pipeline!\n" if ($Query_Acc{$query_acc});
|
|
439 $Query_Acc{$query_acc} = 1;
|
|
440
|
|
441 while (my $hit = $result->next_hit) { # Bio::Search::Hit::GenericHit object; several subject sequences in the database might have hits for a query
|
|
442 my $perc_identity = $hit->frac_identical('query'); # ignores gaps; method will call (requires) BioPerls HSP tiling 'tile_hsps()' to get value (see http://search.cpan.org/dist/BioPerl/Bio/Search/Hit/GenericHit.pm and for 'tile_hsps()' http://search.cpan.org/~cjfields/BioPerl-1.6.924/Bio/Search/SearchUtils.pm)
|
|
443 $perc_identity *= 100;
|
|
444 my $query_cov = $hit->frac_aligned_query; # method requires hsp tiling
|
|
445 $query_cov *= 100;
|
|
446 my $hit_cov = $hit->frac_aligned_hit; # = subject coverage; method requires hsp tiling
|
|
447 $hit_cov *= 100;
|
|
448
|
|
449 # "significant" hit according to cutoffs
|
|
450 if ($perc_identity >= $Ident_Cutoff && $query_cov >= $Cov_Query_Cutoff && $hit_cov >= $Cov_Subject_Cutoff) {
|
|
451 $hit_present = 1;
|
|
452 my $hit_id = $hit->name; # = subject_id
|
|
453 my ($gene, $product, $organism) = split_fasta_header($hit_id, $hit->description); # subroutine to split the subject FASTA ID lines (see cds_extractor.pl with option '-f')
|
|
454 $gene = '' if (!$gene); # empty string if gene tag doesn't exist for print; $product and $organism should always exist
|
|
455 my $evalue = $hit->significance;
|
|
456 $evalue =~ s/\,$//; # rm ',' from the end of the evalue
|
|
457
|
|
458 if (!$Opt_Best_Hit) { # print all hits to STDOUT directly without option '-b'
|
|
459 print "$organism\t$hit_id\t$gene\t$product\t$query_acc\t$query_desc\t$query_cov\t$perc_identity\t$hit_cov\t$evalue\t", $hit->bits, "\n";
|
|
460 push(@subject_ids, $hit_id); # store all hits for current query
|
|
461
|
|
462 } elsif ($Opt_Best_Hit) { # store only the best hit for each subject ID (need to be stored, not printed, to check all queries = $result); print to STDOUT is below
|
|
463 if (!$Best_Subject_Hit{$hit_id} || $Best_Subject_Hit{$hit_id}->{'perc_identity'} < $perc_identity) { # either hit/subject ID doesn't exist yet or replace with hit of higher identity
|
|
464 $Best_Subject_Hit{$hit_id} = {'organism' => $organism,
|
|
465 'gene' => $gene,
|
|
466 'product' => $product,
|
|
467 'query_acc' => $query_acc,
|
|
468 'query_desc' => $query_desc,
|
|
469 'query_cov' => $query_cov,
|
|
470 'perc_identity' => $perc_identity,
|
|
471 'hit_cov' => $hit_cov,
|
|
472 'evalue' => $evalue,
|
|
473 'bit_score' => $hit->bits}; # hash of hash; subject/hit ID keys are unique with '-b'
|
|
474 }
|
|
475 }
|
|
476 }
|
|
477 }
|
|
478
|
|
479 # only if significant hit for current query; needed after 'next_hit'-while to collect all (subject) hits
|
|
480 $Blast_Hits{$query_acc} = \@subject_ids if ($hit_present && !$Opt_Best_Hit); # hash of array; the same hit/subject ID can be a hit for different queries (without option '-b'), thus subject IDs are not unique and hash of (anonymous) array data structure is suitable; done in the same way for option '-b' during print out below (not here to check all queries)
|
|
481 }
|
|
482
|
|
483
|
|
484
|
|
485 ### Option '-b' given; print out only the best hit for each hit/subject sequence and store respective IDs in %Blast_Hits (as was already done without '-b' above)
|
|
486 if ($Opt_Best_Hit) {
|
|
487 print STDERR "Printing only the best hit for each subject protein in the whole BLASTP analysis (option '-b') ...\n"; # run status of script
|
|
488 my $skip = ''; # skip queries that have already been processed
|
|
489
|
|
490 foreach my $hit_id (sort{lc $Best_Subject_Hit{$a}->{'query_acc'} cmp lc $Best_Subject_Hit{$b}->{'query_acc'}} keys %Best_Subject_Hit) { # sort hit/subject IDs (keys of %Best_Subject_Hit) by 'query_acc' to look at each query_acc only once by $skip-ing the others
|
|
491 next if ($Best_Subject_Hit{$hit_id}->{'query_acc'} eq $skip);
|
|
492 $skip = $Best_Subject_Hit{$hit_id}->{'query_acc'};
|
|
493
|
|
494 my @subject_ids = sort{lc $Best_Subject_Hit{$a}->{'organism'} cmp lc $Best_Subject_Hit{$b}->{'organism'}} grep($Best_Subject_Hit{$_}->{'query_acc'} eq $Best_Subject_Hit{$hit_id}->{'query_acc'}, keys %Best_Subject_Hit); # get all hit/subject IDs for the current 'query_acc', sorted by 'organism'
|
|
495 $Blast_Hits{$skip} = \@subject_ids; # store all hit/subject IDs in hash of array with query_acc as key ($skip here), compatible to above without '-b'
|
|
496
|
|
497 foreach my $subject_id (sort @subject_ids) { # print best hits to STDOUT, corresponding to the print of all hits without '-b' above
|
|
498 print "$Best_Subject_Hit{$subject_id}->{'organism'}\t".
|
|
499 "$subject_id\t".
|
|
500 "$Best_Subject_Hit{$subject_id}->{'gene'}\t".
|
|
501 "$Best_Subject_Hit{$subject_id}->{'product'}\t".
|
|
502 "$Best_Subject_Hit{$subject_id}->{'query_acc'}\t".
|
|
503 "$Best_Subject_Hit{$subject_id}->{'query_desc'}\t".
|
|
504 "$Best_Subject_Hit{$subject_id}->{'query_cov'}\t".
|
|
505 "$Best_Subject_Hit{$subject_id}->{'perc_identity'}\t".
|
|
506 "$Best_Subject_Hit{$subject_id}->{'hit_cov'}\t".
|
|
507 "$Best_Subject_Hit{$subject_id}->{'evalue'}\t".
|
|
508 "$Best_Subject_Hit{$subject_id}->{'bit_score'}\n";
|
|
509 }
|
|
510 }
|
|
511 }
|
|
512
|
|
513 my @Queries_No_Blasthit = grep (!$Blast_Hits{$_}, sort{lc $a cmp lc $b} keys %Query_Acc); # get all 'query_acc' without a significant and (for '-b') best blast hit (can use '$hit_present' above only without '-b', but to make consistent use '%Blast_Hits' with and without '-b')
|
|
514 # With '-b' have to use the approach here in case a query protein has a significant hit but not the best hit in any subject, because then wouldn't be in the results (a significant query hit might also be overwritten by a higher identity subsequent hit to another query, thus $hit_present doesn't work with '-b')
|
|
515 if (keys %Query_Acc == @Queries_No_Blasthit) {
|
|
516 rmdir $Result_Dir;
|
|
517 die "\nNo significant BLASTP hits could be found, exiting!\n";
|
|
518 }
|
|
519
|
|
520
|
|
521 ### Create index for multi-FASTA subject protein file for faster sequence retrieval; indeces have to be unique (which works fine for locus tags, the most probable subject IDs with cds_extractor.pl)
|
|
522 print STDERR "Indexing subject multi-FASTA file, '$Subject_File\.idx', for sequence retrieval ...\n"; # run status of script
|
|
523 my $Inx = Bio::Index::Fasta->new(-filename => "$Subject_File\.idx", -write_flag => 1); # see http://www.bioperl.org/wiki/HOWTO:Beginners#Indexing_for_Fast_Retrieval or http://www.bioperl.org/wiki/HOWTO:Local_Databases
|
|
524 $Inx->make_index($Subject_File); # by default the FASTA indexing code will use the string following the > character as a key, in this case the subject IDs
|
|
525
|
|
526
|
|
527
|
|
528 ### Get the significant BLASTP hit sequences from the indexed multi-FASTA protein subject file and the query protein file (w/o index) and write them to the result dir
|
|
529 print STDERR "Using the index to retrieve subject protein sequences from significant BLASTP hits for each query "; # run status of script
|
|
530 my $Query_Seqioobj;
|
|
531 if ($Query_File) { # option '-q' with path to query multi-FASTA file; to retrieve respective query seq and write it as first seq into the hit multi-FASTA result files (*query*_hits.faa)
|
|
532 print STDERR "including each query sequence from file '$Query_File' (option '-q') "; # run status of script
|
|
533 $Query_Seqioobj = Bio::SeqIO->new(-file => "<$Query_File", -format => 'fasta'); # Bio::SeqIO object
|
|
534 }
|
|
535 print STDERR "...\n"; # run status of script
|
|
536
|
|
537 my @Fasta_Files; # store all hit result FASTA filenames for Clustal Omega
|
|
538 foreach my $query_acc (sort keys %Blast_Hits) {
|
|
539 my $fasta_outfile = "$Result_Dir/$query_acc\_hits.faa";
|
|
540 push (@Fasta_Files, $fasta_outfile);
|
|
541 my $seqio_outobj = Bio::SeqIO->new(-file => ">$fasta_outfile"); # write a multi-FASTA file of hits for each query; format not needed, as everything is and should be FASTA anyway
|
|
542
|
|
543 # get the corresponding query_acc sequence if option '-q'
|
|
544 if ($Query_File) {
|
|
545 my $query_found = 0;
|
|
546 while (my $query_seqinobj = $Query_Seqioobj->next_seq) { # Bio::Seq object; index not needed should be small file
|
|
547 if ($query_seqinobj->display_id eq $query_acc) {
|
|
548 $query_found = 1;
|
|
549 $seqio_outobj->write_seq($query_seqinobj); # print seq entry
|
|
550 seek($Query_Seqioobj->_fh, 0, 0); # set filepointer back to zero for the next query_acc/ID
|
|
551 last; # query accn/ID is found
|
|
552 }
|
|
553 }
|
|
554 die "\n### Fatal error:\nQuery ID '$query_acc' not found in the query file '$Query_File' given with option '-q'. Sure this is the correct file which was used for the BLASTP?\n" if (!$query_found);
|
|
555 }
|
|
556
|
|
557 # get all hit-subject sequences for the query
|
|
558 foreach my $subject_id (sort @{ $Blast_Hits{$query_acc} }) {
|
|
559 my $subject_seqobj = $Inx->fetch($subject_id); # a Bio::Seq object; fetch subject seq from index
|
|
560 die "\n### Fatal error:\nSubject ID '$subject_id' not found in the subject file '$Subject_File' given with option '-s'. Sure this is the correct file which was used for the BLASTP?\n" if (!$subject_seqobj);
|
|
561
|
|
562 ## used to have the following out-commented code, but why not use original desc from cds_extractor?
|
|
563 #my ($gene, $product, $organism) = split_fasta_header($subject_id, $subject_seqobj->desc);
|
|
564 #if ($gene) {
|
|
565 #$subject_seqobj->desc("$organism $gene"); # set the description of the FASTA ID line to a new one, if a gene name exists
|
|
566 #} else {
|
|
567 #$subject_seqobj->desc("$organism"); # w/o gene name
|
|
568 #}
|
|
569
|
|
570 $seqio_outobj->write_seq($subject_seqobj);
|
|
571 }
|
|
572 }
|
|
573
|
|
574
|
|
575
|
|
576 ### OPTIONAL method to extract the BLASTP hit protein sequences without BioPerl and without an index; works with out-commented sub 'read_fasta_entry'
|
|
577 #print STDERR "Retrieving subject protein sequences from significant BLASTP hits for each query "; # run status of script
|
|
578 #open (my $Subject_Fh, "<", $Subject_File);
|
|
579
|
|
580 #my $Query_Fh;
|
|
581 #if ($Query_File) { # option '-q' with path to query multi-FASTA file; to retrieve respective query seq and write it as first seq into the hit multi-FASTA result files (*query*_hits.faa)
|
|
582 #print STDERR "including each query sequence from file '$Query_File' (option '-q') "; # run status of script
|
|
583 #open ($Query_Fh, "<", $Query_File);
|
|
584 #}
|
|
585 #print STDERR "...\n"; # run status of script
|
|
586
|
|
587 #my @Fasta_Files; # store all hit result FASTA filenames for Clustal Omega
|
|
588 #foreach my $query_acc (sort keys %Blast_Hits) {
|
|
589 #my $fasta_outfile = "$Result_Dir/$query_acc\_hits.faa";
|
|
590 #push (@Fasta_Files, $fasta_outfile);
|
|
591 #open (my $fasta_out_fh, ">", $fasta_outfile); # write a multi-FASTA file of hits for each query
|
|
592
|
|
593 ## get the corresponding query_acc sequence if option '-q'
|
|
594 #if ($Query_File) {
|
|
595 #my $query_found = 0;
|
|
596 #my $next_fasta_header; # for multi-line FASTA input files to store next entry header/ID line while parsing in subroutine 'read_fasta_entry'
|
|
597 #while (<$Query_Fh>) {
|
|
598 #chomp;
|
|
599 #(my $seq_entry, $next_fasta_header) = read_fasta_entry($_, $Query_Fh, $next_fasta_header); # subroutine to read each FASTA seq entry of a multi-seq file separately; return header (->[0]) and seq (->[1]) as anonymous array in $seq_entry
|
|
600 #if ($seq_entry->[0] =~ /^>$query_acc/) { # use '^' to anchor query acc/ID match (can't use ' ' as below for subjects, in case FASTA header has only one "word"/term after the '>')
|
|
601 #$query_found = 1;
|
|
602 #print $fasta_out_fh "$seq_entry->[0]\n$seq_entry->[1]\n\n"; # print seq entry
|
|
603 #seek $Query_Fh, 0, 0; # set filepointer back to zero for the next query acc/ID
|
|
604 #$. = 0; # set line number of seq file to 0 (seek doesn't do it automatically)
|
|
605 #last; # query acc/ID found
|
|
606 #}
|
|
607 #}
|
|
608 #die "\n### Fatal error:\nQuery sequence '$query_acc' not found in the query file '$Query_File' given with option '-q'. Sure this is the correct file which was used for the BLASTP?\n" if (!$query_found);
|
|
609 #}
|
|
610
|
|
611 ## get all hit-subject sequences for the current query
|
|
612 #foreach my $subject_id (sort @{ $Blast_Hits{$query_acc} }) {
|
|
613 #my $subject_found = 0;
|
|
614 #my $next_fasta_header;
|
|
615 #while (<$Subject_Fh>) {
|
|
616 #chomp;
|
|
617 #(my $seq_entry, $next_fasta_header) = read_fasta_entry($_, $Subject_Fh, $next_fasta_header); # subroutine
|
|
618 #if ($seq_entry->[0] =~ /^>$subject_id /) { # use '^' and ' ' to force complete subject ID match (e.g. problem with ABU83972 'ECABU_c27750' and CFT073 'c2775')
|
|
619 #$subject_found = 1;
|
|
620
|
|
621 ### used to have the following out-commented code, but why not use original desc from cds_extractor?
|
|
622 ## print FASTA header/ID for subject sequence
|
|
623 ##$seq_entry->[0] =~ s/>.+\s(g=.+)$/$1/; # get rid of the subject ID for subroutine 'split_fasta_header' below
|
|
624 ##my ($gene, $product, $organism) = split_fasta_header($subject_id, $seq_entry->[0]);
|
|
625 ##print $fasta_out_fh ">$subject_id $organism ";
|
|
626 ##if ($gene) {
|
|
627 ##print $fasta_out_fh "$gene\n";
|
|
628 ##} else {
|
|
629 ##print $fasta_out_fh "\n";
|
|
630 ##}
|
|
631 ##print $fasta_out_fh "$seq_entry->[1]\n\n";
|
|
632
|
|
633 #print $fasta_out_fh "$seq_entry->[0]\n$seq_entry->[1]\n\n";
|
|
634 #seek $Subject_Fh, 0, 0; # for the next $subject_id
|
|
635 #$. = 0;
|
|
636 #last;
|
|
637 #}
|
|
638 #}
|
|
639 #die "\n### Fatal error:\nSubject sequence '$subject_id' not found in the subject sequence file '$Subject_File' given with option '-s'. Sure this is the correct file which was used for the BLASTP?\n" if (!$subject_found);
|
|
640 #}
|
|
641 #close $fasta_out_fh;
|
|
642 #}
|
|
643 #close $Subject_Fh;
|
|
644 #close $Query_Fh if ($Query_File);
|
|
645
|
|
646
|
|
647
|
|
648 ### Align with Clustal Omega if option '-a' is set
|
|
649 if ($Opt_Align_Clustal) {
|
|
650 print STDERR "Starting Clustal Omega alignment (option '-a') with file\n"; # run status of script
|
|
651 foreach my $fasta_file (@Fasta_Files) {
|
|
652 print STDERR " $fasta_file";
|
|
653 my $out = $fasta_file =~ s/\_hits.faa$//r;
|
|
654 my $clustal_call = " -i $fasta_file -o $out\_aln.fasta --verbose --guidetree-out=$out\_tree.nwk >> $Result_Dir/clustal_omega.log"; # redirect verbose STDOUT output to file (can't use clustalo option '-l' because will overwrite for each call)
|
|
655 $clustal_call = " --threads=$Clustal_Threads" . $clustal_call if ($Clustal_Threads);
|
|
656 if ($Clustal_Path) { # append path to Clustal Omega binary if option '-p'
|
|
657 $clustal_call = $Clustal_Path . $clustal_call;
|
|
658 } else { # otherwise 'clustalo' hopefully present in global path
|
|
659 $clustal_call = 'clustalo' . $clustal_call;
|
|
660 }
|
|
661 system ($clustal_call) == 0 or die "\n### Fatal error:\nClustal Omega alignment with option '-a' for multi-FASTA output file '$fasta_file' could not be run. If Clustal Omega's binary 'clustalo' is not installed in your global \$PATH use option '-p' to give the path to the binary! $!\n";
|
|
662 }
|
|
663 print STDERR "\n";
|
|
664 }
|
|
665
|
|
666
|
|
667
|
|
668 ### Final run status of script, state if queries without BLASTP hits
|
|
669 print STDERR "Result files were created in '$Result_Dir'"; # run status of script
|
|
670 if (@Queries_No_Blasthit) {
|
|
671 my $no_blasthit_file = "$Result_Dir/queries_no_blastp-hits.txt";
|
|
672 print STDERR ", including '$no_blasthit_file' listing all queries without a significant BLASTP hit"; # run status of script
|
|
673 open (my $no_blasthit_fh, ">", "$no_blasthit_file");
|
|
674 print $no_blasthit_fh join("\n", @Queries_No_Blasthit), "\n"; # print query_accs to file
|
|
675 close $no_blasthit_fh;
|
|
676 }
|
|
677 print STDERR ".\n"; # run status of script
|
|
678 print STDERR "If no further BLASTPs with the subject sequences in file '$Subject_File' are needed the index file '$Subject_File.idx' can be deleted!\n";
|
|
679
|
|
680
|
|
681 exit;
|
|
682
|
|
683
|
|
684
|
|
685 ###############
|
|
686 # Subroutines #
|
|
687 ###############
|
|
688
|
|
689 ### Subroutine to empty a directory with user interaction
|
|
690 sub empty_dir {
|
|
691 my $dir = shift;
|
|
692 print STDERR "\nDirectory '$dir' already exists! You can use either option '-d' to set a different result directory name, or do you want to replace the directory and all its contents [y|n]? ";
|
|
693 my $user_ask = <STDIN>;
|
|
694 if ($user_ask =~ /y/i) {
|
|
695 unlink glob "$dir/*"; # remove all files in results directory
|
|
696 } else {
|
|
697 die "\nScript abborted!\n";
|
|
698 }
|
|
699 return 1;
|
|
700 }
|
|
701
|
|
702
|
|
703
|
|
704 ### Read sequence entries from FASTA file
|
|
705 #sub read_fasta_entry {
|
|
706 #my ($line, $fh, $next_fasta_header) = @_;
|
|
707
|
|
708 ## possible multi-line seq in FASTA
|
|
709 #my ($seq, $header);
|
|
710 #if ($. == 1) { # first line of file
|
|
711 #die "\n### Fatal error:\nNot a FASTA input file, first line of file should be a FASTA ID/header line and start with a '>':\n$line\n" if ($line !~ /^>/);
|
|
712 #$header = $line;
|
|
713 #} elsif ($next_fasta_header) {
|
|
714 #$header = $next_fasta_header;
|
|
715 #$seq = $line;
|
|
716 #}
|
|
717 #while (<$fh>) {
|
|
718 #chomp;
|
|
719 #if (/^>/) {
|
|
720 #$next_fasta_header = $_; # store ID/header for next seq entry
|
|
721 #return ([$header, $seq], $next_fasta_header); # return anonymous array with current header and seq
|
|
722 #}
|
|
723 #$seq .= $_; # concatenate multi-line seq
|
|
724 #}
|
|
725 #return ([$header, $seq], $next_fasta_header) if (eof);
|
|
726 #return; # return undef
|
|
727 #}
|
|
728
|
|
729
|
|
730
|
|
731 ### Subroutine to split the headers/IDs of the protein multi-FASTA files from 'cds_extractor.pl' and its '-f' option
|
|
732 sub split_fasta_header {
|
|
733 my ($id, $desc) = @_;
|
|
734
|
|
735 my ($gene, $product, $length, $organism, $ec); # $length and $ec not used
|
|
736 $desc =~ s/^\s*//; # remove possible space at begin of desc string (might be introduced by BioPerl as explained below); needed in case no 'g=' but introduced space in front of 'p=', then split will not work below
|
|
737 $desc =~ s/\s(?!p=|l=|o=|ec=)//g; # if a FASTA ID line is too long the BLAST report hit desc is over several lines and BioPerl will introduce space characters for a newline, thus get rid of these extra spaces with a lookahead in the regex but leave the spaces in the cds_extractor file format intact
|
|
738 die "\n### Fatal error:\nFASTA 'annotation' from 'cds_extractor.pl' is not recognized on the following line. Please use 'cds_extractor.pl' with options '-p -f' to create your multi-FASTA protein subject files.\n>$id $desc\n" if ($desc !~ /(g=\w+)?\s? # optional gene tag
|
|
739 (p=\S+)?\s? # optional product tag; \S+ instead of \w needed for non-alphanumeric characters (e.g. commas)
|
|
740 l=\d+\.\.\d+\s? # location always included
|
|
741 (o=\S+)?\s? # optional organism tag
|
|
742 (ec=\S+)? # optional EC number tag
|
|
743 /x); # for the format see 'cds_extractor.pl'
|
|
744
|
|
745 foreach (split(/\s/, $desc)) {
|
|
746 if (/g=(.*)$/) {
|
|
747 $gene = $1;
|
|
748 } elsif (/p=(.*)$/) {
|
|
749 warn "\n### Warning:\nNo product annotation (p=) on line:\n>$id $desc\nProceeding ...\n" if (!$1);
|
|
750 $product = $1;
|
|
751 $product =~ tr/_/ /; # replace the '_' back to spaces, as this was changed in 'cds_extractor.pl'
|
|
752 } elsif (/l=(\d+)\.\.(\d+)$/) { # $length not used
|
|
753 $length = abs($2 - $1) + 1; # abs(stop - start) + 1
|
|
754 } elsif (/o=(.*)$/) {
|
|
755 $organism = $1; # don't replace the '_' back, no spaces might be better for phylogenetic programs
|
|
756 } elsif (/ec=(.*)$/) { # $ec not used
|
|
757 $ec = $1;
|
|
758 } else {
|
|
759 die "\n### Fatal error:\nFASTA 'annotation' is not recognized on the following line. Please use 'cds_extractor.pl' with options '-p -f' to create your multi-FASTA protein subject files.\n>$id $desc\n";
|
|
760 }
|
|
761 }
|
|
762 return ($gene, $product, $organism);
|
|
763 }
|