tripal_cv.api.inc 28 KB

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