File: test_internal.py

package info (click to toggle)
python-dnaio 1.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 524 kB
  • sloc: python: 2,726; ansic: 164; sh: 28; makefile: 15
file content (814 lines) | stat: -rw-r--r-- 28,945 bytes parent folder | download
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
import gzip
import io
import os
import shutil
import subprocess
import sys
from io import BytesIO
from pathlib import Path
from tempfile import mkdtemp
from textwrap import dedent

import pytest
from pytest import raises, mark

import dnaio
from dnaio import (
    BamReader,
    FileFormatError,
    FastaFormatError,
    FastqFormatError,
    FastaReader,
    FastqReader,
    InterleavedPairedEndReader,
    FastaWriter,
    FastqWriter,
    InterleavedPairedEndWriter,
    TwoFilePairedEndReader,
    records_are_mates,
    record_names_match,
    SequenceRecord,
)
from dnaio.writers import FileWriter
from dnaio.readers import BinaryFileReader
from dnaio._core import bytes_ascii_check

TEST_DATA = Path(__file__).parent / "data"
SIMPLE_FASTQ = str(TEST_DATA / "simple.fastq")
# files tests/data/simple.fast{q,a}
simple_fastq = [
    SequenceRecord("first_sequence", "SEQUENCE1", ":6;;8<=:<"),
    SequenceRecord("second_sequence", "SEQUENCE2", "83<??:(61"),
]

simple_fasta = [SequenceRecord(x.name, x.sequence, None) for x in simple_fastq]

tiny_fastq = b"@r1\nACG\n+\nHHH\n@r2\nT\n+\n#\n"


class TestFastaReader:
    def test_file(self):
        with FastaReader("tests/data/simple.fasta") as f:
            reads = list(f)
        assert reads == simple_fasta

    def test_bytesio(self) -> None:
        fasta = BytesIO(b">first_sequence\nSEQUENCE1\n>second_sequence\nSEQUENCE2\n")
        reads = list(FastaReader(fasta))
        assert reads == simple_fasta

    def test_with_comments(self) -> None:
        fasta = BytesIO(
            dedent(
                """
            # a comment
            # another one
            >first_sequence
            SEQUENCE1
            >second_sequence
            SEQUENCE2
            """
            ).encode()
        )
        reads = list(FastaReader(fasta))
        assert reads == simple_fasta

    def test_wrong_format(self) -> None:
        fasta = BytesIO(
            dedent(
                """# a comment
            # another one
            unexpected
            >first_sequence
            SEQUENCE1
            >second_sequence
            SEQUENCE2
            """
            ).encode()
        )
        with raises(FastaFormatError) as info:
            list(FastaReader(fasta))
        assert info.value.line == 2

    def test_fastareader_keeplinebreaks(self) -> None:
        with FastaReader("tests/data/simple.fasta", keep_linebreaks=True) as f:
            reads = list(f)
        assert reads[0] == simple_fasta[0]
        assert reads[1].sequence == "SEQUEN\nCE2"

    def test_context_manager(self) -> None:
        filename = "tests/data/simple.fasta"
        with open(filename, "rb") as f:
            assert not f.closed
            with dnaio.open(f) as inner_f:
                list(inner_f)
            assert not f.closed
        assert f.closed

        with FastaReader(filename) as sr:
            tmp_sr = sr
            assert not sr._file.closed
            _ = list(sr)
            assert not sr._file.closed
        assert tmp_sr._file is None
        # Open it a second time
        with FastaReader(filename):
            pass


class TestFastqReader:
    def test_fastqreader(self) -> None:
        with FastqReader(SIMPLE_FASTQ) as f:
            reads = list(f)
        assert reads == simple_fastq

    @mark.parametrize("buffer_size", [1, 2, 3, 5, 7, 10, 20])
    def test_fastqreader_buffersize(self, buffer_size) -> None:
        with FastqReader("tests/data/simple.fastq", buffer_size=buffer_size) as f:
            reads = list(f)
        assert reads == simple_fastq

    def test_fastqreader_buffersize_too_small(self) -> None:
        with raises(ValueError) as e:
            with FastqReader("tests/data/simple.fastq", buffer_size=0) as f:
                _ = list(f)  # pragma: no cover
        assert "buffer size too small" in e.value.args[0]

    def test_fastqreader_dos(self) -> None:
        # DOS line breaks
        with open("tests/data/dos.fastq", "rb") as f:
            assert b"\r\n" in f.read()
        with FastqReader("tests/data/dos.fastq") as f:
            dos_reads = list(f)
        with FastqReader("tests/data/small.fastq") as f:
            unix_reads = list(f)
        assert dos_reads == unix_reads

    def test_fastq_wrongformat(self) -> None:
        with raises(FastqFormatError) as info:
            with FastqReader("tests/data/withplus.fastq") as f:
                list(f)  # pragma: no cover
        assert info.value.line == 2

    def test_empty_fastq(self) -> None:
        with FastqReader(BytesIO(b"")) as fq:
            assert list(fq) == []

    @mark.parametrize(
        "s,line",
        [
            (b"@", 0),
            (b"@r", 0),
            (b"@r1", 0),
            (b"@r1\n", 1),
            (b"@r1\nA", 1),
            (b"@r1\nAC", 1),
            (b"@r1\nACG", 1),
            (b"@r1\nACG\n", 2),
            (b"@r1\nACG\n+", 2),
            (b"@r1\nACG\n+\n", 3),
            (b"@r1\nACG\n+\nH", 3),
            (b"@r1\nACG\n+\nHH", 3),
            (b"@r1\nACG\n+\nHHH\n@", 4),
            (b"@r1\nACG\n+\nHHH\n@r", 4),
            (b"@r1\nACG\n+\nHHH\n@r2", 4),
            (b"@r1\nACG\n+\nHHH\n@r2\n", 5),
            (b"@r1\nACG\n+\nHHH\n@r2\nT", 5),
            (b"@r1\nACG\n+\nHHH\n@r2\nT\n", 6),
            (b"@r1\nACG\n+\nHHH\n@r2\nT\n+", 6),
            (b"@r1\nACG\n+\nHHH\n@r2\nT\n+\n", 7),
        ],
    )
    def test_fastq_incomplete(self, s, line) -> None:
        fastq = BytesIO(s)
        with raises(FastqFormatError) as info:
            with FastqReader(fastq) as fq:
                list(fq)
        assert info.value.line == line

    def test_half_record_line_numbers(self) -> None:
        fastq = BytesIO(b"@r\nACG\n+\nHH\n")
        # Choose the buffer size such that only parts of the record fit
        # We want to ensure that the line number is reset properly
        # after the record has been half-parsed
        buffer_size = len("@r\nACG\n+\n")
        with raises(FastqFormatError) as info:
            with FastqReader(fastq, buffer_size=buffer_size) as fq:
                list(fq)  # pragma: no cover
        assert "Length of sequence and qualities differ" in info.value.message
        assert info.value.line == 3

    @mark.parametrize(
        "s,line",
        [
            (b"@r1\nACG\n+\nH#HH\n@r2\nT\n+\nH\n", 3),
            (b"@r1\nACG\n+\n#H\n@r2\nT\n+\nH\n", 3),
            (b"@r1\nACG\n+\nHHH\n@r2\nT\n+\nHH\n", 7),
            (b"@r1\nACG\n+\nHHH\n@r2\nT\n+\n\n", 7),
        ],
    )
    def test_differing_lengths(self, s, line) -> None:
        fastq = BytesIO(s)
        with raises(FastqFormatError) as info:
            with FastqReader(fastq) as fq:
                list(fq)
        assert info.value.line == line

    def test_missing_final_newline(self) -> None:
        # Files with a missing final newline are currently allowed
        fastq = BytesIO(b"@r1\nA\n+\nH")
        with dnaio.open(fastq) as f:
            records = list(f)
        assert records == [SequenceRecord("r1", "A", "H")]

    def test_non_ascii_in_record(self) -> None:
        # \xc4 -> Ä
        fastq = BytesIO(b"@r1\n\xc4\n+\nH")
        with pytest.raises(FastqFormatError) as e:
            with dnaio.open(fastq) as f:
                list(f)
            e.match("Non-ASCII")

    def test_not_opened_as_binary(self) -> None:
        filename = "tests/data/simple.fastq"
        with open(filename, "rt") as f:
            with raises(ValueError):
                list(dnaio.open(f))  # type: ignore

    def test_context_manager(self) -> None:
        filename = "tests/data/simple.fastq"
        with open(filename, "rb") as f:
            assert not f.closed
            reader = dnaio.open(f)
            assert isinstance(reader, FastqReader)
            _ = list(reader)
            assert not f.closed
        assert f.closed

        with FastqReader(filename) as sr:
            tmp_sr = sr
            assert not sr._file.closed
            _ = list(sr)
            assert not sr._file.closed
        assert tmp_sr._file is None

    def test_two_header_detection(self) -> None:
        fastq = BytesIO(b"@r1\nACG\n+r1\nHHH\n@r2\nT\n+r2\n#\n")
        with FastqReader(fastq) as fq:
            assert fq.two_headers
            list(fq)

        fastq = BytesIO(b"@r1\nACG\n+\nHHH\n@r2\nT\n+r2\n#\n")
        with FastqReader(fastq) as fq:
            assert not fq.two_headers
            list(fq)

    def test_second_header_not_equal(self) -> None:
        fastq = BytesIO(b"@r1\nACG\n+xy\nXXX\n")
        with raises(FastqFormatError) as info:
            with FastqReader(fastq) as fq:
                list(fq)  # pragma: no cover
        assert "Sequence descriptions don't match" in info.value.message


class TestOpen:
    def setup_method(self):
        self._tmpdir = mkdtemp()

    def teardown_method(self):
        shutil.rmtree(self._tmpdir)

    def test_sequence_reader(self) -> None:
        # test the autodetection
        with dnaio.open("tests/data/simple.fastq") as f:
            reads = list(f)
        assert reads == simple_fastq

        with dnaio.open("tests/data/simple.fasta") as f:
            reads = list(f)
        assert reads == simple_fasta

        with open("tests/data/simple.fastq", "rb") as f:
            reads = list(dnaio.open(f))
        assert reads == simple_fastq

        # make the name attribute unavailable
        with open("tests/data/simple.fastq", "rb") as f:
            data = f.read()
        bio = BytesIO(data)
        reads = list(dnaio.open(bio))
        assert reads == simple_fastq
        with open("tests/data/simple.fasta", "rb") as f:
            data = f.read()
        bio = BytesIO(data)
        reads = list(dnaio.open(bio))
        assert reads == simple_fasta

    def test_autodetect_fasta_format(self, tmpdir) -> None:
        path = str(tmpdir.join("tmp.fasta"))
        with dnaio.open(path, mode="w") as f:
            assert isinstance(f, FastaWriter)
            for seq in simple_fastq:
                f.write(seq)
        with dnaio.open(path) as f:
            records = list(f)
        assert records == simple_fasta

    def test_write_qualities_to_fasta(self) -> None:
        path = os.path.join(self._tmpdir, "tmp.fasta")
        with dnaio.open(path, mode="w", qualities=True) as f:
            assert isinstance(f, FastaWriter)
            for seq in simple_fastq:
                f.write(seq)
        with dnaio.open(path) as f:
            assert list(f) == simple_fasta

    def test_autodetect_fastq_format(self) -> None:
        path = os.path.join(self._tmpdir, "tmp.fastq")
        with dnaio.open(path, mode="w") as f:
            assert isinstance(f, FastqWriter)
            for seq in simple_fastq:
                f.write(seq)
        with dnaio.open(path) as f:
            assert list(f) == simple_fastq

    def test_autodetect_fastq_weird_name(self) -> None:
        path = os.path.join(self._tmpdir, "tmp.fastq.gz")
        with dnaio.open(path, mode="w") as f:
            assert isinstance(f, FastqWriter)
            for seq in simple_fastq:
                f.write(seq)
        weird_path = os.path.join(self._tmpdir, "tmp.weird.gz")
        os.rename(path, weird_path)
        with dnaio.open(weird_path) as f:
            assert list(f) == simple_fastq

    def test_fastq_qualities_missing(self) -> None:
        path = os.path.join(self._tmpdir, "tmp.fastq")
        with raises(ValueError):
            with dnaio.open(path, mode="w", qualities=False):
                pass  # pragma: no cover


class TestInterleavedReader:
    def test(self) -> None:
        expected = [
            (
                SequenceRecord(
                    "read1/1 some text", "TTATTTGTCTCCAGC", "##HHHHHHHHHHHHH"
                ),
                SequenceRecord(
                    "read1/2 other text", "GCTGGAGACAAATAA", "HHHHHHHHHHHHHHH"
                ),
            ),
            (
                SequenceRecord(
                    "read3/1", "CCAACTTGATATTAATAACA", "HHHHHHHHHHHHHHHHHHHH"
                ),
                SequenceRecord(
                    "read3/2", "TGTTATTAATATCAAGTTGG", "#HHHHHHHHHHHHHHHHHHH"
                ),
            ),
        ]
        with InterleavedPairedEndReader("tests/data/interleaved.fastq") as isr:
            reads = list(isr)

        assert reads == expected
        with dnaio.open("tests/data/interleaved.fastq", interleaved=True) as f:
            reads = list(f)
        assert reads == expected

    def test_missing_partner(self) -> None:
        s = BytesIO(b"@r1\nACG\n+\nHHH\n")
        with raises(FileFormatError) as info:
            with InterleavedPairedEndReader(s) as isr:
                list(isr)
        assert "Interleaved input file incomplete" in info.value.message

    def test_incorrectly_paired(self) -> None:
        s = BytesIO(b"@r1/1\nACG\n+\nHHH\n@wrong_name\nTTT\n+\nHHH\n")
        with raises(FileFormatError) as info:
            with InterleavedPairedEndReader(s) as isr:
                list(isr)
        assert "Reads are improperly paired" in info.value.message


class TestFastaWriter:
    def setup_method(self):
        self._tmpdir = mkdtemp()
        self.path = os.path.join(self._tmpdir, "tmp.fasta")

    def teardown_method(self):
        shutil.rmtree(self._tmpdir)

    def test(self) -> None:
        with FastaWriter(self.path) as fw:
            fw.write("name", "CCATA")
            fw.write("name2", "HELLO")
        assert fw._file.closed
        with open(self.path) as t:
            assert t.read() == ">name\nCCATA\n>name2\nHELLO\n"

    def test_linelength(self) -> None:
        with FastaWriter(self.path, line_length=3) as fw:
            fw.write("r1", "ACG")
            fw.write("r2", "CCAT")
            fw.write("r3", "TACCAG")
        assert fw._file.closed
        with open(self.path) as t:
            d = t.read()
            assert d == ">r1\nACG\n>r2\nCCA\nT\n>r3\nTAC\nCAG\n"

    def test_write_sequence_object(self) -> None:
        with FastaWriter(self.path) as fw:
            fw.write(SequenceRecord("name", "CCATA"))
            fw.write(SequenceRecord("name2", "HELLO"))
        assert fw._file.closed
        with open(self.path) as t:
            assert t.read() == ">name\nCCATA\n>name2\nHELLO\n"

    def test_write_to_file_like_object(self) -> None:
        bio = BytesIO()
        with FastaWriter(bio) as fw:
            fw.write(SequenceRecord("name", "CCATA"))
            fw.write(SequenceRecord("name2", "HELLO"))
        assert bio.getvalue() == b">name\nCCATA\n>name2\nHELLO\n"
        assert not bio.closed
        assert not fw._file.closed

    def test_write_zero_length_sequence_record(self) -> None:
        bio = BytesIO()
        with FastaWriter(bio) as fw:
            fw.write(SequenceRecord("name", ""))
        assert bio.getvalue() == b">name\n\n", "{!r}".format(bio.getvalue())


class TestFastqWriter:
    def setup_method(self):
        self._tmpdir = mkdtemp()
        self.path = os.path.join(self._tmpdir, "tmp.fastq")

    def teardown_method(self):
        shutil.rmtree(self._tmpdir)

    def test(self) -> None:
        with FastqWriter(self.path) as fq:
            fq.writeseq("name", "CCATA", "!#!#!")
            fq.writeseq("name2", "HELLO", "&&&!&&")
        assert fq._file.closed
        with open(self.path) as t:
            assert t.read() == "@name\nCCATA\n+\n!#!#!\n@name2\nHELLO\n+\n&&&!&&\n"

    def test_twoheaders(self) -> None:
        with FastqWriter(self.path, two_headers=True) as fq:
            fq.write(SequenceRecord("name", "CCATA", "!#!#!"))
            fq.write(SequenceRecord("name2", "HELLO", "&&&!&"))
        assert fq._file.closed
        with open(self.path) as t:
            assert (
                t.read() == "@name\nCCATA\n+name\n!#!#!\n@name2\nHELLO\n+name2\n&&&!&\n"
            )

    def test_write_to_file_like_object(self) -> None:
        bio = BytesIO()
        with FastqWriter(bio) as fq:
            fq.writeseq("name", "CCATA", "!#!#!")
            fq.writeseq("name2", "HELLO", "&&&!&&")
        assert bio.getvalue() == b"@name\nCCATA\n+\n!#!#!\n@name2\nHELLO\n+\n&&&!&&\n"


class TestInterleavedWriter:
    def test(self) -> None:
        reads = [
            (
                SequenceRecord("A/1 comment", "TTA", "##H"),
                SequenceRecord("A/2 comment", "GCT", "HH#"),
            ),
            (SequenceRecord("B/1", "CC", "HH"), SequenceRecord("B/2", "TG", "#H")),
        ]
        bio = BytesIO()
        with InterleavedPairedEndWriter(bio) as writer:
            for read1, read2 in reads:
                writer.write(read1, read2)
        assert bio.getvalue() == (
            b"@A/1 comment\nTTA\n+\n##H\n"
            b"@A/2 comment\nGCT\n+\nHH#\n"
            b"@B/1\nCC\n+\nHH\n"
            b"@B/2\nTG\n+\n#H\n"
        )


class TestPairedSequenceReader:
    def test_read(self) -> None:
        s1 = BytesIO(b"@r1\nACG\n+\nHHH\n")
        s2 = BytesIO(b"@r2\nGTT\n+\n858\n")
        with TwoFilePairedEndReader(s1, s2) as psr:
            assert [
                (
                    SequenceRecord("r1", "ACG", "HHH"),
                    SequenceRecord("r2", "GTT", "858"),
                ),
            ] == list(psr)

    def test_record_names_match(self) -> None:
        match = record_names_match
        assert match("abc", "abc")
        assert match("abc def", "abc")
        assert match("abc def", "abc ghi")
        assert match("abc", "abc ghi")
        assert not match("abc", "xyz")
        assert match("abc\tdef", "abc")
        assert match("abc\tdef", "abc\tghi")
        assert match("abc somecomment\tanothercomment", "abc andanothercomment\tbla")
        assert match("abc\tcomments comments", "abc\tothers others")
        assert match("abc\tdef", "abc def")

    def test_record_names_match_with_ignored_trailing_12(self) -> None:
        match = record_names_match
        assert match("abc/1", "abc/2")
        assert match("abc.1", "abc.2")
        assert match("abc1", "abc2")
        assert match("abc2", "abc1")
        assert match("abc1 def", "abc1 ghi")
        assert match("abc1 def", "abc2 ghi")
        assert match("abc2 def", "abc1 ghi")
        assert not match("abc1", "abc4")
        assert not match("abc1", "abc")
        assert not match("abc", "abc1")
        assert not match("abc", "abc2")

    def test_record_names_match_with_ignored_trailing_123(self) -> None:
        match = record_names_match
        assert match("abc/1", "abc/3")
        assert match("abc.1 def", "abc.3 ghi")
        assert match("abc.3 def", "abc.1 ghi")

    def test_missing_partner1(self) -> None:
        s1 = BytesIO(b"")
        s2 = BytesIO(b"@r1\nACG\n+\nHHH\n")

        with raises(FileFormatError) as info:
            with TwoFilePairedEndReader(s1, s2) as psr:
                list(psr)
        assert "There are more reads in file 2 than in file 1" in info.value.message

    def test_missing_partner2(self) -> None:
        s1 = BytesIO(b"@r1\nACG\n+\nHHH\n")
        s2 = BytesIO(b"")

        with raises(FileFormatError) as info:
            with TwoFilePairedEndReader(s1, s2) as psr:
                list(psr)
        assert "There are more reads in file 1 than in file 2" in info.value.message

    def test_empty_sequences_do_not_stop_iteration(self) -> None:
        s1 = BytesIO(b"@r1_1\nACG\n+\nHHH\n@r2_1\nACG\n+\nHHH\n@r3_2\nACG\n+\nHHH\n")
        s2 = BytesIO(b"@r1_1\nACG\n+\nHHH\n@r2_2\n\n+\n\n@r3_2\nACG\n+\nHHH\n")
        # Second sequence for s2 is empty but valid. Should not lead to a stop of iteration.
        with TwoFilePairedEndReader(s1, s2) as psr:
            seqs = list(psr)
        print(seqs)
        assert len(seqs) == 3

    def test_incorrectly_paired(self) -> None:
        s1 = BytesIO(b"@r1/1\nACG\n+\nHHH\n")
        s2 = BytesIO(b"@wrong_name\nTTT\n+\nHHH\n")
        with raises(FileFormatError) as info:
            with TwoFilePairedEndReader(s1, s2) as psr:
                list(psr)
        assert "Reads are improperly paired" in info.value.message


@mark.parametrize(
    "path",
    [
        os.path.join("tests", "data", "simple.fastq"),
        os.path.join("tests", "data", "dos.fastq"),
        os.path.join("tests", "data", "simple.fasta"),
        os.path.join("tests", "data", "with_comment.fasta"),
    ],
)
def test_read_stdin(path) -> None:
    # Get number of records in the input file
    with dnaio.open(path) as f:
        expected = len(list(f))

    # Use piping from a separate subprocess to force the input file name to be unavailable
    cmd = "type" if sys.platform == "win32" else "cat"
    with subprocess.Popen(
        [cmd, path], stdout=subprocess.PIPE, shell=sys.platform == "win32"
    ) as cat:
        with subprocess.Popen(
            [sys.executable, "tests/read_from_stdin.py"],
            stdin=cat.stdout,
            stdout=subprocess.PIPE,
        ) as py:
            assert cat.stdout is not None
            cat.stdout.close()
            # Check that the read_from_stdin.py script prints the correct number of records
            assert str(expected) == py.communicate()[0].decode().strip()


def test_file_writer(tmp_path) -> None:
    path = tmp_path / "out.txt"
    fw = FileWriter(path)
    repr(fw)
    fw.close()
    assert path.exists()
    with raises(ValueError) as e:
        with fw:
            pass  # pragma: no coverage
    assert "operation on closed file" in e.value.args[0]


def test_binary_file_reader() -> None:
    bfr = BinaryFileReader("tests/data/simple.fasta")
    repr(bfr)
    bfr.close()
    with raises(ValueError) as e:
        with bfr:
            pass  # pragma: no coverage
    assert "operation on closed" in e.value.args[0]


def test_fasta_writer_repr(tmp_path) -> None:
    with FastaWriter(tmp_path / "out.fasta") as fw:
        repr(fw)


def test_fastq_writer_repr(tmp_path) -> None:
    with FastqWriter(tmp_path / "out.fastq") as fw:
        repr(fw)


class TestAsciiCheck:
    ASCII_STRING = (
        "In het Nederlands komen bijzondere leestekens niet vaak voor.".encode("ascii")
    )
    # NON-ASCII from the German wikipedia.
    NON_ASCII_STRING = (
        "In späterer Zeit trat Umlaut sehr häufig analogisch ein.".encode("latin-1")
    )

    def test_ascii(self) -> None:
        assert bytes_ascii_check(self.ASCII_STRING)

    def test_ascii_all_chars(self) -> None:
        assert bytes_ascii_check(bytes(range(128)))
        assert not bytes_ascii_check(bytes(range(129)))

    def test_non_ascii(self) -> None:
        assert not bytes_ascii_check(self.NON_ASCII_STRING)

    def test_non_ascii_lengths(self) -> None:
        # Make sure that the function finds the non-ascii byte correctly for
        # all lengths.
        non_ascii_char = "é".encode("latin-1")
        for i in range(len(self.ASCII_STRING)):
            test_string = self.ASCII_STRING[:i] + non_ascii_char
            assert not bytes_ascii_check(test_string)

    def test_ascii_lengths(self) -> None:
        # Make sure the ascii check is correct even though there are non-ASCII
        # bytes directly behind the search space.
        # This ensures there is no overshoot where the algorithm checks bytes
        # after the search space.
        non_ascii_char = "é".encode("latin-1")
        for i in range(1, len(self.ASCII_STRING) + 1):
            test_string = self.ASCII_STRING[:i] + (non_ascii_char * 8)
            assert bytes_ascii_check(test_string, i - 1)


class TestRecordsAreMates:
    def test_records_are_mates(self) -> None:
        assert records_are_mates(
            SequenceRecord("same_name1 some_comment", "A", "H"),
            SequenceRecord("same_name2 other_comment", "A", "H"),
            SequenceRecord("same_name3", "A", "H"),
        )

    @pytest.mark.parametrize("number_of_mates", list(range(2, 11)))
    def test_lots_of_records_are_mates(self, number_of_mates) -> None:
        mates = [SequenceRecord("name", "A", "H") for _ in range(number_of_mates)]
        assert records_are_mates(*mates)

    def test_records_are_not_mates(self) -> None:
        assert not records_are_mates(
            SequenceRecord("same_name1 some_comment", "A", "H"),
            SequenceRecord("same_name2 other_comment", "A", "H"),
            SequenceRecord("shame_name3 different_comment", "A", "H"),
        )

    def test_records_are_mates_zero_arguments(self) -> None:
        with pytest.raises(TypeError) as error:
            records_are_mates()  # type: ignore
        error.match("records_are_mates requires at least two arguments")

    def test_records_are_mates_one_argument(self) -> None:
        with pytest.raises(TypeError) as error:
            records_are_mates(SequenceRecord("A", "A", "A"))  # type: ignore
        error.match("records_are_mates requires at least two arguments")


class TestBamReader:
    bam_file = (
        TEST_DATA / "project.NIST_NIST7035_H7AP8ADXX_TAAGGCGA_1_NA12878"
        ".bwa.markDuplicates.unmapped.bam"
    )
    raw_bam_bytes = gzip.decompress(bam_file.read_bytes())
    complete_record_with_header = raw_bam_bytes[:6661]
    complete_header = complete_record_with_header[:6359]

    def test_parse_bam(self):
        with dnaio.open(self.bam_file) as reader:
            records = list(reader)
        assert len(records) == 3
        assert reader.number_of_records == 3
        assert records[0].name == "HWI-D00119:50:H7AP8ADXX:1:1104:8519:18990"
        assert records[0].sequence == (
            "GATCACAGGTCTATCACCCTATTAACCACTCACGGGAGCTCTCCATGCATTTGGTATTTTCGTCT"
            "GGGGGGTATGCACGCGATAGCATTGCGAGACGCTGG"
        )
        assert records[0].qualities == (
            "CCCFFFFFHFFHHJIJJIJGGJJJJJJJJJJJJJIGHIIEHIJJJJJJIJJJJIBGGIIIHIIII"
            "HHHHDD;9CCDEDDDDDDDDDDEDDDDDDDDDDDDD"
        )
        assert records[1].name == "HWI-D00119:50:H7AP8ADXX:1:2104:18479:82511"
        assert records[1].sequence == (
            "GATCACAGGTCTATCACCCTATTAACCACTCACGGGAGCTCTCCATGCATTTGGTATTTTCGTCT"
            "GGGGGGTATGCACGCGATAGCATTGCGAGACGCTGG"
        )
        assert records[1].qualities == (
            "CCCFFFFFHFFHHJJJJIJJJJIIJJJJJGIJJJJGIJJJJJJJJGJIJJIJJJGHIJJJJJJJI"
            "HHHHDD@>CDDEDDDDDDDDDDEDDCDDDDD?BBD9"
        )
        assert records[2].name == "HWI-D00119:50:H7AP8ADXX:1:2105:7076:23015"
        assert records[2].sequence == (
            "GATCACAGGTCTATCACCCTATTAACCACTCACGGGAGCTCTCCATGCATTTGGTATTTTCGTCT"
            "GGGGGGTATGCACGCGATAGCATTGCGAGACGCTGG"
        )
        assert records[2].qualities == (
            "@@CFFFDFGFHHHJIIJIJIJJJJJJJIIJJJJIIJIJFIIJJJJIIIGIJJJJDHIJIIJIJJJ"
            "HHGGCB>BDDDDDDDDDDDBDDEDDDDDDDDDDDDD"
        )

    def test_parse_header(self):
        header = (
            Path(__file__).parent
            / "data"
            / "project.NIST_NIST7035_H7AP8ADXX_TAAGGCGA_1_NA12878.bwa"
            ".markDuplicates.header.sam"
        )
        header_bytes = header.read_bytes()
        with dnaio.open(self.bam_file) as bam:
            assert bam.header == header_bytes

    @pytest.mark.parametrize(
        "end", range(len(complete_header) + 1, len(complete_record_with_header))
    )
    def test_truncated_record(self, end: int):
        file = io.BytesIO(self.complete_record_with_header[:end])
        with pytest.raises(EOFError) as e:
            list(BamReader(file))
        e.match("Incomplete record at the end of file")

    @pytest.mark.parametrize("end", [3, 5, 2000, 6000])
    def test_truncated_header(self, end):
        file = io.BytesIO(self.complete_record_with_header[:end])
        with pytest.raises(EOFError) as e:
            list(BamReader(file))
        e.match("Truncated BAM file")

    def test_bam_parser_not_binary_error(self):
        file = io.StringIO(
            "Don't be too proud of this technological terror you have constructed."
        )
        with pytest.raises(TypeError) as error:
            BamReader(file)
        error.match("binary mode")

    @pytest.mark.parametrize("buffersize", [4, 8, 10, 20, 40])
    def test_small_buffersize(self, buffersize):
        reader = BamReader(str(self.bam_file), buffer_size=buffersize)
        assert len(list(reader)) == 3

    def test_error_on_mapped_bam(self):
        bam = TEST_DATA / (
            "project.NIST_NIST7035_H7AP8ADXX_TAAGGCGA_1_NA12878"
            ".bwa.markDuplicates.bam"
        )
        reader = BamReader(str(bam))
        it = iter(reader)
        with pytest.raises(NotImplementedError) as error:
            next(it)
        assert error.match("unmapped single reads")

    def test_parse_bam_missing_quals(self):
        bam = TEST_DATA / "missing_quals.bam"
        reader = BamReader(str(bam))
        records = list(reader)
        assert len(records) == 1
        record = records[0]
        assert record.sequence == "GATTACA"
        # Phred scores should default to 0 + 33 == ord('!') when missing.
        assert record.qualities is None