SeqAn3  3.1.0-rc.1
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 <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
28 #include <seqan3/io/exception.hpp>
30 
31 namespace seqan3::detail
32 {
33 
41 template <builtin_character char_t>
44 {
45  // don't assume ownership
46  constexpr auto stream_deleter_noop = [] (std::basic_ostream<char_t> *) {};
47  // assume ownership
48  [[maybe_unused]] constexpr auto stream_deleter_default = [] (std::basic_ostream<char_t> * ptr) { delete ptr; };
49 
50  std::string extension = filename.extension().string();
51 
52  if (extension == ".gz")
53  {
54 #if defined(SEQAN3_HAS_ZLIB)
55  filename.replace_extension("");
56  return {new contrib::basic_gz_ostream<char_t>{primary_stream}, 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 == ".bgzf") || (extension == ".bam"))
62  {
63 #if defined(SEQAN3_HAS_ZLIB)
64  if (extension != ".bam") // remove extension except for bam
65  filename.replace_extension("");
66 
67  return {new contrib::basic_bgzf_ostream<char_t>{primary_stream}, stream_deleter_default};
68 #else
69  throw file_open_error{"Trying to write a bgzf'ed file, but no ZLIB available."};
70 #endif
71  }
72  else if (extension == ".bz2")
73  {
74 #if defined(SEQAN3_HAS_BZIP2)
75  filename.replace_extension("");
76  return {new contrib::basic_bz2_ostream<char_t>{primary_stream}, stream_deleter_default};
77 #else
78  throw file_open_error{"Trying to write a bzipped file, but no libbz2 available."};
79 #endif
80  }
81  else if (extension == ".zst")
82  {
83  throw file_open_error{"Trying to write a zst'ed file, but SeqAn does not yet support this."};
84  }
85 
86  return {&primary_stream, stream_deleter_noop};
87 }
88 
89 } // 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...
auto make_secondary_ostream(std::basic_ostream< char_t > &primary_stream, std::filesystem::path &filename) -> std::unique_ptr< std::basic_ostream< char_t >, std::function< void(std::basic_ostream< char_t > *)>>
Depending on the given filename/extension, create a compression stream or just forward the primary st...
Definition: misc_output.hpp:42
Provides exceptions used in the I/O module.
The internal SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
Thrown if there is an unspecified filesystem or stream error while opening, e.g. permission problem.
Definition: exception.hpp:39