SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
misc_output.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2021, 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 <seqan3/std/filesystem>
16 #include <iostream>
17 #include <string>
18 #include <tuple>
19 
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/io/exception.hpp>
29 
30 namespace seqan3::detail
31 {
32 
39 template <builtin_character char_t>
40 inline auto make_secondary_ostream(std::basic_ostream<char_t> & primary_stream, std::filesystem::path & filename)
42 {
43  // don't assume ownership
44  constexpr auto stream_deleter_noop = [] (std::basic_ostream<char_t> *) {};
45  // assume ownership
46  [[maybe_unused]] constexpr auto stream_deleter_default = [] (std::basic_ostream<char_t> * ptr) { delete ptr; };
47 
48  std::string extension = filename.extension().string();
49 
50  if (extension == ".gz")
51  {
52 #ifdef SEQAN3_HAS_ZLIB
53  filename.replace_extension("");
54  return {new contrib::basic_gz_ostream<char_t>{primary_stream}, stream_deleter_default};
55 #else
56  throw file_open_error{"Trying to write a gzipped file, but no ZLIB available."};
57 #endif
58  }
59  else if ((extension == ".bgzf") || (extension == ".bam"))
60  {
61 #ifdef SEQAN3_HAS_ZLIB
62  if (extension != ".bam") // remove extension except for bam
63  filename.replace_extension("");
64 
65  return {new contrib::basic_bgzf_ostream<char_t>{primary_stream}, stream_deleter_default};
66 #else
67  throw file_open_error{"Trying to write a bgzf'ed file, but no ZLIB available."};
68 #endif
69  }
70  else if (extension == ".bz2")
71  {
72 #ifdef SEQAN3_HAS_BZIP2
73  filename.replace_extension("");
74  return {new contrib::basic_bz2_ostream<char_t>{primary_stream}, stream_deleter_default};
75 #else
76  throw file_open_error{"Trying to write a bzipped file, but no libbz2 available."};
77 #endif
78  }
79  else if (extension == ".zst")
80  {
81  throw file_open_error{"Trying to write a zst'ed file, but SeqAn does not yet support this."};
82  }
83 
84  return {&primary_stream, stream_deleter_noop};
85 }
86 
87 } // namespace seqan3::detail
Provides concepts for core language types and relations that don't have concepts in C++20 (yet).
This header includes C++17 filesystem support and imports it into namespace std::filesystem (independ...
Provides exceptions used in the I/O module.