forked from bystrogenomics/bystro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeqFromQuery.pm
993 lines (742 loc) · 23 KB
/
SeqFromQuery.pm
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
use 5.10.0;
use strict;
use warnings;
# ABSTRACT: Create an annotation from a query
# TODO: 1) Support sort
####### 2) Don't initialize db
####### 3) figure out why even with field => 'pos' we get very slow perf
# 1) igure out if we want to change that.
# - It is ignored because the elasticsearch cluster is too slow on a per-shard basis
# 2) Document it
package SeqFromQuery;
our $VERSION = '0.001';
use namespace::autoclean;
use DDP;
use lib './lib';
use Mouse 2;
use MCE::Loop;
use Search::Elasticsearch;
use Seq::Output::Delimiters;
use Cpanel::JSON::XS qw/decode_json encode_json/;
use YAML::XS qw/LoadFile/;
use Statistics::Distributions qw(chisqrdistr udistr);
use Math::Round qw/nhimult round/;
use POSIX qw/lround/;
# Defines basic things needed in builder and annotator, like logPath,
# Also initializes the database with database_dir unnecessarily
extends 'Seq::Base';
# Defines most of the properties that can be configured at run time
# Needed because there are variations of Seq.pm, ilke SeqFromQuery.pm
with 'Seq::Definition';
# An archive, containing an "annotation" file
has inputQueryBody => (is => 'ro', isa => 'HashRef', required => 1);
# Post-processing to run, before commiting the annotation
has pipeline => (is => 'ro', isa => 'ArrayRef[HashRef]');
# Probably the user id
has indexName => (is => 'ro', required => 1);
# The index type; probably the job id
has indexType => (is => 'ro', required => 1);
has assembly => (is => 'ro', isa => 'Str', required => 1);
has configPath => (is => 'ro', isa => 'Str', default => 'config/');
# has commitEvery => (is => 'ro', default => 5000);
# The user may have given some header fields already
# If so, this is a re-indexing job, and we will want to append the header fields
has fieldNames => (is => 'ro', isa => 'ArrayRef', required => 1);
has indexConfig => (is => 'ro', isa => 'HashRef', required => 1);
# Elasticsearch connection configuration
has connection => (is => 'ro', isa => 'HashRef', required => 1);
has batchSize => (is => 'ro', isa => 'Num', default => 5000);
has shards => (is => 'ro', isa => 'Num', lazy => 1, default => sub {
my $self = shift;
return $self->indexConfig->{index_settings}->{index}->{number_of_shards};
});
has maxShards => (is => 'ro', isa => 'Num');
my $prettyCoder = Cpanel::JSON::XS->new->ascii->pretty->allow_nonref;;
around BUILDARGS => sub {
my ($orig, $class, $href) = @_;
my %data = %$href;
if($data{indexConfig}) {
if(!ref $data{indexConfig}) {
$data{indexConfig} = decode_json($data{indexConfig});
}
return $class->$orig(\%data);
}
my $cf = path('config')->child($data{assembly} . '.mapping.yml')->stringify();
$data{indexConfig} = LoadFile($cf);
return $class->$orig(\%data);
};
sub BUILD {
my $self = shift;
# Makes or fails silently if exists
$self->outDir->mkpath();
if(!$self->shards) {
$self->log('fatal', "Cannot read number of shards in index");
}
}
sub annotate {
my $self = shift;
$self->log( 'info', 'Beginning saving annotation from query' );
$self->log( 'info', 'Input query is: ' . $prettyCoder->encode($self->inputQueryBody) );
my $d = Seq::Output::Delimiters->new();
my %delims = (
'allele' => $d->alleleDelimiter,
'pos' => $d->positionDelimiter,
'value' => $d->valueDelimiter,
'overlap' => $d->overlapDelimiter,
'miss' => $d->emptyFieldChar,
'fieldSep' => $d->fieldSeparator,
);
################## Make the full output path ######################
# The output path always respects the $self->output_file_base attribute path;
my ($err, $outFh, $statsFh) = $self->_getFileHandles();
if ($err) {
$self->_errorWithCleanup($err);
return ($err, undef);
}
# TODO: Support sort
$self->_cleanQuery();
my ($parentsAref, $childrenAref) = $self->_getHeader();
my @childrenOrOnly = @$childrenAref;
my @parentNames = @$parentsAref;
my @fieldNames = @{$self->fieldNames};
my $outputHeader = join($delims{fieldSep}, @fieldNames);
say $outFh $outputHeader;
if($statsFh) {
say $statsFh $outputHeader;
}
($err, my $discordantIdx) = $self->_getDiscordantIdx();
if($err) {
$self->log('fatal', "Couldn't find discordant index");
}
# TODO: figure out why even with field => 'pos' we get very slow perf
# when having slices > shards with very large (1000+ terms) queries
my ($nSlices, $batchSize, $timeout) = $self->_getSearchParams();
my $slice = {
field => 'pos',
max => $nSlices,
};
my $hasSort = exists $self->inputQueryBody->{sort};
my $progressFunc = $self->_makeLogProgress($hasSort, $outFh, $statsFh, 3e4);
my @filterFunctions;
if($self->pipeline) {
my ($err, $filters) = $self->_makePipeline();
if($err) {
return ($err, undef);
}
if(defined $filters) {
@filterFunctions = @$filters;
}
}
MCE::Loop::init {
max_workers => $nSlices,
chunk_size => 1,
gather => $progressFunc,
};
$self->log('info', "Beginning to create annotation from the query");
# We do parallel fetch using sliced scroll queries.
# It is far too expensive to do a single-threaded scroll
# and pass the entire structure to MCE;
# Will get "recursion limit" errors in Storable due to the size of the structure
# for bulk queries
mce_loop {
my ($mce, $chunkRef, $chunkId) = @_;
my $id = $_;
my $es = Search::Elasticsearch->new($self->connection);
$slice->{id} = $id;
$self->inputQueryBody->{slice} = $slice;
my $scroll = $es->scroll_helper(
scroll => $timeout,
size => $batchSize,
body => $self->inputQueryBody,
index => $self->indexName,
type => $self->indexType,
);
while(my @docs = $scroll->next($batchSize)) {
my @sourceData;
# $#sourceData = $#docs;
my $skipped = 0;
DOCS: for my $doc (@docs) {
if(@filterFunctions) {
for my $f (@filterFunctions) {
if($f->($doc->{_source})) {
$skipped++;
next DOCS;
}
}
}
my @rowData;
# Initialize all values to undef
# Output.pm requires a sparse array for any missing values
# To preserve output order
$#rowData = $#fieldNames;
for my $y (0 .. $#fieldNames) {
$rowData[$y] = _populateArrayPathFromHash($childrenOrOnly[$y], $doc->{_source}{$parentNames[$y]});
}
if($rowData[$discordantIdx][0][0] eq 'false') {
$rowData[$discordantIdx][0][0] = 0;
} elsif($rowData[$discordantIdx][0][0] eq 'true') {
$rowData[$discordantIdx][0][0] = 1;
}
push @sourceData, \@rowData;
}
my $outputString = _makeOutputString(\@sourceData, \%delims);
$mce->gather(scalar @docs - $skipped, $outputString, $id);
}
} (0 .. $nSlices - 1);
# Flush
$progressFunc->(0, undef, undef, 1);
MCE::Loop::finish();
################ Finished writing file. If statistics, print those ##########
# First sync output to ensure everything needed is written
# then close all file handles and move files to output dir
$err = $self->safeSystem('sync')
||
$self->safeClose($outFh)
||
($statsFh && $self->safeClose($statsFh))
||
$self->_moveFilesToOutputDir();
if($err) {
$self->_errorWithCleanup($err);
return ($err, undef);
}
return ($err, $self->outputFilesInfo);
}
sub _makePipeline {
my $self = shift;
state $funcs = {
binomMaf => \&makeBinomFilter,
hwe => \&makeHweFilter,
};
if(!$self->pipeline) {
return (undef, undef);
}
my @funcs;
for my $step (@{$self->pipeline}) {
if(!exists $funcs->{$step->{key}}) {
$self->log('warn', "Couldn't find function for $step->{key}");
next;
}
my $makeFunc = $funcs->{$step->{key}};
# Skipped for some reason
if(!$makeFunc) {
next;
}
push @funcs, $makeFunc->($self, $step);
}
return (undef, \@funcs);
}
sub _getSearchParams {
my $self = shift;
my $numTerms = $self->_getNumTerms($self->inputQueryBody);
# -1 simply means we can't approximate the size
my $nSlices;
if($numTerms < 0 || $numTerms > 200) {
$nSlices = $self->shards;
} else {
$nSlices = $self->_getSlices();
}
my $batchSize = $self->batchSize;
my $timeout = '2m';
return ($nSlices, $batchSize, $timeout);
}
sub _cleanQuery {
my $self = shift;
# TODO: Support sort
$self->inputQueryBody->{sort} = ['_doc'];
if(exists $self->inputQueryBody->{aggs}) {
delete $self->inputQueryBody->{aggs};
}
return;
}
# TODO: Support more versions
sub _getNumTerms {
my $self = shift;
my $bool = $self->inputQueryBody->{query}{bool};
if(!$bool) {
return -1;
}
# TODO: handle complex scripts, which are incredibly slow in ES
# my $hasScripts;
# # if($bool->{filter}) {
# # my $str = encode_json($bool->{filter});
# # if($str =~ /script/isg) {
# # $hasScripts = 1;
# # }
# # }
# # # scripts are very expensive, like large queries
# # if($hasScripts) {
# # return -1;
# # }
my $mustObj = $bool->{must} || $bool->{filter};
my $mustQLen = 0;
my $mustQuery = '';
for my $obj (ref $mustObj eq 'ARRAY' ? $mustObj : $mustObj) {
if($obj->{query_string} && $obj->{query_string}{query}) {
$mustQLen += split(/\s+/, $obj->{query_string}{query});
next;
}
if($obj->{match}) {
for my $matchProps (values %{$obj->{match}}) {
# in the form query: {match: {field: {query: string, operator: string}}
if(ref $matchProps) {
$mustQLen += split(/\s+/, $matchProps->{query});
next;
}
# in the form query: {match: {field: string}}
$mustQLen += split(/\s+/, $matchProps);
}
}
}
return $mustQLen;
}
sub _getSlices {
my $self = shift;
my $nShards = $self->shards;
my $nThreads = $self->maxThreads;
if($nShards < $nThreads) {
my $divisor = nhimult(2,$nThreads / $nShards);
return $nShards * $divisor ;
}
# each thread runs at < 100% utilization
return $nShards * 2;
}
sub _getHeader {
my $self = shift;
my @fieldNames = @{$self->fieldNames};;
my @childrenOrOnly;
$#childrenOrOnly = $#fieldNames;
# Elastic top level of { parent => child } is parent.
my @parentNames;
$#parentNames = $#fieldNames;
for my $i (0 .. $#fieldNames) {
if( index($fieldNames[$i], '.') > -1 ) {
my @path = split(/\./, $fieldNames[$i]);
$parentNames[$i] = $path[0];
if(@path == 2) {
$childrenOrOnly[$i] = [ $path[1] ];
} elsif(@path > 2) {
$childrenOrOnly[$i] = [ @path[ 1 .. $#path] ];
}
} else {
$parentNames[$i] = $fieldNames[$i];
$childrenOrOnly[$i] = $fieldNames[$i];
}
}
return (\@parentNames, \@childrenOrOnly);
}
sub _populateArrayPathFromHash {
my ($pathAref, $dataForEndOfPath) = @_;
# $_[0] , $_[1] , $_[2]
if(!ref $pathAref) {
return $dataForEndOfPath;
}
for my $i (0 .. $#$pathAref) {
$dataForEndOfPath = $dataForEndOfPath->{$pathAref->[$i]};
}
return $dataForEndOfPath;
}
sub _makeOutputString {
my ($arrayRef, $delims) = @_;
my $emptyFieldChar = $delims->{miss};
# Expects an array of row arrays, which contain an for each column, or an undefined value
for my $row (@$arrayRef) {
COLUMN_LOOP: for my $column (@$row) {
# Some fields may just be missing; we won't store even the
# alt/pos [[]] structure for those
if(!defined $column) {
$column = $emptyFieldChar;
next COLUMN_LOOP;
}
for my $alleleData (@$column) {
POS_LOOP: for my $positionData (@$alleleData) {
if(!defined $positionData) {
$positionData = $emptyFieldChar;
next POS_LOOP;
}
if(ref $positionData) {
$positionData = join($delims->{value}, map {
defined $_
?
(ref $_ ? join($delims->{overlap}, @$_) : $_)
: $emptyFieldChar
} @$positionData);
next POS_LOOP;
}
}
$alleleData = join($delims->{pos}, @$alleleData);
}
$column = join($delims->{allele}, @$column);
}
$row = join($delims->{fieldSep}, @$row);
}
return join("\n", @$arrayRef);
}
sub _makeLogProgress {
my ($self, $hasSort, $outFh, $statsFh, $throttleThreshold) = @_;
if(!$throttleThreshold) {
$throttleThreshold = 2e4;
}
my $throttleIndicator = 0;
my $total = 0;
my $hasPublisher = $self->hasPublisher;
# my %result;
my $orderId = 0;
return sub {
my ($progress, $outputStringRef, $chunkId, $flush) = @_;
$throttleIndicator += $progress;
if($hasPublisher) {
$total += $progress;
if($throttleIndicator >= $throttleThreshold || $flush) {
$self->publishProgress($total);
$throttleIndicator = 0;
}
}
if(!$outputStringRef) {
return;
}
say $statsFh $outputStringRef;
say $outFh $outputStringRef;
# TODO: Make ordered print work
# while (1) {
# if(!exists $result{$orderId}) {
# $orderId = 0;
# last;
# }
# say $outFh $result{$orderId};
# delete $result{$orderId};
# $orderId++;
# }
return;
}
}
sub _getFileHandles {
my $self = shift;
my ($err, $outFh, $statsFh);
# _working dir from Seq::Definition
my $outPath = $self->_workingDir->child($self->outputFilesInfo->{annotation});
($err, $outFh) = $self->getWriteFh($outPath);
if($err) {
return ($err, undef, undef);
}
if(!$self->run_statistics) {
return ($err, $outFh, $statsFh);
}
my $args = $self->_statisticsRunner->getStatsArguments();
$err = $self->safeOpen($statsFh, "|-", $args);
if($err) {
return ($err, undef, undef);
}
return ($err, $outFh, $statsFh);
}
sub _getDiscordantIdx {
my $self = shift;
my $idx = 0;
my $didx = -1;
for my $field (@{$self->fieldNames}) {
if ($field eq 'discordant') {
$didx = $idx;
last;
}
$idx++;
}
if($didx == -1) {
return ("Couldn't find index", undef);
}
return (undef, $didx);
}
sub _errorWithCleanup {
my ($self, $msg) = @_;
# To send a message to clean up files.
# TODO: Need somethign better
#MCE->gather(undef, undef, $msg);
$self->log('warn', $msg);
return $msg;
}
sub makeHweFilter {
my $self = shift;
my $props = shift;
my $nSamples = $props->{numSamples};
if($nSamples < 1) {
return;
}
my $alpha = $props->{critValue};
if(!$alpha) {
return;
}
# A positive z value
my $chiCrit = chisqrdistr(1, $alpha);
# Copy-on-write in multithreaded env; no need to worry about race
my($eHets, $eHomsMajor, $eHomsMinor, $n, $hets, $homsMajor, $homsMinor, $p);
# binomial approximation
# http://www.halotype.com/RKM/figures/TJF/binomial.txt
return sub {
#my $doc = shift;
# $_[0]
# $q = $_[0]->{'sampleMaf'}[0][0];
$p = 1 - $_[0]->{'sampleMaf'}[0][0];
# TODO: should we allow sites like these? Currently skip
if($p == 0) {
return 1;
}
$n = $nSamples * (1 - $_[0]->{'missingness'}[0][0]);
$eHets = 2 * $p * (1 - $p) * $n;
$eHomsMajor = ($p ** 2) * $n;
$eHomsMinor = $n - ($eHets + $eHomsMajor);
$hets = $n * $_[0]->{'heterozygosity'}[0][0];
$homsMinor = $n * $_[0]->{'homozygosity'}[0][0];
$homsMajor = $n - ($hets + $homsMinor);
if($eHets == 0 || $eHomsMajor == 0 || $eHomsMinor == 0) {
say STDERR "n: $n, missinginess: $_[0]->{'missingness'}[0][0], nSamples: $nSamples, p: $p, eHets: $eHets ; eHomsMajor : $eHomsMajor; eHomsMinor: $eHomsMinor";
say STDERR "hets: $hets, homsMajor: $homsMajor; homsMinor: $homsMinor";
sleep(1000);
}
# Returns truthy if test statistic is > $chiCrit, which means
# in rejection region == skip
return $chiCrit < ( ($hets - $eHets) ** 2 ) / $eHets
+ ( ($homsMajor - $eHomsMajor) ** 2) / $eHomsMajor
+ ( ($homsMinor - $eHomsMinor) ** 2) / $eHomsMinor;
}
}
# TODO: add binomial test if n is small
# Requires numSamples, estimates, and critValue
# Else will not filter anything
sub makeBinomFilter {
my $self = shift;
my $props = shift;
my $nChromosomes = $props->{numSamples} * 2;
if($nChromosomes < 2) {
return;
}
my $afFieldsAref = $props->{estimates};
if(!@$afFieldsAref) {
return;
}
my $alpha = $props->{critValue};
if(!$alpha) {
return;
}
if($alpha == 0 || $alpha > .5) {
$self->log('error', "Alpha must larger than 0 and smaller than .5");
return;
}
# A positive z value; udistr for .05 will give 1.65
# unlike Math::Gauss, for which inv_cdf will give -1.65
my $zCrit = udistr($alpha);
my $snpOnly = $props->{snpOnly};
my $privateMaf = $props->{privateMaf} || 0;
my $minPossibleEstimate = sprintf("%.2f", 1/$nChromosomes);
# Different from 1, because of possible rounding
# my $minK = 1.5;
# TODO: Don't defeat? If privateMaf is set,
# then at minimum we should not allow it to be less than $minPossibleEstimate
if($privateMaf < $minPossibleEstimate) {
$privateMaf = $minPossibleEstimate;
}
# If we calculate K to be a number not quite 1, count it as 1
# for purpose of checking whether we're at minimum
my $roundedMinK = 1.1;
# say STDERR "MIN: $minPossibleEstimate ; snpOnly: $snpOnly ; privateMaf: $minPossibleEstimate ; zCrit: $zCrit; alpha: $alpha";
# sleep(1000);
for my $f (@$afFieldsAref) {
my @path = split(/[.]/, $f);
$f = \@path;
}
# Copy-on-write in multithreaded env; no need to worry about race
my($n, $k, $p, $isRare);
# binomial approximation
# http://www.halotype.com/RKM/figures/TJF/binomial.txt
return sub {
#my $doc = shift;
# $_[0]
#$doc->
if($snpOnly && length($_[0]->{'alt'}[0][0]) > 1) {
#0 means don't skip
return 0;
}
#$doc->
$n = $nChromosomes * (1 - $_[0]->{'missingness'}[0][0]);
#$doc->
$k = $n * $_[0]->{'sampleMaf'}[0][0];
# NOW WE PASS RARE THINGS ONLY IF ESTIMATES ARE ALSO RARE
# The 2nd condition is to ensure that we don't consider something like 1.0016
# to be more common that our $minimumPossibleEstimate
$isRare = $_[0]->{'sampleMaf'}[0][0] <= $privateMaf || ($n == $nChromosomes && $k < 1.5);
undef $p;
my $tested = 0;
AF_LOOP: for my $field (@{$afFieldsAref}) {
# Avoid auto-vivification
#$doc->
if(!exists $_[0]->{$field->[0]}) {
next;
}
# Example: gnomad.exomes.af
if(@$field == 3) {
#$doc-> #$doc->
if(!exists $_[0]->{$field->[0]}{$field->[1]} || !exists $_[0]->{$field->[0]}{$field->[1]}{$field->[2]}) {
next AF_LOOP;
}
#$doc->
$p = $_[0]->{$field->[0]}{$field->[1]}{$field->[2]}[0][0];
} elsif(@$field == 2) {
# Example: dbSNP.alleleFreq
#$doc->
if(!exists $_[0]->{$field->[0]}{$field->[1]}) {
next AF_LOOP;
}
#$doc->
$p = $_[0]->{$field->[0]}{$field->[1]}[0][0];
} else {
# Try to avoid the expensive loop
$p = $_[0]->{$field->[0]};
for(my $i = 1; $i < @$field; $i++) {
if(!exists $p->{$field->[$i]}) {
next AF_LOOP;
}
$p = $p->{$field->[$i]}
}
$p = $p->[0][0];
}
if(!defined $p) {
next AF_LOOP;
}
# Handles cases where p == 1 (can't calculate p-value), p == 0, and p <= privateMef
# TODO: May want to skip to next pop af estimate is $p == 1
# Currently if $p == 1 we allow only if !isRare
# Because something that is extremely common in the population
# but is rare in ours, will be something very very odd
# or an annotation mistake
if(($p == 1 && !$isRare) || ($p <= $privateMaf && $isRare)) {
return 0;
}
# say STDERR "z is ". abs($n * $p - $k) / sqrt( $n * $p * (1 - $p) ) . " for p: $p n: $n , k: $k, zCrit: $zCrit";
# TODO: allow flag to be more conservative; drop if doesn't pass in all
# TODO: prevent underflow more consistently
# If we have a smaller deviation than allowed by our critical value
if( abs($k - $n * $p) / sqrt( $n * $p * (1 - $p) ) <= $zCrit) {
return 0;
}
$tested++;
}
# If the site isn't present in then populations of interest it is
# likely to be rare.
# In those cases, accept sites thats are rare in our dataset as well
if($tested == 0 && $isRare) {
return 0;
}
return 1;
}
# Returns 1 if we want to skip this site, 0 otherwise
# return sub {
# #my ($doc) = @_;
# # $_[0]
# #$doc
# if($snpOnly && length($_[0]->{'alt'}[0][0]) > 1) {
# #0 means don't skip
# return 0;
# }
# #$doc
# if($_[0]->{'sampleMaf'}[0][0] <= $privateMaf) {
# return 0;
# }
# #$doc
# my $n = $N * (1 - $_[0]->{'missingness'}[0][0]);
# #$doc
# my $ac = $n * $_[0]->{'sampleMaf'}[0][0];
# for my $field (@{$afFieldsAref}) {
# my $f = $_[0]->{$field->[0]};
# if(@$field == 2) {
# $f = $f->{$field->[1]};
# } elsif(@$field == 3) {
# $f = $f->{$field->[1]}{$field->[2]};
# } else {
# for(my $i = 1; $i < @$field; $i++) {
# $f = $f->{$field->[$i]}
# }
# }
# if(!defined $f) {
# next;
# }
# #$doc
# if(_binomProb($f->[0][0], $n, $ac) >= $alpha) {
# return 0;
# }
# }
# # 1 means skip
# return 1;
# }
}
# sub _binomProb {
# # N is likely the number of chromosomes
# #my ($popAf, $N, $ac) = @_;
# # $_[0] $_[1], $_[2]
# # $popAf $popAf
# if($_[0] < 0 || $_[0] > 1) {
# return 0;
# }
# # $N $N
# if($_[1] < 2 || $_[1] > 10_000_000) {
# return 0;
# }
# # $popAf
# my $q = 1 - $_[0];
# # $N * $popAf
# my $cent = round($_[1] * $_[0]);
# if($cent == 0) {
# return 0;
# }
# p $cent;
# my @L;
# # $N
# $#L = $_[1];
# $L[$cent] = 1;
# my $eps = 1e-8 / $_[1];
# my $tot = 1;
# my $k;
# for(my $i = $cent - 1; $i >= 0; $i--) {
# $k = $L[$i + 1] * $q * ($i + 1);
# # $popAf $N
# $k /= $_[0] * ($_[1] - $i);
# p $k;
# if($k < $eps) {
# $L[$i] = 0;
# $i = 0;
# } else {
# $L[$i] = $k;
# }
# $tot += $L[$i];
# }
# say STDERR "TOT IS $tot";
# sleep(1000);
# for(my $i = $cent + 1; $i <= $_[1]; $i++) {
# # $popAf $N
# $k = $L[$i-1] * $_[0] * ($_[1]-($i-1));
# $k /= $q * $i;
# if($k < $eps) {
# $L[$i] = 0;
# # $N
# $i = $_[1];
# } else {
# $L[$i] = $k;
# }
# $tot += $L[$i];
# }
# p @L;
# # $N
# for(my $i = 0; $i <= $_[1]; $i++) {
# if(!defined $L[$i]) {
# say STDERR "NOT DEF WHY";
# sleep(1000);
# }
# $L[$i] /= $tot;
# # print "$i $L[$i]\n";
# }
# my $rightTail = 0;
# # $ac $N
# for(my $i = $_[2]; $i<= $_[1]; $i++) {
# $rightTail += $L[$i];
# }
# return $rightTail;
# }
__PACKAGE__->meta->make_immutable;
1;