other_module_api_functions.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. ///////////////////////////////////////////////////////////////////////////
  3. // Module: tripal_core
  4. ///////////////////////////////////////////////////////////////////////////
  5. /*************************************************************************
  6. * Purpose: Get max rank for a given set of criteria
  7. * This function was developed with the many property tables in chado in mind
  8. *
  9. * @params tablename: the name of the chado table you want to select the max rank from
  10. * this table must contain a rank column of type integer
  11. * @params where_options: array(
  12. * <column_name> => array(
  13. * 'type' => <type of column: INT/STRING>,
  14. * 'value' => <the vlaue you want to filter on>,
  15. * 'exact' => <if TRUE use =; if FALSE use ~>,
  16. * )
  17. * )
  18. * where options should include the id and type for that table to correctly
  19. * group a set of records together where the only difference are the value and rank
  20. * @return the maximum rank
  21. *
  22. */
  23. function get_max_chado_rank ($tablename, $where_options) {
  24. $where= array();
  25. //generate the where clause from supplied options
  26. // the key is the column name
  27. foreach ($where_options as $key => $val_array) {
  28. if (preg_match('/INT/', $val_array['type'])) {
  29. $where[] = $key."=".$val_array['value'];
  30. } else {
  31. if ($val_array['exact']) { $operator='='; }
  32. else { $operator='~'; }
  33. $where[] = $key.$operator."'".$val_array['value']."'";
  34. }
  35. }
  36. $previous_db = tripal_db_set_active('chado');
  37. $result = db_fetch_object(db_query(
  38. "SELECT max(rank) as max_rank, count(rank) as count FROM %s WHERE %s",
  39. $tablename,
  40. implode(' AND ',$where)
  41. ));
  42. tripal_db_set_active($previous_db);
  43. if ($result->count > 0) {
  44. return $result->max_rank;
  45. } else {
  46. return -1;
  47. }
  48. }
  49. ///////////////////////////////////////////////////////////////////////////
  50. // Module: tripal_cv
  51. ///////////////////////////////////////////////////////////////////////////
  52. /*************************************************************************
  53. * Purpose: To retrieve a chado cv object
  54. *
  55. * @params where_options: array(
  56. * <column_name> => array(
  57. * 'type' => <type of column: INT/STRING>,
  58. * 'value' => <the vlaue you want to filter on>,
  59. * 'exact' => <if TRUE use =; if FALSE use ~>,
  60. * )
  61. * )
  62. * @return chado cv object with all fields from the chado cv table
  63. */
  64. function get_chado_cv ($where_options) {
  65. $previous_db = tripal_db_set_active('chado');
  66. $where= array();
  67. //generate the where clause from supplied options
  68. // the key is the column name
  69. foreach ($where_options as $key => $val_array) {
  70. if (preg_match('/INT/', $val_array['type'])) {
  71. $where[] = $key."=".$val_array['value'];
  72. } else {
  73. if ($val_array['exact']) { $operator='='; }
  74. else { $operator='~'; }
  75. $where[] = $key.$operator."'".$val_array['value']."'";
  76. }
  77. }
  78. $r = db_fetch_object(db_query(
  79. "SELECT * FROM cv WHERE ".implode(' AND ',$where)
  80. ));
  81. tripal_db_set_active($previous_db);
  82. return $r;
  83. }
  84. /*************************************************************************
  85. * Purpose: Create an options array to be used in a form element
  86. * which provides a list of all chado cvs
  87. *
  88. * @return an array(cv_id => name) for each cv in the chado cv table
  89. */
  90. function get_chado_cv_options() {
  91. $previous_db = tripal_db_set_active('chado');
  92. $result = db_query(
  93. "SELECT cv_id, name FROM cv"
  94. );
  95. db_set_active($previous_db);
  96. $options = array();
  97. while ( $r = db_fetch_object($result) ) {
  98. $options[$r->cv_id] = $r->name;
  99. }
  100. return $options;
  101. }
  102. /*************************************************************************
  103. * Purpose: To retrieve a chado cvterm object
  104. *
  105. * @params where_options: array(
  106. * <column_name> => array(
  107. * 'type' => <type of column: INT/STRING>,
  108. * 'value' => <the vlaue you want to filter on>,
  109. * 'exact' => <if TRUE use =; if FALSE use ~>,
  110. * )
  111. * )
  112. * @return chado cvterm object with all fields from the chado cvterm table
  113. */
  114. function get_chado_cvterm ($where_options) {
  115. $previous_db = tripal_db_set_active('chado');
  116. $where= array();
  117. //generate the where clause from supplied options
  118. // the key is the column name
  119. foreach ($where_options as $key => $val_array) {
  120. if (preg_match('/INT/', $val_array['type'])) {
  121. $where[] = $key."=".$val_array['value'];
  122. } else {
  123. if ($val_array['exact']) { $operator='='; }
  124. else { $operator='~'; }
  125. $where[] = $key.$operator."'".$val_array['value']."'";
  126. }
  127. }
  128. $r = db_fetch_object(db_query(
  129. "SELECT * FROM cvterm WHERE ".implode(' AND ',$where)
  130. ));
  131. tripal_db_set_active($previous_db);
  132. return $r;
  133. }
  134. /*************************************************************************
  135. * Purpose: Create an options array to be used in a form element
  136. * which provides a list of all chado cvterms
  137. *
  138. * @params cv_id: the chado cv_id
  139. * only cvterms with the supplied cv_id will be returned
  140. * @return an array(cvterm_id => name)
  141. * for each cvterm in the chado cvterm table where cv_id=that supplied
  142. */
  143. function get_chado_cvterm_options($cv_id = 0) {
  144. $previous_db = tripal_db_set_active('chado');
  145. if ($cv_id > 0) {
  146. $result = db_query(
  147. "SELECT cvterm_id, name FROM cvterm WHERE cv_id=%d", $cv_id
  148. );
  149. } else {
  150. $result = db_query(
  151. "SELECT cvterm_id, name FROM cvterm"
  152. );
  153. }
  154. db_set_active($previous_db);
  155. $options = array();
  156. while ( $r = db_fetch_object($result) ) {
  157. $options[$r->cvterm_id] = $r->name;
  158. }
  159. return $options;
  160. }
  161. ///////////////////////////////////////////////////////////////////////////
  162. // Module: tripal_db
  163. ///////////////////////////////////////////////////////////////////////////
  164. /*************************************************************************
  165. * Purpose: Create an options array to be used in a form element
  166. * which provides a list of all chado dbs
  167. *
  168. * @return an array(db_id => name) for each db in the chado db table
  169. */
  170. function get_chado_db_options() {
  171. $previous_db = tripal_db_set_active('chado');
  172. $result = db_query(
  173. "SELECT db_id, name FROM db"
  174. );
  175. db_set_active($previous_db);
  176. $options = array();
  177. while ( $r = db_fetch_object($result) ) {
  178. $options[$r->db_id] = $r->name;
  179. }
  180. return $options;
  181. }
  182. /*************************************************************************
  183. * Purpose: To retrieve a chado dbxref object
  184. *
  185. * @params where_options: array(
  186. * <column_name> => array(
  187. * 'type' => <type of column: INT/STRING>,
  188. * 'value' => <the vlaue you want to filter on>,
  189. * 'exact' => <if TRUE use =; if FALSE use ~>,
  190. * )
  191. * )
  192. * @return chado dbxref object with all fields from the chado dbxref table
  193. */
  194. function get_chado_dbxref ($where_options) {
  195. $previous_db = tripal_db_set_active('chado');
  196. $where= array();
  197. //generate the where clause from supplied options
  198. // the key is the column name
  199. foreach ($where_options as $key => $val_array) {
  200. if (preg_match('/INT/', $val_array['type'])) {
  201. $where[] = $key."=".$val_array['value'];
  202. } else {
  203. if ($val_array['exact']) { $operator='='; }
  204. else { $operator='~'; }
  205. $where[] = $key.$operator."'".$val_array['value']."'";
  206. }
  207. }
  208. $r = db_fetch_object(db_query(
  209. "SELECT * FROM dbxref WHERE ".implode(' AND ',$where)
  210. ));
  211. tripal_db_set_active($previous_db);
  212. return $r;
  213. }
  214. ///////////////////////////////////////////////////////////////////////////
  215. // Module: tripal_organism
  216. ///////////////////////////////////////////////////////////////////////////
  217. /*************************************************************************
  218. * Purpose: Create an options array to be used in a form element
  219. * which provides a list of all chado organisms
  220. *
  221. * @return an array(organism_id => common_name)
  222. * for each organism in the chado organism table
  223. */
  224. function get_chado_organism_options() {
  225. $previous_db = tripal_db_set_active('chado');
  226. $result = db_query(
  227. "SELECT organism_id, common_name FROM organism"
  228. );
  229. tripal_db_set_active($previous_db);
  230. $options = array();
  231. while ( $r = db_fetch_object($result) ) {
  232. $options[$r->organism_id] = $r->common_name;
  233. }
  234. return $options;
  235. }
  236. /*************************************************************************
  237. * Purpose: Return a given organism object using the nid or organism id
  238. *
  239. * @return organism object created by node load
  240. */
  241. function get_chado_organism($nid=0, $organism_id=0) {
  242. if (!empty($nid)) {
  243. return node_load($nid);
  244. } else {
  245. if (!empty($organism_id)) {
  246. $sql = "SELECT nid FROM {chado_organism} WHERE organism_id=%d";
  247. $r = db_fetch_object(db_query($sql, $organism_id));
  248. if (!empty($r->nid)) {
  249. return node_load($r->nid);
  250. } else {
  251. drupal_set_message("Function: get_chado_organism() -no organism with that organism id sync'd with drupal", 'error');
  252. }
  253. } else {
  254. drupal_set_message("Function: get_chado_organism() -need to supply at least one of node ID or Organism ID",'error');
  255. }
  256. }
  257. return 0;
  258. }