tripal_chado.organism.api.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) to manage organisms
  5. */
  6. /**
  7. * @defgroup tripal_organism_api Chado Organism
  8. * @ingroup tripal_chado_api
  9. * @{
  10. * @}
  11. */
  12. /**
  13. * Retrieves a chado organism variable.
  14. *
  15. * @param $identifier
  16. * An array with the key stating what the identifier is. Supported keys (only
  17. * on of the following unique keys is required):
  18. * - organism_id: the chado organism.organism_id primary key.
  19. * - genus & species: the chado organism.genus field & organism.species field.
  20. * There are also some specially handled keys. They are:
  21. * - property: An array/object describing the property to select records for.
  22. * It should at least have either a type_name (if unique across cvs) or
  23. * type_id. Other supported keys include: cv_id/cv_name (of the type),
  24. * 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
  28. * definition for additional details.
  29. *
  30. * NOTE: the $identifier parameter can really be any array similar to $values
  31. * passed into chado_select_record(). It should fully specify the organism
  32. * record to be returned.
  33. *
  34. * @return
  35. * If unique values were passed in as an identifier then an object describing
  36. * the organism will be returned (will be a chado variable from
  37. * chado_generate_var()). Otherwise, FALSE will be returned.
  38. *
  39. * @ingroup tripal_organism_api
  40. */
  41. function chado_get_organism($identifiers, $options = array()) {
  42. // Set Defaults.
  43. if (!isset($options['include_fk'])) {
  44. // Tells chado_generate_var not to follow any foreign keys.
  45. $options['include_fk'] = array();
  46. }
  47. // Error Checking of parameters.
  48. if (!is_array($identifiers)) {
  49. tripal_report_error(
  50. 'tripal_organism_api',
  51. TRIPAL_ERROR,
  52. "chado_get_organism: The identifier passed in is expected to be an array with the key
  53. matching a column name in the organism table (ie: organism_id or name). You passed in %identifier.",
  54. array(
  55. '%identifier'=> print_r($identifiers, TRUE)
  56. )
  57. );
  58. }
  59. elseif (empty($identifiers)) {
  60. tripal_report_error(
  61. 'tripal_organism_api',
  62. TRIPAL_ERROR,
  63. "chado_get_organism: You did not pass in anything to identify the organism you want. The identifier
  64. is expected to be an array with the key matching a column name in the organism table
  65. (ie: organism_id or name). You passed in %identifier.",
  66. array(
  67. '%identifier'=> print_r($identifiers, TRUE)
  68. )
  69. );
  70. }
  71. // If one of the identifiers is property then use chado_get_record_with_property().
  72. if (isset($identifiers['property'])) {
  73. $property = $identifiers['property'];
  74. unset($identifiers['property']);
  75. $organism = chado_get_record_with_property(
  76. array('table' => 'organism', 'base_records' => $identifiers),
  77. array('type_name' => $property),
  78. $options
  79. );
  80. }
  81. // Else we have a simple case and we can just use chado_generate_var to get
  82. // the analysis.
  83. else {
  84. // Try to get the organism
  85. $organism = chado_generate_var(
  86. 'organism',
  87. $identifiers,
  88. $options
  89. );
  90. }
  91. // Ensure the organism is singular. If it's an array then it is not singular.
  92. if (is_array($organism)) {
  93. tripal_report_error(
  94. 'tripal_organism_api',
  95. TRIPAL_ERROR,
  96. "chado_get_organism: The identifiers you passed in were not unique. You passed in %identifier.",
  97. array(
  98. '%identifier'=> print_r($identifiers, TRUE)
  99. )
  100. );
  101. }
  102. // Report an error if $organism is FALSE since then chado_generate_var has
  103. // failed.
  104. elseif ($organism === FALSE) {
  105. tripal_report_error(
  106. 'tripal_organism_api',
  107. TRIPAL_ERROR,
  108. "chado_get_organism: chado_generate_var() failed to return a organism based on the identifiers
  109. you passed in. You should check that your identifiers are correct, as well as, look
  110. for a chado_generate_var error for additional clues. You passed in %identifier.",
  111. array(
  112. '%identifier'=> print_r($identifiers, TRUE)
  113. )
  114. );
  115. }
  116. // Else, as far we know, everything is fine so give them their organism :)
  117. else {
  118. return $organism;
  119. }
  120. }
  121. /**
  122. * Returns the full scientific name of an organism.
  123. *
  124. * @param $organism
  125. * An organism object.
  126. * @return
  127. * The full scientific name of the organism.
  128. *
  129. * @ingroup tripal_organism_api
  130. */
  131. function chado_get_organism_scientific_name($organism) {
  132. $name = $organism->genus . ' ' . $organism->species;
  133. // For Chado v1.3 we have a type_id and infraspecific name.
  134. if (property_exists($organism, 'type_id')) {
  135. $rank = '';
  136. // For organism objects crated using chado_generate_var.
  137. if (is_object($organism->type_id)) {
  138. if ($organism->type_id) {
  139. $rank = $orgasnism->type_id->name;
  140. }
  141. }
  142. else {
  143. $rank_term = chado_get_cvterm(array('cvterm_id' => $organism->type_id));
  144. if ($rank_term) {
  145. $rank = $rank_term->name;
  146. }
  147. }
  148. if ($rank) {
  149. $rank = chado_abbreviate_infraspecific_rank($rank);
  150. $name .= ' ' . $rank . ' ' . $organism->infraspecific_name;
  151. }
  152. else if ($organism->infraspecific_name) {
  153. $name .= ' ' . $organism->infraspecific_name;
  154. }
  155. }
  156. return $name;
  157. }
  158. /**
  159. * Returns a list of organisms that are currently synced with Drupal to use in
  160. * select lists.
  161. *
  162. * @param $syncd_only
  163. * Whether or not to return all chado organisms or just those sync'd with
  164. * drupal. Defaults to TRUE (only sync'd organisms).
  165. * @return
  166. * An array of organisms sync'd with Drupal where each value is the organism
  167. * scientific name and the keys are organism_id's.
  168. *
  169. * @ingroup tripal_organism_api
  170. */
  171. function chado_get_organism_select_options($syncd_only = TRUE) {
  172. $org_list = array();
  173. $org_list[] = 'Select an organism';
  174. if ($syncd_only) {
  175. $sql = "
  176. SELECT *
  177. FROM [chado_organism] CO
  178. INNER JOIN {organism} O ON O.organism_id = CO.organism_id
  179. ORDER BY O.genus, O.species
  180. ";
  181. $orgs = chado_query($sql);
  182. // Iterate through the organisms and build an array of those that are synced.
  183. foreach ($orgs as $org) {
  184. $org_list[$org->organism_id] = $org->genus . ' ' . $org->species;
  185. }
  186. }
  187. else {
  188. // use this SQL statement for getting the organisms
  189. $csql = "SELECT * FROM {organism} ORDER BY genus, species";
  190. $orgs = chado_query($csql);
  191. // Iterate through the organisms and build an array of those that are synced.
  192. foreach ($orgs as $org) {
  193. $org_list[$org->organism_id] = $org->genus . ' ' . $org->species;
  194. }
  195. }
  196. return $org_list;
  197. }
  198. /**
  199. * Return the path for the organism image.
  200. *
  201. * @param $organism
  202. * An organism table record.
  203. *
  204. * @return
  205. * If the type parameter is 'url' (the default) then the fully qualified
  206. * url to the image is returend. If no image is present then NULL is returned.
  207. *
  208. * @ingroup tripal_organism_api
  209. */
  210. function chado_get_organism_image_url($organism) {
  211. $url = '';
  212. if (!is_object($organism)) {
  213. return NULL;
  214. }
  215. // Get the organism's node.
  216. $nid = chado_get_nid_from_id('organism', $organism->organism_id);
  217. // Look in the file_usage table of Drupal for the image file. This
  218. // is the current way for handling uploaded images. It allows the file to
  219. // keep it's proper name and extension.
  220. $fid = db_select('file_usage', 'fu')
  221. ->fields('fu', array('fid'))
  222. ->condition('module', 'tripal_organism')
  223. ->condition('type', 'organism_image')
  224. ->condition('id', $nid)
  225. ->execute()
  226. ->fetchField();
  227. if ($fid) {
  228. $file = file_load($fid);
  229. return file_create_url($file->uri);
  230. }
  231. // First look for an image with the genus/species name. This is old-style
  232. // tripal and we keep it for backwards compatibility.
  233. $base_path = realpath('.');
  234. $image_dir = tripal_get_files_dir('tripal_organism') . "/images";
  235. $image_name = $organism->genus . "_" . $organism->species . ".jpg";
  236. $image_path = "$base_path/$image_dir/$image_name";
  237. if (file_exists($image_path)) {
  238. $url = file_create_url("$image_dir/$image_name");
  239. return $url;
  240. }
  241. // If we don't find the file using the genus ans species then look for the
  242. // image with the node ID in the name. This method was used for Tripal 1.1
  243. // and 2.x-alpha version.
  244. $image_name = $nid . ".jpg";
  245. $image_path = "$base_path/$image_dir/$image_name";
  246. if (file_exists($image_path)) {
  247. $url = file_create_url("$image_dir/$image_name");
  248. return $url;
  249. }
  250. return NULL;
  251. }
  252. /**
  253. * This function is intended to be used in autocomplete forms
  254. * for searching for organisms that begin with the provided string.
  255. *
  256. * @param $text
  257. * The string to search for.
  258. *
  259. * @return
  260. * A json array of terms that begin with the provided string.
  261. *
  262. * @ingroup tripal_organism_api
  263. */
  264. function chado_autocomplete_organism($text) {
  265. $matches = array();
  266. $genus = $text;
  267. $species = '';
  268. if (preg_match('/^(.*?)\s+(.*)$/', $text, $matches)) {
  269. $genus = $matches[1];
  270. $species = $matches[2];
  271. }
  272. $sql = "SELECT * FROM {organism} WHERE lower(genus) like lower(:genus) ";
  273. $args = array();
  274. $args[':genus'] = $genus . '%';
  275. if ($species) {
  276. $sql .= "AND lower(species) like lower(:species) ";
  277. $args[':species'] = $species . '%';
  278. }
  279. $sql .= "ORDER BY genus, species ";
  280. $sql .= "LIMIT 25 OFFSET 0 ";
  281. $results = chado_query($sql, $args);
  282. $items = array();
  283. foreach ($results as $organism) {
  284. $name = chado_get_organism_scientific_name($organism);
  285. $items["$name [id: $organism->organism_id]"] = $name;
  286. }
  287. drupal_json_output($items);
  288. }
  289. /**
  290. * A handy function to abbreviate the infraspecific rank.
  291. *
  292. * @param $rank
  293. * The rank below species.
  294. * @return
  295. * The proper abbreviation for the rank.
  296. *
  297. * @ingroup tripal_organism_api
  298. */
  299. function chado_abbreviate_infraspecific_rank($rank) {
  300. $abb = '';
  301. switch($rank) {
  302. case 'no_rank':
  303. $abb = '';
  304. break;
  305. case 'subspecies':
  306. $abb = 'subsp.';
  307. break;
  308. case 'varietas':
  309. $abb = 'var.';
  310. break;
  311. case 'variety':
  312. $abb = 'var.';
  313. break;
  314. case 'subvarietas':
  315. $abb = 'subvar.';
  316. break;
  317. case 'subvariety':
  318. $abb = 'subvar.';
  319. break;
  320. case 'forma':
  321. $abb = 'f.';
  322. break;
  323. case 'subforma':
  324. $abb = 'subf.';
  325. break;
  326. default:
  327. $abb = $rank;
  328. }
  329. return $abb;
  330. }