IntaRNA 3.5.0-pre
RNA-RNA interaction prediction | C++ API
Loading...
Searching...
No Matches
OutputHandlerCsv.h
Go to the documentation of this file.
1
2#ifndef INTARNA_OUTPUTHANDLERCSV_H_
3#define INTARNA_OUTPUTHANDLERCSV_H_
4
5#include "IntaRNA/general.h"
6
10
11#include <iterator>
12#include <list>
13#include <map>
14#include <numeric>
15#include <utility>
16
17#include <boost/algorithm/string.hpp>
18
19namespace IntaRNA {
20
25{
26
27public:
28
30 static const std::string notAvailable;
31
32
91
93 typedef std::list<ColType> ColTypeList;
94
97
101 static
102 const std::map<ColType,std::string> &
104
105protected:
106
108 static std::map<ColType,std::string> colType2string;
109
113 static
114 void
116 if (colType2string.empty()) {
117 // fill container : ensure strings are non-redundant!
118 colType2string[id1] = "id1";
119 colType2string[id2] = "id2";
120 colType2string[seq1] = "seq1";
121 colType2string[seq2] = "seq2";
122 colType2string[start1] = "start1";
123 colType2string[end1] = "end1";
124 colType2string[start2] = "start2";
125 colType2string[end2] = "end2";
126 colType2string[subseq1] = "subseq1";
127 colType2string[subseq2] = "subseq2";
128 colType2string[subseqDP] = "subseqDP";
129 colType2string[subseqDB] = "subseqDB";
130 colType2string[hybridDP] = "hybridDP";
131 colType2string[hybridDB] = "hybridDB";
132 colType2string[hybridDPfull] = "hybridDPfull";
133 colType2string[hybridDBfull] = "hybridDBfull";
134 colType2string[bpList] = "bpList";
135 colType2string[E] = "E";
136 colType2string[Eens] = "Eens";
137 colType2string[Etotal] = "Etotal";
138 colType2string[ED1] = "ED1";
139 colType2string[ED2] = "ED2";
140 colType2string[Pu1] = "Pu1";
141 colType2string[Pu2] = "Pu2";
142 colType2string[E_init] = "E_init";
143 colType2string[E_loops] = "E_loops";
144 colType2string[E_dangleL] = "E_dangleL";
145 colType2string[E_dangleR] = "E_dangleR";
146 colType2string[E_endL] = "E_endL";
147 colType2string[E_endR] = "E_endR";
148 colType2string[E_hybrid] = "E_hybrid";
149 colType2string[E_norm] = "E_norm";
150 colType2string[E_hybridNorm] = "E_hybridNorm";
151 colType2string[E_add] = "E_add";
152 colType2string[w] = "w";
153 colType2string[seedStart1] = "seedStart1";
154 colType2string[seedEnd1] = "seedEnd1";
155 colType2string[seedStart2] = "seedStart2";
156 colType2string[seedEnd2] = "seedEnd2";
157 colType2string[seedE] = "seedE";
158 colType2string[seedED1] = "seedED1";
159 colType2string[seedED2] = "seedED2";
160 colType2string[seedPu1] = "seedPu1";
161 colType2string[seedPu2] = "seedPu2";
162 colType2string[Eall] = "Eall";
163 colType2string[Eall1] = "Eall1";
164 colType2string[Eall2] = "Eall2";
165 colType2string[EallTotal] = "EallTotal";
166 colType2string[Zall] = "Zall";
167 colType2string[Zall1] = "Zall1";
168 colType2string[Zall2] = "Zall2";
169 colType2string[P_E] = "P_E";
170 colType2string[RT] = "RT";
171 // ensure filling is complete
172 for (size_t i=0; i<ColTypeNumber; i++) {
173 if ( colType2string.find( static_cast<ColType>(i) ) == colType2string.end() ) {
174 throw std::runtime_error("OutputHandlerCsv::initColType2string() : ColType "+toString(i)+" without string representative");
175 }
176 }
177 }
178 }
179
180public:
181
196 , std::ostream & out
197 , const InteractionEnergy & energy
198 , const ColTypeList columns
199 , const std::string& colSep = ";"
200 , const bool printHeader = false
201 , const std::string& listSep = ":"
202 );
203
207 virtual ~OutputHandlerCsv() override;
208
215 virtual
216 void
217 add( const Interaction & interaction ) override;
218
225 static
226 std::string
227 list2string( const ColTypeList & colTypes, const std::string & sep );
228
241 static
243 string2list( const std::string & listString );
244
251 static
252 bool
253 needsZall( const ColTypeList & colTypes );
254
261 static
262 bool
263 needBPs( const ColTypeList & colTypes );
264
271 static
272 std::string
273 getHeader( const ColTypeList & colTypes, const std::string& colSep );
274
275
276protected:
277
279 using OutputHandler::Z;
280
282 std::ostream & out;
283
286
288 const std::list< ColType > columns;
289
291 std::string colSep;
292
294 std::string listSep;
295
296
297
298};
299
300
302
303inline
304const std::map<OutputHandlerCsv::ColType,std::string> &
310
312
313inline
314std::string
316list2string( const ColTypeList & colTypes, const std::string & sep )
317{
318 // init string encodings
320
321 // lamda function to add next column to list
322 auto addCol = [&](std::string a, ColType b) {
323 return std::move(a) + sep + colType2string[b];
324 };
325
326 // generate full list
327 return std::accumulate(std::next(colTypes.begin())
328 , colTypes.end()
329 , colType2string[*(colTypes.begin())] // start with first element
330 , addCol);
331}
332
334
335inline
339string2list( const std::string & stringEncoding )
340{
341 // init string encodings
343
344 ColTypeList list;
345 // empty string : give full list
346 if (stringEncoding.empty() || stringEncoding == "*") {
347 // generate list of all types
348 for (size_t c = 0; c < (size_t)ColTypeNumber; c++) {
349 list.push_back( static_cast<ColType>(c) );
350 }
351 }
352 else {
353 // find split position
354 size_t startPos = 0, splitPos = std::string::npos;
355 while (startPos != splitPos) {
356 splitPos = stringEncoding.find(',',startPos);
357 // get current type string
358 std::string curTypeString = stringEncoding.substr(startPos,splitPos-(splitPos==std::string::npos?0:startPos));
359 // trim leading/trailing whitespaces
360 boost::trim(curTypeString);
361 // try to find type encoding
362 bool notFound = true;
363 for (auto it = colType2string.begin(); notFound && it != colType2string.end(); it++ ) {
364 // check if type found (case insensitive for being user friendly
365 if ( boost::iequals( it->second, curTypeString) ) {
366 list.push_back( it->first );
367 notFound = false;
368 }
369 }
370 // check if error to be raised
371 if (notFound) {
372 throw std::runtime_error("OutputHandlerCsv::string2list("+stringEncoding+") contains unsupported ColType encoding '"+curTypeString+"'");
373 }
374 // update start of next interval encoding to parse
375 startPos = splitPos + (splitPos != std::string::npos ? 1 : 0);
376 }
377 }
378 return list;
379}
380
382
383inline
384bool
386needsZall( const ColTypeList & colTypes )
387{
388 for ( const auto& type : colTypes ) {
389 // check if type requires Zall computation
390 switch ( type ) {
391 case Eall:
392 case EallTotal:
393 case Zall:
394 case P_E:
395 return true;
396 default: // ignore all other types
397 break;
398 }
399 }
400 return false;
401}
402
404
405inline
406bool
408needBPs( const ColTypeList & colTypes )
409{
410 for ( const auto& type : colTypes ) {
411 // check if type requires Zall computation
412 switch ( type ) {
413 case hybridDB:
414 case hybridDBfull:
415 case hybridDP:
416 case hybridDPfull:
417 case bpList:
418 return true;
419 default: // ignore all other types
420 break;
421 }
422 }
423 return false;
424}
425
427
428inline
429std::string
431getHeader( const OutputHandlerCsv::ColTypeList & colList, const std::string& colSep )
432{
433 // return string
434 return list2string( colList, colSep ) + "\n";
435}
436
438
439} // namespace
440
441#endif /* OUTPUTHANDLERCSV_H_ */
Definition InteractionEnergy.h:20
Definition Interaction.h:28
Definition OutputConstraint.h:19
Definition OutputHandlerCsv.h:25
virtual ~OutputHandlerCsv() override
static ColTypeList string2list(const std::string &listString)
Definition OutputHandlerCsv.h:339
static const ColTypeList colTypeNumericSort
list of ColTypes that have to be sorted numerically
Definition OutputHandlerCsv.h:96
std::ostream & out
the output stream to write to
Definition OutputHandlerCsv.h:282
static std::string getHeader(const ColTypeList &colTypes, const std::string &colSep)
Definition OutputHandlerCsv.h:431
std::list< ColType > ColTypeList
list of ColTypes
Definition OutputHandlerCsv.h:93
const InteractionEnergy & energy
the interaction energy function used for interaction computation
Definition OutputHandlerCsv.h:285
static const std::string notAvailable
string to encode not available values
Definition OutputHandlerCsv.h:30
static bool needsZall(const ColTypeList &colTypes)
Definition OutputHandlerCsv.h:386
ColType
the column types supported
Definition OutputHandlerCsv.h:34
@ Etotal
overall energy of the interaction including the intra-molecular ensemble energies (E+Eall1+Eall2)
Definition OutputHandlerCsv.h:54
@ seq2
full second sequence
Definition OutputHandlerCsv.h:38
@ P_E
probability of mfe within interaction ensemble (outConstraint.needZall)
Definition OutputHandlerCsv.h:87
@ start1
start index of hybrid in seq1
Definition OutputHandlerCsv.h:43
@ hybridDPfull
hybrid in VRNA dot-bracket notation for full sequence lengths
Definition OutputHandlerCsv.h:49
@ end1
end index of hybrid in seq1
Definition OutputHandlerCsv.h:44
@ E_hybridNorm
length normalized energy of hybridization only = E_hybrid / ln(length(seq1)*length(seq2))
Definition OutputHandlerCsv.h:67
@ E_init
initiation energy
Definition OutputHandlerCsv.h:59
@ E_loops
sum of loop energies (excluding E_init)
Definition OutputHandlerCsv.h:60
@ seedEnd1
end index of the seed in seq1
Definition OutputHandlerCsv.h:70
@ hybridDBfull
hybrid in dot-bar notation for full sequence lengths
Definition OutputHandlerCsv.h:50
@ seedEnd2
end index of the seed in seq2
Definition OutputHandlerCsv.h:72
@ Eall
ensemble energy of all interactions (outConstraint.needZall)
Definition OutputHandlerCsv.h:80
@ start2
start index of hybrid in seq2
Definition OutputHandlerCsv.h:45
@ Eall2
ensemble energy of all intra-molecular structures of seq2
Definition OutputHandlerCsv.h:82
@ seedE
overall energy of the seed only (including accessibility etc)
Definition OutputHandlerCsv.h:73
@ seedED2
ED value of seq2 of the seed only (excluding rest)
Definition OutputHandlerCsv.h:75
@ w
Boltzmann weight of the interaction energy.
Definition OutputHandlerCsv.h:78
@ seq1
full first sequence
Definition OutputHandlerCsv.h:37
@ ED1
ED value of seq1.
Definition OutputHandlerCsv.h:55
@ hybridDB
hybrid in dot-bar notation
Definition OutputHandlerCsv.h:48
@ seedStart2
start index of the seed in seq2
Definition OutputHandlerCsv.h:71
@ subseq2
interacting subsequence of second sequence
Definition OutputHandlerCsv.h:40
@ hybridDP
hybrid in VRNA dot-bracket notation
Definition OutputHandlerCsv.h:47
@ ColTypeNumber
number of column types
Definition OutputHandlerCsv.h:89
@ E_dangleL
dangling end contribution of base pair (start1,end2)
Definition OutputHandlerCsv.h:61
@ seedStart1
start index of the seed in seq1
Definition OutputHandlerCsv.h:69
@ Pu2
probability to be accessible for seq2
Definition OutputHandlerCsv.h:58
@ bpList
list of hybrid base pairs like (1,3),(4,2),...
Definition OutputHandlerCsv.h:51
@ Zall1
partition function of all intra-molecular structures of seq1
Definition OutputHandlerCsv.h:84
@ seedED1
ED value of seq1 of the seed only (excluding rest)
Definition OutputHandlerCsv.h:74
@ EallTotal
total ensemble energy (Eall+Eall1+Eall2) of all interactions including the intra-molecular ensemble e...
Definition OutputHandlerCsv.h:86
@ id1
id of first sequence
Definition OutputHandlerCsv.h:35
@ Eall1
ensemble energy of all intra-molecular structures of seq1
Definition OutputHandlerCsv.h:81
@ seedPu1
probability of seed region to be accessible for seq1
Definition OutputHandlerCsv.h:76
@ Eens
ensemble energy used to select the interaction site
Definition OutputHandlerCsv.h:53
@ E_hybrid
energy of hybridization only = E - ED1 - ED2
Definition OutputHandlerCsv.h:65
@ seedPu2
probability of seed region to be accessible for seq2
Definition OutputHandlerCsv.h:77
@ E_endR
penalty of closing base pair (end1,start2)
Definition OutputHandlerCsv.h:64
@ Zall
partition function of all interactions (outConstraint.needZall)
Definition OutputHandlerCsv.h:83
@ subseq1
interacting subsequence of first sequence
Definition OutputHandlerCsv.h:39
@ Zall2
partition function of all intra-molecular structures of seq2
Definition OutputHandlerCsv.h:85
@ E_add
user provided energy shift
Definition OutputHandlerCsv.h:68
@ ED2
ED value of seq2.
Definition OutputHandlerCsv.h:56
@ Pu1
probability to be accessible for seq1
Definition OutputHandlerCsv.h:57
@ subseqDP
hybrid subsequences compatible with hybridDP
Definition OutputHandlerCsv.h:41
@ E
overall MFE of the reported interaction structure
Definition OutputHandlerCsv.h:52
@ RT
the scaled temperature used for Boltzmann weight computation
Definition OutputHandlerCsv.h:88
@ E_norm
length normalized energy = E/ln(length(seq1)*length(seq2))
Definition OutputHandlerCsv.h:66
@ id2
id of second sequence
Definition OutputHandlerCsv.h:36
@ end2
end index of hybrid in seq2
Definition OutputHandlerCsv.h:46
@ E_dangleR
dangling end contribution of base pair (end1,start2)
Definition OutputHandlerCsv.h:62
@ subseqDB
hybrid subsequences compatible with hybridDB
Definition OutputHandlerCsv.h:42
@ E_endL
penalty of closing base pair (start1,end2)
Definition OutputHandlerCsv.h:63
std::string colSep
the column separator to be used
Definition OutputHandlerCsv.h:291
virtual void add(const Interaction &interaction) override
std::string listSep
the list separator to be used within single columns
Definition OutputHandlerCsv.h:294
static void initColType2string()
Definition OutputHandlerCsv.h:115
static std::map< ColType, std::string > colType2string
mapping of ColTypes to according strings
Definition OutputHandlerCsv.h:108
static const std::map< ColType, std::string > & getColType2string()
Definition OutputHandlerCsv.h:306
static std::string list2string(const ColTypeList &colTypes, const std::string &sep)
Definition OutputHandlerCsv.h:316
OutputHandlerCsv(const OutputConstraint &outConstraint, std::ostream &out, const InteractionEnergy &energy, const ColTypeList columns, const std::string &colSep=";", const bool printHeader=false, const std::string &listSep=":")
const std::list< ColType > columns
the sequence of columns to be reported
Definition OutputHandlerCsv.h:288
static bool needBPs(const ColTypeList &colTypes)
Definition OutputHandlerCsv.h:408
Definition OutputHandler.h:22
const OutputConstraint outConstraint
the output constraints to be applied
Definition OutputHandler.h:116
Z_type Z
overall partition function for the sequences provided in Z_energy
Definition OutputHandler.h:122
#define toString(x)
Definition general.h:60
Definition Accessibility.h:13