tripal_cv.api.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /*************************************************************************
  3. * Purpose: To retrieve a chado cv object
  4. *
  5. * @params where_options: array(
  6. * <column_name> => array(
  7. * 'type' => <type of column: INT/STRING>,
  8. * 'value' => <the vlaue you want to filter on>,
  9. * 'exact' => <if TRUE use =; if FALSE use ~>,
  10. * )
  11. * )
  12. * @return chado cv object with all fields from the chado cv table
  13. */
  14. function tripal_cv_get_cv ($where_options) {
  15. $previous_db = tripal_db_set_active('chado');
  16. $where= array();
  17. //generate the where clause from supplied options
  18. // the key is the column name
  19. foreach ($where_options as $key => $val_array) {
  20. if (preg_match('/INT/', $val_array['type'])) {
  21. $where[] = $key."=".$val_array['value'];
  22. } else {
  23. if ($val_array['exact']) { $operator='='; }
  24. else { $operator='~'; }
  25. $where[] = $key.$operator."'".$val_array['value']."'";
  26. }
  27. }
  28. $r = db_fetch_object(db_query(
  29. "SELECT * FROM cv WHERE ".implode(' AND ',$where)
  30. ));
  31. tripal_db_set_active($previous_db);
  32. return $r;
  33. }
  34. /*************************************************************************
  35. * return the cv object for the specified CV name
  36. */
  37. function tripal_cv_get_cv_by_name ($name) {
  38. $previous_db = tripal_db_set_active('chado');
  39. $r = db_fetch_object(db_query("SELECT * FROM cv WHERE name = '%s'",$name));
  40. tripal_db_set_active($previous_db);
  41. return $r;
  42. }
  43. /*************************************************************************
  44. * return the cv object for the specified CV id
  45. */
  46. function tripal_cv_get_cv_by_id ($cv_id) {
  47. $previous_db = tripal_db_set_active('chado');
  48. $r = db_fetch_object(db_query("SELECT * FROM cv WHERE cv_id = %d",$cv_id));
  49. tripal_db_set_active($previous_db);
  50. return $r;
  51. }
  52. /*************************************************************************
  53. * Purpose: Create an options array to be used in a form element
  54. * which provides a list of all chado cvs
  55. *
  56. * @return an array(cv_id => name) for each cv in the chado cv table
  57. */
  58. function tripal_cv_get_cv_options() {
  59. $previous_db = tripal_db_set_active('chado');
  60. $result = db_query(
  61. "SELECT cv_id, name FROM cv"
  62. );
  63. tripal_db_set_active($previous_db);
  64. $options = array();
  65. while ( $r = db_fetch_object($result) ) {
  66. $options[$r->cv_id] = $r->name;
  67. }
  68. return $options;
  69. }
  70. /*************************************************************************
  71. * Purpose: To retrieve a chado cvterm object
  72. *
  73. * @params where_options: array(
  74. * <column_name> => array(
  75. * 'type' => <type of column: INT/STRING>,
  76. * 'value' => <the vlaue you want to filter on>,
  77. * 'exact' => <if TRUE use =; if FALSE use ~>,
  78. * )
  79. * )
  80. * @return chado cvterm object with all fields from the chado cvterm table
  81. */
  82. function tripal_cv_get_cvterm ($where_options) {
  83. $previous_db = tripal_db_set_active('chado');
  84. $where= array();
  85. //generate the where clause from supplied options
  86. // the key is the column name
  87. foreach ($where_options as $key => $val_array) {
  88. if (preg_match('/INT/', $val_array['type'])) {
  89. $where[] = $key."=".$val_array['value'];
  90. } else {
  91. if ($val_array['exact']) { $operator='='; }
  92. else { $operator='~'; }
  93. $where[] = $key.$operator."'".$val_array['value']."'";
  94. }
  95. }
  96. $r = db_fetch_object(db_query(
  97. "SELECT * FROM cvterm WHERE ".implode(' AND ',$where)
  98. ));
  99. tripal_db_set_active($previous_db);
  100. return $r;
  101. }
  102. /*************************************************************************
  103. * Purpose: Retrieve a chado cvterm object with a given name
  104. *
  105. * @params name: the cvterm.name
  106. * @params cv_id: the cv_id of the term you are looking for
  107. * @return cvterm object
  108. */
  109. function tripal_cv_get_cvterm_by_name ($name, $cv_id=0) {
  110. if (!empty($cv_id)) {
  111. $sql = "SELECT * FROM cvterm WHERE name='%s' AND cv_id=%d";
  112. $previous_db = tripal_db_set_active('chado');
  113. $r = db_fetch_object(db_query($sql, $name, $cv_id));
  114. tripal_db_set_active($previous_db);
  115. } else {
  116. $sql = "SELECT * FROM cvterm WHERE name='%s'";
  117. $previous_db = tripal_db_set_active('chado');
  118. $r = db_fetch_object(db_query($sql, $name));
  119. tripal_db_set_active($previous_db);
  120. }
  121. return $r;
  122. }
  123. /*************************************************************************
  124. * Purpose: Create an options array to be used in a form element
  125. * which provides a list of all chado cvterms
  126. *
  127. * @params cv_id: the chado cv_id
  128. * only cvterms with the supplied cv_id will be returned
  129. * @return an array(cvterm_id => name)
  130. * for each cvterm in the chado cvterm table where cv_id=that supplied
  131. */
  132. function tripal_cv_get_cvterm_options($cv_id = 0) {
  133. $previous_db = tripal_db_set_active('chado');
  134. if ($cv_id > 0) {
  135. $result = db_query(
  136. "SELECT cvterm_id, name FROM cvterm WHERE cv_id=%d", $cv_id
  137. );
  138. } else {
  139. $result = db_query(
  140. "SELECT cvterm_id, name FROM cvterm"
  141. );
  142. }
  143. tripal_db_set_active($previous_db);
  144. $options = array();
  145. while ( $r = db_fetch_object($result) ) {
  146. $options[$r->cvterm_id] = $r->name;
  147. }
  148. return $options;
  149. }
  150. /****************************************************************************
  151. * @section Chado Table Descriptions
  152. ****************************************************************************/
  153. /****************************************************************************
  154. * Implements hook_chado_cvterm_schema()
  155. * Purpose: To add descriptions and foreign keys to default table description
  156. * Note: This array will be merged with the array from all other implementations
  157. *
  158. * @return
  159. * Array describing the cvterm table
  160. */
  161. function tripal_stock_chado_cvterm_schema() {
  162. $description = array();
  163. $description['foreign keys']['cv'] = array(
  164. 'table' => 'cv',
  165. 'columns' => array(
  166. 'cv_id' => 'cv_id',
  167. ),
  168. );
  169. $description['foreign keys']['dbxref'] = array(
  170. 'table' => 'dbxref',
  171. 'columns' => array(
  172. 'dbxref_id' => 'dbxref_id',
  173. ),
  174. );
  175. return $description;
  176. }