tripal_chado.organism.api.inc 10 KB

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