-
Notifications
You must be signed in to change notification settings - Fork 3
/
vcf_file.cpp
executable file
·181 lines (157 loc) · 3.62 KB
/
vcf_file.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* vcf_file.cpp
*
* Created on: Dec 11, 2012
* Author: amarcketta
*/
#include "vcf_file.h"
vcf_file::vcf_file(const parameters &p, string &vcf_filename, int idx)
{
filename = vcf_filename;
compressed = p.vcf_compressed[idx];
stream = p.stream_in;
gzMAX_LINE_LEN = 0;
N_entries = 0; N_kept_entries = 0;
meta_data = header();
if (stream && compressed)
open_gz();
else if (stream)
{
char first = cin.peek();
if (first == 0x1f)
LOG.error("File starts with gzip magic string. Shouldn't you be using --gzvcf?\n");
file_in = &std::cin;
}
else
open();
read_header();
include_indv = vector<bool>(meta_data.N_indv,true);
}
vcf_file::~vcf_file()
{
close();
}
void vcf_file::read_header()
{
string line;
unsigned int line_index = 0;
line_index += meta_data.add_FILTER_descriptor("ID=PASS,Description=PASS", line_index);
while (!eof())
{
read_line(line);
if (line[0] == '#')
if (line[1] == '#')
meta_data.parse_meta(line, line_index);
else
{
meta_data.parse_header(line);
return;
}
else
return;
}
}
void vcf_file::open()
{
struct stat buf;
int i = stat(filename.c_str(), &buf);
if (i != 0)
{
perror("stat error");
LOG.error("Can't determine file type of " + filename, 0);
}
if (!S_ISREG(buf.st_mode))
LOG.error("Does not appear to be a regular file: " + filename, 0);
if (filename.substr(filename.size()-4) == ".bcf")
LOG.error("Filename ends in '.bcf'. Shouldn't you be using --bcf?\n");
if (!compressed)
{
if (filename.substr(filename.size()-3) == ".gz")
LOG.error("Filename ends in '.gz'. Shouldn't you be using --gzvcf or --gzdiff?\n");
file_tmp.open(filename.c_str(), ios::in);
if (!file_tmp.is_open())
LOG.error("Could not open VCF file: " + filename, 0);
file_in = &file_tmp;
}
else
open_gz();
}
void vcf_file::open_gz()
{
gzMAX_LINE_LEN = 1024*1024;
gz_readbuffer = new char[gzMAX_LINE_LEN];
if (stream)
gzfile_in = gzdopen(fileno(stdin), "r");
else
gzfile_in = gzopen(filename.c_str(), "rb");
if (gzfile_in == NULL)
LOG.error("Could not open GZVCF file: " + filename, 0);
#ifdef ZLIB_VERNUM
string tmp(ZLIB_VERSION);
LOG.printLOG("Using zlib version: " + tmp + "\n");
#if (ZLIB_VERNUM >= 0x1240)
gzbuffer(gzfile_in, gzMAX_LINE_LEN); // Included in zlib v1.2.4 and makes things MUCH faster
#else
LOG.printLOG("Versions of zlib >= 1.2.4 will be *much* faster when reading zipped VCF files.\n");
#endif
#endif
}
void vcf_file::close()
{
if (compressed)
{
gzclose(gzfile_in);
delete [] gz_readbuffer;
}
}
bool vcf_file::eof()
{
bool out;
if (!compressed)
out = file_in->eof();
else
out = gzeof(gzfile_in); // Returns 1 when EOF has previously been detected reading the given input stream, otherwise zero.
return out;
}
void vcf_file::get_entry(vector<char> &out)
{
out.resize(0);
read_line(out);
}
entry* vcf_file::get_entry_object()
{
return new vcf_entry(meta_data, include_indv);
}
void vcf_file::read_line(string &out)
{
char * tmp;
out = "";
if (!compressed)
{
getline(*file_in, out);
out.erase( out.find_last_not_of(" \t\n\r") + 1); // Trim whitespace at end of line
}
else
{
bool again = true;
while (again == true)
{
tmp = gzgets(gzfile_in, gz_readbuffer, gzMAX_LINE_LEN);
if (tmp == NULL)
return;
out.append(gz_readbuffer);
if (strlen(gz_readbuffer) != gzMAX_LINE_LEN-1)
again = false;
}
out.erase( out.find_last_not_of(" \t\n\r") + 1); // Trim whitespace at end of line (required in gzipped case!)
}
}
void vcf_file::read_line(vector<char> &out)
{
static string tmp;
tmp="";
out.resize(0);
read_line(tmp);
vector<char> tmp_char(tmp.begin(),tmp.end());
out = tmp_char;
}