-
Notifications
You must be signed in to change notification settings - Fork 41
/
amr_report.cpp
2600 lines (2389 loc) · 84 KB
/
amr_report.cpp
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
// amr_report.cpp
/*===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
* Author: Vyacheslav Brover
*
* File Description:
* Identification of AMR genes using BLAST and HMM search results vs. the NCBI virulence database
*
*/
#undef NDEBUG
#include "common.hpp"
#include "tsv.hpp"
using namespace Common_sp;
#include "gff.hpp"
using namespace GFF_sp;
#include "alignment.hpp"
using namespace Alignment_sp;
#include "columns.hpp"
#include "common.inc"
namespace
{
// Global
// PAR
constexpr bool useCrossOrigin = false; // GPipe: true
constexpr double frac_delta = 1e-5;
constexpr size_t domain_min = 20; // aa
const string na ("NA");
bool ident_min_user = false;
bool equidistant = false;
bool cdsExist = false;
bool print_node = false;
bool print_node_raw = false;
bool reportPseudo = false;
bool parentTargetProt = true;
string input_name;
//const string stopCodonS ("[stop]");
//const string frameShiftS ("[frameshift]");
map <string/*accession*/, Vector<AmrMutation>> accession2mutations;
struct BlastRule : Root
// PD-2310
{
// 0 <=> undefined
// 0 .. 1
double ident {0.0};
// Of alignment
double target_coverage {0.0}; // Not used
double ref_coverage {0.0};
BlastRule (double ident_arg,
double ref_coverage_arg)
: ident (ident_arg)
, ref_coverage (ref_coverage_arg)
{
QC_ASSERT (ident > 0.0);
QC_ASSERT (ident <= 1.0);
QC_ASSERT (target_coverage >= 0.0);
QC_ASSERT (target_coverage <= 1.0);
QC_ASSERT (ref_coverage > 0.0);
QC_ASSERT (ref_coverage <= 1.0);
}
BlastRule () = default;
void saveText (ostream &os) const final
{ os << ident << ' ' << ref_coverage; }
bool empty () const final
{ return ! ident; }
};
BlastRule defaultCompleteBR;
BlastRule defaultPartialBR;
struct Fam
// Table PROTEUS.VF..FAM
{
const Fam* parent {nullptr};
// Tree
string id;
string genesymbol;
string familyName;
uchar reportable {0};
// 0,1,2
// HMM
string hmm;
// May be empty()
double tc1 {NaN};
double tc2 {NaN};
// BlastRule's
BlastRule completeBR;
BlastRule partialBR;
string type;
string subtype;
string classS;
string subclass;
Fam (const string &id_arg,
const string &genesymbol_arg,
const string &hmm_arg,
double tc1_arg,
double tc2_arg,
const BlastRule &completeBR_arg,
const BlastRule &partialBR_arg,
const string &type_arg,
const string &subtype_arg,
const string &class_arg,
const string &subclass_arg,
const string &familyName_arg,
uchar reportable_arg)
: id (id_arg)
, genesymbol (genesymbol_arg)
, familyName (familyName_arg)
, reportable (reportable_arg)
, hmm (hmm_arg)
, tc1 (tc1_arg)
, tc2 (tc2_arg)
, completeBR (completeBR_arg)
, partialBR (partialBR_arg)
, type (type_arg)
, subtype (subtype_arg)
, classS (class_arg)
, subclass (subclass_arg)
{ if (genesymbol == "-")
genesymbol. clear ();
if (hmm == "-")
hmm. clear ();
QC_ASSERT (hmm. empty () == ! tc1);
QC_ASSERT (hmm. empty () == ! tc2);
//IMPLY (! hmm. empty (), tc2 > 0);
if (familyName == "NULL")
familyName. clear ();
QC_ASSERT (tc2 >= 0);
QC_ASSERT (tc2 <= tc1);
QC_ASSERT (! completeBR. empty ())
QC_ASSERT (! partialBR. empty ());
}
Fam () = default;
void saveText (ostream &os) const
{ os << hmm << " " << tc1 << " " << tc2 << " " << familyName << " " << (int) reportable; }
bool descendantOf (const Fam* ancestor) const
{ if (! ancestor)
return true;
if (this == ancestor)
return true;
if (parent)
return parent->descendantOf (ancestor);
return false;
}
const Fam* getHmmFam () const
// Return: most specific HMM
{ const Fam* f = this;
while (f && f->hmm. empty ())
f = f->parent;
return f;
}
};
map<string/*famId*/,const Fam*> famId2fam;
// Value: !nullptr
// Not delete'd
struct Batch;
struct BlastAlignment;
struct HmmAlignment
// Query: AMR HMM
{
string sseqid;
double score1 {NaN};
double score2 {NaN};
// May be different from max(Domain::score)
const Fam* fam {nullptr};
// Query
// !nullptr
//ali_from, ali_to ??
unique_ptr<const BlastAlignment> blastAl;
// (bool)get()
HmmAlignment (const string &line,
const Batch &batch);
// Update: batch.domains
void saveText (ostream &os) const
{ os << sseqid << ' ' << score1 << ' ' << score2 << ' ' << (fam ? fam->hmm : noString); }
bool good () const
{ QC_ASSERT (fam);
QC_ASSERT (! fam->hmm. empty ());
return score1 >= fam->tc1
&& score2 >= fam->tc2;
}
private:
bool betterEq (const HmmAlignment &other,
unsigned char criterion) const
// Reflexive
// For one sseqid: one HmmAlignment is better than all others
{ ASSERT (good ());
ASSERT (other. good ());
if (sseqid != other. sseqid)
return false;
switch (criterion)
{
case 0: return fam->descendantOf (other. fam);
case 1:
{
LESS_PART (other, *this, score1);
//return score2 >= other. score2; // GP-16770
LESS_PART (other, *this, fam->tc1);
if (! equidistant)
LESS_PART (*this, other, fam->id); // Tie resolution
return true;
}
default: ERROR;
}
throw logic_error ("Never call");
}
public:
bool better (const HmmAlignment &other,
unsigned char criterion) const
{ return betterEq (other, criterion)
&& ! other. betterEq (*this, criterion); }
bool better (const BlastAlignment &other) const;
typedef pair<string/*sseqid*/,string/*FAM.id*/> Pair;
struct Domain
{
double score {0};
size_t hmmLen {0};
size_t hmmStart {0};
size_t hmmStop {0};
size_t seqLen {0};
size_t seqStart {0};
size_t seqStop {0};
Domain (const string &line,
Batch &batch);
// Input: line: hmmsearch -domtable line
Domain () = default;
};
};
struct Susceptible : Root
{
// !empty()
string genesymbol;
double cutoff;
string classS;
string subclass;
string name;
Susceptible (const string &genesymbol_arg,
double cutoff_arg,
const string &class_arg,
const string &subclass_arg,
const string &name_arg)
: genesymbol (genesymbol_arg)
, cutoff (cutoff_arg / 100.0)
, classS (class_arg)
, subclass (subclass_arg)
, name (name_arg)
{
QC_ASSERT (cutoff > 0.0);
QC_ASSERT (cutoff <= 1.0);
QC_ASSERT (! name. empty ());
QC_ASSERT (! contains (name, '\t'));
replace (name, '_', ' ');
QC_ASSERT (! contains (name, " "));
}
Susceptible () = default;
Susceptible& operator= (Susceptible&& other) = default;
void saveText (ostream &os) const final
{ os << genesymbol
<< '\t' << cutoff
<< '\t' << classS
<< '\t' << subclass
<< '\t' << name
<< endl;
}
};
map <string/*accession*/, Susceptible> accession2susceptible;
struct BlastAlignment : Alignment
// BLASTP or BLASTX
{
// target
size_t targetAlign {0};
size_t targetAlign_aa {0};
bool partialDna {false};
bool stopCodon {false};
// Reference protein
const bool fromHmm;
string refAccession;
// empty() <=> HMM method
size_t part {1};
// >= 1
// <= parts
size_t parts {1};
// >= 1
VectorPtr<BlastAlignment> fusions;
bool fusionRedundant {false};
// Table FAM
string famId;
string gene;
// FAM.class
string resistance;
uchar reportable {0};
string classS;
string subclass;
//bool stxtyper {false};
const Fam* brFam {nullptr};
// Valid if !fromHmm and !inFam()
BlastRule completeBR;
BlastRule partialBR;
string product;
Vector<Locus> cdss;
static constexpr size_t mismatchTail_aa = 10; // PAR
const Susceptible* susceptible {nullptr};
// In accession2susceptible
// Only for the right organism
const HmmAlignment* hmmAl {nullptr};
BlastAlignment (const string &line,
bool targetProt_arg)
: Alignment (line, targetProt_arg, true)
, fromHmm (false)
{
try
{
try
{
// refName
product = rfindSplit (refName, '|');
classS = rfindSplit (refName, '|');
subclass = rfindSplit (refName, '|');
reportable = (uchar) str2<int> (rfindSplit (refName, '|'));
resistance = rfindSplit (refName, '|');
gene = rfindSplit (refName, '|'); // Reportable_vw.class
famId = rfindSplit (refName, '|'); // Reportable_vw.fam
parts = (size_t) str2<int> (rfindSplit (refName, '|'));
part = (size_t) str2<int> (rfindSplit (refName, '|'));
refAccession = rfindSplit (refName, '|');
str2<long> (refName); // gi: dummy
if (contains (refAccession, ':'))
{
QC_ASSERT (isMutationProt ());
const string geneMutation = rfindSplit (refAccession, ':');
const size_t pos = str2<size_t> (rfindSplit (refAccession, ':'));
ASSERT (refMutation. empty ());
refMutation = std::move (AmrMutation (pos, geneMutation));
QC_ASSERT (! refMutation. empty ());
refMutation. qc ();
}
}
catch (const exception &e)
{
throw runtime_error (string ("Bad AMRFinder database\n") + e. what () + "\n" + line);
}
QC_ASSERT (! refAccession. empty ());
refName = refAccession;
replace (product, '_', ' ');
replace (classS, '_', ' ');
replace (subclass, '_', ' ');
// BlastRule
// PD-2310
if (inFam ())
{
// PD-4856
ASSERT (! brFam);
// brFam
EXEC_ASSERT (brFam = getFam ());
while (brFam)
{
if (partial ())
{
#if 0
PRINT (brFam->id);
PRINT (brFam->genesymbol);
PRINT (brFam->partialBR);
PRINT (pIdentity ());
PRINT (refCoverage ());
#endif
if (passBlastRule (brFam->partialBR))
{
//cout << "match!" << endl;
break;
}
}
else
if (passBlastRule (brFam->completeBR))
{
#if 0
PRINT (brFam->id);
PRINT (brFam->genesymbol);
PRINT (brFam->completeBR);
PRINT (pIdentity ());
PRINT (refCoverage ());
#endif
break;
}
brFam = brFam->parent;
}
}
else
{
completeBR = defaultCompleteBR;
partialBR = defaultPartialBR;
}
partialDna = false;
constexpr size_t mismatchTailDna = 10; // PAR
if (! targetProt && targetEnd - targetStart >= 30) // PAR, PD-671
{
if (refStart > 0 && targetTail (true) <= mismatchTailDna) partialDna = true;
else if (refEnd < refLen && targetTail (false) <= mismatchTailDna) partialDna = true;
}
setTargetAlign ();
if (contains (targetSeq, "*"))
stopCodon = true;
IMPLY (! targetProt, (targetEnd - targetStart) % 3 == 0); // redundant ??
// PD-1280
if ( (! targetProt || targetStart < domain_min) // PD-2381
&& refStart == 0
&& charInSet (targetSeq [0], "LIV")
&& nident < targetAlign_aa
)
nident++;
if (! targetProt)
cdss. emplace_back (0, targetName, targetStart, targetEnd, targetStrand, partialDna, 0, noString, noString);
if (isMutationProt ())
if (const Vector<AmrMutation>* refMutations = findPtr (accession2mutations, refAccession))
{
if (verbose ())
cout << "AmrMutation protein found: " << refAccession << endl << line << endl;
setSeqChanges (*refMutations, 0);
if (verbose ())
cout << endl;
}
susceptible = findPtr (accession2susceptible, refAccession);
}
catch (...)
{
cout << line << endl;
throw;
}
}
BlastAlignment (const HmmAlignment& hmmAl_arg,
const BlastAlignment* best)
: fromHmm (true)
, famId (hmmAl_arg. fam->id)
, gene (hmmAl_arg. fam->id)
, product (hmmAl_arg. fam->familyName)
, hmmAl (& hmmAl_arg)
{ ASSERT (hmmAl_arg. good ());
targetName = hmmAl_arg. sseqid;
targetProt = true;
alProt = true;
if (allele ())
ERROR_MSG (famId + " " + gene);
if (best)
{
targetSeq = best->targetSeq;
targetStart = best->targetStart;
targetEnd = best->targetEnd;
targetLen = best->targetLen;
targetStrand = best->targetStrand;
refProt = true;
refName = best->refName;
refSeq = best->refSeq;
refStart = best->refStart;
refEnd = best->refEnd;
refLen = best->refLen;
alProt = true;
nident = best->nident;
targetAlign = best->targetAlign;
targetAlign_aa = best->targetAlign_aa;
refAccession = best->refAccession;
//stxtyper = best->stxtyper;
}
}
void qc () const override
{
if (! qc_on)
return;
Alignment::qc ();
if (empty ())
return;
QC_ASSERT (! famId. empty ());
QC_ASSERT (! gene. empty ());
QC_ASSERT (part >= 1);
QC_ASSERT (part <= parts);
//QC_IMPLY (part > 1, ! fusions. empty () || fusionRedundant);
QC_IMPLY (! fusions. empty (), parts >= 2);
QC_IMPLY (isMutationProt (), ! isSusceptibleProt ());
QC_IMPLY (susceptible, isSusceptibleProt ());
QC_IMPLY (isSusceptibleProt (), fusions. empty () && ! fusionRedundant);
QC_IMPLY (isMutationProt (), fusions. empty () && ! fusionRedundant);
QC_IMPLY (! fusions. empty (), fusions. size () >= 2);
QC_IMPLY (! fusions. empty (), fusions. front () == this);
QC_IMPLY (fusionRedundant, fusions. empty ());
QC_ASSERT (! product. empty ());
QC_IMPLY (! fromHmm, ! refAccession. empty ());
QC_ASSERT (refAccession. empty () == targetSeq. empty ());
QC_ASSERT (! refAccession. empty () == (bool) refLen);
QC_ASSERT (! refAccession. empty () == (bool) nident);
QC_IMPLY (refAccession. empty () && inFam (), ! getFam () -> hmm. empty ());
QC_IMPLY (targetProt, ! partialDna);
QC_ASSERT (targetAlign);
QC_IMPLY (targetProt, targetAlign == targetAlign_aa);
QC_IMPLY (! targetProt, targetAlign == 3 * targetAlign_aa);
QC_ASSERT (nident <= targetAlign_aa);
QC_IMPLY (! refAccession. empty (), targetAlign_aa <= targetSeq. size ());
QC_IMPLY (! refMutation. empty (), isMutationProt ());
#if 0
if (targetProt)
for (const Locus& cds : cdss)
QC_ASSERT ( cds. size () == 3 * targetLen + 3
|| cds. size () == 3 * targetLen
);
#endif
if (! targetProt)
{
for (const Locus& cds : cdss)
QC_ASSERT (cds. contig == targetName);
}
QC_IMPLY (! seqChanges. empty (), isMutationProt ());
QC_ASSERT ((fromHmm || inFam ()) == completeBR. empty ());
QC_ASSERT ((fromHmm || inFam ()) == partialBR. empty ());
}
void report (TsvOut &td,
const string &parentTargetName,
bool mutationAll) const
{ // PD-736, PD-774, PD-780, PD-799
const string proteinName (isMutationProt ()
? product
: susceptible
? susceptible->name
: refProtExactlyMatched (true) || parts >= 2 || refAccession. empty () // PD-3187, PD-3192
? product
: nvl (getMatchFam () -> familyName, na)
);
ASSERT (! contains (proteinName, '\t'));
Vector<Locus> cdss_ (cdss);
if (cdss_. empty ())
cdss_ << Locus ();
Vector<SeqChange> seqChanges_ (seqChanges);
if (seqChanges_. empty ())
seqChanges_ << SeqChange ();
for (const Locus& cds : cdss_)
{
if (targetProt)
{
if (parentTargetProt)
{ ASSERT (targetName == parentTargetName); }
else
if (cds. contig != parentTargetName)
continue;
}
else
{ ASSERT (targetName == parentTargetName); }
for (const SeqChange& seqChange : seqChanges_)
{
const string method (empty () ? na : getMethod (cds));
VectorPtr<AmrMutation> mutations (seqChange. mutations);
if (mutations. empty ())
mutations << nullptr;
for (const AmrMutation* mut : mutations)
{
IMPLY (! verbose (), isMutationProt () == ! (seqChange. empty () && ! mut));
const bool isMutation = ! seqChange. empty () && mut && ! seqChange. replacement;
if (mutationAll)
{
if (! isMutationProt ())
continue;
}
else if (isMutationProt () && ! isMutation)
continue;
if (! input_name. empty ())
td << input_name;;
td << (targetProt ? nvl(targetName, na) : na); // PD-2534
if (cdsExist)
td << (empty () ? na : (cds. contig. empty () ? nvl (targetName,na) : cds. contig))
<< (empty () ? 0 : (cds. contig. empty () ? targetStart : cds. start) + 1)
<< (empty () ? 0 : (cds. contig. empty () ? targetEnd : cds. stop))
<< (empty () ? na : (cds. contig. empty () ? (targetStrand ? "+" : "-") : (cds. strand ? "+" : "-")));
td << (isMutationProt ()
? mut
? seqChange. empty ()
? mut->wildtype ()
: mut->geneMutation
: gene + "_" + seqChange. getMutationStr ()
: fusion2geneSymbols ()
)
<< (isMutationProt ()
? mut
? seqChange. empty ()
? proteinName + " [WILDTYPE]"
: mut->name
: proteinName + " [UNKNOWN]"
: proteinName
)
<< (isMutationProt () || fusion2core () ? "core" : "plus"); // PD-2825
// PD-1856
if (isMutationProt ())
td << "AMR"
<< "POINT"
<< (mut ? nvl (mut->classS, na) : na)
<< (mut ? nvl (mut->subclass, na) : na);
else
td << nvl (fusion2type (), na)
<< nvl (fusion2subtype (), na)
<< nvl (fusion2class (), na)
<< nvl (fusion2subclass (), na);
td << method
<< (targetProt ? targetLen : targetAlign_aa);
if (refAccession. empty ())
td << na
<< na
<< na
<< na
<< na
<< na;
else
td << refLen
<< refCoverage () * 100.0
<< pIdentity () * 100.0 // refIdentity
<< targetSeq. size ()
<< refAccession
<< product
;
// PD-775
if (isMutationProt ())
td << na
<< na;
else
{
if (const HmmAlignment* hmmAl_ = fusion2hmmAl ())
td << hmmAl_->fam->hmm
<< hmmAl_->fam->familyName;
else
{
td << na
<< na;
ASSERT (method != "HMM");
}
}
if (cdsExist)
{
IMPLY (cds. crossOrigin, useCrossOrigin);
if (useCrossOrigin)
{
if (cds. crossOrigin)
td << cds. contigLen;
else
td << na;
}
}
if (print_node)
{
if (! print_node_raw && allele () && ! refProtExactlyMatched (true))
td << gene;
else
td << fusion2famIds ();
}
IMPLY (isMutationProt () && isMutation /*! seqChange. empty () && mut && ! seqChange. replacement*/, hasMutation ());
td. newLn ();
}
}
}
}
bool isMutationProt () const
{ return resistance == "mutation"; }
bool isSusceptibleProt () const
{ return resistance == "susceptible"; } // Similar to isMutationProt(): low identity <=> point mutations
bool inFam () const
{ return ! isMutationProt ()
&& ! isSusceptibleProt ();
}
#if 0
bool notInFam () const
{ return ! inFam (); }
#endif
Set<string> getMutationSymbols () const
{ Set<string> mutationSymbols;
for (const SeqChange& seqChange : seqChanges)
if (! seqChange. empty ())
for (const AmrMutation* mutation : seqChange. mutations)
mutationSymbols << mutation->geneMutation;
return mutationSymbols;
}
bool allele () const
{ return famId != gene && parts == 1; }
size_t refEffectiveLen () const
{ return partialDna ? refEnd - refStart : refLen; }
bool partial () const
// Requires: good()
{ return refCoverage () < defaultCompleteBR. ref_coverage - frac_delta; }
bool getTargetStrand (const Locus &cds) const
{ return targetProt
? cds. empty ()
? true
: cds. strand
: targetStrand;
}
size_t missedDnaStart (const Locus &cds) const
{ return 3 * (getTargetStrand (cds)
? refStart
: refLen - refEnd
);
}
size_t missedDnaStop (const Locus &cds) const
{ return 3 * (getTargetStrand (cds)
? refLen - refEnd
: refStart
);
}
bool truncated (const Locus &cds) const
{ return (missedDnaStart (cds) > 0 && (targetProt ? (cds. empty () ? false : cds. atContigStart ()) : targetStart <= Locus::end_delta))
|| (missedDnaStop (cds) > 0 && (targetProt ? (cds. empty () ? false : cds. atContigStop ()) : targetLen - targetEnd <= Locus::end_delta));
}
bool truncatedCds () const
{ for (const Locus& cds : cdss)
if (truncated (cds))
return true;
return false;
}
bool alleleMatch () const
{ return refProtExactlyMatched (true) && allele (); }
bool alleleReportable () const // PD-3583
{ return alleleMatch () && reportable >= 2; }
uchar getReportable () const
{ if (const Fam* f = getMatchFam ())
return f->reportable;
return 0;
}
uchar fusion2reportable () const
{ ASSERT (! isMutationProt ());
if (susceptible)
return 2;
if (fusions. empty ())
return getReportable ();
uchar reportable_max = 0;
for (const BlastAlignment* fusion : fusions)
maximize (reportable_max, fusion->getReportable ());
return reportable_max;
}
private:
string getGeneSymbol () const
{ return susceptible
? susceptible->genesymbol
: alleleMatch ()
? famId
: nvl (getMatchFam () -> genesymbol, na);
}
public:
string fusion2geneSymbols () const
{ ASSERT (! isMutationProt ());
if (fusions. empty ())
return getGeneSymbol ();
string s;
for (const BlastAlignment* fusion : fusions)
add (s, "/" /*fusion_infix*/, fusion->getGeneSymbol ()); // PD-5155 ??
return s;
}
string fusion2famIds () const
{ if (isMutationProt () || fusions. empty ())
return famId;
string s;
for (const BlastAlignment* fusion : fusions)
add (s, fusion_infix, fusion->famId);
return s;
}
private:
bool isCore () const
{ return fusion2reportable () >= 2 || alleleReportable (); }
public:
bool fusion2core () const
{ ASSERT (! isMutationProt ());
if (fusions. empty ())
return isCore ();
for (const BlastAlignment* fusion : fusions)
if (fusion->isCore ())
return true;
return false;
}
private:
string getType () const
{ return susceptible ? "AMR" : getMatchFam () -> type; }
public:
string fusion2type () const
{ ASSERT (! isMutationProt ());
if (fusions. empty ())
return getType ();
StringVector vec;
for (const BlastAlignment* fusion : fusions)
vec << fusion->getType ();
vec. sort ();
vec. uniq ();
return vec. toString ("/");
}
private:
string getSubtype () const
{ return susceptible ? "AMR" : getMatchFam () -> subtype; }
public:
string fusion2subtype () const
{ ASSERT (! isMutationProt ());
if (fusions. empty ())
return getSubtype ();
StringVector vec;
for (const BlastAlignment* fusion : fusions)
vec << fusion->getSubtype ();
vec. sort ();
vec. uniq ();
return vec. toString ("/");
}
private:
string getClass () const
{ if (alleleMatch () && ! subclass. empty ()) // class may be empty()
return classS;
return susceptible ? susceptible->classS : getMatchFam () -> classS;
}
public:
string fusion2class () const
{ ASSERT (! isMutationProt ());
if (fusions. empty ())
return getClass ();
StringVector vec;
for (const BlastAlignment* fusion : fusions)
vec << std::move (StringVector (fusion->getClass (), '/', true));
vec. sort ();
vec. uniq ();
return vec. toString ("/");
}
private:
string getSubclass () const
{ if (alleleMatch () && ! subclass. empty ())
return subclass;
return susceptible ? susceptible->subclass : getMatchFam () -> subclass;
}
public:
string fusion2subclass () const
{ ASSERT (! isMutationProt ());
if (fusions. empty ())
return getSubclass ();
StringVector vec;
for (const BlastAlignment* fusion : fusions)
vec << std::move (StringVector (fusion->getSubclass (), '/', true));
vec. sort ();
vec. uniq ();
return vec. toString ("/");
}
const HmmAlignment* fusion2hmmAl () const
{ if (fusions. empty ())
return hmmAl;
for (const BlastAlignment* fusion : fusions)
if (fusion->hmmAl)
return fusion->hmmAl;
return nullptr;
}
string getMethod (const Locus &cds) const
{ //IMPLY (refExactlyMatched () && ! mutation_all. get (), ! isMutationProt ())
string method (fromHmm
? "HMM"
: isMutationProt ()
? "POINT"
: refProtExactlyMatched (true)
? alleleMatch ()
? "ALLELE"
: "EXACT" // PD-776
: partial ()
? truncated (cds)
? "PARTIAL_CONTIG_END" // PD-2267
: "PARTIAL"
: "BLAST"
);
// PD-2088, PD-2320
bool suffix = true;
if ( ( method == "BLAST"
|| method == "PARTIAL"
|| method == "PARTIAL_CONTIG_END"
)
//&& ! targetProt
)
{
if (stopCodon)
{
method = "INTERNAL_STOP";
suffix = false;
}
#if 0
else if (frameShift)
{
method = "FRAME_SHIFT";
suffix = false;
}
#endif
}
if (suffix && method != "HMM")
method += (targetProt ? "P" : "X");
return method;
}
// PD-736
bool passBlastRule (const BlastRule &br) const
{ ASSERT (! br. empty ());
return pIdentity () >= br. ident - frac_delta
&& refCoverage () >= br. ref_coverage - frac_delta;
}
bool partialPseudo () const
{ return partial ()
&& ! cdss. empty ()
&& ! truncatedCds ();
}
bool pseudo () const
{ return stopCodon
|| partialPseudo ();
}
bool good () const
{ ASSERT (! fromHmm);
if (refAccession. empty ())
return true;
//if (! refMutation. empty ()) // PD-4981: drop