Tripal Plant PopGen Submit
chado_utils.inc File Reference

Go to the source code of this file.

Functions

 tpps_add_project_file (array &$state, &$fid)
 
 tpps_chado_insert_multi (array $record_groups, array $options=array())
 
 tpps_chado_insert_record ($table, $records, array $options=array())
 
 tpps_chado_prop_exists ($base_table, $id, $name, array $options=array())
 
 tpps_get_taxon ($org_name, $rank)
 
 tpps_table_columns ($table)
 
 tpps_tripal_entity_publish ($bundle_name, array $vals, array $options=array())
 

Detailed Description

Defines useful functions that interact with Chado.

Definition in file chado_utils.inc.

Function Documentation

◆ tpps_add_project_file()

tpps_add_project_file ( array &  $state,
$fid 
)

Add a file path as a projectprop to a submission.

Parameters
array$stateThe TPPS submission associated with the file.
int$fidThe managed file id number.

Definition at line 522 of file chado_utils.inc.

522  {
523  $file = file_load($fid) ?? NULL;
524  if ($file) {
525  tpps_chado_insert_record('projectprop', array(
526  'project_id' => $state['ids']['project_id'],
527  'type_id' => tpps_load_cvterm('file_path')->cvterm_id,
528  'value' => file_create_url($file->uri),
529  'rank' => $state['file_rank'],
530  ));
531  db_insert('tpps_project_file_managed')
532  ->fields(array(
533  'project_id' => $state['ids']['project_id'],
534  'fid' => $fid,
535  ))
536  ->execute();
537  $state['file_rank']++;
538  }
539 
540  if (!empty($state['revised_files'][$fid]) and ($file = file_load($state['revised_files'][$fid]))) {
541  $fid = $state['revised_files'][$fid];
542  tpps_chado_insert_record('projectprop', array(
543  'project_id' => $state['ids']['project_id'],
544  'type_id' => tpps_load_cvterm('file_path')->cvterm_id,
545  'value' => file_create_url($file->uri),
546  'rank' => $state['file_rank'],
547  ));
548  db_insert('tpps_project_file_managed')
549  ->fields(array(
550  'project_id' => $state['ids']['project_id'],
551  'fid' => $fid,
552  ))
553  ->execute();
554  $state['file_rank']++;
555  }
556 }
tpps_chado_insert_record($table, $records, array $options=array())
Definition: chado_utils.inc:27
tpps_load_cvterm($term, array $options=array(), $version=NULL, $refresh_cache=FALSE)

◆ tpps_chado_insert_multi()

tpps_chado_insert_multi ( array  $record_groups,
array  $options = array() 
)

Inserts multiple records into the chado database.

This function will insert multiple records into multiple tables in the chado database. This function is much faster than the tpps_chado_insert_record() function with the 'multi' option set to true, because .

Parameters
array$record_groupsAn array of tables and the records that will be going into each table.
array$optionsAdditional options.
Returns
mixed An array of primary key ids on success. On failure, return FALSE.

Definition at line 141 of file chado_utils.inc.

141  {
142 
143  if (!isset($options['devel'])) {
144  $options['devel'] = FALSE;
145  }
146 
147  // Initialize insert statements, schemas, foreign keys, and primary keys.
148  $inserts = array();
149  $schemas = array();
150  $primarys = array();
151  $fks = array();
152  foreach (array_keys($record_groups) as $table) {
153  $current_records = &$record_groups[$table];
154  if (empty($current_records)) {
155  unset($record_groups[$table]);
156  continue;
157  }
158  $schemas[$table] = chado_get_schema($table);
159  if (!$schemas[$table]) {
160  return FALSE;
161  }
162 
163  if (!empty($options['fk_overrides'][$table])) {
164  foreach ($options['fk_overrides'][$table] as $fk_type => $props) {
165  $schemas[$table]['foreign keys'][$fk_type] = $props;
166  }
167  }
168 
169  $inserts[$table] = db_insert("chado.$table");
170  $fields_arr = array();
171  foreach (current($current_records) as $field => $val) {
172  if ($field != '#fk') {
173  $fields_arr[] = $field;
174  }
175  else {
176  foreach ($val as $fk_type => $fk_val) {
177  $fields_arr[] = key($schemas[$table]['foreign keys'][$fk_type]['columns']);
178  }
179  }
180  }
181  $inserts[$table]->fields($fields_arr);
182  $primarys[$table] = !empty($schemas[$table]['primary key'][0]) ? $schemas[$table]['primary key'][0] : NULL;
183  $fks[$table] = array();
184  }
185 
186  if ($options['devel']) {
187  $start_time = microtime(TRUE);
188  print_r("\nstart time: " . date('r') . "\n");
189  $times = array(
190  'total' => 0,
191  'unique' => 0,
192  'unique_query' => 0,
193  'unique_other' => 0,
194  'insert' => 0,
195  'other' => 0,
196  );
197  }
198  foreach ($record_groups as $table => $records) {
199  $schema = &$schemas[$table];
200  $primary = &$primarys[$table];
201  $fks_to_populate = array();
202  $has_fields = FALSE;
203 
204  if ($options['devel']) {
205  $start_unique = microtime(TRUE);
206  }
207  foreach ($records as $id => $values) {
208  $unique = !empty($schema['unique keys']) ? $schema['unique keys'] : array();
209  if (!empty($values[$primary])) {
210  $unique['primary'] = array($primary);
211  }
212 
213  $exists = FALSE;
214 
215  if (!empty($values['#fk'])) {
216  foreach ($values['#fk'] as $fk_type => $foreign_record_id) {
217  $fk_table = $schema['foreign keys'][$fk_type]['table'];
218  $col_name = key($schema['foreign keys'][$fk_type]['columns']);
219  $values[$col_name] = $fks[$fk_table][$foreign_record_id];
220  }
221  unset($values['#fk']);
222  }
223 
224  foreach ($unique as $fields) {
225  $u_cols = array('*');
226  $u_vals = array();
227  foreach ($fields as $field) {
228  if (!array_key_exists($field, $values)) {
229  if (array_key_exists('default', $schema['fields'][$field])) {
230  $u_vals[$field] = $schema['fields'][$field]['default'];
231  }
232  }
233  else {
234  $u_vals[$field] = $values[$field];
235  }
236  }
237 
238  $start_query = microtime(TRUE);
239  $results = chado_select_record($table, $u_cols, $u_vals);
240  if ($options['devel']) {
241  $times['unique_query'] += (microtime(TRUE) - $start_query);
242  }
243  if ($results and isset($primary)) {
244  $exists = TRUE;
245  if (count($results) == 1) {
246  if (!empty($values[$primary])) {
247  unset($values[$primary]);
248  }
249  foreach ($values as $field => $val) {
250  if ($val == current($results)->{$field}) {
251  unset($values[$field]);
252  }
253  }
254  if (!empty($values)) {
255  $fks[$table][$id] = chado_update_record($table, $u_vals, $values, array('return_record' => TRUE))->{$primary};
256  break;
257  }
258  }
259  $fks[$table][$id] = current($results)->{$primary};
260  break;
261  }
262  }
263 
264  if (!$exists) {
265  $inserts[$table]->values($values);
266  $has_fields = TRUE;
267  $fks_to_populate[] = $id;
268  }
269  }
270 
271  if ($options['devel']) {
272  $times['unique'] += (microtime(TRUE) - $start_unique);
273  $start_insert = microtime(TRUE);
274  }
275 
276  if ($has_fields) {
277  if ($table == 'featureloc') {
278  $previous = chado_set_active('chado');
279  $last_id = $inserts[$table]->execute();
280  chado_set_active($previous);
281  }
282  else {
283  try {
284  $last_id = $inserts[$table]->execute();
285  }
286  catch (Exception $ex) {
287  print_r($values);
288  throw new Exception($ex);
289  }
290  }
291  $fks[$table] += array_combine($fks_to_populate, range($last_id - count($fks_to_populate) + 1, $last_id));
292  }
293 
294  if ($options['devel']) {
295  $times['insert'] += (microtime(TRUE) - $start_insert);
296  }
297  }
298 
299  if ($options['devel']) {
300  $times['total'] = microtime(TRUE) - $start_time;
301  print_r("\nend time: " . date('r') . "\n");
302  $times['other'] = $times['total'] - $times['unique'] - $times['insert'];
303  $times['unique_other'] = $times['unique'] - $times['unique_query'];
304  print_r("timing breakdown:\n");
305  print_r($times);
306  }
307 
308  if (!empty($options['entities'])) {
309  tpps_tripal_entity_publish($options['entities']['label'], ($fks[$options['entities']['table']] ?? array()), array(
310  'multi' => TRUE,
311  'prefix' => ($options['entities']['prefix'] ?? NULL),
312  ));
313  }
314 
315  if (!empty($options['fks'])) {
316  return $fks[$options['fks']];
317  }
318  else {
319  return $fks;
320  }
321 }
tpps_tripal_entity_publish($bundle_name, array $vals, array $options=array())

◆ tpps_chado_insert_record()

tpps_chado_insert_record (   $table,
  $records,
array  $options = array() 
)

Inserts a record to a chado table using the Tripal Chado API.

This function uses the Tripal Chado API to insert a record. However, if the record already exists, then this function will update that record in the database instead. This function also supports insertion of multiple records at a time, while the standard Tripal chado_insert_record function does not.

Parameters
string$tableThe name of the Chado table being inserted to.
mixed$recordsThe record or array of records to be inserted to chado.
array$optionsAn array of additional options which can be passed to chado_insert_record.
Returns
mixed The id of the new record in the database. False if insertion aborted. This is only supported for single record insertions

Definition at line 27 of file chado_utils.inc.

27  {
28  if (!array_key_exists('multi', $options)) {
29  $options['multi'] = FALSE;
30  }
31 
32  if (!$options['multi']) {
33  $records = array($records);
34  }
35  else {
36  $has_fields = FALSE;
37  $insert = db_insert("chado.$table");
38  $fields_arr = array();
39  foreach ($records[0] as $field => $val) {
40  $fields_arr[] = $field;
41  }
42  $insert->fields($fields_arr);
43  }
44 
45  $table_desc = chado_get_schema($table);
46  if (!$table_desc) {
47  return FALSE;
48  }
49  $primary = !empty($table_desc['primary key'][0]) ? $table_desc['primary key'][0] : NULL;
50  foreach ($records as $values) {
51  // Populate insert_values array and check if a record with the same unique
52  // fields already exists. If it does, return the existing primary key.
53  $insert_values = array();
54 
55  foreach ($values as $field => $value) {
56  if (is_array($value)) {
57  $results = chado_schema_get_foreign_key($table_desc, $field, $value);
58  if (count($results) != 1) {
59  return FALSE;
60  }
61  else {
62  $insert_values[$field] = $results[0];
63  }
64  }
65  else {
66  $insert_values[$field] = $value;
67  }
68  }
69 
70  $unique = array();
71  if (array_key_exists('unique keys', $table_desc)) {
72  $unique = $table_desc['unique keys'];
73  }
74  $unique['primary'] = array($primary);
75 
76  $exists = FALSE;
77  foreach ($unique as $fields) {
78  $u_cols = array($primary);
79  $u_vals = array();
80  foreach ($fields as $field) {
81  $u_cols[] = $field;
82  if (!array_key_exists($field, $insert_values)) {
83  if (array_key_exists('default', $table_desc['fields'][$field])) {
84  $u_vals[$field] = $table_desc['fields'][$field]['default'];
85  }
86  }
87  else {
88  $u_vals[$field] = $insert_values[$field];
89  }
90  }
91 
92  if (!empty($u_vals) and ($results = chado_select_record($table, $u_cols, $u_vals))) {
93  $exists = TRUE;
94  // If the new values match with exactly one record, update that one
95  // and return the primary key.
96  if (!$options['multi'] and count($results) == 1) {
97  chado_update_record($table, $u_vals, $insert_values);
98  return $results[0]->{$primary};
99  }
100  }
101  }
102 
103  if (!$exists and $options['multi']) {
104  $insert->values($insert_values);
105  $has_fields = TRUE;
106  }
107 
108  if (!$options['multi']) {
109  if ($exists) {
110  return $results[0]->{$primary};
111  }
112  $new_record = chado_insert_record($table, $insert_values, $options);
113  if (!empty($new_record[$primary])) {
114  return $new_record[$primary];
115  }
116  else {
117  return;
118  }
119  }
120  }
121  if ($options['multi'] and $has_fields) {
122  $insert->execute();
123  }
124 }

◆ tpps_chado_prop_exists()

tpps_chado_prop_exists (   $base_table,
  $id,
  $name,
array  $options = array() 
)

Checks to see if a property of an item already exists in chado.

Parameters
string$base_tableThe name of the base chado table.
int$idThe record if of the base item.
string$nameThe name of the type_id of the property we are looking for.
array$optionsAdditional options.
Returns
bool TRUE if a property exists, otherwise FALSE.

Definition at line 433 of file chado_utils.inc.

433  {
434  $prop_table = $base_table . 'prop';
435  $cvterm = array('name' => $name);
436 
437  if (array_key_exists('prop_table', $options)) {
438  $prop_table = $options['prop_table'];
439  }
440 
441  if (array_key_exists('cv', $options)) {
442  $cvterm['cv_id'] = array(
443  'name' => $options['cv'],
444  );
445  }
446 
447  $prop_query = chado_select_record($prop_table, array($prop_table . '_id'), array(
448  'type_id' => $cvterm,
449  $base_table . '_id' => $id,
450  ));
451  return !empty($prop_query);
452 }

◆ tpps_get_taxon()

tpps_get_taxon (   $org_name,
  $rank 
)

Tries to retrieve taxonomy information about an organism from the db.

If taxonomy information for the genus does not already exist, then the function tries to retrieve taxonomy information from NCBI.

Parameters
string$org_nameThe full name (genus, species) of the organism.
string$rankThe taxonomic rank (subkingdom, order, family, etc).
Returns
string The the taxonomy information of the organism.

Definition at line 496 of file chado_utils.inc.

496  {
497  $parts = explode(' ', $org_name);
498  $genus = $parts[0];
499 
500  $query = db_select('chado.organismprop', 'op');
501  $query->join('chado.organism', 'o', 'o.organism_id = op.organism_id');
502  $query->fields('op', array('value'));
503  $query->condition('op.type_id', tpps_load_cvterm($rank)->cvterm_id);
504  $query->condition('o.genus', $genus);
505  $query->range(0, 1);
506  $query = $query->execute();
507  $result = $query->fetchObject()->value ?? NULL;
508  if (!empty($result)) {
509  return $result;
510  }
511  return tpps_ncbi_get_taxonomy($org_name, $rank);
512 }
tpps_load_cvterm($term, array $options=array(), $version=NULL, $refresh_cache=FALSE)
tpps_ncbi_get_taxonomy($organism, $rank)
Definition: ncbi_utils.inc:19

◆ tpps_table_columns()

tpps_table_columns (   $table)

Returns an array of field names associated with a db table.

This function first checks the chado schema of the table. If there is no existing chado schema, then it will check the drupal schema. If there is no existing drupal schema, it will return an empty array.

Parameters
string$tableThe name of the table.
Returns
array An array of field names.

Definition at line 467 of file chado_utils.inc.

467  {
468  $schema = chado_get_schema($table);
469  if (empty($schema)) {
470  $schema = drupal_get_schema($table);
471  if (empty($schema)) {
472  return array();
473  }
474  }
475  $cols = array();
476  foreach ($schema['fields'] as $name => $info) {
477  $cols[] = $name;
478  }
479  return $cols;
480 }

◆ tpps_tripal_entity_publish()

tpps_tripal_entity_publish (   $bundle_name,
array  $vals,
array  $options = array() 
)

Publish a Tripal Entity based on the bundle name, title, and record id.

Parameters
string$bundle_nameThe name of the bundle, for example, "Organism", "Gene", "Project", etc.
array$valsThe an array of entity values to be published.
array$optionsAdditional options for publishing.

Definition at line 333 of file chado_utils.inc.

333  {
334  if (!empty($options['multi'])) {
335  $bundle = tripal_load_bundle_entity(array('label' => $bundle_name));
336  if (!isset($bundle)) {
337  return;
338  }
339 
340  $time = time();
341  $entity_insert = db_insert('tripal_entity')
342  ->fields(array('type', 'bundle', 'term_id', 'title', 'created', 'changed'));
343  $entity_count = 0;
344  foreach ($vals as $title => $id) {
345  if (empty(chado_get_record_entity_by_bundle($bundle, $id))) {
346  if (!empty($options['prefix'])) {
347  $title = $options['prefix'] . $title;
348  }
349  $entity_insert->values(array(
350  'type' => 'TripalEntity',
351  'bundle' => 'bio_data_' . $bundle->id,
352  'term_id' => $bundle->term_id,
353  'title' => $title,
354  'created' => $time,
355  'changed' => $time,
356  ));
357  $entity_count++;
358  }
359  }
360  $entity_id = $entity_insert->execute() - $entity_count + 1;
361 
362  $bio_data_insert = db_insert('chado_bio_data_' . $bundle->id)
363  ->fields(array('entity_id', 'record_id'));
364  foreach ($vals as $id) {
365  if (empty(chado_get_record_entity_by_bundle($bundle, $id))) {
366  $bio_data_insert->values(array(
367  'entity_id' => $entity_id,
368  'record_id' => $id,
369  ));
370 
371  $entity = new stdClass();
372  $entity->id = $entity_id;
373  $entity->bundle = 'bio_data_' . $bundle->id;
374  $alias = str_replace(' ', '', $bundle->term->name) . '/' . $entity_id;
375  $controller = entity_get_controller('TripalEntity');
376  $controller->setAlias($entity, $alias);
377  $controller->resetCache();
378 
379  $entity_id++;
380  }
381  }
382  $bio_data_insert->execute();
383  }
384  else {
385  $bundle = tripal_load_bundle_entity(array('label' => $bundle_name));
386 
387  if (!isset($bundle)) {
388  return;
389  }
390 
391  if (empty(chado_get_record_entity_by_bundle($bundle, $vals[1]))) {
392  $entity_id = db_insert('tripal_entity')
393  ->fields(array(
394  'type' => 'TripalEntity',
395  'bundle' => 'bio_data_' . $bundle->id,
396  'term_id' => $bundle->term_id,
397  'title' => $vals[0],
398  'created' => time(),
399  'changed' => time(),
400  ))
401  ->execute();
402 
403  db_insert('chado_bio_data_' . $bundle->id)
404  ->fields(array(
405  'entity_id' => $entity_id,
406  'record_id' => $vals[1],
407  ))
408  ->execute();
409 
410  $entity = tripal_load_entity('TripalEntity', array($entity_id))[$entity_id];
411  $controller = entity_get_controller('TripalEntity');
412  $controller->setAlias($entity);
413  $controller->resetCache();
414  }
415  }
416 }