tripal_chado.analysis.api.inc 5.3 KB

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