tripal_analysis.api.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_analysis_register_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_analysis_unregister_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 analysis_retrieve($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 stock table (ie: stock_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 stock you want. The identifier
  92. is expected to be an array with the key matching a column name in the stock 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('analysis', $property, $identifiers, $options);
  104. }
  105. // Else we have a simple case and we can just use chado_generate_var to get the analysis
  106. else {
  107. // Try to get the analysis
  108. $analysis = chado_generate_var(
  109. 'analysis',
  110. $identifiers,
  111. $options
  112. );
  113. }
  114. // Ensure the analysis is singular. If it's an array then it is not singular
  115. if (is_array($analysis)) {
  116. tripal_report_error(
  117. 'tripal_analysis_api',
  118. TRIPAL_ERROR,
  119. "analysis_retrieve: The identifiers you passed in were not unique. You passed in %identifier.",
  120. array(
  121. '%identifier'=> print_r($identifiers, TRUE)
  122. )
  123. );
  124. }
  125. // Report an error if $analysis is FALSE since then chado_generate_var has failed
  126. elseif ($analysis === FALSE) {
  127. tripal_report_error(
  128. 'tripal_analysis_api',
  129. TRIPAL_ERROR,
  130. "analysis_retrieve: chado_generate_var() failed to return a analysis based on the identifiers
  131. you passed in. You should check that your identifiers are correct, as well as, look
  132. for a chado_generate_var error for additional clues. You passed in %identifier.",
  133. array(
  134. '%identifier'=> print_r($identifiers, TRUE)
  135. )
  136. );
  137. }
  138. // Else, as far we know, everything is fine so give them their analysis :)
  139. else {
  140. return $analysis;
  141. }
  142. }