123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712 |
- <?php
- function tripal_cv_get_cv($select_values) {
- $columns = array(
- 'cv_id',
- 'name',
- 'definition',
- );
- $results = tripal_core_chado_select('cv', $columns, $select_values);
- if (sizeof($results) == 1) {
- return $results[0];
- }
- elseif (empty($results)) {
- watchdog('tripal_cv',
- 'tripal_cv_get_cv: No cv matches criteria values:%values',
- array('%values' => print_r($select_values, TRUE)),
- WATCHDOG_WARNING
- );
- return FALSE;
- }
- else {
- watchdog('tripal_cv',
- 'tripal_cv_get_cv: 2+ cvs match criteria values:%values',
- array('%values' => print_r($select_values, TRUE)),
- WATCHDOG_WARNING
- );
- }
- }
- function tripal_cv_get_cv_by_name($name) {
- $r = tripal_core_chado_select('cv', array('*'), array('name' => $name));
- return $r[0];
- }
- function tripal_cv_get_cv_by_id($cv_id) {
- $r = tripal_core_chado_select('cv', array('*'), array('cv_id' => $cv_id));
- return $r;
- }
- function tripal_cv_get_cv_id($cv_name) {
- $sql = "
- SELECT cv_id FROM {cv} WHERE name = '%s'
- ";
- $cv = db_fetch_object(chado_query($sql, $cv_name));
- return $cv->cv_id;
- }
- function tripal_cv_get_cv_options() {
- $results = tripal_core_chado_select('cv', array('cv_id', 'name'), array());
- $options = array();
- foreach ($results as $r) {
- $options[$r->cv_id] = $r->name;
- }
- return $options;
- }
- function tripal_cv_get_cvterm_by_name($name, $cv_id = 0, $cv_name = 'tripal') {
- if ($cv_id) {
- $values = array(
- 'name' => $name,
- 'cv_id' => $cv_id,
- );
- $r = tripal_core_chado_select('cvterm', array('*'), $values);
- }
- elseif ($cv_name) {
- $values = array(
- 'name' => $name,
- 'cv_id' => array(
- 'name' => $cv_name,
- ),
- );
- $r = tripal_core_chado_select('cvterm', array('*'), $values);
- }
- else {
- $values = array(
- 'name' => $name,
- );
- $r = tripal_core_chado_select('cvterm', array('*'), $values);
- }
- if (!$r) {
- return FALSE;
- }
- if (count($r) > 0) {
- return FALSE;
- }
- return $r[0];
- }
- function tripal_cv_get_cvterm_options($cv_id = 0) {
- if ($cv_id > 0) {
- $results = tripal_core_chado_select('cvterm', array('cvterm_id', 'name'), array('cv_id' => $cv_id));
- }
- else {
- $results = tripal_core_chado_select('cvterm', array('cvterm_id', 'name'), array());
- }
- $options = array();
- foreach ($results as $r) {
- $options[$r->cvterm_id] = $r->name;
- }
- return $options;
- }
- function tripal_cv_update_cvtermpath($cvid, $job_id = NULL) {
-
-
- $cv = db_fetch_object(chado_query("SELECT * FROM {cv} WHERE cv_id = %d", $cvid));
- print "\nUpdating cvtermpath for $cv->name...\n";
-
-
- $sql = "SELECT * FROM fill_cvtermpath('%s')";
- $success = chado_query($sql, $cv->name);
- return TRUE;
- }
- function tripal_cv_add_cv($name, $definition) {
-
-
- $ins_values = array(
- 'name' => $name,
- 'definition' => $definition
- );
-
-
- $sel_values = array('name' => $name);
- $sel_options = array('statement_name' => 'sel_cv_na');
- $results = tripal_core_chado_select('cv', array('*'), $sel_values, $sel_options);
-
- if (count($results) == 0) {
- $ins_options = array('statement_name' => 'ins_cv_nade');
- $success = tripal_core_chado_insert('cv', $ins_values, $ins_options);
- if (!$success) {
- watchdog('tripal_cv', "Failed to create the CV record", NULL, WATCHDOG_WARNING);
- return FALSE;
- }
- $results = tripal_core_chado_select('cv', array('*'), $sel_values, $sel_options);
- }
-
- else {
- $upd_options = array('statement_name' => 'upd_cv_nade');
- $success = tripal_core_chado_update('cv', $sel_values, $ins_values, $upd_options);
- if (!$success) {
- watchdog('tripal_cv', "Failed to update the CV record", NULL, WATCHDOG_WARNING);
- return FALSE;
- }
- $results = tripal_core_chado_select('cv', array('*'), $sel_values, $sel_options);
- }
-
- return $results[0];
- }
- function tripal_cv_add_cvterm($term, $defaultcv = '_global', $is_relationship = 0,
- $update = 1, $dbname = 'internal') {
-
-
- $id = $term['id'];
- $name = '';
- $cvname = '';
- $definition = '';
- $is_obsolete = 0;
- $accession = '';
-
- if (array_key_exists('name', $term)) {
- $name = $term['name'];
- }
- else {
- $name = $id;
- }
- if (array_key_exists('namespace', $term)) {
- $cvname = $term['namespace'];
- }
- else {
- $cvname = $defaultcv;
- }
- if (array_key_exists('def', $term)) {
- $definition = preg_replace('/^\"(.*)\"/', '\1', $term['def']);
- }
- else {
- $definition = '';
- }
- if (array_key_exists('is_obsolete', $term)) {
- $is_obsolete = $term['is_obsolete'];
- if (strcmp($is_obsolete, 'true') == 0) {
- $is_obsolete = 1;
- }
- }
- if (!$name and !$id) {
- watchdog('tripal_cv', "Cannot find cvterm without 'id' or 'name'", NULL, WATCHDOG_WARNING);
- return 0;
- }
- if (!$id) {
- $id = $name;
- }
-
-
- if ($dbname) {
- $accession = $id;
- }
-
- if (preg_match('/^.+?:.*$/', $id)) {
- $accession = preg_replace('/^.+?:(.*)$/', '\1', $id);
- $dbname = preg_replace('/^(.+?):.*$/', '\1', $id);
- }
-
-
- if ($is_relationship and !$dbname) {
- watchdog('tripal_cv', "A database name is not provided for this relationship term: $id", NULL, WATCHDOG_WARNING);
- return 0;
- }
- if (!$is_relationship and !$dbname) {
- watchdog('tripal_cv', "A database identifier is missing from the term: $id", NULL, WATCHDOG_WARNING);
- return 0;
- }
-
-
- $cv = tripal_cv_add_cv($cvname, '');
- if (!$cv) {
- watchdog('tripal_cv', "Cannot find namespace '$cvname' when adding/updating $id", NULL, WATCHDOG_WARNING);
- return 0;
- }
-
-
-
-
- if (!tripal_core_is_sql_prepared('sel_cvterm_by_accession')) {
- $pcvtermsql = "
- PREPARE sel_cvterm_by_accession(text, text) AS
- SELECT CVT.name, CVT.cvterm_id, CV.cv_id, CV.name as cvname,
- DB.name as dbname, DB.db_id, DBX.accession
- FROM cvterm CVT
- INNER JOIN dbxref DBX on CVT.dbxref_id = DBX.dbxref_id
- INNER JOIN db DB on DBX.db_id = DB.db_id
- INNER JOIN cv CV on CV.cv_id = CVT.cv_id
- WHERE DBX.accession = $1 and DB.name = $2";
- if (!tripal_core_chado_prepare('sel_cvterm_by_accession', $pcvtermsql, array('text', 'text'))) {
- watchdog('tripal_cv', "Cannot prepare statement 'sel_cvterm_by_accession'", NULL, WATCHDOG_WARNING);
- return 0;
- }
- }
- $cvtermsql = "EXECUTE sel_cvterm_by_accession('%s','%s')";
-
-
-
- $db = tripal_db_add_db($dbname);
- if (!$db) {
- watchdog('tripal_cv', "Cannot find database '$dbname' in Chado.", NULL, WATCHDOG_WARNING);
- return 0;
- }
-
-
- $values = array(
- 'name' => $name,
- 'is_obsolete' => $is_obsolete,
- 'cv_id' => array(
- 'name' => $cvname,
- ),
- );
- $options = array('statement_name' => 'sel_cvterm_c1');
- $result = tripal_core_chado_select('cvterm', array('*'), $values, $options);
-
-
-
- if (count($result) == 1) {
- $cvterm = $result[0];
-
-
- $values = array('dbxref_id' => $cvterm->dbxref_id);
- $options = array('statement_name' => 'sel_dbxref_id');
- $result = tripal_core_chado_select('dbxref', array('*'), $values, $options);
- $dbxref = $result[0];
-
-
- $values = array('db_id' => $dbxref->db_id);
- $options = array('statement_name' => 'sel_db_id');
- $result = tripal_core_chado_select('db', array('*'), $values, $options);
- $db_check = $result[0];
-
-
-
-
-
- if ($db_check->name != $db->name) {
-
- $values = array(
- 'db_id' => $db->db_id,
- 'accession' => $accession,
- );
- $options = array('statement_name' => 'sel_dbxref_idac');
- $result = tripal_core_chado_select('dbxref', array('*'), $values, $options);
-
-
- if (count($result) > 0) {
- $dbxref = $result[0];
- $match = array('cvterm_id' => $cvterm->cvterm_id);
- $values = array('dbxref_id' => $dbxref->dbxref_id);
- $options = array('statement_name' => 'upd_cvterm_db');
- $success = tripal_core_chado_update('cvterm', $match, $values, $options);
- if (!$success) {
- watchdog('tripal_cv', "Failed to correct the dbxref id for the cvterm " .
- "'$name' (id: $accession), for database $dbname", NULL, WATCHDOG_WARNING);
- return 0;
- }
- }
-
-
- else {
- $match = array('cvterm_id' => $cvterm->cvterm_id);
- $options = array('statement_name' => 'del_cvterm_cv');
- tripal_core_chado_delete('cvterm', $match, $options);
- }
- }
-
-
-
-
-
- if ($dbxref->accession != $accession) {
-
-
- $dbxref_new = tripal_db_add_dbxref($db->db_id, $accession);
- if (!$dbxref_new) {
- watchdog('tripal_cv', "Failed to find or insert the dbxref record for cvterm, " .
- "$name (id: $accession), for database $dbname", NULL, WATCHDOG_WARNING);
- return 0;
- }
-
-
- $values = array(
- 'cvterm_id' => $cvterm->cvterm_id,
- 'dbxref_id' => $dbxref_new->dbxref_id,
- 'is_for_definition' => 1,
- );
- $options = array('statement_name' => 'sel_cvtermdbxref_cvdbis');
- $result = tripal_core_chado_select('cvterm_dbxref', array('*'), $values, $options);
-
-
- if (count($result)==0) {
- $options = array(
- 'statement_name' => 'ins_cvtermdbxref_cvdbis',
- 'return_record' => FALSE,
- );
- $success = tripal_core_chado_insert('cvterm_dbxref', $values, $options);
- if (!$success) {
- watchdog('tripal_cv', "Failed to find or insert the cvterm_dbxref record for a " .
- "duplicated cvterm: $name (id: $accession), for database $dbname", NULL, WATCHDOG_WARNING);
- return 0;
- }
- }
-
- $cvterm = db_fetch_object(chado_query($cvtermsql, $dbxref->accession, $dbname));
- return $cvterm;
- }
-
-
-
- }
-
-
- $cvterm = db_fetch_object(chado_query($cvtermsql, $accession, $dbname));
-
-
-
- if (!$cvterm) {
-
- $dbxref = tripal_db_add_dbxref($db->db_id, $accession);
- if (!$dbxref) {
- watchdog('tripal_cv', "Failed to find or insert the dbxref record for cvterm, " .
- "$name (id: $accession), for database $dbname", NULL, WATCHDOG_WARNING);
- return 0;
- }
-
-
-
- $values = array('dbxref_id' => $dbxref->dbxref_id);
- $options = array('statement_name' => 'sel_cvterm_db');
- $check = tripal_core_chado_select('cvterm', array('cvterm_id'), $values, $options);
- if (count($check) == 0) {
-
- $ins_values = array(
- 'cv_id' => $cv->cv_id,
- 'name' => $name,
- 'definition' => $definition,
- 'dbxref_id' => $dbxref->dbxref_id,
- 'is_obsolete' => $is_obsolete,
- 'is_relationshiptype' => $is_relationship,
- );
- $ins_options = array('statement_name' => 'ins_cvterm_all');
- $success = tripal_core_chado_insert('cvterm', $ins_values, $ins_options);
- if (!$success) {
- if (!$is_relationship) {
- watchdog('tripal_cv', "Failed to insert the term: $name ($dbname)", NULL, WATCHDOG_WARNING);
- return 0;
- }
- else {
- watchdog('tripal_cv', "Failed to insert the relationship term: $name (cv: " . $cvname . " db: $dbname)", NULL, WATCHDOG_WARNING);
- return 0;
- }
- }
- }
-
- else {
- watchdog('tripal_cv', "The dbxref already exists for another cvterm record: $name (cv: " . $cvname . " db: $dbname)", NULL, WATCHDOG_WARNING);
- return 0;
- }
- $cvterm = db_fetch_object(chado_query($cvtermsql, $accession, $dbname));
- }
-
- elseif ($update) {
- $match = array('cvterm_id' => $cvterm->cvterm_id);
- $upd_values = array(
- 'name' => $name,
- 'definition' => $definition,
- 'is_obsolete' => $is_obsolete,
- 'is_relationshiptype' => $is_relationship,
- );
- $upd_options = array('statement_name' => 'upd_cvterm_nadeisis');
- $success = tripal_core_chado_update('cvterm', $match, $upd_values, $upd_options);
- if (!$success) {
- watchdog('tripal_cv', "Failed to update the term: $name", NULL, WATCHDOG_WARNING);
- return 0;
- }
- $cvterm = db_fetch_object(chado_query($cvtermsql, $accession, $dbname));
- }
- else {
-
- }
-
- return $cvterm;
- }
- function tripal_cv_get_custom_tables($table = NULL) {
- if (!$table or strcmp($table, 'tripal_obo_temp')==0) {
- $schema['tripal_obo_temp'] = array(
- 'table' => 'tripal_obo_temp',
- 'fields' => array(
- 'id' => array(
- 'type' => 'varchar',
- 'length' => '255',
- 'not null' => TRUE,
- ),
- 'stanza' => array(
- 'type' => 'text',
- 'not null' => TRUE,
- ),
- 'type' => array(
- 'type' => 'varchar',
- 'length' => '50',
- 'not null' => TRUE,
- ),
- ),
- 'indexes' => array(
- 'tripal_obo_temp_idx0' => array('id'),
- 'tripal_obo_temp_idx0' => array('type'),
- ),
- 'unique keys' => array(
- 'tripal_obo_temp_uq0' => array('id'),
- ),
- );
- }
- return $schema;
- }
|