tripal_stock-api_functions.inc 7.1 KB

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