-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqp_utils.pm
2282 lines (2039 loc) · 76.7 KB
/
sqp_utils.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
#!/usr/bin/perl
#
# sqp_utils.pm
# Eric Nawrocki
# EPN, Tue Mar 19 13:35:06 2019 [incept, in vadr]
# EPN, Tue Jul 2 11:53:49 2019 [migrated from vadr's epn-utils.pm (as of commit 69b003d)]]
# version: 0.10
#
use strict;
use warnings;
use Time::HiRes qw(gettimeofday);
# NOTE: do not add any 'require' statements here, e.g. 'require
# sqp_utils.pm' because the program that uses sequip must handle that
# so each program can specify sequip from a specific directory defined
# by a specific environment variable. This is how, for example,
# ribovore can require a specific version of sequip on the same file
# system that has vadr installed with a potentially different version
# of sequip.
#################################################################
# Subroutine: utl_RunCommand()
# Incept: EPN, Thu Feb 11 13:32:34 2016 [dnaorg.pm]
#
# Purpose: Runs a command using system() and exits in error
# if the command fails. If $be_verbose, outputs
# the command to stdout. If $FH_HR->{"cmd"} is
# defined, outputs command to that file handle.
#
# Arguments:
# $cmd: command to run, with a "system" command;
# $be_verbose: '1' to output command to stdout before we run it, '0' not to
# $do_failok: '1' to NOT exit if command fails, '0' to exit if command fails
# $FH_HR: REF to hash of file handles, including "cmd"
#
# Returns: amount of time the command took, in seconds
#
# Dies: if $cmd fails and $do_failok is '0'
#################################################################
sub utl_RunCommand {
my $sub_name = "utl_RunCommand()";
my $nargs_expected = 4;
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($cmd, $be_verbose, $do_failok, $FH_HR) = (@_);
my $cmd_FH = undef;
if(defined $FH_HR && defined $FH_HR->{"cmd"}) {
$cmd_FH = $FH_HR->{"cmd"};
}
if($be_verbose) {
print ("Running cmd: $cmd\n");
}
if(defined $cmd_FH) {
print $cmd_FH ("$cmd\n");
}
my ($seconds, $microseconds) = gettimeofday();
my $start_time = ($seconds + ($microseconds / 1000000.));
system($cmd);
($seconds, $microseconds) = gettimeofday();
my $stop_time = ($seconds + ($microseconds / 1000000.));
if(($? != 0) && (! $do_failok)) {
ofile_FAIL("ERROR in $sub_name, the following command failed:\n$cmd\n", $?, $FH_HR);
}
return ($stop_time - $start_time);
}
#################################################################
# Subroutine: utl_ArrayOfHashesToArray()
# Incept: EPN, Wed Mar 20 09:07:06 2019
#
# Purpose: Fill @{$AR} with all values in $AHR->[]{$key}.
# Arguments:
# $AHR: REF to array of hashes
# $AR: REF to array to add to
# $key: key of interest
#
# Returns: number of elements added to @{$AR}
#
#################################################################
sub utl_ArrayOfHashesToArray {
my $sub_name = "utl_ArrayOfHashesToArray()";
my $nargs_expected = 3;
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($AHR, $AR, $key) = (@_);
my $ret_n = 0;
my $n = scalar(@{$AHR});
for(my $i = 0; $i < $n; $i++) {
if(defined $AHR->[$i]{$key}) {
push(@{$AR}, $AHR->[$i]{$key});
$ret_n++;
}
}
return $ret_n;
}
#################################################################
# Subroutine: utl_ConcatenateListOfFiles()
# Incept: EPN, Sun Apr 24 08:08:15 2016
#
# Purpose: Concatenate a list of files into one file. If the list is
# more than 20K characters, break it down into multiple
# cat calls of at most 20K characters each, and call this
# subroutine recursively to concanenate them.
#
# To allow recursive calls without clobbering files
# created in previous calls, we use a unique string to
# append to the output file names. This is derived from
# the $caller_sub_name array. If that is undef, we use
# 'rec0' as the unique string, if it ends with 'rec<d>'
# we know we have a recursive call and use 'rec<d+1>' in
# the recursive call.
#
# We remove all files that we concatenate unless
# --keep option is on in %{$opt_HHR}.
#
# Arguments:
# $file_AR: REF to array of all files to concatenate
# $outfile: name of output file to create by concatenating
# all files in @{$file_AR}.
# $caller_sub_name: name of calling subroutine (can be undef)
# $opt_HHR: REF to 2D hash of option values, see top of epn-options.pm for description
# $FH_HR: ref to hash of file handles
#
# Returns: Nothing.
#
# Dies: If one of the cat commands fails.
# If $outfile is in @{$file_AR}
#
#################################################################
sub utl_ConcatenateListOfFiles {
my $nargs_expected = 5;
my $sub_name = "utl_ConcatenateListOfFiles()";
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($file_AR, $outfile, $caller_sub_name, $opt_HHR, $FH_HR) = (@_);
if((! defined $file_AR) || (scalar(@{$file_AR}) == 0)) {
ofile_FAIL(sprintf("ERROR in $sub_name%s, array of file names to concatenate is undefined or empty",
(defined $caller_sub_name) ? " called by $caller_sub_name" : ""), 1, $FH_HR);
}
if(utl_AFindNonNumericValue($file_AR, $outfile, $FH_HR) != -1) {
ofile_FAIL(sprintf("ERROR in $sub_name%s, output file name $outfile exists in list of files to concatenate",
(defined $caller_sub_name) ? " called by $caller_sub_name" : ""), 1, $FH_HR);
}
# determine unique string for tmp naming files that allows
# proper handling of recursive calls without namespace clashes
my $rec_idx = 0;
if((defined $caller_sub_name) && ($caller_sub_name =~ /rec(\d+)$/)) {
# caller was this subroutine, using $1 as the $rec_idx
$rec_idx = $1 + 1;
# e.g. if $caller_sub_name = 'utl_ConcatenateListOfFiles.rec2', then $rec_key set to 3
}
my $nchar_limit = 20000; # 20K characters, hard-coded
my $tmp_outfile = undef; # name of temporary outfile, only used if needed
my $tot_nfiles = scalar(@{$file_AR}); # total number of files in @file_A
my $cur_nfiles = 0; # current number of files in cat call
my $i = 0; # index of file in @{$file_AR}
my $outfile_len = length($outfile); # length of output file name
my $cat_cmd = "cat ";
my $tmp_idx = 1;
my @tmp_outfile_A = (); # array of temporary files we created to avoid too long of a command
for($i = 0; $i < $tot_nfiles; $i++) {
$cat_cmd .= $file_AR->[$i] . " ";
$cur_nfiles++;
if(($cur_nfiles >= 2) && # we're cat'ing at least 2 files
(length($cat_cmd) + 2 + $outfile_len + 4 + length ($rec_idx) + 1 + length($tmp_idx)) > $nchar_limit) { # length of command exceeds $nchar_limit (2 = length("> "), 4 = length(".rec")), 1 = length(".")
# finish the cat command and execute it to create a tmp file we'll cat in a recursive call
$tmp_outfile = $outfile . ".rec" . $rec_idx . "." . $tmp_idx;
$cat_cmd .= "> $tmp_outfile";
# execute the command
utl_RunCommand($cat_cmd, opt_Get("-v", $opt_HHR), 0, $FH_HR);
push(@tmp_outfile_A, $tmp_outfile);
$tmp_idx++;
$cat_cmd = "cat ";
$cur_nfiles = 0;
}
}
if(scalar(@tmp_outfile_A) == 0) {
# we did not create any temporary files
# finish the command and execute it
$cat_cmd .= "> $outfile";
utl_RunCommand($cat_cmd, opt_Get("-v", $opt_HHR), 0, $FH_HR);
}
else {
# we did create at least one temporary file, we need to call this subroutine recursively
# first finish the final tmp file if nec
if($cur_nfiles > 0) {
$tmp_outfile = $outfile . ".rec" . $rec_idx . "." . $tmp_idx;
$cat_cmd .= "> $tmp_outfile";
# execute the command
utl_RunCommand($cat_cmd, opt_Get("-v", $opt_HHR), 0, $FH_HR);
push(@tmp_outfile_A, $tmp_outfile);
}
my $tmp_caller_str = $sub_name . ".rec" . $rec_idx;
utl_ConcatenateListOfFiles(\@tmp_outfile_A, $outfile, (defined $caller_sub_name) ? $caller_sub_name . ":" . $tmp_caller_str : $tmp_caller_str, $opt_HHR, $FH_HR);
}
# remove all original files (recursive call(s) will remove any tmp files we created)
if(! opt_Get("--keep", $opt_HHR)) {
utl_FileRemoveList($file_AR, $sub_name, $opt_HHR, $FH_HR);
}
return;
}
#################################################################
# Subroutine: utl_AFindNonNumericValue()
# Incept: EPN, Tue Feb 16 10:40:57 2016 [dnaorg.pm]
#
# Purpose: Returns (first) index in @{$AR} that has the
# nonnumeric value $value. Returns -1
# if it does not exist.
#
# Arguments:
# $AR: REF to array
# $value: the value we're checking exists in @{$AR}
# $FH_HR: REF to hash of file handles, including "log" and "cmd"
#
# Returns: index ($i) '1' if $value exists in @{$AR}, '-1' if not
#
# Dies: if $value is numeric, or @{$AR} is not defined.
#################################################################
sub utl_AFindNonNumericValue {
my $nargs_expected = 3;
my $sub_name = "utl_AFindNonNumericValue()";
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($AR, $value, $FH_HR) = (@_);
if(utl_IsReal($value)) {
ofile_FAIL("ERROR in $sub_name, value $value seems to be numeric, we can't compare it for equality", 1, $FH_HR);
}
if(! defined $AR) {
ofile_FAIL("ERROR in $sub_name, array reference is not defined", 1, $FH_HR);
}
for(my $i = 0; $i < scalar(@{$AR}); $i++) {
if($AR->[$i] eq $value) {
return $i;
}
}
return -1; # did not find it
}
#################################################################
# Subroutine: utl_AFindValue()
# Incept: EPN, Tue Mar 8 11:26:03 2016
# Synopsis: Look for a value in an array and return the index
# of it, if found, else return -1. If it exists more than
# once, return the minimum index.
#
# Arguments:
# $value: value to look for
# $AR: array to look in
#
# Returns: index ($i) '1' if $value exists in @{$AR}, '-1' if not
#
# Dies: if $value is numeric, or @{$AR} is not defined.
#
#################################################################
sub utl_AFindValue {
my $sub_name = "utl_AFindValue()";
my $nargs_expected = 3;
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($value, $AR, $FH_HR) = @_;
if(! defined $AR) {
ofile_FAIL("ERROR in $sub_name, array reference is not defined", 1, $FH_HR);
}
if(utl_IsReal($value)) { # compare with ==
for(my $i = 0; $i < scalar(@{$AR}); $i++) {
my $el = $AR->[$i];
if(utl_IsReal($el) && ($value == $el)) {
return $i;
}
}
}
else { # compare with 'eq'
for(my $i = 0; $i < scalar(@{$AR}); $i++) {
my $el = $AR->[$i];
if((! utl_IsReal($el)) && ($value eq $el)) {
return $i;
}
}
}
return -1;
}
#################################################################
# Subroutine: utl_ACountNonNumericValue()
# Incept: EPN, Fri Mar 11 06:34:51 2016
#
# Purpose: Returns number of times nonnumeric value
# $value exists in @{$AR}. Returns 0 if
# it doesn't exist.
#
# Arguments:
# $AR: REF to array
# $value: the value we're looking for in @{$AR}
# $FH_HR: REF to hash of file handles, including "log" and "cmd"
#
# Returns: Number of occurrences of $value in @{$AR}.
#
# Dies: if $value is numeric, or @{$AR} is not defined.
#
#################################################################
sub utl_ACountNonNumericValue {
my $nargs_expected = 3;
my $sub_name = "utl_ACountNonNumericValue()";
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($AR, $value, $FH_HR) = @_;
if(utl_IsReal($value)) {
ofile_FAIL("ERROR in $sub_name, value $value seems to be numeric, we can't compare it for equality", 1, $FH_HR);
}
if(! defined $AR) {
ofile_FAIL("ERROR in $sub_name, array reference is not defined", 1, $FH_HR);
}
my $ct = 0;
for(my $i = 0; $i < scalar(@{$AR}); $i++) {
if($AR->[$i] eq $value) {
$ct++;
}
}
return $ct;
}
#################################################################
# Subroutine: utl_FileRemoveUsingSystemRm
# Incept: EPN, Fri Mar 4 15:57:25 2016 [dnaorg.pm]
#
# Purpose: Remove a file from the filesystem by using
# the system rm command.
# Arguments:
# $file: file to remove
# $caller_sub_name: name of caller, can be undef
# $opt_HHR: REF to 2D hash of option values, see top of epn-options.pm for description
# $FH_HR: REF to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: - if the file does not exist
#
#################################################################
sub utl_FileRemoveUsingSystemRm {
my $sub_name = "utl_FileRemoveUsingSystemRm";
my $nargs_expected = 4;
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($file, $caller_sub_name, $opt_HHR, $FH_HR) = (@_);
if(! -e $file) {
ofile_FAIL(sprintf("ERROR in $sub_name, %s trying to remove file $file but it does not exist",
(defined $caller_sub_name) ? "called by $caller_sub_name," : 0), 1, $FH_HR);
}
utl_RunCommand("rm $file", opt_Get("-v", $opt_HHR), 0, $FH_HR);
return;
}
#################################################################
# Subroutine: utl_RemoveDirPath()
# Incept: EPN, Mon Nov 9 14:30:59 2009 [ssu-align]
#
# Purpose: Given a full path of a file remove the directory path.
# For example: "foodir/foodir2/foo.stk" becomes "foo.stk".
#
# Arguments:
# $fullpath: name of original file
#
# Returns: The string $fullpath with dir path removed.
#
#################################################################
sub utl_RemoveDirPath {
my $sub_name = "utl_RemoveDirPath()";
my $nargs_expected = 1;
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($fullpath) = (@_);
$fullpath =~ s/^.+\///;
return $fullpath;
}
#################################################################
# Subroutine: utl_HHFromAH()
# Incept: EPN, Fri Mar 22 09:43:34 2019
#
# Purpose: Create a 2D hash %{$HHR} from an array of hashes
# @{$AHR}.
# First dim keys in %{$HHR} will be values from
# $AHR->[]{$AH_key_for_HH_key}.
# Second dim keys in %{$HHR} will be all other keys from
# $AHR->[]{}.
#
# Arguments:
# $HHR: ref to 2D hash to create
# $AHR: ref to array of hashes to copy from
# $AH_key_for_HH_key: key in @{$AHR} to get to use value from to use
# as key in 1st dim of %{$HHR}
# $call_str: string describing caller, to output if we die
# $FH_HR: ref to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: If not all elements of @{$AHR} have
# $AHR->[]{$AH_key_for_HH_key} defined.
# If more than one elements of @{$AHR} have same
# value for @{$AHR->[]{$key}}.
#
#################################################################
sub utl_HHFromAH {
my $sub_name = "utl_HHFromAH()";
my $nargs_expected = 4;
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($HHR, $AHR, $AH_key_for_HH_key, $call_str, $FH_HR) = (@_);
%{$HHR} = ();
my $n = scalar(@{$AHR});
my $AH_value_for_HH_key = undef;
for(my $i = 0; $i < $n; $i++) {
if(! defined $AHR->[$i]{$AH_key_for_HH_key}) {
ofile_FAIL("ERROR in $sub_name,%selement $i does not have key $AH_key_for_HH_key",
(defined $call_str) ? "$call_str" : "", 1, $FH_HR);
}
$AH_value_for_HH_key = $AHR->[$i]{$AH_key_for_HH_key};
if(defined $HHR->{$AH_value_for_HH_key}) {
ofile_FAIL("ERROR in $sub_name,%stwo elements have same value for $AH_key_for_HH_key key ($AH_value_for_HH_key)",
(defined $call_str) ? "$call_str" : "", 1, $FH_HR);
}
%{$HHR->{$AH_value_for_HH_key}} = ();
foreach my $AH_key2_for_HH_key (keys (%{$AHR->[$i]})) {
if($AH_key2_for_HH_key ne $AH_key_for_HH_key) {
$HHR->{$AH_value_for_HH_key}{$AH_key2_for_HH_key} = $AHR->[$i]{$AH_key2_for_HH_key};
}
}
}
return;
}
#################################################################
# Subroutine: utl_HHFromAHAddIdx()
# Incept: EPN, Thu Mar 21 06:35:28 2019
#
# Purpose: Create a 2D hash %{$HHR} from an array of hashes
# @{$AHR} by calling utL_HHFromAH()
# (see that sub's Purpose for more details)
#
# And then add $HHR->{$key}{"idx"}, that gives index <i> of
# @{$AHR} for which $AHR->[<i>]{$AH_key_for_HH_key} == $key.
#
# Arguments:
# $HHR: ref to 2D hash to create
# $AHR: ref to array of hashes to copy from
# $AH_key_for_HH_key: key in @{$AHR} to get to use value from to use
# as key in 1st dim of %{$HHR}
# $call_str: string describing caller, to output if we die
# $FH_HR: ref to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: If not all elements of @{$AHR} have
# $AHR->[]{$AH_key_for_HH_key} defined.
# If more than one elements of @{$AHR} have same
# value for @{$AHR->[]{$key}}.
# If $AHR->[]{"idx"} is defined for any element.
# If $AH_key_for_HH_key is "idx";
#
#################################################################
sub utl_HHFromAHAddIdx {
my $sub_name = "utl_HHFromAHAddIdx()";
my $nargs_expected = 4;
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($HHR, $AHR, $AH_key_for_HH_key, $call_str, $FH_HR) = (@_);
# make sure "idx" is not the $AH_key_for_HH_key
if($AH_key_for_HH_key eq "idx") {
ofile_FAIL("ERROR in $sub_name,%skey to choose is \"idx\"",
(defined $call_str) ? "$call_str" : "", 1, $FH_HR);
}
# make sure "idx" 2D key does not exist for any element
my $n = scalar(@{$AHR});
for(my $i = 0; $i < $n; $i++) {
if(defined $AHR->[$i]{"idx"}) {
ofile_FAIL("ERROR in $sub_name,%selement $i already has key \"idx\" upon entry",
(defined $call_str) ? "$call_str" : "", 1, $FH_HR);
}
}
# do most of the work with utl_HHFromAH
utl_HHFromAH($HHR, $AHR, $AH_key_for_HH_key, $call_str, $FH_HR);
# add idx
my $AH_value_for_HH_key = undef;
for(my $i = 0; $i < $n; $i++) {
$AH_value_for_HH_key = $AHR->[$i]{$AH_key_for_HH_key};
$HHR->{$AH_value_for_HH_key}{"idx"} = $i;
}
return;
}
#################################################################
# Subroutine: utl_HFromAH()
# Incept: EPN, Fri Mar 22 06:20:18 2019
#
# Purpose: Create a 1D hash %{$HR} using key/value pairs
# from @{$AHR}.
# Keys in %{$HR} will be values from
# $AHR->[]{$AH_key_for_H_key}.
# Values in %{$HR} will be values from
# $AHR->[]{$AH_key_for_H_value}.
#
# Arguments:
# $HR: ref to 1D hash to create
# $AHR: ref to array of hashes to copy from
# $AH_key_for_H_key: key in @{$AHR} to get value from to use as key in %{$HR}
# $AH_key_for_H_value: key in @{$AHR} to get value from to use as value in %{$HR}
# $call_str: string describing caller, to output if we die
# $FH_HR: ref to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: If not all elements of @{$AHR} have
# $AHR->[]{$AH_key_for_H_key}}
# If not all elements of @{$AHR} have
# $AHR->[]{$AH_key_for_H_value}}
# If more than one elements of @{$AHR} have same
# value for @{$AHR->[]{$AH_key_for_H_key}}.
#
#################################################################
sub utl_HFromAH {
my $sub_name = "utl_HFromAH()";
my $nargs_expected = 6;
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($HR, $AHR, $AH_key_for_H_key, $AH_key_for_H_value, $call_str, $FH_HR) = (@_);
printf("in $sub_name, call_str: $call_str\n");
%{$HR} = ();
my $n = scalar(@{$AHR});
my $AH_value_for_H_key = undef;
for(my $i = 0; $i < $n; $i++) {
if(! defined $AHR->[$i]{$AH_key_for_H_key}) {
ofile_FAIL("ERROR in $sub_name,%selement $i does not have key $AH_key_for_H_key",
(defined $call_str) ? "$call_str" : "", 1, $FH_HR);
}
if(! defined $AHR->[$i]{$AH_key_for_H_value}) {
ofile_FAIL("ERROR in $sub_name,%selement $i does not have key $AH_key_for_H_value",
(defined $call_str) ? "$call_str" : "", 1, $FH_HR);
}
my $H_key = $AHR->[$i]{$AH_key_for_H_key};
my $H_value = $AHR->[$i]{$AH_key_for_H_value};
if(defined $HR->{$H_key}) {
ofile_FAIL("ERROR in $sub_name,%stwo elements have same value for key $AH_key_for_H_key ($H_key)",
(defined $call_str) ? "$call_str" : "", 1, $FH_HR);
}
$HR->{$H_key} = $H_value;
}
return;
}
#################################################################
# Subroutine: utl_IdxHFromA()
# Incept: EPN, Fri Mar 22 10:01:38 2019
#
# Purpose: Create a 1D 'index' hash %{$idx_HR} such that
# $idx_HR->{$key} == $i if
# $AR->[$i] == $key
# from @{$AHR}.
#
# Arguments:
# $idx_HR: ref to 1D hash to create
# $AR: ref to array
# $call_str: string describing caller, to output if we die
# $FH_HR: ref to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: Two elements of @{$AR} are identical:
# (if AR->[$i] == AR->[$j] and $i != $j for any $i, $j)
#
#################################################################
sub utl_IdxHFromA {
my $sub_name = "utl_IdxHFromA()";
my $nargs_expected = 4;
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($HR, $AR, $call_str, $FH_HR) = (@_);
%{$HR} = ();
my $n = scalar(@{$AR});
for(my $i = 0; $i < $n; $i++) {
my $key = $AR->[$i];
if(defined $HR->{$key}) {
# should I check here that $key is not a number?
ofile_FAIL("ERROR in $sub_name,%stwo elements in array have same value ($key)",
(defined $call_str) ? "$call_str" : "", 1, $FH_HR);
}
$HR->{$key} = $i;
}
return;
}
#################################################################
# Subroutine: utl_IdxHFromAH()
# Incept: EPN, Fri Mar 22 10:01:38 2019
#
# Purpose: Create a 1D 'index' hash %{$idx_HR} from @{$AHR}
# such that
# $idx_HR->{$key} == $i if
# $AHR->[$i]{$AH_key} == $key
#
# Arguments:
# $idx_HR: ref to 1D hash to create
# $AHR: ref to hash of arrays
# $AH_key: ref to key in %{$AHR->[$i]}
# $call_str: string describing caller, to output if we die
# $FH_HR: ref to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: Two values we are trying to add as keys to %{$idx_HR} are identical
# (if AHR->[$i]{$AH_key} == AHR->[$j]{$AH_key} and $i != $j for any $i, $j)
# If AHR->[$i]{$AH_key} is undefined for any $i
#################################################################
sub utl_IdxHFromAH {
my $sub_name = "utl_IdxHFromAH()";
my $nargs_expected = 5;
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($HR, $AHR, $AH_key, $call_str, $FH_HR) = (@_);
%{$HR} = ();
my $n = scalar(@{$AHR});
for(my $i = 0; $i < $n; $i++) {
if(! defined $AHR->[$i]{$AH_key}) {
ofile_FAIL("ERROR in $sub_name,%shash that is array element $i does not have key $AH_key",
(defined $call_str) ? "$call_str" : "", 1, $FH_HR);
}
my $H_key = $AHR->[$i]{$AH_key};
if(defined $HR->{$H_key}) {
ofile_FAIL("ERROR in $sub_name,%stwo elements of source array of hashes we are trying to use as keys in destination hash have same value ($H_key)",
(defined $call_str) ? "$call_str" : "", 1, $FH_HR);
}
$HR->{$H_key} = $i;
}
return;
}
#################################################################
# Subroutine: utl_AHCountKeyValue()
# Incept: EPN, Tue Mar 19 11:37:32 2019
#
# Synopsis: Return the number of elements in @{AHR} that
# have a key $key in %{$AHR->[]} with value $value.
#
# Arguments:
# $AHR: ref to array of hashes
# $key: hash key
# $value: hash value
#
# Returns: Number of array elements for which $AHR->[]{$key} eq $value
#
#################################################################
sub utl_AHCountKeyValue {
my $sub_name = "utl_AHCountKeyValue";
my $nargs_expected = 3;
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($AHR, $key, $value) = (@_);
my $ret_n = 0;
for(my $i = 0; $i < scalar(@{$AHR}); $i++) {
# printf("in $sub_name: AHR->[$i]{$key} $AHR->[$i]{$key}\n");
if((defined $AHR->[$i]{$key}) &&
($AHR->[$i]{$key} eq $value)) {
$ret_n++;
}
}
# printf("in $sub_name: returning $ret_n\n");
return $ret_n;
}
#################################################################
# Subroutine: utl_AHValidate()
# Incept: EPN, Wed Mar 13 13:24:38 2019
#
# Purpose: Validate an array of hashes, by making sure it
# includes a key/value for all keys in @{$keys_AR}.
# Arguments:
# $AHR: REF to array of hashes to validate
# $keys_AR: REF to array of keys that may be excluded from the hash
# $fail_str: extra string to output if we die
# $FH_HR: REF to hash of file handles, including "log" and "cmd"
#
# Returns: scalar(@{$AHR});
#
# Dies: - if one of the keys in @{$keys_AR} does not exist in all hashes
# of the array
#
#################################################################
sub utl_AHValidate {
my $sub_name = "utl_AHValidate()";
my $nargs_expected = 4;
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($AHR, $keys_AR, $fail_str, $FH_HR) = (@_);
my $n = scalar(@{$AHR});
for(my $i = 0; $i < $n; $i++) {
utl_HValidate($AHR->[$i], $keys_AR, $fail_str, $FH_HR);
}
return $n;
}
#################################################################
# Subroutine: utl_HValidate()
# Incept: EPN, Fri Mar 15 09:37:19 2019
#
# Purpose: Validate a hash, by making sure a defined value
# exists for each key in @{$keys_AR}.
# Arguments:
# $HR: REF to the hash
# $keys_AR: REF to array of keys that may be excluded from the hash
# $fail_str: extra string to output if we die
# $FH_HR: REF to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: - if one of the keys in @{$keys_AR} does not exist in the hash
# of the array
#
#################################################################
sub utl_HValidate {
my $sub_name = "utl_HValidate()";
my $nargs_expected = 4;
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($HR, $keys_AR, $fail_str, $FH_HR) = (@_);
foreach my $key (@{$keys_AR}) {
if(! exists $HR->{$key}) {
ofile_FAIL(sprintf("ERROR in $sub_name, required hash key $key does not exist\n%s", (defined $fail_str) ? $fail_str : ""), 1, $FH_HR);
}
if(! defined $HR->{$key}) {
ofile_FAIL(sprintf("ERROR in $sub_name, required hash key $key exists but its value is undefined\n%s", (defined $fail_str) ? $fail_str : ""), 1, $FH_HR);
}
}
return;
}
#################################################################
# Subroutine: utl_HMaxLengthKey()
# Incept: EPN, Thu Dec 13 15:52:09 2018
#
# Purpose: Return the maximum length of a scalar key
# in a hash.
#
# Arguments:
# $HR: reference to the hash
#
# Returns: The length of the maximum length scalar key.
#
#################################################################
sub utl_HMaxLengthKey {
my $nargs_expected = 1;
my $sub_name = "utl_HMaxLengthKey()";
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($HR) = $_[0];
my $max = 0;
my $len = 0;
foreach my $key (keys (%{$HR})) {
$len = length($key);
if($len > $max) { $max = $len; }
}
return $max;
}
#################################################################
# Subroutine: utl_HMaxLengthValue()
# Incept: EPN, Mon Nov 3 09:09:59 2014 [rnavore]
#
# Purpose: Return the maximum length of a scalar value
# in a hash.
#
# Arguments:
# $HR: reference to the hash
#
# Returns: The length of the maximum length scalar.
#
#################################################################
sub utl_HMaxLengthValue {
my $nargs_expected = 1;
my $sub_name = "utl_HMaxLengthValue()";
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($HR) = $_[0];
my $max = 0;
my $len = 0;
foreach my $key (keys (%{$HR})) {
$len = length($HR->{$key});
if($len > $max) { $max = $len; }
}
return $max;
}
#################################################################
# Subroutine: utl_HHMaxLengthValueGiven2DKey()
# Incept: EPN, Fri Mar 29 12:01:18 2019
#
# Purpose: Return the maximum length of a scalar value
# in a 2D hash for a given 1D key hash.
# max(length($HHR->{}{$key}))
#
# Arguments:
# $HHR: reference to the hash of hashes
# $key2: 2D key
#
# Returns: The length of the maximum length scalar.
#
#################################################################
sub utl_HHMaxLengthValueGiven2DKey {
my $nargs_expected = 2;
my $sub_name = "utl_HHMaxLengthValueGiven2DKey()";
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($HHR, $key2) = (@_);
my $max = 0;
foreach my $key1 (keys (%{$HHR})) {
if(defined $HHR->{$key1}{$key2}) {
$max = utl_Max($max, length($HHR->{$key1}{$key2}));
}
}
return $max;
}
#################################################################
# Subroutine: utl_AMaxLengthValue()
# Incept: EPN, Thu Mar 17 12:38:53 2016
#
# Purpose: Return the maximum length of a scalar value
# in an array.
#
# Arguments:
# $AR: reference to the array
#
# Returns: The length of the maximum length scalar.
#
#################################################################
sub utl_AMaxLengthValue {
my $nargs_expected = 1;
my $sub_name = "utl_AMaxLengthValue()";
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($AR) = $_[0];
my $max = 0;
my $len = 0;
foreach my $el (@{$AR}) {
$len = length($el);
if($len > $max) { $max = $len; }
}
return $max;
}
#################################################################
# Subroutine: utl_AArgMax()
# Incept: EPN, Fri Jan 24 15:09:46 2020
#
# Purpose: Return the index of the maximum value numeric
# element in an array.
#
# Arguments:
# $AR: reference to the array
#
# Returns: The index of the maximum value.
#
#################################################################
sub utl_AArgMax {
my $nargs_expected = 1;
my $sub_name = "utl_AArgMax()";
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($AR) = $_[0];
my $argmax = undef;
my $max = undef;
my $nel = scalar(@{$AR});
if($nel >= 1) {
$argmax = 0;
$max = $AR->[0];
}
for(my $i = 1; $i < $nel; $i++) {
if($AR->[$i] > $max) {
$argmax = $i;
$max = $AR->[$i];
}
}
return $argmax;
}
#################################################################
# Subroutine: utl_NumberOfDigits()
# Incept: EPN, Tue May 9 11:33:50 2017 [ribovore]
# EPN, Fri Nov 13 06:17:25 2009 [ssu-align:ssu.pm:NumberOfDigits()]
#
# Purpose: Return the number of digits in a number before
# the decimal point. (ex: 1234.56 would return 4).
# Arguments:
# $num: the number
#
# Returns: the number of digits before the decimal point
#
#################################################################
sub utl_NumberOfDigits {
my $nargs_expected = 1;
my $sub_name = "utl_NumberOfDigits()";
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($num) = (@_);
my $ndig = 1;
while($num >= 10) { $ndig++; $num /= 10.; }
return $ndig;
}
#################################################################
# Subroutine: utl_Max()
# Incept: EPN, Tue Mar 26 11:52:54 2019
#
# Purpose: Returns the maximum of $x and $y.
# Arguments:
# $x: first number
# $y: second number
#
# Returns: maximum of $x and $y
#
#################################################################
sub utl_Max {
my $nargs_expected = 2;
my $sub_name = "utl_Max()";
if(scalar(@_) != $nargs_expected) { printf STDERR ("ERROR, $sub_name entered with %d != %d input arguments.\n", scalar(@_), $nargs_expected); exit(1); }
my ($x, $y) = (@_);
return ($x > $y) ? $x : $y;
}
#################################################################
# Subroutine: utl_Min()
# Incept: EPN, Tue Mar 26 11:54:09 2019
#
# Purpose: Returns the minimum of $x and $y.
# Arguments:
# $x: first number
# $y: second number
#
# Returns: minimum of $x and $y