SeqAn3  3.0.2
The Modern C++ library for sequence analysis.
misc_output.hpp
Go to the documentation of this file.
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 // 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 <iostream>
16 #include <string>
17 #include <tuple>
18 
20 #ifdef SEQAN3_HAS_BZIP2
21  #include <seqan3/contrib/stream/bz2_ostream.hpp>
22 #endif
23 #ifdef SEQAN3_HAS_ZLIB
24  #include <seqan3/contrib/stream/bgzf_ostream.hpp>
25  #include <seqan3/contrib/stream/gz_ostream.hpp>
26 #endif
27 #include <seqan3/std/filesystem>
28 
29 namespace seqan3::detail
30 {
31 
38 template <builtin_character char_t>
39 inline auto make_secondary_ostream(std::basic_ostream<char_t> & primary_stream, std::filesystem::path & filename)
41 {
42  // don't assume ownership
43  constexpr auto stream_deleter_noop = [] (std::basic_ostream<char_t> *) {};
44  // assume ownership
45  [[maybe_unused]] constexpr auto stream_deleter_default = [] (std::basic_ostream<char_t> * ptr) { delete ptr; };
46 
47  std::string extension = filename.extension().string();
48 
49  if ((extension == ".gz") || (extension == ".bgzf") || (extension == ".bam"))
50  {
51  #ifdef SEQAN3_HAS_ZLIB
52  if (extension != ".bam") // remove extension except for bam
53  filename.replace_extension("");
54 
55  return {new contrib::basic_bgzf_ostream<char_t>{primary_stream},
56  stream_deleter_default};
57  #else
58  throw file_open_error{"Trying to write a gzipped file, but no ZLIB available."};
59  #endif
60  }
61  else if (extension == ".bz2")
62  {
63  #ifdef SEQAN3_HAS_BZIP2
64  filename.replace_extension("");
65  return {new contrib::basic_bz2_ostream<char_t>{primary_stream}, stream_deleter_default};
66  #else
67  throw file_open_error{"Trying to write a bzipped file, but no libbz2 available."};
68  #endif
69  }
70  else if (extension == ".zst")
71  {
72  throw file_open_error{"Trying to write a zst'ed file, but SeqAn does not yet support this."};
73  }
74 
75  return {&primary_stream, stream_deleter_noop};
76 }
77 
78 } // namespace seqan3::detail
std::string
tuple
std::function
filesystem
This header includes C++17 filesystem support and imports it into namespace std::filesystem (independ...
iostream
std::filesystem::path
std::basic_ostream
core_language.hpp
Provides concepts for core language types and relations that don't have concepts in C++20 (yet).
std::unique_ptr
string