Skip to content

Commit

Permalink
[MISC] Make seqan3::sam_file_header::program_info_t easier to copy
Browse files Browse the repository at this point in the history
See #3137.
  • Loading branch information
tsnorri authored and eseiler committed Apr 24, 2023
1 parent 00fb372 commit b3a9a6f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
32 changes: 19 additions & 13 deletions include/seqan3/io/sam_file/header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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].
Expand Down
37 changes: 37 additions & 0 deletions test/unit/io/sam_file/sam_file_output_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#include <gtest/gtest.h>

#include <iterator>
#include <ranges>
#include <sstream>
#include <type_traits>

#include <seqan3/io/sam_file/input.hpp>
#include <seqan3/io/sam_file/output.hpp>
Expand Down Expand Up @@ -197,6 +199,41 @@ void assign_impl(source_t && source)
EXPECT_EQ(reinterpret_cast<std::ostringstream &>(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<decltype(input_header.ref_ids())>
input_ref_ids_type; // std::deque <std::string> by default.
typedef std::array<std::string, 0> output_ref_ids_type;
static_assert(!std::is_same_v<input_ref_ids_type, output_ref_ids_type>);

seqan3::sam_file_output fout{std::ostringstream{},
output_ref_ids_type{},
std::ranges::empty_view<std::size_t>{},
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
// ----------------------------------------------------------------------------
Expand Down

0 comments on commit b3a9a6f

Please sign in to comment.