tripal_stock.api.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. $stocks[$r->stock_id] = node_load($r->nid);
  43. }
  44. return $stocks;
  45. }
  46. /*************************************************************************
  47. * Purpose: Return all stocks that match a given criteria
  48. *
  49. * @params $values
  50. * An associative array containing the values for filtering the results.
  51. * @return
  52. * An array of matching stock objects (produced using node_load)
  53. * matching the given criteria
  54. *
  55. * Example usage:
  56. * @code
  57. * $values = array(
  58. * 'organism_id' => array(
  59. * 'genus' => 'Lens',
  60. * 'species' => 'culinaris',
  61. * ),
  62. * 'name' => 'CDC Redberry',
  63. * 'type_id' => array (
  64. * 'cv_id' => array (
  65. * 'name' => 'germplasm',
  66. * ),
  67. * 'name' => 'registered_cultivar',
  68. * 'is_obsolete' => 0
  69. * ),
  70. * );
  71. * $result = tripal_stock_get_stocks($values);
  72. * @endcode
  73. * The above code selects a record from the chado stock table using three fields with values which
  74. * identify a stock or multiple stocks. Then the node for each stock identified is returned, if it
  75. * exists. The $values array is nested such that the organism is identified by way of the
  76. * organism_id foreign key constraint by specifying the genus and species. The cvterm is also
  77. * specified using its foreign key and the cv_id for the cvterm is nested as well.
  78. */
  79. function tripal_stock_get_stocks($values) {
  80. $stock_ids = tripal_core_chado_select('stock',array('stock_id'),$values);
  81. // Change from stock_ids to nodes-----------------------------------
  82. $stock_ids = array_filter($stock_ids);
  83. $stock_ids = array_unique($stock_ids);
  84. $stocks = array();
  85. foreach ($stock_ids as $stock_id) {
  86. $stocks[] = tripal_stock_get_stock_by_stock_id($stock_id->stock_id);
  87. }
  88. return $stocks;
  89. }
  90. /*************************************************************************
  91. * Purpose: Return all stocks with a given name identifier
  92. * which might match stock.name, stock.uniquename, dbxref.accession,
  93. * stockprop.value where stockprop.type='synonym'
  94. *
  95. * @param $name
  96. * The name identfier to be used
  97. * @params $organism_id
  98. * The stock.organism_id of the stock to be selected
  99. * @return
  100. * An array of stock node objects
  101. */
  102. function tripal_stock_get_stock_by_name_identifier($name, $organism_id) {
  103. $stock_ids = array();
  104. // where name_identifier = stock.name-------------------------------
  105. $current_stocks = tripal_core_chado_select('stock',array('stock_id'),
  106. array(
  107. 'name' => $name,
  108. 'organism_id' => $organism_id,
  109. )
  110. );
  111. if (!empty($current_stocks)) {
  112. $stock_ids = array_merge($stock_ids, $current_stocks);
  113. }
  114. // where name_identifier = stock.uniquename-------------------------------
  115. $current_stocks = tripal_core_chado_select('stock',array('stock_id'),
  116. array(
  117. 'uniquename' => $name,
  118. 'organism_id' => $organism_id,
  119. )
  120. );
  121. if (!empty($current_stocks)) {
  122. $stock_ids = array_merge($stock_ids, $current_stocks);
  123. }
  124. // where name_identifier = dbxref.accession-------------------------------
  125. // linked to stock through stock.dbxref
  126. $current_stocks = tripal_core_chado_select('stock',array('stock_id'),
  127. array(
  128. 'dbxref_id' => array(
  129. 'accession' => $name,
  130. ),
  131. 'organism_id' => $organism_id,
  132. )
  133. );
  134. if (!empty($current_stocks)) {
  135. $stock_ids = array_merge($stock_ids, $current_stocks);
  136. }
  137. // linked to stock through stock_dbxref?
  138. // where name_identifier = stockprop.value-------------------------------
  139. // where type='synonym'
  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. $stocks[] = tripal_stock_get_stock_by_stock_id($stock_id->stock_id);
  146. }
  147. return $stocks;
  148. }