tripal_chado.analysis.api.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * @file
  4. * Provides functions for managing analysis'.
  5. *
  6. * @ingroup tripal_analysis
  7. */
  8. /**
  9. * @defgroup tripal_analysis_api Analysis Module API
  10. * @ingroup tripal_api
  11. * @{
  12. * Provides an interface for specialized analysis modules to extend the basic functionality.
  13. * @}
  14. */
  15. /**
  16. * Register tripal_analysis_api sub-modules
  17. *
  18. * @param $modulename
  19. * The name of the module to be registered as a tripal analysis submodule
  20. *
  21. * @ingroup tripal_analysis_api
  22. */
  23. function tripal_register_analysis_child($modulename) {
  24. $sql = "SELECT * FROM {tripal_analysis} WHERE modulename = :modname";
  25. if (!db_query($sql, array(':modname' => $modulename))->fetchField()) {
  26. $sql = "INSERT INTO {tripal_analysis} (modulename) VALUES (:modname)";
  27. db_query($sql, array(':modname' => $modulename));
  28. }
  29. }
  30. /**
  31. * Un-register a tripal analysis sub-module
  32. *
  33. * @param $modulename
  34. * The name of the module to un-register
  35. *
  36. * @ingroup tripal_analysis_api
  37. */
  38. function tripal_unregister_analysis_child($modulename) {
  39. if (db_table_exists('tripal_analysis')) {
  40. $sql = "DELETE FROM {tripal_analysis} WHERE modulename = :modname";
  41. db_query($sql, array(':modname' => $modulename));
  42. }
  43. }
  44. /**
  45. * Retrieves an chado analysis variable
  46. *
  47. * @param $itentifier
  48. * an array with the key stating what the identifier is. Supported keys (only on of the
  49. * following unique keys is required):
  50. * - analysis_id: the chado analysis.analysis_id primary key
  51. * - nid: the drupal node.nid primary key
  52. * There are also some specially handled keys. They are:
  53. * - property: An array/object describing the property to select records for. It
  54. * should at least have either a type_name (if unique across cvs) or type_id. Other
  55. * supported keys include: cv_id/cv_name (of the type), value and rank
  56. * @param $options
  57. * An array of options. Supported keys include:
  58. * - Any keys supported by chado_generate_var(). See that function definition for
  59. * additional details.
  60. *
  61. * NOTE: the $identifier parameter can really be any array similar to $values passed into
  62. * chado_select_record(). It should fully specify the stock record to be returned.
  63. *
  64. * @return
  65. * the analysis node matching the passed in identifier
  66. *
  67. * @ingroup tripal_analysis_api
  68. */
  69. function tripal_get_analysis($identifier, $options) {
  70. // Set Defaults
  71. if (!isset($options['include_fk'])) {
  72. // Tells chado_generate_var not to follow any foreign keys
  73. $options['include_fk'] = array();
  74. }
  75. // Error Checking of parameters
  76. if (!is_array($identifiers)) {
  77. tripal_report_error(
  78. 'tripal_stock_api',
  79. TRIPAL_ERROR,
  80. "chado_get_stock: The identifier passed in is expected to be an array with the key
  81. matching a column name in the analysis table (ie: analysis_id or name). You passed in %identifier.",
  82. array(
  83. '%identifier'=> print_r($identifiers, TRUE)
  84. )
  85. );
  86. }
  87. elseif (empty($identifiers)) {
  88. tripal_report_error(
  89. 'tripal_stock_api',
  90. TRIPAL_ERROR,
  91. "chado_get_stock: You did not pass in anything to identify the analysis you want. The identifier
  92. is expected to be an array with the key matching a column name in the analysis table
  93. (ie: stock_id or name). You passed in %identifier.",
  94. array(
  95. '%identifier'=> print_r($identifiers, TRUE)
  96. )
  97. );
  98. }
  99. // If one of the identifiers is property then use chado_get_record_with_property()
  100. if (isset($identifiers['property'])) {
  101. $property = $identifiers['property'];
  102. unset($identifiers['property']);
  103. $analysis = chado_get_record_with_property(
  104. array('table' => 'analysis', 'base_records' => $identifiers),
  105. array('type_name' => $property)
  106. );
  107. }
  108. // Else we have a simple case and we can just use chado_generate_var to get the analysis
  109. else {
  110. // Try to get the analysis
  111. $analysis = chado_generate_var(
  112. 'analysis',
  113. $identifiers,
  114. $options
  115. );
  116. }
  117. // Ensure the analysis is singular. If it's an array then it is not singular
  118. if (is_array($analysis)) {
  119. tripal_report_error(
  120. 'tripal_analysis_api',
  121. TRIPAL_ERROR,
  122. "tripal_get_analysis: The identifiers you passed in were not unique. You passed in %identifier.",
  123. array(
  124. '%identifier'=> print_r($identifiers, TRUE)
  125. )
  126. );
  127. }
  128. // Report an error if $analysis is FALSE since then chado_generate_var has failed
  129. elseif ($analysis === FALSE) {
  130. tripal_report_error(
  131. 'tripal_analysis_api',
  132. TRIPAL_ERROR,
  133. "tripal_get_analysis: chado_generate_var() failed to return a analysis based on the identifiers
  134. you passed in. You should check that your identifiers are correct, as well as, look
  135. for a chado_generate_var error for additional clues. You passed in %identifier.",
  136. array(
  137. '%identifier'=> print_r($identifiers, TRUE)
  138. )
  139. );
  140. }
  141. // Else, as far we know, everything is fine so give them their analysis :)
  142. else {
  143. return $analysis;
  144. }
  145. }
  146. /**
  147. * Returns a list of analyses that are currently synced with Drupal to use in select lists
  148. *
  149. * @param $syncd_only
  150. * Whether or not to return all chado analyses or just those sync'd with drupal. Defaults
  151. * to TRUE (only sync'd analyses)
  152. * @return
  153. * An array of analyses sync'd with Drupal where each value is the analysis scientific
  154. * name and the keys are analysis_id's
  155. *
  156. * @ingroup tripal_analysis_api
  157. */
  158. function tripal_get_analysis_select_options($syncd_only = TRUE) {
  159. $analysis_list = array();
  160. $analysis_list[] = 'Select an analysis';
  161. if ($syncd_only) {
  162. $sql = "
  163. SELECT *
  164. FROM public.chado_analysis CA
  165. INNER JOIN {analysis} A ON A.analysis_id = CO.analysis_id
  166. ORDER BY A.name
  167. ";
  168. $orgs = chado_query($sql);
  169. // iterate through the analyses and build an array of those that are synced
  170. foreach ($analyses as $analysis) {
  171. $analysis_list[$analysis->analysis_id] = $analysis->name;
  172. }
  173. }
  174. else {
  175. // use this SQL statement for getting the analyses
  176. $csql = "SELECT * FROM {analysis} ORDER BY name";
  177. $analyses = chado_query($csql);
  178. // iterate through the analyses and build an array of those that are synced
  179. foreach ($analyses as $analysis) {
  180. $analysis_list[$analysis->analysis_id] = $analysis->name;
  181. }
  182. }
  183. return $analysis_list;
  184. }