tripal_cv.api.inc 27 KB

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