tripal_stock.api.inc 8.4 KB

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