tripal_chado.stock.api.inc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * @file
  4. * Provides API functions specificially for managing stock
  5. * records in Chado.
  6. */
  7. /**
  8. * @defgroup tripal_stock_api Chado Stock
  9. * @ingroup tripal_chado_api
  10. * @{
  11. * Provides API functions specificially for managing stock
  12. * records in Chado. The stock table of Chado is used for storing a variety
  13. * of data types besides just stocks from a stock collection. Examples of
  14. * other records commonly stored in the stock table are germplasm and
  15. * individuals from a breeding population.
  16. * @}
  17. */
  18. /**
  19. * Used for autocomplete in forms for identifying stocks
  20. *
  21. * @param $string
  22. * The string to search for.
  23. *
  24. * @return
  25. * A json array of terms that begin with the provided string.
  26. *
  27. * @ingroup tripal_stock_api
  28. */
  29. function chado_autocomplete_stock($string = '') {
  30. $items = [];
  31. $sql = "
  32. SELECT
  33. B.stock_id as id, B.uniquename, B.name,
  34. O.genus, O,species,
  35. CVT.name as type
  36. FROM {stock} B
  37. INNER JOIN {organism} O ON O.organism_id = B.organism_id
  38. INNER JOIN {cvterm} CVT ON CVT.cvterm_id = B.type_id
  39. WHERE lower(B.uniquename) like lower(:str) OR lower(B.name) like lower(:str)
  40. ORDER by B.name
  41. LIMIT 25 OFFSET 0
  42. ";
  43. $records = chado_query($sql, [':str' => $string . '%']);
  44. while ($r = $records->fetchObject()) {
  45. $key = "$r->name [id: $r->id]";
  46. $items[$key] = "$r->name ($r->uniquename, $r->type, $r->genus $r->species)";
  47. }
  48. drupal_json_output($items);
  49. }
  50. /**
  51. * Retrieves a chado stock variable
  52. *
  53. * @param $identifier
  54. * An array with the key stating what the identifier is. Supported keys (only
  55. * one of the following unique keys is required):
  56. * - stock_id: the chado stock.stock_id primary key
  57. * - nid: the drupal nid of the stock
  58. * There are also some specially handled keys. They are:
  59. * - property: An array/object describing the property to select records for.
  60. * It should at least have either a type_name (if unique across cvs) or
  61. * type_id. Other supported keys include: cv_id/cv_name (of the type),
  62. * value and rank
  63. * @param $options
  64. * An array of options. Supported keys include:
  65. * - Any keys supported by chado_generate_var(). See that function
  66. * definition for additional details.
  67. *
  68. * NOTE: the $identifier parameter can really be any array similar to $values
  69. * passed into chado_select_record(). It should fully specify the stock record
  70. * to be returned.
  71. *
  72. * @return
  73. * If unique values were passed in as an identifier then an object describing
  74. * the stock will be returned (will be a chado variable from
  75. * chado_generate_var()). Otherwise, FALSE will be returned.
  76. *
  77. * @ingroup tripal_stock_api
  78. */
  79. function chado_get_stock($identifiers, $options = []) {
  80. // Set Defaults.
  81. if (!isset($options['include_fk'])) {
  82. // Tells chado_generate_var to only expand 1 level.
  83. $options['include_fk'] = ['type_id' => TRUE, 'dbxref_id' => TRUE];
  84. }
  85. // Error Checking of parameters.
  86. if (!is_array($identifiers)) {
  87. tripal_report_error(
  88. 'tripal_stock_api',
  89. TRIPAL_ERROR,
  90. "chado_get_stock: The identifier passed in is expected to be an array with the key
  91. matching a column name in the stock table (ie: stock_id or name). You passed in %identifier.",
  92. [
  93. '%identifier' => print_r($identifiers, TRUE),
  94. ]
  95. );
  96. }
  97. elseif (empty($identifiers)) {
  98. tripal_report_error(
  99. 'tripal_stock_api',
  100. TRIPAL_ERROR,
  101. "chado_get_stock: You did not pass in anything to identify the stock you want. The identifier
  102. is expected to be an array with the key matching a column name in the stock table
  103. (ie: stock_id or name). You passed in %identifier.",
  104. [
  105. '%identifier' => print_r($identifiers, TRUE),
  106. ]
  107. );
  108. }
  109. // If one of the identifiers is property then use
  110. // chado_get_record_with_property().
  111. if (isset($identifiers['property'])) {
  112. $property = $identifiers['property'];
  113. unset($identifiers['property']);
  114. $stock = chado_get_record_with_property(
  115. ['table' => 'stock', 'base_records' => $identifiers],
  116. ['type_name' => $property],
  117. $options
  118. );
  119. }
  120. // Else we have a simple case and we can just use chado_generate_var to get
  121. // the stock.
  122. else {
  123. // Try to get the stock.
  124. $stock = chado_generate_var(
  125. 'stock',
  126. $identifiers,
  127. $options
  128. );
  129. }
  130. // Ensure the stock is singular. If it's an array then it is not singular.
  131. if (is_array($stock)) {
  132. tripal_report_error(
  133. 'tripal_stock_api',
  134. TRIPAL_ERROR,
  135. "chado_get_stock: The identifiers you passed in were not unique. You passed in %identifier.",
  136. [
  137. '%identifier' => print_r($identifiers, TRUE),
  138. ]
  139. );
  140. }
  141. // Report an error if $stock is FALSE since then chado_generate_var has failed.
  142. elseif ($stock === FALSE) {
  143. tripal_report_error(
  144. 'tripal_stock_api',
  145. TRIPAL_ERROR,
  146. "chado_get_stock: chado_generate_var() failed to return a stock based on the identifiers
  147. you passed in. You should check that your identifiers are correct, as well as, look
  148. for a chado_generate_var error for additional clues. You passed in %identifier.",
  149. [
  150. '%identifier' => print_r($identifiers, TRUE),
  151. ]
  152. );
  153. }
  154. // Else, as far we know, everything is fine so give them their stock :)
  155. else {
  156. return $stock;
  157. }
  158. }
  159. /**
  160. * Retrieves a chado stock variable.
  161. *
  162. * @param $identifier
  163. * An array with the key stating what the identifier is. Supported keys
  164. * include any field in the stock table. See the chado_select_record() $values
  165. * parameter for additional details including an example.
  166. * @param $options
  167. * An array of options. Supported keys include:
  168. * - Any keys supported by chado_generate_var(). See that function
  169. * definition for additional details.
  170. *
  171. * @return
  172. * An array of stock objects matching the criteria.
  173. *
  174. * @ingroup tripal_stock_api
  175. */
  176. function chado_get_multiple_stocks($identifiers, $options = []) {
  177. // Set Defaults.
  178. if (!isset($options['include_fk'])) {
  179. // Tells chado_generate_var to only expand 1 level.
  180. $options['include_fk'] = ['type_id' => TRUE, 'dbxref_id' => TRUE];
  181. }
  182. // Error Checking of parameters.
  183. if (!is_array($identifiers)) {
  184. tripal_report_error(
  185. 'tripal_stock_api',
  186. TRIPAL_ERROR,
  187. "chado_get_stock: The identifier passed in is expected to be an array with the key
  188. matching a column name in the stock table (ie: stock_id or name). You passed in %identifier.",
  189. [
  190. '%identifier' => print_r($identifiers, TRUE),
  191. ]
  192. );
  193. }
  194. elseif (empty($identifiers)) {
  195. tripal_report_error(
  196. 'tripal_stock_api',
  197. TRIPAL_ERROR,
  198. "chado_get_stock: You did not pass in anything to identify the stock you want. The identifier
  199. is expected to be an array with the key matching a column name in the stock table
  200. (ie: stock_id or name). You passed in %identifier.",
  201. [
  202. '%identifier' => print_r($identifiers, TRUE),
  203. ]
  204. );
  205. }
  206. // If one of the identifiers is property then use
  207. // chado_get_record_with_property().
  208. if (isset($identifiers['property'])) {
  209. $property = $identifiers['property'];
  210. unset($identifiers['property']);
  211. $stock = chado_get_record_with_property(
  212. ['table' => 'stock', 'base_records' => $identifiers],
  213. ['type_name' => $property],
  214. $options
  215. );
  216. }
  217. // Else we have a simple case and we can just use chado_generate_var to get
  218. //the stock.
  219. else {
  220. // Try to get the stock.
  221. $stock = chado_generate_var(
  222. 'stock',
  223. $identifiers,
  224. $options
  225. );
  226. }
  227. // Report an error if $stock is FALSE since then chado_generate_var has failed.
  228. if ($stock === FALSE) {
  229. tripal_report_error(
  230. 'tripal_stock_api',
  231. TRIPAL_ERROR,
  232. "chado_get_stock: chado_generate_var() failed to return a stock based on the identifiers
  233. you passed in. You should check that your identifiers are correct, as well as, look
  234. for a chado_generate_var error for additional clues. You passed in %identifier.",
  235. [
  236. '%identifier' => print_r($identifiers, TRUE),
  237. ]
  238. );
  239. }
  240. // Else, as far we know, everything is fine so give them their stock :)
  241. else {
  242. return $stock;
  243. }
  244. }