tripal_cv.api.inc 27 KB

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