1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630 |
- <?php
- function chado_get_cv($identifiers, $options = array()) {
-
- if (!isset($options['include_fk'])) {
-
- $options['include_fk'] = array();
- }
-
- if (!is_array($identifiers)) {
- tripal_report_error(
- 'tripal_chado_api',
- TRIPAL_ERROR,
- "chado_get_cv: The identifier passed in is expected to be an array with the key
- matching a column name in the cv table (ie: cv_id or name). You passed in %identifier.",
- array(
- '%identifier'=> print_r($identifiers, TRUE)
- )
- );
- }
- elseif (empty($identifiers)) {
- tripal_report_error(
- 'tripal_chado_api',
- TRIPAL_ERROR,
- "chado_get_cv: You did not pass in anything to identify the cv you want. The identifier
- is expected to be an array with the key matching a column name in the cv table
- (ie: cv_id or name). You passed in %identifier.",
- array(
- '%identifier'=> print_r($identifiers, TRUE)
- )
- );
- }
-
- $cv = chado_generate_var(
- 'cv',
- $identifiers,
- $options
- );
-
- if (is_array($cv)) {
- tripal_report_error(
- 'tripal_chado_api',
- TRIPAL_ERROR,
- "chado_get_cv: The identifiers you passed in were not unique. You passed in %identifier.",
- array(
- '%identifier'=> print_r($identifiers, TRUE)
- )
- );
- }
-
- elseif ($cv === FALSE) {
- tripal_report_error(
- 'tripal_chado_api',
- TRIPAL_ERROR,
- "chado_get_cv: chado_generate_var() failed to return a cv 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 $cv;
- }
- }
- function chado_get_cv_select_options() {
- $results = chado_select_record('cv', array('cv_id', 'name'), array(), array('order_by' => array('name' => 'ASC')));
- $options = array();
- $options[] = 'Select a Vocabulary';
- foreach ($results as $r) {
- $options[$r->cv_id] = $r->name;
- }
- return $options;
- }
- function chado_get_cvterm($identifiers, $options = array()) {
-
- if (!isset($options['include_fk'])) {
-
- $options['include_fk'] = array('cv_id' => TRUE);
- }
-
- if (!is_array($identifiers)) {
- tripal_report_error('tripal_cv_api', TRIPAL_ERROR,
- "chado_get_cvterm: The identifier passed in is expected to be an array with the key
- matching a column name in the cvterm table (ie: cvterm_id or name). You passed in %identifier.",
- array('%identifier'=> print_r($identifiers, TRUE))
- );
- }
- elseif (empty($identifiers)) {
- tripal_report_error('tripal_cv_api', TRIPAL_ERROR,
- "chado_get_cvterm: You did not pass in anything to identify the cvterm you want. The identifier
- is expected to be an array with the key matching a column name in the cvterm table
- (ie: cvterm_id or name). You passed in %identifier.",
- array('%identifier'=> print_r($identifiers, TRUE))
- );
- }
-
-
- if (isset($identifiers['synonym'])) {
- $synonym = $identifiers['synonym']['name'];
- $values = array('synonym' => $synonym);
- if (isset($identifiers['synonym']['cv_id'])) {
- $values['cvterm_id'] = array('cv_id' => $identifiers['synonym']['cv_id']);
- }
- if (isset($identifiers['synonym']['cv_name'])) {
- $values['cvterm_id'] = array('cv_id' => array('name' => $identifiers['synonym']['cv_name']));
- }
- $options = array(
- 'case_insensitive_columns' => array('name')
- );
- $result = chado_select_record('cvtermsynonym', array('cvterm_id'), $values, $options);
-
-
- if (count($result) == 0) {
- return FALSE;
- }
- if (count($result) > 1) {
- return FALSE;
- }
- $identifiers = array('cvterm_id' => $result[0]->cvterm_id);
- }
-
- if (isset($identifiers['property'])) {
- $property = $identifiers['property'];
- unset($identifiers['property']);
- $cvterm = chado_get_record_with_property(
- array('table' => 'cvterm', 'base_records' => $identifiers),
- array('type_name' => $property),
- $options
- );
- }
- if (isset($identifiers['id'])) {
- list($db_name, $accession) = preg_split('/:/', $identifiers['id']);
- $cvterm = chado_generate_var('cvterm',array(
- 'dbxref_id' => array(
- 'db_id' => array(
- 'name' => $db_name,
- ),
- 'accession' => $accession,
- )
- ));
- }
-
-
- else {
-
- $cvterm = chado_generate_var('cvterm', $identifiers, $options);
- }
-
- if (is_array($cvterm)) {
- tripal_report_error(
- 'tripal_cv_api',
- TRIPAL_ERROR,
- "chado_get_cvterm: The identifiers you passed in were not unique. You passed in %identifier.",
- array(
- '%identifier'=> print_r($identifiers, TRUE)
- )
- );
- }
-
-
- elseif ($cvterm === FALSE) {
- tripal_report_error(
- 'tripal_cv_api',
- TRIPAL_ERROR,
- "chado_get_cvterm: chado_generate_var() failed to return a cvterm 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 $cvterm;
- }
- }
- function chado_get_cvterm_select_options($cv_id, $rel_type = FALSE) {
- $columns = array('cvterm_id', 'name');
- $values = array('cv_id' => $cv_id);
- if ($rel_type) {
- $values['is_relationshiptype'] = 1;
- }
- $s_options = array('order_by' => array('name' => 'ASC'));
- $cvterms = chado_select_record('cvterm', $columns, $values, $s_options);
- $options = array();
- $options[0] = 'Select a Type';
- foreach ($cvterms as $cvterm) {
- $options[$cvterm->cvterm_id] = $cvterm->name;
- }
- return $options;
- }
- function tripal_update_cvtermpath_old($cv_id, $job_id = NULL) {
-
-
- $sql = "SELECT * FROM {cv} WHERE cv_id = :cv_id";
- $cv = chado_query($sql, array(':cv_id' => $cv_id))->fetchObject();
- print "\nUpdating cvtermpath for $cv->name...\n";
-
-
- $previous = chado_set_active('chado');
- try {
- $sql = "SELECT * FROM fill_cvtermpath(:name)";
- db_query($sql, array(':name' => $cv->name));
- chado_set_active($previous);
- }
- catch (Exception $e) {
- chado_set_active($previous);
- $error = $e->getMessage();
- tripal_report_error('tripal_chado', TRIPAL_ERROR, "Could not fill cvtermpath table: @error", array('@error' => $error));
- return FALSE;
- }
- return TRUE;
- }
- function chado_update_cvtermpath($cv_id, $job_id = NULL){
-
-
- $prev_db = chado_set_active('chado');
- print "\nNOTE: Updating CV Term Path is performed using a database transaction. \n" .
- "If the update fails or is terminated prematurely then any new changes \n" .
- "made will be rolled back.\n\n";
- $transaction = db_transaction();
- try {
- $result = db_query('
- SELECT DISTINCT t.*
- FROM cvterm t
- LEFT JOIN cvterm_relationship r ON (t.cvterm_id = r.subject_id)
- INNER JOIN cvterm_relationship r2 ON (t.cvterm_id = r2.object_id)
- WHERE t.cv_id = :cvid AND r.subject_id is null',
- array(':cvid' => $cv_id)
- );
-
- $record = $result->fetchAll();
- $roots = [];
- foreach ($record as $item){
- _chado_update_cvtermpath_root_loop($item->cvterm_id, $item->cv_id, $roots);
- }
- }
- catch (Exception $e) {
-
- $transaction->rollback();
- throw $e;
- } finally {
-
- chado_set_active($prev_db);
- }
- }
- function _chado_update_cvtermpath_root_loop($rootid, $cvid, &$roots) {
- $ttype = db_query(
- 'SELECT cv.cvterm_id
- FROM cvterm cv
- WHERE cv.name = :isa
- OR cv.name = :is_a
- '
- ,
- array(':isa' => "isa", ':is_a' => "is_a")
- );
- $result = $ttype->fetchObject();
- $term_id = $rootid . '|' . $rootid . '|' . $cvid . '|' . $result->cvterm_id;
-
- foreach ($roots as $element_id) {
- if ($element_id == $term_id) {
- return;
- }
- }
-
- $roots[] = $term_id;
-
- $tree_path = [];
- $matched_rows = [];
- $possible_start_of_loop = [];
- $depth = 0;
- _chado_update_cvtermpath_loop($rootid, $rootid, $cvid, $result->cvterm_id, $depth,
- 0, $tree_path, FALSE, $matched_rows, $possible_start_of_loop, FALSE);
-
-
- $cterm = db_query(
- 'SELECT *
- FROM cvterm_relationship
- WHERE object_id = :rootid
- ',
- [':rootid' => $rootid]
- );
- while ($cterm_result = $cterm->fetchAssoc()) {
- _chado_update_cvtermpath_root_loop($cterm_result['subject_id'], $cvid, $roots);
- }
- }
- function _chado_update_cvtermpath_loop(
- $origin,
- $child_id,
- $cv_id,
- $type_id,
- $depth,
- $increment_of_depth,
- $tree_path,
- $possible_loop,
- $matched_rows,
- $possible_start_of_loop,
- $no_loop_skip_test) {
-
- $new_match_rows = [];
- if (!empty($possible_start_of_loop)) {
-
- if (count($matched_rows) === 1) {
-
- $cvtermpath_id = (int) $matched_rows[0]->cvtermpath_id;
- $cvtermpath_id = $cvtermpath_id + 1;
- chado_set_active('chado');
- $next_row = db_query(
- 'SELECT *
- FROM cvtermpath
- WHERE cvtermpath_id = :cvtermpath_id
- ',
- [':cvtermpath_id' => $cvtermpath_id]
- );
- $next_row = $next_row->fetchObject();
-
- if (($next_row->type_id === $type_id) &&
- ($next_row->subject_id === $child_id) &&
- ($next_row->object_id === $origin) &&
- ($next_row->cv_id === $cv_id)) {
- $new_match_rows[] = $next_row;
- }
- elseif (($next_row->type_id === $possible_start_of_loop['type_id']) &&
- ($next_row->subject_id === $possible_start_of_loop['subject_id']) &&
- ($next_row->object_id === $possible_start_of_loop['object_id']) &&
- ($next_row->cv_id === $possible_start_of_loop['cv_id'])) {
-
-
- $possible_loop == FALSE;
- $matched_rows = [];
- _chado_update_cvtermpath_loop_increment($origin, $child_id, $cv_id,
- $type_id, $depth + 1, $increment_of_depth, $tree_path, $possible_loop,
- $new_match_rows, $possible_start_of_loop, $no_loop_skip_test);
- }
- }
- else {
- foreach ($matched_rows as $matched_row) {
-
- $cvtermpath_id = (int) $match_row->cvtermpath_id;
-
- $cvtermpath_id = $cvtermpath_id + 1;
- chado_set_active('chado');
- $next_row = db_query(
- 'SELECT *
- FROM cvtermpath
- WHERE cvtermpath_id = :cvtermpath_id
- ',
- [':cvtermpath_id' => $cvtermpath_id]
- );
- $next_row = $next_row->fetchObject();
-
- if (($next_row->type_id === $type_id) &&
- ($next_row->subject_id === $child_id) &&
- ($next_row->object_id === $origin) &&
- ($next_row->cv_id === $cv_id)) {
- $new_match_rows[] = $next_row;
- }
- elseif (($next_row->type_id === $possible_start_of_loop['type_id']) &&
- ($next_row->subject_id === $possible_start_of_loop['subject_id']) &&
- ($next_row->object_id === $possible_start_of_loop['object_id']) &&
- ($next_row->cv_id === $possible_start_of_loop['cv_id'])) {
-
-
- $possible_loop == FALSE;
- $matched_rows = [];
- _chado_update_cvtermpath_loop_increment($origin, $child_id, $cv_id,
- $type_id, $depth + 1, $increment_of_depth, $tree_path, $possible_loop,
- $new_match_rows, $possible_start_of_loop, $no_loop_skip_test);
- }
- }
- }
-
- if (empty($new_match_rows)) {
- $possible_loop == FALSE;
- $matched_rows = [];
- unset($new_match_rows);
- $no_loop_skip_test = TRUE;
-
-
- _chado_update_cvtermpath_loop_increment($possible_start_of_loop->subject_id,
- $possible_start_of_loop->child_id, $possible_start_of_loop->cvid,
- $possible_start_of_loop->type_id, $possible_start_of_loop->depth,
- $increment_of_depth, $tree_path, $possible_loop, $matched_rows,
- $possible_start_of_loop, $no_loop_skip_test);
- }
-
- else {
- _chado_update_cvtermpath_loop_increment($origin, $child_id, $cv_id,
- $type_id, $depth + 1, $increment_of_depth, $tree_path, $possible_loop,
- $match_rows, $possible_start_of_loop, $no_loop_skip_test);
- }
- }
- elseif ($possible_loop === FALSE) {
- _chado_update_cvtermpath_loop_increment($origin, $child_id, $cv_id,
- $type_id, $depth + 1, $increment_of_depth, $tree_path, $possible_loop,
- $matched_rows, $possible_start_of_loop, $no_loop_skip_test);
- }
- }
- function _chado_update_cvtermpath_loop_increment(
- $origin,
- $child_id,
- $cv_id,
- $type_id,
- $depth,
- $increment_of_depth,
- $tree_path,
- $possible_loop,
- $matched_rows,
- &$possible_start_of_loop,
- $no_loop_skip_test) {
-
- if ($possible_loop === FALSE && empty($possible_start_of_loop)) {
- chado_set_active('chado');
- $count = db_query(
- ' SELECT *
- FROM cvtermpath
- WHERE object_id = :origin
- AND subject_id = :child_id
- AND pathdistance = :depth
- AND type_id = :type_id
- ',
- [
- ':origin' => $origin,
- ':child_id' => $child_id,
- ':depth' => $depth,
- ':type_id' => $type_id
- ]
- );
- $count->fetchAll();
- $count_total = $count->rowCount();
- if ($count_total > 0) {
- return $count;
- }
-
- $term_id = $origin . '|' . $child_id . '|' . $cv_id . '|' . $type_id;
- if ($no_loop_skip_test === FALSE) {
-
-
- if ($increment_of_depth != 0) {
-
- foreach ($tree_path as $parent) {
-
-
- if ($parent['build_id'] == $term_id) {
-
-
- $possible_loop = TRUE;
-
-
- $matching_rows = db_query(
- ' SELECT *
- FROM cvtermpath
- WHERE cv_id = :cvid
- AND object_id = :origin
- AND subject_id = :child_id
- AND type_id = :type_id
- ',
- [
- ':cvid' => $cv_id,
- ':origin' => $origin,
- ':child_id' => $child_id,
- ':type_id' => $type_id
- ]
- );
- $matched_rows = $matching_rows->fetchAll();
- $possible_start_of_loop = array(
- 'cvid' => $cv_id,
- 'subject_id' => $origin,
- 'child_id' => $child_id,
- 'type_id' => $type_id,
- 'depth' => $depth,
- );
- }
- }
- }
- $find_query = db_select('chado.cvtermpath', 'CVTP');
- $find_query->fields('CVTP', ['subject_id']);
- $find_query->condition('object_id', $origin);
- $find_query->condition('subject_id', $child_id);
- $find_query->condition('type_id', $type_id);
- $find_query->condition('pathdistance', $depth);
- $exists = $find_query->execute()->fetchObject();
- if(!$exists){
- $query = db_insert('cvtermpath')
- ->fields([
- 'object_id' => $origin,
- 'subject_id' => $child_id,
- 'cv_id' => $cv_id,
- 'type_id' => $type_id,
- 'pathdistance' => $depth,
- ]);
- try {
- $rows = $query->execute();
- } catch (Exception $e) {
- $error = $e->getMessage();
- tripal_report_error('tripal_chado', TRIPAL_ERROR, "Could not fill cvtermpath term: @error", array('@error' => $error));
- return false;
- }
- }
-
- $tree_path[$increment_of_depth] = [
- 'build_id' => $term_id,
- 'depth' => $depth
- ];
- }
-
- $no_loop_skip_test == FALSE;
- $possible_start_of_loop = [];
-
-
-
- $query = db_select('cvterm_relationship', 'cvtr')
- ->fields('cvtr')
- ->condition('cvtr.object_id', $child_id, '=')
- ->execute();
- $cterm_relationships = $query->fetchAll();
- foreach ($cterm_relationships as $item) {
- $increment_of_depth++;
- _chado_update_cvtermpath_loop_increment($origin, $item->subject_id, $cv_id,
- $item->type_id, $depth + 1, $increment_of_depth, $tree_path, $possible_loop,
- $matched_rows, $possible_start_of_loop, $no_loop_skip_test);
- }
- }
- elseif ($possible_loop === FALSE && !empty($possible_start_of_loop)) {
-
-
- return $possible_start_of_loop;
- }
- elseif ($possible_loop === TRUE) {
-
-
-
- $query = db_select('cvterm_relationship', 'cvtr')
- ->fields('cvtr')
- ->condition('cvtr.object_id', $child_id, '=')
- ->execute();
- $cterm_relationships = $query->fetchAll();
- foreach ($cterm_relationships as $item) {
- $increment_of_depth++;
- _chado_update_cvtermpath_loop($origin, $item->subject_id, $cv_id,
- $item->type_id, $depth + 1, $increment_of_depth, $tree_path, $possible_loop,
- $matched_rows, $possible_start_of_loop, $no_loop_skip_test);
- }
- }
- }
- function chado_insert_cv($name, $definition) {
-
- $ins_values = array(
- 'name' => $name,
- 'definition' => $definition
- );
-
- $sel_values = array('name' => $name);
- $results = chado_select_record('cv', array('*'), $sel_values);
-
- if (count($results) == 0) {
- $success = chado_insert_record('cv', $ins_values);
- if (!$success) {
- tripal_report_error('tripal_chado', TRIPAL_WARNING, "Failed to create the CV record", NULL);
- return FALSE;
- }
- $results = chado_select_record('cv', array('*'), $sel_values);
- }
-
- else {
- $success = chado_update_record('cv', $sel_values, $ins_values);
- if (!$success) {
- tripal_report_error('tripal_chado', TRIPAL_WARNING, "Failed to update the CV record", NULL);
- return FALSE;
- }
- $results = chado_select_record('cv', array('*'), $sel_values);
- }
-
- return $results[0];
- }
- function chado_insert_cvterm($term, $options = array()) {
-
- $id = (isset($term['id'])) ? $term['id'] : '';
- $name = '';
- $cvname = '';
- $definition = '';
- $is_obsolete = 0;
- $accession = '';
-
- if (isset($term['cv_name'])) {
- $cvname = $term['cv_name'];
- }
- else {
- $cvname = 'local';
- }
-
-
- if (array_key_exists('namespace', $term)) {
- $cvname = $term['namespace'];
- }
- if (isset($term['is_relationship'])) {
- $is_relationship = $term['is_relationship'];
- }
- else {
- $is_relationship = 0;
- }
- if (isset($term['db_name'])) {
- $dbname = $term['db_name'];
- }
- else {
- $dbname = 'internal';
- }
- if (isset($options['update_existing'])) {
- $update = $options['update_existing'];
- }
- else {
- $update = 1;
- }
- if (array_key_exists('name', $term)) {
- $name = $term['name'];
- }
- else {
- $name = $id;
- }
- if (array_key_exists('definition', $term)) {
- $definition = preg_replace('/^\"(.*)\"/', '\1', $term['definition']);
- }
- 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) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Cannot find cvterm without 'id' or 'name'", NULL);
- 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) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "A database name is not provided for this relationship term: $id", NULL);
- return 0;
- }
- if (!$is_relationship and !$dbname) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "A database identifier is missing from the term: $id", NULL);
- return 0;
- }
-
- $cv = chado_get_cv(array('name' => $cvname));
- if (!$cv) {
- $cv = chado_insert_cv($cvname, '');
- }
- if (!$cv) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Cannot find namespace '$cvname' when adding/updating $id", NULL);
- return 0;
- }
-
-
-
-
- $cvtermsql = "
- 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 = :accession and DB.name = :name
- ";
-
-
- $db = chado_get_db(array('name' => $dbname));
- if (!$db) {
- $db = chado_insert_db(array('name' => $dbname));
- }
- if (!$db) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Cannot find database '$dbname' in Chado.", NULL);
- return 0;
- }
-
-
- $values = array(
- 'name' => $name,
- 'is_obsolete' => $is_obsolete,
- 'cv_id' => array(
- 'name' => $cvname,
- ),
- );
- $result = chado_select_record('cvterm', array('*'), $values);
- if (count($result) == 1) {
- $cvterm = $result[0];
-
- $values = array('dbxref_id' => $cvterm->dbxref_id);
- $result = chado_select_record('dbxref', array('*'), $values);
- $dbxref = $result[0];
- if (!$dbxref) {
- tripal_report_error('tripal_cv', TRIPAL_ERROR,
- 'Unable to access the dbxref record for the :term cvterm. Term Record: !record',
- array(':term' => $name, '!record' => print_r($cvterm, TRUE))
- );
- return FALSE;
- }
-
- $values = array('db_id' => $dbxref->db_id);
- $result = chado_select_record('db', array('*'), $values);
- $db_check = $result[0];
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if ($dbxref->accession != $accession) {
-
- $dbxref_new = chado_insert_dbxref(array(
- 'db_id' => $db->db_id,
- 'accession' => $accession
- ));
- if (!$dbxref_new) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to find or insert the dbxref record for cvterm, " .
- "$name (id: $accession), for database $dbname", NULL);
- return 0;
- }
-
- $values = array(
- 'cvterm_id' => $cvterm->cvterm_id,
- 'dbxref_id' => $dbxref_new->dbxref_id,
- 'is_for_definition' => 1,
- );
- $result = chado_select_record('cvterm_dbxref', array('*'), $values);
-
- if (count($result)==0) {
- $options = array(
- 'return_record' => FALSE,
- );
- $success = chado_insert_record('cvterm_dbxref', $values, $options);
- if (!$success) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to find or insert the cvterm_dbxref record for a " .
- "duplicated cvterm: $name (id: $accession), for database $dbname", NULL);
- return 0;
- }
- }
-
- $result = chado_query($cvtermsql, array(':accession' => $dbxref->accession, ':name' => $dbname));
- $cvterm = $result->fetchObject();
- return $cvterm;
- }
-
-
- }
-
- $result = chado_query($cvtermsql, array(':accession' => $accession, ':name' => $dbname));
- $cvterm = $result->fetchObject();
- if (!$cvterm) {
-
- $dbxref = chado_insert_dbxref(array(
- 'db_id' => $db->db_id,
- 'accession' => $accession
- ));
- if (!$dbxref) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to find or insert the dbxref record for cvterm, " .
- "$name (id: $accession), for database $dbname", NULL);
- return 0;
- }
-
- $values = array('dbxref_id' => $dbxref->dbxref_id);
- $check = chado_select_record('cvterm', array('cvterm_id'), $values);
- 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,
- );
- $success = chado_insert_record('cvterm', $ins_values);
- if (!$success) {
- if (!$is_relationship) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to insert the term: $name ($dbname)", NULL);
- return 0;
- }
- else {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to insert the relationship term: $name (cv: " . $cvname . " db: $dbname)", NULL);
- return 0;
- }
- }
- }
-
- else {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "The dbxref already exists for another cvterm record: $name (cv: " . $cvname . " db: $dbname)", NULL);
- return 0;
- }
- $result = chado_query($cvtermsql, array(':accession' => $accession, ':name' => $dbname));
- $cvterm = $result->fetchObject();
- }
-
- elseif ($update) {
-
- $match = array('cvterm_id' => $cvterm->cvterm_id);
- $upd_values = array(
- 'name' => $name,
- 'definition' => $definition,
- 'is_obsolete' => $is_obsolete,
- 'is_relationshiptype' => $is_relationship,
- );
- $success = chado_update_record('cvterm', $match, $upd_values);
- if (!$success) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to update the term: $name", NULL);
- return 0;
- }
-
-
- $checksql = "
- SELECT cvterm_id
- 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 = :accession and DB.name = :dbname and CVT.name = :term and CV.name = :cvname
- ";
- $check = chado_query($checksql, array(':accession' => $accession, ':dbname' => $dbname, ':term' => $name, ':cvname' => $cvname))->fetchObject();
- if (!$check) {
-
- $dbxref = chado_insert_dbxref(array(
- 'db_id' => $db->db_id,
- 'accession' => $accession
- ));
- if (!$dbxref) {
- tripal_report_error('tripal_chado', TRIPAL_WARNING, "Failed to find or insert the dbxref record for cvterm, " .
- "$name (id: $accession), for database $dbname", NULL);
- return 0;
- }
- $match = array('cvterm_id' => $cvterm->cvterm_id);
- $upd_values = array(
- 'dbxref_id' => $dbxref->dbxref_id,
- );
- $success = chado_update_record('cvterm', $match, $upd_values);
- if (!$success) {
- tripal_report_error('tripal_chado', TRIPAL_WARNING, "Failed to update the term $name with new accession $db:$accession", NULL);
- return 0;
- }
- }
-
- $result = chado_query($cvtermsql, array(':accession' => $accession, ':name' => $dbname));
- $cvterm = $result->fetchObject();
- }
- else {
-
- }
-
- return $cvterm;
- }
- function chado_submit_obo_job($obo) {
- global $user;
-
- $obo['obo_id'] = (isset($obo['obo_id'])) ? $obo['obo_id'] : NULL;
- $obo['name'] = (isset($obo['name'])) ? $obo['name'] : NULL;
- $obo['url'] = (isset($obo['url'])) ? $obo['url'] : NULL;
- $obo['file'] = (isset($obo['file'])) ? $obo['file'] : NULL;
- $includes = array(
- drupal_get_path('module', 'tripal_chado') . '/includes/tripal_chado.cv.inc',
- );
- if ($obo['obo_id']) {
- $sql = "SELECT * FROM {tripal_cv_obo} WHERE obo_id = :obo_id";
- $result = db_query($sql, array(':obo_id' => $obo['obo_id']))->fetchObject();
- $args = array($result->obo_id);
- return tripal_add_job("Load OBO " . $result->name, 'tripal_chado',
- "tripal_cv_load_obo", $args, $user->uid, 10, $includes);
- }
- else {
- if ($obo['url']) {
- $sql = "SELECT * FROM {tripal_cv_obo} WHERE path = :url";
- $result = db_query($sql, array(':url' => $obo['url']))->fetchObject();
- $args = array($result->obo_id);
- return tripal_add_job("Load OBO " . $result->name, 'tripal_chado',
- "tripal_cv_load_obo", $args, $user->uid, 10, $includes);
- }
- elseif ($obo['file']) {
- $sql = "SELECT * FROM {tripal_cv_obo} WHERE path = :file";
- $result = db_query($sql, array(':url' => $obo['file']))->fetchObject();
- $args = array($result->obo_id);
- return tripal_add_job("Load OBO " . $result->name, 'tripal_chado',
- "tripal_cv_load_obo", $args, $user->uid, 10, $includes);
- }
- }
- return FALSE;
- }
- function chado_insert_obo($name, $path) {
-
- $obo_id = db_select('tripal_cv_obo', 'tco')
- ->fields('tco', array('obo_id'))
- ->condition('name', $name)
- ->execute()
- ->fetchField();
- if ($obo_id) {
- db_update('tripal_cv_obo')
- ->fields(array(
- 'path' => $path,
- ))
- ->condition('name', $name)
- ->execute();
- return $obo_id;
- }
- else {
- $obo_id = db_insert('tripal_cv_obo')
- ->fields(array(
- 'name' => $name,
- 'path' => $path,
- ))
- ->execute();
- return $obo_id;
- }
- }
- function chado_get_obo($values) {
- $query = db_select('tripal_cv_obo', 'tco')
- ->fields('tco');
- if (array_key_exists('name', $values)) {
- $query->condition('tco.name', $values['name']);
- }
- if (array_key_exists('obo_id', $values)) {
- $query->condition('tco.obo_id', $values['obo_id']);
- }
- return $query->execute()->fetchObject();
- }
- function chado_autocomplete_cv($string = '') {
- $sql = "
- SELECT CV.cv_id, CV.name
- FROM {cv} CV
- WHERE lower(CV.name) like lower(:name)
- ORDER by CV.name
- LIMIT 25 OFFSET 0
- ";
- $results = chado_query($sql, array(':name' => $string . '%'));
- $items = array();
- foreach ($results as $cv) {
- $items[$cv->name] = $cv->name;
- }
- drupal_json_output($items);
- }
- function chado_autocomplete_cvterm($cv_id, $string = '') {
- if ($cv_id) {
- $sql = "
- SELECT CVT.cvterm_id, CVT.name
- FROM {cvterm} CVT
- WHERE CVT.cv_id = :cv_id and lower(CVT.name) like lower(:name)
- UNION
- SELECT CVT2.cvterm_id, CVTS.synonym as name
- FROM {cvterm} CVT2
- INNER JOIN {cvtermsynonym} CVTS ON CVTS.cvterm_id = CVT2.cvterm_id
- WHERE CVT2.cv_id = :cv_id and lower(CVTS.synonym) like lower(:name)
- ORDER by name
- LIMIT 25 OFFSET 0
- ";
- $results = chado_query($sql, array(':cv_id' => $cv_id, ':name' => $string . '%'));
- $items = array();
- foreach ($results as $term) {
- $items[$term->name] = $term->name;
- }
- }
-
-
- else {
- $sql = "
- SELECT CVT.cvterm_id, CVT.name, CV.name as cvname, CVT.cv_id
- FROM {cvterm} CVT
- INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
- WHERE lower(CVT.name) like lower(:name)
- UNION
- SELECT CVT2.cvterm_id, CVTS.synonym as name, CV2.name as cvname, CVT2.cv_id
- FROM {cvterm} CVT2
- INNER JOIN {cv} CV2 on CVT2.cv_id = CV2.cv_id
- INNER JOIN {cvtermsynonym} CVTS ON CVTS.cvterm_id = CVT2.cvterm_id
- WHERE lower(CVTS.synonym) like lower(:name)
- ORDER by name
- LIMIT 25 OFFSET 0
- ";
- $results = chado_query($sql, array(':name' => $string . '%'));
- $items = array();
- foreach ($results as $term) {
- $items[$term->name] = $term->name;
- }
- }
- drupal_json_output($items);
- }
- function chado_associate_cvterm($basetable, $record_id, $cvterm, $options = array()) {
- $linking_table = $basetable . '_cvterm';
- $foreignkey_name = $basetable . '_id';
-
- $options['insert_cvterm'] = (isset($options['insert_cvterm'])) ? $options['insert_cvterm'] : FALSE;
-
-
- if (!isset($cvterm['cvterm_id'])) {
- $values = array(
- 'name' => $cvterm['name'],
- );
- if (isset($cvterm['cv_id'])) {
- $values['cv_id'] = $cvterm['cv_id'];
- }
- elseif (isset($cvterm['cv_name'])) {
- $values['cv_id'] = array(
- 'name' => $cvterm['cv_name']
- );
- }
- else {
- tripal_report_error('tripal_chado_api', TRIPAL_WARNING,
- "chado_associate_cvterm: The cvterm needs to have either the cv_name or cv_id
- supplied. You were trying to associate a cvterm with the %base %record_id
- and supplied the cvterm values: %cvterm.",
- array('%base' => $basetable, '%record_id' => $record_id, '%cvterm' => print_r($cvterm,TRUE))
- );
- return FALSE;
- }
-
-
- $select = chado_select_record('cvterm', array('*'), $values);
- if ($select) {
- $cvterm['cvterm_id'] = $select[0]->cvterm_id;
- }
- elseif ($options['insert_cvterm']) {
-
- $insert = chado_insert_cvterm($values);
- if (isset($insert->cvterm_id)) {
- $cvterm['cvterm_id'] = $insert->cvterm_id;
- }
- else {
- tripal_report_error('tripal_chado_api', TRIPAL_WARNING,
- "chado_associate_cvterm: Unable to insert the cvterm using the cvterm values: %cvterm.",
- array('%cvterm' => print_r($cvterm,TRUE))
- );
- return FALSE;
- }
- }
- else {
- tripal_report_error('tripal_api', TRIPAL_WARNING,
- "chado_associate_cvterm: The cvterm doesn't already exist. You supplied the cvterm values: %cvterm.",
- array('%cvterm' => print_r($cvterm,TRUE))
- );
- return FALSE;
- }
- }
-
- if ($cvterm['cvterm_id'] > 0) {
- $values = array(
- 'cvterm_id' => $cvterm['cvterm_id'],
- $foreignkey_name => $record_id,
- 'pub_id' => 1,
- );
-
- $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 %term",
- array('%base' => $linking_table, '%term' => $cvterm['name'])
- );
- return FALSE;
- }
- $result = chado_select_record($linking_table, array('*'), $values);
- }
- if (isset($result[0])) {
- return $result[0];
- }
- else {
- return FALSE;
- }
- }
- return FALSE;
- }
|