-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvcfheader.cpp
127 lines (95 loc) · 2.96 KB
/
vcfheader.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
/*
This file is part of CuteVCF.
Foobar is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Foobar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
@author : Sacha Schutz <[email protected]>
*/
#include "vcfheader.h"
VcfHeader::VcfHeader()
{
}
VcfHeader::VcfHeader(const QByteArray &raw)
{
setRaw(raw);
}
void VcfHeader::setRaw(const QByteArray &raw)
{
mRaw = raw;
mInfos.clear();
mFormats.clear();
mColnames.clear();
mTags.clear();
QByteArrayList lines = mRaw.split('\n');
for (QByteArray b_line : lines)
{
QString line(b_line);
if (line.contains(QRegularExpression("^##[a-zA-Z0-9]+=[^<]"))){
QStringList lines = line.split("=");
QString key = lines.at(0);
QString value = lines.at(1);
mTags.insert(key.replace("##",""), value);
}
if (line.startsWith("##INFO")){
QHash<QString, QVariant> info = captureParams(line);
QString id = info.value("ID","unknown").toString();
mInfos[id] = info;
}
if (line.startsWith("##FORMAT")){
QHash<QString, QVariant> format = captureParams(line);
QString id = format.value("ID","unknown").toString();
mFormats[id] = format;
}
if (line.startsWith("#CHROM")){
mColnames = line.split("\t");
}
}
}
const QByteArray &VcfHeader::raw() const
{
return mRaw;
}
QHash<QString, QVariant> VcfHeader::info(const QString &key) const
{
return mInfos.value(key);
}
QHash<QString, QVariant> VcfHeader::format(const QString &key) const
{
return mFormats.value(key);
}
const QHash<QString, QVariant> &VcfHeader::tags() const
{
return mTags;
}
const QStringList &VcfHeader::colnames() const
{
return mColnames;
}
QHash<QString, QVariant> VcfHeader::captureParams(const QString &line)
{
QString src = line;
QHash<QString, QVariant> output;
// Remove < and > at begin and end
src = src.remove(QRegularExpression("^##\\w+=<")).remove(QRegularExpression(">$"));
//capture key and value
QRegularExpression re("([A-Za-z_0-9]+)=\"?([^,=<>\"]+)\"?");
QRegularExpressionMatchIterator i = re.globalMatch(src);
while (i.hasNext())
{
QRegularExpressionMatch match = i.next();
if (match.capturedTexts().count() == 3)
{
QString key = match.captured(1);
QString val = match.captured(2);
output.insert(key, val);
}
}
return output;
}