123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566 |
- <?php
- function tripal_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,
- "tripal_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,
- "tripal_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,
- "tripal_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,
- "tripal_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 tripal_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 tripal_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,
- "tripal_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,
- "tripal_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,
- "tripal_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,
- "tripal_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 tripal_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($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 tripal_update_cvtermpath_bak($cv_id, $job_id = NULL){
- chado_set_active('chado');
- $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->fetchObject();
- tripal_update_cvtermpath_root_loop($record->cvterm_id, $record->cv_id);
- }
- function tripal_update_cvtermpath_root_loop($rootid, $cvid){
- chado_set_active('chado');
- $ttype = db_select('cvterm', 'cv')
- ->fields('cv', array('cvterm_id'));
- $db_or = db_or();
- $db_or->condition('cv.name', "isa", '=' );
- $db_or->condition('cv.name', "is_a", '=' );
- $ttype->condition($db_or);
- $result = $ttype->execute()->fetchObject();
- $result = tripal_update_cvtermpath_loop($rootid, $rootid, $cvid, $result->cvterm_id, 0, 0, array());
- $cterm = db_query(
- 'SELECT *
- FROM cvterm_relationship
- WHERE object_id = :rootid
- ',
- array(':rootid' => $rootid)
- );
- while($cterm_result = $cterm->fetchAssoc()) {
- if ($result === 'LOOP'){
- watchdog('debug', '<pre>$result: ' . print_r($result, TRUE) . '</pre>');
- continue;
- }
- else {
- tripal_update_cvtermpath_root_loop($cterm_result['subject_id'], $cvid);
- }
- }
- }
- function tripal_update_cvtermpath_loop($origin, $child_id, $cv_id, $type_id, $depth, $increment_of_depth, $array_of_children){
-
- chado_set_active('chado');
- $count = db_query(
- 'SELECT *
- FROM cvtermpath
- WHERE cv_id = :cvid
- AND object_id = :origin
- AND subject_id = :child_id
- AND pathdistance = :depth
- ',
- array(':cvid' => $cv_id, ':origin' => $origin, ':child_id' => $child_id, ':depth' => $depth)
- );
- $count_total = $count->rowCount();
- try{
- if($count_total == 0) {
-
- chado_set_active('chado');
- $query = db_insert('cvtermpath')
- ->fields(array(
- 'object_id' => $origin,
- 'subject_id' => $child_id,
- 'cv_id' => $cv_id,
- 'type_id' => $type_id,
- 'pathdistance' => $depth,
- ));
- $rows = $query->execute();
-
-
-
- $children_id = $origin . '|' . $child_id . '|' . $cv_id . '|' . $type_id;
-
- if ($increment_of_depth != 0) {
-
- foreach ($array_of_children as $key => $val) {
- if ($val['build_id'] == $children_id) {
-
- $result_of_loop_checker = tripal_update_cvtermpath_loop_checker($origin, $child_id, $cv_id, $type_id, $depth, $increment_of_depth, 0, $val, [], $depth);
- watchdog('debug', '<pre>tripal_update_cvtermpath_loop $result_of_loop_checker: ' . print_r($result_of_loop_checker, TRUE) . '</pre>');
- if ($result_of_loop_checker) {
- watchdog('debug', 'Loop found exit the loop function');
- return 'LOOP';
- }
- }
- }
- }
-
- $array_of_children[$increment_of_depth] = [
- 'build_id' => $children_id,
- 'depth' => $depth
- ];
- $query = db_select('cvterm_relationship', 'cvtr')
- ->fields('cvtr')
- ->condition('cvtr.object_id', $child_id, '=')
- ->execute();
- $cterm = $query->fetchAll();
- foreach ($cterm as $item) {
- $increment_of_depth++;
- tripal_update_cvtermpath_loop($origin, $item->subject_id, $cv_id, $item->type_id, $depth + 1, $increment_of_depth, $array_of_children);
- }
-
- }
- }
- catch(Exception $e){
- watchdog_exception('tripal_ds', $e);
- return FALSE;
- }
- }
- function tripal_update_cvtermpath_loop_checker($origin, $child_id, $cv_id, $type_id, $depth, $increment_of_depth, $distance_between_parent_child, $possible_start_of_loop, $array_of_possible_loop, $depth_at_start_of_loop){
- chado_set_active('chado');
- $query = db_select('cvterm_relationship', 'cvtr')
- ->fields('cvtr')
- ->condition('cvtr.object_id', $child_id, '=')
- ->execute();
- $cterm = $query->fetchAll();
- foreach ($cterm as $item){
- if ($distance_between_parent_child > 0){
-
- foreach ($array_of_possible_loop as $key => $val) {
- if ($val['build_id'] === $possible_start_of_loop['build_id']) {
- $possible_loop_starts = $val;
-
- if (!empty($possible_loop_starts)) {
- $result = tripal_update_cvtermpath_loop_checker_traverse($origin, $child_id, $cv_id, $type_id, $depth, $increment_of_depth, $possible_start_of_loop, $array_of_possible_loop, array(), 0);
- watchdog('debug', '<pre>tripal_update_cvtermpath_loop_checker $result: '. print_r($result, TRUE) .'</pre>');
- if(!empty($result)){
- return $result;
- }
- }
- }
- }
- }
- elseif($loop_found !== TRUE){}
- $increment_of_depth++;
- $distance_between_parent_child++;
- $children_id = $origin .'|' .$item->subject_id .'|' .$cv_id.'|' .$item->type_id;
- $array_of_possible_loop[$distance_between_parent_child] = array('build_id' => $children_id);
- $result = tripal_update_cvtermpath_loop_checker($origin, $item->subject_id, $cv_id, $item->type_id, $depth + 1, $increment_of_depth, $distance_between_parent_child, $possible_start_of_loop, $array_of_possible_loop, $depth_at_start_of_loop);
- if($result !== FALSE){
- return $result;
- }
- }
- return FALSE;
- }
- function tripal_update_cvtermpath_loop_checker_traverse($origin, $child_id, $cv_id, $type_id, $depth, $increment_of_depth, $possible_start_of_loop, $array_of_possible_loop, $traverse_of_loop, $increment){
- if($increment > 10){
- die();
- }
- chado_set_active('chado');
- $query = db_select('cvterm_relationship', 'cvtr')
- ->fields('cvtr')
- ->condition('cvtr.object_id', $child_id, '=')
- ->execute();
- $cterm = $query->fetchAll();
- foreach ($cterm as $item){
- if($array_of_possible_loop === $traverse_of_loop){
- watchdog('debug', '<pre>$array_of_possible_loop: '. print_r($array_of_possible_loop, TRUE) .'</pre>');
- watchdog('debug', '<pre>$traverse_of_loop: '. print_r($traverse_of_loop, TRUE) .'</pre>');
- watchdog('debug', 'LOOP');
-
- return $item;
- }
- elseif($array_of_possible_loop != $traverse_of_loop) {
- $increment_of_depth++;
- $increment++;
- $children_id = $origin . '|' . $item->subject_id . '|' . $cv_id . '|' . $item->type_id;
- $traverse_of_loop[$increment] = ['build_id' => $children_id];
- $result = tripal_update_cvtermpath_loop_checker_traverse($origin, $item->subject_id, $cv_id, $item->type_id, $depth + 1, $increment_of_depth, $possible_start_of_loop, $array_of_possible_loop, $traverse_of_loop, $increment);
- if($result !== FALSE){
- return $result;
- }
- }
- }
- return FALSE;
- }
-
-
- function tripal_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 tripal_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 = tripal_get_cv(array('name' => $cvname));
- if (!$cv) {
- $cv = tripal_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 = tripal_get_db(array('name' => $dbname));
- if (!$db) {
- $db = tripal_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 = tripal_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 = tripal_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' => $dbxref->accession, ':dbname' => $dbname, ':term' => $name, ':cvname' => $cvname))->fetchObject();
- if (!$check) {
-
- $dbxref = tripal_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 tripal_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 tripal_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 tripal_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 tripal_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 tripal_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,
- "tripal_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 = tripal_insert_cvterm($values);
- if (isset($insert->cvterm_id)) {
- $cvterm['cvterm_id'] = $insert->cvterm_id;
- }
- else {
- tripal_report_error('tripal_chado_api', TRIPAL_WARNING,
- "tripal_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,
- "tripal_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;
- }
- function tripal_set_default_cv($table, $field, $cv_name, $cv_id = FALSE) {
-
- if ($cv_id) {
- $cv = tripal_get_cv(array('cv_id' => $cv_id));
- }
- else {
- $cv = tripal_get_cv(array('name' => $cv_name));
- }
- if ($cv) {
-
- $num_deleted = db_delete('tripal_cv_defaults')
- ->condition('table_name', $table)
- ->condition('field_name', $field)
- ->execute();
-
- $cv_default_id = db_insert('tripal_cv_defaults')
- ->fields(array(
- 'table_name' => $table,
- 'field_name' => $field,
- 'cv_id' => $cv->cv_id,
- ))
- ->execute();
- if (!$cv_default_id) {
- tripal_report_error('tripal_chado', TRIPAL_WARNING,
- "Cannot set default vocabulary for %table.%field. Check the error logs.",
- array('%table' => $table, '%field' => $field));
- return FALSE;
- }
- }
- else {
- tripal_report_error('tripal_chado', TRIPAL_WARNING,
- "Cannot set default vocabulary for %table.%field. The vocabulary name, '%cvname', doesn't exist.",
- array('%table' => $table, '%field' => $field, '%cvname' => $cv_name));
- return FALSE;
- }
- }
|