tripal_stock.api.inc

  1. 2.x tripal_stock/api/tripal_stock.api.inc
  2. 1.x tripal_stock/api/tripal_stock.api.inc

Provides an application programming interface (API) to manage stocks

File

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