tripal_cv.api.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. <?php
  2. /**
  3. * @file
  4. * Controlled Vocabulary API
  5. *
  6. * @defgroup tripal_cv_api CV Module API
  7. * @ingroup tripal_api
  8. * This module provides a set of functions to simplify working with
  9. * controlled vocabularies. Most of the API functions deal with retrieving
  10. * terms or their parent vocabularies.
  11. *
  12. * However, the API also supports
  13. * generation of trees for browsing a vocabulary as well as generation of
  14. * pie graphs for display of hierarchical counts of terms. Version 0.3b of
  15. * Tripal provides a feature browser and a feature summary chart uses
  16. * the API functions provided here. But in general charts and trees can be
  17. * created for any controlled vocabulary.
  18. *
  19. */
  20. /**
  21. * Purpose: To retrieve a chado controlled vocabulary object
  22. *
  23. * @param $select_values
  24. * An array meant to uniquely select a given controlled vocabulary
  25. *
  26. * @return
  27. * Chado controlled vocabulary object
  28. *
  29. * The controlled vocabulary is selected using tripal_core_chado select and as such the
  30. * $select_values array parameter meant to uniquely identify the controlled vocab to be
  31. * returned follows the same form as when using tripal_core_chado_select directly.
  32. *
  33. * Example Usage:
  34. * @code
  35. $select_values = array(
  36. 'name' => 'feature_property'
  37. );
  38. $cv_object = tripal_cv_get_cv($select_values);
  39. * @endcode
  40. * The above code selects the feature_property cv and returns the following object:
  41. * @code
  42. $cv_object = stdClass Object (
  43. [cv_id] => 13
  44. [name] => feature_property
  45. [definition] =>
  46. );
  47. * @endcode
  48. *
  49. * @ingroup tripal_cv_api
  50. */
  51. function tripal_cv_get_cv($select_values) {
  52. $columns = array(
  53. 'cv_id',
  54. 'name',
  55. 'definition',
  56. );
  57. $results = tripal_core_chado_select('cv', $columns, $select_values);
  58. if (sizeof($results) == 1) {
  59. return $results[0];
  60. }
  61. elseif (empty($results)) {
  62. watchdog('tripal_cv',
  63. 'tripal_cv_get_cv: No cv matches criteria values:%values',
  64. array('%values' => print_r($select_values, TRUE)),
  65. WATCHDOG_WARNING
  66. );
  67. return FALSE;
  68. }
  69. else {
  70. watchdog('tripal_cv',
  71. 'tripal_cv_get_cv: 2+ cvs match criteria values:%values',
  72. array('%values' => print_r($select_values, TRUE)),
  73. WATCHDOG_WARNING
  74. );
  75. }
  76. }
  77. // Purpose: To retrieve a chado cv object
  78. // @param $where_options
  79. // @code
  80. // array(
  81. // <column_name> => array(
  82. // 'type' => <type of column: INT/STRING>,
  83. // 'value' => <the vlaue you want to filter on>,
  84. // 'exact' => <if TRUE use =; if FALSE use ~>,
  85. // )
  86. // )
  87. // @endcode
  88. //
  89. // @return
  90. // Chado cv object with all fields from the chado cv table
  91. //
  92. // @ingroup tripal_cv_api
  93. //
  94. //function tripal_cv_get_cv ($where_options)
  95. /**
  96. * Retrieve a cv given the cv name
  97. *
  98. * @param $name
  99. * The name of the cv to be returned
  100. * @return
  101. * The cv object for the specified CV name
  102. *
  103. * @ingroup tripal_cv_api
  104. */
  105. function tripal_cv_get_cv_by_name($name) {
  106. $r = tripal_core_chado_select('cv', array('*'), array('name' => $name));
  107. return $r[0];
  108. }
  109. /**
  110. * Retrieve the cv object for the specified CV id
  111. *
  112. * NOTE: This function is deprecated.
  113. * @see tripal_core_chado_generate_vars()
  114. *
  115. * @param $cv_id
  116. * The unique identifier for the cv to retrieve
  117. *
  118. * @return
  119. * An object describing the cv
  120. *
  121. * @ingroup tripal_cv_api
  122. */
  123. function tripal_cv_get_cv_by_id($cv_id) {
  124. $r = tripal_core_chado_select('cv', array('*'), array('cv_id' => $cv_id));
  125. return $r;
  126. }
  127. /**
  128. * Retrieve the cv id for the specified CV by name
  129. *
  130. * NOTE: This function is deprecated.
  131. * @see tripal_core_chado_generate_vars()
  132. *
  133. * @param $cv_name
  134. * The unique name for the cv to retrieve
  135. *
  136. * @return
  137. * The numeric cv ID
  138. *
  139. * @ingroup tripal_cv_api
  140. */
  141. function tripal_cv_get_cv_id($cv_name) {
  142. $sql = "
  143. SELECT cv_id FROM {cv} WHERE name = '%s'
  144. ";
  145. $cv = db_fetch_object(chado_query($sql, $cv_name));
  146. return $cv->cv_id;
  147. }
  148. /**
  149. * Create an options array to be used in a form element which provides a
  150. * list of all chado cvs
  151. *
  152. * NOTE: This function is deprecated as of Tripal v1.0
  153. *
  154. * @return
  155. * An array(cv_id => name) for each cv in the chado cv table
  156. *
  157. * @ingroup tripal_cv_api
  158. */
  159. function tripal_cv_get_cv_options() {
  160. $results = tripal_core_chado_select('cv', array('cv_id', 'name'), array());
  161. $options = array();
  162. foreach ($results as $r) {
  163. $options[$r->cv_id] = $r->name;
  164. }
  165. return $options;
  166. }
  167. /**
  168. * Retrieve a chado cvterm object with a given name
  169. *
  170. * @param $name
  171. * the cvterm.name
  172. * @param $cv_id
  173. * the cv_id of the term you are looking for
  174. * @param $cv_name
  175. * the name of the CV
  176. *
  177. * @return
  178. * cvterm object
  179. *
  180. * @ingroup tripal_cv_api
  181. */
  182. function tripal_cv_get_cvterm_by_name($name, $cv_id = 0, $cv_name = 'tripal') {
  183. if ($cv_id) {
  184. $values = array(
  185. 'name' => $name,
  186. 'cv_id' => $cv_id,
  187. );
  188. $r = tripal_core_chado_select('cvterm', array('*'), $values);
  189. }
  190. elseif ($cv_name) {
  191. $values = array(
  192. 'name' => $name,
  193. 'cv_id' => array(
  194. 'name' => $cv_name,
  195. ),
  196. );
  197. $r = tripal_core_chado_select('cvterm', array('*'), $values);
  198. }
  199. else {
  200. $values = array(
  201. 'name' => $name,
  202. );
  203. $r = tripal_core_chado_select('cvterm', array('*'), $values);
  204. }
  205. if (!$r) {
  206. return FALSE;
  207. }
  208. if (count($r) > 0) {
  209. return FALSE;
  210. }
  211. return $r[0];
  212. }
  213. /**
  214. * Create an options array to be used in a form element
  215. * which provides a list of all chado cvterms
  216. *
  217. * @param $cv_id
  218. * The chado cv_id;
  219. * only cvterms with the supplied cv_id will be returned
  220. * @return
  221. * An array(cvterm_id => name)
  222. * for each cvterm in the chado cvterm table where cv_id=that supplied
  223. *
  224. * @ingroup tripal_cv_api
  225. */
  226. function tripal_cv_get_cvterm_options($cv_id = 0) {
  227. if ($cv_id > 0) {
  228. $results = tripal_core_chado_select('cvterm', array('cvterm_id', 'name'), array('cv_id' => $cv_id));
  229. }
  230. else {
  231. $results = tripal_core_chado_select('cvterm', array('cvterm_id', 'name'), array());
  232. }
  233. $options = array();
  234. foreach ($results as $r) {
  235. $options[$r->cvterm_id] = $r->name;
  236. }
  237. return $options;
  238. }
  239. /**
  240. * Updates the cvtermpath table of Chado for the specified CV.
  241. *
  242. * @param $cv_id
  243. * The chado cv_id;
  244. * @param $job_id
  245. * This function is intended to be used with the Tripal Jobs API.
  246. * When this function is called as a job the $job_id is automatically
  247. * passed to this function.
  248. * @return
  249. * TRUE on success FALSE on failure
  250. *
  251. * @ingroup tripal_cv_api
  252. */
  253. function tripal_cv_update_cvtermpath($cvid, $job_id = NULL) {
  254. // TODO: need better error checking in this function
  255. // first get the controlled vocabulary name:
  256. $cv = db_fetch_object(chado_query("SELECT * FROM {cv} WHERE cv_id = %d", $cvid));
  257. print "\nUpdating cvtermpath for $cv->name...\n";
  258. // now fill the cvtermpath table
  259. // @coder-ignore: using a function rather then tablename therefore table prefixing doesn't apply
  260. $sql = "SELECT * FROM fill_cvtermpath('%s')";
  261. $success = chado_query($sql, $cv->name);
  262. return TRUE;
  263. }
  264. /**
  265. * Adds a controlled vocabular to the CV table of Chado.
  266. *
  267. * @param $name
  268. * The name of the controlled vocabulary. These are typically all lower case
  269. * with no special characters other than an undrescore (for spaces).
  270. * @param $comment
  271. * A description or definition of the vocabulary.
  272. *
  273. * @return
  274. * An object populated with fields from the newly added database.
  275. *
  276. * @ingroup tripal_cv_api
  277. */
  278. function tripal_cv_add_cv($name, $definition) {
  279. // insert/update values
  280. $ins_values = array(
  281. 'name' => $name,
  282. 'definition' => $definition
  283. );
  284. // see if the CV (default-namespace) exists already in the database
  285. $sel_values = array('name' => $name);
  286. $sel_options = array('statement_name' => 'sel_cv_na');
  287. $results = tripal_core_chado_select('cv', array('*'), $sel_values, $sel_options);
  288. // if it does not exists then add it
  289. if (count($results) == 0) {
  290. $ins_options = array('statement_name' => 'ins_cv_nade');
  291. $success = tripal_core_chado_insert('cv', $ins_values, $ins_options);
  292. if (!$success) {
  293. watchdog('tripal_cv', "Failed to create the CV record", NULL, WATCHDOG_WARNING);
  294. return FALSE;
  295. }
  296. $results = tripal_core_chado_select('cv', array('*'), $sel_values, $sel_options);
  297. }
  298. // if it already exists then do an update
  299. else {
  300. $upd_options = array('statement_name' => 'upd_cv_nade');
  301. $success = tripal_core_chado_update('cv', $sel_values, $ins_values, $upd_options);
  302. if (!$success) {
  303. watchdog('tripal_cv', "Failed to update the CV record", NULL, WATCHDOG_WARNING);
  304. return FALSE;
  305. }
  306. $results = tripal_core_chado_select('cv', array('*'), $sel_values, $sel_options);
  307. }
  308. // return the cv object
  309. return $results[0];
  310. }
  311. /**
  312. * Add's a CV term to the cvterm table. If the parent CV does not exist then
  313. * that too is added to the CV table. If the cvterm is a relationship term
  314. * then the $is_relationship argument should be set. The function will try
  315. * to first find the relationship in the relationship ontology for updating and
  316. * if it can't be found will add the relationship to the __global CV. All terms
  317. * must also have a corresponding database. This is specified in the term's
  318. * ID just before the colon (e.g. GO:003824). If the database does not exist
  319. * in the DB table then it will be added automatically. The accession (the
  320. * value just after the colon in the term's ID) will be added to the dbxref
  321. * table. If the CVterm already exists and $update is set (default) then the
  322. * cvterm is updated. If the CVTerm already exists and $update is not set, then
  323. * no changes are made and the CVTerm object is returned.
  324. *
  325. * @param $term
  326. * An associative array with the following keys: 'id', 'name' and 'namespace',
  327. * 'is_obsolete', and 'def'. Where 'id' is the term accession, 'name' is the
  328. * term name, 'namespace' is the CV name for the term, 'def' is the term
  329. * definition and 'is_obsolete' is present and set to 1 if the term is defunct.
  330. * The 'id' must be of the form <DB>:<ACCESSION>, where <DB> is the name of
  331. * the database to which the cvterm belongs and the <ACCESSION> is the
  332. * term's accession number in the database.
  333. * @param $defaultcv
  334. * Optional. The CV name to which the term
  335. * belongs. If this arugment is null or not provided then the function tries
  336. * to find a record in the CV table with the same name provided in the
  337. * $term[namespace]. If this field is provided then it overrides what the
  338. * value in $term[namespace]
  339. * @param $is_relationship
  340. * If this term is a relationship term then this value should be 1.
  341. * @param $update
  342. * By default this is set to 1. If the term exists it is automatically updated.
  343. * @param $dbname
  344. * In some cases the database name will not be part of the $term['id'] and it
  345. * needs to be explicitly set. Use this argument only if the database name
  346. * cannot be specififed in the term ID (e.g. <DB>:<ACCESSION>).
  347. *
  348. * @return
  349. * A CVTerm object
  350. *
  351. * @ingroup tripal_cv_api
  352. */
  353. function tripal_cv_add_cvterm($term, $defaultcv = '_global', $is_relationship = 0,
  354. $update = 1, $dbname = 'internal') {
  355. // get the term properties
  356. $id = $term['id'];
  357. $name = '';
  358. $cvname = '';
  359. $definition = '';
  360. $is_obsolete = 0;
  361. $accession = '';
  362. if (array_key_exists('name', $term)) {
  363. $name = $term['name'];
  364. }
  365. else {
  366. $name = $id;
  367. }
  368. if (array_key_exists('namespace', $term)) {
  369. $cvname = $term['namespace'];
  370. }
  371. else {
  372. $cvname = $defaultcv;
  373. }
  374. if (array_key_exists('def', $term)) {
  375. $definition = preg_replace('/^\"(.*)\"/', '\1', $term['def']);
  376. }
  377. else {
  378. $definition = '';
  379. }
  380. if (array_key_exists('is_obsolete', $term)) {
  381. $is_obsolete = $term['is_obsolete'];
  382. if (strcmp($is_obsolete, 'true') == 0) {
  383. $is_obsolete = 1;
  384. }
  385. }
  386. if (!$name and !$id) {
  387. watchdog('tripal_cv', "Cannot find cvterm without 'id' or 'name'", NULL, WATCHDOG_WARNING);
  388. return 0;
  389. }
  390. if (!$id) {
  391. $id = $name;
  392. }
  393. // get the accession and the database from the cvterm id
  394. if ($dbname) {
  395. $accession = $id;
  396. }
  397. if (preg_match('/^.+?:.*$/', $id)) {
  398. $accession = preg_replace('/^.+?:(.*)$/', '\1', $id);
  399. $dbname = preg_replace('/^(.+?):.*$/', '\1', $id);
  400. }
  401. // check that we have a database name, give a different message if it's a relationship
  402. if ($is_relationship and !$dbname) {
  403. watchdog('tripal_cv', "A database name is not provided for this relationship term: $id", NULL, WATCHDOG_WARNING);
  404. return 0;
  405. }
  406. if (!$is_relationship and !$dbname) {
  407. watchdog('tripal_cv', "A database identifier is missing from the term: $id", NULL, WATCHDOG_WARNING);
  408. return 0;
  409. }
  410. // make sure the CV name exists
  411. $cv = tripal_cv_add_cv($cvname, '');
  412. if (!$cv) {
  413. watchdog('tripal_cv', "Cannot find namespace '$cvname' when adding/updating $id", NULL, WATCHDOG_WARNING);
  414. return 0;
  415. }
  416. // this SQL statement will be used a lot to find a cvterm so just set it
  417. // here for easy reference below. Because CV terms can change their names
  418. // but accessions don't change, the following SQL finds cvterms based on
  419. // their accession rather than the name
  420. if (!tripal_core_is_sql_prepared('sel_cvterm_by_accession')) {
  421. $pcvtermsql = "
  422. PREPARE sel_cvterm_by_accession(text, text) AS
  423. SELECT CVT.name, CVT.cvterm_id, CV.cv_id, CV.name as cvname,
  424. DB.name as dbname, DB.db_id, DBX.accession
  425. FROM cvterm CVT
  426. INNER JOIN dbxref DBX on CVT.dbxref_id = DBX.dbxref_id
  427. INNER JOIN db DB on DBX.db_id = DB.db_id
  428. INNER JOIN cv CV on CV.cv_id = CVT.cv_id
  429. WHERE DBX.accession = $1 and DB.name = $2";
  430. if (!tripal_core_chado_prepare('sel_cvterm_by_accession', $pcvtermsql, array('text', 'text'))) {
  431. watchdog('tripal_cv', "Cannot prepare statement 'sel_cvterm_by_accession'", NULL, WATCHDOG_WARNING);
  432. return 0;
  433. }
  434. }
  435. $cvtermsql = "EXECUTE sel_cvterm_by_accession('%s','%s')";
  436. // add the database. The function will just return the DB object if the
  437. // database already exists.
  438. $db = tripal_db_add_db($dbname);
  439. if (!$db) {
  440. watchdog('tripal_cv', "Cannot find database '$dbname' in Chado.", NULL, WATCHDOG_WARNING);
  441. return 0;
  442. }
  443. // the cvterm table has two unique dependencies. We need to check both.
  444. // first check the (name, cv_id, is_obsolete) constraint
  445. $values = array(
  446. 'name' => $name,
  447. 'is_obsolete' => $is_obsolete,
  448. 'cv_id' => array(
  449. 'name' => $cvname,
  450. ),
  451. );
  452. $options = array('statement_name' => 'sel_cvterm_c1');
  453. $result = tripal_core_chado_select('cvterm', array('*'), $values, $options);
  454. // if the constraint is met then let's check it to see if
  455. // the database name matches the one we have been provided
  456. if (count($result) == 1) {
  457. $cvterm = $result[0];
  458. // get the dbxref record
  459. $values = array('dbxref_id' => $cvterm->dbxref_id);
  460. $options = array('statement_name' => 'sel_dbxref_id');
  461. $result = tripal_core_chado_select('dbxref', array('*'), $values, $options);
  462. $dbxref = $result[0];
  463. // get the db
  464. $values = array('db_id' => $dbxref->db_id);
  465. $options = array('statement_name' => 'sel_db_id');
  466. $result = tripal_core_chado_select('db', array('*'), $values, $options);
  467. $db_check = $result[0];
  468. // the database name for this existing term does not match that of the
  469. // one provided to this function. The CV name matches otherwise we
  470. // wouldn't have made it this far. So, let's swap the database for
  471. // this term
  472. if ($db_check->name != $db->name) {
  473. // look to see if the correct dbxref record already exists for this database
  474. $values = array(
  475. 'db_id' => $db->db_id,
  476. 'accession' => $accession,
  477. );
  478. $options = array('statement_name' => 'sel_dbxref_idac');
  479. $result = tripal_core_chado_select('dbxref', array('*'), $values, $options);
  480. // if we already have a good dbxref then we want to update our cvterm
  481. // to use this dbxref
  482. if (count($result) > 0) {
  483. $dbxref = $result[0];
  484. $match = array('cvterm_id' => $cvterm->cvterm_id);
  485. $values = array('dbxref_id' => $dbxref->dbxref_id);
  486. $options = array('statement_name' => 'upd_cvterm_db');
  487. $success = tripal_core_chado_update('cvterm', $match, $values, $options);
  488. if (!$success) {
  489. watchdog('tripal_cv', "Failed to correct the dbxref id for the cvterm " .
  490. "'$name' (id: $accession), for database $dbname", NULL, WATCHDOG_WARNING);
  491. return 0;
  492. }
  493. }
  494. // if we don't have the record then we want to delete our cvterm and let the code
  495. // below recreate it with the correct info
  496. else {
  497. $match = array('cvterm_id' => $cvterm->cvterm_id);
  498. $options = array('statement_name' => 'del_cvterm_cv');
  499. tripal_core_chado_delete('cvterm', $match, $options);
  500. }
  501. }
  502. // check that the accession matches. Sometimes an OBO can define the same term
  503. // multiple times but with different accessions. If this is the case we
  504. // can't do an insert or it will violate the constraint in the cvterm table.
  505. // so we'll need to add the record to the cvterm_dbxref table instead
  506. if ($dbxref->accession != $accession) {
  507. // get/add the dbxref fort his term
  508. $dbxref_new = tripal_db_add_dbxref($db->db_id, $accession);
  509. if (!$dbxref_new) {
  510. watchdog('tripal_cv', "Failed to find or insert the dbxref record for cvterm, " .
  511. "$name (id: $accession), for database $dbname", NULL, WATCHDOG_WARNING);
  512. return 0;
  513. }
  514. // check to see if the cvterm_dbxref record already exists
  515. $values = array(
  516. 'cvterm_id' => $cvterm->cvterm_id,
  517. 'dbxref_id' => $dbxref_new->dbxref_id,
  518. 'is_for_definition' => 1,
  519. );
  520. $options = array('statement_name' => 'sel_cvtermdbxref_cvdbis');
  521. $result = tripal_core_chado_select('cvterm_dbxref', array('*'), $values, $options);
  522. // if the cvterm_dbxref record does not exists then add it
  523. if (count($result)==0) {
  524. $options = array(
  525. 'statement_name' => 'ins_cvtermdbxref_cvdbis',
  526. 'return_record' => FALSE,
  527. );
  528. $success = tripal_core_chado_insert('cvterm_dbxref', $values, $options);
  529. if (!$success) {
  530. watchdog('tripal_cv', "Failed to find or insert the cvterm_dbxref record for a " .
  531. "duplicated cvterm: $name (id: $accession), for database $dbname", NULL, WATCHDOG_WARNING);
  532. return 0;
  533. }
  534. }
  535. // get the original cvterm with the same name and return that.
  536. $cvterm = db_fetch_object(chado_query($cvtermsql, $dbxref->accession, $dbname));
  537. return $cvterm;
  538. }
  539. // continue on, we've fixed the record if the db_id did not match,
  540. // we can now perform and updated if we need to.
  541. }
  542. // get the CVterm record
  543. $cvterm = db_fetch_object(chado_query($cvtermsql, $accession, $dbname));
  544. //print "$pcvtermsql\n$cvtermsql\n$accession, $dbname\n";
  545. //print "CVTERM:\n";
  546. //print_r($cvterm);
  547. if (!$cvterm) {
  548. // check to see if the dbxref exists if not, add it
  549. $dbxref = tripal_db_add_dbxref($db->db_id, $accession);
  550. if (!$dbxref) {
  551. watchdog('tripal_cv', "Failed to find or insert the dbxref record for cvterm, " .
  552. "$name (id: $accession), for database $dbname", NULL, WATCHDOG_WARNING);
  553. return 0;
  554. }
  555. // check to see if the dbxref already has an entry in the cvterm table
  556. // this is the second constraint in the cvterm table
  557. $values = array('dbxref_id' => $dbxref->dbxref_id);
  558. $options = array('statement_name' => 'sel_cvterm_db');
  559. $check = tripal_core_chado_select('cvterm', array('cvterm_id'), $values, $options);
  560. if (count($check) == 0) {
  561. // now add the cvterm
  562. $ins_values = array(
  563. 'cv_id' => $cv->cv_id,
  564. 'name' => $name,
  565. 'definition' => $definition,
  566. 'dbxref_id' => $dbxref->dbxref_id,
  567. 'is_obsolete' => $is_obsolete,
  568. 'is_relationshiptype' => $is_relationship,
  569. );
  570. $ins_options = array('statement_name' => 'ins_cvterm_all');
  571. $success = tripal_core_chado_insert('cvterm', $ins_values, $ins_options);
  572. if (!$success) {
  573. if (!$is_relationship) {
  574. watchdog('tripal_cv', "Failed to insert the term: $name ($dbname)", NULL, WATCHDOG_WARNING);
  575. return 0;
  576. }
  577. else {
  578. watchdog('tripal_cv', "Failed to insert the relationship term: $name (cv: " . $cvname . " db: $dbname)", NULL, WATCHDOG_WARNING);
  579. return 0;
  580. }
  581. }
  582. }
  583. // this dbxref already exists in the cvterm table
  584. else {
  585. watchdog('tripal_cv', "The dbxref already exists for another cvterm record: $name (cv: " . $cvname . " db: $dbname)", NULL, WATCHDOG_WARNING);
  586. return 0;
  587. }
  588. $cvterm = db_fetch_object(chado_query($cvtermsql, $accession, $dbname));
  589. }
  590. // upate the cvterm
  591. elseif ($update) {
  592. $match = array('cvterm_id' => $cvterm->cvterm_id);
  593. $upd_values = array(
  594. 'name' => $name,
  595. 'definition' => $definition,
  596. 'is_obsolete' => $is_obsolete,
  597. 'is_relationshiptype' => $is_relationship,
  598. );
  599. $upd_options = array('statement_name' => 'upd_cvterm_nadeisis');
  600. $success = tripal_core_chado_update('cvterm', $match, $upd_values, $upd_options);
  601. if (!$success) {
  602. watchdog('tripal_cv', "Failed to update the term: $name", NULL, WATCHDOG_WARNING);
  603. return 0;
  604. }
  605. $cvterm = db_fetch_object(chado_query($cvtermsql, $accession, $dbname));
  606. }
  607. else {
  608. // do nothing, we have the cvterm but we don't want to update
  609. }
  610. // return the cvterm
  611. return $cvterm;
  612. }
  613. /**
  614. * This function defines the custom tables that will be created
  615. * in the chado schema.
  616. *
  617. * @ingroup tripal_cv
  618. */
  619. function tripal_cv_get_custom_tables($table = NULL) {
  620. if (!$table or strcmp($table, 'tripal_obo_temp')==0) {
  621. $schema['tripal_obo_temp'] = array(
  622. 'table' => 'tripal_obo_temp',
  623. 'fields' => array(
  624. 'id' => array(
  625. 'type' => 'varchar',
  626. 'length' => '255',
  627. 'not null' => TRUE,
  628. ),
  629. 'stanza' => array(
  630. 'type' => 'text',
  631. 'not null' => TRUE,
  632. ),
  633. 'type' => array(
  634. 'type' => 'varchar',
  635. 'length' => '50',
  636. 'not null' => TRUE,
  637. ),
  638. ),
  639. 'indexes' => array(
  640. 'tripal_obo_temp_idx0' => array('id'),
  641. 'tripal_obo_temp_idx0' => array('type'),
  642. ),
  643. 'unique keys' => array(
  644. 'tripal_obo_temp_uq0' => array('id'),
  645. ),
  646. );
  647. }
  648. return $schema;
  649. }