tripal_chado.semweb.api.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * Adds a new Chado table to the semantic web support for Chado.
  4. *
  5. * Newly added tables (i.e. custom tables) need to be integrated into the
  6. * semantic web infrastructure. After a new table is created and added to
  7. * the Chado schema, this function should be called to indicate that the
  8. * table should be included in the semantic web. No associations are made for
  9. * the columns. The associations should be added using the
  10. * tripal_associate_chado_semweb_term() function.
  11. *
  12. * If the table has already been added previously then this function does
  13. * nothing. It will not overwrite existing assocations.
  14. *
  15. * Temporary tables (e.g. Tripal tables that begin with 'tripal_' and end with
  16. * '_temp', are not supported.
  17. *
  18. * @param $chado_table
  19. * The name of the Chado table.
  20. */
  21. function tripal_add_chado_semweb_table($chado_table) {
  22. // Don't include the tripal temp tables
  23. if (preg_match('/tripal_.+_temp/', $chado_table)) {
  24. return;
  25. }
  26. // Get the table's schema and add all of it's fields if they aren't
  27. // already there.
  28. $schema = chado_get_schema($chado_table);
  29. foreach ($schema['fields'] as $chado_column => $details) {
  30. // If the record already exists don't overwrite it
  31. $record = db_select('chado_semweb', 'CS')
  32. ->fields('CS', array('chado_semweb_id'))
  33. ->condition('CS.chado_table', $chado_table)
  34. ->condition('CS.chado_column', $chado_column)
  35. ->execute()
  36. ->fetchField();
  37. if (!$record) {
  38. $record = array(
  39. 'chado_table' => $chado_table,
  40. 'chado_column' => $chado_column,
  41. );
  42. drupal_write_record('chado_semweb', $record);
  43. }
  44. }
  45. }
  46. /**
  47. * Associates a controlled vocabulary term with a field in a Chado table.
  48. *
  49. * For sharing of data via the semantic web we need to associate a
  50. * term from a controlled vocabulary with every column of every table in Chado.
  51. *
  52. * Temporary tables (e.g. Tripal tables that begin with 'tripal_' and end with
  53. * '_temp', are not supported.
  54. *
  55. * @param $chado_table
  56. * The name of the table in Chado. This argument is optional. If left empty
  57. * or set to NULL then all fields in all Chado tables with that have the
  58. * $column_name will be associated with the provided $term.
  59. * @param $chado_column
  60. * The column name in the Chado table to which the term should be associated.
  61. * @param $term
  62. * A cvterm object as returned by chado_generate_var().
  63. * @param $update
  64. * Set to TRUE if the association should be updated to use the new term
  65. * if a term is already associated with the table and column. Default is
  66. * FALSE. If not TRUE and a term is already associated, then no change
  67. * occurs.
  68. *
  69. * @return boolean
  70. * Returns TRUE if the association was made successfully and FALSE otherwise.
  71. */
  72. function tripal_associate_chado_semweb_term($chado_table, $chado_column, $term,
  73. $update = FALSE) {
  74. // Check for required arguments.
  75. if (!$chado_column) {
  76. tripal_set_message('Please provide the $chado_column argument.', TRIPAL_ERROR);
  77. return FALSE;
  78. }
  79. if (!$term) {
  80. tripal_set_message('Please provide the $term argument.', TRIPAL_ERROR);
  81. return FALSE;
  82. }
  83. // Make sure the field is a real field for the table.
  84. if ($chado_table) {
  85. $schema = chado_get_schema($chado_table);
  86. if (!$schema) {
  87. tripal_set_message('The $chado_table is not a known table in Chado.', TRIPAL_ERROR);
  88. return FALSE;
  89. }
  90. if (!array_key_exists($chado_column, $schema['fields'])) {
  91. tripal_set_message('The $chado_column is not a known column in the $chado_table.', TRIPAL_ERROR);
  92. return FALSE;
  93. }
  94. }
  95. // First check to see if a valid record exists that matches the table and
  96. // column indicated. If it doesn't then insert the record.
  97. $query = db_select('chado_semweb', 'CS')
  98. ->fields('CS', array('chado_semweb_id'))
  99. ->condition('chado_column', $chado_column);
  100. if ($chado_table) {
  101. $query->condition('chado_table', $chado_table);
  102. }
  103. $query->range(0,1);
  104. $id = $query->execute()->fetchField();
  105. if (!$id) {
  106. // If no $chado_table record is provided then return FALSE as we can't
  107. // insert a record without a table.
  108. if (!$chado_table) {
  109. tripal_set_message('The provided $chado_column has no match for any
  110. table currently known. This could be because the table has not yet
  111. been added to the semantic web management. Please provide the
  112. $chado_table.', TRIPAL_ERROR);
  113. return FALSE;
  114. }
  115. // Insert the record.
  116. $id = db_insert('chado_semweb')
  117. ->fields(array(
  118. 'chado_table' => $chado_table,
  119. 'chado_column' => $chado_column,
  120. 'cvterm_id' => $term->cvterm_id,
  121. ));
  122. if ($id) {
  123. return TRUE;
  124. }
  125. else {
  126. tripal_set_message('Failure associating term.', TRIPAL_ERROR);
  127. return FALSE;
  128. }
  129. }
  130. // If the $chado_table argument is empty or NULL then the term applies to
  131. // all fields of the specified name.
  132. $update = db_update('chado_semweb')
  133. ->fields(array(
  134. 'cvterm_id' => $term->cvterm_id
  135. ))
  136. ->condition('chado_column', $chado_column);
  137. if ($chado_table) {
  138. $update->condition('chado_table', $chado_table);
  139. }
  140. if (!$update) {
  141. $update->condition('cvterm_id', NULL);
  142. }
  143. $num_updated = $update->execute();
  144. if (!$num_updated) {
  145. tripal_set_message('Failure associating term.', TRIPAL_ERROR);
  146. return FALSE;
  147. }
  148. return TRUE;
  149. }
  150. /**
  151. * Retrieves the term that maps to the given Chado table and field.
  152. *
  153. * @param $chado_table
  154. * The name of the Chado table.
  155. * @param $chado_column
  156. * The name of the Chado field.
  157. * @param $options
  158. * An associative array of one or more of the following keys:
  159. * -return_object: Set to TRUE to return the cvterm object rather than
  160. * the string version of the term.
  161. *
  162. * @return
  163. * Returns a string-based representation of the term (e.g. SO:0000704). If
  164. * the 'return_object' options is provided then a cvterm object is returned.
  165. * returns NULL if no term is mapped to the table and column.
  166. */
  167. function tripal_get_chado_semweb_term($chado_table, $chado_column, $options = array()) {
  168. $cvterm_id = db_select('chado_semweb', 'CS')
  169. ->fields('CS', array('cvterm_id'))
  170. ->condition('chado_column', $chado_column)
  171. ->condition('chado_table', $chado_table)
  172. ->execute()
  173. ->fetchField();
  174. if ($cvterm_id) {
  175. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
  176. if (array_key_exists('return_object', $options)) {
  177. return $cvterm;
  178. }
  179. return $cvterm->dbxref_id->db_id->name . ':' . $cvterm->dbxref_id->accession;
  180. }
  181. }
  182. /**
  183. * Retreive the column name in a Chado table that matches a given term.
  184. *
  185. * @param $chado_table
  186. * The name of the Chado table.
  187. * @param $term
  188. * The term. This can be a term name or a unique identifer of the form
  189. * {db}:{accession}
  190. *
  191. * @return
  192. * The name of the Chado column that matches the given term or FALSE if the
  193. * term is not mapped to the Chado table.
  194. */
  195. function tripal_get_chado_semweb_column($chado_table, $term) {
  196. $columns = db_select('chado_semweb', 'CS')
  197. ->fields('CS')
  198. ->condition('chado_table', $chado_table)
  199. ->execute();
  200. while($column = $columns->fetchObject()) {
  201. $cvterm_id = $column->cvterm_id;
  202. if ($cvterm_id) {
  203. $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
  204. if ($term == $cvterm->name) {
  205. return $column->chado_column;
  206. }
  207. else if ($term == preg_replace('/ /', '_', $cvterm->name)) {
  208. return $column->chado_column;
  209. }
  210. else if ($term == $cvterm->dbxref_id->db_id->name . ':' . $cvterm->dbxref_id->accession) {
  211. return $column->chado_column;
  212. }
  213. }
  214. }
  215. return FALSE;
  216. }