tripal_chado.analysis.api.inc 5.4 KB

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