123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585 |
- <?php
- function tripal_get_db($identifiers, $options = array()) {
-
- if (!isset($options['include_fk'])) {
-
- $options['include_fk'] = array();
- }
-
- if (!is_array($identifiers)) {
- tripal_report_error(
- 'tripal_chado_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_chado_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)
- )
- );
- }
-
- $db = chado_generate_var(
- 'db',
- $identifiers,
- $options
- );
-
- if (is_array($db)) {
- tripal_report_error(
- 'tripal_chado_api',
- TRIPAL_ERROR,
- "tripal_get_db: The identifiers you passed in were not unique. You passed in %identifier.",
- array(
- '%identifier'=> print_r($identifiers, TRUE)
- )
- );
- }
-
- elseif ($db === FALSE) {
- tripal_report_error(
- 'tripal_chado_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 {
- return $db;
- }
- }
- 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;
- }
- function tripal_get_dbxref($identifiers, $options = array()) {
-
- if (!isset($options['include_fk'])) {
-
- $options['include_fk'] = array('db_id' => TRUE);
- }
-
- 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 (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 {
- $dbxref = chado_generate_var('dbxref', $identifiers, $options);
- }
-
- 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))
- );
- }
-
- 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 {
- return $dbxref;
- }
- }
- function tripal_get_dbxref_url($dbxref) {
- $final_url = '';
-
- if ($dbxref->db_id->urlprefix) {
- $db_count = 0;
- $acc_count = 0;
- $url = $dbxref->db_id->urlprefix;
-
- $url = preg_replace('/\{db\}/', $dbxref->db_id->name, $url, -1, $db_count);
- $url = preg_replace('/\{accession\}/', $dbxref->accession, $url, -1, $acc_count);
- $final_url = $url;
-
-
- if (!$db_count and !$acc_count) {
- $final_url = $dbxref->db_id->urlprefix . $dbxref->db_id->name . ':' . $dbxref->accession;
- }
-
- if (!preg_match('/^(http|https)/', $final_url)) {
- $final_url = url($final_url, array('absolute' => TRUE));
- }
- }
- return $final_url;
- }
- function tripal_insert_db($values, $options = array()) {
-
- $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;
-
- $ins_values = array(
- 'name' => $dbname,
- 'description' => $description,
- 'url' => $url,
- 'urlprefix' => $urlprefix
- );
-
- $sel_values = array('name' => $dbname);
- $result = chado_select_record('db', array('*'), $sel_values);
-
- 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_chado', TRIPAL_WARNING, "Cannot create db '$dbname'.", NULL);
- return 0;
- }
- $result = chado_select_record('db', array('*'), $sel_values);
- }
-
- 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_chado', TRIPAL_WARNING, "Cannot update db '$dbname'.", NULL);
- return 0;
- }
- $result = chado_select_record('db', array('*'), $sel_values);
- }
-
- return $result[0];
- }
- 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
- );
-
- $sel_values = array(
- 'db_id' => $db_id,
- 'accession' => $accession,
- 'version' => $version
- );
- $result = chado_select_record('dbxref', array('*'), $sel_values);
-
- if (!$result) {
- $success = chado_insert_record('dbxref', $ins_values);
- if (!$success) {
- tripal_report_error('tripal_chado', 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;
- }
- }
- function tripal_associate_dbxref($basetable, $record_id, $dbxref, $options = array()) {
- $linking_table = $basetable . '_dbxref';
- $foreignkey_name = $basetable . '_id';
-
- $options['insert_dbxref'] = (isset($options['insert_dbxref'])) ? $options['insert_dbxref'] : TRUE;
-
-
- 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_chado_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 = tripal_insert_dbxref($values);
- if (isset($insert->dbxref_id)) {
- $dbxref['dbxref_id'] = $insert->dbxref_id;
- }
- else {
- tripal_report_error(
- 'tripal_chado_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;
- }
- }
-
- 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 (!$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;
- }
- function tripal_autocomplete_dbxref($db_id, $string = '') {
- if (!$db_id) {
- return drupal_json_output(array());
- }
- $sql = "
- SELECT dbxref_id, accession
- FROM {dbxref}
- WHERE db_id = :db_id and lower(accession) like lower(:accession)
- ORDER by accession
- LIMIT 25 OFFSET 0
- ";
- $results = chado_query($sql, array(':db_id' => $db_id, ':accession' => $string . '%'));
- $items = array();
- foreach ($results as $ref) {
- $items[$ref->accession] = $ref->accession;
- }
- drupal_json_output($items);
- }
|