-
Notifications
You must be signed in to change notification settings - Fork 1
/
nd_genotypes.module
662 lines (580 loc) · 23.9 KB
/
nd_genotypes.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
<?php
/**
* @file
* Contains general functionality for this module.
*/
require_once('api/nd_genotypes.api.inc');
require_once('includes/nd_genotypes.mview.inc');
require_once('includes/nd_genotypes.mview.sync.inc');
require_once('includes/nd_genotypes.form_elements.inc');
require_once('includes/nd_genotypes.fields.inc');
require_once('includes/nd_genotypes.genotype_matrix.inc');
require_once('includes/nd_genotypes.genotype_matrix.download.inc');
/**
* Implements hook_menu().
*/
function nd_genotypes_menu() {
// Administration.
$items['admin/tripal/extension/nd_genotypes'] = array(
'title' => 'Natural Diversity Genotypes',
'description' => 'Provides an interface for genotypes and the experiments that generate them.',
'access arguments' => array('administer tripal'),
'page callback' => 'nd_genotypes_admin_landing_page',
'file' => 'includes/nd_genotypes.admin.inc'
);
$items['admin/tripal/extension/nd_genotypes/settings'] = array(
'title' => 'Settings',
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array('nd_genotypes_admin_settings'),
'access arguments' => array('administer tripal'),
'weight' => 0,
'file' => 'includes/nd_genotypes.admin.inc'
);
$items['admin/tripal/extension/nd_genotypes/sync'] = array(
'title' => 'Sync',
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array('nd_genotypes_admin_sync_mviews'),
'access arguments' => array('administer tripal'),
'weight' => 2,
'file' => 'includes/nd_genotypes.admin.inc'
);
$items['admin/tripal/extension/nd_genotypes/help'] = array(
'title' => 'Help',
'type' => MENU_LOCAL_TASK,
'page callback' => 'theme',
'page arguments' => array('nd_genotypes__help'),
'access arguments' => array('administer tripal'),
'weight' => 4
);
// For auto-complete of germplasm that had been genotyped.
$items['tripal_ajax/nd_genotypes/genotyped_germplasm/name_to_id/%/only'] = array(
'title' => 'Germplasm Genotyped AJAX',
'type' => MENU_CALLBACK,
'page callback' => 'nd_genotypes_lookup_germplasm_name_callback',
'page arguments' => array(4), //arg is partition
'access arguments' => array('access content'),
'weight' => 4
);
// For autocomplete of sequence backbone names.
$items['tripal_ajax/nd_genotypes/genotyped_backbones/name_to_id/%/only'] = array(
'title' => 'Backbones Genotyped AJAX',
'type' => MENU_CALLBACK,
'page callback' => 'nd_genotypes_lookup_backbone_name_callback',
'page arguments' => array(4), //arg is partition
'access arguments' => array('access content'),
'weight' => 4
);
// For autocomplete of project names.
$items['tripal_ajax/nd_genotypes/project/name'] = array(
'title' => 'Projects AJAX',
'type' => MENU_CALLBACK,
'page callback' => 'nd_genotypes_lookup_project_name_callback',
'access arguments' => array('access content'),
'weight' => 4
);
// Add Marker x Germplasm Matrix links (1/organism synced)
$sql = "
SELECT *
FROM {organism} O
ORDER BY O.genus, O.species
";
$orgs = chado_query($sql);
foreach ($orgs as $organism) {
// @PARTITION: We use the partition to determine if the mview tables are available
// for this organism before making the page available.
$partition = $organism->genus;
if (ndg_mview_tables_exist($partition)) {
$items['chado/genotype/' . $organism->genus] = array(
'title' => $organism->genus . ' Genotypes',
'description' => 'Marker by Germplasm Genotypes Search for ' . $organism->genus . ' species.',
'page callback' => 'nd_genotype_matrix_page',
'page arguments' => array($organism->genus),
//'page callback' => 'drupal_get_form',
//'page arguments' => array('nd_genotypes_matrix_filter_form', $organism->genus),
'access arguments' => array('access nd_genotypes'),
'type' => MENU_NORMAL_ITEM,
'file' => 'includes/nd_genotypes.genotype_matrix.inc',
);
$items['chado/genotype/' . $organism->genus . '/csv'] = array(
'title' => 'Download Genotypes: CSV',
'page callback' => 'trpdownload_download_page',
'page arguments' => array('genotype_matrix_csv', 4),
'access arguments' => array('download nd_genotype_matrix'),
'type' => MENU_CALLBACK,
);
$items['chado/genotype/' . $organism->genus . '/hapmap'] = array(
'title' => 'Download Genotypes: HAPMAP',
'page callback' => 'trpdownload_download_page',
'page arguments' => array('genotype_matrix_hapmap', 4),
'access arguments' => array('download nd_genotype_matrix'),
'type' => MENU_CALLBACK,
);
// JSON Callback: Number of results for genotype matrix.
$items['chado/genotype/' . $organism->genus . '/num_results.json'] = array(
'title' => 'Number of Results',
'page callback' => 'nd_genotypes_count_genotype_matrix_results_callback',
'page arguments' => array(2,4),
'access arguments' => array('access nd_genotypes'),
'type' => MENU_CALLBACK,
);
// JSON Callback: Number of results for genotype matrix.
$items['chado/genotype/' . $organism->genus . '/num_variants.json'] = array(
'title' => 'Number of Results',
'page callback' => 'nd_genotypes_count_genotype_matrix_variants_callback',
'page arguments' => array(2,4),
'access arguments' => array('access nd_genotypes'),
'type' => MENU_CALLBACK,
);
}
}
return $items;
}
/**
* Register any theme functions/templates for this module.
* Implements hook_theme().
*/
function nd_genotypes_theme($existing, $type, $theme, $path) {
$items = array();
// Help Page
// @todo: Update this page.
$items['nd_genotypes__help'] = array(
'template' => 'nd_genotypes--help',
'path' => "$path/theme/templates",
);
// This is used for the "Germplasm" filter on the genotype matrix form.
// It's a custom form element that lets you select multiple germplasm to be filtered on.
$items['multiple_organism_stock'] = array(
'render element' => 'element',
);
$items['pairwise_compare_germplasm'] = array(
'render element' => 'element'
);
// This template executes the query as well as building the genotype matrix table.
// The filter form is in nd_genotypes_matrix_filter_form().
$items['nd_genotype_matrix'] = array(
'template' => 'nd_genotypes.genotype_matrix',
'path' => $path . '/theme/templates/',
);
return $items;
}
/**
* Implements hook_permission().
*/
function nd_genotypes_permission() {
return array(
'access nd_genotypes' => array(
'title' => t('Access ND Genotypes.'),
'description' => t('Provides access to the genotype matrix page and Genotype/Sequence panes on feature pages.'),
),
'download nd_genotype_matrix' => array(
'title' => t('Download Genotypes.'),
'description' => t('Allows bulk download of genotypes through the genotype matrix page.'),
),
);
}
/**
* Simple function to render the genotype matrix based on the template file.
*/
function nd_genotype_matrix_page($genus) {
// @PARTITION: This is where the partition is set for the genotype matrix. From here
// if is carried throughout the rest of the functions.
global $base_url;
$vars = array('genus' => $genus, 'partition' => strtolower($genus),
'baseUrl' => $base_url . '/chado/genotype/' . $genus);
// Add the CSS.
drupal_add_css(drupal_get_path('module', 'nd_genotypes') . '/theme/css/nd_genotype_matrix.css');
// Add the JS to add result counts via AJAX.
drupal_add_js('misc/ajax.js');
drupal_add_js(drupal_get_path('module', 'nd_genotypes') . '/theme/js/genotypeMatrixGetResultCounts.js');
drupal_add_js(array('NDgenotypes' => $vars), 'setting');
// Save the query paramters.
$vars['q'] = drupal_get_query_parameters();
// Process the query paramters to determine the defaults for the filter form
// as well as to isolate the values that are needed to retrieve the data.
nd_genotypes_process_query_parameters($vars);
// Actually query whatever storage method you are currently using and retrieve the
// data needed for the current page of the matrix.
// Currently there is only one backend supported (postgresql: chado) so we will just
// use it but things have been set-up this way to facilitate use of other backends
// in the future as needed or desired.
// ASSUMPTION: the following function should return a Variant X Germplasm Grid stored
// in a 'data' associative array and a list of variant details in a 'variants'
// associative array.
if ($_SERVER['REQUEST_METHOD'] =='GET') {
nd_genotypes_retrive_matrix_postgresql($vars);
}
return theme('nd_genotype_matrix', $vars);
}
/**
* Auto-complete: Look-up names of germplasm genotyped.
*/
function nd_genotypes_lookup_germplasm_name_callback($partition, $string = "") {
$matches = array();
if (!empty($partition) AND trim($string) AND ndg_mview_tables_exist($partition)) {
$result = ndg_mview_query($partition, '
SELECT germplasm_name
FROM {mview_ndg_germplasm_genotyped} m
WHERE partition = :partition AND germplasm_name ~* :query
LIMIT 10
', array(':query' => $string, ':partition' => $partition))->fetchCol();
foreach ($result as $name) {
$matches[$name] = $name;
}
}
drupal_json_output($matches);
}
/**
* Auto-complete: Look-up names of backbones genotyped.
*/
function nd_genotypes_lookup_backbone_name_callback($partition, $string = "") {
$matches = array();
if (!empty($partition) AND trim($string) AND ndg_mview_tables_exist($partition)) {
$result = ndg_mview_query($partition, '
SELECT srcfeature_name as name
FROM {mview_ndg_variants} m
WHERE srcfeature_name ~* :query
GROUP BY srcfeature_name
LIMIT 10
', array(':query' => $string))->fetchCol();
foreach ($result as $name) {
$matches[$name] = $name;
}
}
drupal_json_output($matches);
}
/**
* Auto-complete: Look-up names of projects.
*/
function nd_genotypes_lookup_project_name_callback($string = '') {
$matches = array();
if (trim($string)) {
$result = chado_query('
SELECT name
FROM {project} p
WHERE name ~* :query
LIMIT 10
', array(':query' => $string))->fetchCol();
foreach ($result as $name) {
$matches[$name] = $name;
}
}
drupal_json_output($matches);
}
/**
* JSON Callback: Returns the number of results for a given genotype matrix.
*
* The genotype matrix is specified in the query parameters ($_GET). This we processs it
* just as we do when building the matrix but instead of limiting the query to the given
* page, we count the entire resultset.
*
* This callback is used in the genotype matrix to asynchronously fill in the number of results.
* @see theme/js/genotypeMatrixGetResultCount.js
*/
function nd_genotypes_count_genotype_matrix_results_callback($partition = '', $type = 'ajax') {
// @PARTITION: This is where the partition is set for the genotype matrix. From here
// if is carried throughout the rest of the functions.
$vars = array('genus' => $partition, 'partition' => $partition);
if (!ndg_mview_tables_exist($partition)) {
return FALSE;
}
// Save the query paramters.
$vars['q'] = drupal_get_query_parameters();
// Process the query paramters to determine the defaults for the filter form
// as well as to isolate the values that are needed to retrieve the data.
nd_genotypes_process_query_parameters($vars);
// Compile all the parameters we will use when grabbing the calls so that we can restrict
// the variant list to those that will have data once we do retrieve the calls.
$query_args = $vars['query_args'];
$call = $variant = $call_args = $variant_args = $variant_joins = array();
// Sequence Ranges:
// First get the arguements. There must always be a backbone and fmin even if the user
// did not provide one.
if (isset($query_args['start_backbone'])) {
if (isset($query_args['start_pos'])) {
$variant_args[':sbackbone'] = $query_args['start_backbone'];
$variant_args[':sfmin'] = (int) $query_args['start_pos'];
}
else {
$variant_args[':sbackbone'] = $query_args['start_backbone'];
$variant_args[':sfmin'] = 1;
}
unset($query_args['start_backbone'], $query_args['start_pos']);
}
if (isset($query_args['end_backbone'])) {
if (isset($query_args['end_pos'])) {
$variant_args[':ebackbone'] = $query_args['end_backbone'];
$variant_args[':efmin'] = (int) $query_args['end_pos'];
}
else {
$variant_args[':ebackbone'] = $query_args['end_backbone'];
// @TODO: Switch to bigints then change this value to match the largest bigint.
$variant_args[':efmin'] = 2147483645;
}
unset($query_args['end_backbone'], $query_args['end_pos']);
}
// Now add the query where condition.
if (isset($variant_args[':sbackbone']) AND isset($variant_args[':ebackbone'])) {
$variant[] = 'ROW(v.srcfeature_name, v.fmin) BETWEEN ROW(:sbackbone, :sfmin) AND ROW(:ebackbone,:efmin)';
}
elseif (isset($variant_args[':sbackbone'])) {
$variant[] = 'ROW(v.srcfeature_name, v.fmin) >= ROW(:sbackbone, :sfmin)';
}
elseif (isset($variant_args[':ebackbone'])) {
$variant[] = 'ROW(v.srcfeature_name, v.fmin) <= ROW(:ebackbone, :efmin)';
}
// Variant Type:
if (isset($query_args['variant_type'])) {
$variant[] = 'variant_type=:variant_type';
$variant_args[':variant_type'] = $query_args['variant_type'];
unset($query_args['variant_type']);
}
// Variant Name:
$variant_name_filter = FALSE;
if (isset($query_args['variant_name'])) {
$variant[] = "variant_name IN (VALUES('" . implode("'), ('", $query_args['variant_name']) . "'))";
//$variant_args[':variant_names'] = $query_args['variant_name'];
unset($query_args['variant_name']);
$variant_name_filter = TRUE;
}
elseif (isset($query_args['variant_name_list'])) {
$list_json = db_query('SELECT list FROM {ndg_matrix_variant_user_lists} WHERE list_id=:id',
array(':id' => $query_args['variant_name_list']))->fetchField();
$list = json_decode($list_json);
$variant[] = "variant_name IN (VALUES('" . implode("'), ('", $list) . "'))";
}
// Polymorphic:
$polymorphic_filter = FALSE;
if (isset($query_args['polymorphic'])
AND is_numeric($query_args['polymorphic']['first'])
AND is_numeric($query_args['polymorphic']['second'])) {
if (!$variant_name_filter) {
$variant_joins[] = "LEFT JOIN (
SELECT a.variant_id, a.allele_call=b.allele_call as monomorphic
FROM {mview_ndg_calls} a, {mview_ndg_calls} b
WHERE a.variant_id=b.variant_id AND a.germplasm_id=:poly1 AND b.germplasm_id=:poly2
) p ON p.variant_id=v.variant_id ";
$variant_args[':poly1'] = $query_args['polymorphic']['first'];
$variant_args[':poly2'] = $query_args['polymorphic']['second'];
$variant[] = "p.monomorphic IS FALSE";
}
$polymorphic_filter = TRUE;
}
// Germplasm:
if (isset($query_args['germplasm_id']) AND !empty($query_args['germplasm_id'])) {
$call[] = 'germplasm_id IN (:germplasm_ids)';
$call_args[':germplasm_ids'] = $query_args['germplasm_id'];
unset($query_args['germplasm_id']);
}
else {
return FALSE;
}
// Project:
if (isset($query_args['project_id']) AND !empty($query_args['project_id'])) {
if (sizeof($query_args['project_id']) == 1) {
$call[] = 'project_id = :project_id';
}
else {
$call[] = 'project_id IN (:project_id)';
}
$call_args[':project_id'] = $query_args['project_id'];
}
// Marker Type:
if (isset($query_args['marker_type'])) {
$call[] = 'marker_type = :marker_type';
$call_args[':marker_type'] = $query_args['marker_type'];
}
// Now determine the query for the variants.
$sql = "SELECT count(v.variant_id) \n"
. "FROM {mview_ndg_variants} v \n"
. implode("\n", $variant_joins)
. "WHERE \n";
// Add in conditions on the variants table.
if ($variant) {
$sql .= implode("\n AND ",$variant) . "\n AND ";
}
// Add in conditions on the call table.
$sql .= "EXISTS ( \n"
. " SELECT true \n"
. " FROM {mview_ndg_calls} call \n"
. " WHERE \n"
. " " . implode("\n AND ", $call) . "\n"
. " AND call.variant_id=v.variant_id\n"
. " ) \n";
if ($polymorphic_filter AND $variant_name_filter) {
$subsql = str_replace('SELECT count(v.variant_id)', 'SELECT v.variant_id', $sql);
$sql = "SELECT count(v.variant_id) FROM (\n"
. $subsql . ") v \n"
. "LEFT JOIN (
SELECT a.variant_id, a.allele_call=b.allele_call as monomorphic
FROM {mview_ndg_calls} a, {mview_ndg_calls} b
WHERE a.variant_id=b.variant_id AND a.germplasm_id=:poly1 AND b.germplasm_id=:poly2
) p ON p.variant_id=v.variant_id \n"
. "WHERE p.monomorphic IS FALSE \n";
$variant_args[':poly1'] = $query_args['polymorphic']['first'];
$variant_args[':poly2'] = $query_args['polymorphic']['second'];
}
$args = array_merge($call_args, $variant_args);
$count = ndg_mview_query($vars['partition'], $sql, $args)->fetchField();
drupal_json_output(array(
'raw' => $count,
'formatted' => number_format($count),
));
}
/**
* JSON Callback: Returns the number of variants for a given genotype matrix.
*
* The genotype matrix is specified in the query parameters ($_GET). We still process the
* query arguements the same but this time the query we build needs to group by variant_id.
*
* This callback is used in the genotype matrix to asynchronously fill in the number of variants.
* @see theme/js/genotypeMatrixGetResultCount.js
*/
function nd_genotypes_count_genotype_matrix_variants_callback($partition = '', $type = 'ajax') {
// @PARTITION: This is where the partition is set for the genotype matrix. From here
// if is carried throughout the rest of the functions.
$vars = array('genus' => $partition, 'partition' => $partition);
// Save the query paramters.
$vars['q'] = drupal_get_query_parameters();
// Process the query paramters to determine the defaults for the filter form
// as well as to isolate the values that are needed to retrieve the data.
nd_genotypes_process_query_parameters($vars);
// Compile all the parameters we will use when grabbing the calls so that we can restrict
// the variant list to those that will have data once we do retrieve the calls.
$query_args = $vars['query_args'];
$call = $variant = $call_args = $variant_args = $variant_joins = array();
// Sequence Ranges:
// First get the arguements. There must always be a backbone and fmin even if the user
// did not provide one.
if (isset($query_args['start_backbone'])) {
if (isset($query_args['start_pos'])) {
$variant_args[':sbackbone'] = $query_args['start_backbone'];
$variant_args[':sfmin'] = (int) $query_args['start_pos'];
}
else {
$variant_args[':sbackbone'] = $query_args['start_backbone'];
$variant_args[':sfmin'] = 1;
}
unset($query_args['start_backbone'], $query_args['start_pos']);
}
if (isset($query_args['end_backbone'])) {
if (isset($query_args['end_pos'])) {
$variant_args[':ebackbone'] = $query_args['end_backbone'];
$variant_args[':efmin'] = (int) $query_args['end_pos'];
}
else {
$variant_args[':ebackbone'] = $query_args['end_backbone'];
// @TODO: Switch to bigints then change this value to match the largest bigint.
$variant_args[':efmin'] = 2147483645;
}
unset($query_args['end_backbone'], $query_args['end_pos']);
}
// Now add the query where condition.
if (isset($variant_args[':sbackbone']) AND isset($variant_args[':ebackbone'])) {
$variant[] = 'ROW(v.srcfeature_name, v.fmin) BETWEEN ROW(:sbackbone, :sfmin) AND ROW(:ebackbone,:efmin)';
}
elseif (isset($variant_args[':sbackbone'])) {
$variant[] = 'ROW(v.srcfeature_name, v.fmin) >= ROW(:sbackbone, :sfmin)';
}
elseif (isset($variant_args[':ebackbone'])) {
$variant[] = 'ROW(v.srcfeature_name, v.fmin) <= ROW(:ebackbone, :efmin)';
}
// Variant Type:
if (isset($query_args['variant_type'])) {
$variant[] = 'variant_type=:variant_type';
$variant_args[':variant_type'] = $query_args['variant_type'];
unset($query_args['variant_type']);
}
// Variant Name:
$variant_name_filter = FALSE;
if (isset($query_args['variant_name'])) {
$variant[] = "variant_name IN (VALUES('" . implode("'), ('", $query_args['variant_name']) . "'))";
//$variant_args[':variant_names'] = $query_args['variant_name'];
unset($query_args['variant_name']);
$variant_name_filter = TRUE;
}
elseif (isset($query_args['variant_name_list'])) {
$list_json = db_query('SELECT list FROM {ndg_matrix_variant_user_lists} WHERE list_id=:id',
array(':id' => $query_args['variant_name_list']))->fetchField();
$list = json_decode($list_json);
$variant[] = "variant_name IN (VALUES('" . implode("'), ('", $list) . "'))";
}
// Polymorphic:
$polymorphic_filter = FALSE;
if (isset($query_args['polymorphic'])
AND is_numeric($query_args['polymorphic']['first'])
AND is_numeric($query_args['polymorphic']['second'])) {
if (!$variant_name_filter) {
$variant_joins[] = "LEFT JOIN (
SELECT a.variant_id, a.allele_call=b.allele_call as monomorphic
FROM {mview_ndg_calls} a, {mview_ndg_calls} b
WHERE a.variant_id=b.variant_id AND a.germplasm_id=:poly1 AND b.germplasm_id=:poly2
) p ON p.variant_id=v.variant_id ";
$variant_args[':poly1'] = $query_args['polymorphic']['first'];
$variant_args[':poly2'] = $query_args['polymorphic']['second'];
$variant[] = "p.monomorphic IS FALSE";
}
$polymorphic_filter = TRUE;
}
// Germplasm:
if (isset($query_args['germplasm_id']) AND !empty($query_args['germplasm_id'])) {
$call[] = 'germplasm_id IN (:germplasm_ids)';
$call_args[':germplasm_ids'] = $query_args['germplasm_id'];
unset($query_args['germplasm_id']);
}
else {
return FALSE;
}
// Project:
if (isset($query_args['project_id']) AND !empty($query_args['project_id'])) {
if (sizeof($query_args['project_id']) == 1) {
$call[] = 'project_id = :project_id';
}
else {
$call[] = 'project_id IN (:project_id)';
}
$call_args[':project_id'] = $query_args['project_id'];
}
// Marker Type:
if (isset($query_args['marker_type'])) {
$call[] = 'marker_type = :marker_type';
$call_args[':marker_type'] = $query_args['marker_type'];
}
// Now determine the query for the variants.
$sql = "SELECT v.variant_id, v.variant_name \n"
. "FROM {mview_ndg_variants} v \n"
. implode("\n", $variant_joins)
. "WHERE \n";
// Add in conditions on the variants table.
if ($variant) {
$sql .= implode("\n AND ",$variant) . "\n AND ";
}
// Add in conditions on the call table.
$sql .= "EXISTS ( \n"
. " SELECT true \n"
. " FROM {mview_ndg_calls} call \n"
. " WHERE \n"
. " " . implode("\n AND ", $call) . "\n"
. " AND call.variant_id=v.variant_id\n"
. " ) \n";
if ($polymorphic_filter AND $variant_name_filter) {
$sql = "SELECT v.variant_id, v.variant_name FROM (\n"
. $sql . ") v \n"
. "LEFT JOIN (
SELECT a.variant_id, a.allele_call=b.allele_call as monomorphic
FROM {mview_ndg_calls} a, {mview_ndg_calls} b
WHERE a.variant_id=b.variant_id AND a.germplasm_id=:poly1 AND b.germplasm_id=:poly2
) p ON p.variant_id=v.variant_id \n"
. "WHERE p.monomorphic IS FALSE \n";
$variant_args[':poly1'] = $query_args['polymorphic']['first'];
$variant_args[':poly2'] = $query_args['polymorphic']['second'];
}
$count_sql = "SELECT count(distinct variant_name) FROM (" . $sql . " GROUP BY v.variant_id, v.variant_name) t";
$args = array_merge($call_args, $variant_args);
$count = ndg_mview_query($vars['partition'], $count_sql, $args)->fetchField();
drupal_json_output(array(
'raw' => $count,
'formatted' => number_format($count),
));
}