-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
186 lines (160 loc) · 4.85 KB
/
main.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
#include <iostream>
#include <fstream>
#include <getopt.h>
#include "sdsl/bit_vectors.hpp"
#include "stream.hpp"
#include "cpp/vg.pb.h"
#include "succinct.hpp"
using namespace std;
using namespace sdsl;
using namespace vg;
using namespace scg;
void help_main(char** argv) {
cerr << "usage: " << argv[0] << " [options] <graph.vg> >[graph.scg]" << endl
<< "Compressed graph, emits on stdout." << endl
<< endl
<< "options:" << endl
<< " -v, --vg FILE compress graph in vg FILE" << endl
<< " -o, --out FILE serialize graph to FILE" << endl
<< " -i, --in FILE use index in FILE" << endl
<< " -n, --node ID graph neighborhood around node with ID" << endl
<< " -c, --context N steps of context to extract when building neighborhood" << endl
<< " -s, --node-seq ID provide node sequence for ID" << endl
<< " -f, --edges-from ID list edges from node with ID" << endl
<< " -t, --edges-to ID list edges to node with ID" << endl
<< " -h, --help this text" << endl;
}
int main(int argc, char** argv) {
if (argc == 1) {
help_main(argv);
return 1;
}
string vg_name;
string out_name;
string in_name;
int64_t node_id;
bool edges_from = false;
bool edges_to = false;
bool node_sequence = false;
int context_steps = 0;
bool node_context = false;
int c;
optind = 1; // force optind past command positional argument
while (true) {
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{"vg", required_argument, 0, 'v'},
{"out", required_argument, 0, 'o'},
{"in", required_argument, 0, 'i'},
{"node", required_argument, 0, 'n'},
{"context", required_argument, 0, 'c'},
{"edges-from", required_argument, 0, 'f'},
{"edges-to", required_argument, 0, 't'},
{"node-seq", required_argument, 0, 's'},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long (argc, argv, "hv:o:i:f:t:s:c:n:",
long_options, &option_index);
// Detect the end of the options.
if (c == -1)
break;
switch (c)
{
case 'v':
vg_name = optarg;
break;
case 'o':
out_name = optarg;
break;
case 'i':
in_name = optarg;
break;
case 'n':
node_id = atol(optarg);
node_context = true;
break;
case 'c':
context_steps = atoi(optarg);
break;
case 'f':
node_id = atol(optarg);
edges_from = true;
break;
case 't':
node_id = atol(optarg);
edges_to = true;
break;
case 's':
node_id = atol(optarg);
node_sequence = true;
break;
case 'h':
case '?':
help_main(argv);
exit(1);
break;
default:
abort ();
}
}
SuccinctGraph* graph;
//string file_name = argv[optind];
if (in_name.empty()) assert(!vg_name.empty());
if (vg_name == "-") {
graph = new SuccinctGraph;
graph->from_vg(std::cin);
} else if (vg_name.size()) {
ifstream in;
in.open(vg_name.c_str());
graph = new SuccinctGraph;
graph->from_vg(in);
}
if (out_name.size()) {
if (out_name == "-") {
graph->serialize(std::cout);
std::cout.flush();
} else {
ofstream out;
out.open(out_name.c_str());
graph->serialize(out);
out.flush();
}
}
if (in_name.size()) {
graph = new SuccinctGraph;
if (in_name == "-") {
graph->load(std::cin);
} else {
ifstream in;
in.open(in_name.c_str());
graph->load(in);
}
}
// queries
if (node_sequence) {
cout << node_id << ": " << graph->node_sequence(node_id) << endl;
}
if (edges_from) {
vector<Edge> edges = graph->edges_from(node_id);
for (auto& edge : edges) {
cout << edge.from() << " -> " << edge.to() << endl;
}
}
if (edges_to) {
vector<Edge> edges = graph->edges_to(node_id);
for (auto& edge : edges) {
cout << edge.from() << " -> " << edge.to() << endl;
}
}
if (node_context) {
Graph g;
graph->neighborhood(node_id, context_steps, g);
vector<Graph> gb = { g };
stream::write_buffered(cout, gb, 0);
}
// fail...
//delete graph;
return 0;
}