tripal_stock.api.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 get_chado_stock($nid=0, $stock_id=0) {
  11. function triapl_stock_get_stock_by_nid ($nid) {
  12. return node_load($nid);
  13. }
  14. /*************************************************************************
  15. * Purpose: Return a given stock object using the stock id
  16. *
  17. * @return stock object created by node load
  18. */
  19. //function get_chado_stock($nid=0, $stock_id=0) {
  20. function triapl_stock_get_stock_by_stock_id ($stock_id) {
  21. $sql = "SELECT nid FROM {chado_stock} WHERE stock_id=%d";
  22. $r = db_fetch_object(db_query($sql, $stock_id));
  23. if (!empty($r->nid)) {
  24. return node_load($r->nid);
  25. } else {
  26. drupal_set_message("Function: triapl_stock_get_stock_by_stock_id() -no stock with that stock id sync'd with drupal", 'error');
  27. }
  28. return 0;
  29. }
  30. /*************************************************************************
  31. * @section: Return Multiple Stocks
  32. *************************************************************************/
  33. /*************************************************************************
  34. * Purpose: Returns all stocks currently sync'd with drupal
  35. *
  36. * @return array(
  37. * <stock_id> => <stock object created by node load>
  38. * )
  39. */
  40. //function get_all_chado_stocks() {
  41. function tripal_stock_get_all_stocks() {
  42. $sql = "SELECT stock_id, nid from {chado_stock}";
  43. $resource = db_query($sql);
  44. $stocks = array();
  45. while ($r = db_fetch_object($resource)) {
  46. $stocks[$r->stock_id] = node_load($r->nid);
  47. }
  48. return $stocks;
  49. }
  50. /*************************************************************************
  51. * Purpose: Return all stocks that match a given criteria
  52. *
  53. * @params Criteria = array(
  54. * <column name> => array(
  55. * 'value'=> <value of column>,
  56. * 'regex' => <False if exact value, TRUE otherwise>,
  57. * 'type' => <INT or STRING>
  58. * )
  59. * )
  60. * column name can be any of those for the stock table
  61. * additional column names include: accession, synonym
  62. * if you don't know which column the value is from and want to match
  63. * the value against any of name, uniquename, dbxref accessions, and synonyms
  64. * use 'unknown' => array(
  65. * 'value' => <value to search for>,
  66. * 'columns' => array of columns to search
  67. * )
  68. * where columns can be any combination of 'name', 'uniquename', 'stock','accession', or 'synonym'
  69. * @params match_type: can be either ALL or ANY
  70. * where ALL= mathcing stocks must match all criteria supplied
  71. * ANY= matching stock need only match ONE of the supplied criteria
  72. * @return an array of matching stock objects (produced using node_load)
  73. * matching the given criteria
  74. */
  75. //function get_chado_stocks($criteria, $match_type, $organism_id = NULL, $error_checking = FALSE) {
  76. function tripal_stock_get_stocks($criteria, $match_type, $organism_id = NULL, $error_checking = FALSE) {
  77. //Deal with unknown column----------------------------
  78. if (!empty($criteria['unknown'])) {
  79. if ($error_checking) { drupal_set_message('Uknown provided','warning');}
  80. $unknown_provided = TRUE;
  81. $new_criteria = array();
  82. foreach ($criteria['unknown']['columns'] as $column_name) {
  83. if (in_array($column_name, array('stock_id','dbxref_id','organism_id','type_id') )) {
  84. if (preg_match('/^\d+$/',$criteria['unknown']['value'])) {
  85. $new_criteria[$column_name] = array('type'=>'INT','value' => $criteria['unknown']['value']);
  86. }
  87. } else {
  88. $new_criteria[$column_name] = array('type'=>'STRING','value' => $criteria['unknown']['value'], 'regex'=>TRUE);
  89. }
  90. }
  91. if ($error_checking) { drupal_set_message('Re-calling tripal_stock_get_stocks(<pre>'.print_r($new_criteria,TRUE).'</pre>, ANY, '.$organism_id.')','warning'); }
  92. $unknown_stocks = tripal_stock_get_stocks($new_criteria, 'ANY', $organism_id, $error_checking);
  93. if ($error_checking) { drupal_set_message(sizeof($unknown_stocks).' Unknown Stocks', 'warning'); }
  94. }
  95. unset($criteria['unknown']); //so it's not mistaken as a column in the stock table
  96. // Determine all criteria------------------------------
  97. $where = array(); // parts of the where portion of the SQL query
  98. $joins = array(); //joins between the stock table and other tables
  99. foreach ($criteria as $column_name => $v) {
  100. if (preg_match("/accession/i",$column_name)) {
  101. if ($v['regex']) { $operator = '~'; }
  102. else { $operator = '='; }
  103. $where[] = 'dbxref.accession'.$operator."'".$v['value']."'";
  104. $joins[] = 'LEFT JOIN stock_dbxref stock_dbxref ON stock_dbxref.stock_id=stock.stock_id';
  105. $joins[] = 'LEFT JOIN dbxref dbxref ON dbxref.dbxref_id=stock_dbxref.dbxref_id';
  106. } elseif (preg_match("/synonym/i",$column_name)) {
  107. if ($v['regex']) { $operator = '~'; }
  108. else { $operator = '='; }
  109. $synonym_cvterm = tripal_cv_get_cvterm_by_name('synonym'),
  110. 'cv_id' => array('type'=>'INT', 'value'=> variable_get('chado_stock_prop_types_cv', 'null')) ));
  111. $where[] = '(stockprop.type_id='.$synonym_cvterm->cvterm_id.' AND stockprop.value'.$operator."'".$v['value']."')";
  112. $joins[] = 'LEFT JOIN stockprop stockprop ON stockprop.stock_id=stock.stock_id';
  113. } else {
  114. if ($v['regex']) { $operator = '~'; }
  115. else { $operator = '='; }
  116. if (preg_match('/INT/', $v['type'])) {
  117. $where[] = 'stock.'.$column_name.'='.$v['value'];
  118. } else {
  119. $where[] = 'stock.'.$column_name.$operator."'".$v['value']."'";
  120. }
  121. }
  122. }
  123. if ($error_checking) {
  124. drupal_set_message('Where Conditions: <pre>'.print_r($where,TRUE).'</pre>', 'warning');
  125. drupal_set_message('Left Joins: <pre>'.print_r($joins,TRUE).'</pre>', 'warning');
  126. }
  127. //Build query-----------------------------------------
  128. if (preg_match('/ANY/', $match_type)) {
  129. $where_string = implode(' OR ',$where);
  130. if ($organism_id) {
  131. $where_string = '('.$where_string.') AND organism_id='.$organism_id;
  132. }
  133. } else {
  134. $where_string = implode(' AND ',$where);
  135. if ($organism_id) {
  136. $where_string .= ' AND organism_id='.$organism_id; }
  137. }
  138. if (sizeof($where) >= 1) {
  139. $execute_query = TRUE;
  140. $sql_query = 'SELECT stock.stock_id FROM stock '.implode(' ',$joins).' WHERE '.$where_string;
  141. //drupal_set_message('Query='.$sql_query);
  142. } elseif (!$unknown_provided) {
  143. $execute_query = TRUE;
  144. $sql_query = 'SELECT stock.stock_id FROM stock';
  145. drupal_set_message('You did not enter any criteria during the stock selection process. All stocks will be returned.','warning');
  146. } else {
  147. $execute_query = FALSE;
  148. }
  149. if ($error_checking) { drupal_set_message('Query: '.$sql_query, 'warning'); }
  150. if ($execute_query) {
  151. //Get stock_ids---------------------------------------
  152. $previous_db = tripal_db_set_active('chado');
  153. $resource = db_query($sql_query);
  154. tripal_db_set_active($previous_db);
  155. $stock_ids = array();
  156. while ($r = db_fetch_object($resource)) {
  157. $stock_ids[] = $r->stock_id;
  158. }
  159. $stock_ids = array_unique($stock_ids);
  160. if ($error_checking) { drupal_set_message('Stock IDs: <pre>'.print_r($stock_ids,TRUE).'</pre>', 'warning'); }
  161. //Get Stocks------------------------------------------
  162. if (!empty($stock_ids)) {
  163. $resource = db_query("SELECT nid FROM {chado_stock} WHERE stock_id IN (%s)",implode(',',$stock_ids));
  164. $main_stocks = array();
  165. while ($r = db_fetch_object($resource)) {
  166. $main_stocks[] = node_load($r->nid);
  167. }
  168. }
  169. }
  170. if (!empty($main_stocks)) {
  171. if(!empty($unknown_stocks)){
  172. return array_merge($unknown_stocks,$main_stocks);
  173. } else {
  174. return $main_stocks;
  175. }
  176. } else {
  177. if(!empty($unknown_stocks)){
  178. return $unknown_stocks;
  179. } else {
  180. //drupal_set_message('No Stocks matched the given criteria','warning');
  181. return array();
  182. }
  183. }
  184. }