SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
misc_output.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2006-2023, Knut Reinert & Freie Universität Berlin
3// Copyright (c) 2016-2023, Knut Reinert & MPI für molekulare Genetik
4// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5// shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6// -----------------------------------------------------------------------------------------------------
7
13#pragma once
14
15#include <filesystem>
16#include <functional>
17#include <iostream>
18#include <string>
19#include <tuple>
20
21#if defined(SEQAN3_HAS_BZIP2)
22# include <seqan3/contrib/stream/bz2_ostream.hpp>
23#endif
24#if defined(SEQAN3_HAS_ZLIB)
25# include <seqan3/contrib/stream/bgzf_ostream.hpp>
26# include <seqan3/contrib/stream/gz_ostream.hpp>
27#endif
31
32namespace seqan3::detail
33{
34
42template <builtin_character char_t>
43inline auto make_secondary_ostream(std::basic_ostream<char_t> & primary_stream, std::filesystem::path & filename)
45{
46 // don't assume ownership
47 constexpr auto stream_deleter_noop = [](std::basic_ostream<char_t> *) {};
48 // assume ownership
49 [[maybe_unused]] constexpr auto stream_deleter_default = [](std::basic_ostream<char_t> * ptr)
50 {
51 delete ptr;
52 };
53
54 std::string extension = filename.extension().string();
55
56 if (extension == ".gz")
57 {
58#if defined(SEQAN3_HAS_ZLIB)
59 filename.replace_extension("");
60 return {new contrib::basic_gz_ostream<char_t>{primary_stream}, stream_deleter_default};
61#else
62 throw file_open_error{"Trying to write a gzipped file, but no ZLIB available."};
63#endif
64 }
65 else if ((extension == ".bgzf") || (extension == ".bam"))
66 {
67#if defined(SEQAN3_HAS_ZLIB)
68 if (extension != ".bam") // remove extension except for bam
69 filename.replace_extension("");
70
71 return {new contrib::basic_bgzf_ostream<char_t>{primary_stream}, stream_deleter_default};
72#else
73 throw file_open_error{"Trying to write a bgzf'ed file, but no ZLIB available."};
74#endif
75 }
76 else if (extension == ".bz2")
77 {
78#if defined(SEQAN3_HAS_BZIP2)
79 filename.replace_extension("");
80 return {new contrib::basic_bz2_ostream<char_t>{primary_stream}, stream_deleter_default};
81#else
82 throw file_open_error{"Trying to write a bzipped file, but no libbz2 available."};
83#endif
84 }
85 else if (extension == ".zst")
86 {
87 throw file_open_error{"Trying to write a zst'ed file, but SeqAn does not yet support this."};
88 }
89
90 return {&primary_stream, stream_deleter_noop};
91}
92
93} // namespace seqan3::detail
Provides seqan3::contrib::bgzf_thread_count.
Provides exceptions used in the I/O module.
Provides concepts that do not have equivalents in C++20.