tripal_stock.api.inc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) to manage stocks
  5. */
  6. /**
  7. * @defgroup tripal_stock_api Stock Module API
  8. * @ingroup tripal_api
  9. * @{
  10. * Provides an application programming interface (API) to manage stocks
  11. * @}
  12. */
  13. /**
  14. * Purpose: Return a given stock node using the nid
  15. *
  16. * @param $nid
  17. * The node ID of the stock you want to load
  18. *
  19. * @return
  20. * stock node with the passed in node ID
  21. *
  22. * @ingroup tripal_stock_api
  23. */
  24. function tripal_stock_get_stock_by_nid($nid) {
  25. return node_load($nid);
  26. }
  27. /**
  28. * Purpose: Return a given stock object using the stock id
  29. *
  30. * @return
  31. * Stock object created by node load
  32. *
  33. * @ingroup tripal_stock_api
  34. */
  35. function tripal_stock_get_stock_by_stock_id($stock_id) {
  36. $sql = "SELECT nid FROM {chado_stock} WHERE stock_id = :stock_id";
  37. $r = db_query($sql, array(':stock_id' => $stock_id))->fetchObject();
  38. if (!empty($r->nid)) {
  39. return node_load($r->nid);
  40. }
  41. else {
  42. watchdog('tripal_stock', 'tripal_stock_get_stock_by_stock_id(!stock_id): no stock with that stock_id is sync\'d with drupal', array('!stock_id' => $stock_id), WATCHDOG_WARNING);
  43. }
  44. return 0;
  45. }
  46. /**
  47. * Purpose: Returns all stocks currently sync'd with drupal
  48. *
  49. * @return
  50. * An array of node objects keyed by stock_id
  51. *
  52. * @ingroup tripal_stock_api
  53. */
  54. function tripal_stock_get_all_stocks() {
  55. $sql = "SELECT stock_id, nid from {chado_stock}";
  56. $resource = db_query($sql);
  57. $stocks = array();
  58. while ($r = $resource->fetchObject()) {
  59. $node = node_load($r->nid);
  60. if ($node) {
  61. $stocks[$r->stock_id] = $node;
  62. }
  63. }
  64. return $stocks;
  65. }
  66. /**
  67. * Purpose: Return all stocks that match a given criteria
  68. *
  69. * @param $values
  70. * An associative array containing the values for filtering the results.
  71. *
  72. * @return
  73. * An array of matching stock objects (produced using node_load)
  74. * matching the given criteria
  75. *
  76. * Example usage:
  77. * @code
  78. * $values = array(
  79. * 'organism_id' => array(
  80. * 'genus' => 'Lens',
  81. * 'species' => 'culinaris',
  82. * ),
  83. * 'name' => 'CDC Redberry',
  84. * 'type_id' => array (
  85. * 'cv_id' => array (
  86. * 'name' => 'germplasm',
  87. * ),
  88. * 'name' => 'registered_cultivar',
  89. * 'is_obsolete' => 0
  90. * ),
  91. * );
  92. * $result = tripal_stock_get_stocks($values);
  93. * @endcode
  94. * The above code selects a record from the chado stock table using three fields with values which
  95. * identify a stock or multiple stocks. Then the node for each stock identified is returned, if it
  96. * exists. The $values array is nested such that the organism is identified by way of the
  97. * organism_id foreign key constraint by specifying the genus and species. The cvterm is also
  98. * specified using its foreign key and the cv_id for the cvterm is nested as well.
  99. *
  100. * @ingroup tripal_stock_api
  101. */
  102. function tripal_stock_get_stocks($values) {
  103. $stock_ids = tripal_core_chado_select('stock', array('stock_id'), $values);
  104. // Change from stock_ids to nodes-----------------------------------
  105. $stock_ids = array_filter($stock_ids);
  106. $stock_ids = array_unique($stock_ids);
  107. $stocks = array();
  108. foreach ($stock_ids as $stock_id) {
  109. $node = tripal_stock_get_stock_by_stock_id($stock_id->stock_id);
  110. if ($node) {
  111. $stocks[] = $node;
  112. }
  113. }
  114. return $stocks;
  115. }
  116. /**
  117. * Purpose: Retrieve stocks based on associated stock properties
  118. *
  119. * @param $stockprop_values
  120. * An array of column_name => value where column_name is any column in the stockprop table
  121. * and value is the value you want that column to be. This is used as a tripal_core_chado_select
  122. * values array so nesting is allowed.
  123. * @param $stock_values
  124. * An array of column_name => value where column_name is any column in the stock table
  125. * and value is the value you want that column to be. This is used as a tripal_core_chado_select
  126. * values array so nesting is allowed.
  127. *
  128. * @return
  129. * An array of stock node objects
  130. *
  131. * Example usage:
  132. * @code
  133. * $stockprop_values = array(
  134. * 'value' => 'CDC Redberry',
  135. * 'type_id' => array (
  136. * 'cv_id' => array (
  137. * 'name' => 'stock_properties',
  138. * ),
  139. * 'name' => 'synonym',
  140. * 'is_obsolete' => 0
  141. * ),
  142. * );
  143. * $stock_values = array(
  144. * 'organism_id' => array(
  145. * 'genus' => 'Lens',
  146. * 'species' => 'culinaris',
  147. * ),
  148. * );
  149. * $result = tripal_stock_get_stocks_by_stockprop($stockprop_values, $stock_values);
  150. * @endcode
  151. * The above code selects all Lens culinaris stocks with the synonym (stock property) CDC Redberry.
  152. * The nodes for each stock selected are loaded and returned in an array.
  153. *
  154. * @ingroup tripal_stock_api
  155. */
  156. function tripal_stock_get_stocks_by_stockprop($stockprop_values, $stock_values) {
  157. //add stock values to stockprop values
  158. if (!empty($stock_values)) {
  159. $stockprop_values['stock_id'] = $stock_values;
  160. }
  161. //get stock_ids from stockprop table
  162. $stock_ids = tripal_core_chado_select('stockprop', array('stock_id'), $stockprop_values);
  163. // Change from stock_ids to nodes-----------------------------------
  164. $stock_ids = array_filter($stock_ids);
  165. $stock_ids = array_unique($stock_ids);
  166. $stocks = array();
  167. foreach ($stock_ids as $stock_id) {
  168. $node = tripal_stock_get_stock_by_stock_id($stock_id->stock_id);
  169. if ($node) {
  170. $stocks[] = $node;
  171. }
  172. }
  173. return $stocks;
  174. }
  175. /**
  176. * Purpose: Return all stocks with a given name identifier
  177. * which might match stock.name, stock.uniquename, dbxref.accession,
  178. * stockprop.value where stockprop.type='synonym'
  179. *
  180. * @param $name
  181. * The name identfier to be used
  182. * @param $organism_id
  183. * The stock.organism_id of the stock to be selected
  184. *
  185. * @return
  186. * An array of stock node objects
  187. *
  188. * @ingroup tripal_stock_api
  189. */
  190. function tripal_stock_get_stock_by_name_identifier($name, $organism_id) {
  191. $stock_ids = array();
  192. $options = array(
  193. 'case_insensitive_columns' => array('name', 'uniquename', 'accession', 'value')
  194. );
  195. // where name_identifier = stock.name-------------------------------
  196. $current_stocks = tripal_core_chado_select('stock', array('stock_id'),
  197. array(
  198. 'name' => $name,
  199. 'organism_id' => $organism_id,
  200. ),
  201. array(
  202. 'case_insensitive_columns' => array('name'),
  203. )
  204. );
  205. if (!empty($current_stocks)) {
  206. foreach ($current_stocks as $c) {
  207. $stock_ids[] = $c->stock_id; }
  208. }
  209. // where name_identifier = stock.uniquename-------------------------------
  210. $current_stocks = tripal_core_chado_select('stock', array('stock_id'),
  211. array(
  212. 'uniquename' => $name,
  213. 'organism_id' => $organism_id,
  214. ),
  215. array(
  216. 'case_insensitive_columns' => array('uniquename'),
  217. )
  218. );
  219. if (!empty($current_stocks)) {
  220. foreach ($current_stocks as $c) {
  221. $stock_ids[] = $c->stock_id; }
  222. }
  223. // where name_identifier = dbxref.accession-------------------------------
  224. // linked to stock through stock.dbxref
  225. $current_stocks = tripal_core_chado_select('stock', array('stock_id'),
  226. array(
  227. 'dbxref_id' => array(
  228. 'accession' => $name,
  229. ),
  230. 'organism_id' => $organism_id,
  231. ),
  232. array(
  233. 'case_insensitive_columns' => array('accession'),
  234. )
  235. );
  236. if (!empty($current_stocks)) {
  237. foreach ($current_stocks as $c) {
  238. $stock_ids[] = $c->stock_id; }
  239. }
  240. // linked to stock through stock_dbxref?
  241. $current_stocks = tripal_core_chado_select('stock_dbxref', array('stock_id'),
  242. array(
  243. 'dbxref_id' => array(
  244. 'accession' => $name,
  245. ),
  246. 'stock_id' => array(
  247. 'organism_id' => $organism_id,
  248. ),
  249. ),
  250. array(
  251. 'case_insensitive_columns' => array('accession'),
  252. )
  253. );
  254. if (!empty($current_stocks)) {
  255. foreach ($current_stocks as $c) {
  256. $stock_ids[] = $c->stock_id;
  257. }
  258. }
  259. // where name_identifier = stockprop.value-------------------------------
  260. // where type='synonym'
  261. $current_stocks = tripal_core_chado_select('stockprop', array('stock_id'),
  262. array(
  263. 'stock_id' => array(
  264. 'organism_id' => $organism_id,
  265. ),
  266. 'type_id' => array(
  267. 'cv_id' => variable_get('chado_stock_prop_types_cv', 'null'),
  268. 'name' => 'synonym',
  269. ),
  270. 'value' => $name,
  271. ),
  272. array(
  273. 'case_insensitive_columns' => array('value'),
  274. )
  275. );
  276. if (!empty($current_stocks)) {
  277. foreach ($current_stocks as $c) {
  278. $stock_ids[] = $c->stock_id;
  279. }
  280. }
  281. // Change from stock_ids to nodes-----------------------------------
  282. $stock_ids = array_filter($stock_ids);
  283. $stock_ids = array_unique($stock_ids);
  284. $stocks = array();
  285. foreach ($stock_ids as $stock_id) {
  286. $node = tripal_stock_get_stock_by_stock_id($stock_id);
  287. if ($node) {
  288. $stocks[] = $node;
  289. }
  290. }
  291. return $stocks;
  292. }