12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631 |
- <?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;
- }
|