forked from glennhickey/sg2vg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sg2vgjson.h
105 lines (78 loc) · 2.6 KB
/
sg2vgjson.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
/*
* Copyright (C) 2015 by Glenn Hickey ([email protected])
*
* Released under the MIT license, see LICENSE.cactus
*/
#ifndef _SG2VGJSON_H
#define _SG2VGJSON_H
#include <vector>
#include <string>
#include <stdexcept>
#include "rapidjson/document.h"
#include "sidegraph.h"
/** Handle all writing of VG JSON format here. This should be replaced
with writing directly to VG protobuf format. In the meantime, we can
load it in wit vg view -J.
Note: current implementation puts everything in memory which is quite
unncessary, but code will probably get scrapped when move to direct vg
interface...
We are writing a SideGraph object, but one that was created with
side2seq -- ie all joins are to ends of sequences, so can be translated
directly to vg sequence graph...
VG JSON Format follows protobuf...
node array
{"node": [{"sequence": "CAAGTAGAGGATC", "id": 1},...
"edge": [{"from": 1, "to": 2}, {"from": 1, "to": 3},
"path": [{"name": "c", "mapping": [{"position": {"node_id": 1}},
*/
class SG2VGJSON
{
public:
SG2VGJSON();
~SG2VGJSON();
/** init output stream and json document */
void init(std::ostream* os);
/** write nodes and edges and paths*/
void writeGraph(const SideGraph* sg,
const std::vector<std::string>& bases,
const std::vector<std::pair<
std::string, std::vector<SGSegment> > >& paths);
protected:
// add to json doc
void addNode(const SGSequence* seq);
void addEdge(const SGJoin* join);
void addPath(const std::string& name, const std::vector<SGSegment>& path);
// rapidjson interface seems pretty horrible but too late to switch
// apis. some helpers:
void addInt(rapidjson::Value& value, const std::string& name, int v);
void addString(rapidjson::Value& value, const std::string& name,
const std::string& v);
void addBool(rapidjson::Value& value, const std::string& name, bool v);
// access arrays
rapidjson::Value& nodes();
rapidjson::Value& edges();
rapidjson::Value& paths();
rapidjson::Document::AllocatorType& allocator();
std::ostream* _os;
const SideGraph* _sg;
const std::vector<std::string>* _bases;
const std::vector<std::pair<std::string, std::vector<SGSegment> > >* _paths;
rapidjson::Document* _doc;
};
inline rapidjson::Value& SG2VGJSON::nodes()
{
return (*_doc)["node"];
}
inline rapidjson::Value& SG2VGJSON::edges()
{
return (*_doc)["edge"];
}
inline rapidjson::Value& SG2VGJSON::paths()
{
return (*_doc)["path"];
}
inline rapidjson::Document::AllocatorType& SG2VGJSON::allocator()
{
return _doc->GetAllocator();
}
#endif