tripal_chado.api.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * This file contains miscellaneous API functions specific to working with
  6. * records in Chado that do not have a home in any other sub category of
  7. * API functions.
  8. */
  9. /**
  10. * @defgroup tripal_chado_api Chado
  11. *
  12. * @ingroup tripal_api
  13. * The Tripal Chado API is a set of functions for interacting with data
  14. * inside of a Chado relational database. Entities (or pages) in Drupal
  15. * that are provided by Tripal can supply data from any supported database
  16. * back-end, and Chado is the default. This API contains a variety of sub
  17. * categories (or groups) where functions are organized. Any extension module
  18. * that desires to work with data in Chado will find these functions useful.
  19. */
  20. /**
  21. * Publishes content in Chado as a new TripalEntity entity.
  22. *
  23. * @param $values
  24. * A key/value associative array that supports the following keys:
  25. * - bundle_name: The name of the the TripalBundle (e.g. bio_data-12345).
  26. * @param $job_id
  27. * (Optional) The numeric job ID as provided by the Tripal jobs system. There
  28. * is no need to specify this argument if this function is being called
  29. * outside of the jobs systems.
  30. *
  31. * @return boolean
  32. * TRUE if all of the records of the given bundle type were published, and
  33. * FALSE if a failure occured.
  34. *
  35. * @ingroup tripal_chado_api
  36. */
  37. function chado_publish_records($values, $job_id = NULL) {
  38. // We want the job object in order to report progress.
  39. if (is_object($job_id)) {
  40. $job = $job_id;
  41. }
  42. if (is_numeric($job_id)) {
  43. $job = new TripalJob();
  44. $job->load($job_id);
  45. }
  46. $report_progress = FALSE;
  47. if (is_object($job)) {
  48. $report_progress = TRUE;
  49. }
  50. // Make sure we have the required options: bundle_name.
  51. if (!array_key_exists('bundle_name', $values) or !$values['bundle_name']) {
  52. tripal_report_error('tripal_chado', TRIPAL_ERROR,
  53. "Could not publish record: @error",
  54. array('@error' => 'The bundle name must be provided'));
  55. return FALSE;
  56. }
  57. // Get the incoming arguments from the $values array.
  58. $bundle_name = $values['bundle_name'];
  59. $filters = array_key_exists('filters', $values) ? $values['filters'] : array();
  60. $sync_node = array_key_exists('sync_node', $values) ? $values['sync_node'] : '';
  61. // Load the bundle entity so we can get information about which Chado
  62. // table/field this entity belongs to.
  63. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  64. if (!$bundle) {
  65. tripal_report_error('tripal_chado', TRIPAL_ERROR,
  66. "Unknown bundle. Could not publish record: @error",
  67. array('@error' => 'The bundle name must be provided'));
  68. return FALSE;
  69. }
  70. $chado_entity_table = chado_get_bundle_entity_table($bundle);
  71. // Get the mapping of the bio data type to the Chado table.
  72. $chado_bundle = db_select('chado_bundle', 'cb')
  73. ->fields('cb')
  74. ->condition('bundle_id', $bundle->id)
  75. ->execute()
  76. ->fetchObject();
  77. if(!$chado_bundle) {
  78. tripal_report_error('tripal_chado', TRIPAL_ERROR,
  79. "Cannot find mapping of bundle to Chado tables. Could not publish record.");
  80. return FALSE;
  81. }
  82. $table = $chado_bundle->data_table;
  83. $type_column = $chado_bundle->type_column;
  84. $type_linker_table = $chado_bundle->type_linker_table;
  85. $cvterm_id = $chado_bundle->type_id;
  86. $type_value = $chado_bundle->type_value;
  87. // Get the table information for the Chado table.
  88. $table_schema = chado_get_schema($table);
  89. $pkey_field = $table_schema['primary key'][0];
  90. // Construct the SQL for identifying which records should be published.
  91. $args = array();
  92. $select = "SELECT T.$pkey_field as record_id ";
  93. $from = "
  94. FROM {" . $table . "} T
  95. LEFT JOIN [" . $chado_entity_table . "] CE on CE.record_id = T.$pkey_field
  96. ";
  97. // For migration of Tripal v2 nodes to entities we want to include the
  98. // coresponding chado linker table.
  99. if ($sync_node && db_table_exists('chado_' . $table)) {
  100. $select = "SELECT T.$pkey_field as record_id, CT.nid ";
  101. $from .= " INNER JOIN [chado_" . $table . "] CT ON CT.$pkey_field = T.$pkey_field";
  102. }
  103. $where = " WHERE CE.record_id IS NULL ";
  104. // Handle records that are mapped to property tables.
  105. if ($type_linker_table and $type_column and $type_value) {
  106. $propschema = chado_get_schema($type_linker_table);
  107. $fkeys = $propschema['foreign keys'][$table]['columns'];
  108. foreach ($fkeys as $leftkey => $rightkey) {
  109. if ($rightkey == $pkey_field) {
  110. $from .= " INNER JOIN {" . $type_linker_table . "} LT ON T.$pkey_field = LT.$leftkey ";
  111. }
  112. }
  113. $where .= "AND LT.$type_column = :cvterm_id and LT.value = :prop_value";
  114. $args[':cvterm_id'] = $cvterm_id;
  115. $args[':prop_value'] = $type_value;
  116. }
  117. // Handle records that are mapped to cvterm linking tables.
  118. if ($type_linker_table and $type_column and !$type_value) {
  119. $cvtschema = chado_get_schema($type_linker_table);
  120. $fkeys = $cvtschema['foreign keys'][$table]['columns'];
  121. foreach ($fkeys as $leftkey => $rightkey) {
  122. if ($rightkey == $pkey_field) {
  123. $from .= " INNER JOIN {" . $type_linker_table . "} LT ON T.$pkey_field = LT.$leftkey ";
  124. }
  125. }
  126. $where .= "AND LT.$type_column = :cvterm_id";
  127. $args[':cvterm_id'] = $cvterm_id;
  128. }
  129. // Handle records that are mapped via a type_id column in the base table.
  130. if (!$type_linker_table and $type_column) {
  131. $where .= "AND T.$type_column = :cvterm_id";
  132. $args[':cvterm_id'] = $cvterm_id;
  133. }
  134. // Handle the case where records are in the cvterm table and mapped via a single
  135. // vocab. Here we use the type_value for the cv_id.
  136. if ($table == 'cvterm' and $type_value) {
  137. $where .= "AND T.cv_id = :cv_id";
  138. $args[':cv_id'] = $type_value;
  139. }
  140. // Handle the case where records are in the cvterm table but we want to
  141. // use all of the child terms.
  142. if ($table == 'cvterm' and !$type_value) {
  143. $where .= "AND T.cvterm_id IN (
  144. SELECT CVTP.subject_id
  145. FROM {cvtermpath} CVTP
  146. WHERE CVTP.object_id = :cvterm_id)
  147. ";
  148. $args[':cvterm_id'] = $cvterm_id;
  149. }
  150. // Now add in any additional filters
  151. $fields = field_info_field_map();
  152. foreach ($fields as $field_name => $details) {
  153. if (array_key_exists('TripalEntity', $details['bundles']) and
  154. in_array($bundle_name, $details['bundles']['TripalEntity']) and
  155. in_array($field_name, array_keys($filters))){
  156. $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
  157. $chado_table = $instance['settings']['chado_table'];
  158. $chado_column = $instance['settings']['chado_column'];
  159. if ($chado_table == $table) {
  160. $where .= " AND T.$chado_column = :$field_name";
  161. $args[":$field_name"] = $filters[$field_name];
  162. }
  163. }
  164. }
  165. // First get the count
  166. $sql = "SELECT count(*) as num_records " . $from . $where;
  167. $result = chado_query($sql, $args);
  168. $count = $result->fetchField();
  169. // calculate the interval for updates
  170. $interval = intval($count / 50);
  171. if ($interval < 1) {
  172. $interval = 1;
  173. }
  174. // Perform the query.
  175. $sql = $select . $from . $where;
  176. $records = chado_query($sql, $args);
  177. $transaction = db_transaction();
  178. print "\nNOTE: publishing records is performed using a database transaction. \n" .
  179. "If the load fails or is terminated prematurely then the entire set of \n" .
  180. "is rolled back with no changes to the database\n\n";
  181. $i = 0;
  182. printf("%d of %d records. (%0.2f%%) Memory: %s bytes\r", $i, $count, 0, number_format(memory_get_usage()));
  183. try {
  184. while($record = $records->fetchObject()) {
  185. // update the job status every interval
  186. if ($i % $interval == 0) {
  187. $complete = ($i / $count) * 33.33333333;
  188. // Currently don't support setting job progress within a transaction.
  189. // if ($report_progress) { $job->setProgress(intval($complete * 3)); }
  190. printf("%d of %d records. (%0.2f%%) Memory: %s bytes\r", $i, $count, $complete * 3, number_format(memory_get_usage()));
  191. }
  192. // First save the tripal_entity record.
  193. $record_id = $record->record_id;
  194. $ec = entity_get_controller('TripalEntity');
  195. $entity = $ec->create(array(
  196. 'bundle' => $bundle_name,
  197. 'term_id' => $bundle->term_id,
  198. // Add in the Chado details for when the hook_entity_create()
  199. // is called and our tripal_chado_entity_create() implementation
  200. // can deal with it.
  201. 'chado_record' => chado_generate_var($table, array($pkey_field => $record_id)),
  202. 'chado_record_id' => $record_id,
  203. ));
  204. $entity = $entity->save();
  205. if (!$entity) {
  206. throw new Exception('Could not create entity.');
  207. }
  208. // Next save the chado entity record.
  209. $entity_record = array(
  210. 'entity_id' => $entity->id,
  211. 'record_id' => $record_id,
  212. );
  213. // For the Tv2 to Tv3 migration we want to add the nid to the
  214. // entity so we can associate the node with the entity.
  215. if (property_exists($record, 'nid')) {
  216. $entity_record['nid'] = $record->nid;
  217. }
  218. $result = db_insert($chado_entity_table)
  219. ->fields($entity_record)
  220. ->execute();
  221. if(!$result){
  222. throw new Exception('Could not create mapping of entity to Chado record.');
  223. }
  224. $i++;
  225. }
  226. }
  227. catch (Exception $e) {
  228. $transaction->rollback();
  229. $error = $e->getMessage();
  230. tripal_report_error('tripal_chado', TRIPAL_ERROR, "Could not publish record: @error", array('@error' => $error));
  231. drupal_set_message('Failed publishing record. See recent logs for more details.', 'error');
  232. return FALSE;
  233. }
  234. drupal_set_message("Succesfully published $i " . $bundle->label . " record(s).");
  235. return TRUE;
  236. }
  237. /**
  238. * Returns an array of tokens based on Tripal Entity Fields.
  239. *
  240. * @param $base_table
  241. * The name of a base table in Chado.
  242. * @return
  243. * An array of tokens where the key is the machine_name of the token.
  244. *
  245. * @ingroup tripal_chado_api
  246. */
  247. function chado_get_tokens($base_table) {
  248. $tokens = array();
  249. $table_descrip = chado_get_schema($base_table);
  250. foreach ($table_descrip['fields'] as $field_name => $field_details) {
  251. $token = '[' . $base_table . '.' . $field_name . ']';
  252. $location = implode(' > ',array($base_table, $field_name));
  253. $tokens[$token] = array(
  254. 'name' => ucwords(str_replace('_',' ',$base_table)) . ': ' . ucwords(str_replace('_',' ',$field_name)),
  255. 'table' => $base_table,
  256. 'field' => $field_name,
  257. 'token' => $token,
  258. 'description' => array_key_exists('description', $field_details) ? $field_details['description'] : '',
  259. 'location' => $location
  260. );
  261. if (!array_key_exists('description', $field_details) or preg_match('/TODO/',$field_details['description'])) {
  262. $tokens[$token]['description'] = 'The '.$field_name.' field of the '.$base_table.' table.';
  263. }
  264. }
  265. // RECURSION:
  266. // Follow the foreign key relationships recursively
  267. if (array_key_exists('foreign keys', $table_descrip)) {
  268. foreach ($table_descrip['foreign keys'] as $table => $details) {
  269. foreach ($details['columns'] as $left_field => $right_field) {
  270. $sub_token_prefix = $base_table . '.' . $left_field;
  271. $sub_location_prefix = implode(' > ',array($base_table, $left_field));
  272. $sub_tokens = chado_get_tokens($table);
  273. if (is_array($sub_tokens)) {
  274. $tokens = array_merge($tokens, $sub_tokens);
  275. }
  276. }
  277. }
  278. }
  279. return $tokens;
  280. }
  281. /**
  282. * Replace all Chado Tokens in a given string.
  283. *
  284. * NOTE: If there is no value for a token then the token is removed.
  285. *
  286. * @param string $string
  287. * The string containing tokens.
  288. * @param $record
  289. * A Chado record as generated by chado_generate_var()
  290. *
  291. * @return
  292. * The string will all tokens replaced with values.
  293. *
  294. * @ingroup tripal_chado_api
  295. */
  296. function chado_replace_tokens($string, $record) {
  297. // Get the list of tokens
  298. $tokens = chado_get_tokens($record->tablename);
  299. // Determine which tokens were used in the format string
  300. if (preg_match_all('/\[[^]]+\]/', $string, $used_tokens)) {
  301. // Get the value for each token used
  302. foreach ($used_tokens[0] as $token) {
  303. $token_info = $tokens[$token];
  304. if (!empty($token_info)) {
  305. $table = $token_info['table'];
  306. $var = $record;
  307. $value = '';
  308. // Iterate through each portion of the location string. An example string
  309. // might be: stock > type_id > name.
  310. $location = explode('>', $token_info['location']);
  311. foreach ($location as $index) {
  312. $index = trim($index);
  313. // if $var is an object then it is the $node object or a table
  314. // that has been expanded.
  315. if (is_object($var)) {
  316. // check to see if the index is a member of the object. If so,
  317. // then reset the $var to this value.
  318. if (property_exists($var, $index)) {
  319. $value = $var->$index;
  320. }
  321. }
  322. // if the $var is an array then there are multiple instances of the same
  323. // table in a FK relationship (e.g. relationship tables)
  324. elseif (is_array($var)) {
  325. $value = $var[$index];
  326. }
  327. else {
  328. tripal_report_error('tripal_chado', TRIPAL_WARNING,
  329. 'Tokens: Unable to determine the value of %token. Things went awry when trying ' .
  330. 'to access \'%index\' for the following: \'%var\'.',
  331. array('%token' => $token, '%index' => $index, '%var' => print_r($var,TRUE))
  332. );
  333. }
  334. }
  335. $string = str_replace($token, $value, $string);
  336. }
  337. }
  338. }
  339. return $string;
  340. }