File size: 33,733 Bytes
b7731cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright 2012 by Wibowo Arindrarto.  All rights reserved.
#
# This file is part of the Biopython distribution and governed by your
# choice of the "Biopython License Agreement" or the "BSD 3-Clause License".
# Please see the LICENSE file that should have been included as part of this
# package.

"""Bio.SearchIO parser for BLAST+ tab output format, with or without comments."""

import re

from Bio.SearchIO._index import SearchIndexer
from Bio.SearchIO._model import QueryResult, Hit, HSP, HSPFragment


__all__ = ("BlastTabIndexer", "BlastTabParser", "BlastTabWriter")


# longname-shortname map
# maps the column names shown in a commented output to its short name
# (the one used in the command line)
_LONG_SHORT_MAP = {
    "query id": "qseqid",
    "query acc.": "qacc",
    "query acc.ver": "qaccver",
    "query length": "qlen",
    "subject id": "sseqid",
    "subject acc.": "sacc",
    "subject acc.ver": "saccver",
    "subject length": "slen",
    "alignment length": "length",
    "bit score": "bitscore",
    "score": "score",
    "evalue": "evalue",
    "identical": "nident",
    "% identity": "pident",
    "positives": "positive",
    "% positives": "ppos",
    "mismatches": "mismatch",
    "gaps": "gaps",
    "q. start": "qstart",
    "q. end": "qend",
    "s. start": "sstart",
    "s. end": "send",
    "query frame": "qframe",
    "sbjct frame": "sframe",
    "query/sbjct frames": "frames",
    "query seq": "qseq",
    "subject seq": "sseq",
    "gap opens": "gapopen",
    "query gi": "qgi",
    "subject ids": "sallseqid",
    "subject gi": "sgi",
    "subject gis": "sallgi",
    "BTOP": "btop",
    "subject accs.": "sallacc",
    "subject tax ids": "staxids",
    "subject sci names": "sscinames",
    "subject com names": "scomnames",
    "subject blast names": "sblastnames",
    "subject super kingdoms": "sskingdoms",
    "subject title": "stitle",
    "subject titles": "salltitles",
    "subject strand": "sstrand",
    "% subject coverage": "qcovs",
    "% hsp coverage": "qcovhsp",
}


# function to create a list from semicolon-delimited string
# used in BlastTabParser._parse_result_row
def _list_semicol(s):
    return s.split(";")


def _list_diamond(s):
    return s.split("<>")


# column to class attribute map
_COLUMN_QRESULT = {
    "qseqid": ("id", str),
    "qacc": ("accession", str),
    "qaccver": ("accession_version", str),
    "qlen": ("seq_len", int),
    "qgi": ("gi", str),
}
_COLUMN_HIT = {
    "sseqid": ("id", str),
    "sallseqid": ("id_all", _list_semicol),
    "sacc": ("accession", str),
    "saccver": ("accession_version", str),
    "sallacc": ("accession_all", _list_semicol),
    "sgi": ("gi", str),
    "sallgi": ("gi_all", str),
    "slen": ("seq_len", int),
    "staxids": ("tax_ids", _list_semicol),
    "sscinames": ("sci_names", _list_semicol),
    "scomnames": ("com_names", _list_semicol),
    "sblastnames": ("blast_names", _list_semicol),
    "sskingdoms": ("super_kingdoms", _list_semicol),
    "stitle": ("title", str),
    "salltitles": ("title_all", _list_diamond),
    # set strand as HSP property?
    "sstrand": ("strand", str),
    "qcovs": ("query_coverage", float),
}
_COLUMN_HSP = {
    "bitscore": ("bitscore", float),
    "score": ("bitscore_raw", int),
    "evalue": ("evalue", float),
    "nident": ("ident_num", int),
    "pident": ("ident_pct", float),
    "positive": ("pos_num", int),
    "ppos": ("pos_pct", float),
    "mismatch": ("mismatch_num", int),
    "gaps": ("gap_num", int),
    "gapopen": ("gapopen_num", int),
    "btop": ("btop", str),
    "qcovhsp": ("query_coverage", float),
}
_COLUMN_FRAG = {
    "length": ("aln_span", int),
    "qstart": ("query_start", int),
    "qend": ("query_end", int),
    "sstart": ("hit_start", int),
    "send": ("hit_end", int),
    "qframe": ("query_frame", int),
    "sframe": ("hit_frame", int),
    "frames": ("frames", str),
    "qseq": ("query", str),
    "sseq": ("hit", str),
}
_SUPPORTED_FIELDS = set(
    list(_COLUMN_QRESULT) + list(_COLUMN_HIT) + list(_COLUMN_HSP) + list(_COLUMN_FRAG)
)

# column order in the non-commented tabular output variant
# values must be keys inside the column-attribute maps above
_DEFAULT_FIELDS = [
    "qseqid",
    "sseqid",
    "pident",
    "length",
    "mismatch",
    "gapopen",
    "qstart",
    "qend",
    "sstart",
    "send",
    "evalue",
    "bitscore",
]
# one field from each of the following sets must exist in order for the
# parser to work
_MIN_QUERY_FIELDS = {"qseqid", "qacc", "qaccver"}
_MIN_HIT_FIELDS = {"sseqid", "sacc", "saccver", "sallseqid"}

# simple function to create BLAST HSP attributes that may be computed if
# other certain attributes are present
# This was previously implemented in the HSP objects in the old model

_RE_GAPOPEN = re.compile(r"\w-")


def _compute_gapopen_num(hsp):
    """Return the number of gap openings in the given HSP (PRIVATE)."""
    gapopen = 0
    for seq_type in ("query", "hit"):
        seq = str(getattr(hsp, seq_type).seq)
        gapopen += len(re.findall(_RE_GAPOPEN, seq))
    return gapopen


def _augment_blast_hsp(hsp, attr):
    """Calculate the given HSP attribute, for writing (PRIVATE)."""
    if not hasattr(hsp, attr) and not attr.endswith("_pct"):

        # aln_span is number of identical matches + mismatches + gaps
        if attr == "aln_span":
            hsp.aln_span = hsp.ident_num + hsp.mismatch_num + hsp.gap_num

        # ident and gap requires the num values to be computed first
        elif attr.startswith("ident"):
            setattr(hsp, attr, hsp.aln_span - hsp.mismatch_num - hsp.gap_num)
        elif attr.startswith("gap"):
            setattr(hsp, attr, hsp.aln_span - hsp.ident_num - hsp.mismatch_num)

        elif attr == "mismatch_num":
            setattr(hsp, attr, hsp.aln_span - hsp.ident_num - hsp.gap_num)

        elif attr == "gapopen_num":
            if not hasattr(hsp, "query") or not hasattr(hsp, "hit"):
                raise AttributeError
            hsp.gapopen_num = _compute_gapopen_num(hsp)

    # if the attr is a percent value, calculate it
    if attr == "ident_pct":
        hsp.ident_pct = hsp.ident_num / hsp.aln_span * 100

    elif attr == "pos_pct":
        hsp.pos_pct = hsp.pos_num / hsp.aln_span * 100

    elif attr == "gap_pct":
        hsp.gap_pct = hsp.gap_num / hsp.aln_span * 100


class BlastTabParser:
    """Parser for the BLAST tabular format."""

    def __init__(self, handle, comments=False, fields=_DEFAULT_FIELDS):
        """Initialize the class."""
        self.handle = handle
        self.has_comments = comments
        self.fields = self._prep_fields(fields)
        self.line = self.handle.readline().strip()

    def __iter__(self):
        """Iterate over BlastTabParser, yields query results."""
        # stop iteration if file has no lines
        if not self.line:
            return
        # determine which iterator to use
        elif self.has_comments:
            iterfunc = self._parse_commented_qresult
        else:
            if self.line.startswith("#"):
                raise ValueError(
                    "Encountered unexpected character '#' at the beginning of a line. "
                    "Set comments=True if the file is a commented file."
                )
            iterfunc = self._parse_qresult

        yield from iterfunc()

    def _prep_fields(self, fields):
        """Validate and format the given fields for use by the parser (PRIVATE)."""
        # cast into list if fields is a space-separated string
        if isinstance(fields, str):
            fields = fields.strip().split(" ")
        # blast allows 'std' as a proxy for the standard default lists
        # we want to transform 'std' to its proper column names
        if "std" in fields:
            idx = fields.index("std")
            fields = fields[:idx] + _DEFAULT_FIELDS + fields[idx + 1 :]
        # if set(fields) has a null intersection with minimum required
        # fields for hit and query, raise an exception
        if not set(fields).intersection(_MIN_QUERY_FIELDS) or not set(
            fields
        ).intersection(_MIN_HIT_FIELDS):
            raise ValueError("Required query and/or hit ID field not found.")

        return fields

    def _parse_commented_qresult(self):
        """Yield ``QueryResult`` objects from a commented file (PRIVATE)."""
        while True:
            comments = self._parse_comments()
            if comments:
                try:
                    self.fields = comments["fields"]
                    # iterator for the query results
                    qres_iter = self._parse_qresult()
                except KeyError:
                    # no fields means the query has no results
                    assert "fields" not in comments
                    # create an iterator returning one empty qresult
                    # if the query has no results
                    qres_iter = iter([QueryResult()])

                for qresult in qres_iter:
                    for key, value in comments.items():
                        setattr(qresult, key, value)
                    yield qresult

            else:
                break

    def _parse_comments(self):
        """Return a dictionary containing tab file comments (PRIVATE)."""
        comments = {}
        while True:
            # parse program and version
            # example: # BLASTX 2.2.26+
            if "BLAST" in self.line and "processed" not in self.line:
                program_line = self.line[len(" #") :].split(" ")
                comments["program"] = program_line[0].lower()
                comments["version"] = program_line[1]
            # parse query id and description (if available)
            # example: # Query: gi|356995852 Mus musculus POU domain
            elif "Query" in self.line:
                query_line = self.line[len("# Query: ") :].split(" ", 1)
                comments["id"] = query_line[0]
                if len(query_line) == 2:
                    comments["description"] = query_line[1]
            # parse target database
            # example: # Database: db/minirefseq_protein
            elif "Database" in self.line:
                comments["target"] = self.line[len("# Database: ") :]
            # parse RID (from remote searches)
            elif "RID" in self.line:
                comments["rid"] = self.line[len("# RID: ") :]
            # parse column order, required for parsing the result lines
            # example: # Fields: query id, query gi, query acc., query length
            elif "Fields" in self.line:
                comments["fields"] = self._parse_fields_line()
            # if the line has these strings, it's either the end of a comment
            # or the end of a file, so we return all the comments we've parsed
            elif " hits found" in self.line or "processed" in self.line:
                self.line = self.handle.readline().strip()
                return comments

            self.line = self.handle.readline()

            if not self.line:
                return comments
            else:
                self.line = self.line.strip()

    def _parse_fields_line(self):
        """Return column short names line from 'Fields' comment line (PRIVATE)."""
        raw_field_str = self.line[len("# Fields: ") :]
        long_fields = raw_field_str.split(", ")
        fields = [_LONG_SHORT_MAP[long_name] for long_name in long_fields]
        return self._prep_fields(fields)

    def _parse_result_row(self):
        """Return a dictionary of parsed row values (PRIVATE)."""
        fields = self.fields
        columns = self.line.strip().split("\t")
        if len(fields) != len(columns):
            raise ValueError(
                "Expected %i columns, found: %i" % (len(fields), len(columns))
            )

        qresult, hit, hsp, frag = {}, {}, {}, {}
        for idx, value in enumerate(columns):
            sname = fields[idx]
            # flag to check if any of the _COLUMNs contain sname
            in_mapping = False
            # iterate over each dict, mapping pair to determine
            # attribute name and value of each column
            for parsed_dict, mapping in (
                (qresult, _COLUMN_QRESULT),
                (hit, _COLUMN_HIT),
                (hsp, _COLUMN_HSP),
                (frag, _COLUMN_FRAG),
            ):
                # process parsed value according to mapping
                if sname in mapping:
                    attr_name, caster = mapping[sname]
                    if caster is not str:
                        value = caster(value)
                    parsed_dict[attr_name] = value
                    in_mapping = True
            # make sure that any unhandled field is not supported
            if not in_mapping:
                assert sname not in _SUPPORTED_FIELDS

        return {"qresult": qresult, "hit": hit, "hsp": hsp, "frag": frag}

    def _get_id(self, parsed):
        """Return the value used for a QueryResult or Hit ID from a parsed row (PRIVATE)."""
        # use 'id', with 'id_all', 'accession' and 'accession_version'
        # fallbacks one of these must have a value since we've checked whether
        # they exist or not when parsing the comments
        id_cache = parsed.get("id")
        if id_cache is None and "id_all" in parsed:
            id_cache = parsed.get("id_all")[0]
        if id_cache is None:
            id_cache = parsed.get("accession")
        if id_cache is None:
            id_cache = parsed.get("accession_version")

        return id_cache

    def _parse_qresult(self):
        """Yield QueryResult objects (PRIVATE)."""
        # state values, used to determine what to do with each line
        state_EOF = 0
        state_QRES_NEW = 1
        state_QRES_SAME = 3
        state_HIT_NEW = 2
        state_HIT_SAME = 4
        # dummies for initial states
        qres_state = None
        hit_state = None
        file_state = None
        cur_qid = None
        cur_hid = None
        # dummies for initial id caches
        prev_qid = None
        prev_hid = None
        # dummies for initial parsed value containers
        cur, prev = None, None
        hit_list, hsp_list = [], []

        while True:
            # store previous line's parsed values if we've past the first line
            if cur is not None:
                prev = cur
                prev_qid = cur_qid
                prev_hid = cur_hid
            # only parse the line if it's not EOF or not a comment line
            if self.line and not self.line.startswith("#"):
                cur = self._parse_result_row()
                cur_qid = self._get_id(cur["qresult"])
                cur_hid = self._get_id(cur["hit"])
            else:
                file_state = state_EOF
                # mock values for cur_qid and cur_hid since the line is empty
                cur_qid, cur_hid = None, None

            # get the state of hit and qresult
            if prev_qid != cur_qid:
                qres_state = state_QRES_NEW
            else:
                qres_state = state_QRES_SAME
            # new hits are hits with different id or hits in a new qresult
            if prev_hid != cur_hid or qres_state == state_QRES_NEW:
                hit_state = state_HIT_NEW
            else:
                hit_state = state_HIT_SAME

            # we're creating objects for the previously parsed line(s),
            # so nothing is done in the first parsed line (prev == None)
            if prev is not None:
                # every line is essentially an HSP with one fragment, so we
                # create both of these for every line
                frag = HSPFragment(prev_hid, prev_qid)
                for attr, value in prev["frag"].items():
                    # adjust coordinates to Python range
                    # NOTE: this requires both start and end coords to be
                    # present, otherwise a KeyError will be raised.
                    # Without this limitation, we might misleadingly set the
                    # start / end coords
                    for seq_type in ("query", "hit"):
                        if attr == seq_type + "_start":
                            value = min(value, prev["frag"][seq_type + "_end"]) - 1
                        elif attr == seq_type + "_end":
                            value = max(value, prev["frag"][seq_type + "_start"])
                    setattr(frag, attr, value)
                # strand and frame setattr require the full parsed values
                # to be set first
                for seq_type in ("hit", "query"):
                    # try to set hit and query frame
                    frame = self._get_frag_frame(frag, seq_type, prev["frag"])
                    setattr(frag, "%s_frame" % seq_type, frame)
                    # try to set hit and query strand
                    strand = self._get_frag_strand(frag, seq_type, prev["frag"])
                    setattr(frag, "%s_strand" % seq_type, strand)

                hsp = HSP([frag])
                for attr, value in prev["hsp"].items():
                    setattr(hsp, attr, value)
                hsp_list.append(hsp)

                # create hit and append to temp hit container if hit_state
                # says we're not at the same hit or at a new query
                if hit_state == state_HIT_NEW:
                    hit = Hit(hsp_list)
                    for attr, value in prev["hit"].items():
                        if attr != "id_all":
                            setattr(hit, attr, value)
                        else:
                            # not setting hit ID since it's already set from the
                            # prev_hid above
                            setattr(hit, "_id_alt", value[1:])
                    hit_list.append(hit)
                    hsp_list = []
                # create qresult and yield if we're at a new qresult or EOF
                if qres_state == state_QRES_NEW or file_state == state_EOF:
                    qresult = QueryResult(hit_list, prev_qid)
                    for attr, value in prev["qresult"].items():
                        setattr(qresult, attr, value)
                    yield qresult
                    # if current line is EOF, break
                    if file_state == state_EOF:
                        break
                    hit_list = []

            self.line = self.handle.readline().strip()

    def _get_frag_frame(self, frag, seq_type, parsedict):
        """Return fragment frame for given object (PRIVATE).

        Returns ``HSPFragment`` frame given the object, its sequence type,
        and its parsed dictionary values.
        """
        assert seq_type in ("query", "hit")
        frame = getattr(frag, "%s_frame" % seq_type, None)
        if frame is not None:
            return frame
        else:
            if "frames" in parsedict:
                # frames is 'x1/x2' string, x1 is query frame, x2 is hit frame
                idx = 0 if seq_type == "query" else 1
                return int(parsedict["frames"].split("/")[idx])
            # else implicit None return

    def _get_frag_strand(self, frag, seq_type, parsedict):
        """Return fragment strand for given object (PRIVATE).

        Returns ``HSPFragment`` strand given the object, its sequence type,
        and its parsed dictionary values.
        """
        # NOTE: this will never set the strands as 0 for protein
        # queries / hits, since we can't detect the blast flavors
        # from the columns alone.
        assert seq_type in ("query", "hit")
        strand = getattr(frag, "%s_strand" % seq_type, None)
        if strand is not None:
            return strand
        else:
            # using parsedict instead of the fragment object since
            # we need the unadjusted coordinated values
            start = parsedict.get("%s_start" % seq_type)
            end = parsedict.get("%s_end" % seq_type)
            if start is not None and end is not None:
                return 1 if start <= end else -1
            # else implicit None return


class BlastTabIndexer(SearchIndexer):
    """Indexer class for BLAST+ tab output."""

    _parser = BlastTabParser

    def __init__(self, filename, comments=False, fields=_DEFAULT_FIELDS):
        """Initialize the class."""
        SearchIndexer.__init__(self, filename, comments=comments, fields=fields)

        # if the file doesn't have comments,
        # get index of column used as the key (qseqid / qacc / qaccver)
        if not self._kwargs["comments"]:
            if "qseqid" in fields:
                self._key_idx = fields.index("qseqid")
            elif "qacc" in fields:
                self._key_idx = fields.index("qacc")
            elif "qaccver" in fields:
                self._key_idx = fields.index("qaccver")
            else:
                raise ValueError(
                    "Custom fields is missing an ID column. One of these must be "
                    "present: 'qseqid', 'qacc', or 'qaccver'."
                )

    def __iter__(self):
        """Iterate over the file handle; yields key, start offset, and length."""
        handle = self._handle
        handle.seek(0)

        if not self._kwargs["comments"]:
            iterfunc = self._qresult_index
        else:
            iterfunc = self._qresult_index_commented

        for key, offset, length in iterfunc():
            yield key.decode(), offset, length

    def _qresult_index_commented(self):
        """Indexer for commented BLAST tabular files (PRIVATE)."""
        handle = self._handle
        handle.seek(0)
        start_offset = 0
        # mark of a new query
        query_mark = None
        # mark of the query's ID
        qid_mark = b"# Query: "
        # mark of the last line
        end_mark = b"# BLAST processed"

        while True:
            end_offset = handle.tell()
            line = handle.readline()

            if query_mark is None:
                query_mark = line
                start_offset = end_offset
            elif line.startswith(qid_mark):
                qresult_key = line[len(qid_mark) :].split()[0]
            elif line == query_mark or line.startswith(end_mark):
                yield qresult_key, start_offset, end_offset - start_offset
                start_offset = end_offset
            elif not line:
                break

    def _qresult_index(self):
        """Indexer for noncommented BLAST tabular files (PRIVATE)."""
        handle = self._handle
        handle.seek(0)
        start_offset = 0
        qresult_key = None
        key_idx = self._key_idx

        while True:
            # get end offset here since we only know a qresult ends after
            # encountering the next one
            end_offset = handle.tell()
            # line = handle.readline()
            line = handle.readline()

            if qresult_key is None:
                qresult_key = line.split(b"\t")[key_idx]
            else:
                try:
                    curr_key = line.split(b"\t")[key_idx]
                except IndexError:
                    curr_key = b""

                if curr_key != qresult_key:
                    yield qresult_key, start_offset, end_offset - start_offset
                    qresult_key = curr_key
                    start_offset = end_offset

            # break if we've reached EOF
            if not line:
                break

    def get_raw(self, offset):
        """Return the raw bytes string of a QueryResult object from the given offset."""
        if self._kwargs["comments"]:
            getfunc = self._get_raw_qresult_commented
        else:
            getfunc = self._get_raw_qresult

        return getfunc(offset)

    def _get_raw_qresult(self, offset):
        """Return the raw bytes string of a single QueryResult from a noncommented file (PRIVATE)."""
        handle = self._handle
        handle.seek(offset)
        qresult_raw = b""
        key_idx = self._key_idx
        qresult_key = None

        while True:
            line = handle.readline()
            # get the key if the first line (qresult key)
            if qresult_key is None:
                qresult_key = line.split(b"\t")[key_idx]
            else:
                try:
                    curr_key = line.split(b"\t")[key_idx]
                except IndexError:
                    curr_key = b""
                # only break when qresult is finished (key is different)
                if curr_key != qresult_key:
                    break
            # append to the raw string as long as qresult is the same
            qresult_raw += line

        return qresult_raw

    def _get_raw_qresult_commented(self, offset):
        """Return the bytes raw string of a single QueryResult from a commented file (PRIVATE)."""
        handle = self._handle
        handle.seek(offset)
        qresult_raw = b""
        end_mark = b"# BLAST processed"

        # query mark is the line marking a new query
        # something like '# TBLASTN 2.2.25+'
        query_mark = None
        line = handle.readline()
        while line:
            # since query_mark depends on the BLAST search, we need to obtain it
            # first
            if query_mark is None:
                query_mark = line
            # break when we've reached the next qresult or the search ends
            elif line == query_mark or line.startswith(end_mark):
                break

            qresult_raw += line
            line = handle.readline()

        return qresult_raw


class BlastTabWriter:
    """Writer for blast-tab output format."""

    def __init__(self, handle, comments=False, fields=_DEFAULT_FIELDS):
        """Initialize the class."""
        self.handle = handle
        self.has_comments = comments
        self.fields = fields

    def write_file(self, qresults):
        """Write to the handle, return how many QueryResult objects were written."""
        handle = self.handle
        qresult_counter, hit_counter, hsp_counter, frag_counter = 0, 0, 0, 0

        for qresult in qresults:
            if self.has_comments:
                handle.write(self._build_comments(qresult))
            if qresult:
                handle.write(self._build_rows(qresult))
                if not self.has_comments:
                    qresult_counter += 1
                hit_counter += len(qresult)
                hsp_counter += sum(len(hit) for hit in qresult)
                frag_counter += sum(len(hit.fragments) for hit in qresult)
            # if it's commented and there are no hits in the qresult, we still
            # increment the counter
            if self.has_comments:
                qresult_counter += 1

        # commented files have a line saying how many queries were processed
        if self.has_comments:
            handle.write("# BLAST processed %i queries" % qresult_counter)

        return qresult_counter, hit_counter, hsp_counter, frag_counter

    def _build_rows(self, qresult):
        """Return a string containing tabular rows of the QueryResult object (PRIVATE)."""
        coordinates = {"qstart", "qend", "sstart", "send"}
        qresult_lines = ""
        for hit in qresult:
            for hsp in hit:
                line = []
                for field in self.fields:
                    # get the column value ~ could either be an attribute
                    # of qresult, hit, or hsp
                    if field in _COLUMN_QRESULT:
                        value = getattr(qresult, _COLUMN_QRESULT[field][0])
                    elif field in _COLUMN_HIT:
                        if field == "sallseqid":
                            value = getattr(hit, "id_all")
                        else:
                            value = getattr(hit, _COLUMN_HIT[field][0])
                    # special case, since 'frames' can be determined from
                    # query frame and hit frame
                    elif field == "frames":
                        value = "%i/%i" % (hsp.query_frame, hsp.hit_frame)
                    elif field in _COLUMN_HSP:
                        try:
                            value = getattr(hsp, _COLUMN_HSP[field][0])
                        except AttributeError:
                            attr = _COLUMN_HSP[field][0]
                            _augment_blast_hsp(hsp, attr)
                            value = getattr(hsp, attr)
                    elif field in _COLUMN_FRAG:
                        value = getattr(hsp, _COLUMN_FRAG[field][0])
                    else:
                        assert field not in _SUPPORTED_FIELDS
                        continue

                    # adjust from and to according to strand, if from and to
                    # is included in the output field
                    if field in coordinates:
                        value = self._adjust_coords(field, value, hsp)
                    # adjust output formatting
                    value = self._adjust_output(field, value)

                    line.append(value)

                hsp_line = "\t".join(line)
                qresult_lines += hsp_line + "\n"

        return qresult_lines

    def _adjust_coords(self, field, value, hsp):
        """Adjust start and end coordinates according to strand (PRIVATE)."""
        assert field in ("qstart", "qend", "sstart", "send")
        # determine sequence type to operate on based on field's first letter
        seq_type = "query" if field.startswith("q") else "hit"

        strand = getattr(hsp, "%s_strand" % seq_type, None)
        if strand is None:
            raise ValueError(
                "Required attribute %r not found." % ("%s_strand" % (seq_type))
            )
        # switch start <--> end coordinates if strand is -1
        if strand < 0:
            if field.endswith("start"):
                value = getattr(hsp, "%s_end" % seq_type)
            elif field.endswith("end"):
                value = getattr(hsp, "%s_start" % seq_type) + 1
        elif field.endswith("start"):
            # adjust start coordinate for positive strand
            value += 1

        return value

    def _adjust_output(self, field, value):
        """Adjust formatting of given field and value to mimic native tab output (PRIVATE)."""
        # qseq and sseq are stored as SeqRecord, but here we only need the str
        if field in ("qseq", "sseq"):
            value = str(value.seq)

        # evalue formatting, adapted from BLAST+ source:
        # src/objtools/align_format/align_format_util.cpp#L668
        elif field == "evalue":
            if value < 1.0e-180:
                value = "0.0"
            elif value < 1.0e-99:
                value = "%2.0e" % value
            elif value < 0.0009:
                value = "%3.0e" % value
            elif value < 0.1:
                value = "%4.3f" % value
            elif value < 1.0:
                value = "%3.2f" % value
            elif value < 10.0:
                value = "%2.1f" % value
            else:
                value = "%5.0f" % value

        # pident and ppos formatting
        elif field in ("pident", "ppos"):
            value = "%.2f" % value

        # evalue formatting, adapted from BLAST+ source:
        # src/objtools/align_format/align_format_util.cpp#L723
        elif field == "bitscore":
            if value > 9999:
                value = "%4.3e" % value
            elif value > 99.9:
                value = "%4.0d" % value
            else:
                value = "%4.1f" % value

        # coverages have no comma (using floats still ~ a more proper
        # representation)
        elif field in ("qcovhsp", "qcovs"):
            value = "%.0f" % value

        # list into '<>'-delimited string
        elif field == "salltitles":
            value = "<>".join(value)

        # list into ';'-delimited string
        elif field in (
            "sallseqid",
            "sallacc",
            "staxids",
            "sscinames",
            "scomnames",
            "sblastnames",
            "sskingdoms",
        ):
            value = ";".join(value)

        # everything else
        else:
            value = str(value)

        return value

    def _build_comments(self, qres):
        """Return QueryResult tabular comment as a string (PRIVATE)."""
        comments = []
        # inverse mapping of the long-short name map, required
        # for writing comments
        inv_field_map = {v: k for k, v in _LONG_SHORT_MAP.items()}

        # try to anticipate qress without version
        program = qres.program.upper()
        try:
            version = qres.version
        except AttributeError:
            program_line = "# %s" % program
        else:
            program_line = f"# {program} {version}"
        comments.append(program_line)
        # description may or may not be None
        if qres.description is None:
            comments.append("# Query: %s" % qres.id)
        else:
            comments.append(f"# Query: {qres.id} {qres.description}")
        # try appending RID line, if present
        try:
            comments.append("# RID: %s" % qres.rid)
        except AttributeError:
            pass
        comments.append("# Database: %s" % qres.target)
        # qresults without hits don't show the Fields comment
        if qres:
            comments.append(
                "# Fields: %s"
                % ", ".join(inv_field_map[field] for field in self.fields)
            )
        comments.append("# %i hits found" % len(qres))

        return "\n".join(comments) + "\n"


# if not used as a module, run the doctest
if __name__ == "__main__":
    from Bio._utils import run_doctest

    run_doctest()