/
tripal_db.api.inc
520 lines (477 loc) · 16.5 KB
/
tripal_db.api.inc
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<?php
/**
* @file
* Provides an application programming interface (API) to manage references to
* external databases
*/
/**
* @defgroup tripal_db_api Database Reference API
* @ingroup tripal_api
* @{
* Provides an application programming interface (API) to manage references to external databases
* @}
*/
/**
* Retrieves a chado db variable
*
* Example Usage:
* @code
$select_values = array(
'name' => 'SOFP'
);
$db_object = tripal_get_db($select_values);
* @endcode
* The above code selects the SOFP db and returns the following object:
* @code
$db_object = stdClass Object (
[db_id] => 49
[name] => SOFP
[description] =>
[urlprefix] =>
[url] =>
);
* @endcode
*
* @param $identifier
* An array with the key stating what the identifier is. Supported keys (only on of the
* following unique keys is required):
* - db_id: the chado db.db_id primary key
* - name: the chado db.name field (assume unique)
* @param $options
* An array of options. Supported keys include:
* - Any keys supported by chado_generate_var(). See that function definition for
* additional details.
*
* NOTE: the $identifier parameter can really be any array similar to $values passed into
* chado_select_record(). It should fully specify the db record to be returned.
*
* @return
* If unique values were passed in as an identifier then an object describing the cv
* will be returned (will be a chado variable from chado_generate_var()). Otherwise,
* an array of objects will be returned.
*
* @ingroup tripal_db_api
*/
function tripal_get_db($identifiers, $options = array()) {
// Set Defaults
if (!isset($options['include_fk'])) {
// Tells chado_generate_var not to follow any foreign keys
$options['include_fk'] = array();
}
// Error Checking of parameters
if (!is_array($identifiers)) {
tripal_report_error(
'tripal_db_api',
TRIPAL_ERROR,
"tripal_get_db: The identifier passed in is expected to be an array with the key
matching a column name in the db table (ie: db_id or name). You passed in %identifier.",
array(
'%identifier'=> print_r($identifiers, TRUE)
)
);
}
elseif (empty($identifiers)) {
tripal_report_error(
'tripal_db_api',
TRIPAL_ERROR,
"tripal_get_db: You did not pass in anything to identify the db you want. The identifier
is expected to be an array with the key matching a column name in the db table
(ie: db_id or name). You passed in %identifier.",
array(
'%identifier'=> print_r($identifiers, TRUE)
)
);
}
// Try to get the db
$db = chado_generate_var(
'db',
$identifiers,
$options
);
// Ensure the db is singular. If it's an array then it is not singular
if (is_array($db)) {
tripal_report_error(
'tripal_db_api',
TRIPAL_ERROR,
"tripal_get_db: The identifiers you passed in were not unique. You passed in %identifier.",
array(
'%identifier'=> print_r($identifiers, TRUE)
)
);
}
// Report an error if $db is FALSE since then chado_generate_var has failed
elseif ($db === FALSE) {
tripal_report_error(
'tripal_db_api',
TRIPAL_ERROR,
"tripal_get_db: chado_generate_var() failed to return a db based on the identifiers
you passed in. You should check that your identifiers are correct, as well as, look
for a chado_generate_var error for additional clues. You passed in %identifier.",
array(
'%identifier'=> print_r($identifiers, TRUE)
)
);
}
// Else, as far we know, everything is fine so give them their db :)
else {
return $db;
}
}
/**
* Create an options array to be used in a form element
* which provides a list of all chado dbs
*
* @return
* An array(db_id => name) for each db in the chado db table
*
* @ingroup tripal_db_api
*/
function tripal_get_db_select_options() {
$dbs = chado_query("SELECT db_id, name FROM {db} ORDER BY name");
$options = array();
$options[] = 'Select a Database';
foreach ($dbs as $db) {
$options[$db->db_id] = $db->name;
}
return $options;
}
/**
* Retrieves a chado database reference variable
*
* Example Usage:
* @code
$identifiers = array(
'accession' => 'synonym',
'db_id' => array(
'name' => 'SOFP'
)
);
$dbxref_object = tripal_get_dbxref($identifiers);
* @endcode
* The above code selects the synonym database reference and returns the following object:
* @code
$dbxref_object = stdClass Object (
[dbxref_id] => 2581
[accession] => synonym
[description] =>
[version] =>
[db_db_id] => 49
[db_name] => SOFP
[db_description] =>
[db_urlprefix] =>
[db_url] =>
);
* @endcode
*
* @param $identifier
* An array apropriate for use with the chado_generate_var for uniquely
* identifying a dbxref record. Alternatively, there are also some specially
* handled keys. They are:
* - property: An array/object describing the property to select records for. It
* should at least have either a type_name (if unique across cvs) or type_id. Other
* supported keys include: cv_id/cv_name (of the type), value and rank
* There are also some specially handled keys. They are:
* - property: An array/object describing the property to select records for. It
* should at least have either a type_name (if unique across cvs) or type_id. Other
* supported keys include: cv_id/cv_name (of the type), value and rank
* @param $options
* An array of options. Supported keys include:
* - Any keys supported by chado_generate_var(). See that function definition for
* additional details.
*
* NOTE: the $identifier parameter can really be any array similar to $values passed into
* chado_select_record(). It should fully specify the dbxref record to be returned.
*
* @return
* If unique values were passed in as an identifier then an object describing the dbxref
* will be returned (will be a chado variable from chado_generate_var()). Otherwise,
* FALSE will be returned.
*
* @ingroup tripal_db_api
*/
function tripal_get_dbxref($identifiers, $options = array()) {
// Set Defaults
if (!isset($options['include_fk'])) {
// Tells chado_generate_var not only expand the db
$options['include_fk'] = array('db_id' => TRUE);
}
// Error Checking of parameters
if (!is_array($identifiers)) {
tripal_report_error(
'tripal_db_api',
TRIPAL_ERROR,
"tripal_get_dbxref: The identifier passed in is expected to be an array with the key
matching a column name in the dbxref table (ie: dbxref_id or name). You passed in %identifier.",
array(
'%identifier'=> print_r($identifiers, TRUE)
)
);
}
elseif (empty($identifiers)) {
tripal_report_error(
'tripal_db_api',
TRIPAL_ERROR,
"tripal_get_dbxref: You did not pass in anything to identify the dbxref you want. The identifier
is expected to be an array with the key matching a column name in the dbxref table
(ie: dbxref_id or name). You passed in %identifier.",
array(
'%identifier'=> print_r($identifiers, TRUE)
)
);
}
// If one of the identifiers is property then use chado_get_record_with_property()
if (isset($identifiers['property'])) {
$property = $identifiers['property'];
unset($identifiers['property']);
$dbxref = chado_get_record_with_property(
array('table' => 'dbxref', 'base_records' => $identifiers),
array('type_name' => $property),
$options
);
}
// Else we have a simple case and we can just use chado_generate_var to get the analysis
else {
$dbxref = chado_generate_var('dbxref', $identifiers, $options);
}
// Ensure the dbxref is singular. If it's an array then it is not singular
if (is_array($dbxref)) {
tripal_report_error('tripal_db_api', TRIPAL_ERROR,
"tripal_get_dbxref: The identifiers you passed in were not unique. You passed in %identifier.",
array('%identifier'=> print_r($identifiers, TRUE))
);
}
// Report an error if $dbxref is FALSE since then chado_generate_var has failed
elseif ($dbxref === FALSE) {
tripal_report_error(
'tripal_db_api',
TRIPAL_ERROR,
"tripal_get_dbxref: chado_generate_var() failed to return a dbxref based on the identifiers
you passed in. You should check that your identifiers are correct, as well as, look
for a chado_generate_var error for additional clues. You passed in %identifier.",
array(
'%identifier'=> print_r($identifiers, TRUE)
)
);
}
// Else, as far we know, everything is fine so give them their dbxref :)
else {
return $dbxref;
}
}
/**
* Adds a new database to the Chado DB table and returns the DB object.
*
* @param $values
* An associative array of the values of the db (those to be inserted)
* - name: The name of the database. This name is usually used as the prefix for
* CV term accessions
* - description: (Optional) A description of the database. By default no description is required.
* - url: (Optional) The URL for the database
* - urlprefix: (Optional) The URL that is to be used as a prefix when constructing a
* link to a database term
* @param $options
* Optional. An associative array of options that can include:
* - update_existing: Set this to '1' to force an update of the database if it
* already exists. The default is to not update. If the database exists
* then nothing is added.
*
* @return
* An object populated with fields from the newly added database. If the
* database already exists it returns the values in the current entry.
*
* @ingroup tripal_db_api
*/
function tripal_insert_db($values, $options = array()) {
// Default Values
$dbname = $values['name'];
$description = (isset($values['description'])) ? $values['description'] : '';
$url = (isset($values['url'])) ? $values['url'] : '';
$urlprefix = (isset($values['urlprefix'])) ? $values['urlprefix'] : '';
$update = (isset($options['update_existing'])) ? $options['update_existing'] : TRUE;
// build the values array for inserting/updating
$ins_values = array(
'name' => $dbname,
'description' => $description,
'url' => $url,
'urlprefix' => $urlprefix
);
// get the database record if it already exists
$sel_values = array('name' => $dbname);
$sel_options = array('statement_name' => 'sel_db_na');
$result = chado_select_record('db', array('*'), $sel_values, $sel_options);
// if it does not exists then add it
if (count($result) == 0) {
$ins_options = array('statement_name' => 'ins_db_nadeurur');
$success = chado_insert_record('db', $ins_values, $ins_options);
if (!$success) {
tripal_report_error('tripal_db', TRIPAL_WARNING, "Cannot create db '$dbname'.", NULL);
return 0;
}
$result = chado_select_record('db', array('*'), $sel_values, $sel_options);
}
// if it exists and update is enabled the do the update
elseif ($update) {
$upd_options = array('statement_name' => 'upd_db_nadeurur');
$success = chado_update_record('db', $sel_values, $ins_values, $upd_options);
if (!$success) {
tripal_report_error('tripal_db', TRIPAL_WARNING, "Cannot update db '$dbname'.", NULL);
return 0;
}
$result = chado_select_record('db', array('*'), $sel_values, $sel_options);
}
// return the database object
return $result[0];
}
/**
* Add a database reference
*
* @param $values
* An associative array of the values to be inserted including:
* - db_id: the database_id of the database the reference is from
* - accession: the accession
* - version: (Optional) The version of the database reference
* - description: (Optional) A description of the database reference
*
* @ingroup tripal_db_api
*/
function tripal_insert_dbxref($values) {
$db_id = $values['db_id'];
$accession = $values['accession'];
$version = (isset($values['version'])) ? $values['version'] : '';
$description = (isset($values['description'])) ? $values['description'] : '';
$ins_values = array(
'db_id' => $db_id,
'accession' => $accession,
'version' => $version,
'description' => $description
);
// check to see if the dbxref exists
$sel_values = array(
'db_id' => $db_id,
'accession' => $accession,
'version' => $version
);
$result = chado_select_record('dbxref', array('*'), $sel_values);
// if it doesn't already exist then add it
if (!$result) {
$success = chado_insert_record('dbxref', $ins_values);
if (!$success) {
tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to insert the dbxref record $accession", NULL);
return 0;
}
$result = chado_select_record('dbxref', array('*'), $sel_values);
}
if (isset($result[0])) {
return $result[0];
}
else {
return FALSE;
}
}
/**
* Add a record to a database reference linking table (ie: feature_dbxref)
*
* @param $basetable
* The base table for which the dbxref should be associated. Thus to associate a dbxref
* with a feature the basetable=feature and dbxref_id is added to the feature_dbxref table
* @param $record_id
* The primary key of the basetable to associate the dbxref with. This should be in integer.
* @param $dbxref
* An associative array describing the dbxref. Valid keys include: 'accession' => the
* accession for the dbxref, 'db_name' => the name of the database the dbxref belongs to;
* 'db_id' => the primary key of the database the dbxref belongs to.
* @param $options
* An associative array of options. Valid keys include:
* - insert_dbxref: Insert the dbxref if it doesn't already exist. TRUE is the default
*
* @ingroup tripal_db_api
*/
function tripal_associate_dbxref($basetable, $record_id, $dbxref, $options = array()) {
$linking_table = $basetable . '_dbxref';
$foreignkey_name = $basetable . '_id';
// Default Values
$options['insert_dbxref'] = (isset($options['insert_dbxref'])) ? $options['insert_dbxref'] : TRUE;
// If the dbxref_id is set then we know it already exists
// Otherwise, select to check
if (!isset($dbxref['dbxref_id'])) {
$values = array(
'accession' => $dbxref['accession'],
);
if (isset($dbxref['db_id'])) {
$values['db_id'] = $dbxref['db_id'];
} elseif (isset($dbxref['db_name'])) {
$values['db_id'] = array(
'name' => $dbxref['db_name']
);
}
else {
tripal_report_error(
'tripal_db_api',
TRIPAL_WARNING,
"tripal_associate_dbxref: The dbxref needs to have either the db_name or db_id
supplied. You were trying to associate a dbxref with the %base %record_id
and supplied the dbxref values: %dbxref.",
array('%base' => $basetable, '%record_id' => $record_id, '%dbxref' => print_r($dbxref,TRUE))
);
return FALSE;
}
$select = chado_select_record('dbxref',array('*'), $values);
if ($select) {
$dbxref['dbxref_id'] = $select[0]->dbxref_id;
}
elseif ($options['insert_dbxref']) {
// Insert the dbxref
$insert = tripal_insert_dbxref($values);
if (isset($insert->dbxref_id)) {
$dbxref['dbxref_id'] = $insert->dbxref_id;
}
else {
tripal_report_error(
'tripal_db_api',
TRIPAL_WARNING,
"tripal_associate_dbxref: Unable to insert the dbxref using the dbxref values: %dbxref.",
array('%dbxref' => print_r($dbxref,TRUE))
);
return FALSE;
}
}
else {
tripal_report_error(
'tripal_api',
TRIPAL_WARNING,
"tripal_associate_dbxref: The dbxref doesn't already exist. You supplied the dbxref values: %dbxref.",
array('%dbxref' => print_r($dbxref,TRUE))
);
return FALSE;
}
}
// Now add the link between the record & dbxref
if ($dbxref['dbxref_id'] > 0) {
$values = array(
'dbxref_id' => $dbxref['dbxref_id'],
$foreignkey_name => $record_id
);
$result = chado_select_record($linking_table, array('*'), $values);
// if it doesn't already exist then add it
if (!$result) {
$success = chado_insert_record($linking_table, $values);
if (!$success) {
tripal_report_error(
'tripal_api',
TRIPAL_WARNING,
"Failed to insert the %base record %accession",
array('%base' => $linking_table, '%accession' => $dbxref['accession'])
);
return FALSE;
}
$result = chado_select_record($linking_table, array('*'), $values);
}
if (isset($result[0])) {
return $result[0];
}
else {
return FALSE;
}
}
return FALSE;
}