tripal_cv.api.inc 27 KB

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