123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
- function tripal_load_chado_entity($table, $record_id) {
- $entity_id = db_select('chado_entity', 'ce')
- ->fields('ce', array('entity_id'))
- ->condition('ce.record_id', $record_id)
- ->condition('ce.data_table', $table)
- ->execute()
- ->fetchField();
- if ($entity_id) {
- $entity = entity_load('TripalEntity', array($entity_id));
- return reset($entity);
- }
- return NULL;
- }
- function tripal_get_chado_entity_id($data_table, $record_id) {
- return db_select('chado_entity', 'ce')
- ->fields('ce', array('entity_id'))
- ->condition('data_table', $data_table)
- ->condition('record_id', $record_id)
- ->execute()
- ->fetchField();
- }
- function tripal_chado_publish_records($values, $job_id = NULL) {
-
- if (!array_key_exists('bundle_name', $values) or !$values['bundle_name']) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR,
- "Could not publish record: @error",
- array('@error' => 'The bundle name must be provided'));
- return FALSE;
- }
- $bundle_name = $values['bundle_name'];
- $sync_node = array_key_exists('sync_node', $values) ? $values['sync_node'] : '';
-
-
- $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
- $bundle_id = $bundle->id;
- $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
- $vocab = $term->vocab;
- $params = array(
- 'vocabulary' => $vocab->vocabulary,
- 'accession' => $term->accession,
- );
- $mapped_table = chado_get_cvterm_mapping($params);
- $table = $mapped_table->chado_table;
- $column = $mapped_table->chado_field;
- $cvterm_id = $mapped_table->cvterm->cvterm_id;
-
- $table_schema = chado_get_schema($table);
- $pkey_field = $table_schema['primary key'][0];
-
- $select = "SELECT $pkey_field as record_id ";
- $from = "
- FROM {" . $table . "} T
- LEFT JOIN public.chado_entity CE on CE.record_id = T.$pkey_field
- AND CE.data_table = '$table'
- ";
-
-
- if ($sync_node && db_table_exists('chado_' . $table)) {
- $select = "SELECT T.$pkey_field as record_id, CT.nid ";
- $from .= "INNER JOIN public.chado_$table CT ON CT.$pkey_field = T.$pkey_field";
- }
- $where = " WHERE CE.record_id IS NULL ";
-
-
-
- if ($table != 'analysis' and $table != 'organism') {
- $where .= "AND $column = $cvterm_id";
- }
-
- $sql = "SELECT count(*) as num_records " . $from . $where;
- $result = chado_query($sql);
- $count = $result->fetchField();
-
- $interval = intval($count / 1000);
- if ($interval < 1) {
- $interval = 1;
- }
-
- $sql = $select . $from . $where;
- $records = chado_query($sql);
- $transaction = db_transaction();
- printf("%d of %d records. (%0.2f%%) Memory: %s bytes\r", $i, $count, 0, number_format(memory_get_usage()));
- try {
- $i = 0;
- while($record = $records->fetchObject()) {
-
-
- $complete = ($i / $count) * 33.33333333;
-
- printf("%d of %d records. (%0.2f%%) Memory: %s bytes\r", $i, $count, $complete * 3, number_format(memory_get_usage()));
-
-
- $record_id = $record->record_id;
- $ec = entity_get_controller('TripalEntity');
- $entity = $ec->create(array(
- 'bundle' => $bundle_name,
- 'term_id' => $bundle->term_id,
- 'chado_record' => chado_generate_var($table, array($pkey_field => $record_id)),
- 'chado_record_id' => $record_id,
- ));
- $entity = $entity->save();
- if (!$entity) {
- throw new Exception('Could not create entity.');
- }
-
- $entity_record = array(
- 'entity_id' => $entity->id,
- 'record_id' => $record_id,
- 'data_table' => $table,
- 'type_table' => $table,
- 'field' => $column,
- );
-
-
- if (property_exists($record, 'nid')) {
- $entity_record['nid'] = $record->nid;
- }
- $success = drupal_write_record('chado_entity', $entity_record);
- $i++;
- }
- }
- catch (Exception $e) {
- $transaction->rollback();
- $error = $e->getMessage();
- tripal_report_error('tripal_chado', TRIPAL_ERROR, "Could not publish record: @error", array('@error' => $error));
- drupal_set_message('Failed publishing record. See recent logs for more details.', 'error');
- return FALSE;
- }
- drupal_set_message("Succesfully published $i " . $bundle->label . " record(s).");
- return TRUE;
- }
- function tripal_get_chado_tokens($base_table) {
- $tokens = array();
- $table_descrip = chado_get_schema($base_table);
- foreach ($table_descrip['fields'] as $field_name => $field_details) {
- $token = '[' . $base_table . '.' . $field_name . ']';
- $location = implode(' > ',array($base_table, $field_name));
- $tokens[$token] = array(
- 'name' => ucwords(str_replace('_',' ',$base_table)) . ': ' . ucwords(str_replace('_',' ',$field_name)),
- 'table' => $base_table,
- 'field' => $field_name,
- 'token' => $token,
- 'description' => array_key_exists('description', $field_details) ? $field_details['description'] : '',
- 'location' => $location
- );
- if (!array_key_exists('description', $field_details) or preg_match('/TODO/',$field_details['description'])) {
- $tokens[$token]['description'] = 'The '.$field_name.' field of the '.$base_table.' table.';
- }
- }
-
-
- if (array_key_exists('foreign keys', $table_descrip)) {
- foreach ($table_descrip['foreign keys'] as $table => $details) {
- foreach ($details['columns'] as $left_field => $right_field) {
- $sub_token_prefix = $base_table . '.' . $left_field;
- $sub_location_prefix = implode(' > ',array($base_table, $left_field));
- $sub_tokens = tripal_get_chado_tokens($table);
- if (is_array($sub_tokens)) {
- $tokens = array_merge($tokens, $sub_tokens);
- }
- }
- }
- }
- return $tokens;
- }
- function tripal_replace_chado_tokens($string, $record) {
-
- $tokens = tripal_get_chado_tokens($record->tablename);
-
- if (preg_match_all('/\[[^]]+\]/', $string, $used_tokens)) {
-
- foreach ($used_tokens[0] as $token) {
- $token_info = $tokens[$token];
- if (!empty($token_info)) {
- $table = $token_info['table'];
- $var = $record;
- $value = '';
-
-
- $location = explode('>', $token_info['location']);
- foreach ($location as $index) {
- $index = trim($index);
-
-
- if (is_object($var)) {
-
-
- if (property_exists($var, $index)) {
- $value = $var->$index;
- }
- }
-
-
- elseif (is_array($var)) {
- $value = $var[$index];
- }
- else {
- tripal_report_error('tripal_chado', TRIPAL_WARNING,
- 'Tokens: Unable to determine the value of %token. Things went awry when trying ' .
- 'to access \'%index\' for the following: \'%var\'.',
- array('%token' => $token, '%index' => $index, '%var' => print_r($var,TRUE))
- );
- }
- }
- $string = str_replace($token, $value, $string);
- }
- }
- }
- return $string;
- }
|