tripal_cv.api.inc 28 KB

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