tripal_chado.analysis.api.inc 5.2 KB

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