tripal_core.chado_general.api.inc 8.7 KB

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