Tripal Plant PopGen Submit
tpps.queries.api.inc
Go to the documentation of this file.
1 <?php
2 
28 function tpps_api_author_pid_query(array $authors) {
29  $query = db_select('chado.pubauthor', 'pa');
30  $query->join('chado.project_pub', 'pp', 'pp.pub_id = pa.pub_id');
31  $query->fields('pp', array('project_id'));
32  $or = tpps_api_query_or($orgs, array(
33  'pa.givennames',
34  'pa.surname',
35  ));
36  $query->condition($or);
37  return $query->execute();
38 }
39 
49 function tpps_api_organism_pid_query(array $orgs) {
50  $query = db_select('chado.organism', 'o');
51  $query->join('chado.project_organism', 'po', 'po.organism_id = o.organism_id');
52  $query->fields('po', array('project_id'));
53  $or = tpps_api_query_or($orgs, array(
54  'o.genus',
55  'o.species',
56  ));
57  $query->condition($or);
58  return $query->execute();
59 }
60 
70 function tpps_api_organization_pid_query(array $orgs) {
71  $query = db_select('chado.contact', 'c');
72  $query->join('chado.contact_relationship', 'cr', 'c.contact_id = cr.object_id');
73  $query->join('chado.project_contact', 'pc', 'pc.contact_id = cr.subject_id');
74  $query->fields('pc', array('project_id'));
75  $or = tpps_api_query_or($orgs, array(
76  'c.name',
77  ));
78  $and = db_and()
79  ->condition('c.type_id', tpps_load_cvterm('organization')->cvterm_id)
80  ->condition($or);
81  $query->condition($and);
82  return $query->execute();
83 }
84 
96 function tpps_api_project_query(array $pids, $entities = FALSE) {
97  if (!$entities) {
98  return $pids;
99  }
100 
101  return tpps_api_record_entities('Project', $pids);
102 }
103 
115 function tpps_api_project_publication_query(array $pids, $entities = FALSE) {
116  $query = db_select('chado.project_pub', 'pp')
117  ->fields('pp', array('pub_id'));
118  $or = tpps_api_query_or($pids, array(
119  'pp.project_id',
120  ), '=');
121  $query->condition($or);
122  $query = $query->execute();
123 
124  $results = $query->fetchCol();
125 
126  if (!$entities) {
127  return $results;
128  }
129 
130  return tpps_api_record_entities('Publication', $results);
131 }
132 
146 function tpps_api_project_author_query(array $pids, $entities = FALSE) {
147  $query = db_select('chado.project_pub', 'pp');
148  $query->join('chado.pubauthor', 'pa', 'pa.pub_id = pp.pub_id');
149  $query->fields('pa', array('pubauthor_id'));
150  $or = tpps_api_query_or($pids, array(
151  'pp.project_id',
152  ), '=');
153  $query->condition($or);
154  $query = $query->execute();
155  return $query->fetchCol();
156 }
157 
169 function tpps_api_project_organism_query(array $pids, $entities = FALSE) {
170  $query = db_select('chado.project_organism', 'po')
171  ->fields('po', array('organism_id'));
172  $or = tpps_api_query_or($pids, array(
173  'po.project_id',
174  ), '=');
175  $query->condition($or);
176  $query = $query->execute();
177 
178  $results = $query->fetchCol();
179 
180  if (!$entities) {
181  return $results;
182  }
183 
184  return tpps_api_record_entities('Organism', $results);
185 }
186 
198 function tpps_api_project_design_query(array $pids) {
199  $query = db_select('chado.projectprop', 'pp');
200  $query->join('chado.cvterm', 'c', 'c.cvterm_id = pp.type_id');
201  $query->fields('c', array('name'))
202  ->fields('pp', array('value'));
203  $or = tpps_api_query_or($pids, array(
204  'pp.project_id',
205  ), '=');
206  $query->condition($or);
207  $query = $query->execute();
208 
209  $results = array();
210  while (($result = $query->fetchObject())) {
211  $results[$result->name][] = $result->value;
212  }
213 
214  return $results;
215 }
216 
228 function tpps_api_project_stock_query(array $pids, $entities = FALSE) {
229  $query = db_select('chado.project_stock', 'p')
230  ->fields('p', array('stock_id'));
231  $or = tpps_api_query_or($pids, array(
232  'p.project_id',
233  ), '=');
234  $query->condition($or);
235  $query = $query->execute();
236 
237  $results = $query->fetchCol();
238 
239  if (!$entities) {
240  return $results;
241  }
242 
243  return tpps_api_record_entities('Stock', $results);
244 }
245 
259 function tpps_api_project_phenotype_query(array $pids, $entities = FALSE) {
260  $query = db_select('chado.project_stock', 'ps');
261  $query->join('chado.stock_phenotype', 'sp', 'sp.stock_id = ps.stock_id');
262  $or = tpps_api_query_or($pids, array(
263  'ps.project_id',
264  ), '=');
265  $query->fields('sp', array('phenotype_id'))
266  ->condition($or);
267  $query = $query->execute();
268 
269  $results = $query->fetchCol();
270  return $results;
271 }
272 
284 function tpps_api_project_genotype_query(array $pids, $entities = FALSE) {
285  $query = db_select('chado.project_stock', 'ps');
286  $query->join('chado.stock_genotype', 'sg', 'sg.stock_id = ps.stock_id');
287  $or = tpps_api_query_or($pids, array(
288  'ps.project_id',
289  ), '=');
290  $query->fields('sg', array('genotype_id'))
291  ->condition($or);
292  $query = $query->execute();
293 
294  $results = $query->fetchCol();
295 
296  if (!$entities) {
297  return $results;
298  }
299  return tpps_api_record_entities('Genotype', $results);
300 }
301 
313 function tpps_api_project_stats_query(array $states) {
314  $results = array();
315  foreach ($states as $state) {
316  $results[] = $state['stats'];
317  }
318  return $results;
319 }
320 
339 function tpps_api_query_or(array $vals, array $fields, $override_op = NULL) {
340  $or = db_or();
341  foreach ($vals as $val) {
342  $op = '~*';
343  if (preg_match('/^(.*)\[(.+)\]$/', $val, $matches)) {
344  $val = $matches[1];
345  $op = $matches[2];
346  }
347  $op = !empty($override_op) ? $override_op : $op;
348  foreach ($fields as $field) {
349  $or->condition($field, $val, $op);
350  }
351  }
352  return $or;
353 }
354 
370 function tpps_api_record_entities($label, array $record_ids) {
371  $bundle = tripal_load_bundle_entity(array('label' => $label));
372  $details = @tripal_get_bundle_details($bundle->name);
373 
374  $entity_ids = array();
375  foreach ($record_ids as $id) {
376  $entity_ids[] = chado_get_record_entity_by_bundle($bundle, $id);
377  }
378 
379  $field_ids = array();
380  if (!empty($details['fields'])) {
381  foreach ($details['fields'] as $field) {
382  $field_ids[] = field_info_field($field['name'])['id'];
383  }
384  }
385 
386  return tripal_load_entity('TripalEntity', $entity_ids, FALSE, $field_ids);
387 }
tpps_api_project_author_query(array $pids, $entities=FALSE)
tpps_api_project_publication_query(array $pids, $entities=FALSE)
tpps_api_project_genotype_query(array $pids, $entities=FALSE)
tpps_api_project_stats_query(array $states)
tpps_api_project_phenotype_query(array $pids, $entities=FALSE)
tpps_api_project_organism_query(array $pids, $entities=FALSE)
tpps_api_project_stock_query(array $pids, $entities=FALSE)
tpps_api_author_pid_query(array $authors)
tpps_api_project_query(array $pids, $entities=FALSE)
tpps_load_cvterm($term, array $options=array(), $version=NULL, $refresh_cache=FALSE)
tpps_api_record_entities($label, array $record_ids)
tpps_api_query_or(array $vals, array $fields, $override_op=NULL)
tpps_api_project_design_query(array $pids)
tpps_api_organization_pid_query(array $orgs)
tpps_api_organism_pid_query(array $orgs)