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
/
DBConnector.pm
314 lines (259 loc) · 10.3 KB
/
DBConnector.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
package DBConnector;
use strict;
use warnings;
use Interaction;
use DBI;
use Data::Dumper;
sub new() {
my ( $class, $host, $port, $dbname, $username, $password ) = @_;
my $this = {
'_host' => $host,
'_port' => $port,
'_dbname' => $dbname,
'_username' => $username,
'_password' => $password,
'_dbh' => undef,
# might make following arguments editable
'_autoCommit' => 0,
'_raiseError' => 1,
'_printError' => 0
};
$this->{'_dbh'} = DBI->connect(
'dbi:Pg:dbname=' . $dbname . ';host=' . $host . ';port=' . $port,
$username,
$password,
{
AutoCommit => $this->{'_autoCommit'},
RaiseError => $this->{'_raiseError'},
PrintError => $this->{'_printError'}
}
) or die "Error DBI.\n";
bless( $this, $class );
return $this;
}
sub _commit() {
my ($this) = @_;
$this->{'_dbh'}->commit();
}
sub _rollback() {
my ($this) = @_;
$this->{'_dbh'}->rollback();
}
sub disconnect () {
my ($this) = shift;
eval { $this->{'_dbh'}->disconnect() };
}
sub insert() {
# Insère une liste de PPI dans la base de données
#
#
# DESCRIPTION
# On insère les données "dures" dans un premier temps, à savoir:
# notation: - <table> (<champ1>, <champ2>, <etc>)
#
# - protein (uniprot_id, gene_name)
# - source_database (name)
# - publication (pubmed_id)
# On récupère alors les identifiants des données nouvellement insérées, ou les ID existants le cas échéant.
#
# On récupère ensuite les données "fixes", à savoir
# - organism (tax_id)
# - experimental_system (name)
#
# Il nous reste à remplir DANS CET ORDRE (!) les tables de relations avec les données, à savoir
# - interaction_data (db_source_name, pubmed_id, organism_tax_id,experimental_system)
# - interaction (protein_id1, protein_id2, interaction_data_id)
# (- homogolgy) -> pour plus tard
# Petit rappel sur l'organisation des requêtes
# - on prépare une requete, avec "?" pour symboliser les variables
# - on execute autant qu'on veut la requete, avec les variables en paramètre
# - on commit le tout quand ça nous arrange
my ( $this, $PPiGrp ) = @_;
# print "[INFO] preparing statements\n";
#--- preparer toutes les requetes (statements) pour le groupe de PPi ---#
# INSERT statements
my $sth_insert_protein = $this->{'_dbh'}->prepare("INSERT INTO protein(uniprot_id, gene_name, organism_id) VALUES (?, ?, ?) RETURNING id");
my $sth_insert_publication = $this->{'_dbh'}->prepare("INSERT INTO publication(pubmed_id) VALUES (?) RETURNING pubmed_id");
my $sth_insert_exp_system = $this->{'_dbh'}->prepare("INSERT INTO experimental_system(name) VALUES (?) RETURNING name");
# my $sth_insert_src_db = $this->{'_dbh'}->prepare("INSERT INTO source_database(name) VALUES (?) RETURNING name");
# my $sth_insert_organism = $this->{'_dbh'}->prepare("INSERT INTO organism(tax_id) VALUES (?) RETURNING tax_id");
my $sth_insert_interaction_data = $this->{'_dbh'}->prepare("INSERT INTO interaction_data(db_source_name, pubmed_id, experimental_system) VALUES (?,?,?) RETURNING id");
my $sth_insert_interaction = $this->{'_dbh'}->prepare("INSERT INTO interaction(protein_id1, protein_id2) VALUES (?,?) RETURNING id");
my $sth_insert_link_data_interaction = $this->{'_dbh'}->prepare("INSERT INTO link_data_interaction(interaction_id, interaction_data_id) VALUES (?,?)");
# SELECT statements
my $sth_select_protein = $this->{'_dbh'}->prepare("SELECT id FROM protein WHERE uniprot_id = ? AND gene_name = ?");
my $sth_select_src_db = $this->{'_dbh'}->prepare("SELECT name FROM source_database WHERE name = ?");
my $sth_select_publication = $this->{'_dbh'}->prepare("SELECT pubmed_id FROM publication WHERE pubmed_id = ?");
my $sth_select_organism_from_tax_id = $this->{'_dbh'}->prepare("SELECT tax_id FROM organism WHERE tax_id = ?");
my $sth_select_organism_from_name = $this->{'_dbh'}->prepare("SELECT tax_id FROM organism WHERE name = ?");
my $sth_select_exp_sys = $this->{'_dbh'}->prepare("SELECT name FROM experimental_system WHERE name = ?");
my $sth_select_interaction_data = $this->{'_dbh'}->prepare("SELECT id FROM interaction_data WHERE db_source_name = ? AND pubmed_id = ? AND experimental_system = ?");
my $sth_select_interaction = $this->{'_dbh'}->prepare("SELECT id FROM interaction WHERE protein_id1 = ? AND protein_id2 = ?");
# no update statement needed here
#print "[INFO] Start reading PPi\n";
# parcours de la liste de PPi
foreach my $PPi ( @{$PPiGrp} ) {
# print "--------------------------------------------\n";
# les différents ID nécessaires pour peupler les tables de relations
# /!\ pubmed et sysExp sont des listes d'ID, il faudra mettre toutes les combinaisons dans interaction_data (et donc plusieurs insertions)
my $idInteractorA = undef;
my $idInteractorB = undef;
my $idInteraction = undef;
my @idInteractionData = ();
my @idPublications = @{ $PPi->{pubmed} };
my @idSysExp = @{ $PPi->{sys_exp} };
my $db_source = $PPi->{database};
# execution des statements
# print "[INFO] insertion interacteur A\n";
#--- insertion de l'interacteur A et récupération de son ID ---#
eval {
$sth_insert_protein->execute( $PPi->getUniprotA(), $PPi->getGeneNameA(), $PPi->getTaxIdA() );
$this->_commit();
$idInteractorA = (keys %{$sth_insert_protein->fetchall_hashref('id')})[0];
# print "[INTERACTOR A]\t",$PPi->getUniprotA(), ":", $PPi->getGeneNameA(),"\n";
1;
} or do {
$this->_rollback();
$sth_select_protein->execute( $PPi->getUniprotA(), $PPi->getGeneNameA() );
($idInteractorA) = $sth_select_protein->fetchrow_array();
};
# print "[INFO] ID interacteur A: $idInteractorA\n";
#--- insertion de l'interacteur B puis récupération de son ID ---#
# print "[INFO] insertion interacteur B\n";
eval {
$sth_insert_protein->execute( $PPi->getUniprotB(), $PPi->getGeneNameB(), $PPi->getTaxIdB() );
$this->_commit();
($idInteractorB) = (keys %{$sth_insert_protein->fetchall_hashref('id')})[0];
# print "[INTERACTOR B]\t",$PPi->getUniprotB(), ":", $PPi->getGeneNameB(),"\n";
1;
} or do {
$this->_rollback();
$sth_select_protein->execute( $PPi->getUniprotB(), $PPi->getGeneNameB() );
($idInteractorB) = $sth_select_protein->fetchrow_array();
};
#print "[INFO] ID interacteur B: $idInteractorB\n";
#print "[INFO] insertion publications\n";
#--- Insertion de la liste des pubmedIDs ---#
foreach my $pubmedid ( @{ $PPi->{pubmed} } ) {
eval {
$sth_insert_publication->execute($pubmedid);
$this->_commit();
#print "[PUBMED ID]\t", $pubmedid,"\n";
1;
} or do {
$this->_rollback();
};
}
#print "[INFO] insertion experimental systems\n";
#--- Insertion de la liste des systemes experimentaux ---#
foreach my $sysexp ( @{ $PPi->{sys_exp} } ) {
eval {
$sth_insert_exp_system->execute($sysexp);
$this->_commit();
#print "[EXP SYSTEM]\t$sysexp\n";
1;
} or do {
$this->_rollback();
};
}
print "----------------------------------------------------------\n\n";
print "Interactor A under ID: ", $idInteractorA, "\n";
print "Interactor B under ID: ", $idInteractorB, "\n";
print "Pubmed IDs : @idPublications \n";
print "ExpSystems : @idSysExp \n";
print "----------------------------------------------------------\n\n";
#--- Insertion des interaction_data ---#
foreach my $pubmed_id (@idPublications) {
foreach my $sys_exp (@idSysExp) {
eval {
# print " ### $PPi->{database}\t$pubmed_id\t$PPi->{organism}\t$sys_exp\n";
$sth_insert_interaction_data->execute(
$PPi->{database},
$pubmed_id,
$sys_exp
);
$this->_commit();
push (@idInteractionData , (keys %{$sth_insert_interaction_data->fetchall_hashref('id')})[0]);
1;
} or do {
$this->_rollback();
$sth_select_interaction_data->execute(
$PPi->{database},
$pubmed_id,
$sys_exp
);
push (@idInteractionData , $sth_select_interaction_data->fetchrow_array());
};
}
}
#--- next PPi if we cannot properly add an interaction ---#
# thus we store in the database all information about the interaction (which can be reused)
# print "@idInteractionData\n";
next unless (@idInteractionData && defined($idInteractorA) && defined($idInteractorB));
#--- Insertion de l'interaction ---#
# print "[INFO] insertion interaction\n";
eval {
$sth_insert_interaction->execute( $idInteractorA, $idInteractorB );
$this->_commit();
($idInteraction) = (keys %{$sth_insert_interaction->fetchall_hashref('id')})[0];
# print "[SUCCESS]- insertion interaction\n";
1;
} or do {
$this->_rollback();
$sth_select_interaction->execute( $idInteractorA, $idInteractorB );
($idInteraction) = $sth_select_interaction->fetchrow_array();
};
#--- Insertion dans link_data_interaction ---#
foreach my $idInteractionData (@idInteractionData) {
print "[DEBUG : DBConnector] idInteraction: $idInteraction :: idInteractionData: $idInteractionData\n" if ($main::verbose);
eval {
$sth_insert_link_data_interaction->execute( $idInteraction, $idInteractionData );
$this->_commit();
1;
} or do {
$this->_rollback();
};
}
}
}
#
# Insert a set of homologies from a Homologene database file
#
sub insertHomology {
my ($this, $HomGrp) = @_;
my $sth_insert_homology = $this->{'_dbh'}->prepare("INSERT INTO homology(h_id, ptn_id) VALUES (?, ?)");
my $sth_select_protein = $this->{'_dbh'}->prepare("SELECT id FROM protein WHERE uniprot_id = ? AND gene_name = ?");
foreach my $hmlgy ( @{$HomGrp} ) {
my($uniprot, $genename, $hid) = @{$hmlgy};
my $ptn_id;
#-- recup le iprotein.id
eval {
print "[DEBUG : DBConnector] trying to get id for $uniprot / $genename\n" if ($main::verbose);
$sth_select_protein->execute( $uniprot, $genename );
($ptn_id) = $sth_select_protein->fetchrow_array();
#print "[DEBUG : DBConnector] protein id : $ptn_id\n";
print "." unless ($main::verbose);
1;
} or do {
print "[DEBUG : DBConnector] Cannot get ID for $uniprot / $genename, next\n" if ($main::verbose);
next;
};
#--- Insertion de l'homologie ---#
print "[DBConnector : INFO] insertion homologie\n" if ($main::verbose);
eval {
$sth_insert_homology->execute( $hid, $ptn_id );
$this->_commit();
print "[DBConnector : SUCCESS]- homologie interaction\n" if ($main::verbose);
1;
} or do {
$this->_rollback();
print "[DBConnector : FAILED] homolgy\n" if ($main::verbose);
};
}
}
sub DESTROY {
my ($this) = shift;
$this->disconnect();
}
1;
__END__