tripal_organism.api.inc

  1. 2.x tripal_organism/api/tripal_organism.api.inc
  2. 1.x tripal_organism/api/tripal_organism.api.inc

Provides an application programming interface (API) to manage organisms

File

tripal_organism/api/tripal_organism.api.inc
View source
  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. // @todo: Re-write to support external chado databases.
  135. $sql = "
  136. SELECT *
  137. FROM [chado_organism] CO
  138. INNER JOIN {organism} O ON O.organism_id = CO.organism_id
  139. ORDER BY O.genus, O.species
  140. ";
  141. $orgs = chado_query($sql);
  142. // iterate through the organisms and build an array of those that are synced
  143. foreach ($orgs as $org) {
  144. $org_list[$org->organism_id] = $org->genus . ' ' . $org->species;
  145. }
  146. }
  147. else {
  148. // use this SQL statement for getting the organisms
  149. $csql = "SELECT * FROM {organism} ORDER BY genus, species";
  150. $orgs = chado_query($csql);
  151. // iterate through the organisms and build an array of those that are synced
  152. foreach ($orgs as $org) {
  153. $org_list[$org->organism_id] = $org->genus . ' ' . $org->species;
  154. }
  155. }
  156. return $org_list;
  157. }
  158. /**
  159. * Return the path for the organism image.
  160. *
  161. * @param $organism
  162. * An organism table record
  163. *
  164. * @return
  165. * If the type parameter is 'url' (the default) then the fully qualified
  166. * url to the image is returend. If no image is present then NULL is returned
  167. */
  168. function tripal_get_organism_image_url($organism) {
  169. $url = '';
  170. if (!is_object($organism)) {
  171. return NULL;
  172. }
  173. // Get the organism's node
  174. $nid = chado_get_nid_from_id('organism', $organism->organism_id);
  175. // Look in the file_usage table of Drupal for the image file. This
  176. // is the current way for handling uploaded images. It allows the file to
  177. // keep it's proper name and extension.
  178. $fid = db_select('file_usage', 'fu')
  179. ->fields('fu', array('fid'))
  180. ->condition('module', 'tripal_organism')
  181. ->condition('type', 'organism_image')
  182. ->condition('id', $nid)
  183. ->execute()
  184. ->fetchField();
  185. if ($fid) {
  186. $file = file_load($fid);
  187. return file_create_url($file->uri);
  188. }
  189. // First look for an image with the genus/species name. This is old-style tripal
  190. // and we keep it for backwards compatibility.
  191. $base_path = realpath('.');
  192. $image_dir = tripal_get_files_dir('tripal_organism') . "/images";
  193. $image_name = $organism->genus . "_" . $organism->species . ".jpg";
  194. $image_path = "$base_path/$image_dir/$image_name";
  195. if (file_exists($image_path)) {
  196. $url = file_create_url("$image_dir/$image_name");
  197. return $url;
  198. }
  199. // If we don't find the file using the genus ans species then look for the
  200. // image with the node ID in the name. This method was used for Tripal 1.1
  201. // and 2.x-alpha version.
  202. $image_name = $nid . ".jpg";
  203. $image_path = "$base_path/$image_dir/$image_name";
  204. if (file_exists($image_path)) {
  205. $url = file_create_url("$image_dir/$image_name");
  206. return $url;
  207. }
  208. return NULL;
  209. }
  210. /**
  211. * This function is intended to be used in autocomplete forms
  212. * for searching for organisms that begin with the provided string
  213. *
  214. * @param $text
  215. * The string to search for
  216. *
  217. * @return
  218. * A json array of terms that begin with the provided string
  219. *
  220. * @ingroup tripal_organism_api
  221. */
  222. function tripal_autocomplete_organism($text) {
  223. $matches = array();
  224. $genus = $text;
  225. $species = '';
  226. if (preg_match('/^(.*?) (.*)$/', $text, $matches)) {
  227. $genus = $matches[1];
  228. $species = $matches[2];
  229. }
  230. $sql = "SELECT * FROM {organism} WHERE lower(genus) like lower(:genus) ";
  231. $args = array();
  232. $args[':genus'] = $genus . '%';
  233. if ($species) {
  234. $sql .= "AND lower(species) like lower(:species) ";
  235. $args[':species'] = $species . '%';
  236. }
  237. $sql .= "ORDER BY genus, species ";
  238. $sql .= "LIMIT 25 OFFSET 0 ";
  239. $results = chado_query($sql, $args);
  240. $items = array();
  241. foreach ($results as $organism) {
  242. $name = $organism->genus . ' ' .$organism->species;
  243. $items[$name] = $name;
  244. }
  245. drupal_json_output($items);
  246. }