-
Notifications
You must be signed in to change notification settings - Fork 23
/
vadr.pm
8047 lines (7276 loc) · 342 KB
/
vadr.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/env perl
#
# version: 1.6.4 [Jun 2024]
#
# vadr.pm
# Eric Nawrocki
# EPN, Mon Feb 1 15:23:00 2016
#
# Some subroutines are taken from rnavore.pm [EPN, Tue Sep 23 09:22:55 2014]
# and ssu.pm [EPN, Thu Nov 5 05:39:37 2009].
#
# Perl module used by vadr-build.pl and vadr-annotate.pl scripts
# which contains subroutines called by both of those scripts.
#
#########################
# Naming conventions used in this file:
#
# - Hash data structures use the abbreviation 'H', typically at the end of
# a variable name (e.g. %length_H)
#
# - Array data structures use the abbreviation 'A', typically at the end of
# a variable name (e.g. @accession_A)
#
# - Multidimensional arrays/hashes use concatenated 'H's and 'A's, e.g.:
# @size_AA is a two dimensional array, and %info_HHA is a two dimensional
# hash of arrays
#
# - File handles typically start or end with 'FH', e.g. $log_FH, or a hash
# of file handles that is used commonly is %FH_HR.
#
#########################
# Common data structures used in this file:
#
# - $ftr_info_AHR: reference to an array of hashes with feature information for a single model.
#
# - $sgm_info_AHR: reference to an array of hashes with model-segment information for a single model.
#
# - $alt_info_HHR: reference to a hash of hashes with alert information.
#
########################################################################################
use strict;
use warnings;
use Cwd;
use LWP::Simple;
# use LWP::Protocol::https;
# use Mozilla::CA;
# above line is purposefully commented out, b/c LWP::Protocol::https and Mozilla::CA are only
# needed for v-build.pl, and many users only use v-annotate.pl only, which can run without them,
# when a better solution for checking for modules during installation is found, uncomment these lines
require "sqp_opts.pm";
require "sqp_ofile.pm";
require "sqp_seq.pm";
require "sqp_seqfile.pm";
require "sqp_utils.pm";
#########################################################################################
#
# List of subroutines in this file, divided into categories.
#
# Subroutines related to features or segments:
# vdr_FeatureInfoImputeCoords()
# vdr_FeatureInfoImputeLength()
# vdr_FeatureInfoInitializeParentIndexStrings()
# vdr_FeatureInfoImputeOutname()
# vdr_FeatureInfoImpute3paFtrIdx()
# vdr_FeatureInfoImputeByOverlap()
# vdr_FeatureInfoInitializeMiscNotFailure()
# vdr_FeatureInfoInitializeIsDeletable()
# vdr_FeatureInfoInitializeAlternativeFeatureSet()
# vdr_FeatureInfoInitializeAlternativeFeatureSetSubstitution()
# vdr_FeatureInfoValidateMiscNotFailure()
# vdr_FeatureInfoValidateIsDeletable()
# vdr_FeatureInfoValidateAlternativeFeatureSet()
# vdr_FeatureInfoValidateAndConvertAlternativeFeatureSetSubstitution()
# vdr_FeatureInfoValidateCanonSpliceSites()
# vdr_FeatureInfoValidateExceptionKeys()
# vdr_FeatureInfoStartStopStrandArrays()
# vdr_FeatureInfoCountType()
# vdr_FeatureInfoValidateCoords()
# vdr_FeatureInfoValidateParentIndexStrings()
# vdr_FeatureInfoChildrenArrayOfArrays()
# vdr_FeatureInfoMapFtrTypeIndicesToFtrIndices()
# vdr_FeatureInfoMerge()
# vdr_FeatureInfoCdsStartStopCodonCoords()
# vdr_FeatureInfoMaxNumCdsSegments()
#
# vdr_SegmentInfoPopulate()
#
# vdr_FeatureTypeAndTypeIndexString()
# vdr_FeatureTypeIndex()
# vdr_FeatureTypeIsCds()
# vdr_FeatureTypeIsCdsOrIdStartAndStop()
# vdr_FeatureTypeIsMatPeptide()
# vdr_FeatureTypeIsGene()
# vdr_FeatureTypeIsCdsOrGene()
# vdr_FeatureTypeIsCdsOrMatPeptide()
# vdr_FeatureTypeIsCdsOrMatPeptideOrIdCoords()
# vdr_FeatureTypeIsCdsOrMatPeptideOrIdStartAndStop()
# vdr_FeatureTypeIsCdsOrMatPeptideOrGene()
# vdr_FeatureTypeCanBecomeMiscFeature()
# vdr_FeatureOutType()
# vdr_FeatureNumSegments()
# vdr_FeatureRelativeSegmentIndex()
# vdr_Feature5pMostPosition()
# vdr_Feature3pMostPosition()
# vdr_FeatureParentIndex()
# vdr_FeatureSummarizeSegment()
# vdr_FeatureStartStopStrandArrays()
# vdr_FeatureSummaryStrand()
# vdr_FeaturePositionSpecificValueBreakdown()
# vdr_FeatureCoordsListBreakdown()
#
# vdr_SegmentStartIdenticalToCds()
# vdr_SegmentStopIdenticalToCds()
#
# Subroutines related to alerts:
# vdr_AlertInfoInitialize()
# vdr_AlertInfoAdd()
# vdr_AlertInfoSetFTableInvalidatedBy()
# vdr_AlertInfoSetCausesFailure()
# vdr_AlertInfoSetMiscNotFailure()
# vdr_AlertInfoDump()
#
# Subroutines related to feature-specific alerts (misc_not_failure support):
# vdr_FeatureAlertCausesFailure()
# vdr_FeatureAlertIsMiscNotFailure()
#
# Subroutines related to alert exceptions:
# vdr_ExceptionCoordsAndValuesToSegmentsAndValues()
# vdr_ExceptionSegmentAndValueToPositionsAndValues()
# vdr_ExceptionSegmentAndValueParse()
# vdr_ExceptionCoordsAndValuesValidate()
#
# Subroutines related to parallelization on the compute farm:
# vdr_ParseQsubFile()
# vdr_SubmitJob()
# vdr_WaitForFarmJobsToFinish()
#
# Subroutines related to sequence and model coordinates:
# vdr_CoordsToSegments()
# vdr_CoordsSegmentParse()
# vdr_CoordsSegmentCreate()
# vdr_CoordsValidate()
# vdr_CoordsSegmentValidate()
# vdr_CoordsSinglePositionSegmentCreate()
# vdr_CoordsAppendSegment()
# vdr_CoordsLength()
# vdr_CoordsFromLocation()
# vdr_CoordsReverseComplement()
# vdr_CoordsSegmentReverseComplement()
# vdr_CoordsMin()
# vdr_CoordsMax()
# vdr_CoordsMissing()
# vdr_CoordsCheckIfSpans()
# vdr_CoordsSegmentOverlap()
# vdr_CoordsRelativeToAbsolute()
# vdr_CoordsRelativeSegmentToAbsolute()
# vdr_CoordsRelativeSingleCoordToAbsolute()
# vdr_CoordsProteinRelativeToAbsolute()
# vdr_CoordsProteinToNucleotide()
# vdr_CoordsMergeAllAdjacentSegments()
# vdr_CoordsMergeTwoSegmentsIfAdjacent()
# vdr_CoordsMaxLengthSegment()
# vdr_CoordsFromStartStopStrandArrays()
# vdr_CoordsSegmentActualToFractional()
# vdr_CoordsSegmentFractionalToActual()
#
# Subroutines related to eutils:
# vdr_EutilsFetchToFile()
# vdr_EutilsFetchUrl()
#
# Subroutines related to model info files:
# vdr_ModelInfoFileWrite()
# vdr_ModelInfoFileParse()
# vdr_ModelInfoCoordListValueBreakdown()
# vdr_ModelInfoValidateExceptionKeys()
#
# Subroutines related to cmalign output:
# vdr_CmalignCheckStdOutput()
# vdr_CmalignParseInsertFile()
# vdr_CmalignWriteInsertFile()
#
# Subroutines related to the --split option:
# vdr_CmalignCheckStdOutput()
# vdr_CmalignParseInsertFile()
# vdr_CmalignWriteInsertFile()
#
# Other subroutines related to running infernal programs
# vdr_CmemitConsensus()
#
# Subroutines related to merging output (--split):
# vdr_MergeOutputConcatenateOnly()
# vdr_MergeOutputConcatenatePreserveSpacing()
# vdr_MergeOutputGetFileList()
# vdr_MergeOutputMdlTabularFile()
# vdr_MergeOutputAlcTabularFile()
# vdr_MergeFrameshiftStockholmFiles()
# vdr_MergeAlignments()
#
# Subroutines supporting N-replacement (-r):
# vdr_ReplacePseudoCoordsStringCreate()
# vdr_ReplacePseudoCoordsStringParse()
#
# Subroutines related to backwards compatibility:
# vdr_BackwardsCompatibilityExceptions()
#
# Miscellaneous subroutines:
# vdr_SplitFastaFile()
# vdr_SplitNumSeqFiles()
# vdr_CdsFetchStockholmToFasta()
# vdr_ParseSeqFileToSeqHash()
# vdr_FrameAdjust()
# vdr_WriteCommandScript()
# vdr_GlsearchFormat3And9CToStockholmAndInsertFile()
# vdr_CigarToInsertsHash()
# vdr_CigarToPositionMap()
# vdr_UpdateInsertTokenInInsertString()
#
#################################################################
# Subroutine: vdr_FeatureInfoImputeCoords
# Incept: EPN, Wed Mar 13 13:15:33 2019
#
# Purpose: Fill "coords" values in %{$ftr_info_AHR}
#
# Arguments:
# $ftr_info_AHR: REF to feature information, added to here
# $FH_HR: REF to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: if $ftr_info_AHR is invalid upon entry
#
#################################################################
sub vdr_FeatureInfoImputeCoords {
my $sub_name = "vdr_FeatureInfoImputeCoords";
my $nargs_expected = 2;
if(scalar(@_) != $nargs_expected) { die "ERROR $sub_name entered with wrong number of input args" }
my $do_carrots = 0; # do not include carrots in coords strings, even if they exist in location strings
my ($ftr_info_AHR, $FH_HR) = @_;
# ftr_info_AHR should already have array data for keys "type", "location"
my @keys_A = ("type", "location");
my $nftr = utl_AHValidate($ftr_info_AHR, \@keys_A, "ERROR in $sub_name", $FH_HR);
for(my $ftr_idx = 0; $ftr_idx < $nftr; $ftr_idx++) {
$ftr_info_AHR->[$ftr_idx]{"coords"} = vdr_CoordsFromLocation($ftr_info_AHR->[$ftr_idx]{"location"}, $do_carrots, $FH_HR);
}
return;
}
#################################################################
# Subroutine: vdr_FeatureInfoImputeLength
# Incept: EPN, Thu Mar 14 12:07:16 2019
#
# Purpose: Fill "length" values in @{$ftr_info_AHR}
#
# Arguments:
# $ftr_info_AHR: REF to feature information, added to here
# $FH_HR: REF to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: if $ftr_info_AHR is invalid upon entry
#
#################################################################
sub vdr_FeatureInfoImputeLength {
my $sub_name = "vdr_FeatureInfoImputeLength";
my $nargs_expected = 2;
if(scalar(@_) != $nargs_expected) { die "ERROR $sub_name entered with wrong number of input args" }
my ($ftr_info_AHR, $FH_HR) = @_;
# ftr_info_AHR should already have array data for keys "type", "coords"
my @keys_A = ("type", "coords");
my $nftr = utl_AHValidate($ftr_info_AHR, \@keys_A, "ERROR in $sub_name", $FH_HR);
# go through all features and determine length by parsing the
# "coords" value
for(my $ftr_idx = 0; $ftr_idx < $nftr; $ftr_idx++) {
$ftr_info_AHR->[$ftr_idx]{"length"} = vdr_CoordsLength($ftr_info_AHR->[$ftr_idx]{"coords"}, $FH_HR);
}
return;
}
#################################################################
# Subroutine: vdr_FeatureInfoInitializeParentIndexStrings
# Incept: EPN, Wed Mar 13 13:33:33 2019
#
# Purpose: Set "parent_idx_str" value to "GBNULL" for any feature
# in which it is not already defined in @{$ftr_info_AHR}
#
# Arguments:
# $ftr_info_AHR: REF to feature information, added to here
# $FH_HR: REF to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: if $ftr_info_AHR is invalid upon entry
#
#################################################################
sub vdr_FeatureInfoInitializeParentIndexStrings {
my $sub_name = "vdr_FeatureInfoInitializeParentIndexStrings";
my $nargs_expected = 2;
if(scalar(@_) != $nargs_expected) { die "ERROR $sub_name entered with wrong number of input args" }
my ($ftr_info_AHR, $FH_HR) = @_;
my $nftr = scalar(@{$ftr_info_AHR});
for(my $ftr_idx = 0; $ftr_idx < $nftr; $ftr_idx++) {
if(! defined $ftr_info_AHR->[$ftr_idx]{"parent_idx_str"}) {
$ftr_info_AHR->[$ftr_idx]{"parent_idx_str"} = "GBNULL";
}
}
return;
}
#################################################################
# Subroutine: vdr_FeatureInfoImputeOutname()
# Incept: EPN, Mon Apr 1 06:49:20 2019
#
# Purpose: Fill "outname" values in @{$ftr_info_AHR}
# This is defined as:
# $ftr_info_AHR->[$ftr_idx]{"product"} if defined,
# else $ftr_info_AHR->[$ftr_idx]{"gene"} if defined,
# else string of type and type index (e.g. CDS.1)
#
# Arguments:
# $ftr_info_AHR: REF to array of hashes of feature info
#
# Returns: Feature name string
#
# Dies: Never, nothing is validated
#
#################################################################
sub vdr_FeatureInfoImputeOutname {
my $sub_name = "vdr_FeatureInfoImputeOutname";
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 ($ftr_info_AHR, $ftr_idx) = (@_);
my $nftr = scalar(@{$ftr_info_AHR});
for(my $ftr_idx = 0; $ftr_idx < $nftr; $ftr_idx++) {
if(defined $ftr_info_AHR->[$ftr_idx]{"product"}) {
$ftr_info_AHR->[$ftr_idx]{"outname"} = $ftr_info_AHR->[$ftr_idx]{"product"};
}
elsif(defined $ftr_info_AHR->[$ftr_idx]{"gene"}) {
$ftr_info_AHR->[$ftr_idx]{"outname"} = $ftr_info_AHR->[$ftr_idx]{"gene"};
}
else {
$ftr_info_AHR->[$ftr_idx]{"outname"} = vdr_FeatureTypeAndTypeIndexString($ftr_info_AHR, $ftr_idx, ".");
}
}
return;
}
#################################################################
# Subroutine: vdr_FeatureInfoImpute3paFtrIdx
# Incept: EPN, Wed Mar 13 13:39:34 2019
#
# Purpose: Fill "3pa_ftr_idx" values in @{$ftr_info_AHR}
#
# Arguments:
# $ftr_info_AHR: REF to feature information, added to here
# $FH_HR: REF to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: if $ftr_info_AHR is invalid upon entry
#
#################################################################
sub vdr_FeatureInfoImpute3paFtrIdx {
my $sub_name = "vdr_FeatureInfoImpute3paFtrIdx";
my $nargs_expected = 2;
if(scalar(@_) != $nargs_expected) { die "ERROR $sub_name entered with wrong number of input args" }
my ($ftr_info_AHR, $FH_HR) = @_;
# ftr_info_AHR should already have array data for keys "type", "coords"
my @keys_A = ("type", "coords");
my $nftr = utl_AHValidate($ftr_info_AHR, \@keys_A, "ERROR in $sub_name", $FH_HR);
# go through all features and determine adjacent mat_peptides (set '3pa_ftr_idx')
#
# $ftr_info_AHR->{"3pa_ftr_idx"}[$ftr_idx] set to $ftr_idx2 if:
# - $ftr_idx type is mat_peptide
# - $ftr_idx2 type is mat_peptide
# - $ftr_idx starts at 1 position 3' of stop position of $ftr_idx
# - $ftr_idx and $ftr_idx2 are both "+" or both "-" strands
#
# else "-1" if no $ftr_idx2 exists for $ftr_idx that satisfies above
#
# dies if more than one $ftr_idx2 satisfies above
my ($ftr_idx, $ftr_idx2); # feature indices
my $ftr_3p_pos; # 3'-most position for feature $ftr_idx
my $ftr_5p_pos2; # 5'-most position for feature $ftr_idx2
my $ftr_strand; # strand for feature $ftr_idx
my $ftr_strand2; # strand for feature $ftr_idx2
my $found_adj = 0;
for($ftr_idx = 0; $ftr_idx < $nftr; $ftr_idx++) {
$ftr_info_AHR->[$ftr_idx]{"3pa_ftr_idx"} = -1;
if($ftr_info_AHR->[$ftr_idx]{"type"} eq "mat_peptide") {
$ftr_3p_pos = vdr_Feature3pMostPosition($ftr_info_AHR->[$ftr_idx]{"coords"}, $FH_HR);
$ftr_strand = vdr_FeatureSummaryStrand($ftr_info_AHR->[$ftr_idx]{"coords"}, $FH_HR);
for($ftr_idx2 = 0; $ftr_idx2 < $nftr; $ftr_idx2++) {
$ftr_5p_pos2 = vdr_Feature5pMostPosition($ftr_info_AHR->[$ftr_idx2]{"coords"}, $FH_HR);
$ftr_strand2 = vdr_FeatureSummaryStrand($ftr_info_AHR->[$ftr_idx2]{"coords"}, $FH_HR);
$found_adj = 0;
if(($ftr_idx != $ftr_idx2) &&
($ftr_info_AHR->[$ftr_idx2]{"type"} eq "mat_peptide") &&
($ftr_strand eq $ftr_strand2)) {
if(($ftr_strand eq "+") && (($ftr_3p_pos+1) == ($ftr_5p_pos2))) {
$found_adj = 1;
}
if(($ftr_strand eq "-") && (($ftr_3p_pos-1) == ($ftr_5p_pos2))) {
$found_adj = 1;
}
if($found_adj) {
if($ftr_info_AHR->[$ftr_idx]{"3pa_ftr_idx"} == -1) {
# if multiple features are 3'-adjacent, keep only the first one
$ftr_info_AHR->[$ftr_idx]{"3pa_ftr_idx"} = $ftr_idx2;
}
}
}
}
}
}
return;
}
#################################################################
# Subroutine: vdr_FeatureInfoImputeByOverlap
# Incept: EPN, Tue May 21 06:28:07 2019
#
# Purpose: Add a qualifier and value to instances of one feature
# type based on qualifier and values of another feature
# type and overlap of nucleotides
#
# Arguments:
# $ftr_info_AHR: REF to feature information, added to here
# $src_type: source feature type
# $src_key: source feature key (value in %{$ftr_info_AH[$src_idx]})
# $dst_type: destination feature type
# $dst_key: destination feature key (value in %{$ftr_info_AH[$dst_idx]})
# $FH_HR: REF to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: if $ftr_info_AHR is invalid upon entry
#
#################################################################
sub vdr_FeatureInfoImputeByOverlap {
my $sub_name = "vdr_FeatureInfoImputeByOverlap";
my $nargs_expected = 6;
if(scalar(@_) != $nargs_expected) { die "ERROR $sub_name entered with wrong number of input args" }
my ($ftr_info_AHR, $src_type, $src_key, $dst_type, $dst_key, $FH_HR) = @_;
# printf("in $sub_name, src_type: $src_type, src_key: $src_key, dst_type: $dst_type, dst_key: $dst_key\n");
# contract check
if($src_type eq $dst_type) {
ofile_FAIL("ERROR in $sub_name, source type and destination type are equal ($src_type)\n", 1, $FH_HR);
}
# ftr_info_AHR should already have array data for keys "type", "coords"
my @keys_A = ("type", "coords");
my $nftr = utl_AHValidate($ftr_info_AHR, \@keys_A, "ERROR in $sub_name", $FH_HR);
# - go through all features and find F1 of type $dst_type
# - if F1 does not have a value for $dst_key
# find other features F2 of type $src_type that 'span' F1
# where spans means every position in F1 also exists in F2
# - set F1 $dst_key to value in F2 for $src_key
#
# if more than one F2 exists for an F1 that meet criteria above:
# - set F1 $dst_key to value in F2 for $src_key for shortest F2
#
# if more than one F2 exists with identical coords for an F1 that meet criteria above
# - die
#
my ($dst_ftr_idx, $src_ftr_idx); # feature indices
for($dst_ftr_idx = 0; $dst_ftr_idx < $nftr; $dst_ftr_idx++) {
if(($ftr_info_AHR->[$dst_ftr_idx]{"type"} eq $dst_type) &&
(! defined $ftr_info_AHR->[$dst_ftr_idx]{$dst_key})) {
my $found_src_ftr_idx = undef;
for($src_ftr_idx = 0; $src_ftr_idx < $nftr; $src_ftr_idx++) {
if(($dst_ftr_idx != $src_ftr_idx) &&
($ftr_info_AHR->[$src_ftr_idx]{"type"} eq $src_type) &&
(defined $ftr_info_AHR->[$src_ftr_idx]{$src_key})) {
# determine if $dst_ftr_idx is completely spanned by $src_ftr_idx overlap completely
# printf("checking overlap between src " . $ftr_info_AHR->[$src_ftr_idx]{"coords"} . " and dst " . $ftr_info_AHR->[$dst_ftr_idx]{"coords"} . "\n");
if(vdr_CoordsCheckIfSpans($ftr_info_AHR->[$src_ftr_idx]{"coords"}, $ftr_info_AHR->[$dst_ftr_idx]{"coords"}, $FH_HR)) {
# $src_ftr_idx completely spans $dst_ftr_idx
my $do_update = 1; # set to '0' below if $found_src_ftr_idx is defined and is shorter than $src_ftr_idx
if(defined $found_src_ftr_idx) {
# this is okay, pick the shorter of the two
# (but if they have identical coords values --> die)
my $existing_coords = $ftr_info_AHR->[$found_src_ftr_idx]{"coords"};
my $current_coords = $ftr_info_AHR->[$src_ftr_idx]{"coords"};
if($existing_coords eq $current_coords) {
ofile_FAIL("ERROR in $sub_name, more than one feature of type $src_type (with key $src_key) of identical coords ($existing_coords) spans feature idx $dst_ftr_idx of type $dst_type with coords " . $ftr_info_AHR->[$dst_ftr_idx]{"coords"} . "\n1: idx: $found_src_ftr_idx, value: " . $ftr_info_AHR->[$found_src_ftr_idx]{$src_key} . "\n2: idx: $src_ftr_idx, value: " . $ftr_info_AHR->[$src_ftr_idx]{$src_key} . "\n", 1, $FH_HR);
}
# if we get here, the coords differ, only update if current is smaller than existing
if((vdr_CoordsLength($current_coords, $FH_HR)) >= (vdr_CoordsLength($existing_coords, $FH_HR))) {
# printf("in $sub_name, skipping update of ftr_info_AHR->[$dst_ftr_idx]{$dst_key} of type $dst_type to feature idx $src_ftr_idx with value $ftr_info_AHR->[$src_ftr_idx]{$src_key} and coords " . $ftr_info_AHR->[$src_ftr_idx]{"coords"} . "\nbecause shorter overlap exists: feature $found_src_ftr_idx with value $ftr_info_AHR->[$found_src_ftr_idx]{$src_key} and coords . " . $ftr_info_AHR->[$found_src_ftr_idx]{"coords"} . "\n");
$do_update = 0;
}
} # end of 'if(defined $found_src_ftr_idx)'
if($do_update) { # will be '1' if (! defined $found_src_ftr_idx) or if $src_ftr_idx is shorter than $found_src_ftr_idx
# printf("in $sub_name, setting ftr_info_AHR->[$dst_ftr_idx]{$dst_key} of type $dst_type to $ftr_info_AHR->[$src_ftr_idx]{$src_key}\n");
$ftr_info_AHR->[$dst_ftr_idx]{$dst_key} = $ftr_info_AHR->[$src_ftr_idx]{$src_key};
$found_src_ftr_idx = $src_ftr_idx;
}
}
}
}
}
}
return;
}
#################################################################
# Subroutine: vdr_FeatureInfoInitializeMiscNotFailure
# Incept: EPN, Fri Feb 5 11:44:11 2021
#
# Purpose: Set "misc_not_failure" value to 0 for any feature
# in which it is not already defined in @{$ftr_info_AHR}.
# If $force_zero, set all values to 0 even if they are
# already defined.
#
# Arguments:
# $ftr_info_AHR: REF to feature information, added to here
# $force_zero: '1' to set values to '0' for all features, even if already defined
# $FH_HR: REF to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: never
#
#################################################################
sub vdr_FeatureInfoInitializeMiscNotFailure {
my $sub_name = "vdr_FeatureInfoInitializeMiscNotFailure";
my $nargs_expected = 3;
if(scalar(@_) != $nargs_expected) { die "ERROR $sub_name entered with wrong number of input args" }
my ($ftr_info_AHR, $force_zero, $FH_HR) = @_;
my $nftr = scalar(@{$ftr_info_AHR});
for(my $ftr_idx = 0; $ftr_idx < $nftr; $ftr_idx++) {
if(($force_zero) || (! defined $ftr_info_AHR->[$ftr_idx]{"misc_not_failure"})) {
$ftr_info_AHR->[$ftr_idx]{"misc_not_failure"} = 0;
}
}
return;
}
#################################################################
# Subroutine: vdr_FeatureInfoInitializeIsDeletable
# Incept: EPN, Tue Sep 28 21:09:22 2021
#
# Purpose: Set "is_deletable" value to 0 for any feature
# in which it is not already defined in @{$ftr_info_AHR}.
# If $force_zero, set all values to 0 even if they are
# already defined.
#
# Arguments:
# $ftr_info_AHR: REF to feature information, added to here
# $force_zero: '1' to set values to '0' for all features, even if already defined
# $FH_HR: REF to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: never
#
#################################################################
sub vdr_FeatureInfoInitializeIsDeletable {
my $sub_name = "vdr_FeatureInfoInitializeIsDeletable";
my $nargs_expected = 3;
if(scalar(@_) != $nargs_expected) { die "ERROR $sub_name entered with wrong number of input args" }
my ($ftr_info_AHR, $force_zero, $FH_HR) = @_;
my $nftr = scalar(@{$ftr_info_AHR});
for(my $ftr_idx = 0; $ftr_idx < $nftr; $ftr_idx++) {
if(($force_zero) || (! defined $ftr_info_AHR->[$ftr_idx]{"is_deletable"})) {
$ftr_info_AHR->[$ftr_idx]{"is_deletable"} = 0;
}
}
return;
}
#################################################################
# Subroutine: vdr_FeatureInfoInitializeAlternativeFeatureSet
# Incept: EPN, Tue Oct 12 19:43:46 2021
#
# Purpose: Set "alternative_ftr_set" value to "" for any feature
# in which it is not already defined in @{$ftr_info_AHR}.
# If $force_empty, set all values to "" even if they are
# already defined.
#
# Arguments:
# $ftr_info_AHR: REF to feature information, added to here
# $force_empty: '1' to set values to "" for all features, even if already defined
# $FH_HR: REF to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: never
#
#################################################################
sub vdr_FeatureInfoInitializeAlternativeFeatureSet {
my $sub_name = "vdr_FeatureInfoInitializeAlternativeFeatureSet";
my $nargs_expected = 3;
if(scalar(@_) != $nargs_expected) { die "ERROR $sub_name entered with wrong number of input args" }
my ($ftr_info_AHR, $force_empty, $FH_HR) = @_;
my $nftr = scalar(@{$ftr_info_AHR});
for(my $ftr_idx = 0; $ftr_idx < $nftr; $ftr_idx++) {
if(($force_empty) || (! defined $ftr_info_AHR->[$ftr_idx]{"alternative_ftr_set"})) {
$ftr_info_AHR->[$ftr_idx]{"alternative_ftr_set"} = "";
}
}
return;
}
#################################################################
# Subroutine: vdr_FeatureInfoInitializeAlternativeFeatureSetSubstitution
# Incept: EPN, Thu Oct 14 21:22:20 2021
#
# Purpose: Set "alternative_ftr_set_subn" value to "" for any feature
# in which it is not already defined in @{$ftr_info_AHR}.
# If $force_empty, set all values to "" even if they are
# already defined.
#
# Arguments:
# $ftr_info_AHR: REF to feature information, added to here
# $force_empty: '1' to set values to "" for all features, even if already defined
# $FH_HR: REF to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: never
#
#################################################################
sub vdr_FeatureInfoInitializeAlternativeFeatureSetSubstitution {
my $sub_name = "vdr_FeatureInfoInitializeAlternativeFeatureSetSubstitution";
my $nargs_expected = 3;
if(scalar(@_) != $nargs_expected) { die "ERROR $sub_name entered with wrong number of input args" }
my ($ftr_info_AHR, $force_empty, $FH_HR) = @_;
my $nftr = scalar(@{$ftr_info_AHR});
for(my $ftr_idx = 0; $ftr_idx < $nftr; $ftr_idx++) {
if(($force_empty) || (! defined $ftr_info_AHR->[$ftr_idx]{"alternative_ftr_set_subn"})) {
$ftr_info_AHR->[$ftr_idx]{"alternative_ftr_set_subn"} = "";
}
}
return;
}
#################################################################
# Subroutine: vdr_FeatureInfoInitializeCanonSpliceSites
# Incept: EPN, Mon Aug 7 13:34:57 2023
#
# Purpose: Set "canon_splice_sites" value to 0 for any feature
# in which it is not already defined in @{$ftr_info_AHR}.
# If $force_empty, set all values to 0 even if they are
# already defined.
#
# Arguments:
# $ftr_info_AHR: REF to feature information, added to here
# $force_one: '1' to set values to 1 for all features, even if already defined as 0
# $force_zero: '1' to set values to 0 for all features, even if already defined as 1
# $FH_HR: REF to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: never
#
#################################################################
sub vdr_FeatureInfoInitializeCanonSpliceSites {
my $sub_name = "vdr_FeatureInfoInitializeCanonSpliceSites";
my $nargs_expected = 4;
if(scalar(@_) != $nargs_expected) { die "ERROR $sub_name entered with wrong number of input args" }
my ($ftr_info_AHR, $force_one, $force_zero, $FH_HR) = @_;
my $nftr = scalar(@{$ftr_info_AHR});
for(my $ftr_idx = 0; $ftr_idx < $nftr; $ftr_idx++) {
if($force_one) {
$ftr_info_AHR->[$ftr_idx]{"canon_splice_sites"} = 1;
}
if(($force_zero) || (! defined $ftr_info_AHR->[$ftr_idx]{"canon_splice_sites"})) {
$ftr_info_AHR->[$ftr_idx]{"canon_splice_sites"} = 0;
}
}
return;
}
#################################################################
# Subroutine: vdr_FeatureInfoValidateMiscNotFailure
# Incept: EPN, Fri Feb 5 11:45:29 2021
#
# Purpose: Validate "misc_not_failure" values are either 0 or 1.
# Should probably be called after vdr_FeatureInfoInitializeMiscNotFailure()
#
# I considered enforcing that "misc_not_failure" could only exist
# for feature types that can become misc_features but decided
# against it so that 'gene' features can have alerts that would
# be fatal except that a misc_not_failure=1 value causes them not
# to be fatal. These won't be turned into misc_features because
# output_feature_table() won't let them be, but that's ok.
#
# Arguments:
# $ftr_info_AHR: REF to feature information, added to here
# $FH_HR: REF to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: if misc_not_failure is invalid for any feature
#
#################################################################
sub vdr_FeatureInfoValidateMiscNotFailure {
my $sub_name = "vdr_FeatureInfoValidateMiscNotFailure";
my $nargs_expected = 2;
if(scalar(@_) != $nargs_expected) { die "ERROR $sub_name entered with wrong number of input args" }
my ($ftr_info_AHR, $FH_HR) = @_;
my $nftr = scalar(@{$ftr_info_AHR});
my $fail_str = ""; # added to if any elements are out of range
for(my $ftr_idx = 0; $ftr_idx < $nftr; $ftr_idx++) {
if(! defined $ftr_info_AHR->[$ftr_idx]{"misc_not_failure"}) {
$fail_str .= "ftr_idx: $ftr_idx, undefined\n";
}
elsif(($ftr_info_AHR->[$ftr_idx]{"misc_not_failure"} != 1) && ($ftr_info_AHR->[$ftr_idx]{"misc_not_failure"} != 0)) {
$fail_str .= "ftr_idx: $ftr_idx, " . $ftr_info_AHR->[$ftr_idx]{"misc_not_failure"} . " != 0 and != 1\n";
}
# decided against this enforcement (see Purpose):
# elsif(($ftr_info_AHR->[$ftr_idx]{"misc_not_failure"} == 1) && (! vdr_FeatureTypeCanBecomeMiscFeature($ftr_info_AHR, $ftr_idx))) {
# $fail_str .= "ftr_idx: $ftr_idx, " . $ftr_info_AHR->[$ftr_idx]{"misc_not_failure"} . " is 1 but type is " . $ftr_info_AHR->[$ftr_idx]{"type"} . " and that type cannot become a misc_feature (hard-coded)\n";
# }
}
if($fail_str ne "") {
ofile_FAIL("ERROR in $sub_name, some misc_not_failure values are invalid or don't make sense:\n$fail_str\n", 1, $FH_HR);
}
return;
}
#################################################################
# Subroutine: vdr_FeatureInfoValidateIsDeletable
# Incept: EPN, Tue Sep 28 21:09:10 2021
#
# Purpose: Validate "is_deletable" values are either 0 or 1.
# Should probably be called after vdr_FeatureInfoInitializeIsDeletable()
#
# Arguments:
# $ftr_info_AHR: REF to feature information, added to here
# $FH_HR: REF to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: if is_deletable is invalid for any feature
#
#################################################################
sub vdr_FeatureInfoValidateIsDeletable {
my $sub_name = "vdr_FeatureInfoValidateIsDeletable";
my $nargs_expected = 2;
if(scalar(@_) != $nargs_expected) { die "ERROR $sub_name entered with wrong number of input args" }
my ($ftr_info_AHR, $FH_HR) = @_;
my $nftr = scalar(@{$ftr_info_AHR});
my $fail_str = ""; # added to if any elements are out of range
for(my $ftr_idx = 0; $ftr_idx < $nftr; $ftr_idx++) {
if(! defined $ftr_info_AHR->[$ftr_idx]{"is_deletable"}) {
$fail_str .= "ftr_idx: $ftr_idx, undefined\n";
}
elsif(($ftr_info_AHR->[$ftr_idx]{"is_deletable"} != 1) && ($ftr_info_AHR->[$ftr_idx]{"is_deletable"} != 0)) {
$fail_str .= "ftr_idx: $ftr_idx, " . $ftr_info_AHR->[$ftr_idx]{"is_deletable"} . " != 0 and != 1\n";
}
}
if($fail_str ne "") {
ofile_FAIL("ERROR in $sub_name, some is_deletable values are invalid or don't make sense:\n$fail_str\n", 1, $FH_HR);
}
return;
}
#################################################################
# Subroutine: vdr_FeatureInfoValidateAlternativeFeatureSet
# Incept: EPN, Tue Sep 28 21:09:10 2021
#
# Purpose: Validate "alternative_ftr_set" values are either "" or another
# string. If another string, each other string must be the
# value for "alternative_ftr_set" in more than one
# feature. Also ensure that for any sets that have >= 1
# children, all the features in that set are all the
# children of the same parent.
#
# Should probably be called after
# vdr_FeatureInfoInitializeAlternativeFeatureSet()
# and
# vdr_FeatureInfoValidateParentIndexStrings()
#
# Arguments:
# $ftr_info_AHR: REF to feature information, added to here
# $FH_HR: REF to hash of file handles, including "log" and "cmd"
#
# Returns: '1' if there are any 'alternative_ftr_set' values ne ""
# '0' if all 'alternative_ftr_set' values are ""
#
# Dies: if any alternative_ftr_set values are undefined
# if any alternative_ftr_set values exist only once
#
#################################################################
sub vdr_FeatureInfoValidateAlternativeFeatureSet {
my $sub_name = "vdr_FeatureInfoValidateAlternativeFeatureSet";
my $nargs_expected = 2;
if(scalar(@_) != $nargs_expected) { die "ERROR $sub_name entered with wrong number of input args" }
my ($ftr_info_AHR, $FH_HR) = @_;
my $nftr = scalar(@{$ftr_info_AHR});
my $ret_val = 0; # set to '1' if we see any values ne ""
my $fail_str = ""; # added to if any elements are out of range
my %set_HA = (); # key is set value, array is feature indices in that set
my $ftr_idx = undef;
for($ftr_idx = 0; $ftr_idx < $nftr; $ftr_idx++) {
if(! defined $ftr_info_AHR->[$ftr_idx]{"alternative_ftr_set"}) {
$fail_str .= "ftr_idx: $ftr_idx, undefined\n";
}
else {
my $value = $ftr_info_AHR->[$ftr_idx]{"alternative_ftr_set"};
if($value ne "") {
$ret_val = 1;
if(! defined $set_HA{$value}) {
@{$set_HA{$value}} = ();
}
push(@{$set_HA{$value}}, $ftr_idx);
}
}
}
# make sure that each alternative_ftr_set has >= 2 members
# and that for any set that has >= 1 children, all members are children with the same parent
foreach my $key (sort keys (%set_HA)) {
my $nset = scalar(@{$set_HA{$key}});
if($nset == 1) {
$fail_str .= "alternative_ftr_set value: $key exists only once, each value must exist at least twice\n";
}
else {
my $nchildren = 0;
my $common_parent_idx = undef;
foreach $ftr_idx (@{$set_HA{$key}}) {
my $parent_idx = $ftr_info_AHR->[$ftr_idx]{"parent_idx_str"};
if((defined $parent_idx) && ($parent_idx ne "GBNULL")) {
$nchildren++;
if(! defined $common_parent_idx) {
$common_parent_idx = $parent_idx;
}
elsif($parent_idx != $common_parent_idx) {
$fail_str .= "ftr_idx: $ftr_idx is child of parent $parent_idx but >= 1 other feature(s) in same set ($key) have a different parent ($common_parent_idx), this is not allowed\n";
}
}
}
# make sure if any members are children, then all members are children
if(($nchildren != 0) && ($nchildren != $nset)) {
$fail_str .= "for alternative_ftr_set with key $key, some but not all members are children of $common_parent_idx\n";
}
}
}
if($fail_str ne "") {
ofile_FAIL("ERROR in $sub_name, some alternative_ftr_set values are invalid or don't make sense:\n$fail_str\n", 1, $FH_HR);
}
return $ret_val;
}
#################################################################
# Subroutine: vdr_FeatureInfoValidateAndConvertAlternativeFeatureSetSubstitution
# Incept: EPN, Fri Oct 15 10:07:54 2021
#
# Purpose: Validate "alternative_ftr_set_subn" values are either "",
# "<s>.<d1>" or "<d2>" where <s> is a valid
# "alternative_ftr_set" for a set other than the set of
# the current feature, and "<d1>" is an index 1..n where n
# is the size of the set "<s>". Alternatively, for
# backwards compatibility, "<d2>" can be a feature index
# <0..nftr-1> as long as it isn't self idx.
#
# Should probably be called after
# vdr_FeatureInfoInitializeAlternativeFeatureSetSubstitution()
#
# Arguments:
# $ftr_info_AHR: REF to feature information, added to here
# $FH_HR: REF to hash of file handles, including "log" and "cmd"
#
# Returns: void
#
# Dies: if any alternative_ftr_set_subn values are undefined
# if any alternative_ftr_set_subn values are invalid
#
#################################################################
sub vdr_FeatureInfoValidateAndConvertAlternativeFeatureSetSubstitution {
my $sub_name = "vdr_FeatureInfoValidateAndConvertAlternativeFeatureSetSubstitution";
my $nargs_expected = 2;
if(scalar(@_) != $nargs_expected) { die "ERROR $sub_name entered with wrong number of input args" }
my ($ftr_info_AHR, $FH_HR) = @_;
my $nftr = scalar(@{$ftr_info_AHR});
my $fail_str = ""; # added to if any elements are out of range
for(my $ftr_idx = 0; $ftr_idx < $nftr; $ftr_idx++) {
my $subn_val = $ftr_info_AHR->[$ftr_idx]{"alternative_ftr_set_subn"};
if(! defined $subn_val) {
$fail_str .= "ftr_idx: $ftr_idx, undefined\n";
}
elsif($subn_val ne "") {
if($subn_val =~ /^(\S+)\.(\d+)$/) { # e.g. "attachment(cds).2"
my ($set, $set_idx) = ($1, $2);
# make sure $set is not the set that this ftr belongs to
if((defined $ftr_info_AHR->[$ftr_idx]{"alternative_ftr_set"}) &&
($ftr_info_AHR->[$ftr_idx]{"alternative_ftr_set"} eq $set)) {
$fail_str .= "ftr_idx: $ftr_idx, " . $subn_val . " implies the substitution set is the same as the ftr set ($set) for this feature.\n";
}
else {
# find the ftr $set.$set_idx corresponds to:
my $cur_set_idx = 0;
my $found_flag = 0; # set to 1 if we find it
for(my $ftr_idx2 = 0; $ftr_idx2 < $nftr; $ftr_idx2++) {
if((defined $ftr_info_AHR->[$ftr_idx2]{"alternative_ftr_set"}) &&
($ftr_info_AHR->[$ftr_idx2]{"alternative_ftr_set"} eq $set)) {
$cur_set_idx++;
if($cur_set_idx == $set_idx) {
# rewrite value
$ftr_info_AHR->[$ftr_idx]{"alternative_ftr_set_subn"} = $ftr_idx2;
$found_flag = 1;
}
}
}
if(! $found_flag) {
$fail_str .= "ftr_idx: $ftr_idx, " . $subn_val . " implies substitution ftr is from alternative_ftr_set $set index $set_idx, but found $cur_set_idx features with that alternative_ftr_set value.\n";
}
}
}
elsif($subn_val =~ /^\d+$/) {
if($subn_val < 0) {
$fail_str .= "ftr_idx: $ftr_idx, " . $subn_val . " < 0\n";
}
elsif($subn_val >= $nftr) {
$fail_str .= "ftr_idx: $ftr_idx, " . $subn_val . " >= $nftr (num features, should be 0.." . ($nftr-1) . ")\n";
}
elsif($subn_val == $ftr_idx) {
$fail_str .= "ftr_idx: $ftr_idx, is its own substitute, this is not allowed\n";
}
}
# else valid feature index
}
}
if($fail_str ne "") {
ofile_FAIL("ERROR in $sub_name, some alternative_ftr_set_subn index strings are undefined or don't make sense:\n$fail_str\n", 1, $FH_HR);
}
return;
}
#################################################################
# Subroutine: vdr_FeatureInfoValidateCanonSpliceSites
# Incept: EPN, Mon Aug 7 13:37:15 2023
#
# Purpose: Validate "canon_splice_sites" values are either 0 or 1.
# Should probably be called after vdr_FeatureInfoInitializeCanonSpliceSites()
#
# Arguments: