tripal_cv.api.inc 27 KB

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