tripal_chado.organism.api.inc 10 KB

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