tripal_chado.organism.api.inc 10 KB

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