forked from solgenomics/sgn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.pm
2249 lines (1952 loc) · 97 KB
/
Search.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
994
995
996
997
998
999
1000
package CXGN::Genotype::Search;
=head1 NAME
CXGN::Genotype::Search - an object to handle searching genotypes for stocks
=head1 USAGE
PLEASE BE AWARE THAT THE DEFAULT OPTIONS FOR genotypeprop_hash_select, protocolprop_top_key_select, protocolprop_marker_hash_select ARE PRONE TO EXCEEDING THE MEMORY LIMITS OF VM. CHECK THE MOOSE ATTRIBUTES BELOW TO SEE THE DEFAULTS, AND ADJUST YOUR MOOSE INSTANTIATION ACCORDINGLY
my $genotypes_search = CXGN::Genotype::Search->new({
bcs_schema=>$schema,
people_schema=>$people_schema,
accession_list=>$accession_list,
tissue_sample_list=>$tissue_sample_list,
trial_list=>$trial_list,
protocol_id_list=>$protocol_id_list,
markerprofile_id_list=>$markerprofile_id_list,
genotype_data_project_list=>$genotype_data_project_list,
chromosome_list=>\@chromosome_numbers,
start_position=>$start_position,
end_position=>$end_position,
marker_name_list=>['S80_265728', 'S80_265723'],
genotypeprop_hash_select=>['DS', 'GT', 'DP'], #THESE ARE THE KEYS IN THE GENOTYPEPROP OBJECT
protocolprop_top_key_select=>['reference_genome_name', 'header_information_lines', 'marker_names', 'markers'], #THESE ARE THE KEYS AT THE TOP LEVEL OF THE PROTOCOLPROP OBJECT
protocolprop_marker_hash_select=>['name', 'chrom', 'pos', 'alt', 'ref'], #THESE ARE THE KEYS IN THE MARKERS OBJECT IN THE PROTOCOLPROP OBJECT
return_only_first_genotypeprop_for_stock=>0, #THIS IS TO CONSERVE MEMORY USAGE
limit=>$limit,
offset=>$offset,
forbid_cache=>$forbid_cache
# marker_search_hash_list=>[{'S80_265728' => {'pos' => '265728', 'chrom' => '1'}}], NOT IMPLEMENTED
# marker_score_search_hash_list=>[{'S80_265728' => {'GT' => '0/0', 'GQ' => '99'}}], NOT IMPLEMENTED
});
# my ($total_count, $genotypes) = $genotypes_search->get_genotype_info();
# RECOMMENDED
If you just want to get a file with the genotype result in a dosage matrix or VCF file, use get_cached_file_dosage_matrix or get_cached_file_VCF functions instead.
If you want results in json format use get_cached_file_search_json
If you want results in json format for only the metadata (no genotype call data), use get_cached_file_search_json()
=head1 DESCRIPTION
=head1 AUTHORS
Nicolas Morales <[email protected]>
With code moved from CXGN::BreederSearch
Lukas Mueller <[email protected]>
Aimin Yan <[email protected]>
=cut
use strict;
use warnings;
use Moose;
use Try::Tiny;
use Data::Dumper;
use SGN::Model::Cvterm;
use CXGN::Trial;
use JSON;
use CXGN::Stock::Accession;
use CXGN::Genotype::Protocol;
use CXGN::Genotype::ComputeHybridGenotype;
use Cache::File;
use Digest::MD5 qw | md5_hex |;
use File::Slurp qw | write_file |;
use File::Temp qw | tempfile |;
use File::Copy;
use POSIX;
has 'bcs_schema' => (
isa => 'Bio::Chado::Schema',
is => 'rw',
required => 1
);
has 'people_schema' => (
isa => 'CXGN::People::Schema',
is => 'rw',
required => 1
);
has 'protocol_id_list' => (
isa => 'ArrayRef[Int]|Undef',
is => 'rw',
);
has 'markerprofile_id_list' => (
isa => 'ArrayRef[Int]|Undef',
is => 'ro',
);
has 'accession_list' => (
isa => 'ArrayRef[Int]|Undef',
is => 'ro',
);
has 'tissue_sample_list' => (
isa => 'ArrayRef[Int]|Undef',
is => 'ro',
);
has 'trial_list' => (
isa => 'ArrayRef[Int]|Undef',
is => 'ro',
);
has 'genotype_data_project_list' => (
isa => 'ArrayRef[Int]|Undef',
is => 'ro',
);
has 'chromosome_list' => (
isa => 'ArrayRef[Int]|ArrayRef[Str]|Undef',
is => 'ro',
);
has 'start_position' => (
isa => 'Int|Undef',
is => 'ro',
);
has 'end_position' => (
isa => 'Int|Undef',
is => 'ro',
);
has 'marker_name_list' => (
isa => 'ArrayRef[Str]|Undef',
is => 'ro',
);
has 'genotypeprop_hash_select' => (
isa => 'ArrayRef[Str]',
is => 'ro',
default => sub {['GT', 'AD', 'DP', 'GQ', 'DS', 'PL', 'NT']} #THESE ARE THE GENERIC AND EXPECTED VCF ATRRIBUTES
);
has 'protocolprop_top_key_select' => (
isa => 'ArrayRef[Str]',
is => 'ro',
default => sub {['reference_genome_name', 'species_name', 'header_information_lines', 'sample_observation_unit_type_name', 'marker_names', 'markers', 'markers_array']} #THESE ARE ALL POSSIBLE TOP LEVEL KEYS IN PROTOCOLPROP BASED ON VCF LOADING
);
has 'protocolprop_marker_hash_select' => (
isa => 'ArrayRef[Str]',
is => 'ro',
default => sub {['name', 'chrom', 'pos', 'alt', 'ref', 'qual', 'filter', 'info', 'format']} #THESE ARE ALL POSSIBLE PROTOCOLPROP MARKER HASH KEYS BASED ON VCF LOADING
);
has 'return_only_first_genotypeprop_for_stock' => (
isa => 'Bool',
is => 'ro',
default => 0
);
# When using the get_cached_file_dosage_matrix or get_cached_file_VCF functions, need the following three keys.
has 'cache_root' => (
isa => 'Str',
is => 'rw',
);
has 'cache' => (
isa => 'Cache::File',
is => 'rw',
);
has 'cache_expiry' => (
isa => 'Int',
is => 'rw',
default => 0, # never expires?
);
has 'forbid_cache' => (
isa => 'Bool',
is => 'ro',
default => 0
);
has 'prevent_transpose' => (
isa => 'Bool',
is => 'ro',
default => 0
);
has '_iterator_query_handle' => (
isa => 'Ref',
is => 'rw'
);
has '_iterator_genotypeprop_query_handle' => (
isa => 'Ref',
is => 'rw'
);
has '_filtered_markers' => (
isa => 'HashRef',
is => 'rw',
default => sub {{}}
);
has '_snp_genotyping_cvterm_id' => (
isa => 'Int',
is => 'rw'
);
has '_vcf_snp_genotyping_cvterm_id' => (
isa => 'Int',
is => 'rw'
);
has '_vcf_map_details_cvterm_id' => (
isa => 'Int',
is => 'rw'
);
has '_vcf_map_details_markers_cvterm_id' => (
isa => 'Int',
is => 'rw'
);
has '_vcf_map_details_markers_array_cvterm_id' => (
isa => 'Int',
is => 'rw'
);
has '_igd_genotypeprop_cvterm_id' => (
isa => 'Int',
is => 'rw'
);
has '_accession_cvterm_id' => (
isa => 'Int',
is => 'rw'
);
has '_tissue_sample_cvterm_id' => (
isa => 'Int',
is => 'rw'
);
has '_plot_cvterm_id' => (
isa => 'Int',
is => 'rw'
);
has '_plant_cvterm_id' => (
isa => 'Int',
is => 'rw'
);
has '_tissue_sample_of_cvterm_id' => (
isa => 'Int',
is => 'rw'
);
has '_protocolprop_markers_h' => (
isa => 'Ref',
is => 'rw'
);
has '_protocolprop_top_key_h' => (
isa => 'Ref',
is => 'rw'
);
has '_protocolprop_top_key_markers_h' => (
isa => 'Ref',
is => 'rw'
);
has '_protocolprop_top_key_markers_array_h' => (
isa => 'Ref',
is => 'rw'
);
has '_genotypeprop_h' => (
isa => 'Ref',
is => 'rw'
);
has '_protocolprop_marker_hash_select_arr' => (
isa => 'ArrayRef',
is => 'rw'
);
has '_protocolprop_top_key_select_arr' => (
isa => 'ArrayRef',
is => 'rw'
);
has '_selected_protocol_marker_info' => (
isa => 'Ref',
is => 'rw'
);
has '_selected_protocol_top_key_info' => (
isa => 'Ref',
is => 'rw'
);
has '_genotypeprop_infos' => (
isa => 'ArrayRef',
is => 'rw'
);
has '_genotypeprop_infos_counter' => (
isa => 'Int',
is => 'rw'
);
has '_genotypeprop_hash_select_arr' => (
isa => 'ArrayRef',
is => 'rw'
);
#NOT IMPLEMENTED
has 'marker_search_hash_list' => (
isa => 'ArrayRef[HashRef]|Undef',
is => 'ro',
);
#NOT IMPLEMENTED
has 'marker_score_search_hash_list' => (
isa => 'ArrayRef[HashRef]|Undef',
is => 'ro',
);
has 'limit' => (
isa => 'Int|Undef',
is => 'rw',
);
has 'offset' => (
isa => 'Int|Undef',
is => 'rw',
);
=head2 get_genotype_info
returns: an array with genotype information
=cut
=head2 get_genotype_info()
Function for getting genotype data iteratively.
Should be used like:
my $genotype_search = CXGN::Genotype::Search->new({
bcs_schema=>$schema,
etc...
});
my ($count, $genotype_data) = $genotype_search->get_genotype_info();
If you want to get results iteratively, use the init and iterator function defined below instead. Iterative retrieval minimizes memory load.
If you just want to get a file with the genotype result in a dosage matrix or VCF file, use get_cached_file_dosage_matrix or get_cached_file_VCF functions instead.
If you want results in json format use get_cached_file_search_json
If you want results in json format for only the metadata (no genotype call data), use get_cached_file_search_json()
=cut
sub get_genotype_info {
my $self = shift;
my $schema = $self->bcs_schema;
my $trial_list = $self->trial_list;
my $genotype_data_project_list = $self->genotype_data_project_list;
my $protocol_id_list = $self->protocol_id_list;
my $markerprofile_id_list = $self->markerprofile_id_list;
my $accession_list = $self->accession_list;
my $tissue_sample_list = $self->tissue_sample_list;
my $marker_name_list = $self->marker_name_list;
my $chromosome_list = $self->chromosome_list;
my $start_position = $self->start_position;
my $end_position = $self->end_position;
my $genotypeprop_hash_select = $self->genotypeprop_hash_select;
my $protocolprop_top_key_select = $self->protocolprop_top_key_select;
my $protocolprop_marker_hash_select = $self->protocolprop_marker_hash_select;
my $marker_search_hash_list = $self->marker_search_hash_list;
my $marker_score_search_hash_list = $self->marker_score_search_hash_list;
my $return_only_first_genotypeprop_for_stock = $self->return_only_first_genotypeprop_for_stock;
my $limit = $self->limit;
my $offset = $self->offset;
my @data;
my %search_params;
my @where_clause;
my $snp_genotyping_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'snp genotyping', 'genotype_property')->cvterm_id();
my $vcf_snp_genotyping_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'vcf_snp_genotyping', 'genotype_property')->cvterm_id();
my $vcf_map_details_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'vcf_map_details', 'protocol_property')->cvterm_id();
my $vcf_map_details_markers_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'vcf_map_details_markers', 'protocol_property')->cvterm_id();
my $vcf_map_details_markers_array_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'vcf_map_details_markers_array', 'protocol_property')->cvterm_id();
my $igd_genotypeprop_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'igd number', 'genotype_property')->cvterm_id();
my $accession_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'accession', 'stock_type')->cvterm_id();
my $tissue_sample_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'tissue_sample', 'stock_type')->cvterm_id();
my $plot_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'plot', 'stock_type')->cvterm_id();
my $plant_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'plant', 'stock_type')->cvterm_id();
my $tissue_sample_of_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'tissue_sample_of', 'stock_relationship')->cvterm_id();
my @trials_accessions;
foreach (@$trial_list){
my $trial = CXGN::Trial->new({bcs_schema=>$schema, trial_id=>$_});
my $accessions = $trial->get_accessions();
foreach (@$accessions){
push @trials_accessions, $_->{stock_id};
}
}
#If accessions are explicitly given, then accessions found from trials will not be added to the search.
if (!$accession_list || scalar(@$accession_list)==0) {
push @$accession_list, @trials_accessions;
}
#For projects inserted into database during the addition of genotypes and genotypeprops
if (scalar(@trials_accessions)==0){
if ($trial_list && scalar(@$trial_list)>0) {
my $trial_sql = join ("," , @$trial_list);
push @where_clause, "project.project_id in ($trial_sql)";
}
}
#For genotyping_data_project
if ($genotype_data_project_list && scalar($genotype_data_project_list)>0) {
my $sql = join ("," , @$genotype_data_project_list);
push @where_clause, "project.project_id in ($sql)";
}
if ($protocol_id_list && scalar(@$protocol_id_list)>0) {
my $protocol_sql = join ("," , @$protocol_id_list);
push @where_clause, "nd_protocol.nd_protocol_id in ($protocol_sql)";
}
if ($accession_list && scalar(@$accession_list)>0) {
my $accession_sql = join ("," , @$accession_list);
push @where_clause, " ( stock.stock_id in ($accession_sql) OR (accession_of_tissue_sample.stock_id in ($accession_sql) AND accession_of_tissue_sample.type_id = $accession_cvterm_id) ) ";
push @where_clause, "stock.type_id in ($accession_cvterm_id, $tissue_sample_cvterm_id)";
}
if ($tissue_sample_list && scalar(@$tissue_sample_list)>0) {
my $stock_sql = join ("," , @$tissue_sample_list);
push @where_clause, "stock.stock_id in ($stock_sql)";
push @where_clause, "stock.type_id = $tissue_sample_cvterm_id";
}
if ($markerprofile_id_list && scalar(@$markerprofile_id_list)>0) {
my $markerprofile_sql = join ("," , @$markerprofile_id_list);
push @where_clause, "genotype.genotype_id in ($markerprofile_sql)";
}
if ($marker_name_list && scalar(@$marker_name_list)>0) {
my $search_vals_sql = "'".join ("','" , @$marker_name_list)."'";
push @where_clause, "nd_protocolprop.value->'marker_names' \\?& array[$search_vals_sql]";
}
if ($marker_search_hash_list && scalar(@$marker_search_hash_list)>0) {
foreach (@$marker_search_hash_list){
my $json_val = encode_json $_;
push @where_clause, "nd_protocolprop.value->'markers' \\@> $json_val"."::jsonb";
}
}
if ($marker_score_search_hash_list && scalar(@$marker_score_search_hash_list)>0) {
foreach (@$marker_score_search_hash_list){
my $json_val = encode_json $_;
push @where_clause, "genotype_values.value \\@> $json_val"."::jsonb";
}
}
my $where_clause = scalar(@where_clause)>0 ? " WHERE " . (join (" AND " , @where_clause)) : '';
my $offset_clause = '';
my $limit_clause = '';
if ($limit){
$limit_clause = " LIMIT $limit ";
}
if ($offset){
$offset_clause = " OFFSET $offset ";
}
my $stock_select = '';
if ($return_only_first_genotypeprop_for_stock) {
$stock_select = 'distinct on (stock.stock_id) stock.stock_id';
} else {
$stock_select = 'stock.stock_id';
}
my $q = "SELECT $stock_select, igd_number_genotypeprop.value, nd_protocol.nd_protocol_id, nd_protocol.name, stock.uniquename, stock.type_id, stock_cvterm.name, genotype.genotype_id, genotype.uniquename, genotype.description, project.project_id, project.name, project.description, accession_of_tissue_sample.stock_id, accession_of_tissue_sample.uniquename, count(genotype.genotype_id) OVER() AS full_count
FROM stock
JOIN cvterm AS stock_cvterm ON(stock.type_id = stock_cvterm.cvterm_id)
LEFT JOIN stock_relationship ON(stock_relationship.subject_id=stock.stock_id AND stock_relationship.type_id = $tissue_sample_of_cvterm_id)
LEFT JOIN stock AS accession_of_tissue_sample ON(stock_relationship.object_id=accession_of_tissue_sample.stock_id)
JOIN nd_experiment_stock ON(stock.stock_id=nd_experiment_stock.stock_id)
JOIN nd_experiment USING(nd_experiment_id)
JOIN nd_experiment_protocol USING(nd_experiment_id)
JOIN nd_experiment_project USING(nd_experiment_id)
JOIN nd_experiment_genotype USING(nd_experiment_id)
JOIN nd_protocol USING(nd_protocol_id)
LEFT JOIN nd_protocolprop ON(nd_protocolprop.nd_protocol_id = nd_protocol.nd_protocol_id AND nd_protocolprop.type_id = $vcf_map_details_cvterm_id)
JOIN genotype USING(genotype_id)
LEFT JOIN genotypeprop AS igd_number_genotypeprop ON(igd_number_genotypeprop.genotype_id = genotype.genotype_id AND igd_number_genotypeprop.type_id = $igd_genotypeprop_cvterm_id)
JOIN project USING(project_id)
$where_clause
ORDER BY stock.stock_id, genotype.genotype_id ASC
$limit_clause
$offset_clause;";
#print STDERR Dumper $q;
my $h = $schema->storage->dbh()->prepare($q);
$h->execute();
my $total_count = 0;
my @genotype_id_array;
my @genotypeprop_array;
my %genotype_hash;
my %genotypeprop_hash;
my %protocolprop_hash;
while (my ($stock_id, $igd_number_json, $protocol_id, $protocol_name, $stock_name, $stock_type_id, $stock_type_name, $genotype_id, $genotype_uniquename, $genotype_description, $project_id, $project_name, $project_description, $accession_id, $accession_uniquename, $full_count) = $h->fetchrow_array()) {
my $igd_number_hash = $igd_number_json ? decode_json $igd_number_json : undef;
my $igd_number = $igd_number_hash ? $igd_number_hash->{'igd number'} : undef;
$igd_number = !$igd_number && $igd_number_hash ? $igd_number_hash->{'igd_number'} : undef;
my $germplasmName = '';
my $germplasmDbId = '';
if ($stock_type_name eq 'accession'){
$germplasmName = $stock_name;
$germplasmDbId = $stock_id;
}
if ($stock_type_name eq 'tissue_sample'){
$germplasmName = $accession_uniquename;
$germplasmDbId = $accession_id;
}
my $stock_object = CXGN::Stock::Accession->new({schema=>$self->bcs_schema, stock_id=>$germplasmDbId});
push @genotype_id_array, $genotype_id;
$genotype_hash{$genotype_id} = {
markerProfileDbId => $genotype_id,
germplasmDbId => $germplasmDbId,
germplasmName => $germplasmName,
synonyms => $stock_object->synonyms,
stock_id => $stock_id,
stock_name => $stock_name,
stock_type_id => $stock_type_id,
stock_type_name => $stock_type_name,
genotypeDbId => $genotype_id,
genotypeUniquename => $genotype_uniquename,
genotypeDescription => $genotype_description,
analysisMethodDbId => $protocol_id,
analysisMethod => $protocol_name,
genotypingDataProjectDbId => $project_id,
genotypingDataProjectName => $project_name,
genotypingDataProjectDescription => $project_description,
igd_number => $igd_number,
};
$protocolprop_hash{$protocol_id}++;
$total_count = $full_count;
}
print STDERR "CXGN::Genotype::Search has genotype_ids $total_count\n";
my @found_protocolprop_ids = keys %protocolprop_hash;
my @protocolprop_marker_hash_select_arr;
foreach (@$protocolprop_marker_hash_select){
push @protocolprop_marker_hash_select_arr, "s.value->>'$_'";
}
my @protocolprop_top_key_select_arr;
my %protocolprop_top_key_select_hash;
foreach (@$protocolprop_top_key_select){
if ($_ ne 'markers' && $_ ne 'markers_array') {
push @protocolprop_top_key_select_arr, "value->>'$_'";
}
$protocolprop_top_key_select_hash{$_}++;
}
my %selected_protocol_marker_info;
my %selected_protocol_top_key_info;
my %filtered_markers;
my $genotypeprop_chromosome_rank_string = '';
if (scalar(@found_protocolprop_ids)>0){
my $protocolprop_id_sql = join ("," , @found_protocolprop_ids);
my $protocolprop_where_sql = "nd_protocol_id in ($protocolprop_id_sql) and type_id = $vcf_map_details_cvterm_id";
my $protocolprop_where_markers_sql = "nd_protocol_id in ($protocolprop_id_sql) and type_id = $vcf_map_details_markers_cvterm_id";
my $protocolprop_where_markers_array_sql = "nd_protocol_id in ($protocolprop_id_sql) and type_id = $vcf_map_details_markers_array_cvterm_id";
my $protocolprop_hash_select_sql = scalar(@protocolprop_marker_hash_select_arr) > 0 ? ', '.join ',', @protocolprop_marker_hash_select_arr : '';
my $chromosome_where = '';
if ($chromosome_list && scalar(@$chromosome_list)>0) {
my $chromosome_list_sql = '\'' . join('\', \'', @$chromosome_list) . '\'';
$chromosome_where = " AND (s.value->>'chrom')::text IN ($chromosome_list_sql)";
#$genotypeprop_chromosome_rank_string = " AND value->>'CHROM' IN ($chromosome_list_sql) ";
}
my $start_position_where = '';
if (defined($start_position)) {
$start_position_where = " AND (s.value->>'pos')::int >= $start_position";
}
my $end_position_where = '';
if (defined($end_position)) {
$end_position_where = " AND (s.value->>'pos')::int <= $end_position";
}
my $protocolprop_q = "SELECT nd_protocol_id, s.key $protocolprop_hash_select_sql
FROM nd_protocolprop, jsonb_each(nd_protocolprop.value) as s
WHERE $protocolprop_where_markers_sql $chromosome_where $start_position_where $end_position_where;";
my $protocolprop_h = $schema->storage->dbh()->prepare($protocolprop_q);
$protocolprop_h->execute();
while (my ($protocol_id, $marker_name, @protocolprop_info_return) = $protocolprop_h->fetchrow_array()) {
for my $s (0 .. scalar(@protocolprop_marker_hash_select_arr)-1){
$selected_protocol_marker_info{$protocol_id}->{$marker_name}->{$protocolprop_marker_hash_select->[$s]} = $protocolprop_info_return[$s];
}
$filtered_markers{$marker_name}++;
}
my $protocolprop_top_key_select_sql = scalar(@protocolprop_top_key_select_arr) > 0 ? ', '.join ',', @protocolprop_top_key_select_arr : '';
my $protocolprop_top_key_q = "SELECT nd_protocol_id $protocolprop_top_key_select_sql from nd_protocolprop WHERE $protocolprop_where_sql;";
my $protocolprop_top_key_h = $schema->storage->dbh()->prepare($protocolprop_top_key_q);
$protocolprop_top_key_h->execute();
while (my ($protocol_id, @protocolprop_top_key_return) = $protocolprop_top_key_h->fetchrow_array()) {
for my $s (0 .. scalar(@protocolprop_top_key_select_arr)-1){
my $protocolprop_i = $protocolprop_top_key_select->[$s];
my $val;
if ($protocolprop_i eq 'header_information_lines' || $protocolprop_i eq 'marker_names') {
$val = decode_json $protocolprop_top_key_return[$s];
} else {
$val = $protocolprop_top_key_return[$s];
}
$selected_protocol_top_key_info{$protocol_id}->{$protocolprop_i} = $val;
}
}
if (exists($protocolprop_top_key_select_hash{'markers'})) {
my $protocolprop_top_key_q = "SELECT nd_protocol_id, value from nd_protocolprop WHERE $protocolprop_where_markers_sql;";
my $protocolprop_top_key_h = $schema->storage->dbh()->prepare($protocolprop_top_key_q);
$protocolprop_top_key_h->execute();
while (my ($protocol_id, $markers_value) = $protocolprop_top_key_h->fetchrow_array()) {
$selected_protocol_top_key_info{$protocol_id}->{'markers'} = decode_json $markers_value;
}
}
if (exists($protocolprop_top_key_select_hash{'markers_array'})) {
my $protocolprop_top_key_q = "SELECT nd_protocol_id, value from nd_protocolprop WHERE $protocolprop_where_markers_array_sql;";
my $protocolprop_top_key_h = $schema->storage->dbh()->prepare($protocolprop_top_key_q);
$protocolprop_top_key_h->execute();
while (my ($protocol_id, $markers_value) = $protocolprop_top_key_h->fetchrow_array()) {
$selected_protocol_top_key_info{$protocol_id}->{'markers_array'} = decode_json $markers_value;
}
}
}
my @genotypeprop_hash_select_arr;
foreach (@$genotypeprop_hash_select){
push @genotypeprop_hash_select_arr, "s.value->>'$_'";
}
if (scalar(@genotype_id_array)>0) {
my $genotypeprop_id_sql = join ("," , @genotype_id_array);
my $genotypeprop_hash_select_sql = scalar(@genotypeprop_hash_select_arr) > 0 ? ', '.join ',', @genotypeprop_hash_select_arr : '';
my $filtered_markers_sql = '';
if (scalar(keys %filtered_markers) >0 && scalar(keys %filtered_markers) < 10000) {
$filtered_markers_sql = " AND s.key IN ('". join ("','", keys %filtered_markers) ."')";
}
my $q2 = "SELECT genotypeprop_id
FROM genotypeprop WHERE genotype_id = ? AND type_id=$vcf_snp_genotyping_cvterm_id $genotypeprop_chromosome_rank_string;";
my $h2 = $schema->storage->dbh()->prepare($q2);
my $genotypeprop_q = "SELECT s.key $genotypeprop_hash_select_sql
FROM genotypeprop, jsonb_each(genotypeprop.value) as s
WHERE genotypeprop_id = ? AND s.key != 'CHROM' AND type_id = $vcf_snp_genotyping_cvterm_id $filtered_markers_sql;";
my $genotypeprop_h = $schema->storage->dbh()->prepare($genotypeprop_q);
foreach my $genotype_id (@genotype_id_array){
$h2->execute($genotype_id);
while (my ($genotypeprop_id) = $h2->fetchrow_array()) {
$genotypeprop_h->execute($genotypeprop_id);
while (my ($marker_name, @genotypeprop_info_return) = $genotypeprop_h->fetchrow_array()) {
for my $s (0 .. scalar(@genotypeprop_hash_select_arr)-1){
$genotype_hash{$genotype_id}->{selected_genotype_hash}->{$marker_name}->{$genotypeprop_hash_select->[$s]} = $genotypeprop_info_return[$s];
}
}
}
}
}
foreach (@genotype_id_array) {
my $info = $genotype_hash{$_};
my $selected_marker_info = $selected_protocol_marker_info{$info->{analysisMethodDbId}} ? $selected_protocol_marker_info{$info->{analysisMethodDbId}} : {};
my $selected_protocol_info = $selected_protocol_top_key_info{$info->{analysisMethodDbId}} ? $selected_protocol_top_key_info{$info->{analysisMethodDbId}} : {};
my @all_protocol_marker_names = keys %$selected_marker_info;
$selected_protocol_info->{markers} = $selected_marker_info;
$info->{resultCount} = scalar(keys %{$info->{selected_genotype_hash}});
$info->{all_protocol_marker_names} = \@all_protocol_marker_names;
$info->{selected_protocol_hash} = $selected_protocol_info;
push @data, $info;
}
#print STDERR Dumper \@data;
return ($total_count, \@data);
}
=head2 init_genotype_iterator()
Function for initiating genotype search query and then used to get genotype data iteratively. Iterative search retrieval minimizes memory usage.
Should be used like:
my $genotype_search = CXGN::Genotype::Search->new({
bcs_schema=>$schema,
etc...
});
$genotype_search->init_genotype_iterator();
while (my ($count, $genotype_data) = $genotype_search->get_next_genotype_info) {
#Do something with genotype data
}
If you just want to get a file with the genotype result in a dosage matrix or VCF file, use get_cached_file_dosage_matrix or get_cached_file_VCF functions instead.
If you want results in json format use get_cached_file_search_json
If you want results in json format for only the metadata (no genotype call data), use get_cached_file_search_json()
=cut
sub init_genotype_iterator {
my $self = shift;
my $schema = $self->bcs_schema;
my $trial_list = $self->trial_list;
my $genotype_data_project_list = $self->genotype_data_project_list;
my $protocol_id_list = $self->protocol_id_list;
my $markerprofile_id_list = $self->markerprofile_id_list;
my $accession_list = $self->accession_list;
my $tissue_sample_list = $self->tissue_sample_list;
my $marker_name_list = $self->marker_name_list;
my $chromosome_list = $self->chromosome_list;
my $start_position = $self->start_position;
my $end_position = $self->end_position;
my $genotypeprop_hash_select = $self->genotypeprop_hash_select;
my $protocolprop_top_key_select = $self->protocolprop_top_key_select;
my $protocolprop_marker_hash_select = $self->protocolprop_marker_hash_select;
my $marker_search_hash_list = $self->marker_search_hash_list;
my $marker_score_search_hash_list = $self->marker_score_search_hash_list;
my $return_only_first_genotypeprop_for_stock = $self->return_only_first_genotypeprop_for_stock;
my $limit = $self->limit;
my $offset = $self->offset;
my @data;
my %search_params;
my @where_clause;
my $snp_genotyping_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'snp genotyping', 'genotype_property')->cvterm_id();
$self->_snp_genotyping_cvterm_id($snp_genotyping_cvterm_id);
my $vcf_snp_genotyping_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'vcf_snp_genotyping', 'genotype_property')->cvterm_id();
$self->_vcf_snp_genotyping_cvterm_id($vcf_snp_genotyping_cvterm_id);
my $vcf_map_details_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'vcf_map_details', 'protocol_property')->cvterm_id();
$self->_vcf_map_details_cvterm_id($vcf_map_details_cvterm_id);
my $vcf_map_details_markers_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'vcf_map_details_markers', 'protocol_property')->cvterm_id();
$self->_vcf_map_details_markers_cvterm_id($vcf_map_details_markers_cvterm_id);
my $vcf_map_details_markers_array_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'vcf_map_details_markers_array', 'protocol_property')->cvterm_id();
$self->_vcf_map_details_markers_array_cvterm_id($vcf_map_details_markers_array_cvterm_id);
my $igd_genotypeprop_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'igd number', 'genotype_property')->cvterm_id();
$self->_igd_genotypeprop_cvterm_id($igd_genotypeprop_cvterm_id);
my $accession_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'accession', 'stock_type')->cvterm_id();
$self->_accession_cvterm_id($accession_cvterm_id);
my $tissue_sample_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'tissue_sample', 'stock_type')->cvterm_id();
$self->_tissue_sample_cvterm_id($tissue_sample_cvterm_id);
my $plot_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'plot', 'stock_type')->cvterm_id();
$self->_plot_cvterm_id($plot_cvterm_id);
my $plant_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'plant', 'stock_type')->cvterm_id();
$self->_plant_cvterm_id($plant_cvterm_id);
my $tissue_sample_of_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->bcs_schema, 'tissue_sample_of', 'stock_relationship')->cvterm_id();
$self->_tissue_sample_of_cvterm_id($tissue_sample_of_cvterm_id);
my @trials_accessions;
foreach (@$trial_list){
my $trial = CXGN::Trial->new({bcs_schema=>$schema, trial_id=>$_});
my $accessions = $trial->get_accessions();
foreach (@$accessions){
push @trials_accessions, $_->{stock_id};
}
}
#If accessions are explicitly given, then accessions found from trials will not be added to the search.
if (!$accession_list || scalar(@$accession_list)==0) {
push @$accession_list, @trials_accessions;
}
#For projects inserted into database during the addition of genotypes and genotypeprops
if (scalar(@trials_accessions)==0){
if ($trial_list && scalar(@$trial_list)>0) {
my $trial_sql = join ("," , @$trial_list);
push @where_clause, "project.project_id in ($trial_sql)";
}
}
#For genotyping_data_project
if ($genotype_data_project_list && scalar($genotype_data_project_list)>0) {
my $sql = join ("," , @$genotype_data_project_list);
push @where_clause, "project.project_id in ($sql)";
}
if ($protocol_id_list && scalar(@$protocol_id_list)>0) {
my $protocol_sql = join ("," , @$protocol_id_list);
push @where_clause, "nd_protocol.nd_protocol_id in ($protocol_sql)";
}
if ($accession_list && scalar(@$accession_list)>0) {
my $accession_sql = join ("," , @$accession_list);
push @where_clause, " ( stock.stock_id in ($accession_sql) OR (accession_of_tissue_sample.stock_id in ($accession_sql) AND accession_of_tissue_sample.type_id = $accession_cvterm_id) ) ";
push @where_clause, "stock.type_id in ($accession_cvterm_id, $tissue_sample_cvterm_id)";
}
if ($tissue_sample_list && scalar(@$tissue_sample_list)>0) {
my $stock_sql = join ("," , @$tissue_sample_list);
push @where_clause, "stock.stock_id in ($stock_sql)";
push @where_clause, "stock.type_id = $tissue_sample_cvterm_id";
}
if ($markerprofile_id_list && scalar(@$markerprofile_id_list)>0) {
my $markerprofile_sql = join ("," , @$markerprofile_id_list);
push @where_clause, "genotype.genotype_id in ($markerprofile_sql)";
}
my %filtered_markers;
if ($marker_name_list && scalar(@$marker_name_list)>0) {
my $search_vals_sql = "'".join ("','" , @$marker_name_list)."'";
push @where_clause, "nd_protocolprop.value->'marker_names' \\?& array[$search_vals_sql]";
foreach (@$marker_name_list) {
$filtered_markers{$_}++;
}
}
if ($marker_search_hash_list && scalar(@$marker_search_hash_list)>0) {
foreach (@$marker_search_hash_list){
my $json_val = encode_json $_;
push @where_clause, "nd_protocolprop.value->'markers' \\@> $json_val"."::jsonb";
}
}
if ($marker_score_search_hash_list && scalar(@$marker_score_search_hash_list)>0) {
foreach (@$marker_score_search_hash_list){
my $json_val = encode_json $_;
push @where_clause, "genotype_values.value \\@> $json_val"."::jsonb";
}
}
my $where_clause = scalar(@where_clause)>0 ? " WHERE " . (join (" AND " , @where_clause)) : '';
my $offset_clause = '';
my $limit_clause = '';
if ($limit){
$limit_clause = " LIMIT $limit ";
}
if ($offset){
$offset_clause = " OFFSET $offset ";
}
my $stock_select = '';
if ($return_only_first_genotypeprop_for_stock) {
$stock_select = 'distinct on (stock.stock_id) stock.stock_id';
} else {
$stock_select = 'stock.stock_id';
}
# Setup protocolprop query handles
my @protocolprop_marker_hash_select_arr;
foreach (@$protocolprop_marker_hash_select){
push @protocolprop_marker_hash_select_arr, "s.value->>'$_'";
}
$self->_protocolprop_marker_hash_select_arr(\@protocolprop_marker_hash_select_arr);
my @protocolprop_top_key_select_arr;
foreach (@$protocolprop_top_key_select){
if ($_ ne 'markers' && $_ ne 'markers_array') {
push @protocolprop_top_key_select_arr, "value->>'$_'";
}
}
$self->_protocolprop_top_key_select_arr(\@protocolprop_top_key_select_arr);
my $protocolprop_hash_select_sql = scalar(@protocolprop_marker_hash_select_arr) > 0 ? ', '.join ',', @protocolprop_marker_hash_select_arr : '';
my $chromosome_where = '';
my $genotypeprop_chromosome_rank_string = '';
if ($chromosome_list && scalar(@$chromosome_list)>0) {
my $chromosome_list_sql = '\'' . join('\', \'', @$chromosome_list) . '\'';
$chromosome_where = " AND (s.value->>'chrom')::text IN ($chromosome_list_sql)";
#$genotypeprop_chromosome_rank_string = " AND value->>'CHROM' IN ($chromosome_list_sql) ";
}
my $start_position_where = '';
if (defined($start_position)) {
$start_position_where = " AND (s.value->>'pos')::int >= $start_position";
}
my $end_position_where = '';
if (defined($end_position)) {
$end_position_where = " AND (s.value->>'pos')::int <= $end_position";
}
my $marker_name_list_where = '';
if ($marker_name_list && scalar(@$marker_name_list)>0) {
my $search_vals_sql = '\''.join ('\', \'' , @$marker_name_list).'\'';
$marker_name_list_where = "AND (s.value->>'name')::text IN ($search_vals_sql)";
}
my $protocolprop_q = "SELECT nd_protocol_id, s.key $protocolprop_hash_select_sql
FROM nd_protocolprop, jsonb_each(nd_protocolprop.value) as s
WHERE nd_protocol_id = ? AND type_id = $vcf_map_details_markers_cvterm_id $chromosome_where $start_position_where $end_position_where $marker_name_list_where;";
#print STDERR Dumper $protocolprop_q;
my $protocolprop_h = $schema->storage->dbh()->prepare($protocolprop_q);
$self->_protocolprop_markers_h($protocolprop_h);
my $protocolprop_top_key_select_sql = scalar(@protocolprop_top_key_select_arr) > 0 ? ', '.join ',', @protocolprop_top_key_select_arr : '';
my $protocolprop_top_key_q = "SELECT nd_protocol_id $protocolprop_top_key_select_sql from nd_protocolprop WHERE nd_protocol_id = ? AND type_id = $vcf_map_details_cvterm_id;";
my $protocolprop_top_key_h = $schema->storage->dbh()->prepare($protocolprop_top_key_q);
$self->_protocolprop_top_key_h($protocolprop_top_key_h);
my $protocolprop_top_key_markers_q = "SELECT nd_protocol_id, value from nd_protocolprop WHERE nd_protocol_id = ? AND type_id = $vcf_map_details_markers_cvterm_id;";
my $protocolprop_top_key_markers_h = $schema->storage->dbh()->prepare($protocolprop_top_key_markers_q);
$self->_protocolprop_top_key_markers_h($protocolprop_top_key_markers_h);
my $protocolprop_top_key_markers_array_q = "SELECT nd_protocol_id, value from nd_protocolprop WHERE nd_protocol_id = ? AND type_id = $vcf_map_details_markers_array_cvterm_id;";
my $protocolprop_top_key_markers_array_h = $schema->storage->dbh()->prepare($protocolprop_top_key_markers_array_q);
$self->_protocolprop_top_key_markers_array_h($protocolprop_top_key_markers_array_h);
my $q = "SELECT $stock_select, igd_number_genotypeprop.value, nd_protocol.nd_protocol_id, nd_protocol.name, stock.uniquename, stock.type_id, stock_cvterm.name, genotype.genotype_id, genotype.uniquename, genotype.description, project.project_id, project.name, project.description, accession_of_tissue_sample.stock_id, accession_of_tissue_sample.uniquename, count(genotype.genotype_id) OVER() AS full_count
FROM stock
JOIN cvterm AS stock_cvterm ON(stock.type_id = stock_cvterm.cvterm_id)
LEFT JOIN stock_relationship ON(stock_relationship.subject_id=stock.stock_id AND stock_relationship.type_id = $tissue_sample_of_cvterm_id)
LEFT JOIN stock AS accession_of_tissue_sample ON(stock_relationship.object_id=accession_of_tissue_sample.stock_id)
JOIN nd_experiment_stock ON(stock.stock_id=nd_experiment_stock.stock_id)
JOIN nd_experiment USING(nd_experiment_id)
JOIN nd_experiment_protocol USING(nd_experiment_id)
JOIN nd_experiment_project USING(nd_experiment_id)
JOIN nd_experiment_genotype USING(nd_experiment_id)
JOIN nd_protocol USING(nd_protocol_id)
LEFT JOIN nd_protocolprop ON(nd_protocolprop.nd_protocol_id = nd_protocol.nd_protocol_id AND nd_protocolprop.type_id = $vcf_map_details_cvterm_id)
JOIN genotype USING(genotype_id)
LEFT JOIN genotypeprop AS igd_number_genotypeprop ON(igd_number_genotypeprop.genotype_id = genotype.genotype_id AND igd_number_genotypeprop.type_id = $igd_genotypeprop_cvterm_id)
JOIN project USING(project_id)
$where_clause
ORDER BY stock.stock_id, genotype.genotype_id ASC
$limit_clause
$offset_clause;";
#print STDERR Dumper $q;
my $h = $schema->storage->dbh()->prepare($q);
$h->execute();
my @genotypeprop_infos;
my %seen_protocol_ids;
while (my ($stock_id, $igd_number_json, $protocol_id, $protocol_name, $stock_name, $stock_type_id, $stock_type_name, $genotype_id, $genotype_uniquename, $genotype_description, $project_id, $project_name, $project_description, $accession_id, $accession_uniquename, $full_count) = $h->fetchrow_array()) {
my $germplasmName = '';
my $germplasmDbId = '';
my $igd_number_hash = $igd_number_json ? decode_json $igd_number_json : undef;
my $igd_number = $igd_number_hash ? $igd_number_hash->{'igd number'} : undef;
$igd_number = !$igd_number && $igd_number_hash ? $igd_number_hash->{'igd_number'} : undef;
if ($stock_type_name eq 'accession'){
$germplasmName = $stock_name;
$germplasmDbId = $stock_id;
}
if ($stock_type_name eq 'tissue_sample'){
$germplasmName = $accession_uniquename;
$germplasmDbId = $accession_id;
}
my $stock_object = CXGN::Stock::Accession->new({schema=>$self->bcs_schema, stock_id=>$germplasmDbId});
my %genotypeprop_info = (
markerProfileDbId => $genotype_id,
germplasmDbId => $germplasmDbId,
germplasmName => $germplasmName,
synonyms => $stock_object->synonyms,
stock_id => $stock_id,
stock_name => $stock_name,
stock_type_id => $stock_type_id,
stock_type_name => $stock_type_name,
genotypeDbId => $genotype_id,
genotypeUniquename => $genotype_uniquename,
genotypeDescription => $genotype_description,
analysisMethodDbId => $protocol_id,
analysisMethod => $protocol_name,
genotypingDataProjectDbId => $project_id,
genotypingDataProjectName => $project_name,
genotypingDataProjectDescription => $project_description,
igd_number => $igd_number,
full_count => $full_count
);
$seen_protocol_ids{$protocol_id}++;
push @genotypeprop_infos, \%genotypeprop_info;
}
$self->_genotypeprop_infos(\@genotypeprop_infos);
$self->_genotypeprop_infos_counter(0);
my @seen_protocol_ids = keys %seen_protocol_ids;
my %protocolprop_top_key_select_hash = map {$_ => 1} @protocolprop_top_key_select_arr;
my %selected_protocol_marker_info;
my %selected_protocol_top_key_info;
my $val;
foreach my $protocol_id (@seen_protocol_ids){
$protocolprop_h->execute($protocol_id);
while (my ($protocol_id, $marker_name, @protocolprop_info_return) = $protocolprop_h->fetchrow_array()) {
for my $s (0 .. scalar(@protocolprop_marker_hash_select_arr)-1){
$selected_protocol_marker_info{$protocol_id}->{$marker_name}->{$protocolprop_marker_hash_select->[$s]} = $protocolprop_info_return[$s];
}
$filtered_markers{$marker_name}++;
}
$protocolprop_top_key_h->execute($protocol_id);
while (my ($protocol_id, @protocolprop_top_key_return) = $protocolprop_top_key_h->fetchrow_array()) {
for my $s (0 .. scalar(@protocolprop_top_key_select_arr)-1){
my $protocolprop_i = $protocolprop_top_key_select->[$s];
if ($protocolprop_i eq 'header_information_lines' || $protocolprop_i eq 'marker_names') {
$val = decode_json $protocolprop_top_key_return[$s];
} else {
$val = $protocolprop_top_key_return[$s];
}
$selected_protocol_top_key_info{$protocol_id}->{$protocolprop_i} = $val;
}
}
if (exists($protocolprop_top_key_select_hash{'markers'})) {