|
| 1 | +// ----------------------------------------------------------------------------------------------------- |
| 2 | +// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin |
| 3 | +// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik |
| 4 | +// Copyright (c) 2020-2021, deCODE Genetics |
| 5 | +// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License |
| 6 | +// shipped with this file and also available at: https://github.com/seqan/b.i.o./blob/master/LICENSE |
| 7 | +// ----------------------------------------------------------------------------------------------------- |
| 8 | + |
| 9 | +#include <algorithm> |
| 10 | +#include <sstream> |
| 11 | + |
| 12 | +#include <gtest/gtest.h> |
| 13 | + |
| 14 | +#include <seqan3/alphabet/nucleotide/dna5.hpp> |
| 15 | +#include <seqan3/test/expect_range_eq.hpp> |
| 16 | +#include <seqan3/test/expect_same_type.hpp> |
| 17 | +#include <seqan3/test/tmp_filename.hpp> |
| 18 | + |
| 19 | +#include <bio/map_io/reader.hpp> |
| 20 | + |
| 21 | +#include "data.hpp" |
| 22 | + |
| 23 | +TEST(map_io_reader, concepts) |
| 24 | +{ |
| 25 | + using t = bio::map_io::reader<>; |
| 26 | + EXPECT_TRUE((std::ranges::input_range<t>)); |
| 27 | + |
| 28 | + using ct = bio::map_io::reader<> const; |
| 29 | + // not const-iterable |
| 30 | + EXPECT_FALSE((std::ranges::input_range<ct>)); |
| 31 | +} |
| 32 | + |
| 33 | +void map_io_reader_filename_constructor(bool ext_check, auto &&... args) |
| 34 | +{ |
| 35 | + /* just the filename */ |
| 36 | + { |
| 37 | + seqan3::test::tmp_filename filename{"map_io_reader_constructor.sam"}; |
| 38 | + std::ofstream filecreator{filename.get_path(), std::ios::out | std::ios::binary}; |
| 39 | + |
| 40 | + EXPECT_NO_THROW((bio::map_io::reader{filename.get_path(), std::forward<decltype(args)>(args)...})); |
| 41 | + } |
| 42 | + |
| 43 | + // correct format check is done by tests of that format |
| 44 | + |
| 45 | + /* non-existent file */ |
| 46 | + { |
| 47 | + EXPECT_THROW((bio::map_io::reader{"/dev/nonexistant/foobarOOO", std::forward<decltype(args)>(args)...}), |
| 48 | + bio::file_open_error); |
| 49 | + } |
| 50 | + |
| 51 | + /* wrong extension */ |
| 52 | + if (ext_check) |
| 53 | + { |
| 54 | + seqan3::test::tmp_filename filename{"map_io_reader_constructor.xyz"}; |
| 55 | + std::ofstream filecreator{filename.get_path(), std::ios::out | std::ios::binary}; |
| 56 | + EXPECT_THROW((bio::map_io::reader{filename.get_path(), std::forward<decltype(args)>(args)...}), |
| 57 | + bio::unhandled_extension_error); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +TEST(map_io_reader, constructor1_just_filename) |
| 62 | +{ |
| 63 | + map_io_reader_filename_constructor(true); |
| 64 | + EXPECT_TRUE((std::same_as<decltype(bio::map_io::reader{""}), bio::map_io::reader<>>)); |
| 65 | +} |
| 66 | + |
| 67 | +TEST(map_io_reader, constructor1_with_opts) |
| 68 | +{ |
| 69 | + bio::map_io::reader_options opt{.field_types = bio::map_io::field_types_sam<>}; |
| 70 | + using control_t = bio::map_io::reader<std::remove_cvref_t<decltype(bio::map_io::default_field_ids)>, |
| 71 | + std::remove_cvref_t<decltype(bio::map_io::field_types_sam<>)>, |
| 72 | + seqan3::type_list<bio::sam>>; |
| 73 | + |
| 74 | + map_io_reader_filename_constructor(true, std::move(opt)); |
| 75 | + EXPECT_TRUE((std::same_as<decltype(bio::map_io::reader{"", opt}), control_t>)); |
| 76 | +} |
| 77 | + |
| 78 | +TEST(map_io_reader, constructor2_just_filename_direct_format) |
| 79 | +{ |
| 80 | + map_io_reader_filename_constructor(false, bio::sam{}); |
| 81 | + EXPECT_TRUE((std::same_as<decltype(bio::map_io::reader{"", bio::sam{}}), bio::map_io::reader<>>)); |
| 82 | +} |
| 83 | + |
| 84 | +TEST(map_io_reader, constructor2_with_opts_direct_format) |
| 85 | +{ |
| 86 | + bio::map_io::reader_options opt{.field_types = bio::map_io::field_types_sam<>}; |
| 87 | + using control_t = bio::map_io::reader<std::remove_cvref_t<decltype(bio::map_io::default_field_ids)>, |
| 88 | + std::remove_cvref_t<decltype(bio::map_io::field_types_sam<>)>, |
| 89 | + seqan3::type_list<bio::sam>>; |
| 90 | + |
| 91 | + map_io_reader_filename_constructor(false, bio::sam{}, std::move(opt)); |
| 92 | + EXPECT_TRUE((std::same_as<decltype(bio::map_io::reader{"", bio::sam{}, opt}), control_t>)); |
| 93 | +} |
| 94 | + |
| 95 | +TEST(map_io_reader, constructor2_just_filename_format_variant) |
| 96 | +{ |
| 97 | + std::variant<bio::sam> var{}; |
| 98 | + |
| 99 | + map_io_reader_filename_constructor(false, var); |
| 100 | + EXPECT_TRUE((std::same_as<decltype(bio::map_io::reader{"", var}), bio::map_io::reader<>>)); |
| 101 | +} |
| 102 | + |
| 103 | +TEST(map_io_reader, constructor2_with_opts_format_variant) |
| 104 | +{ |
| 105 | + std::variant<bio::sam> var{}; |
| 106 | + bio::map_io::reader_options opt{.field_types = bio::map_io::field_types_sam<>}; |
| 107 | + using control_t = bio::map_io::reader<std::remove_cvref_t<decltype(bio::map_io::default_field_ids)>, |
| 108 | + std::remove_cvref_t<decltype(bio::map_io::field_types_sam<>)>, |
| 109 | + seqan3::type_list<bio::sam>>; |
| 110 | + |
| 111 | + map_io_reader_filename_constructor(false, var, std::move(opt)); |
| 112 | + EXPECT_TRUE((std::same_as<decltype(bio::map_io::reader{"", var, std::move(opt)}), control_t>)); |
| 113 | +} |
| 114 | + |
| 115 | +TEST(map_io_reader, constructor3) |
| 116 | +{ |
| 117 | + std::istringstream str; |
| 118 | + |
| 119 | + EXPECT_NO_THROW((bio::map_io::reader{str, bio::sam{}})); |
| 120 | + EXPECT_TRUE((std::same_as<decltype(bio::map_io::reader{str, bio::sam{}}), bio::map_io::reader<>>)); |
| 121 | +} |
| 122 | + |
| 123 | +TEST(map_io_reader, constructor3_with_opts) |
| 124 | +{ |
| 125 | + std::istringstream str; |
| 126 | + bio::map_io::reader_options opt{.field_types = bio::map_io::field_types_sam<>}; |
| 127 | + using control_t = bio::map_io::reader<std::remove_cvref_t<decltype(bio::map_io::default_field_ids)>, |
| 128 | + std::remove_cvref_t<decltype(bio::map_io::field_types_sam<>)>, |
| 129 | + seqan3::type_list<bio::sam>>; |
| 130 | + |
| 131 | + EXPECT_NO_THROW((bio::map_io::reader{str, bio::sam{}, opt})); |
| 132 | + EXPECT_TRUE((std::same_as<decltype(bio::map_io::reader{str, bio::sam{}, opt}), control_t>)); |
| 133 | +} |
| 134 | + |
| 135 | +TEST(map_io_reader, constructor4) |
| 136 | +{ |
| 137 | + std::istringstream str; |
| 138 | + |
| 139 | + EXPECT_NO_THROW((bio::map_io::reader{std::move(str), bio::sam{}})); |
| 140 | + EXPECT_TRUE((std::same_as<decltype(bio::map_io::reader{std::move(str), bio::sam{}}), bio::map_io::reader<>>)); |
| 141 | +} |
| 142 | + |
| 143 | +TEST(map_io_reader, constructor4_with_opts) |
| 144 | +{ |
| 145 | + std::istringstream str; |
| 146 | + bio::map_io::reader_options opt{.field_types = bio::map_io::field_types_sam<>}; |
| 147 | + using control_t = bio::map_io::reader<std::remove_cvref_t<decltype(bio::map_io::default_field_ids)>, |
| 148 | + std::remove_cvref_t<decltype(bio::map_io::field_types_sam<>)>, |
| 149 | + seqan3::type_list<bio::sam>>; |
| 150 | + |
| 151 | + EXPECT_NO_THROW((bio::map_io::reader{std::move(str), bio::sam{}, opt})); |
| 152 | + EXPECT_TRUE((std::same_as<decltype(bio::map_io::reader{std::move(str), bio::sam{}, opt}), control_t>)); |
| 153 | +} |
| 154 | + |
| 155 | +TEST(map_io_reader, iteration) |
| 156 | +{ |
| 157 | + { |
| 158 | + std::istringstream str{static_cast<std::string>(input)}; |
| 159 | + bio::map_io::reader reader{str, bio::sam{}}; |
| 160 | + |
| 161 | + EXPECT_EQ(std::ranges::distance(reader), 3); |
| 162 | + } |
| 163 | + |
| 164 | + { |
| 165 | + std::istringstream str{static_cast<std::string>(input)}; |
| 166 | + bio::map_io::reader reader{str, bio::sam{}}; |
| 167 | + |
| 168 | + size_t count = 0; |
| 169 | + for (auto & rec : reader) |
| 170 | + { |
| 171 | + ++count; |
| 172 | + EXPECT_TRUE(rec.id().starts_with("read")); |
| 173 | + // only very basic check here, rest in format test |
| 174 | + } |
| 175 | + EXPECT_EQ(count, 3); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +TEST(map_io_reader, empty_file) |
| 180 | +{ |
| 181 | + { |
| 182 | + seqan3::test::tmp_filename filename{"map_io_reader_constructor.sam"}; |
| 183 | + std::ofstream filecreator{filename.get_path(), std::ios::out | std::ios::binary}; |
| 184 | + |
| 185 | + bio::map_io::reader reader{filename.get_path()}; |
| 186 | + |
| 187 | + EXPECT_THROW(reader.begin(), bio::file_open_error); |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +TEST(map_io_reader, empty_stream) |
| 192 | +{ |
| 193 | + { |
| 194 | + std::istringstream str{""}; |
| 195 | + bio::map_io::reader reader{str, bio::sam{}}; |
| 196 | + |
| 197 | + EXPECT_THROW(reader.begin(), bio::file_open_error); |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +// TEST(map_io_reader, custom_field_types) |
| 202 | +// { |
| 203 | +// bio::map_io::reader_options opt{.field_types = bio::map_io::field_types<bio::ownership::deep>}; |
| 204 | + |
| 205 | +// std::istringstream str{static_cast<std::string>(input)}; |
| 206 | +// bio::map_io::reader reader{str, bio::sam{}, opt}; |
| 207 | + |
| 208 | +// EXPECT_TRUE((std::same_as<decltype(reader.front().seq()), std::vector<seqan3::dna5> &>)); |
| 209 | +// EXPECT_TRUE((std::same_as<decltype(reader.front().id()), std::string &>)); |
| 210 | +// } |
| 211 | + |
| 212 | +TEST(map_io_reader, custom_field_ids_structured_bindings) |
| 213 | +{ |
| 214 | + bio::map_io::reader_options opt{.field_ids = bio::vtag<bio::field::seq, bio::field::id>, |
| 215 | + .field_types = bio::ttag<std::string, std::string>}; |
| 216 | + |
| 217 | + std::istringstream str{static_cast<std::string>(input)}; |
| 218 | + bio::map_io::reader reader{str, bio::sam{}, opt}; |
| 219 | + |
| 220 | + for (auto & [seq, id] : reader) |
| 221 | + EXPECT_TRUE(id.starts_with("read")); |
| 222 | +} |
| 223 | + |
| 224 | +TEST(map_io_reader, decompression_filename) |
| 225 | +{ |
| 226 | + seqan3::test::tmp_filename filename{"map_io_reader.sam.gz"}; |
| 227 | + |
| 228 | + { |
| 229 | + std::ofstream filecreator{filename.get_path(), std::ios::out | std::ios::binary}; |
| 230 | + bio::detail::fast_ostreambuf_iterator it{filecreator}; |
| 231 | + it.write_range(input_bgzipped); |
| 232 | + } |
| 233 | + |
| 234 | + bio::map_io::reader reader{filename.get_path()}; |
| 235 | + |
| 236 | + size_t count = 0; |
| 237 | + for (auto & rec : reader) |
| 238 | + { |
| 239 | + ++count; |
| 240 | + EXPECT_TRUE(rec.id().starts_with("read")); |
| 241 | + // only very basic check here, rest in format test |
| 242 | + } |
| 243 | + EXPECT_EQ(count, 3); |
| 244 | +} |
| 245 | + |
| 246 | +TEST(map_io_reader, decompression_stream) |
| 247 | +{ |
| 248 | + std::istringstream str{static_cast<std::string>(input_bgzipped)}; |
| 249 | + |
| 250 | + bio::map_io::reader reader{str, bio::sam{}}; |
| 251 | + |
| 252 | + size_t count = 0; |
| 253 | + for (auto & rec : reader) |
| 254 | + { |
| 255 | + ++count; |
| 256 | + EXPECT_TRUE(rec.id().starts_with("read")); |
| 257 | + // only very basic check here, rest in format test |
| 258 | + } |
| 259 | + EXPECT_EQ(count, 3); |
| 260 | +} |
| 261 | + |
| 262 | +TEST(map_io_reader, get_header) |
| 263 | +{ |
| 264 | + // get header before calling begin() |
| 265 | + { |
| 266 | + std::istringstream str{static_cast<std::string>(input)}; |
| 267 | + bio::map_io::reader reader{str, bio::sam{}}; |
| 268 | + |
| 269 | + bio::map_io::header const & hdr = reader.header(); |
| 270 | + |
| 271 | + EXPECT_EQ(hdr.format_version, "1.6"); |
| 272 | + } |
| 273 | + |
| 274 | + // get header after calling begin() |
| 275 | + { |
| 276 | + std::istringstream str{static_cast<std::string>(input)}; |
| 277 | + bio::map_io::reader reader{str, bio::sam{}}; |
| 278 | + |
| 279 | + auto it = reader.begin(); |
| 280 | + EXPECT_EQ(it->id(), "read1"); |
| 281 | + |
| 282 | + bio::map_io::header const & hdr = reader.header(); |
| 283 | + |
| 284 | + EXPECT_EQ(hdr.format_version, "1.6"); |
| 285 | + } |
| 286 | +} |
0 commit comments