other_module_api_functions.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 value 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. //drupal_set_message("Max Rank Query=SELECT max(rank) as max_rank, count(rank) as count FROM ".$tablename." WHERE ".implode(' AND ',$where));
  44. if ($result->count > 0) {
  45. return $result->max_rank;
  46. } else {
  47. return -1;
  48. }
  49. }
  50. ///////////////////////////////////////////////////////////////////////////
  51. // Module: tripal_cv
  52. ///////////////////////////////////////////////////////////////////////////
  53. /*************************************************************************
  54. * Purpose: To retrieve a chado cv object
  55. *
  56. * @params where_options: array(
  57. * <column_name> => array(
  58. * 'type' => <type of column: INT/STRING>,
  59. * 'value' => <the vlaue you want to filter on>,
  60. * 'exact' => <if TRUE use =; if FALSE use ~>,
  61. * )
  62. * )
  63. * @return chado cv object with all fields from the chado cv table
  64. */
  65. function get_chado_cv ($where_options) {
  66. $previous_db = tripal_db_set_active('chado');
  67. $where= array();
  68. //generate the where clause from supplied options
  69. // the key is the column name
  70. foreach ($where_options as $key => $val_array) {
  71. if (preg_match('/INT/', $val_array['type'])) {
  72. $where[] = $key."=".$val_array['value'];
  73. } else {
  74. if ($val_array['exact']) { $operator='='; }
  75. else { $operator='~'; }
  76. $where[] = $key.$operator."'".$val_array['value']."'";
  77. }
  78. }
  79. $r = db_fetch_object(db_query(
  80. "SELECT * FROM cv WHERE ".implode(' AND ',$where)
  81. ));
  82. tripal_db_set_active($previous_db);
  83. return $r;
  84. }
  85. /*************************************************************************
  86. * Purpose: Create an options array to be used in a form element
  87. * which provides a list of all chado cvs
  88. *
  89. * @return an array(cv_id => name) for each cv in the chado cv table
  90. */
  91. function get_chado_cv_options() {
  92. $previous_db = tripal_db_set_active('chado');
  93. $result = db_query(
  94. "SELECT cv_id, name FROM cv"
  95. );
  96. db_set_active($previous_db);
  97. $options = array();
  98. while ( $r = db_fetch_object($result) ) {
  99. $options[$r->cv_id] = $r->name;
  100. }
  101. return $options;
  102. }
  103. /*************************************************************************
  104. * Purpose: To retrieve a chado cvterm object
  105. *
  106. * @params where_options: array(
  107. * <column_name> => array(
  108. * 'type' => <type of column: INT/STRING>,
  109. * 'value' => <the vlaue you want to filter on>,
  110. * 'exact' => <if TRUE use =; if FALSE use ~>,
  111. * )
  112. * )
  113. * @return chado cvterm object with all fields from the chado cvterm table
  114. */
  115. function get_chado_cvterm ($where_options) {
  116. $previous_db = tripal_db_set_active('chado');
  117. $where= array();
  118. //generate the where clause from supplied options
  119. // the key is the column name
  120. foreach ($where_options as $key => $val_array) {
  121. if (preg_match('/INT/', $val_array['type'])) {
  122. $where[] = $key."=".$val_array['value'];
  123. } else {
  124. if ($val_array['exact']) { $operator='='; }
  125. else { $operator='~'; }
  126. $where[] = $key.$operator."'".$val_array['value']."'";
  127. }
  128. }
  129. $r = db_fetch_object(db_query(
  130. "SELECT * FROM cvterm WHERE ".implode(' AND ',$where)
  131. ));
  132. tripal_db_set_active($previous_db);
  133. return $r;
  134. }
  135. /*************************************************************************
  136. * Purpose: Create an options array to be used in a form element
  137. * which provides a list of all chado cvterms
  138. *
  139. * @params cv_id: the chado cv_id
  140. * only cvterms with the supplied cv_id will be returned
  141. * @return an array(cvterm_id => name)
  142. * for each cvterm in the chado cvterm table where cv_id=that supplied
  143. */
  144. function get_chado_cvterm_options($cv_id = 0) {
  145. $previous_db = tripal_db_set_active('chado');
  146. if ($cv_id > 0) {
  147. $result = db_query(
  148. "SELECT cvterm_id, name FROM cvterm WHERE cv_id=%d", $cv_id
  149. );
  150. } else {
  151. $result = db_query(
  152. "SELECT cvterm_id, name FROM cvterm"
  153. );
  154. }
  155. db_set_active($previous_db);
  156. $options = array();
  157. while ( $r = db_fetch_object($result) ) {
  158. $options[$r->cvterm_id] = $r->name;
  159. }
  160. return $options;
  161. }
  162. ///////////////////////////////////////////////////////////////////////////
  163. // Module: tripal_db
  164. ///////////////////////////////////////////////////////////////////////////
  165. /*************************************************************************
  166. * Purpose: To retrieve a chado db object
  167. *
  168. * @params where_options: array(
  169. * <column_name> => array(
  170. * 'type' => <type of column: INT/STRING>,
  171. * 'value' => <the vlaue you want to filter on>,
  172. * 'exact' => <if TRUE use =; if FALSE use ~>,
  173. * )
  174. * )
  175. * @return chado db object with all fields from the chado db table
  176. */
  177. function get_chado_db ($where_options) {
  178. $previous_db = tripal_db_set_active('chado');
  179. $where= array();
  180. //generate the where clause from supplied options
  181. // the key is the column name
  182. foreach ($where_options as $key => $val_array) {
  183. if (preg_match('/INT/', $val_array['type'])) {
  184. $where[] = $key."=".$val_array['value'];
  185. } else {
  186. if ($val_array['exact']) { $operator='='; }
  187. else { $operator='~'; }
  188. $where[] = $key.$operator."'".$val_array['value']."'";
  189. }
  190. }
  191. $r = db_fetch_object(db_query(
  192. "SELECT * FROM db WHERE ".implode(' AND ',$where)
  193. ));
  194. tripal_db_set_active($previous_db);
  195. return $r;
  196. }
  197. /*************************************************************************
  198. * Purpose: Create an options array to be used in a form element
  199. * which provides a list of all chado dbs
  200. *
  201. * @return an array(db_id => name) for each db in the chado db table
  202. */
  203. function get_chado_db_options() {
  204. $previous_db = tripal_db_set_active('chado');
  205. $result = db_query(
  206. "SELECT db_id, name FROM db"
  207. );
  208. db_set_active($previous_db);
  209. $options = array();
  210. while ( $r = db_fetch_object($result) ) {
  211. $options[$r->db_id] = $r->name;
  212. }
  213. return $options;
  214. }
  215. /*************************************************************************
  216. * Purpose: To retrieve a chado dbxref object
  217. *
  218. * @params where_options: array(
  219. * <column_name> => array(
  220. * 'type' => <type of column: INT/STRING>,
  221. * 'value' => <the vlaue you want to filter on>,
  222. * 'exact' => <if TRUE use =; if FALSE use ~>,
  223. * )
  224. * )
  225. * @return chado dbxref object with all fields from the chado dbxref table
  226. */
  227. function get_chado_dbxref ($where_options) {
  228. $previous_db = tripal_db_set_active('chado');
  229. $where= array();
  230. //generate the where clause from supplied options
  231. // the key is the column name
  232. foreach ($where_options as $key => $val_array) {
  233. if (preg_match('/INT/', $val_array['type'])) {
  234. $where[] = $key."=".$val_array['value'];
  235. } else {
  236. if ($val_array['exact']) { $operator='='; }
  237. else { $operator='~'; }
  238. $where[] = $key.$operator."'".$val_array['value']."'";
  239. }
  240. }
  241. $r = db_fetch_object(db_query(
  242. "SELECT * FROM dbxref WHERE ".implode(' AND ',$where)
  243. ));
  244. tripal_db_set_active($previous_db);
  245. return $r;
  246. }
  247. ///////////////////////////////////////////////////////////////////////////
  248. // Module: tripal_organism
  249. ///////////////////////////////////////////////////////////////////////////
  250. /*************************************************************************
  251. * Purpose: Create an options array to be used in a form element
  252. * which provides a list of all chado organisms
  253. *
  254. * @return an array(organism_id => common_name)
  255. * for each organism in the chado organism table
  256. */
  257. function get_chado_organism_options() {
  258. $previous_db = tripal_db_set_active('chado');
  259. $result = db_query(
  260. "SELECT organism_id, common_name FROM organism"
  261. );
  262. tripal_db_set_active($previous_db);
  263. $options = array();
  264. while ( $r = db_fetch_object($result) ) {
  265. $options[$r->organism_id] = $r->common_name;
  266. }
  267. return $options;
  268. }
  269. /*************************************************************************
  270. * Purpose: Return a given organism object using the nid or organism id
  271. *
  272. * @return organism object created by node load
  273. */
  274. function get_chado_organism($nid=0, $organism_id=0) {
  275. if (!empty($nid)) {
  276. return node_load($nid);
  277. } else {
  278. if (!empty($organism_id)) {
  279. $sql = "SELECT nid FROM {chado_organism} WHERE organism_id=%d";
  280. $r = db_fetch_object(db_query($sql, $organism_id));
  281. if (!empty($r->nid)) {
  282. return node_load($r->nid);
  283. } else {
  284. drupal_set_message("Function: get_chado_organism() -no organism with that organism id sync'd with drupal", 'error');
  285. }
  286. } else {
  287. drupal_set_message("Function: get_chado_organism() -need to supply at least one of node ID or Organism ID",'error');
  288. }
  289. }
  290. return 0;
  291. }