-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathAnnotationInputFile.h
244 lines (224 loc) · 7.67 KB
/
AnnotationInputFile.h
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#ifndef _ANNOTATIONINPUTFILE_H_
#define _ANNOTATIONINPUTFILE_H_
typedef enum {VCF = 0 , PLAIN, PLINK, EPACTS} InputFileFormat;
// hold input file
// and return (chrom, pos, ref, alt) iteratively
class AnnotationInputFile{
public:
AnnotationInputFile(const char* inputFileName, const char* inputFormatStr) {
// check inputFormat
std::string inputFormat = toLower(inputFormatStr);
if (!inputFormat.empty() &&
inputFormat != "vcf" &&
inputFormat != "plain" &&
inputFormat != "plink" &&
inputFormat != "epacts") {
fprintf(stderr, "Unsupported input format [ %s ], we support VCF, plain, plink and EPACTS formats.\n", inputFormatStr);
LOG << "Unsupported input format [ " << inputFormatStr << " ], we support VCF, plain, plink and EPACTS formats.\n";
abort();
};
if (inputFormat == "vcf" || inputFormat.empty()) {
// ga.annotateVCF(FLAG_inputFile.c_str(), FLAG_outputFile.c_str());
this->format = VCF;
} else if (inputFormat == "plain") {
// ga.annotatePlain(FLAG_inputFile.c_str(), FLAG_outputFile.c_str());
this->format = PLAIN;
} else if (inputFormat == "plink") {
// ga.annotatePlink(FLAG_inputFile.c_str(), FLAG_outputFile.c_str());
this->format = PLINK;
} else if (inputFormat == "epacts") {
// ga.annotateEpacts(FLAG_inputFile.c_str(), FLAG_outputFile.c_str());
this->format = EPACTS;
} else{
fprintf(stderr, "Cannot recognize input file format: %s \n", inputFileName);
abort();
};
// open input files
this->lr = new LineReader(inputFileName);
// init values
checkReference = true;
failedReferenceSite = 0;
};
~AnnotationInputFile() {
this->close();
}
void close() {
if (this->lr) {
delete lr;
this->lr = NULL;
}
if (checkReference && failedReferenceSite > 0) {
fprintf(stderr, "ERROR: Total [ %d ] sites have unmatched Reference alleles\n", failedReferenceSite);
LOG << "ERROR: Total [ " << failedReferenceSite << " ] sites have unmatched Reference alleles\n";
}
};
int openReferenceGenome(const char* referenceGenomeFileName) {
return this->gs.open(referenceGenomeFileName);
}
// // extract one header line
// bool extractHeader(std::string* l) {
// this->line.clear();
// bool ret = this->lr->readByLine(this->line);
// *l = this->line;
// return ret;
// };
// // extract one header line
// bool extractHeader(std::string* l) {
// this->line.clear();
// bool ret = this->lr->readByLine(this->line);
// *l = this->line;
// return ret;
// };
// check ref and alt alleles (may switch ref and alt)
//@return true if (1) ref match reference; (2) after switch ref and alt, match reference genome.
bool forceReferenceStrand(const std::string& chrom,
const int& pos,
std::string* ref,
std::string* alt) {
// determine ref base from reference
bool refMatchRef = true;
for (size_t i = 0; i < ref->size(); i++ ) {
if ((*ref)[i] != gs[chrom][pos - 1 + i]) {
refMatchRef = false;
break;
}
}
if (!refMatchRef) {
bool altMatchRef = true;
for (size_t i = 0; i < alt->size(); i++ ) {
if ( (*alt)[i] != gs[chrom][pos - 1 + i]) {
altMatchRef = false;
break;
}
}
if (!altMatchRef) {
fprintf(stderr, "Ref [%s] and alt [%s] does not match reference: %s:%d\n", ref->c_str(), alt->c_str(),
chrom.c_str(), pos);
return false;
} else {
std::swap(*ref, *alt);
}
}
return true;
}
void setCheckReference(bool b) {
this->checkReference = b;
};
// if reach end or experience something wrong, will @return false
bool extract(std::string* chrom,
int* pos,
std::string* ref,
std::string* alt) {
bool ret;
do {
ret = this->lr->readLine(&this->line);
if (ret == false)
return ret;
} while (this->line.empty());
// for any line beginning with '#', store headers
// " CHR " is for PLINK header, "CHROM" is for other headers
while (line[0] == '#' || line.substr(0,5) == "CHROM" || line.substr(0, 5) == " CHR ") {
this->header.push_back(line);
do {
ret = this->lr->readLine(&this->line);
if (ret == false)
return ret;
} while (this->line.empty());
}
switch (this->format){
case VCF:
stringTokenize(line, "\t ", &fd);
if (fd.size() < 5) return false;
*chrom = fd[0];
*pos = toInt(fd[1]);
*ref = fd[3];
*alt = fd[4];
break;
case PLAIN:
stringNaturalTokenize(line, "\t ", &fd);
if (fd.size() < 4) return false;
*chrom = fd[0];
*pos = toInt(fd[1]);
*ref = fd[2];
*alt = fd[3];
break;
case PLINK:
stringNaturalTokenize(line, "\t ", &fd);
// will access fd[6], so at least 7 elements here
if (fd.size() <= 7) return false;
*chrom = fd[0];
*pos = toInt(fd[2]);
*ref = fd[3];
*alt = fd[6];
if (!forceReferenceStrand(*chrom, *pos, ref, alt))
return false;
break;
case EPACTS:
{
stringNaturalTokenize(line, "\t ", &fd);
// e.g.
// 20:139681_G/A 266 1 1 0.0018797 NA NA
// find
int beg = 0;
int sep = fd[0].find(':', beg);
*chrom = fd[0].substr(beg, sep - beg);
beg = sep + 1;
sep = fd[0].find('_', beg);
*pos = toInt(fd[0].substr(beg, sep - beg));
beg = sep + 1;
sep = fd[0].find('/', beg);
*ref = toUpper(fd[0].substr(beg, sep - beg));
beg = sep + 1;
sep = fd[0].find_first_of(" _\t", beg);
*alt = toUpper(fd[0].substr(beg, sep - beg));
epactsPrefixLength = sep;
if ( chrom->empty() || *pos <= 0 || ref->empty() || alt->empty()) {
fprintf(stderr, "Skip line: %s ...." , fd[0].c_str());
LOG << "Skip: " << fd[0];
return false;
}
}
break;
default:
fprintf(stderr, "Unknown format, quitting!\n");
abort();
break;
}// end switch
// verify reference
if (this->checkReference) {
// getBase() use 0-based index
std::string refFromGenome = this->gs.getBase(*chrom, *pos - 1, *pos + ref->size() - 1);
if ((*ref) != refFromGenome) {
++ failedReferenceSite;
if (failedReferenceSite <= 10) { // output at most 10 warnings
fprintf(stderr, "ERROR: Reference allele [ %s ] does not match reference genome [ %s ] at [ %s:%d ]\n", ref->c_str(), refFromGenome.c_str(), chrom->c_str(), *pos);
}
LOG << "ERRROR: Reference allele [" << (*ref) << "] does not match reference genome [" << refFromGenome << "] at " << *chrom << ":" << *pos << "\n";
};
}
return true;
};
InputFileFormat getFormat() const {
return this->format;
};
std::string getEpactsPrefix(const std::string& s) const{
return s.substr(0, this->epactsPrefixLength);
};
const std::vector< std::string>& getHeader() const {
return this->header;
};
const std::vector< std::string>& getFields() const {
return this->fd;
};
private:
bool checkReference;
int failedReferenceSite;
InputFileFormat format;
LineReader* lr;
std::vector< std::string> fd;
std::string line;
std::vector< std::string> header;
GenomeSequence gs; // check if ref alleles matched reference genome
size_t epactsPrefixLength;
}; // end class AnnotationInputFile
#endif /* _ANNOTATIONINPUTFILE_H_ */