12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433 |
- <?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_clear_cvtermpath($cv_id) {
- $sql = "DELETE FROM {cvtermpath} WHERE cv_id = :cv_id";
- chado_query($sql, [':cv_id' => $cv_id]);
- }
- function chado_update_cvtermpath($cv_id, $job = NULL) {
- $cv = new ChadoRecord('cv', $cv_id);
- print "Building cvterm paths for vocabulary: " . $cv->getValue('name') ."\n";
- print "Clearing the cvtermpath table for this vocabulary...\n";
- chado_clear_cvtermpath($cv_id);
- print "Clearing completed.\n";
-
- $cache = [
-
- 'rels' => [],
-
- 'roots_processed' => [],
-
-
- 'nodes_processed' => [],
-
- 'is_a' => NULL,
- ];
-
-
- $transaction = db_transaction();
- try {
-
- $sql = "SELECT * FROM {cvterm} WHERE (name = :is_a or name = :is_a_alt) and cv_id = :cv_id";
- $args = [':is_a' => 'is_a', ':is_a_alt' => 'is a', ':cv_id' => $cv_id];
- $cache['is_a'] = chado_query($sql, $args)->fetchObject();
-
-
- print "Retrieving relationships...\n";
- $sql = "
- SELECT CVTR.subject_id, CVTR.type_id, CVTR.object_id, CVTS.name
- FROM {cvterm_relationship} CVTR
- INNER JOIN {cvterm} CVTO on CVTO.cvterm_id = CVTR.object_id
- INNER JOIN {cvterm} CVTS on CVTS.cvterm_id = CVTR.subject_id
- WHERE CVTO.cv_id = :cv_id
- ORDER BY CVTO.name, CVTS.name
- ";
- $rels = chado_query($sql, [':cv_id' => $cv_id]);
- $total_items;
- while ($rel = $rels->fetchObject()) {
- $cache['rels'][$rel->object_id][] = [$rel->subject_id, $rel->type_id, $rel->name];
- }
- $total_items = count(array_keys($cache['rels']));
- if ($job) {
- $job->logMessage('Found !total relationships in this vocabulary.', ['!total' => $total_items]);
- $job->setTotalItems($total_items);
- $job->logMessage('Note: Progress updates occur as each term is processed and ' .
- 'some terms take longer than others.');
- $job->setProgress(0);
- $job->setInterval(1);
- }
-
-
- $sql = "
- SELECT DISTINCT CVT.cvterm_id, CVT.name
- FROM {cvterm} CVT
- LEFT JOIN {cvterm_relationship} CVTR ON CVT.cvterm_id = CVTR.subject_id
- INNER JOIN {cvterm_relationship} CVTR2 ON CVT.cvterm_id = CVTR2.object_id
- WHERE CVT.cv_id = :cvid AND CVTR.subject_id is NULL and
- CVT.is_relationshiptype = 0 and CVT.is_obsolete = 0
- ";
- $roots = chado_query($sql, [':cvid' => $cv_id]);
-
- print "Processing terms...\n";
- while ($root = $roots->fetchObject()) {
- $root_id = $root->cvterm_id;
- $root_name = $root->name;
- if ($job) {
- $job->logMessage('Processing tree root: ' . $root_name . '...');
- }
-
-
- $num_handled = 0;
- $depth = 0;
- _chado_update_cvtermpath_root($cv_id, $root_id, $root_name, $cache, $job,
- $num_handled, $depth);
- }
- }
- catch (Exception $e) {
- $transaction->rollback();
- throw $e;
- }
- }
- function _chado_update_cvtermpath_root($cv_id, $root_id, $root_name, &$cache,
- $job, &$num_handled, $root_depth = 0) {
-
- if (in_array($root_id, $cache['roots_processed'])) {
- return;
- }
-
- $cache['roots_processed'][] = $root_id;
-
- if ($root_depth == 0) {
- $is_a = $cache['is_a'];
- $type_id = $is_a->cvterm_id;
- $depth = 1;
- _chado_update_cvtermpath_add_relationship($type_id, $root_id, $root_id, $cv_id, $depth);
- }
-
- if (!array_key_exists($root_id, $cache['rels'])) {
- return;
- }
-
- $children = $cache['rels'][$root_id];
-
- if (!$children) {
- return;
- }
-
- if ($job) {
- $job->setItemsHandled($num_handled);
- }
- $num_handled++;
-
-
-
-
- $path = [];
-
- $cache['nodes_processed'] = [];
- $next_depth = 0;
- _chado_update_cvtermpath_process_children($cv_id, $root_id, $root_id, $path, $cache, $next_depth);
-
- foreach ($children as $child) {
- $child_id = $child[0];
- $child_type_id = $child[1];
- $child_name = $child[2];
-
- $next_depth = $root_depth + 1;
- _chado_update_cvtermpath_root($cv_id, $child_id, $child_name, $cache,
- $job, $num_handled, $next_depth);
- }
- }
- function _chado_update_cvtermpath_process_children($cv_id, $root_id, $cvterm_id,
- $path, &$cache, $depth = 1) {
- $cache['nodes_processed'][$cvterm_id] = TRUE;
-
-
- if (in_array($cvterm_id, $path)) {
- return;
- }
-
- $path[] = $cvterm_id;
-
-
- if (!array_key_exists($cvterm_id, $cache['rels'])) {
- return;
- }
-
- $children = $cache['rels'][$cvterm_id];
-
- if (!$children) {
- return;
- }
-
- $next_depth = $depth + 1;
- foreach ($children as $child) {
- $child_id = $child[0];
- $child_type_id = $child[1];
-
- if (array_key_exists($child_id, $cache['nodes_processed'])) {
- continue;
- }
- _chado_update_cvtermpath_add_relationship($child_type_id, $child_id, $root_id,
- $cv_id, $next_depth);
- _chado_update_cvtermpath_process_children($cv_id, $root_id, $child_id, $path, $cache, $next_depth);
- }
- }
- function _chado_update_cvtermpath_add_relationship($type_id, $cvterm_id,
- $root_id, $cv_id, $depth) {
- $cvtermpath = new ChadoRecord('cvtermpath');
- $cvtermpath->setValues([
- 'type_id' => $type_id,
- 'subject_id' => $cvterm_id,
- 'object_id' => $root_id,
- 'cv_id' => $cv_id,
- 'pathdistance' => $depth,
- ]);
- $cvtermpath->insert();
- }
- 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;
- }
|