-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstxtyper.cpp
1233 lines (1134 loc) · 42.9 KB
/
stxtyper.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
// stxtyper.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:
* stx typing using protein reference sequences
*
* Dependencies: NCBI BLAST, gunzip (optional)
*
* Release changes:
* 1.0.27 10/23/2024 PD-5155 "Hierarchy node" with mixed types is <stx1>::<stx2>
* 1.0.26 10/22/2024 PD-5085 Change column "Element length" to "Target length"
* 1.0.25 08/16/2024 PD-5085 AMRFinderPlus column names to match MicroBIGG-E
* 1.0.24 08/05/2024 PD-5076 "na" -> "NA"
* 1.0.23 07/29/2024 PD-5064 AMBIGUOUS operon type
* 1.0.22 07/25/2024 First codon L|I|V -> M
* 1.0.21 07/15/2024 PD-5038 --nucleotide_output
* 1.0.20 05/21/2024 PD-5002 {A|B}_reference_subtype
* 1.0.19 03/26/2024 BlastAlignment::targetAlign is removed
* 1.0.18 03/19/2024 PD-4910 Element symbol is <stx type>_operon, Element name contains operon quality attribute"
Sequence name -> Element name in header
Sequence name, now Element name, should be type/subtype and include info when not complete e.g.,:
stx1a operon
stx2c operon
Partial stx2 operon
stx2 operon with frameshift
Novel stx2 operon
stx2 operon with internal stop
Element subtype should be STX_TYPE
Subclass should be to type only where element symbol is to type only
E.g. a partial stx2a should have Element symbol of stx2 and a subclass of STX2
Target length should be in nucleotide sequence coordinates
Reference sequence length and % coverage of reference sequence should be blank
* 1.0.17 03/18/2024 PD-4910
* 1.0.16 03/13/2024 PD-4926 --amrfinder: <stx type>_operon, "Gene symbol" -> "Element symbol"
* 1.0.15 03/11/2024 PD-4924 dead stxA2j EFK5907329.1 is replaced by EMA1832120.1
* 1.0.14 03/05/2024 PD-4918 --print_node: print AMRFinderPlus hierarchy node
* 1.0.13 03/05/2024 PD-4910 --amrfinder prints output in the AMRFinderPlus format
* 1.0.12 02/29/2024 TsvOut.live() is used
* 1.0.11 02/28/2024 PD-4911 wrong QC for log file output
* PD-4897 single-subunit operons are de-redundified
* 1.0.10 02/22/2024 PD-4901 multiple types for the same protein sequence are allowed; only Flemming's reference proteins are used
* 1.0.9 02/16/2024 PD-4901 new database (includes Flemming's data)
* 1.0.8 02/16/2024 PD-4892, PD-4898 steps: (1) find operons where A subtype = B subtype and operon identity >= threshold
* (2) find other operons where operon identity >= threshold for each subunit
* (3) find other operons with relaxed intergenic region size
* 1.0.7 02.15/2024 PD-4897 extend intergenic region for partial operons
* 1.0.6 02/13/2024 PD-4874 --translation_table is removed
* 02/13/2024 PD-4894 EXTENDED operon type
* 02/13/2024 PD-4892 Subunits A and B are not preferred to be of the same stx class
* 1.0.5 02/12/2024 PD-4891 -v == --version
* stable choice of a blast hit among equivalent ones
* 1.0.4 02/08/2024 PD-4891 stable choice of a blast hit among equivalent ones
* 02/08/2024 PD-4887 a stand-alone blast hit suppresses a covered blast hit if the reported gene symbol is the same
* "STANDARD" -> "COMPLETE"
* PARTIAL_CONTIG_END is detected for separate A and B subunits
* 1.0.3 02/07/2024 PD-4874 strand is '+' or '-'
* 1.0.2 02/06/2024 blastx -> tblastn
* 1.0.1 02/05/2024 PD-4874 github.com/vbrover/stxtyper
* 1.0.0 11/21/2023 PD-4798
*
*/
#ifdef _MSC_VER
#error "UNIX is required"
#endif
#undef NDEBUG
#include "common.hpp"
#include "tsv.hpp"
using namespace Common_sp;
#include "amrfinder_columns.hpp"
#include "common.inc"
namespace
{
string input_name;
bool amrfinder = false;
bool print_node = false;
map<string,double> stxClass2identity;
// PAR
constexpr size_t intergenic_max {36}; // Max. intergenic region in the reference set + 2
constexpr size_t slack = 30;
const string stxS ("stx");
const string na ("NA");
string stxType_reported_operon2elementName (const string &stxType_reported,
const string &operon)
{
string elementName (stxType_reported + " operon");
if (operon == "FRAMESHIFT")
elementName += " with frameshift";
else if (operon == "INTERNAL_STOP")
elementName += " with internal stop";
else if (contains (operon, "PARTIAL"))
elementName = "Partial " + elementName;
else if (operon == "EXTENDED")
elementName = "Extended " + elementName;
else if (contains (operon, "NOVEL"))
elementName = "Novel " + elementName;
return elementName;
}
struct BlastAlignment
{
size_t length {0}, nident {0}, positives {0} // aa
, refStart {0}, refEnd {0}, refLen {0}
, targetStart {0}, targetEnd {0}, targetLen {0};
// Positions are 0-based
// targetStart < targetEnd
bool stopCodon {false};
bool frameshift {false};
// target
string targetName;
string targetSeq;
bool targetStrand {true};
// false <=> negative
//size_t targetAlign {0};
// bp
// Reference
// Whole sequence ends with '*'
string refAccession;
string refSeq;
// Function of refAccession
string stxType;
string stxClass;
// Function of stxType
string stxSuperClass;
// Function of stxClass
char subunit {'\0'};
// 'A' or 'B'
string subClass; // = as in AMRFinderPlus report
bool reported {false};
BlastAlignment (const string &line)
{
{
string sseqid;
{
istringstream iss (line);
iss >> targetName >> sseqid >> targetStart >> targetEnd >> targetLen >> refStart >> refEnd >> refLen >> targetSeq >> refSeq;
// format: qseqid sseqid qstart qend qlen sstart send slen qseq sseq
// blast: 62285 63017 88215 105 837 837
}
QC_ASSERT (! targetSeq. empty ());
string famId;
try
{
subClass = rfindSplit (sseqid, '|');
famId = rfindSplit (sseqid, '|');
refAccession = rfindSplit (sseqid, '|');
}
catch (const exception &e)
{
throw runtime_error (string ("Bad StxTyper database\n") + e. what () + "\n" + line);
}
QC_ASSERT (famId. size () == 6);
QC_ASSERT (isLeft (famId, stxS));
subunit = famId [3];
stxType = famId. substr (4);
}
ASSERT (stxType. size () == 2);
length = targetSeq. size ();
nident = 0;
positives = 0;
QC_ASSERT (targetSeq. size () == refSeq. size ());
FFOR (size_t, i, targetSeq. size ())
if (targetSeq [i] == refSeq [i])
{
nident++;
positives++;
}
else
if ( targetSeq [i] == 'X'
|| refSeq [i] == 'X'
)
positives++;
if ( refStart == 0
&& charInSet (targetSeq [0], "LIV")
&& refSeq [0] == 'M'
)
{
nident++;
positives++;
}
stxClass = stxType;
if ( stxType == "2a"
|| stxType == "2c"
|| stxType == "2d"
)
stxClass = "2";
stxSuperClass = stxClass. substr (0, 1);
QC_ASSERT (refStart < refEnd);
QC_ASSERT (targetStart != targetEnd);
targetStrand = targetStart < targetEnd;
if (! targetStrand)
swap (targetStart, targetEnd);
QC_ASSERT (refStart >= 1);
QC_ASSERT (targetStart >= 1);
refStart--;
targetStart--;
//targetAlign = targetEnd - targetStart;
//QC_ASSERT (targetAlign_aa % 3 == 0);
//targetAlign_aa /= 3;
const size_t stopCodonPos = targetSeq. find ('*');
if (stopCodonPos != string::npos && stopCodonPos + 1 < targetSeq. size ())
stopCodon = true;
}
void qc () const
{
if (! qc_on)
return;
QC_ASSERT (length);
QC_ASSERT (nident);
QC_ASSERT (nident <= positives);
//QC_ASSERT (nident <= length);
QC_ASSERT (positives <= length);
QC_ASSERT (targetStart < targetEnd);
QC_ASSERT (targetEnd <= targetLen);
QC_ASSERT (refStart < refEnd);
QC_ASSERT (refEnd <= refLen);
if (! frameshift)
{
QC_ASSERT (positives <= refEnd - refStart);
QC_ASSERT (refEnd - refStart <= length);
}
QC_ASSERT (! targetName. empty ());
QC_ASSERT (contains (stxClass2identity, stxClass));
QC_ASSERT (isLeft (stxType, stxClass));
QC_ASSERT (subunit == 'A' || subunit == 'B');
QC_ASSERT (subClass. size () > stxS. size ());
QC_ASSERT (! refAccession. empty ());
QC_ASSERT (! targetSeq. empty ());
QC_ASSERT (! refSeq. empty ());
QC_ASSERT (targetSeq. size () == refSeq. size ());
QC_IMPLY (! frameshift, length == targetSeq. size ());
QC_ASSERT (stxType. size () == 2);
}
void saveTsvOut (TsvOut& td,
bool verboseP) const
{ if (! td. live ())
return;
const string stxType_reported (verboseP ? getGenesymbol () : (stxS + stxType. substr (0, 1)));
const string operon (frameshift
? "FRAMESHIFT"
: stopCodon
? "INTERNAL_STOP"
: truncated () || otherTruncated ()
? "PARTIAL_CONTIG_END"
: verboseP && getRelCoverage () == 1.0
? "COMPLETE_SUBUNIT"
: getExtended ()
? "EXTENDED"
: "PARTIAL"
);
const char strand (targetStrand ? '+' : '-');
const double refCoverage = getRelCoverage () * 100.0;
const double refIdentity = getIdentity () * 100.0;
// td
if (! input_name. empty ())
td << input_name;
if (amrfinder)
{
const string subunitS (1, subunit);
string subclass (stxType_reported /*stxS + stxType*/);
strUpper (subclass);
td << na // 1 "Protein identifier"
<< targetName // 2 "Contig id"
<< targetStart + 1 // 3 "Start"
<< targetEnd // 4 "Stop"
<< strand // 5 "Strand"
<< stxType_reported + "_operon" // 6 "Element symbol"
<< stxType_reported_operon2elementName (stxType_reported, operon) // 7 "Element name"
<< "plus" // 8 "Scope"
<< "VIRULENCE" // 9 "Element type"
<< "STX_TYPE" //10 "Element subtype"
<< subclass. substr (0, 4) //11 "Class"
<< subclass //12 "Subclass"
<< operon //13 "Method"
<< targetEnd - targetStart /*targetAlign*/ //14 "Target length"
<< noString /*refLen*/ //15 "Reference sequence length"
<< noString /*refCoverage*/ //16 "% Coverage of reference sequence"
<< refIdentity //17 "% Identity to reference sequence"
<< length //18 "Alignment length"
<< refAccession //19 "Accession of closest sequence"
<< "Shiga toxin " + stxS + stxType + " subunit " + subunitS //20 "Name of closest sequence"
<< na //21 "HMM id"
<< na //22 "HMM description"
;
if (print_node)
td << getGenesymbol ();
}
else
{
td << targetName
<< stxType_reported
<< operon
<< noString
<< targetStart + 1
<< targetEnd
<< strand;
if (subunit == 'B')
td << noString
<< noString
<< noString
<< noString;
td << refAccession
<< subClass
<< refIdentity
<< refCoverage;
if (subunit == 'A')
td << noString
<< noString
<< noString
<< noString;
}
td. newLn ();
}
string getGenesymbol () const
{ return stxS + subunit + stxType; }
void merge (const BlastAlignment &prev)
{ ASSERT (targetName == prev. targetName);
ASSERT (refAccession == prev. refAccession);
ASSERT (targetStrand == prev. targetStrand);
ASSERT (targetLen == prev. targetLen);
ASSERT (refLen == prev. refLen);
ASSERT (targetStart > prev. targetStart);
targetStart = prev. targetStart;
if (targetStrand)
refStart = prev. refStart;
else
refEnd = prev. refEnd;
// Approximately
length += prev. length;
nident += prev. nident;
positives += prev. positives;
//targetAlign += prev. targetAlign;
//
if (prev. stopCodon)
stopCodon = true;
frameshift = true;
}
size_t getFrame () const
{ return (targetStart % 3) + 1; }
double getIdentity () const
{ return (double) nident / (double) (length); }
size_t getAbsCoverage () const
{ return refEnd - refStart; }
double getRelCoverage () const
{ return (double) getAbsCoverage () / (double) refLen; }
size_t getDiff () const
{ return refStart + (refLen - refEnd) + (length - nident); }
bool truncated () const
{ return (targetStart <= 3 /*Locus::end_delta*/ && ((targetStrand && refStart) || (! targetStrand && refEnd + 1 < refLen)))
|| (targetLen - targetEnd <= 3 /*Locus::end_delta*/ && ((targetStrand && refEnd + 1 < refLen) || (! targetStrand && refStart)));
}
bool otherTruncated () const
{ constexpr size_t missed_max = intergenic_max + 3 * 20 /*min. domain length*/; // PAR
return (targetStrand == (subunit == 'B') && targetStart <= missed_max)
|| (targetStrand == (subunit == 'A') && targetLen - targetEnd <= missed_max);
}
bool getExtended () const // On C-terminus
{ ASSERT (! truncated ());
return ! refStart && refEnd + 1 == refLen;
}
bool insideEq (const BlastAlignment &other) const
{ return targetStart >= other. targetStart
&& targetEnd <= other. targetEnd;
}
string refMap (size_t len) const
{ QC_ASSERT (refLen <= len);
string s = string (refStart, '-');
FFOR (size_t, i, refSeq. size ())
if (refSeq [i] != '-')
s += targetSeq [i];
s += string (len - refEnd, '-');
return s;
}
bool ambig () const
{ return nident < positives; }
static bool frameshiftLess (const BlastAlignment* a,
const BlastAlignment* b)
{ ASSERT (a);
ASSERT (b);
ASSERT (! a->reported);
ASSERT (! b->reported);
LESS_PART (*a, *b, targetName);
LESS_PART (*a, *b, targetStrand);
LESS_PART (*a, *b, refAccession);
LESS_PART (*a, *b, targetStart);
LESS_PART (*a, *b, targetEnd);
return false;
}
static bool sameTypeLess (const BlastAlignment* a,
const BlastAlignment* b)
{ ASSERT (a);
ASSERT (b);
LESS_PART (*a, *b, reported);
LESS_PART (*a, *b, targetName);
LESS_PART (*a, *b, targetStrand);
LESS_PART (*a, *b, stxClass);
LESS_PART (*a, *b, subunit);
LESS_PART (*a, *b, targetStart);
LESS_PART (*a, *b, getDiff ());
LESS_PART (*a, *b, refAccession);
return false;
}
static bool less (const BlastAlignment* a,
const BlastAlignment* b)
// = sameTypeLess(), but without stxClass
{ ASSERT (a);
ASSERT (b);
//LESS_PART (*a, *b, reported);
LESS_PART (*a, *b, targetName);
LESS_PART (*a, *b, targetStrand);
LESS_PART (*a, *b, subunit);
LESS_PART (*a, *b, targetStart);
LESS_PART (*a, *b, getDiff ());
LESS_PART (*a, *b, refAccession);
return false;
}
static bool reportLess (const BlastAlignment* a,
const BlastAlignment* b)
{ ASSERT (a);
ASSERT (b);
LESS_PART (*a, *b, reported);
LESS_PART (*a, *b, targetName);
LESS_PART (*a, *b, targetStrand);
LESS_PART (*b, *a, getAbsCoverage ());
LESS_PART (*a, *b, getDiff ());
LESS_PART (*a, *b, targetStart);
LESS_PART (*a, *b, refAccession);
return false;
}
};
struct Operon
{
const BlastAlignment* al1 {nullptr};
// !nullptr
const BlastAlignment* al2 {nullptr};
// al1->targetEnd < al2->targetStart
Operon () = default;
Operon (const BlastAlignment& al1_arg,
const BlastAlignment& al2_arg)
: al1 (& al1_arg)
, al2 (& al2_arg)
{}
explicit Operon (const BlastAlignment& al1_arg)
: al1 (& al1_arg)
{}
void qc () const
{ if (! qc_on)
return;
QC_ASSERT (al1);
al1->qc ();
QC_ASSERT (al1->reported);
if (! al2)
return;
al2->qc ();
QC_ASSERT (al1->targetName == al2->targetName);
QC_ASSERT (al1->targetStrand == al2->targetStrand);
QC_ASSERT (al1->targetEnd < al2->targetStart);
QC_ASSERT (al1->subunit != al2->subunit);
QC_ASSERT (al2->reported);
}
void saveTsvOut (TsvOut& td,
bool verboseP) const
{ ASSERT (al1);
if (! td. live ())
return;
if (al2)
{
string stxType (getStxType (verboseP));
const string standard ("COMPLETE");
const bool novel = al1->stxClass != al2->stxClass
|| getIdentity () < stxClass2identity [al1->stxClass]
|| stxType. size () <= 1;
const string operonType = getA () -> frameshift
|| getB () -> frameshift
? "FRAMESHIFT"
: getA () -> stopCodon
|| getB () -> stopCodon
? "INTERNAL_STOP"
: getA () -> truncated ()
|| getB () -> truncated ()
? "PARTIAL_CONTIG_END"
: partial ()
? "PARTIAL" // Complete ??
: getA () -> getExtended ()
|| getB () -> getExtended ()
? "EXTENDED" // Complete ??
: novel
? ambig ()
? "AMBIGUOUS"
: standard + "_NOVEL"
: standard;
if (! verboseP)
{
ASSERT (stxType. size () <= 2);
if (operonType == standard)
{ ASSERT (stxType. size () == 2); }
else
if (stxType. size () == 2)
stxType. erase (1);
}
const string targetName (al1->targetName);
const size_t start = al1->targetStart + 1;
const size_t stop = al2->targetEnd;
const char strand (al1->targetStrand ? '+' : '-');
const string stxType_reported (stxS + stxType);
const double refIdentity = getIdentity () * 100.0;
// td
if (! input_name. empty ())
td << input_name;
if (amrfinder)
{
const string genesymbol (al1->stxType == al2->stxType ? stxS + al1->stxType : stxType_reported);
string subclass (stxType_reported /*genesymbol*/);
strUpper (subclass);
const size_t targetAlign = al2->targetEnd - al1->targetStart;
//const size_t refLen = al1->refLen + al2->refLen;
//const double refCoverage = double (al1->getAbsCoverage () + al2->getAbsCoverage ()) / double (refLen) * 100.0;
const size_t alignmentLen = al1->length + al2->length;
const string refAccessions (al1->refAccession + ", " + al2->refAccession);
const string fam (al1->getGenesymbol () + fusion_infix + al2->getGenesymbol ());
td << na // 1 "Protein identifier"
<< targetName // 2 "Contig id"
<< start // 3 "Start"
<< stop // 4 "Stop"
<< strand // 5 "Strand"
<< stxType_reported + "_operon" // 6 "Element symbol"
<< stxType_reported_operon2elementName (stxType_reported, operonType) // 7 "Element name"
<< "plus" // 8 "Scope"
<< "VIRULENCE" // 9 "Element type"
<< "STX_TYPE" //10 "Element subtype"
<< subclass. substr (0, 4) //11 "Class"
<< subclass //12 "Subclass"
<< operonType //13 "Method"
<< targetAlign //14 "Target length"
<< noString /*refLen*/ //15 "Reference sequence length"
<< noString /*refCoverage*/ //16 "% Coverage of reference sequence"
<< refIdentity //17 "% Identity to reference sequence"
<< alignmentLen //18 "Alignment length"
<< refAccessions //19 "Accession of closest sequence"
<< "Shiga toxin " + genesymbol //20 "Name of closest sequence"
<< na //21 "HMM id"
<< na //22 "HMM description"
;
if (print_node)
td << fam;
}
else
td << targetName
<< stxType_reported
<< operonType
<< refIdentity
<< start
<< stop
<< strand
// Approximately if frameshift
<< getA () -> refAccession
<< getA () -> subClass
<< getA () -> getIdentity () * 100.0
<< getA () -> getRelCoverage () * 100.0
<< getB () -> refAccession
<< getB () -> subClass
<< getB () -> getIdentity () * 100.0
<< getB () -> getRelCoverage () * 100.0
;
td. newLn ();
}
else
al1->saveTsvOut (td, verboseP);
}
private:
const BlastAlignment* getA () const
{ return al1->targetStrand ? al1 : al2; }
const BlastAlignment* getB () const
{ return al1->targetStrand ? al2 : al1; }
bool hasAl2 () const
{ return al2; }
string getRefAccession2 () const
{ if (al2)
return al2->refAccession;
return noString;
}
string getStxType (bool verboseP) const
{ if (! al2)
return al1->stxType;
if (al1->stxClass != al2->stxClass)
{
//return al1->stxClass + fusion_infix + al2->stxClass; // order alphabetically
if (al1->stxSuperClass == al2->stxSuperClass)
return al1->stxSuperClass;
return noString;
}
if (al1->stxClass != "2")
return al1->stxType;
const string a (getA () -> refMap (319 + 1));
const string b (getB () -> refMap ( 89 + 1));
if ( (a [312] == 'F' || a [312] == 'S')
&& (a [318] == 'K' || a [318] == 'E')
&& b [34] == 'D'
)
return "2a";
if ( a [312] == 'F'
&& (a [318] == 'K' || a [318] == 'E')
&& b [34] == 'N'
)
return "2c";
if ( a [312] == 'S'
&& a [318] == 'E'
&& b [34] == 'N'
)
return "2d";
if (verboseP)
return string ("2 ") + a [312] + a [318] + b [34];
return "2";
}
bool partial () const
{ return (getA () -> getRelCoverage () < 1.0 && ! getA () -> getExtended ())
|| (getB () -> getRelCoverage () < 1.0 && ! getB () -> getExtended ());
}
bool ambig () const
{ return getA () -> ambig ()
|| getB () -> ambig ();
}
public:
double getIdentity () const
{ return double (al1->nident + al2->nident) / double (al1->length/*refLen*/ + al2->length/*refLen*/); }
bool insideEq (const Operon &other) const
{ return al1->targetStrand == other. al1->targetStrand
&& al1->targetStart + slack >= other. al1->targetStart
&& al2->targetEnd <= other. al2->targetEnd + slack;
}
bool operator< (const Operon &other) const
{ LESS_PART (*this, other, al1->targetName);
LESS_PART (other, *this, getIdentity ());
LESS_PART (*this, other, hasAl2 ());
LESS_PART (*this, other, al1->refAccession);
LESS_PART (*this, other, hasAl2 ());
LESS_PART (*this, other, getRefAccession2 ());
return false;
}
static bool reportLess (const Operon &a,
const Operon &b)
{ LESS_PART (a, b, al1->targetName);
LESS_PART (a, b, al1->targetStart);
LESS_PART (a, b, al1->targetEnd);
LESS_PART (b, a, al1->targetStrand);
LESS_PART (a, b, al1->refAccession);
LESS_PART (a, b, hasAl2 ());
LESS_PART (a, b, getRefAccession2 ());
return false;
}
};
void goodBlasts2operons (const VectorPtr<BlastAlignment> &goodBlastAls,
Vector<Operon> &operons,
bool sameType,
bool strong,
TsvOut &logTd)
{
IMPLY (sameType, strong);
LOG ("\nGood blasts:");
size_t start = 0;
FFOR (size_t, i, goodBlastAls. size ())
{
const BlastAlignment* alB = goodBlastAls [i];
ASSERT (alB);
if (alB->reported)
continue;
alB->saveTsvOut (logTd, true);
if (alB->subunit != 'B')
continue;
while ( start < i
&& ! ( goodBlastAls [start] -> targetName == alB->targetName
&& goodBlastAls [start] -> targetStrand == alB->targetStrand
&& ( ! sameType
|| goodBlastAls [start] -> stxClass == alB->stxClass
)
)
)
start++;
FOR_START (size_t, j, start, i)
{
const BlastAlignment* alA = goodBlastAls [j];
ASSERT (alA);
if (alA->reported)
continue;
ASSERT (alA->targetName == alB->targetName);
ASSERT (alA->targetStrand == alB->targetStrand);
IMPLY (sameType, alA->stxClass == alB->stxClass);
ASSERT (alA->subunit <= alB->subunit);
if (alA->subunit == alB->subunit)
break;
ASSERT (alA->subunit == 'A');
const BlastAlignment* al1 = alA;
const BlastAlignment* al2 = alB;
if (! al1->targetStrand)
swap (al1, al2);
if ( al1->targetEnd <= al2->targetStart
&& al2->targetStart - al1->targetEnd <= intergenic_max * (strong ? 1 : 2) // PAR // PD-4897
)
{
Operon op (*al1, *al2);
LOG ("Operon:\t" + to_string (op. getIdentity ()) + "\t" + to_string (stxClass2identity [op. al1->stxClass]));
op. saveTsvOut (logTd, true);
if ( ! strong
|| ( op. getIdentity () >= stxClass2identity [op. al1->stxClass]
&& op. getIdentity () >= stxClass2identity [op. al2->stxClass]
)
)
{
operons << std::move (op);
var_cast (al1) -> reported = true;
var_cast (al2) -> reported = true;
}
}
}
}
LOG ("# Operons: " + to_string (operons. size ()));
LOG ("\nSuppress goodBlastAls by operons");
for (const BlastAlignment* al : goodBlastAls)
{
ASSERT (al);
if (! al->reported)
for (const Operon& op : operons)
{
ASSERT (op. al2);
if ( al->targetName == op. al1->targetName
&& al->targetStart + slack >= op. al1->targetStart
&& al->targetEnd <= op. al2->targetEnd + slack
&& al->targetStrand == op. al1->targetStrand
)
{
var_cast (al) -> reported = true;
break;
}
}
}
}
// ThisApplication
struct ThisApplication : ShellApplication
{
ThisApplication ()
: ShellApplication ("Determine stx type(s) of a genome, print .tsv-file", true, false, true, true)
{
addKey ("nucleotide", "Input nucleotide FASTA file (can be gzipped)", "", 'n', "NUC_FASTA");
//addKey ("translation_table", "NCBI genetic code for translated BLAST", "11", 't', "TRANSLATION_TABLE");
addKey ("name", "Text to be added as the first column \"name\" to all rows of the report, for example it can be an assembly name", "", '\0', "NAME");
addKey ("output", "Write output to OUTPUT_FILE instead of STDOUT", "", 'o', "OUTPUT_FILE");
addKey ("blast_bin", "Directory for BLAST. Deafult: $BLAST_BIN", "", '\0', "BLAST_DIR");
addFlag ("amrfinder", "Print output in the nucleotide AMRFinderPlus format");
addFlag ("print_node", "Print AMRFinderPlus hierarchy node");
addKey ("nucleotide_output", "Output nucleotide FASTA file of reported nucleotide sequences", "", '\0', "NUC_FASTA_OUT");
version = SVN_REV;
}
void shellBody () const final
{
const string fName = shellQuote (getArg ("nucleotide"));
const uint gencode = /*arg2uint ("translation_table")*/ 11;
input_name = getArg ("name");
const string output = getArg ("output");
string blast_bin = getArg ("blast_bin");
amrfinder = getFlag ("amrfinder");
print_node = getFlag ("print_node");
const string dna_out = shellQuote (getArg ("nucleotide_output"));
if (contains (input_name, '\t'))
throw runtime_error ("NAME cannot contain a tab character");
if (print_node && ! amrfinder)
throw runtime_error ("--print_node requires --amrfinder");
stderr << "Software directory: " << shellQuote (execDir) << '\n';
stderr << "Version: " << version << '\n';
const string logFName (tmp + "/log");
const string qcS (qc_on ? " -qc" : "");
#define BLASTX 0
// blast_bin
if (blast_bin. empty ())
if (const char* s = getenv ("BLAST_BIN"))
blast_bin = string (s);
if (! blast_bin. empty ())
{
addDirSlash (blast_bin);
#if BLASTX
prog2dir ["blastx"] = blast_bin;
#else
prog2dir ["tblastn"] = blast_bin;
prog2dir ["makeblastdb"] = blast_bin;
#endif
}
const string dna_flat = uncompress (fName, "dna_flat");
#if BLASTX
size_t nDna = 0;
size_t dnaLen_max = 0;
size_t dnaLen_total = 0;
#endif
{
prog2dir ["fasta_check"] = execDir;
exec (fullProg ("fasta_check") + dna_flat + " -hyphen -ambig " + qcS + " -log " + logFName + " > " + tmp + "/nseq", logFName);
const StringVector vec (tmp + "/nseq", (size_t) 10, true);
if (vec. size () != 3)
throw runtime_error ("fasta_check failed: " + vec. toString ("\n"));
#if BLASTX
nDna = str2<size_t> (vec [0]);
dnaLen_max = str2<size_t> (vec [1]);
dnaLen_total = str2<size_t> (vec [2]);
#endif
}
#if BLASTX
QC_ASSERT (nDna);
QC_ASSERT (dnaLen_max);
QC_ASSERT (dnaLen_total);
#endif
//stderr. section ("Running blast");
const string blastOut (tmp + "/blast");
{
const Chronometer_OnePass_cerr cop ("blast");
// Database: created by ~brovervv/code/database/stx.prot.sh
#if BLASTX
findProg ("blastx");
const string blast_fmt ("-outfmt '6 qseqid sseqid qstart qend qlen sstart send slen qseq sseq'");
exec (fullProg ("blastx") + " -query " + dna_flat + " -db " + execDir + "stx.prot "
+ "-comp_based_stats 0 -evalue 1e-10 -seg no -max_target_seqs 10000 -word_size 5 -query_gencode " + to_string (gencode) + " "
+ getBlastThreadsParam ("blastx", min (nDna, dnaLen_total / 10002))
+ " " + blast_fmt + " -out " + blastOut + " > /dev/null 2> " + tmp + "/blast-err", tmp + "/blast-err");
#else
findProg ("makeblastdb");
exec (fullProg ("makeblastdb") + "-in " + dna_flat + " -dbtype nucl -out " + tmp + "/db -logfile " + tmp + "/db.log > /dev/null", tmp + "db.log");
findProg ("tblastn");
const string blast_fmt ("-outfmt '6 sseqid qseqid sstart send slen qstart qend qlen sseq qseq'");
exec (fullProg ("tblastn") + " -query " + execDir + "stx.prot -db " + tmp + "/db "
+ "-comp_based_stats 0 -evalue 1e-10 -seg no -max_target_seqs 10000 -word_size 5 -db_gencode " + to_string (gencode)
//+ " -task tblastn-fast -threshold 100 -window_size 15" // from amrfinder.cpp: Reduces time by 9%
//+ " -num_threads 10 -mt_mode 1" // Reduces time by 30%
+ " " + blast_fmt + " -out " + blastOut + " > /dev/null 2> " + tmp + "/blast-err", tmp + "/blast-err");
#endif
}
// stxClass2identity[]
stxClass2identity ["1a"] = 0.983;
stxClass2identity ["1c"] = 0.983;
stxClass2identity ["1d"] = 0.983;
stxClass2identity ["1e"] = 0.983;
stxClass2identity ["2"] = 0.98;
stxClass2identity ["2b"] = 0.98;
stxClass2identity ["2e"] = 0.98;
stxClass2identity ["2f"] = 0.98;
stxClass2identity ["2g"] = 0.98;
stxClass2identity ["2h"] = 0.98;
stxClass2identity ["2i"] = 0.98;
stxClass2identity ["2j"] = 0.98;
stxClass2identity ["2k"] = 0.985;
stxClass2identity ["2l"] = 0.985;
stxClass2identity ["2m"] = 0.98;
stxClass2identity ["2n"] = 0.98;
stxClass2identity ["2o"] = 0.98;
const string tmpOut (tmp + "/out");
OFStream fOut (tmpOut);
TsvOut td (& fOut, 2, false);
TsvOut logTd (logPtr, 2, false);
if (! input_name. empty ())
td << "name";
if (amrfinder)
{
td << /* 1*/ prot_colName
<< /* 2*/ contig_colName
<< /* 3*/ start_colName
<< /* 4*/ stop_colName
<< /* 5*/ strand_colName
<< /* 6*/ genesymbol_colName
<< /* 7*/ elemName_colName
<< /* 8*/ scope_colName
<< /* 9*/ type_colName
<< /*10*/ subtype_colName
<< /*11*/ class_colName
<< /*12*/ subclass_colName
<< /*13*/ method_colName
<< /*14*/ targetLen_colName
<< /*15*/ refLen_colName
<< /*16*/ refCov_colName
<< /*17*/ refIdent_colName
<< /*18*/ alignLen_colName
<< /*19*/ closestRefAccession_colName
<< /*20*/ closestRefName_colName
<< /*21*/ hmmAccession_colName
<< /*22*/ hmmDescr_colName
;
if (print_node)
td << hierarchyNode_colName;
}
else
td << "target_contig"
<< "stx_type"
<< "operon"
<< "identity"
<< "target_start"
<< "target_stop"
<< "target_strand"
<< "A_reference"
<< "A_reference_subtype"
<< "A_identity"
<< "A_coverage"
<< "B_reference"
<< "B_reference_subtype"
<< "B_identity"
<< "B_coverage"
;
td. newLn ();