3762 |
19 Oct 18 |
peter |
// $Id$ |
3762 |
19 Oct 18 |
peter |
2 |
|
3762 |
19 Oct 18 |
peter |
3 |
/* |
4089 |
07 Sep 21 |
peter |
Copyright (C) 2018, 2019, 2021 Peter Johansson |
3762 |
19 Oct 18 |
peter |
5 |
|
3762 |
19 Oct 18 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
3762 |
19 Oct 18 |
peter |
7 |
|
3762 |
19 Oct 18 |
peter |
The yat library is free software; you can redistribute it and/or |
3762 |
19 Oct 18 |
peter |
modify it under the terms of the GNU General Public License as |
3762 |
19 Oct 18 |
peter |
published by the Free Software Foundation; either version 3 of the |
3762 |
19 Oct 18 |
peter |
License, or (at your option) any later version. |
3762 |
19 Oct 18 |
peter |
12 |
|
3762 |
19 Oct 18 |
peter |
The yat library is distributed in the hope that it will be useful, |
3762 |
19 Oct 18 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
3762 |
19 Oct 18 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3762 |
19 Oct 18 |
peter |
General Public License for more details. |
3762 |
19 Oct 18 |
peter |
17 |
|
3762 |
19 Oct 18 |
peter |
You should have received a copy of the GNU General Public License |
3762 |
19 Oct 18 |
peter |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
3762 |
19 Oct 18 |
peter |
20 |
*/ |
3762 |
19 Oct 18 |
peter |
21 |
|
3762 |
19 Oct 18 |
peter |
22 |
#include <config.h> |
3762 |
19 Oct 18 |
peter |
23 |
|
3762 |
19 Oct 18 |
peter |
24 |
#include "VcfFile.h" |
3789 |
04 Apr 19 |
peter |
25 |
#include "VCF.h" |
3762 |
19 Oct 18 |
peter |
26 |
#include "VcfHeader.h" |
3762 |
19 Oct 18 |
peter |
27 |
|
3762 |
19 Oct 18 |
peter |
28 |
#include <yat/utility/Exception.h> |
3762 |
19 Oct 18 |
peter |
29 |
|
3762 |
19 Oct 18 |
peter |
30 |
#include <boost/iostreams/filter/gzip.hpp> |
3762 |
19 Oct 18 |
peter |
31 |
|
3762 |
19 Oct 18 |
peter |
32 |
#include <cassert> |
3762 |
19 Oct 18 |
peter |
33 |
#include <fstream> |
3762 |
19 Oct 18 |
peter |
34 |
#include <string> |
3762 |
19 Oct 18 |
peter |
35 |
|
3762 |
19 Oct 18 |
peter |
36 |
namespace theplu { |
3762 |
19 Oct 18 |
peter |
37 |
namespace yat { |
3762 |
19 Oct 18 |
peter |
38 |
namespace omic { |
3762 |
19 Oct 18 |
peter |
39 |
|
3762 |
19 Oct 18 |
peter |
40 |
VcfFile::VcfFile(void) |
3762 |
19 Oct 18 |
peter |
41 |
{ |
3762 |
19 Oct 18 |
peter |
42 |
} |
3762 |
19 Oct 18 |
peter |
43 |
|
3762 |
19 Oct 18 |
peter |
44 |
|
3762 |
19 Oct 18 |
peter |
45 |
VcfFile::VcfFile(const std::string& fn) |
3762 |
19 Oct 18 |
peter |
46 |
{ |
3762 |
19 Oct 18 |
peter |
47 |
open(fn); |
3762 |
19 Oct 18 |
peter |
48 |
} |
3762 |
19 Oct 18 |
peter |
49 |
|
3762 |
19 Oct 18 |
peter |
50 |
|
3762 |
19 Oct 18 |
peter |
51 |
void VcfFile::close(void) |
3762 |
19 Oct 18 |
peter |
52 |
{ |
3762 |
19 Oct 18 |
peter |
// set pointers to NULL |
3762 |
19 Oct 18 |
peter |
54 |
header_.reset(); |
3762 |
19 Oct 18 |
peter |
55 |
if (file_stream_) |
3762 |
19 Oct 18 |
peter |
56 |
file_stream_.reset(); |
3771 |
25 Oct 18 |
peter |
57 |
|
3771 |
25 Oct 18 |
peter |
58 |
fis_.reset(); |
3762 |
19 Oct 18 |
peter |
59 |
} |
3762 |
19 Oct 18 |
peter |
60 |
|
3762 |
19 Oct 18 |
peter |
61 |
|
3773 |
26 Oct 18 |
peter |
62 |
bool VcfFile::good(void) const |
3773 |
26 Oct 18 |
peter |
63 |
{ |
3773 |
26 Oct 18 |
peter |
64 |
return is_open() && fis_.good(); |
3773 |
26 Oct 18 |
peter |
65 |
} |
3773 |
26 Oct 18 |
peter |
66 |
|
3773 |
26 Oct 18 |
peter |
67 |
|
3762 |
19 Oct 18 |
peter |
68 |
VcfHeader& VcfFile::header(void) |
3762 |
19 Oct 18 |
peter |
69 |
{ |
3762 |
19 Oct 18 |
peter |
70 |
assert(header_); |
3762 |
19 Oct 18 |
peter |
71 |
return *header_; |
3762 |
19 Oct 18 |
peter |
72 |
} |
3762 |
19 Oct 18 |
peter |
73 |
|
3762 |
19 Oct 18 |
peter |
74 |
|
3762 |
19 Oct 18 |
peter |
75 |
const VcfHeader& VcfFile::header(void) const |
3762 |
19 Oct 18 |
peter |
76 |
{ |
3762 |
19 Oct 18 |
peter |
77 |
assert(header_); |
3762 |
19 Oct 18 |
peter |
78 |
return *header_; |
3762 |
19 Oct 18 |
peter |
79 |
} |
3762 |
19 Oct 18 |
peter |
80 |
|
3762 |
19 Oct 18 |
peter |
81 |
|
3762 |
19 Oct 18 |
peter |
82 |
bool VcfFile::is_open(void) const |
3762 |
19 Oct 18 |
peter |
83 |
{ |
3762 |
19 Oct 18 |
peter |
84 |
return header_ != NULL; |
3762 |
19 Oct 18 |
peter |
85 |
} |
3762 |
19 Oct 18 |
peter |
86 |
|
3762 |
19 Oct 18 |
peter |
87 |
|
3762 |
19 Oct 18 |
peter |
88 |
void VcfFile::open(const std::string& fn) |
3762 |
19 Oct 18 |
peter |
89 |
{ |
3764 |
22 Oct 18 |
peter |
// Currently, if object is already open, the behaviour is |
3764 |
22 Oct 18 |
peter |
// undefined. What is the expected/desired behaviour? |
3762 |
19 Oct 18 |
peter |
92 |
assert(!is_open()); |
3762 |
19 Oct 18 |
peter |
93 |
name_ = fn; |
3762 |
19 Oct 18 |
peter |
94 |
|
3762 |
19 Oct 18 |
peter |
// pointer to input stream we're working on, either cin of |
3762 |
19 Oct 18 |
peter |
// constructed file stream. |
3762 |
19 Oct 18 |
peter |
97 |
std::istream* is = NULL; |
3762 |
19 Oct 18 |
peter |
98 |
if (fn == "-") |
3762 |
19 Oct 18 |
peter |
99 |
is = &std::cin; |
3762 |
19 Oct 18 |
peter |
100 |
else { |
3764 |
22 Oct 18 |
peter |
101 |
file_stream_.reset(new std::ifstream(fn.c_str())); |
3762 |
19 Oct 18 |
peter |
102 |
is = file_stream_.get(); |
3762 |
19 Oct 18 |
peter |
103 |
} |
3762 |
19 Oct 18 |
peter |
104 |
|
3762 |
19 Oct 18 |
peter |
// if stream is no good, throw |
3762 |
19 Oct 18 |
peter |
106 |
if (!is || !is->good()) { |
3762 |
19 Oct 18 |
peter |
107 |
std::ostringstream os; |
3762 |
19 Oct 18 |
peter |
108 |
os << "failed to open '" << fn << "'"; |
3762 |
19 Oct 18 |
peter |
109 |
throw utility::IO_error(os.str()); |
3762 |
19 Oct 18 |
peter |
110 |
} |
3762 |
19 Oct 18 |
peter |
111 |
|
3762 |
19 Oct 18 |
peter |
112 |
|
3762 |
19 Oct 18 |
peter |
// peek at the first character and determine if compressed or not |
3762 |
19 Oct 18 |
peter |
// if compressed, first char in stream is 0x1f (31) |
3762 |
19 Oct 18 |
peter |
115 |
bool compressed = (is->peek() == 31); |
3762 |
19 Oct 18 |
peter |
116 |
if (compressed) |
3762 |
19 Oct 18 |
peter |
117 |
fis_.push(boost::iostreams::gzip_decompressor()); |
3762 |
19 Oct 18 |
peter |
118 |
|
3762 |
19 Oct 18 |
peter |
119 |
fis_.push(*is); |
3762 |
19 Oct 18 |
peter |
120 |
assert(fis_.is_complete()); |
3762 |
19 Oct 18 |
peter |
121 |
|
3762 |
19 Oct 18 |
peter |
122 |
try { |
3764 |
22 Oct 18 |
peter |
123 |
assert(!header_); |
3764 |
22 Oct 18 |
peter |
124 |
header_.reset(new VcfHeader(fis_)); |
3762 |
19 Oct 18 |
peter |
125 |
} |
3762 |
19 Oct 18 |
peter |
126 |
catch (utility::IO_error& e) { |
3762 |
19 Oct 18 |
peter |
127 |
std::ostringstream os; |
3762 |
19 Oct 18 |
peter |
128 |
os << "failed to open '" << fn << "' as "; |
3762 |
19 Oct 18 |
peter |
// compressed case |
3762 |
19 Oct 18 |
peter |
130 |
if (compressed) |
3762 |
19 Oct 18 |
peter |
131 |
os << "a compressed "; |
3762 |
19 Oct 18 |
peter |
132 |
else |
3762 |
19 Oct 18 |
peter |
133 |
os << "an uncompressed "; |
3762 |
19 Oct 18 |
peter |
134 |
os << "VCF file: " << e.what(); |
4060 |
10 May 21 |
peter |
135 |
std::throw_with_nested(utility::runtime_error(os.str())); |
3762 |
19 Oct 18 |
peter |
136 |
} |
3762 |
19 Oct 18 |
peter |
137 |
assert(header_); |
3762 |
19 Oct 18 |
peter |
138 |
assert(is_open()); |
3762 |
19 Oct 18 |
peter |
139 |
} |
3762 |
19 Oct 18 |
peter |
140 |
|
3773 |
26 Oct 18 |
peter |
141 |
|
3773 |
26 Oct 18 |
peter |
142 |
bool VcfFile::read(VCF& vcf) |
3773 |
26 Oct 18 |
peter |
143 |
{ |
3773 |
26 Oct 18 |
peter |
144 |
fis_ >> vcf; |
3773 |
26 Oct 18 |
peter |
145 |
return good(); |
3773 |
26 Oct 18 |
peter |
146 |
} |
3773 |
26 Oct 18 |
peter |
147 |
|
3762 |
19 Oct 18 |
peter |
148 |
}}} |