From b3a9a6f9513f2db10aaf5cd558fbc2ea5561edf8 Mon Sep 17 00:00:00 2001 From: Tuukka Norri Date: Thu, 20 Apr 2023 16:01:54 +0300 Subject: [PATCH] [MISC] Make seqan3::sam_file_header::program_info_t easier to copy See #3137. --- include/seqan3/io/sam_file/header.hpp | 32 +++++++++------- .../unit/io/sam_file/sam_file_output_test.cpp | 37 +++++++++++++++++++ 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/include/seqan3/io/sam_file/header.hpp b/include/seqan3/io/sam_file/header.hpp index b98af2ea8d..1e670abdaf 100644 --- a/include/seqan3/io/sam_file/header.hpp +++ b/include/seqan3/io/sam_file/header.hpp @@ -24,6 +24,21 @@ namespace seqan3 { +/*!\brief Stores information of the program/tool that was used to create a SAM/BAM file. + * \ingroup io_sam_file + * + * \remark For a complete overview, take a look at \ref io_sam_file + */ +struct sam_file_program_info_t +{ + std::string id; //!< A unique (file scope) id. + std::string name; //!< The official name. + std::string command_line_call; //!< The command line call that produces the file. + std::string previous; //!< The id of the previous program if program calls were chained. + std::string description; //!< A description of the program and/or program call. + std::string version; //!< The program/tool version. +}; + /*!\brief Stores the header information of SAM/BAM files. * \ingroup io_sam_file * @@ -63,19 +78,10 @@ class sam_file_header {} //!\} - //!\brief Stores information of the program/tool that was used to create the file. - struct program_info_t - { - std::string id; //!< A unique (file scope) id. - std::string name; //!< The official name. - std::string command_line_call; //!< The command line call that produces the file. - std::string previous; //!< The id of the previous program if program calls were chained. - std::string description; //!< A description of the program and/or program call. - std::string version; //!< The program/tool version. - }; - - std::string format_version; //!< The file format version. Note: this is overwritten by our formats on output. - std::string sorting; //!< The sorting of the file. SAM: [unknown, unsorted, queryname, coordinate]. + using program_info_t = + sam_file_program_info_t; //!< Stores information of the program/tool that was used to create the file. + std::string format_version; //!< The file format version. Note: this is overwritten by our formats on output. + std::string sorting; //!< The sorting of the file. SAM: [unknown, unsorted, queryname, coordinate]. std::string subsorting; //!< The sub-sorting of the file. SAM: [unknown, unsorted, queryname, coordinate](:[A-Za-z0-9_-]+)+. std::string grouping; //!< The grouping of the file. SAM: [none, query, reference]. diff --git a/test/unit/io/sam_file/sam_file_output_test.cpp b/test/unit/io/sam_file/sam_file_output_test.cpp index 0d4fc35cb1..b8be9cbe7c 100644 --- a/test/unit/io/sam_file/sam_file_output_test.cpp +++ b/test/unit/io/sam_file/sam_file_output_test.cpp @@ -8,7 +8,9 @@ #include #include +#include #include +#include #include #include @@ -197,6 +199,41 @@ void assign_impl(source_t && source) EXPECT_EQ(reinterpret_cast(fout.get_stream()).str(), output_comp); } +// ---------------------------------------------------------------------------- +// header +// ---------------------------------------------------------------------------- +TEST(header, copy_program_info_t) +{ + std::string comp = + R"(@HD VN:1.6 SO:unknown GO:none +@SQ SN:ref LN:26 +@PG ID:prog1 PN:cool_program +@CO This is a comment. +read1 41 ref 1 61 1S1M1D1M1I ref 10 300 ACGT !##$ AS:i:2 NM:i:7 +)"; + seqan3::sam_file_input fin{std::istringstream{comp}, seqan3::format_sam{}}; + + auto & input_header(fin.header()); + + // We would like to test that (some of) the headers can be copied even if + // ref_ids_types do not match. + typedef std::remove_cvref_t + input_ref_ids_type; // std::deque by default. + typedef std::array output_ref_ids_type; + static_assert(!std::is_same_v); + + seqan3::sam_file_output fout{std::ostringstream{}, + output_ref_ids_type{}, + std::ranges::empty_view{}, + seqan3::format_sam{}}; + fout.header().program_infos = fin.header().program_infos; + + // Verify that the above was sufficient to copy the headers. + auto const & pg_infos(fout.header().program_infos); + EXPECT_FALSE(pg_infos.empty()); + EXPECT_EQ("cool_program", pg_infos.front().name); +} + // ---------------------------------------------------------------------------- // row // ----------------------------------------------------------------------------