tripal_chado.organism.api.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. // Tripal 2.
  178. if ($syncd_only && chado_table_exists('chado_organism')) {
  179. $sql = "
  180. SELECT *
  181. FROM [chado_organism] CO
  182. INNER JOIN {organism} O ON O.organism_id = CO.organism_id
  183. ORDER BY O.genus, O.species
  184. ";
  185. $orgs = chado_query($sql);
  186. // Iterate through the organisms and build an array of those that are synced.
  187. foreach ($orgs as $org) {
  188. $org_list[$org->organism_id] = $org->genus . ' ' . $org->species;
  189. }
  190. return $org_list;
  191. }
  192. if ($syncd_only) {
  193. $bundle_tables = chado_get_bundles_by_base_table('organism');
  194. if (empty($bundle_tables)) {
  195. return $org_list;
  196. }
  197. $all_orgs = [];
  198. $query = db_select('chado.organism', 'o')
  199. ->fields('o', ['genus', 'species', 'organism_id']);
  200. foreach ($bundle_tables as $table) {
  201. $query_copy = $query;
  202. $query_copy->join('public.' . $table, $table, $table . '.record_id = o.organism_id');
  203. $orgs = $query_copy->execute()->fetchAll();
  204. array_merge($all_orgs, $orgs);
  205. }
  206. foreach ($all_orgs as $org) {
  207. $org_list[$org->organism_id] = $org->genus . ' ' . $org->species;
  208. }
  209. return $org_list;
  210. }
  211. // Case: ignore published status.
  212. // Use this SQL statement for getting the organisms.
  213. $csql = "SELECT * FROM {organism} ORDER BY genus, species";
  214. $orgs = chado_query($csql);
  215. // Iterate through the organisms and build an array of those that are synced.
  216. foreach ($orgs as $org) {
  217. $org_list[$org->organism_id] = $org->genus . ' ' . $org->species;
  218. }
  219. return $org_list;
  220. }
  221. /**
  222. * Return the path for the organism image.
  223. *
  224. * @param $organism
  225. * An organism table record.
  226. *
  227. * @return
  228. * If the type parameter is 'url' (the default) then the fully qualified
  229. * url to the image is returend. If no image is present then NULL is returned.
  230. *
  231. * @ingroup tripal_organism_api
  232. */
  233. function chado_get_organism_image_url($organism) {
  234. $url = '';
  235. if (!is_object($organism)) {
  236. return NULL;
  237. }
  238. // Get the organism's node.
  239. $nid = chado_get_nid_from_id('organism', $organism->organism_id);
  240. // Look in the file_usage table of Drupal for the image file. This
  241. // is the current way for handling uploaded images. It allows the file to
  242. // keep it's proper name and extension.
  243. $fid = db_select('file_usage', 'fu')
  244. ->fields('fu', array('fid'))
  245. ->condition('module', 'tripal_organism')
  246. ->condition('type', 'organism_image')
  247. ->condition('id', $nid)
  248. ->execute()
  249. ->fetchField();
  250. if ($fid) {
  251. $file = file_load($fid);
  252. return file_create_url($file->uri);
  253. }
  254. // First look for an image with the genus/species name. This is old-style
  255. // tripal and we keep it for backwards compatibility.
  256. $base_path = realpath('.');
  257. $image_dir = tripal_get_files_dir('tripal_organism') . "/images";
  258. $image_name = $organism->genus . "_" . $organism->species . ".jpg";
  259. $image_path = "$base_path/$image_dir/$image_name";
  260. if (file_exists($image_path)) {
  261. $url = file_create_url("$image_dir/$image_name");
  262. return $url;
  263. }
  264. // If we don't find the file using the genus ans species then look for the
  265. // image with the node ID in the name. This method was used for Tripal 1.1
  266. // and 2.x-alpha version.
  267. $image_name = $nid . ".jpg";
  268. $image_path = "$base_path/$image_dir/$image_name";
  269. if (file_exists($image_path)) {
  270. $url = file_create_url("$image_dir/$image_name");
  271. return $url;
  272. }
  273. return NULL;
  274. }
  275. /**
  276. * This function is intended to be used in autocomplete forms
  277. * for searching for organisms that begin with the provided string.
  278. *
  279. * @param $text
  280. * The string to search for.
  281. *
  282. * @return
  283. * A json array of terms that begin with the provided string.
  284. *
  285. * @ingroup tripal_organism_api
  286. */
  287. function chado_autocomplete_organism($text) {
  288. $matches = array();
  289. $genus = $text;
  290. $species = '';
  291. if (preg_match('/^(.*?)\s+(.*)$/', $text, $matches)) {
  292. $genus = $matches[1];
  293. $species = $matches[2];
  294. }
  295. $sql = "SELECT * FROM {organism} WHERE lower(genus) like lower(:genus) ";
  296. $args = array();
  297. $args[':genus'] = $genus . '%';
  298. if ($species) {
  299. $sql .= "AND lower(species) like lower(:species) ";
  300. $args[':species'] = $species . '%';
  301. }
  302. $sql .= "ORDER BY genus, species ";
  303. $sql .= "LIMIT 25 OFFSET 0 ";
  304. $results = chado_query($sql, $args);
  305. $items = array(['args' => [$sql => $args]]);
  306. foreach ($results as $organism) {
  307. $name = chado_get_organism_scientific_name($organism);
  308. $items["$name [id: $organism->organism_id]"] = $name;
  309. }
  310. drupal_json_output($items);
  311. }
  312. /**
  313. * A handy function to abbreviate the infraspecific rank.
  314. *
  315. * @param $rank
  316. * The rank below species.
  317. * @return
  318. * The proper abbreviation for the rank.
  319. *
  320. * @ingroup tripal_organism_api
  321. */
  322. function chado_abbreviate_infraspecific_rank($rank) {
  323. $abb = '';
  324. switch($rank) {
  325. case 'no_rank':
  326. $abb = '';
  327. break;
  328. case 'subspecies':
  329. $abb = 'subsp.';
  330. break;
  331. case 'varietas':
  332. $abb = 'var.';
  333. break;
  334. case 'variety':
  335. $abb = 'var.';
  336. break;
  337. case 'subvarietas':
  338. $abb = 'subvar.';
  339. break;
  340. case 'subvariety':
  341. $abb = 'subvar.';
  342. break;
  343. case 'forma':
  344. $abb = 'f.';
  345. break;
  346. case 'subforma':
  347. $abb = 'subf.';
  348. break;
  349. default:
  350. $abb = $rank;
  351. }
  352. return $abb;
  353. }