-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathevalPHI-base.pl
executable file
·244 lines (200 loc) · 6.77 KB
/
evalPHI-base.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
#!/usr/bin/env perl
#
# INGLÊS/ENGLISH
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# http://www.gnu.org/copyleft/gpl.html
#
#
# PORTUGUÊS/PORTUGUESE
# Este programa é distribuído na expectativa de ser útil aos seus
# usuários, porém NÃO TEM NENHUMA GARANTIA, EXPLÍCITAS OU IMPLÍCITAS,
# COMERCIAIS OU DE ATENDIMENTO A UMA DETERMINADA FINALIDADE. Consulte
# a Licença Pública Geral GNU para maiores detalhes.
# http://www.gnu.org/copyleft/gpl.html
#
# Copyright (C) 2012 Universidade de São Paulo
#
# Universidade de São Paulo
# Laboratório de Biologia do Desenvolvimento de Abelhas
# Núcleo de Bioinformática (LBDA-BioInfo)
#
# Daniel Guariz Pinheiro
# http://zulu.fmrp.usp.br/bioinfo
#
# $Id$
=head1 NAME
=head1 SYNOPSIS
=head1 ABSTRACT
=head1 DESCRIPTION
Arguments:
-h/--help Help
-l/--level Log level [Default: FATAL]
OFF
FATAL
ERROR
WARN
INFO
DEBUG
TRACE
ALL
=head1 AUTHOR
Daniel Guariz Pinheiro E<lt>[email protected]<gt>
Copyright (c) 2012 Universidade de São Paulo
=head1 LICENSE
GNU General Public License
http://www.gnu.org/copyleft/gpl.html
=cut
use strict;
use warnings;
use Readonly;
use Getopt::Long;
use Storable;
use vars qw/$LOGGER/;
INIT {
use Log::Log4perl qw/:easy/;
Log::Log4perl->easy_init($FATAL);
$LOGGER = Log::Log4perl->get_logger($0);
}
use FileHandle;
my ($level,$infile,$outfile,$dumpdir,$phifile,$taxon_id);
# Command line named arguments (see function Usage() )
Usage("Too few arguments") if $#ARGV < 0;
GetOptions( "h|?|help" => sub { &Usage(); },
"l|level=s"=> \$level,
"i|infile=s"=>\$infile,
"o|outfile=s"=>\$outfile,
"d|dumpdir=s"=>\$dumpdir,
"p|phifile=s"=>\$phifile,
"t|taxon_id=i"=>\$taxon_id
) or &Usage();
#Log4perl levels
if ($level) {
my %LEVEL = (
'OFF' =>$OFF,
'FATAL' =>$FATAL,
'ERROR' =>$ERROR,
'WARN' =>$WARN,
'INFO' =>$INFO,
'DEBUG' =>$DEBUG,
'TRACE' =>$TRACE,
'ALL' =>$ALL);
$LOGGER->logdie("Wrong log level ($level). Choose one of: ".join(', ', keys %LEVEL)) unless (exists $LEVEL{$level});
Log::Log4perl->easy_init($LEVEL{$level});
}
$LOGGER->logdie("Missing input file with PHI-base search results") unless ($infile);
$LOGGER->logdie("Wrong input file ($infile)") unless (-e $infile);
my %inh;
open( IN, "<", $infile ) or $LOGGER->logdie($!);
while (<IN>) {
chomp;
next if ( $_ =~ /^#/ );
my ( $id, @F ) = split( /\t/, $_ );
$inh{$id} = \@F;
}
close(IN);
$dumpdir||=".";
my $hr_phibase;
if ($phifile) {
unless (-e $phifile) {
$LOGGER->logdie("Missing PHI-base csv file");
}
open(PHI, "<", $phifile) or $LOGGER->logdie($!);
{
local $/ = "";
my $header_line=<PHI>;
chomp($header_line);
my @header=split(/,/, $header_line);
foreach my $h (@header) {
$h=~s/\s+//g;
}
while(my $l=<PHI>) {
$l=~s/\n//g;
chomp($l);
my %data;
my @m;
$l=~s/""//g;
while ($l=~ /("[^"]+")/g) {
push(@m, $1);
}
foreach my $e (@m) {
my $m=$e;
$m=~s/,/;/g;
my $qe=quotemeta($e);
$l=~s/$qe/$m/;
}
if ($l) {
@data{@header} = split(/,/, $l);
$LOGGER->logdie("Not found PHIMolConnID in this record ($.): $l") unless ($data{'PHIMolConnID'});
$LOGGER->logdie("Not found the right PHIMolConnID (found: $data{'PHIMolConnID'}) in this record ($.): $l") unless ($data{'PHIMolConnID'}=~/^PHI:\d+$/);
$LOGGER->logdie("Not found HostID in this record ($.): $l") unless ($data{'HostID'});
if (($data{'HostID'} eq 'No host tests done')||($data{'HostID'} eq 'Lethal pathogen phenotype')) {
$data{'HostID'}=0;
}
$data{'HostID'}=~s/^[^0-9]+|[^0-9]+$//;
$LOGGER->logdie("Not found the right HostID (found: $data{'HostID'}) in this record ($.): $l") unless ($data{'HostID'}=~/^\d+$/);
$hr_phibase->{ $data{'PHIMolConnID'} }->{'HostID'} = $data{'HostID'};
}
}
}
close(PHI);
store $hr_phibase, "$dumpdir/evalPHI-base.dump";
} else {
unless (-e "$dumpdir/evalPHI-base.dump") {
$LOGGER->logdie("Missing PHI-base dump or csv file");
}
}
if (-e "$dumpdir/evalPHI-base.dump") {
# Retrieve the hash reference from a file
$hr_phibase = retrieve("$dumpdir/evalPHI-base.dump");
}
#foreach my $p (keys %{ $hr_phibase }) {
# print $p,"\t",$hr_phibase->{$p}->{'HostID'}||'',"\n";
#}
open(OUT, ">", $outfile) or $LOGGER->logdie($!);
foreach my $id (sort { $inh{$b}->[1]<=>$inh{$a}->[1] } keys %inh) {
my @r_info=split(/#/, $inh{$id}->[0]);
my $set=undef;
foreach my $phi_id ( split(/__/, $r_info[1]) ) {
$LOGGER->logdie("Not found PHI identifier ($phi_id) on database") unless (exists $hr_phibase->{$phi_id});
my $r_taxon_id=$hr_phibase->{$phi_id}->{'HostID'};
$LOGGER->logdie("Not found HostID for PHI identifier ($phi_id) on database") unless (defined $r_taxon_id);
my $test_cmd = 'echo -e "'.$r_taxon_id.'\n'.$taxon_id.'" | GetLCA.pl 2>/dev/null';
my $test_id = `$test_cmd`;
chomp($test_id);
if (($test_id==$taxon_id)||
($test_id==$r_taxon_id)) {
$set=1;
last;
}
}
print OUT $id,"\t",join("\t", @{$inh{$id}}),"\n" if ($set);
}
close(OUT);
# Subroutines
sub Usage {
my ($msg) = @_;
Readonly my $USAGE => <<"END_USAGE";
Daniel Guariz Pinheiro (dgpinheiro\@gmail.com)
(c)2012 Universidade de São Paulo
Usage
$0 [-h/--help] [-l/--level <LEVEL>]
Argument(s)
-h --help Help
-l --level Log level [Default: FATAL]
-i --in Input file of PHI-base search results
-e --excludes Exclude lineages (comma separated names)
-o --outfile Output file with filtered PHI-base search results considering Host taxonomy clade
-d --dumpdir Dump directory [Default: ./]
-p --phifile PHI-base csv file
-t --taxon_id NCBI taxonomy for host taxonomy clade filtering
END_USAGE
print STDERR "\nERR: $msg\n\n" if $msg;
print STDERR qq[$0 ] . q[$Revision$] . qq[\n];
print STDERR $USAGE;
exit(1);
}