A library for working with phylogenetic data.
v0.25.0
hts_file.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2020 Lucas Czech
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
31 #ifdef GENESIS_HTSLIB
32 
34 
35 extern "C" {
36  #include <htslib/hts.h>
37 }
38 
39 #include <cstdlib>
40 #include <stdexcept>
41 
42 namespace genesis {
43 namespace population {
44 
45 // =================================================================================================
46 // Constructors and Rule of Five
47 // =================================================================================================
48 
50  std::string const& file_name,
51  std::string const& mode
52 )
53  : file_name_( file_name )
54  , hts_file_( ::hts_open( file_name.c_str(), mode.c_str() ))
55 {
56  // Check if file opening worked.
57  if( ! hts_file_ ) {
58  throw std::runtime_error( "Failed to open htslib file " + file_name );
59  }
60 }
61 
63 {
64  if( hts_file_ ) {
65  ::hts_close( hts_file_ );
66  }
67 }
68 
70 {
71  // We need a custom move, as the explicitly defaulted one "moves" the hts_file_ pointer, which
72  // effectively copies it, and then upon destruction of the moved-from object frees the hts_file_.
73  // So, instead we swap, so that once `other` gets destroyed (as it is moved from, it will go
74  // out of scope soon), our current data of `this` gets also destroyed with it.
75  std::swap( file_name_, other.file_name_ );
76  std::swap( hts_file_, other.hts_file_ );
77 }
78 
80 {
81  // Same reasoning as above. Need additional check for self assignment,
82  // as otherwise we'd destroy the header as well.
83  if( this == &other ) {
84  return *this;
85  }
86  std::swap( file_name_, other.file_name_ );
87  std::swap( hts_file_, other.hts_file_ );
88  return *this;
89 }
90 
91 // =================================================================================================
92 // Accessors
93 // =================================================================================================
94 
95 std::string HtsFile::format_description() const
96 {
97  ::htsFormat const* fmt_ptr = ::hts_get_format( hts_file_ );
98  char* fmt_str = ::hts_format_description( fmt_ptr );
99  auto const ret = std::string( fmt_str );
100  free( fmt_str );
101  return ret;
102 }
103 
104 std::string HtsFile::format_extension() const
105 {
106  ::htsFormat const* fmt_ptr = ::hts_get_format( hts_file_ );
107  return std::string( ::hts_format_file_extension( fmt_ptr ));
108 }
109 
110 } // namespace population
111 } // namespace genesis
112 
113 #endif // htslib guard
genesis::placement::swap
void swap(Sample &lhs, Sample &rhs)
Definition: sample.cpp:104
genesis::population::HtsFile::HtsFile
HtsFile()=default
Create an empty instance, with no file attached.
genesis::population::HtsFile::format_extension
std::string format_extension() const
Return the file format extension.
Definition: hts_file.cpp:104
genesis::population::HtsFile::format_description
std::string format_description() const
Return a human-readable description of the file format.
Definition: hts_file.cpp:95
genesis::population::HtsFile::file_name
std::string const & file_name() const
Definition: hts_file.hpp:92
genesis::population::HtsFile::operator=
HtsFile & operator=(HtsFile const &)=delete
hts_file.hpp
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::population::HtsFile::~HtsFile
~HtsFile()
Definition: hts_file.cpp:62
genesis::population::HtsFile
Wrap an ::htsFile struct.
Definition: hts_file.hpp:56