tripal_chado.analysis.api.inc 5.3 KB

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