forked from HIITMetagenomics/dsm-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutputWriter.h
230 lines (203 loc) · 6.65 KB
/
OutputWriter.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
/**
* Handle output (file or stdin).
*
* Currently supported output formats:
* tab delimited (see README)
* SAM
*
* TODO
* [ ] CIGAR
* [ ] quality scores
*/
#ifndef _OutputWriter_H_
#define _OutputWriter_H_
#include "Pattern.h"
#include "TextCollection.h"
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <cstdlib> // exit()
#include <cassert>
/**
* Base class to write output
*/
class OutputWriter
{
public:
enum output_format_t { output_tabs, output_sam };
static OutputWriter* build(output_format_t output, std::string file = "");
virtual void reportIndels(Pattern const &p,
TextCollection::position_result const &pos,
std::string const &refName,
std::string const &text,
unsigned occs) = 0;
virtual void reportMismatches(Pattern const &p,
TextCollection::position_result const &pos,
std::string const &refName,
std::string const &text,
unsigned occs) = 0;
virtual ~OutputWriter()
{
if (fp != &std::cout)
delete fp;
fp = 0;
}
protected:
OutputWriter(std::string const &file)
: fp(0)
{
if (file == "")
fp = &std::cout;
else
fp = new std::ofstream(file.c_str());
if (!fp->good())
{
std::cerr << "unable to open output file " << file << std::endl;
std::exit(1); // or throw?
}
}
std::ostream *fp;
private:
OutputWriter();
// No copy constructor or assignment
OutputWriter(OutputWriter const&);
OutputWriter& operator = (OutputWriter const&);
};
/**
* Format: tab delimited values, see README.
*
* Inherit constructor from OutputWriter
*/
class TabDelimitedOutputWriter : public OutputWriter
{
public:
TabDelimitedOutputWriter(std::string const &file)
: OutputWriter(file)
{ }
virtual void reportIndels(Pattern const &p,
TextCollection::position_result const &pos,
std::string const &refName,
std::string const &text,
unsigned occs)
{
std::string edits = getEdits(p.getPattern(), text);
report(p, pos, refName, text, occs, edits);
}
virtual void reportMismatches(Pattern const &p,
TextCollection::position_result const &pos,
std::string const &refName,
std::string const &text,
unsigned occs)
{
std::string edits = getMismatches(p.getPattern(), text);
report(p, pos, refName, text, occs, edits);
}
private:
inline void report(Pattern const &p,
TextCollection::position_result const &pos,
std::string const &refName,
std::string const &text,
unsigned occs,
std::string const &edits)
{
#ifdef PARALLEL_SUPPORT
#pragma omp critical (OUTPUTWRITER_REPORT)
#endif
{
if (!fp->good())
{
std::cerr << "error: unable to write output!" << std::endl;
std::abort();
}
*fp << p.getName() << "\t"
<< refName << "\t"
<< pos.second+1 << "\t"
<< pos.second+text.size() << "\t"
<< occs << "\t"
<< (p.isReverseComplement() ? 'R' : 'F') << "\t"
<< edits << std::endl;
}
}
std::string getEdits(std::string const &, std::string const &);
std::string getMismatches(std::string const &, std::string const &);
};
/**
* Format: SAM
*
* Inherit constructor from OutputWriter
*/
class SamOutputWriter : public OutputWriter
{
public:
SamOutputWriter(std::string const &file)
: OutputWriter(file)
{ }
virtual void reportIndels(Pattern const &p,
TextCollection::position_result const &pos,
std::string const &refName,
std::string const &text,
unsigned occs)
{
std::string cigar = getCigar(p.getPattern(), text);
report(p, pos, refName, text, occs, cigar);
}
virtual void reportMismatches(Pattern const &p,
TextCollection::position_result const &pos,
std::string const &refName,
std::string const &text,
unsigned occs)
{
std::stringstream cigar;
cigar << p.size();
cigar << "M";
report(p, pos, refName, text, occs, cigar.str());
}
private:
inline void report(Pattern const &p,
TextCollection::position_result const &pos,
std::string const &refName,
std::string const &text,
unsigned occs,
std::string const &cigar)
{
#ifdef PARALLEL_SUPPORT
#pragma omp critical (OUTPUTWRITER_REPORT)
#endif
{
if (!fp->good())
{
std::cerr << "error: unable to write output!" << std::endl;
std::abort();
}
*fp << p.getName() << '\t'; // QNAME
unsigned flag = 0; // FLAG
if (p.isReverseComplement())
flag |= 0x0010; // strand of the query (0 forward, 1 reverse)
*fp << flag << '\t'
<< refName << '\t' // RNAME
<< pos.second+1 << '\t' // POS
<< 255 << '\t' // MAPQ (MAPping Quality)
<< cigar << '\t'
<< "*\t" // MRNM (Mate Reference sequence NaMe)
<< "0\t" // MPOS (Mate POSition)
<< "0\t"; // ISIZE (inferred Insert SIZE)
if (Pattern::getColor()) // SEQ (query SEQuence)
*fp << "*\t";
else
*fp << p.getPattern() << '\t';
/*if (p.getQuality().empty()) // QUAL (query QUALity)
*fp << "*\t";
else
{ FIXME Quality should be in PHRED33 format
assert(p.getQuality().size() == p.getPattern().size());
*fp << p.getQuality() << '\t';
}*/
*fp << "*\t";
// Optional fields
*fp << std::endl;
}
}
std::string getCigar(std::string const &, std::string const &);
};
#endif // _OutputWriter_H_