tripal_cv.api.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <?php
  2. /**
  3. * @file
  4. * Controlled Vocabulary API
  5. *
  6. * @defgroup tripal_cv_api CV Module API
  7. * @ingroup tripal_api
  8. * @ingroup tripal_cv
  9. * This module provides a set of functions to simplify working with
  10. * controlled vocabularies. Most of the API functions deal with retrieving
  11. * terms or their parent vocabularies.
  12. *
  13. * However, the API also supports
  14. * generation of trees for browsing a vocabulary as well as generation of
  15. * pie graphs for display of hierarchical counts of terms. Version 0.3b of
  16. * Tripal provides a feature browser and a feature summary chart uses
  17. * the API functions provided here. But in general charts and trees can be
  18. * created for any controlled vocabulary.
  19. *
  20. */
  21. /**
  22. * Purpose: To retrieve a chado controlled vocabulary object
  23. *
  24. * @param $select_values
  25. * An array meant to uniquely select a given controlled vocabulary
  26. *
  27. * @return
  28. * Chado controlled vocabulary object
  29. *
  30. * The controlled vocabulary is selected using tripal_core_chado select and as such the
  31. * $select_values array parameter meant to uniquely identify the controlled vocab to be
  32. * returned follows the same form as when using tripal_core_chado_select directly.
  33. *
  34. * Example Usage:
  35. * @code
  36. $select_values = array(
  37. 'name' => 'feature_property'
  38. );
  39. $cv_object = tripal_cv_get_cv($select_values);
  40. * @endcode
  41. * The above code selects the feature_property cv and returns the following object:
  42. * @code
  43. $cv_object = stdClass Object (
  44. [cv_id] => 13
  45. [name] => feature_property
  46. [definition] =>
  47. );
  48. * @endcode
  49. *
  50. * @ingroup tripal_cv_api
  51. */
  52. function tripal_cv_get_cv($select_values) {
  53. $columns = array(
  54. 'cv_id',
  55. 'name',
  56. 'definition',
  57. );
  58. $results = tripal_core_chado_select('cv', $columns, $select_values);
  59. if (sizeof($results) == 1) {
  60. return $results[0];
  61. }
  62. elseif (empty($results)) {
  63. watchdog('tripal_cv',
  64. 'tripal_cv_get_cv: No cv matches criteria values:%values',
  65. array('%values' => print_r($select_values, TRUE)),
  66. WATCHDOG_WARNING
  67. );
  68. return FALSE;
  69. }
  70. else {
  71. watchdog('tripal_cv',
  72. 'tripal_cv_get_cv: 2+ cvs match criteria values:%values',
  73. array('%values' => print_r($select_values, TRUE)),
  74. WATCHDOG_WARNING
  75. );
  76. }
  77. }
  78. // Purpose: To retrieve a chado cv object
  79. // @param $where_options
  80. // @code
  81. // array(
  82. // <column_name> => array(
  83. // 'type' => <type of column: INT/STRING>,
  84. // 'value' => <the vlaue you want to filter on>,
  85. // 'exact' => <if TRUE use =; if FALSE use ~>,
  86. // )
  87. // )
  88. // @endcode
  89. //
  90. // @return
  91. // Chado cv object with all fields from the chado cv table
  92. //
  93. // @ingroup tripal_cv_api
  94. //
  95. //function tripal_cv_get_cv ($where_options)
  96. /**
  97. * Retrieve a cv given the cv name
  98. *
  99. * @param $name
  100. * The name of the cv to be returned
  101. * @return
  102. * The cv object for the specified CV name
  103. *
  104. * @ingroup tripal_cv_api
  105. */
  106. function tripal_cv_get_cv_by_name($name) {
  107. $previous_db = tripal_db_set_active('chado');
  108. $r = tripal_core_chado_select('cv', array('*'), array('name' => $name));
  109. tripal_db_set_active($previous_db);
  110. return $r[0];
  111. }
  112. /**
  113. * Retrieve the cv object for the specified CV id
  114. *
  115. * NOTE: This function is deprecated.
  116. * @see tripal_core_chado_generate_vars()
  117. *
  118. * @param $cv_id
  119. * The unique identifier for the cv retrieve
  120. *
  121. * @return
  122. * An object describing the cv
  123. *
  124. * @ingroup tripal_cv_api
  125. */
  126. function tripal_cv_get_cv_by_id($cv_id) {
  127. $previous_db = tripal_db_set_active('chado');
  128. $r = tripal_core_chado_select('cv', array('*'), array('cv_id' => $cv_id));
  129. tripal_db_set_active($previous_db);
  130. return $r;
  131. }
  132. /**
  133. * Create an options array to be used in a form element which provides a list of all chado cvs
  134. *
  135. * @return
  136. * An array(cv_id => name) for each cv in the chado cv table
  137. *
  138. * @ingroup tripal_cv_api
  139. */
  140. function tripal_cv_get_cv_options() {
  141. $results = tripal_core_chado_select('cv', array('cv_id', 'name'), array());
  142. $options = array();
  143. foreach ($results as $r) {
  144. $options[$r->cv_id] = $r->name;
  145. }
  146. return $options;
  147. }
  148. /**
  149. * Retrieve a chado cvterm object with a given name
  150. *
  151. * @param $name
  152. * the cvterm.name
  153. * @param $cv_id
  154. * the cv_id of the term you are looking for
  155. * @param $cv_name
  156. * the name of the CV
  157. *
  158. * @return
  159. * cvterm object
  160. *
  161. * @ingroup tripal_cv_api
  162. */
  163. function tripal_cv_get_cvterm_by_name($name, $cv_id = 0, $cv_name = 'tripal') {
  164. if ($cv_id) {
  165. $values = array(
  166. 'name' => $name,
  167. 'cv_id' => $cv_id,
  168. );
  169. $r = tripal_core_chado_select('cvterm', array('*'), $values);
  170. }
  171. elseif ($cv_name) {
  172. $values = array(
  173. 'name' => $name,
  174. 'cv_id' => array(
  175. 'name' => $cv_name,
  176. ),
  177. );
  178. $r = tripal_core_chado_select('cvterm', array('*'), $values);
  179. }
  180. else {
  181. $values = array(
  182. 'name' => $name,
  183. );
  184. $r = tripal_core_chado_select('cvterm', array('*'), $values);
  185. }
  186. if (!$r) {
  187. return FALSE;
  188. }
  189. if (count($r) > 0) {
  190. return FALSE;
  191. }
  192. return $r[0];
  193. }
  194. /**
  195. * Create an options array to be used in a form element
  196. * which provides a list of all chado cvterms
  197. *
  198. * @param $cv_id
  199. * The chado cv_id;
  200. * only cvterms with the supplied cv_id will be returned
  201. * @return
  202. * An array(cvterm_id => name)
  203. * for each cvterm in the chado cvterm table where cv_id=that supplied
  204. *
  205. * @ingroup tripal_cv_api
  206. */
  207. function tripal_cv_get_cvterm_options($cv_id = 0) {
  208. if ($cv_id > 0) {
  209. $results = tripal_core_chado_select('cvterm', array('cvterm_id', 'name'), array('cv_id' => $cv_id));
  210. }
  211. else {
  212. $results = tripal_core_chado_select('cvterm', array('cvterm_id', 'name'), array());
  213. }
  214. $options = array();
  215. foreach ($results as $r) {
  216. $options[$r->cvterm_id] = $r->name;
  217. }
  218. return $options;
  219. }
  220. /**
  221. * Adds a controlled vocabular to the CV table of Chado.
  222. *
  223. * @param $name
  224. * The name of the controlled vocabulary. These are typically all lower case
  225. * with no special characters other than an undrescore (for spaces).
  226. * @param $comment
  227. * A description or definition of the vocabulary.
  228. *
  229. * @return
  230. * An object populated with fields from the newly added database.
  231. *
  232. * @ingroup tripal_cv_api
  233. */
  234. function tripal_cv_add_cv($name, $comment) {
  235. // see if the CV (default-namespace) exists already in the database
  236. $vocab = $name;
  237. $remark = $comment;
  238. $cv_sql = "SELECT * FROM {cv} WHERE name = '%s'";
  239. $cv = db_fetch_object(db_query($cv_sql, $vocab));
  240. // if the CV exists then update it, otherwise insert
  241. if (!$cv) {
  242. $sql = "INSERT INTO {cv} (name,definition) VALUES ('%s','%s')";
  243. if (!db_query($sql, $vocab, $remark)) {
  244. watchdog('tripal_cv', "Failed to create the CV record", NULL, WATCHDOG_WARNING);
  245. return FALSE;
  246. }
  247. $cv = db_fetch_object(db_query($cv_sql, $vocab));
  248. }
  249. else {
  250. $sql = "UPDATE {cv} SET definition = '%s' WHERE name ='%s'";
  251. if (!db_query($sql, $remark, $vocab)) {
  252. watchdog('tripal_cv', "Failed to update the CV record", NULL, WATCHDOG_WARNING);
  253. return FALSE;
  254. }
  255. $cv = db_fetch_object(db_query($cv_sql, $vocab));
  256. }
  257. return $cv;
  258. }
  259. /**
  260. * Add's a CV term to the cvterm table. If the parent CV does not exist then
  261. * that too is added to the CV table. If the cvterm is a relationship term
  262. * then the $is_relationship argument should be set. The function will try
  263. * to first find the relationship in the relationship ontology for updating and
  264. * if it can't be found will add the relationship to the __global CV. All terms
  265. * must also have a corresponding database. This is specified in the term's
  266. * ID just before the colon (e.g. GO:003824). If the database does not exist
  267. * in the DB table then it will be added automatically. The accession (the
  268. * value just after the colon in the term's ID) will be added to the dbxref
  269. * table. If the CVterm already exists and $update is set (default) then the
  270. * cvterm is updated. If the CVTerm already exists and $update is not set, then
  271. * no changes are made and the CVTerm object is returned.
  272. *
  273. * @param $term
  274. * An associative array with the following keys: 'id', 'name' and 'namespace',
  275. * 'is_obsolete', and 'def'. Where 'id' is the term accession, 'name' is the
  276. * term name, 'namespace' is the CV name for the term, 'def' is the term
  277. * definition and 'is_obsolete' is present and set to 1 if the term is defunct.
  278. * The 'id' must be of the form <DB>:<ACCESSION>, where <DB> is the name of
  279. * the database to which the cvterm belongs and the <ACCESSION> is the
  280. * term's accession number in the database.
  281. * @param $defaultcv
  282. * Optional. The CV name to which the term
  283. * belongs. If this arugment is null or not provided then the function tries
  284. * to find a record in the CV table with the same name provided in the
  285. * $term[namespace]. If this field is provided then it overrides what the
  286. * value in $term[namespace]
  287. * @param $is_relationship
  288. * If this term is a relationship term then this value should be 1.
  289. * @param $update
  290. * By default this is set to 1. If the term exists it is automatically updated.
  291. * @param $dbname
  292. * In some cases the database name will not be part of the $term['id'] and it
  293. * needs to be explicitly set. Use this argument only if the database name
  294. * cannot be specififed in the term ID (e.g. <DB>:<ACCESSION>).
  295. *
  296. * @return
  297. * A CVTerm object
  298. *
  299. * @ingroup tripal_cv_api
  300. */
  301. function tripal_cv_add_cvterm($term, $defaultcv='', $is_relationship = 0, $update = 1, $dbname=NULL) {
  302. // get the term properties
  303. $id = $term['id'];
  304. $name = $term['name'];
  305. $cvname = $term['namespace'];
  306. $definition = preg_replace('/^\"(.*)\"/', '\1', $term['def']);
  307. $is_obsolete = 0;
  308. if (isset($term['is_obsolete']) and strcmp($term['is_obsolete'], 'true') == 0) {
  309. $is_obsolete = 1;
  310. }
  311. if (!$name and !$id) {
  312. watchdog('tripal_cv', "Cannot find cvterm without 'id' or 'name'", NULL, WATCHDOG_WARNING);
  313. return 0;
  314. }
  315. if (!$id) {
  316. $id = $name;
  317. }
  318. if (!$name) {
  319. $name = $id;
  320. }
  321. if (!$cvname) {
  322. $cvname = $defaultcv;
  323. }
  324. // make sure the CV name exists
  325. $cv = tripal_cv_add_cv($cvname, '');
  326. if (!$cv) {
  327. watchdog('tripal_cv', "Cannot find namespace '$cvname' when adding/updating $id", NULL, WATCHDOG_WARNING);
  328. return 0;
  329. }
  330. // this SQL statement will be used a lot to find a cvterm so just set it
  331. // here for easy reference below. Because CV terms can change their names
  332. // but accessions don't change, the following SQL finds cvterms based on
  333. // their accession rather than the name
  334. $cvtermsql = "SELECT CVT.name, CVT.cvterm_id, DB.name as dbname, DB.db_id
  335. FROM {cvterm} CVT
  336. INNER JOIN {dbxref} DBX on CVT.dbxref_id = DBX.dbxref_id
  337. INNER JOIN {db} DB on DBX.db_id = DB.db_id
  338. INNER JOIN {cv} CV on CV.cv_id = CVT.cv_id
  339. WHERE DBX.accession = '%s' and DB.name = '%s'";
  340. // get the accession and the database from the cvterm
  341. if ($dbname) {
  342. $accession = $id;
  343. }
  344. elseif (preg_match('/^.+?:.*$/', $id)) {
  345. $accession = preg_replace('/^.+?:(.*)$/', '\1', $id);
  346. $dbname = preg_replace('/^(.+?):.*$/', '\1', $id);
  347. }
  348. if ($is_relationship and !$dbname) {
  349. $accession = $id;
  350. // because this is a relationship cvterm first check to see if it
  351. // exists in the relationship ontology. If it does then return the cvterm.
  352. // If not then set the dbname to _global and we'll add it or find it there
  353. $cvterm = db_fetch_object(db_query($cvtermsql, $accession, 'OBO_REL'));
  354. if ($cvterm) {
  355. return $cvterm;
  356. }
  357. else {
  358. // next check if this term is in the _global ontology. If it is then
  359. // return it no matter what the original CV
  360. $dbname = '_global';
  361. $cvterm = db_fetch_object(db_query($cvtermsql, $accesion, $dbname));
  362. if ($cvterm) {
  363. return $cvterm;
  364. }
  365. }
  366. }
  367. if (!$is_relationship and !$dbname) {
  368. watchdog('tripal_cv', "A database identifier is missing from the term: $id", NULL, WATCHDOG_WARNING);
  369. return 0;
  370. }
  371. // add the database. The function will just return the DB object if the
  372. // database already exists.
  373. $db = tripal_db_add_db($dbname);
  374. if (!$db) {
  375. watchdog('tripal_cv', "Cannot find database '$dbname' in Chado.", NULL, WATCHDOG_WARNING);
  376. return 0;
  377. }
  378. // if the cvterm doesn't exist then add it otherwise just update it
  379. $cvterm = db_fetch_object(db_query($cvtermsql, $accession, $dbname));
  380. if (!$cvterm) {
  381. // check to see if the dbxref exists if not, add it
  382. $dbxref = tripal_db_add_dbxref($db->db_id, $accession);
  383. if (!$dbxref) {
  384. watchdog('tripal_cv', "Failed to find or insert the dbxref record for cvterm, $name (id: $accession), for database $dbname", NULL, WATCHDOG_WARNING);
  385. return 0;
  386. }
  387. // check to see if the dbxref already has an entry in the cvterm table
  388. $sql = "SELECT * FROM {cvterm} WHERE dbxref_id = %d";
  389. $check = db_fetch_object(db_query($sql, $dbxref->dbxref_id));
  390. if (!$check) {
  391. // now add the cvterm
  392. $sql = "
  393. INSERT INTO {cvterm} (cv_id, name, definition, dbxref_id,
  394. is_obsolete, is_relationshiptype)
  395. VALUES (%d,'%s','%s',%d,%d,%d)
  396. ";
  397. if (!db_query($sql, $cv->cv_id, $name, $definition, $dbxref->dbxref_id, $is_obsolete, $is_relationship)) {
  398. if (!$is_relationship) {
  399. watchdog('tripal_cv', "Failed to insert the term: $name ($dbname)", NULL, WATCHDOG_WARNING);
  400. return 0;
  401. }
  402. else {
  403. watchdog('tripal_cv', "Failed to insert the relationship term: $name (cv: " . $cvname . " db: $dbname)", NULL, WATCHDOG_WARNING);
  404. return 0;
  405. }
  406. }
  407. }
  408. // if the dbxref already exists check to make sure it exists for the correct databaes name
  409. // if it does then we're good and we don't need to do anything
  410. elseif ($check and strcmp($check->name, $name) == 0) {
  411. // this entry already exists. We're good, so do nothing
  412. }
  413. // if the dbxref exists but does not map to the same database name
  414. elseif ($check and strcmp($check->name, $name) != 0) {
  415. watchdog(
  416. 'tripal_cv',
  417. "The dbxref already exists in the cvterm table. DBXREF ID: $dbxref->dbxref_id, ACCESSION: $accession. DB: '" . $dbxref->db_name . "'. term '$name'. The requested db was '$dbname'",
  418. NULL,
  419. WATCHDOG_WARNING);
  420. return FALSE;
  421. }
  422. $cvterm = db_fetch_object(db_query($cvtermsql, $name, $dbname));
  423. if (!$is_relationship) {
  424. print "Added CV term: $name ($dbname)\n";
  425. }
  426. else {
  427. print "Added relationship CV term: $name ($dbname)\n";
  428. }
  429. }
  430. elseif ($update) { // update the cvterm
  431. $sql = "
  432. UPDATE {cvterm} SET name='%s', definition='%s',
  433. is_obsolete = %d, is_relationshiptype = %d
  434. WHERE cvterm_id = %d
  435. ";
  436. if (!db_query($sql, $term['name'], $definition, $is_obsolete, $is_relationship, $cvterm->cvterm_id)) {
  437. watchdog('tripal_cv', "Failed to update the term: $name", NULL, WATCHDOG_WARNING);
  438. return FALSE;
  439. }
  440. $cvterm = db_fetch_object(db_query($cvtermsql, $name, $dbname));
  441. if (!$is_relationship) {
  442. print "Updated CV term: $name ($dbname)\n";
  443. }
  444. else {
  445. print "Updated relationship CV term: $name ($dbname)\n";
  446. }
  447. }
  448. // return the cvterm
  449. return $cvterm;
  450. }