SeqAn3
The Modern C++ library for sequence analysis.
misc_output.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2019, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2019, 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 
28 namespace seqan3::detail
29 {
30 
37 template<Char char_t>
38 inline auto make_secondary_ostream(std::basic_ostream<char_t> & primary_stream, std::filesystem::path & filename)
40 {
41  // don't assume ownership
42  constexpr auto stream_deleter_noop = [] (std::basic_ostream<char_t> *) {};
43  // assume ownership
44  [[maybe_unused]] constexpr auto stream_deleter_default = [] (std::basic_ostream<char_t> * ptr) { delete ptr; };
45 
46  std::string extension = filename.extension().string();
47 
48  if ((extension == ".gz") || (extension == ".bgzf") || (extension == ".bam"))
49  {
50  #ifdef SEQAN3_HAS_ZLIB
51  if (extension != ".bam") // remove extension except for bam
52  filename.replace_extension("");
53 
54  return {new contrib::basic_bgzf_ostream<char_t>{primary_stream},
55  stream_deleter_default};
56  #else
57  throw file_open_error{"Trying to write a gzipped file, but no ZLIB available."};
58  #endif
59  }
60  else if (extension == ".bz2")
61  {
62  #ifdef SEQAN3_HAS_BZIP2
63  filename.replace_extension("");
64  return {new contrib::basic_bz2_ostream<char_t>{primary_stream}, stream_deleter_default};
65  #else
66  throw file_open_error{"Trying to write a bzipped file, but no libbz2 available."};
67  #endif
68  }
69  else if (extension == ".zst")
70  {
71  throw file_open_error{"Trying to write a zst'ed file, but SeqAn3 does not yet support this."};
72  }
73 
74  return {&primary_stream, stream_deleter_noop};
75 }
76 
77 } // namespace seqan3::detail
Provides concepts for core language types and relations that don&#39;t have concepts in C++20 (yet)...
Definition: aligned_sequence_concept.hpp:35