-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJSONFileWriter.cpp
More file actions
153 lines (120 loc) · 4.44 KB
/
JSONFileWriter.cpp
File metadata and controls
153 lines (120 loc) · 4.44 KB
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
//
// Created by Daniel Olson on 3/11/22.
//
#include "JSONFileWriter.hpp"
#include "repeat.hpp"
#include "ultra.hpp"
void JSONFileWriter::OutputJSONKeyValue(std::string key, std::string value,
bool quotes) {
if (!quotes)
fprintf(out, ",\n\"%s\": %s", key.c_str(), value.c_str());
else
fprintf(out, ",\n\"%s\": \"%s\"", key.c_str(), value.c_str());
}
void JSONFileWriter::InitializeWriter(Ultra *ultra, FILE *out_f) {
owner = ultra;
out = out_f;
fprintf(out, "{\"Repeats\": [\n");
}
std::string JSONFileWriter::StringForSubRepeat(RepeatRegion *r, int split_index,
int start_pos) {
std::string repeatString = "{";
int start = start_pos;
int end = r->repeatLength;
int consensusPosition = r->splits->size();
if (split_index >= 0) {
end = r->splits->at(split_index);
consensusPosition = split_index;
}
float frac_rep = (float)r->repeatLength / (float)(end - start_pos);
float subScore = r->regionScore / frac_rep;
repeatString += "\"Start\": ";
repeatString += std::to_string(start);
repeatString += ",\n\"Consensus\": \"";
// Converting to a c string is important.
// ...Dunno why, but here we are
repeatString += r->consensi->at(consensusPosition).c_str();
repeatString += "\"}";
return repeatString;
}
std::string JSONFileWriter::SubRepeatsString(RepeatRegion *r) {
std::string subRepeats = "[";
int start = 0;
for (int s = 0; s < r->splits->size(); ++s) {
int split_i = r->splits->at(s);
if (split_i >= 0) {
if (start > 0)
subRepeats += ",\n";
subRepeats += StringForSubRepeat(r, s, start);
start = split_i;
}
}
if (start > 0)
subRepeats += ",\n";
subRepeats += StringForSubRepeat(r, -1, start);
subRepeats.push_back(']');
return subRepeats;
}
void JSONFileWriter::WriteRepeat(RepeatRegion *repeat) {
if (this->repeatsOutput > 0) {
fprintf(out, ",\n\n");
}
++this->repeatsOutput;
fprintf(out, "{\"SequenceName\": \"%s\"", repeat->sequenceName.c_str());
this->OutputJSONKeyValue("Start", std::to_string(repeat->sequenceStart));
this->OutputJSONKeyValue("Length", std::to_string(repeat->repeatLength));
this->OutputJSONKeyValue("Period", std::to_string(repeat->repeatPeriod));
this->OutputJSONKeyValue("Score", std::to_string(repeat->regionScore));
if (owner->settings->pval) {
double pval = owner->PvalForScore(repeat->regionScore);
this->OutputJSONKeyValue("PVal", std::to_string(pval));
}
if (owner->settings->show_counts) {
auto copies =
(repeat->repeatLength - repeat->insertions + repeat->deletions) /
repeat->repeatPeriod;
this->OutputJSONKeyValue("Copies", std::to_string(copies));
this->OutputJSONKeyValue("Substitutions",
std::to_string(repeat->mismatches));
this->OutputJSONKeyValue("Insertions", std::to_string(repeat->insertions));
this->OutputJSONKeyValue("Deletions", std::to_string(repeat->deletions));
}
this->OutputJSONKeyValue("Consensus", repeat->string_consensus, true);
if (owner->settings->show_seq) {
this->OutputJSONKeyValue("Sequence", repeat->sequence, true);
}
if (owner->settings->show_trace) {
this->OutputJSONKeyValue("Traceback", repeat->traceback, true);
}
if (owner->settings->show_logo_nums && repeat->logoNumbers != nullptr) {
std::string logoNumbers = "\"" + std::to_string(repeat->logoNumbers[0]);
for (int i = 0; i < repeat->repeatLength; ++i) {
logoNumbers.append("," + std::to_string(repeat->logoNumbers[i]));
}
logoNumbers += "\"";
this->OutputJSONKeyValue("LogoNumbers", logoNumbers);
}
if (owner->settings->show_deltas && repeat->scores != nullptr) {
std::string positionScoreDeltas = "[";
for (int i = 0; i < repeat->repeatLength; ++i) {
if (i > 0)
positionScoreDeltas += ",";
positionScoreDeltas += std::to_string(repeat->scores[i]);
}
positionScoreDeltas.push_back(']');
this->OutputJSONKeyValue("PositionScoreDeltas", positionScoreDeltas);
}
int numberOfValidSplits = 0;
if (repeat->splits != nullptr) {
for (int i = 0; i < repeat->splits->size(); ++i) {
if (repeat->splits->at(i) > 0)
++numberOfValidSplits;
}
}
if (numberOfValidSplits > 0) {
std::string subRepeats = SubRepeatsString(repeat);
this->OutputJSONKeyValue("SubRepeats", subRepeats);
}
fprintf(out, "}");
}
void JSONFileWriter::EndWriter() { fprintf(out, "]\n}"); }