File: sequence_file_input_test.cpp

package info (click to toggle)
seqan3 3.4.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,580 kB
  • sloc: cpp: 145,192; sh: 307; xml: 264; javascript: 95; makefile: 70; perl: 29; php: 15
file content (469 lines) | stat: -rw-r--r-- 17,406 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
// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: BSD-3-Clause

#include <gtest/gtest.h>

#include <algorithm>
#include <iterator>
#include <ranges>
#include <sstream>

#include <seqan3/alphabet/detail/debug_stream_alphabet.hpp>
#include <seqan3/io/sequence_file/input.hpp>
#include <seqan3/test/expect_range_eq.hpp>
#include <seqan3/test/tmp_directory.hpp>
#include <seqan3/utility/views/convert.hpp>

using seqan3::operator""_dna5;

using default_fields = seqan3::fields<seqan3::field::seq, seqan3::field::id, seqan3::field::qual>;

TEST(sequence_file_input_iterator, concepts)
{
    using it_t = typename seqan3::sequence_file_input<>::iterator;
    using sen_t = typename seqan3::sequence_file_input<>::sentinel;

    EXPECT_TRUE((std::input_iterator<it_t>));
    EXPECT_TRUE((std::sentinel_for<sen_t, it_t>));
}

struct sequence_file_input_f : public ::testing::Test
{
    std::string input{">TEST 1\n"
                      "ACGT\n"
                      ">Test2\n"
                      "AGGCTGN\n"
                      ">Test3\n"
                      "GGAGTATAATATATATATATATAT\n"};

    seqan3::dna5_vector seq_comp[3]{"ACGT"_dna5, "AGGCTGN"_dna5, "GGAGTATAATATATATATATATAT"_dna5};

    std::string id_comp[3]{"TEST 1", "Test2", "Test3"};
};

TEST_F(sequence_file_input_f, concepts)
{
    using t = seqan3::sequence_file_input<>;
    EXPECT_TRUE((std::ranges::input_range<t>));

    using ct = seqan3::sequence_file_input<> const;
    // not const-iterable
    EXPECT_FALSE((std::ranges::input_range<ct>));
}

TEST_F(sequence_file_input_f, construct_by_filename)
{
    /* just the filename */
    {
        seqan3::test::tmp_directory tmp;
        auto filename = tmp.path() / "sequence_file_input_constructor.fasta";

        {
            std::ofstream filecreator{filename, std::ios::out | std::ios::binary};
        }

        EXPECT_NO_THROW(seqan3::sequence_file_input<>{filename});
    }

    // correct format check is done by tests of that format

    /* wrong extension */
    {
        seqan3::test::tmp_directory tmp;
        auto filename = tmp.path() / "sequence_file_input_constructor.xyz";

        std::ofstream filecreator{filename, std::ios::out | std::ios::binary};
        EXPECT_THROW(seqan3::sequence_file_input<>{filename}, seqan3::unhandled_extension_error);
    }

    /* non-existent file */
    {
        EXPECT_THROW(seqan3::sequence_file_input<>{"/dev/nonexistant/foobarOOO"}, seqan3::file_open_error);
    }

    /* filename + fields */
    {
        using fields_seq = seqan3::fields<seqan3::field::seq>;

        seqan3::test::tmp_directory tmp;
        auto filename = tmp.path() / "sequence_file_input_constructor.fasta";

        {
            std::ofstream filecreator{filename, std::ios::out | std::ios::binary};
        }

        EXPECT_NO_THROW((seqan3::sequence_file_input<seqan3::sequence_file_input_default_traits_dna,
                                                     fields_seq,
                                                     seqan3::type_list<seqan3::format_fasta>>{filename, fields_seq{}}));
    }
}

TEST_F(sequence_file_input_f, construct_from_stream)
{
    /* stream + format_tag */
    EXPECT_NO_THROW((seqan3::sequence_file_input<seqan3::sequence_file_input_default_traits_dna,
                                                 default_fields,
                                                 seqan3::type_list<seqan3::format_fasta>>{std::istringstream{input},
                                                                                          seqan3::format_fasta{}}));

    /* stream + format_tag + fields */
    EXPECT_NO_THROW((seqan3::sequence_file_input<seqan3::sequence_file_input_default_traits_dna,
                                                 default_fields,
                                                 seqan3::type_list<seqan3::format_fasta>>{std::istringstream{input},
                                                                                          seqan3::format_fasta{},
                                                                                          default_fields{}}));
}

TEST_F(sequence_file_input_f, default_template_args_and_deduction_guides)
{
    using comp0 = seqan3::sequence_file_input_default_traits_dna;
    using comp2 = seqan3::type_list<seqan3::format_embl,
                                    seqan3::format_fasta,
                                    seqan3::format_fastq,
                                    seqan3::format_genbank,
                                    seqan3::format_sam>;
    using comp3 = char;

    /* default template args */
    {
        using t = seqan3::sequence_file_input<>;
        EXPECT_TRUE((std::is_same_v<typename t::traits_type, comp0>));
        EXPECT_TRUE((std::is_same_v<typename t::selected_field_ids, default_fields>));
        EXPECT_TRUE((std::is_same_v<typename t::valid_formats, comp2>));
        EXPECT_TRUE((std::is_same_v<typename t::stream_char_type, comp3>));
    }

    /* guided filename constructor */
    {
        seqan3::test::tmp_directory tmp;
        auto filename = tmp.path() / "sequence_file_input_constructor.fasta";

        {
            std::ofstream filecreator{filename, std::ios::out | std::ios::binary};
        }

        seqan3::sequence_file_input fin{filename};

        using t = decltype(fin);
        EXPECT_TRUE((std::is_same_v<typename t::traits_type, comp0>));
        EXPECT_TRUE((std::is_same_v<typename t::selected_field_ids, default_fields>));
        EXPECT_TRUE((std::is_same_v<typename t::valid_formats, comp2>));
        EXPECT_TRUE((std::is_same_v<typename t::stream_char_type, comp3>));
    }

    /* guided filename constructor + custom fields */
    {
        seqan3::test::tmp_directory tmp;
        auto filename = tmp.path() / "sequence_file_input_constructor.fasta";

        {
            std::ofstream filecreator{filename, std::ios::out | std::ios::binary};
        }

        seqan3::sequence_file_input fin{filename, seqan3::fields<seqan3::field::seq>{}};

        using t = decltype(fin);
        EXPECT_TRUE((std::is_same_v<typename t::traits_type, comp0>));
        EXPECT_TRUE((std::is_same_v<typename t::selected_field_ids, seqan3::fields<seqan3::field::seq>>)); // changed
        EXPECT_TRUE((std::is_same_v<typename t::valid_formats, comp2>));
        EXPECT_TRUE((std::is_same_v<typename t::stream_char_type, comp3>));
    }

    /* guided stream constructor */
    {
        std::istringstream ext{input};
        seqan3::sequence_file_input fin{ext, seqan3::format_fasta{}};

        using t = decltype(fin);
        EXPECT_TRUE((std::is_same_v<typename t::traits_type, comp0>));
        EXPECT_TRUE((std::is_same_v<typename t::selected_field_ids, default_fields>));
        EXPECT_TRUE((std::is_same_v<typename t::valid_formats, seqan3::type_list<seqan3::format_fasta>>)); // changed
        EXPECT_TRUE((std::is_same_v<typename t::stream_char_type, comp3>));
    }

    /* guided stream temporary constructor */
    {
        seqan3::sequence_file_input fin{std::istringstream{input}, seqan3::format_fasta{}};

        using t = decltype(fin);
        EXPECT_TRUE((std::is_same_v<typename t::traits_type, comp0>));
        EXPECT_TRUE((std::is_same_v<typename t::selected_field_ids, default_fields>));
        EXPECT_TRUE((std::is_same_v<typename t::valid_formats, seqan3::type_list<seqan3::format_fasta>>)); // changed
        EXPECT_TRUE((std::is_same_v<typename t::stream_char_type, comp3>));
    }
}

TEST_F(sequence_file_input_f, empty_file)
{
    seqan3::test::tmp_directory tmp;
    auto filename = tmp.path() / "empty.fasta";

    std::ofstream filecreator{filename, std::ios::out | std::ios::binary};

    seqan3::sequence_file_input fin{filename};

    EXPECT_EQ(fin.begin(), fin.end());
}

TEST_F(sequence_file_input_f, empty_stream)
{
    seqan3::sequence_file_input fin{std::istringstream{std::string{}}, seqan3::format_fasta{}};

    EXPECT_EQ(fin.begin(), fin.end());
}

TEST_F(sequence_file_input_f, record_reading)
{
    /* record based reading */
    seqan3::sequence_file_input fin{std::istringstream{input}, seqan3::format_fasta{}};

    size_t counter = 0;
    for (auto & rec : fin)
    {
        EXPECT_RANGE_EQ(rec.id(), id_comp[counter]);
        EXPECT_RANGE_EQ(rec.sequence(), seq_comp[counter]);
        EXPECT_TRUE(empty(rec.base_qualities()));

        ++counter;
    }

    EXPECT_EQ(counter, 3u);
}

TEST_F(sequence_file_input_f, record_reading_struct_bind)
{
    /* record based reading */
    seqan3::sequence_file_input fin{std::istringstream{input}, seqan3::format_fasta{}};

    size_t counter = 0;
    for (auto & [seq, id, qual] : fin)
    {
        EXPECT_RANGE_EQ(seq, seq_comp[counter]);
        EXPECT_RANGE_EQ(id, id_comp[counter]);
        EXPECT_TRUE(empty(qual));

        ++counter;
    }

    EXPECT_EQ(counter, 3u);
}

TEST_F(sequence_file_input_f, record_reading_custom_options)
{
    std::istringstream istream{std::string{">ID1 lala\n"
                                           "ACGTTTTTTTTTTTTTTT\n"
                                           ">ID2\n"
                                           "ACGTTTTTTT\n"
                                           ">ID3 lala\n"
                                           "ACGTTTA\n"}};

    /* record based reading */
    seqan3::sequence_file_input fin{istream, seqan3::format_fasta{}};
    fin.options.truncate_ids = true;

    auto it = fin.begin();
    EXPECT_EQ((*it).id(), "ID1");
    ++it;
    EXPECT_EQ((*it).id(), "ID2");
    ++it;
    EXPECT_EQ((*it).id(), "ID3");
}

TEST_F(sequence_file_input_f, file_view)
{
    seqan3::sequence_file_input fin{std::istringstream{input}, seqan3::format_fasta{}};

    auto minimum_length_filter = std::views::filter(
        [](auto const & rec)
        {
            return size(rec.sequence()) >= 5;
        });

    size_t counter = 1; // the first record will be filtered out
    for (auto & rec : fin | minimum_length_filter)
    {
        EXPECT_RANGE_EQ(rec.id(), id_comp[counter]);
        EXPECT_RANGE_EQ(rec.sequence(), seq_comp[counter]);
        EXPECT_TRUE(empty(rec.base_qualities()));

        ++counter;
    }

    EXPECT_EQ(counter, 3u);
}

// ----------------------------------------------------------------------------
// decompression
// ----------------------------------------------------------------------------

template <typename fixture_t, typename input_file_t>
void decompression_impl(fixture_t & fix, input_file_t & fin)
{
    size_t counter = 0;
    for (auto & rec : fin)
    {
        EXPECT_RANGE_EQ(rec.sequence(), fix.seq_comp[counter]);
        EXPECT_RANGE_EQ(rec.id(), fix.id_comp[counter]);
        EXPECT_TRUE(empty(rec.base_qualities()));

        ++counter;
    }

    EXPECT_EQ(counter, 3u);
}

#if SEQAN3_HAS_ZLIB
std::string input_gz{
    '\x1F', '\x8B', '\x08', '\x00', '\x33', '\xBF', '\x13', '\x5C', '\x00', '\x03', '\xB3', '\x53', '\x08', '\x71',
    '\x0D', '\x0E', '\x51', '\x30', '\xE4', '\x72', '\x74', '\x76', '\x0F', '\xE1', '\xB2', '\x0B', '\x49', '\x2D',
    '\x2E', '\x31', '\xE2', '\x72', '\x74', '\x77', '\x77', '\x0E', '\x71', '\xF7', '\xE3', '\xB2', '\x53', '\x00',
    '\xF1', '\x8D', '\xB9', '\xDC', '\xDD', '\x1D', '\xDD', '\x43', '\x1C', '\x43', '\x1C', '\x1D', '\x43', '\x50',
    '\x21', '\x17', '\x00', '\xEF', '\x24', '\xC2', '\xE9', '\x3E', '\x00', '\x00', '\x00',
};

TEST_F(sequence_file_input_f, decompression_by_filename_gz)
{
    seqan3::test::tmp_directory tmp;
    auto filename = tmp.path() / "sequence_file_output_test.fasta.gz";

    {
        std::ofstream of{filename, std::ios::binary};

        std::copy(begin(input_gz), end(input_gz), std::ostreambuf_iterator<char>{of});
    }

    seqan3::sequence_file_input fin{filename};

    decompression_impl(*this, fin);
}

TEST_F(sequence_file_input_f, decompression_by_stream_gz)
{
    seqan3::sequence_file_input fin{std::istringstream{input_gz}, seqan3::format_fasta{}};

    decompression_impl(*this, fin);
}

TEST_F(sequence_file_input_f, read_empty_gz_file)
{
    std::string empty_zipped_file{'\x1f', '\x8b', '\x08', '\x08', '\x5a', '\x07', '\x98', '\x5c',
                                  '\x00', '\x03', '\x66', '\x6f', '\x6f', '\x00', '\x03', '\x00',
                                  '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'};
    seqan3::sequence_file_input fin{std::istringstream{empty_zipped_file}, seqan3::format_fasta{}};

    EXPECT_TRUE(fin.begin() == fin.end());
}

std::string input_bgzf{'\x1F', '\x8B', '\x08', '\x04', '\x00', '\x00', '\x00', '\x00', '\x00', '\xFF', '\x06', '\x00',
                       '\x42', '\x43', '\x02', '\x00', '\x4A', '\x00', '\xB3', '\x53', '\x08', '\x71', '\x0D', '\x0E',
                       '\x51', '\x30', '\xE4', '\x72', '\x74', '\x76', '\x0F', '\xE1', '\xB2', '\x0B', '\x49', '\x2D',
                       '\x2E', '\x31', '\xE2', '\x72', '\x74', '\x77', '\x77', '\x0E', '\x71', '\xF7', '\xE3', '\xB2',
                       '\x53', '\x00', '\xF1', '\x8D', '\xB9', '\xDC', '\xDD', '\x1D', '\xDD', '\x43', '\x1C', '\x43',
                       '\x1C', '\x1D', '\x43', '\x50', '\x21', '\x17', '\x00', '\xEF', '\x24', '\xC2', '\xE9', '\x3E',
                       '\x00', '\x00', '\x00', '\x1F', '\x8B', '\x08', '\x04', '\x00', '\x00', '\x00', '\x00', '\x00',
                       '\xFF', '\x06', '\x00', '\x42', '\x43', '\x02', '\x00', '\x1B', '\x00', '\x03', '\x00', '\x00',
                       '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'};

TEST_F(sequence_file_input_f, bgzf_decompression_by_filename_bgzf)
{
    seqan3::test::tmp_directory tmp;
    auto filename = tmp.path() / "sequence_file_output_test.fasta.bgzf";

    {
        std::ofstream of{filename, std::ios::binary};

        std::copy(input_bgzf.begin(), input_bgzf.end(), std::ostreambuf_iterator<char>{of});
    }

    seqan3::sequence_file_input fin{filename};

    decompression_impl(*this, fin);
}

TEST_F(sequence_file_input_f, bgzf_decompression_by_filename_gz)
{
    seqan3::test::tmp_directory tmp;
    auto filename = tmp.path() / "sequence_file_output_test.fasta.gz";

    {
        std::ofstream of{filename, std::ios::binary};
        std::copy(input_bgzf.begin(), input_bgzf.end(), std::ostreambuf_iterator<char>{of});
    }

    seqan3::sequence_file_input fin{filename};
    decompression_impl(*this, fin);
}

TEST_F(sequence_file_input_f, decompression_by_stream_bgzf)
{
    seqan3::sequence_file_input fin{std::istringstream{input_bgzf}, seqan3::format_fasta{}};

    decompression_impl(*this, fin);
}

TEST_F(sequence_file_input_f, read_empty_bgzf_file)
{
    std::string empty_bgzf_file{
        '\x1F', '\x8B', '\x08', '\x04', '\x00', '\x00', '\x00', '\x00', '\x00', '\xFF', '\x06', '\x00', '\x42', '\x43',
        '\x02', '\x00', '\x1B', '\x00', '\x03', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
    };
    seqan3::sequence_file_input fin{std::istringstream{empty_bgzf_file}, seqan3::format_fasta{}};

    EXPECT_TRUE(fin.begin() == fin.end());
}
#endif // SEQAN3_HAS_ZLIB

#if SEQAN3_HAS_BZIP2
std::string input_bz2{'\x42', '\x5A', '\x68', '\x39', '\x31', '\x41', '\x59', '\x26', '\x53', '\x59', '\x8D', '\xD7',
                      '\xE7', '\xD6', '\x00', '\x00', '\x06', '\x5F', '\x80', '\x00', '\x10', '\x40', '\x00', '\x38',
                      '\x01', '\x2A', '\x81', '\x0C', '\x00', '\x02', '\x00', '\x0C', '\x00', '\x20', '\x00', '\x54',
                      '\x44', '\x34', '\xC0', '\x00', '\x4A', '\x9B', '\x44', '\x68', '\x9E', '\x48', '\x5D', '\x34',
                      '\x67', '\x4F', '\x24', '\xFC', '\x6F', '\x10', '\xC5', '\xA0', '\x3C', '\x12', '\x61', '\xDD',
                      '\xE9', '\x45', '\xA5', '\xD4', '\x26', '\x31', '\xBC', '\xF1', '\x49', '\x61', '\x81', '\xA2',
                      '\xEE', '\x48', '\xA7', '\x0A', '\x12', '\x11', '\xBA', '\xFC', '\xFA', '\xC0'};

TEST_F(sequence_file_input_f, decompression_by_filename_bz2)
{
    seqan3::test::tmp_directory tmp;
    auto filename = tmp.path() / "sequence_file_output_test.fasta.bz2";

    {
        std::ofstream of{filename, std::ios::binary};

        std::copy(begin(input_bz2), end(input_bz2), std::ostreambuf_iterator<char>{of});
    }

    seqan3::sequence_file_input fin{filename};

    decompression_impl(*this, fin);
}

TEST_F(sequence_file_input_f, decompression_by_stream_bz2)
{
    seqan3::sequence_file_input fin{std::istringstream{input_bz2}, seqan3::format_fasta{}};

    decompression_impl(*this, fin);
}

TEST_F(sequence_file_input_f, read_empty_bz2_file)
{
    std::string empty_zipped_file{'\x42',
                                  '\x5a',
                                  '\x68',
                                  '\x39',
                                  '\x17',
                                  '\x72',
                                  '\x45',
                                  '\x38',
                                  '\x50',
                                  '\x90',
                                  '\x00',
                                  '\x00',
                                  '\x00',
                                  '\x00'};
    seqan3::sequence_file_input fin{std::istringstream{empty_zipped_file}, seqan3::format_fasta{}};

    EXPECT_TRUE(fin.begin() == fin.end());
}
#endif // SEQAN3_HAS_BZIP2