-
Notifications
You must be signed in to change notification settings - Fork 2
/
output.h
114 lines (94 loc) · 3.16 KB
/
output.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
#ifndef __OUTPUT
#define __OUTPUT
#include <ostream>
#include <string>
#include <iomanip>
#include "bloom.h"
struct MatchResult
{
unsigned int num_kmers_found;
unsigned int num_query_kmer;
FilterInfo subject_info;
MatchResult()
{
};
MatchResult(const unsigned int &m_num_kmers_found,
const unsigned int &m_num_query_kmer, const FilterInfo &m_subject_info) :
num_kmers_found(m_num_kmers_found),
num_query_kmer(m_num_query_kmer),
subject_info(m_subject_info)
{
};
// The output is sorted in *descending order* by num_kmers_found
inline bool operator<(const MatchResult &m_rhs) const
{
// Sort in *descending* order by the number of kmer matches
return (num_kmers_found > m_rhs.num_kmers_found);
};
};
void write_csv_header(std::ostream &m_out)
{
m_out << "query,num_kmers,num_kmers_found,percent_kmers_found,sample_metadata\n";
}
template<typename _ITERATOR>
void write_csv(std::ostream &m_out, const std::string &m_query,
_ITERATOR m_begin, _ITERATOR m_end)
{
for(_ITERATOR i = m_begin;i != m_end;++i){
const float norm = i->num_query_kmer ? 1.0f/i->num_query_kmer : 0.0f;
m_out << '"' << m_query << "\","
<< i->num_query_kmer << ','
<< i->num_kmers_found << ','
<< (100.0f*i->num_kmers_found)*norm << ",\""
<< i->subject_info.csv_string() << '"' << std::endl;
}
}
void write_csv_footer(std::ostream &m_out)
{
// Nothing to do!
}
void write_json_header(std::ostream &m_out, bool m_multiple_query_matches)
{
if(m_multiple_query_matches){
m_out << '[';
}
}
template<typename _ITERATOR>
void write_json(std::ostream &m_out, const std::string &m_query,
bool m_multiple_query_matches, bool m_first_match,
const float &m_threshold, _ITERATOR m_begin, _ITERATOR m_end)
{
const std::string prefix = m_multiple_query_matches ? "\t" : "";
// Format floating point values to have one significant figure after the decimal point
// and to not use scientific notation (i.e. use fixed notation instead). This is to match
// the output format of the original bigsi python implementation.
m_out << ( (m_multiple_query_matches && !m_first_match) ? "," : "") << '\n' << prefix
<< "{\n" << prefix << "\t\"query\": \"" << m_query << "\",\n" << prefix
<< "\t\"threshold\": "
<< std::showpoint << std::setprecision(1) << std::fixed << m_threshold
<< ",\n" << prefix << "\t\"results\": [";
for(_ITERATOR i = m_begin;i != m_end;++i){
const float norm = i->num_query_kmer ? 1.0f/i->num_query_kmer : 0.0f;
m_out << ( (i != m_begin) ? "," : "")
<< "\n" << prefix << "\t\t{\n" << prefix
<< "\t\t\t\"percent_kmers_found\": "
<< (100.0*i->num_kmers_found)*norm
<< ",\n" << prefix << "\t\t\t\"num_kmers\": " << i->num_query_kmer
<< ",\n" << prefix << "\t\t\t\"num_kmers_found\": " << i->num_kmers_found
<< ",\n" << prefix << "\t\t\t\"sample_metadata\": {\n"
<< i->subject_info.json_string(prefix + "\t\t\t\t")
<< "\n" << prefix
<< "\t\t\t}\n" << prefix << "\t\t}";
}
if(m_begin != m_end){
m_out << "\n" << prefix << '\t';
}
m_out << "]\n" << prefix << "}";
}
void write_json_footer(std::ostream &m_out, bool m_multiple_query_matches)
{
if(m_multiple_query_matches){
m_out << "\n]\n";
}
}
#endif // __OUTPUT