tripal_chado.organism.api.inc 10 KB

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