-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsg2vgjson.cpp
275 lines (239 loc) · 8 KB
/
sg2vgjson.cpp
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* Copyright (C) 2015 by Glenn Hickey ([email protected])
*
* Released under the MIT license, see LICENSE.cactus
*/
#include <iostream>
#include <sstream>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "sg2vgjson.h"
using namespace std;
using namespace rapidjson;
SG2VGJSON::SG2VGJSON() : _os(0), _sg(0), _bases(0), _paths(0), _doc(0)
{
}
SG2VGJSON::~SG2VGJSON()
{
delete _doc;
}
void SG2VGJSON::init(ostream* os)
{
_os = os;
delete _doc;
_doc = new Document();
_doc->Parse("{\"node\": [], \"edge\": [], \"path\": []}");
assert(_doc->IsObject());
assert(nodes().IsArray());
assert(edges().IsArray());
assert(paths().IsArray());
}
void SG2VGJSON::writeGraph(const SideGraph* sg,
const vector<string>& bases,
const vector<SGNamedPath>& paths)
{
_sg = sg;
_bases = &bases;
_paths = &paths;
*_os << "{";
// add every node to json doc
for (int i = 0; i < _sg->getNumSequences(); ++i)
{
addNode(_sg->getSequence(i));
}
// add every edge to json doc
const SideGraph::JoinSet* joinSet = _sg->getJoinSet();
for (SideGraph::JoinSet::const_iterator i = joinSet->begin();
i != joinSet->end(); ++i)
{
addEdge(*i);
}
// add every path to json doc
for (int i = 0; i < paths.size(); ++i)
{
addPath(paths[i].first, paths[i].second);
}
// after this we'll have added 2 copies of the graph to memory
// 1) json doc 2) this buffer
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
_doc->Accept(writer);
// make that a 3rd time -- strip outer nesting {}'s.
string temp(buffer.GetString());
*_os << temp.substr(1, temp.length()-1);
}
void SG2VGJSON::writeChunkedGraph(const SideGraph* sg,
const std::vector<std::string>& bases,
const std::vector<SGNamedPath>& paths,
int sequencesPerChunk,
int joinsPerChunk,
int pathSegsPerChunk)
{
// current offset wrt to what's been written so far
int sequencesAdded = 0;
SideGraph::JoinSet::const_iterator joinsAdded = sg->getJoinSet()->begin();
int pathsAdded = 0;
int segmentsAdded = 0; // note this is relative to current last path.
for(bool wroteSomething = true; wroteSomething == true;)
{
// current count in chunk
int sequencesInChunk = 0;
int joinsInChunk = 0;
int segmentsInChunk = 0;
init(_os);
_sg = sg;
_bases = &bases;
_paths = &paths;
*_os << "{";
// add every node to json doc
for (; sequencesInChunk < sequencesPerChunk &&
sequencesAdded < _sg->getNumSequences();
++sequencesInChunk, ++sequencesAdded)
{
addNode(_sg->getSequence(sequencesAdded));
}
double progress = (double)sequencesInChunk / sequencesPerChunk;
// add every edge to json doc
int joinsToAdd = joinsPerChunk * (1. - progress);
const SideGraph::JoinSet* joinSet = _sg->getJoinSet();
for (; joinsAdded != joinSet->end() && joinsInChunk < joinsToAdd;
++joinsInChunk, ++joinsAdded)
{
addEdge(*joinsAdded);
}
progress += (double)joinsInChunk / joinsPerChunk;
// add every path to json doc
int segmentsToAdd = pathSegsPerChunk * (1. - progress);
for (; pathsAdded < paths.size() && segmentsInChunk < segmentsToAdd;
++pathsAdded)
{
const SGNamedPath& path = paths[pathsAdded];
if (segmentsAdded == 0 && segmentsToAdd >= path.second.size())
{
// we're adding entire path
addPath(path.first, path.second);
segmentsInChunk += path.second.size();
}
else
{
// we need to chop path
while (segmentsAdded < path.second.size() &&
segmentsInChunk < segmentsToAdd)
{
int pathChunkSize = min(pathSegsPerChunk,
(int)path.second.size() - segmentsAdded);
vector<SGSegment>::const_iterator a =
path.second.begin() + segmentsAdded;
vector<SGSegment>::const_iterator b = a + pathChunkSize;
addPath(path.first, vector<SGSegment>(a, b), segmentsAdded);
segmentsAdded += pathChunkSize;
segmentsInChunk += pathChunkSize;
}
}
segmentsAdded = 0;
}
// after this we'll have added 2 copies of the graph to memory
// 1) json doc 2) this buffer
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
_doc->Accept(writer);
// make that a 3rd time -- strip outer nesting {}'s.
string temp(buffer.GetString());
*_os << temp.substr(1, temp.length()-1);
wroteSomething = sequencesInChunk + joinsInChunk + segmentsInChunk > 0;
}
}
void SG2VGJSON::addNode(const SGSequence* seq)
{
Value node;
node.SetObject();
addString(node, "sequence", _bases->at(seq->getID()));
addString(node, "name", seq->getName());
// node id's are 1-based in VG!
addInt(node, "id", seq->getID() + 1);
nodes().PushBack(node, allocator());
}
void SG2VGJSON::addEdge(const SGJoin* join)
{
Value edge;
edge.SetObject();
// node id's are 1-based in VG!
addInt(edge, "from", join->getSide1().getBase().getSeqID() + 1);
addInt(edge, "to", join->getSide2().getBase().getSeqID() + 1);
addBool(edge, "from_start", join->getSide1().getForward() == true);
addBool(edge, "to_end", join->getSide2().getForward() == false);
edges().PushBack(edge, allocator());
}
void SG2VGJSON::addPath(const string& name, const vector<SGSegment>& path,
int rank)
{
Value jpath;
jpath.SetObject();
addString(jpath, "name", name);
Value mappings;
mappings.SetArray();
int inputPathLength = 0;
int outputPathLength = 0;
for (int i = 0; i < path.size(); ++i)
{
sg_int_t sgSeqID = path[i].getSide().getBase().getSeqID();
if (path[i].getLength() != _sg->getSequence(sgSeqID)->getLength())
{
stringstream ss;
ss << "Sanity check fail for Mapping " << i << " of path " << name
<< ": Segment size " << path[i].getLength() << " does not span "
<< "all of node " << (sgSeqID + 1) << " which has length "
<< _sg->getSequence(sgSeqID)->getLength();
throw runtime_error(ss.str());
}
inputPathLength += path[i].getLength();
Value position;
position.SetObject();
// node id's are 1-based in VG!
addInt(position, "node_id", sgSeqID + 1);
// Offsets are along the strand of the node that is being visited.
// We always use the whole node.
addInt(position, "offset", 0);
addBool(position, "is_reverse", !path[i].getSide().getForward());
outputPathLength += _sg->getSequence(sgSeqID)->getLength();
Value mapping;
mapping.SetObject();
mapping.AddMember("position", position, allocator());
addInt(mapping, "rank", rank + i + 1);
mappings.PushBack(mapping, allocator());
}
if (inputPathLength != outputPathLength)
{
stringstream ss;
ss << "Sanity check fail for path " << name << ": input length ("
<< inputPathLength << ") != output length (" << outputPathLength << ")";
throw runtime_error(ss.str());
}
jpath.AddMember("mapping", mappings, allocator());
paths().PushBack(jpath, allocator());
}
void SG2VGJSON::addInt(Value& value, const string& name, int v)
{
Value intValue;
intValue.SetInt(v);
Value nameValue;
nameValue.SetString(name.c_str(), name.length(), allocator());
value.AddMember(nameValue, intValue, allocator());
}
void SG2VGJSON::addString(Value& value, const string& name, const string& v)
{
Value stringValue;
stringValue.SetString(v.c_str(), v.length(), allocator());
Value nameValue;
nameValue.SetString(name.c_str(), name.length(), allocator());
value.AddMember(nameValue, stringValue, allocator());
}
void SG2VGJSON::addBool(Value& value, const string& name, bool v)
{
Value boolValue;
boolValue.SetBool(v);
Value nameValue;
nameValue.SetString(name.c_str(), name.length(), allocator());
value.AddMember(nameValue, boolValue, allocator());
}