tripal_chado.organism.api.inc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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(
  75. array('table' => 'organism', 'base_records' => $identifiers),
  76. array('type_name' => $property),
  77. $options
  78. );
  79. }
  80. // Else we have a simple case and we can just use chado_generate_var to get the analysis
  81. else {
  82. // Try to get the organism
  83. $organism = chado_generate_var(
  84. 'organism',
  85. $identifiers,
  86. $options
  87. );
  88. }
  89. // Ensure the organism is singular. If it's an array then it is not singular
  90. if (is_array($organism)) {
  91. tripal_report_error(
  92. 'tripal_organism_api',
  93. TRIPAL_ERROR,
  94. "tripal_get_organism: The identifiers you passed in were not unique. You passed in %identifier.",
  95. array(
  96. '%identifier'=> print_r($identifiers, TRUE)
  97. )
  98. );
  99. }
  100. // Report an error if $organism is FALSE since then chado_generate_var has failed
  101. elseif ($organism === FALSE) {
  102. tripal_report_error(
  103. 'tripal_organism_api',
  104. TRIPAL_ERROR,
  105. "tripal_get_organism: chado_generate_var() failed to return a organism based on the identifiers
  106. you passed in. You should check that your identifiers are correct, as well as, look
  107. for a chado_generate_var error for additional clues. You passed in %identifier.",
  108. array(
  109. '%identifier'=> print_r($identifiers, TRUE)
  110. )
  111. );
  112. }
  113. // Else, as far we know, everything is fine so give them their organism :)
  114. else {
  115. return $organism;
  116. }
  117. }
  118. /**
  119. * Returns a list of organisms that are currently synced with Drupal to use in select lists
  120. *
  121. * @param $syncd_only
  122. * Whether or not to return all chado organisms or just those sync'd with drupal. Defaults
  123. * to TRUE (only sync'd organisms)
  124. * @return
  125. * An array of organisms sync'd with Drupal where each value is the organism scientific
  126. * name and the keys are organism_id's
  127. *
  128. * @ingroup tripal_organism_api
  129. */
  130. function tripal_get_organism_select_options($syncd_only = TRUE) {
  131. $org_list = array();
  132. $org_list[] = 'Select an organism';
  133. if ($syncd_only) {
  134. $sql = "
  135. SELECT *
  136. FROM {chado_organism} CO
  137. INNER JOIN {organism} O ON O.organism_id = CO.organism_id
  138. ORDER BY O.genus, O.species
  139. ";
  140. $orgs = chado_query($sql);
  141. // iterate through the organisms and build an array of those that are synced
  142. foreach ($orgs as $org) {
  143. $org_list[$org->organism_id] = $org->genus . ' ' . $org->species;
  144. }
  145. }
  146. else {
  147. // use this SQL statement for getting the organisms
  148. $csql = "SELECT * FROM {organism} ORDER BY genus, species";
  149. $orgs = chado_query($csql);
  150. // iterate through the organisms and build an array of those that are synced
  151. foreach ($orgs as $org) {
  152. $org_list[$org->organism_id] = $org->genus . ' ' . $org->species;
  153. }
  154. }
  155. return $org_list;
  156. }
  157. /**
  158. * Return the path for the organism image.
  159. *
  160. * @param $organism
  161. * An organism table record
  162. *
  163. * @return
  164. * If the type parameter is 'url' (the default) then the fully qualified
  165. * url to the image is returend. If no image is present then NULL is returned
  166. */
  167. function tripal_get_organism_image_url($organism) {
  168. $url = '';
  169. if (!is_object($organism)) {
  170. return NULL;
  171. }
  172. // Get the organism's node
  173. $nid = chado_get_nid_from_id('organism', $organism->organism_id);
  174. // Look in the file_usage table of Drupal for the image file. This
  175. // is the current way for handling uploaded images. It allows the file to
  176. // keep it's proper name and extension.
  177. $fid = db_select('file_usage', 'fu')
  178. ->fields('fu', array('fid'))
  179. ->condition('module', 'tripal_organism')
  180. ->condition('type', 'organism_image')
  181. ->condition('id', $nid)
  182. ->execute()
  183. ->fetchField();
  184. if ($fid) {
  185. $file = file_load($fid);
  186. return file_create_url($file->uri);
  187. }
  188. // First look for an image with the genus/species name. This is old-style tripal
  189. // and we keep it for backwards compatibility.
  190. $base_path = realpath('.');
  191. $image_dir = tripal_get_files_dir('tripal_organism') . "/images";
  192. $image_name = $organism->genus . "_" . $organism->species . ".jpg";
  193. $image_path = "$base_path/$image_dir/$image_name";
  194. if (file_exists($image_path)) {
  195. $url = file_create_url("$image_dir/$image_name");
  196. return $url;
  197. }
  198. // If we don't find the file using the genus ans species then look for the
  199. // image with the node ID in the name. This method was used for Tripal 1.1
  200. // and 2.x-alpha version.
  201. $image_name = $nid . ".jpg";
  202. $image_path = "$base_path/$image_dir/$image_name";
  203. if (file_exists($image_path)) {
  204. $url = file_create_url("$image_dir/$image_name");
  205. return $url;
  206. }
  207. return NULL;
  208. }
  209. /**
  210. * This function is intended to be used in autocomplete forms
  211. * for searching for organisms that begin with the provided string
  212. *
  213. * @param $text
  214. * The string to search for
  215. *
  216. * @return
  217. * A json array of terms that begin with the provided string
  218. *
  219. * @ingroup tripal_organism_api
  220. */
  221. function tripal_autocomplete_organism($text) {
  222. $matches = array();
  223. $genus = $text;
  224. $species = '';
  225. if (preg_match('/^(.*?) (.*)$/', $text, $matches)) {
  226. $genus = $matches[1];
  227. $species = $matches[2];
  228. }
  229. $sql = "SELECT * FROM {organism} WHERE lower(genus) like lower(:genus) ";
  230. $args = array();
  231. $args[':genus'] = $genus . '%';
  232. if ($species) {
  233. $sql .= "AND lower(species) like lower(:species) ";
  234. $args[':species'] = $species . '%';
  235. }
  236. $sql .= "ORDER BY genus, species ";
  237. $sql .= "LIMIT 25 OFFSET 0 ";
  238. $results = chado_query($sql, $args);
  239. $items = array();
  240. foreach ($results as $organism) {
  241. $name = $organism->genus . ' ' .$organism->species;
  242. $items["$name [id: $organism->organism_id]"] = $name;
  243. }
  244. drupal_json_output($items);
  245. }