-
Notifications
You must be signed in to change notification settings - Fork 17
/
AnnotationOutputFile.h
204 lines (190 loc) · 5.81 KB
/
AnnotationOutputFile.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
#ifndef _ANNOTATIONOUTPUTFILE_H_
#define _ANNOTATIONOUTPUTFILE_H_
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "IO.h"
#include "AnnotationInputFile.h"
bool fileExists(std::string& s) {
struct stat res;
int ret = stat(s.c_str(), &res);
return (ret == 0);
};
time_t getFileMtime(std::string& s) {
struct stat res;
int ret = stat(s.c_str(), &res);
if (ret != 0) {
// perror("stat");
return -1;
}
return res.st_mtime;
};
class AnnotationOutputFile{
public:
AnnotationOutputFile(const std::string& out):headerOutputted(false), totalVariants(0),outputFileName(out) {
/* if (strncmp (out, "stdout", 6) ) { */
/* this->fout = fopen(out, "wt"); */
/* if (!this->fout) { */
/* fprintf(stderr, "Cannot open otuput file %s for write.\n", out); */
/* } */
/* } else { */
/* this->fout = stdout; */
/* } */
if (hasSuffix(out, ".gz")) {
this->fout = new FileWriter(out.c_str(), IO_BGZIP);
} else {
this->fout = new FileWriter(out.c_str());
}
};
~AnnotationOutputFile() {
this->close();
}
void close() {
if (this->fout) {
fprintf(stderr, "DONE: %d varaints are annotated.\n", totalVariants);
fprintf(stderr, "DONE: Generated annotation output in [ %s ].\n", outputFileName.c_str());
delete this->fout;
this->fout = NULL;
}
};
void linkToInput(const AnnotationInputFile& in) {
this->aif = ∈
};
void writeHeader() {
writeHeader(this->aif->getHeader());
};
void writeHeader(const std::vector< std::string> & h) {
for (size_t i = 0; i < h.size(); ++i) {
if (i != h.size() - 1) {
this->fout->write(h[i].c_str());
this->fout->write("\n");
} else { // last line
if ( (aif->getFormat() == PLAIN || aif->getFormat() == PLINK) &&
(h[i].substr(0, 5) == "CHROM" || h[i].substr(0, 6) == "#CHROM") ) {
this->fout->write(h[i].c_str());
this->fout->write("\tANNO\tANNO_FULL\n");
} else {
this->fout->write(h[i].c_str());
this->fout->write("\n");
}
}
}
}
void writeResult(const OrderedMap<std::string, std::string>& res) {
//fprintf(stderr, "Need to link output to input!\n");
assert(aif);
if (!this->headerOutputted) {
this->writeHeader();
this->headerOutputted = true;
}
const std::vector< std::string> & field = aif->getFields();
std::string key;
std::string val;
switch (aif->getFormat()) {
case VCF:
{
for (size_t i = 0; i < field.size(); ++i) {
if (i) this->fout->write("\t");
this->fout->write(field[i].c_str()) ;
if (i == 7) { // 7: the 8th column in 0-based index
if (!field[i].empty())
this->fout->write(VCF_INFO_SEPARATOR);
for (size_t j = 0; j < res.size(); ++j) {
if (j)
this->fout->write(VCF_INFO_SEPARATOR);
res.at(j, &key, &val);
this->fout->write(key.c_str());
this->fout->write("=");
this->fout->write(val.c_str());
}
}
};
}
break;
case PLAIN:
case PLINK:
{
for (size_t i = 0; i < field.size(); ++i) {
if (i) this->fout->write("\t");
this->fout->write(field[i].c_str());
};
for (size_t j = 0; j < res.size(); ++j) {
this->fout->write("\t");
res.at(j, &key, &val);
// this->fout->write(key.c_str());
// this->fout->write("=");
this->fout->write(val.c_str());
}
}
break;
case EPACTS:
{
//this->fout->write(field[0].substr(0, sep).c_str());
this->fout->write(aif->getEpactsPrefix(field[0]).c_str());
this->fout->write("_");
std::string s;
res.value("ANNO", &s);
this->fout->write(s.c_str());
for (unsigned int i = 1; i < field.size(); i++ ){
this->fout->write("\t");
this->fout->write(field[i].c_str()) ;
}
}
break;
default:
break;
};
this->fout->write("\n");
++this->totalVariants;
};
int indexOutput() {
// back up existing/older index file
size_t len = outputFileName.size();
std::string indexFileName = outputFileName.substr(0, len - 3); // strip ".gz"
if (fileExists(indexFileName) &&
getFileMtime(indexFileName) <= getFileMtime(outputFileName)) { // index too old
remove(indexFileName.c_str());
fprintf(stderr, "Done: Removed old index file ...\n");
}
if ( bgzf_is_bgzf(outputFileName.c_str())!=1 )
{
fprintf(stderr,"[tabix] was bgzip used to compress this file? %s\n", outputFileName.c_str());
return 1;
}
// set up format
ti_conf_t conf = ti_conf_vcf;
switch (aif->getFormat()) {
case VCF:
conf = ti_conf_vcf;
break;
case PLAIN:
case PLINK:
conf.sc = 1;
conf.bc = 2;
conf.ec = 2;
conf.meta_char = '#';
conf.line_skip = 0;
for (size_t i = 0; i < aif->getHeader().size(); ++i) {
if (aif->getHeader()[i][0] != '#')
conf.line_skip ++;
}
break;
case EPACTS:
fprintf(stderr, "EPACTS outputs are not tab-delimited and not indextable for now.\n");
return -1;
}
// index
return ti_index_build(outputFileName.c_str(), &conf);
};
private:
// don't copy
AnnotationOutputFile(const AnnotationOutputFile& fw);
AnnotationOutputFile& operator=(const AnnotationOutputFile& fw);
private:
bool headerOutputted;
const AnnotationInputFile* aif;
FileWriter* fout;
int totalVariants;
std::string outputFileName;
}; // class AnnotationOutputFile
#endif /* _ANNOTATIONOUTPUTFILE_H_ */