tripal_organism.api.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) to manage organisms
  5. */
  6. /**
  7. * @defgroup tripal_organism_api Organism API
  8. * @ingroup tripal_api
  9. * @{
  10. * Provides an application programming interface (API) to manage organisms
  11. * @}
  12. */
  13. /**
  14. * Retrieves a chado organism variable
  15. *
  16. * @param $identifier
  17. * An array with the key stating what the identifier is. Supported keys (only on of the
  18. * following unique keys is required):
  19. * - organism_id: the chado organism.organism_id primary key
  20. * - genus & species: the chado organism.genus field & organism.species field
  21. * There are also some specially handled keys. They are:
  22. * - property: An array/object describing the property to select records for. It
  23. * should at least have either a type_name (if unique across cvs) or type_id. Other
  24. * supported keys include: cv_id/cv_name (of the type), value and rank
  25. * @param $options
  26. * An array of options. Supported keys include:
  27. * - Any keys supported by chado_generate_var(). See that function definition for
  28. * additional details.
  29. *
  30. * NOTE: the $identifier parameter can really be any array similar to $values passed into
  31. * chado_select_record(). It should fully specify the organism record to be returned.
  32. *
  33. * @return
  34. * If unique values were passed in as an identifier then an object describing the organism
  35. * will be returned (will be a chado variable from chado_generate_var()). Otherwise,
  36. * FALSE will be returned.
  37. *
  38. * @ingroup tripal_organism_api
  39. */
  40. function tripal_get_organism($identifiers, $options = array()) {
  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_organism_api',
  50. TRIPAL_ERROR,
  51. "tripal_get_organism: The identifier passed in is expected to be an array with the key
  52. matching a column name in the organism table (ie: organism_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_organism_api',
  61. TRIPAL_ERROR,
  62. "tripal_get_organism: You did not pass in anything to identify the organism you want. The identifier
  63. is expected to be an array with the key matching a column name in the organism table
  64. (ie: organism_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. $organism = chado_get_record_with_property('organism', $property, $identifiers, $options);
  75. }
  76. // Else we have a simple case and we can just use chado_generate_var to get the analysis
  77. else {
  78. // Try to get the organism
  79. $organism = chado_generate_var(
  80. 'organism',
  81. $identifiers,
  82. $options
  83. );
  84. }
  85. // Ensure the organism is singular. If it's an array then it is not singular
  86. if (is_array($organism)) {
  87. tripal_report_error(
  88. 'tripal_organism_api',
  89. TRIPAL_ERROR,
  90. "tripal_get_organism: The identifiers you passed in were not unique. You passed in %identifier.",
  91. array(
  92. '%identifier'=> print_r($identifiers, TRUE)
  93. )
  94. );
  95. }
  96. // Report an error if $organism is FALSE since then chado_generate_var has failed
  97. elseif ($organism === FALSE) {
  98. tripal_report_error(
  99. 'tripal_organism_api',
  100. TRIPAL_ERROR,
  101. "tripal_get_organism: chado_generate_var() failed to return a organism based on the identifiers
  102. you passed in. You should check that your identifiers are correct, as well as, look
  103. for a chado_generate_var error for additional clues. You passed in %identifier.",
  104. array(
  105. '%identifier'=> print_r($identifiers, TRUE)
  106. )
  107. );
  108. }
  109. // Else, as far we know, everything is fine so give them their organism :)
  110. else {
  111. return $organism;
  112. }
  113. }
  114. /**
  115. * Returns a list of organisms that are currently synced with Drupal to use in select lists
  116. *
  117. * @param $syncd_only
  118. * Whether or not to return all chado organisms or just those sync'd with drupal. Defaults
  119. * to TRUE (only sync'd organisms)
  120. * @return
  121. * An array of organisms sync'd with Drupal where each value is the organism scientific
  122. * name and the keys are organism_id's
  123. *
  124. * @ingroup tripal_organism_api
  125. */
  126. function tripal_get_organism_select_options($syncd_only = TRUE) {
  127. if ($syncd_only) {
  128. // use this SQL for getting synced organisms
  129. $dsql = "SELECT * FROM {chado_organism}";
  130. $orgs = db_query($dsql);
  131. // use this SQL statement for getting the organisms
  132. $csql = "SELECT * FROM {organism} " .
  133. "WHERE organism_id = :organism_id";
  134. $org_list = array();
  135. // iterate through the organisms and build an array of those that are synced
  136. foreach ($orgs as $org) {
  137. $args = array(':organism_id' => $org->organism_id);
  138. $org = chado_query($csql, $args)->fetchObject();
  139. $org_list[$org->organism_id] = $org->genus . ' ' . $org->species;
  140. }
  141. }
  142. else {
  143. // use this SQL statement for getting the organisms
  144. $csql = "SELECT * FROM {organism}";
  145. $orgs = chado_query($csql)->execute();
  146. $org_list = array();
  147. // iterate through the organisms and build an array of those that are synced
  148. foreach ($orgs as $org) {
  149. $org_list[$org->organism_id] = $org->genus . ' ' . $org->species;
  150. }
  151. }
  152. return $org_list;
  153. }
  154. /**
  155. * Return the URL for the organism image
  156. *
  157. * @param $organism
  158. * An organism table record
  159. * @param $nid
  160. * (Optional) the node id of the organism node. if not supplied it will be looked up
  161. *
  162. * @return
  163. * The fully qualified url to the image
  164. */
  165. function tripal_get_organism_image($organism, $nid = NULL) {
  166. $url = '';
  167. // first look for an image with the genus/species name. This is old-style tripal
  168. // and we keep it for backwards compatibility. If we don't find that file
  169. // then look for the image with the node ID in the name. If we don't find that then
  170. // no image tag is generated
  171. $base_path = realpath('.');
  172. $image_dir = tripal_get_files_dir('tripal_organism') . "/images";
  173. $image_name = $organism->genus . "_" . $organism->species . ".jpg";
  174. if (file_exists("$base_path/$image_dir/$image_name")) {
  175. $url = file_create_url("$image_dir/$image_name");
  176. }
  177. else {
  178. $image_name = $nid . ".jpg";
  179. if (file_exists("$base_path/$image_dir/$image_name")) {
  180. $url = file_create_url("$image_dir/$image_name");
  181. }
  182. }
  183. return $url;
  184. }