tripal_stock.api.inc 8.5 KB

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