This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bind.pm
432 lines (353 loc) · 12.8 KB
/
Bind.pm
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package Bind;
use strict; #Variable declaration control
use DBpublic;
use Interaction;
use LWP::UserAgent;
use LWP::Simple;
use File::Copy;
use Protein;
$SIG{INT} = \&catch_ctrlc;
our @ISA = ("DBpublic");
#Bind retriever constructor (Connecting to BOND: http://bond.unleashedinformatics.com/)
# @param $email => Valid email identifier for BOND database connexion
# @param $password => Valid password for BOND database connexion
sub new() {
my ( $classe, $email, $password, $connector ) = @_; #Sending arguments to constructor
my $this = $classe->SUPER::new($connector);
$this->{"email"} = $email;
$this->{"password"} = $password;
bless( $this, $classe ); #Linking the reference to the class
return $this; #Returning the blessed reference
}
sub parse {
my ( $this, $stop, $adresse ) = @_;
$stop = defined($stop) ? $stop : -1;
$adresse ||= 1;
my %hash_orga_tax =
( # Hash to easily retrieve the correspondance between the taxonomy id and the seven reference organisms
'3702' => 'Arabidopsis thaliana',
'6239' => 'Caenorhabditis elegans',
'7227' => 'Drosophilia Melanogaster',
'9606' => 'Homo sapiens',
'10090' => 'Mus musculus',
'4932' => 'Saccharomyces cerevisiae',
'4896' => 'Schizosaccharomyces pombe'
);
%hash_orga_tax = reverse(%hash_orga_tax);
my %hash_error
; #hash of error, retrieve of uniprot or gene name from internet;
my $hash_uniprot_id={}; # A hash to store the uniprot id corresponding to a gene name and an organism
# This avoid to run the same request several times in the uniprot.org server
if (-f "gene_name_to_uniprot_database.txt") {
open( gene_name_to_uniprot_file, ">>gene_name_to_uniprot_database.txt" ); # A file to keep this hash
while (<gene_name_to_uniprot_file>)
{ # We initialize the hash with the data contained in the file
chomp($_);
my @convertion_data = split( /\t/, $_ );
$hash_uniprot_id->{ $convertion_data[0] }->{ $convertion_data[2] } = $convertion_data[1];
}
}
print "[DEBUG : BIND] list of uniprot/gene has been load\n" if ($main::verbose);
close(gene_name_to_uniprot_file);
my $database = 'bind'; # We note the corresponding database we are using
my $i = 0;
open( gene_name_to_uniprot_file, ">>gene_name_to_uniprot_database.txt" ); # During this time, we complete the file which contains the uniprot id for a gene name and an organism
my $orgaA;
my $orgaB;
my $prot;
my $gene;
my $intA;
my $intB;
my $uniprot_A;
my $uniprot_B;
my %hash_sysExp;
my %hash_pubmed;
my @pubmed;
my @sys_exp;
my @A;
my @B;
my $good = 1;
my $orga_queryA;
my $orga_queryB;
open( data_file, $adresse ); # We open the database file
while (<data_file>) {
if (/^BINDID/) {
if ( $i > 0 ) {
@pubmed = keys(%hash_pubmed);
@sys_exp = keys(%hash_sysExp);
$good = 0 if ( $#pubmed == -1 || $#sys_exp == -1 );
if ( $good == 1 ) {
my $protA = Protein->new ($uniprot_A, $intA, $orgaA);
my $protB = Protein->new ($uniprot_B, $intB, $orgaB);
#@A = ( $uniprot_A, $intA );
#@B = ( $uniprot_B, $intB );
my $interaction =
Interaction->new( $protA, $protB, $database, \@pubmed, \@sys_exp );
$this->SUPER::addInteraction($interaction);
print "[BIND] $i : uniprot A : $uniprot_A - gene name A :$intA\tuniprot B : $uniprot_B - gene name B :$intB\n"
if ( !$main::verbose );
print "[DEBUG : BIND] Done : $i\n--------------------------------------------\n" if ($main::verbose);
}
if ( $this->SUPER::getLength() >= 29 ) {
$this->SUPER::sendBDD();
close gene_name_to_uniprot_file;
open( gene_name_to_uniprot_file,
">>gene_name_to_uniprot_database.txt" );
$this->SUPER::error_internet( \%hash_error );
%hash_error = ();
}
}
$i++;
last if ( $i == $stop );
$orgaA = undef;
$orgaB = undef;
$intA = undef;
$intB = undef;
$uniprot_A = undef;
$uniprot_B = undef;
$orga_queryA = undef;
$orga_queryB = undef;
@pubmed = undef;
@sys_exp = undef;
%hash_sysExp = ();
%hash_pubmed = ();
$good = 1; #0 if bad, 1 for good
}
else {
if ( $_ =~ /^MOLECULE(.)/ && $good == 1 ) {
$prot = $1;
next;
}
if ( $_ =~ /^.SHLABEL\s?(\S+)$/ && $good == 1 ) {
$good = 0 if ($1 eq "");
$intA = $1 if ( $prot eq "A" );
$intB = $1 if ( $prot eq "B" );
$good = 0 if (($prot eq "A" && $intA eq "") || ($prot eq "B" && $intB eq ""));
next;
}
if ( $_ =~ /\s+ORGANISM\s(.+)$/ && $good == 1 ) {
my $orga = $1;
if ( $prot eq "A" ) {
$orgaA = $hash_orga_tax{$orga} if ( defined( $hash_orga_tax{$orga} ) );
$good = 0 if ( !defined($orgaA) );
}
elsif ( $prot eq "B" ) {
$orgaB = $hash_orga_tax{$orga} if ( defined( $hash_orga_tax{$orga} ) );
$good = 0 if ( !defined($orgaB) );
}
if ( defined($orgaA) && defined($orgaB) ) {
$orga_queryA = "$hash_orga_tax{$orgaA} [$orgaA]";
$orga_queryB = "$hash_orga_tax{$orgaB} [$orgaB]";
if ( exists( $hash_uniprot_id->{$intA}->{$orga_queryA} ) )
{ # If the uniprot id has already been retrieved (and is now stored in the file)
if ($hash_uniprot_id->{$intA}->{$orga_queryA} eq "undef") { # If the uniprot id is currently irrecoverable
print "[DEBUG : BIND] uniprot A : Uniprot is unknown for $intA and $orga_queryA\n" if ($main::verbose);
$good = 0;
next;
}
else { # If the uniprot id exists and is already retrieving
$uniprot_A = $hash_uniprot_id->{$intA}->{$orga_queryA}; # we retrieve it from the file
print "[DEBUG : BIND] uniprot A : $uniprot_A retrieve from file\n" if ($main::verbose);
}
}
else { # If we need to retrieve it from the web
$uniprot_A = $this->SUPER::gene_name_to_uniprot_id( $intA, $orga_queryA )
; # We call the corresponding function
if ( $uniprot_A eq "1" || $uniprot_A eq "0" ) {
$hash_error{$intA} = $uniprot_A;
$hash_uniprot_id->{$intA}->{$orga_queryA} = "undef"; # We indicates that we already search it during this running
# But we don't store it into the file to be able to search it later
print"[DEBUG : BIND] uniprot A : error retrieving uniprot from internet\n"
if ($main::verbose);
$good = 0;
next;
}
else {
$hash_uniprot_id->{$intA}->{$orga_queryA} = $uniprot_A; # We store it in the hash
print gene_name_to_uniprot_file "$intA\t$uniprot_A\t$orga_queryA\n"; # We store it in the file
}
}
if ( exists( $hash_uniprot_id->{$intB}->{$orga_queryB} ) )
{ # If the uniprot id has already been retrieved (and is now stored in the file)
if ($hash_uniprot_id->{$intB}->{$orga_queryB} eq "undef") { # If the uniprot id is currently irrecoverable
print "[DEBUG : BIND] uniprot B : Uniprot is unknown for $intB and $orga_queryB\n" if ($main::verbose);
$good = 0;
next;
}
else { # If the uniprot id exists and is already retrieving
$uniprot_B = $hash_uniprot_id->{$intB}->{$orga_queryB}; # we retrieve it from the file
print "[DEBUG : BIND] uniprot B : $uniprot_B retrieve from file\n" if ($main::verbose);
}
}
else { # If we need to retrieve it from the web
$uniprot_B =
$this->gene_name_to_uniprot_id( $intB, $orga_queryB )
; # We call the corresponding function
if ( $uniprot_B eq "1" || $uniprot_B eq "0" ) {
$hash_error{$intB} = $uniprot_B;
$hash_uniprot_id->{$intB}->{$orga_queryB} = "undef"; # We indicates that we already search it during this running
# But we don't store it into the file to be able to search it later
print "[DEBUG : BIND] uniprot B : error retrieving uniprot from internet\n"
if ($main::verbose);
$good = 0;
next;
}
else {
$hash_uniprot_id->{$intB}->{$orga_queryB} = $uniprot_B; # We store it in the hash
print gene_name_to_uniprot_file "$intB\t$uniprot_B\t$orga_queryB\n"; # We store it in the file
}
}
}
next;
}
if ( /^\sEXPSYSTEM\s(.*)$/ && $good == 1 ) {
next if ( $1 eq "" );
$hash_sysExp{$1} = 1 if ( !defined( $hash_sysExp{$1} ) );
print "[DEBUG : BIND] sys_exp retrieved\n" if ($main::verbose);
}
if ( /^\sEXPCONDREFPMID\s(\d*)$/ && $good == 1 ) {
next if ( $1 eq "" );
$hash_pubmed{$1} = $1 if ( !defined( $hash_pubmed{$1} ) );
print "[DEBUG : BIND] pubmed retrieved\n" if ($main::verbose);
}
}
}
$this->SUPER::sendBDD();
close gene_name_to_uniprot_file;
$this->SUPER::error_internet( \%hash_error );
close data_file;
print "\nEOF\n";
}
#Download Bind data file from BOND database
# @param @taxids => (Optional) an array of taxonomy identifier. The program will download interaction only from thoses organism
# @return => the file path to .txt file
# => the sucess/failure code
# 1 Sucess: New version found and downloaded
# -1 Sucess: No new version, no need for update (but you still have the path to current version as first return value)
# -2 Failure: Connexion or login failed
# -3 Failure: Can't create/find download folder
# -4 Failure: Uncompressing failed
# -5 Failure: Missing email or password
sub download {
my ( $this, @taxids ) = @_;
unless (@taxids) {
@taxids = ( "3702", "6239", "7227", "9606", "10090", "4932", "4896" );
}
no warnings 'numeric';
use URI::Escape;
#checking if we have a email and password used to connect to BOND
if ( not $this->{email} or not $this->{password} ) {
print("Error: Missing email or passowrd for Login!");
return ( "", -5 );
}
#working folder (with the name of current DB)
my $folder = $this->setDownloadFolder( uc(__PACKAGE__) );
#Setting folder failed
return ( "", -3 ) if ( int($folder) == -1 );
my $fileUncompressed = $folder . "Bind.txt";
#Preparing the user agent and the cookie storage
my $ua = $this->setUserAgent();
#Login to BOND
my $result = "";
my $res;
my $attempt = 1;
until ( $result =~ /.*>Logout<.*/ ) {
return ( "", -2 ) if ( $attempt > 5 ); #Login failed
print( "Connecting to " . __PACKAGE__ . "... " );
if ( $attempt > 1 ) { print( "(Attempt #" . $attempt . ")" ); }
print("\n");
$res = $ua->post(
"http://bond.unleashedinformatics.com/Action?pg=50000",
[
"email" => $this->{email},
"password" => $this->{password},
"url" => ""
]
);
$result = $res->content;
#print Dumper($res);
$attempt++;
}
#Query searching all protein-protein interaction for all organisms in @taxids
my $query = '
RecordType: (interaction)
AND
(
(
+(
+RecordType:(interaction)
+(
+interaction.object.type:"protein"
+taxid:(' . join( " ", @taxids ) . ' )
)
)
)
-(
(+BINDInteraction.a.type:"not specified" +BINDInteraction.a.shortlabel:"Unknown")
(+BINDInteraction.b.type:"not specified" +BINDInteraction.b.shortlabel:"Unknown")
)
)';
$query =~ s/\n//g;
$query =~ s/\t//g;
$query =~ s/\s{2}//g;
$query = uri_escape($query); #Encoding for post transaction
#print $query; exit();
#Downloading Bind interactions
use HTTP::Request::Common qw( POST );
my $req = POST(
"http://bond.unleashedinformatics.com/Action",
Content_Type => 'application/x-www-form-urlencoded',
#Connection => "keep-alive",
Content => "pg=3105"
. "&butval=change"
. "&type=6"
. #File type: flat file
"&query=" . $query #Query for BOND database extractingg informations
);
#If download file already exists => saving the old one as old.
my $oldFile = $folder . "old-Bind.txt";
move( $fileUncompressed, $oldFile ) if ( -e $fileUncompressed );
#Downloading BIND with multiple attempt
my $downloadOk = 0;
$attempt = 1;
$ua->show_progress('true value');
until ($downloadOk) {
return ( "", -2 ) if ( $attempt > 5 ); #Download failed after 5 attempt
print( "Downloading " . __PACKAGE__ . " data... (Can be very long)" );
if ( $attempt > 1 ) {
sleep(1);
print( " (Attempt #" . $attempt . ")" );
}
print("\n");
#use Data::Dumper;print Dumper($req);
$res = $ua->request( $req, $fileUncompressed );
$downloadOk = $res->is_success;
$attempt++;
}
#Connexion failed unless the response is successful
return ( "", -2 ) unless ( $res->is_success );
#Checking that the file is not a dummy html file
return ( "", -2 ) unless ( -e $fileUncompressed );
my $i = 0;
open F, $fileUncompressed;
while (<F>) {
last if ( $i > 30 ); #Checking only the first 30 lines
if (/.*<html>.*/) {
unlink($fileUncompressed);
return ( "", -2 ); #File contains html :(
}
$i++;
}
close F;
#Compare new and old file (if exists)
if ( -e $oldFile ) {
if ( $this->md5CheckFile( $fileUncompressed, $oldFile ) ) {
unlink($oldFile);
return ( $fileUncompressed, -1 )
; # No need for update old and new are the same
}
}
#Fully successful
return ( $fileUncompressed, 1 );
}
1;