tripal_organism.api.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 object
  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. * @param $options
  22. * An array of options. Supported keys include:
  23. * - Any keys supported by chado_generate_var(). See that function definition for
  24. * additional details.
  25. *
  26. * NOTE: the $identifier parameter can really be any array similar to $values passed into
  27. * chado_select_record(). It should fully specify the organism record to be returned.
  28. *
  29. * @return
  30. * If unique values were passed in as an identifier then an object describing the organism
  31. * will be returned (will be a chado variable from chado_generate_var()). Otherwise,
  32. * FALSE will be returned.
  33. *
  34. * @ingroup tripal_organism_api
  35. */
  36. function chado_get_organism($identifiers, $options = array()) {
  37. // Error Checking of parameters
  38. if (!is_array($identifiers)) {
  39. tripal_report_error(
  40. 'tripal_organism_api',
  41. TRIPAL_ERROR,
  42. "chado_get_organism: The identifier passed in is expected to be an array with the key
  43. matching a column name in the organism table (ie: organism_id or name). You passed in %identifier.",
  44. array(
  45. '%identifier'=> print_r($identifiers, TRUE)
  46. )
  47. );
  48. }
  49. elseif (empty($identifiers)) {
  50. tripal_report_error(
  51. 'tripal_organism_api',
  52. TRIPAL_ERROR,
  53. "chado_get_organism: You did not pass in anything to identify the organism you want. The identifier
  54. is expected to be an array with the key matching a column name in the organism table
  55. (ie: organism_id or name). You passed in %identifier.",
  56. array(
  57. '%identifier'=> print_r($identifiers, TRUE)
  58. )
  59. );
  60. }
  61. // Try to get the organism
  62. $organism = chado_generate_var(
  63. 'organism',
  64. $identifiers,
  65. $options
  66. );
  67. // Ensure the organism is singular. If it's an array then it is not singular
  68. if (is_array($organism)) {
  69. tripal_report_error(
  70. 'tripal_organism_api',
  71. TRIPAL_ERROR,
  72. "chado_get_organism: The identifiers you passed in were not unique. You passed in %identifier.",
  73. array(
  74. '%identifier'=> print_r($identifiers, TRUE)
  75. )
  76. );
  77. }
  78. // Report an error if $organism is FALSE since then chado_generate_var has failed
  79. elseif ($organism === FALSE) {
  80. tripal_report_error(
  81. 'tripal_organism_api',
  82. TRIPAL_ERROR,
  83. "chado_get_organism: chado_generate_var() failed to return a organism based on the identifiers
  84. you passed in. You should check that your identifiers are correct, as well as, look
  85. for a chado_generate_var error for additional clues. You passed in %identifier.",
  86. array(
  87. '%identifier'=> print_r($identifiers, TRUE)
  88. )
  89. );
  90. }
  91. // Else, as far we know, everything is fine so give them their organism :)
  92. else {
  93. return $organism;
  94. }
  95. }
  96. /**
  97. * Returns a list of organisms that are currently synced with Drupal to use in select lists
  98. *
  99. * @param $syncd_only
  100. * Whether or not to return all chado organisms or just those sync'd with drupal. Defaults
  101. * to TRUE (only sync'd organisms)
  102. * @return
  103. * An array of organisms sync'd with Drupal where each value is the organism scientific
  104. * name and the keys are organism_id's
  105. *
  106. * @ingroup tripal_organism_api
  107. */
  108. function organism_get_select_options($syncd_only = TRUE) {
  109. if ($syncd_only) {
  110. // use this SQL for getting synced organisms
  111. $dsql = "SELECT * FROM {chado_organism}";
  112. $orgs = db_query($dsql);
  113. // use this SQL statement for getting the organisms
  114. $csql = "SELECT * FROM {organism} " .
  115. "WHERE organism_id = :organism_id";
  116. $org_list = array();
  117. // iterate through the organisms and build an array of those that are synced
  118. foreach ($orgs as $org) {
  119. $args = array(':organism_id' => $org->organism_id);
  120. $org = chado_query($csql, $args)->fetchObject();
  121. $org_list[$org->organism_id] = $org->genus . ' ' . $org->species;
  122. }
  123. }
  124. else {
  125. // use this SQL statement for getting the organisms
  126. $csql = "SELECT * FROM {organism}";
  127. $orgs = chado_query($csql)->execute();
  128. $org_list = array();
  129. // iterate through the organisms and build an array of those that are synced
  130. foreach ($orgs as $org) {
  131. $org_list[$org->organism_id] = $org->genus . ' ' . $org->species;
  132. }
  133. }
  134. return $org_list;
  135. }
  136. /**
  137. * Return the URL for the organism image
  138. *
  139. * @param $organism
  140. * An organism table record
  141. * @param $nid
  142. * (Optional) the node id of the organism node. if not supplied it will be looked up
  143. *
  144. * @return
  145. * The fully qualified url to the image
  146. */
  147. function organism_get_image($organism, $nid = NULL) {
  148. $url = '';
  149. // first look for an image with the genus/species name. This is old-style tripal
  150. // and we keep it for backwards compatibility. If we don't find that file
  151. // then look for the image with the node ID in the name. If we don't find that then
  152. // no image tag is generated
  153. $base_path = realpath('.');
  154. $image_dir = tripal_get_files_dir('tripal_organism') . "/images";
  155. $image_name = $organism->genus . "_" . $organism->species . ".jpg";
  156. if (file_exists("$base_path/$image_dir/$image_name")) {
  157. $url = file_create_url("$image_dir/$image_name");
  158. }
  159. else {
  160. $image_name = $nid . ".jpg";
  161. if (file_exists("$base_path/$image_dir/$image_name")) {
  162. $url = file_create_url("$image_dir/$image_name");
  163. }
  164. }
  165. return $url;
  166. }