tripal_core.chado_general.api.inc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) to manage data withing the Chado database.
  5. */
  6. require_once 'tripal_core.schema_v1.2.api.inc';
  7. require_once 'tripal_core.schema_v1.11.api.inc';
  8. /**
  9. * @defgroup tripal_chado_api Chado API
  10. * @ingroup tripal_core_api
  11. * @{
  12. * Provides an application programming interface (API) to manage data withing the Chado database.
  13. *
  14. * This includes functions for selecting, inserting, updating and deleting records
  15. * in Chado tables. The functions will ensure proper integrity contraints are met
  16. * for inserts and updates.
  17. *
  18. * Also, a set of functions is provided for creating template variables. First,
  19. * is the tripal_core_generate_chado_vars which is used to select one ore more
  20. * records from a table and return an array with foreign key relationships fully
  21. * populated. For example, if selecting a feature, the organism_id and type_id
  22. * would be present in the returned array as a nested array with their respective
  23. * foreign keys also nested. The only fields that are not included are text
  24. * fields (which may be very large) or many-to-many foreign key relationships.
  25. * However, these fields and relationships can be expanded using the
  26. * chado_expand_var.
  27. *
  28. * When a row from a chado table is selected using these two functions, it provides
  29. * a way for users who want to cutomize Drupal template files to access all data
  30. * associate with a specific record.
  31. *
  32. * Finally, the property tables in Chado generally follow the same format. Therefore
  33. * there is a set of functions for inserting, updating and deleting properties for
  34. * any table. This provides quick lookup of properties (provided the CV term is
  35. * known).
  36. *
  37. * @}
  38. *
  39. */
  40. // Globals used by Tripals Error catching functions
  41. // Should match those defined by watchdog
  42. define('TRIPAL_CRITICAL',2);
  43. define('TRIPAL_ERROR',3);
  44. define('TRIPAL_WARNING',4);
  45. define('TRIPAL_NOTICE',5);
  46. define('TRIPAL_INFO',6);
  47. define('TRIPAL_DEBUG',7);
  48. /**
  49. * Provide better error notice for Tripal
  50. * @param $type
  51. * The catagory to which this message belongs. Can be any string, but the general
  52. * practice is to use the name of the module.
  53. * @param $message
  54. * The message to store in the log. Keep $message translatable by not concatenating
  55. * dynamic values into it! Variables in the message should be added by using placeholder
  56. * strings alongside the variables argument to declare the value of the placeholders.
  57. * See t() for documentation on how $message and $variables interact.
  58. * @param $variables
  59. * Array of variables to replace in the message on display or NULL if message is
  60. * already translated or not possible to translate.
  61. * @param $severity
  62. * The severity of the message; one of the following values:
  63. * - TRIPAL_CRITICAL: Critical conditions.
  64. * - TRIPAL_ERROR: Error conditions.
  65. * - TRIPAL_WARNING: Warning conditions.
  66. * - TRIPAL_NOTICE: (default) Normal but significant conditions.
  67. * - TRIPAL_INFO: Informational messages.
  68. * - TRIPAL_DEBUG: Debug-level messages.
  69. * @param $options
  70. * An array of options. Some available options include:
  71. * - print: prints the error message to the terminal screen. Useful when display is the command-line
  72. *
  73. * @ingroup tripal_chado_api
  74. */
  75. function tripal_report_error($type, $severity, $message, $variables = array(), $options = array()) {
  76. // Get human-readable severity string
  77. $severity_string = '';
  78. switch ($severity) {
  79. case TRIPAL_CRITICAL:
  80. $severity_string = 'CRITICAL';
  81. break;
  82. case TRIPAL_ERROR:
  83. $severity_string = 'ERROR';
  84. break;
  85. case TRIPAL_WARNING:
  86. $severity_string = 'WARNING';
  87. break;
  88. case TRIPAL_NOTICE:
  89. $severity_string = 'NOTICE';
  90. break;
  91. case TRIPAL_INFO:
  92. $severity_string = 'INFO';
  93. break;
  94. case TRIPAL_DEBUG:
  95. $severity_string = 'DEBUG';
  96. break;
  97. }
  98. // Send to watchdog
  99. try {
  100. watchdog($type, $message, $variables, $severity);
  101. }
  102. catch (Exception $e) {
  103. print "CRITICAL (TRIPAL_CORE): Unable to register error message with watchdog";
  104. $options['print'] = TRUE;
  105. }
  106. // If print option supplied then print directly to the screen
  107. if (isset($options['print'])) {
  108. if (sizeof($variables) > 0) {
  109. $message = str_replace(array_keys($variables), $variables, $message);
  110. }
  111. print $severity_string . ' (' . strtoupper($type) . '):' . $message . "\n";
  112. }
  113. }
  114. /**
  115. * Get chado id for a node. E.g, if you want to get 'analysis_id' from the
  116. * 'analysis' table for a synced 'chado_analysis' node, (the same for
  117. * organisms and features):
  118. * $analysis_id = chado_get_id_from_nid ('analysis', $node->nid)
  119. * $organism_id = chado_get_id_from_nid ('organism', $node->nid)
  120. * $feature_id = chado_get_id_from_nid ('feature', $node->nid)
  121. *
  122. * @param $table
  123. * @param $nid
  124. *
  125. * @ingroup tripal_chado_api
  126. */
  127. function chado_get_id_from_nid($table, $nid) {
  128. $sql = "SELECT " . $table . "_id as id FROM {chado_$table} WHERE nid = :nid";
  129. return db_query($sql, array(':nid' => $nid))->fetchField();
  130. }
  131. /**
  132. * Get node id for a chado feature/organism/analysis. E.g, if you want to
  133. * get the node id for an analysis, use:
  134. * $nid = chado_get_nid_from_id ('analysis', $analysis_id)
  135. * Likewise,
  136. * $nid = chado_get_nid_from_id ('organism', $organism_id)
  137. * $nid = chado_get_nid_from_id ('feature', $feature_id)
  138. *
  139. * @ingroup tripal_chado_api
  140. */
  141. function chado_get_nid_from_id($table, $id) {
  142. $sql = "SELECT nid FROM {chado_$table} WHERE " . $table . "_id = :" . $table . "_id";
  143. return db_query($sql, array(":" . $table . "_id" => $id))->fetchField();
  144. }
  145. /**
  146. * Set the Tripal Database
  147. *
  148. * The chado_set_active function is used to prevent namespace collisions
  149. * when chado and drupal are installed in the same database but in different
  150. * schemas. It is also used for backwards compatibility with older versions
  151. * of tripal or in cases where chado is located outside of the Drupal database.
  152. * or when using Drupal functions such as db_table_exists()
  153. *
  154. * @ingroup tripal_chado_api
  155. */
  156. function chado_set_active($dbname = 'default') {
  157. global $databases, $active_db;
  158. if ($dbname ) {
  159. if ($dbname == 'chado') {
  160. db_query('set search_path to chado,public');
  161. return 'default';
  162. }
  163. else {
  164. db_query('set search_path to public');
  165. return 'chado';
  166. }
  167. }
  168. // if the 'chado' database is in the $db_url variable then chado is
  169. // not in the same Drupal database, so we don't need to set any
  170. // search_path and can just change the database
  171. elseif (array_key_exists($dbname, $databases)) {
  172. return db_set_active($dbname);
  173. }
  174. }
  175. /**
  176. * Get max rank for a given set of criteria
  177. * This function was developed with the many property tables in chado in mind but will
  178. * work for any table with a rank
  179. *
  180. * @params tablename: the name of the chado table you want to select the max rank from
  181. * this table must contain a rank column of type integer
  182. * @params where_options: array(
  183. * <column_name> => array(
  184. * 'type' => <type of column: INT/STRING>,
  185. * 'value' => <the value you want to filter on>,
  186. * 'exact' => <if TRUE use =; if FALSE use ~>,
  187. * )
  188. * )
  189. * where options should include the id and type for that table to correctly
  190. * group a set of records together where the only difference are the value and rank
  191. * @return the maximum rank
  192. *
  193. * @ingroup tripal_chado_api
  194. */
  195. function chado_get_table_max_rank($tablename, $where_options) {
  196. $where_clauses = array();
  197. $where_args = array();
  198. //generate the where clause from supplied options
  199. // the key is the column name
  200. $i = 0;
  201. $sql = "
  202. SELECT max(rank) as max_rank, count(rank) as count
  203. FROM {".$tablename."}
  204. WHERE
  205. ";
  206. foreach ($where_options as $key => $value) {
  207. $where_clauses[] = "$key = :$key";
  208. $where_args[":$key"] = $value;
  209. }
  210. $sql .= implode($where_clauses, ' AND ');
  211. $result = chado_query($sql, $where_args)->fetchObject();
  212. if ($result->count > 0) {
  213. return $result->max_rank;
  214. }
  215. else {
  216. return -1;
  217. }
  218. }
  219. /**
  220. * Use this function to encapsulate text intended to be
  221. * visible only by the site administrator. A small tripal logo
  222. * appears alongside the text. Do not call this function directly, but
  223. * rather, use the theme() function:
  224. *
  225. * theme('tripal_admin_message', array('message' => $my_message));
  226. *
  227. * @param $message
  228. * The message to be displayed to the site administrator
  229. *
  230. * @ingroup tripal_chado_api
  231. */
  232. function theme_tripal_admin_message($variables) {
  233. $message = $variables['message'];
  234. if (!user_access('access administration pages')) {
  235. return '';
  236. }
  237. return "
  238. <div class=\"tripal-site-admin-only\">
  239. <div class=\"tripal-site-admin-message\">$message</div>
  240. </div>";
  241. }