forked from glennhickey/sg2vg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsgclient.cpp
775 lines (682 loc) · 22.9 KB
/
sgclient.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
/*
* Copyright (C) 2015 by Glenn Hickey (hickey@soe.ucsc.edu)
*
* 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 "sgclient.h"
#include "json2sg.h"
using namespace std;
using namespace rapidjson;
const int SGClient::DefaultPageSize = 1000;
const string SGClient::CTHeader = "Content-Type: application/json";
SGClient::SGClient() : _sg(0), _os(0), _pageSize(DefaultPageSize),
_skipPaths(false)
{
}
SGClient::~SGClient()
{
erase();
}
void SGClient::erase()
{
delete _sg;
_url = "";
_sg = 0;
_os = 0;
}
void SGClient::setURL(const string& baseURL)
{
erase();
_url = baseURL;
assert(_url.length() > 1);
if (_url[_url.length() - 1] == '/')
{
_url.resize(_url.length() - 1);
}
size_t lastSlash = _url.find_last_of("/");
size_t lastV = _url.find_last_of("vV");
if (lastV == string::npos ||
(lastSlash != string::npos && lastV <= lastSlash) ||
lastV + 6 < _url.length())
{
throw runtime_error("Version not detected at end of URL");
}
_sg = new SideGraph();
}
void SGClient::setOS(ostream* os)
{
_os = os;
}
void SGClient::setPageSize(int pageSize)
{
_pageSize = pageSize;
}
void SGClient::setSkipPaths(bool skipPaths)
{
_skipPaths = skipPaths;
}
ostream& SGClient::os()
{
return _os != NULL ? *_os : _ignore;
}
const SideGraph* SGClient::downloadGraph(vector<string>& outBases,
vector<NamedPath>& outPaths)
{
map<int, string> refIDMap;
os() << "Downloading References...";
for (int pageToken = 0; pageToken >= 0;)
{
pageToken = downloadReferences(refIDMap, pageToken, _pageSize);
}
os() << " (" << refIDMap.size() << " references retrieved)" << endl;
if (refIDMap.size() == 0)
{
os() << "Warning: No references found" << endl;
}
vector<const SGSequence*> seqs;
outBases.clear();
os() << "Downloading Sequences...";
for (int pageToken = 0; pageToken >= 0;)
{
pageToken = downloadSequences(seqs, &outBases,
refIDMap.empty() ? NULL : &refIDMap,
pageToken, _pageSize);
}
os() << " (" << seqs.size() << " sequences retrieved)" << endl;
vector<const SGJoin*> joins;
os() << "Downloading Joins...";
for (int pageToken = 0; pageToken >= 0;)
{
pageToken = downloadJoins(joins, pageToken, _pageSize);
}
os() << " (" << joins.size() << " joins retrieved)" << endl;
outPaths.clear();
if (_skipPaths == false)
{
os() << "Downloading allele paths... ";
for (int pageToken = 0; pageToken >= 0;)
{
pageToken = downloadAllelePaths(outPaths, pageToken, _pageSize);
}
os() << "(" << outPaths.size() << " paths retrieved)" << endl;
}
return getSideGraph();
}
int SGClient::downloadSequences(vector<const SGSequence*>& outSequences,
vector<string>* outBases,
const map<int, string>* nameIdMap,
int pageToken, int pageSize,
int referenceSetID, int variantSetID)
{
string postOptions = getSequencePostOptions(pageToken, pageSize,
referenceSetID,
variantSetID, outBases != NULL);
string path = "/sequences/search";
// Send the Request
const char* result = _download.postRequest(_url + path,
vector<string>(1, CTHeader),
postOptions);
// Parse the JSON output into a Sequences array and add it to the side graph
JSON2SG parser;
vector<SGSequence*> sequences;
vector<string> bases;
int nextPageToken = -2;
int ret = parser.parseSequences(result, sequences, bases, nextPageToken);
if (ret == -1 || nextPageToken <= -2)
{
stringstream ss;
ss << "Error: POST request for Sequences returned " << result;
throw runtime_error(ss.str());
}
if (nextPageToken >= 0 && pageToken + sequences.size() != nextPageToken)
{
stringstream ss;
ss << "Error: nextPageToken=" << nextPageToken << " returned does not "
<< "equal number of sequences returned (" << sequences.size()
<< ") + pageToken=" << pageToken;
throw runtime_error(ss.str());
}
assert(!outBases || bases.size() == sequences.size());
for (int i = 0; i < sequences.size(); ++i)
{
sg_int_t originalID = sequences[i]->getID();
bool foundName = false;
if (nameIdMap != NULL)
{
map<int, string>::const_iterator si = nameIdMap->find(
sequences[i]->getID());
if (si != nameIdMap->end())
{
// name mapped from reference name
sequences[i]->setName(si->second);
foundName = true;
}
else
{
os() << "\nWarning: Could not find Reference for sequence id "
<< sequences[i]->getID() << " ";
}
}
// no name in the json. no reference found / loaded. so we give it a name
// based on original id
if (foundName == false)
{
stringstream ss;
ss << "Seq" << originalID;
sequences[i]->setName(ss.str());
}
// store map to original id as Side Graph interface requires
// ids be [0,n) which may be unessarily strict. Too lazy right
// now to track down SGExport code that depends on this..
const SGSequence* addedSeq = _sg->addSequence(sequences[i]);
addSeqIDMapping(originalID, addedSeq->getID());
outSequences.push_back(addedSeq);
if (outBases != NULL)
{
outBases->push_back(bases[i]);
}
}
return nextPageToken;
}
int SGClient::downloadReferences(map<int, string>& outIdMap,
int pageToken,
int pageSize,
int referenceSetID)
{
string postOptions = getReferencePostOptions(pageToken, pageSize,
referenceSetID,
vector<int>(),
vector<string>(),
vector<string>(),
vector<string>());
string path = "/references/search";
// Send the Request
const char* result = _download.postRequest(_url + path,
vector<string>(1, CTHeader),
postOptions);
// Parse the JSON output into a Sequences array and add it to the side graph
JSON2SG parser;
map<int, string> idMap;
int nextPageToken = -2;
int ret = parser.parseReferences(result, idMap, nextPageToken);
if (ret == -1 || nextPageToken <= -2)
{
stringstream ss;
ss << "Error: POST request for References returned " << result;
throw runtime_error(ss.str());
}
if (nextPageToken >= 0 && pageToken + idMap.size() != nextPageToken)
{
stringstream ss;
ss << "Error: nextPageToken=" << nextPageToken << " returned does not "
<< "equal number of references returned (" << idMap.size()
<< ") + pageToken=" << pageToken;
throw runtime_error(ss.str());
}
outIdMap.insert(idMap.begin(), idMap.end());
return nextPageToken;
}
int SGClient::downloadBases(sg_int_t sgSeqID, string& outBases, int start,
int end)
{
outBases.clear();
const SGSequence* seq = _sg->getSequence(sgSeqID);
if (seq == NULL)
{
stringstream ss;
ss << "Unable to downloadBases for sgSeqID " << sgSeqID << " because"
<< " sequence was never downloaded using downloadSequecnes";
throw runtime_error(ss.str());
}
int origID = getOriginalSeqID(sgSeqID);
int queryLen = seq->getLength();
stringstream opts;
opts << "/sequences/" << origID << "/bases";
if (start != 0 && end != -1)
{
if (start < 0 || end < 0 || end <= start || end > seq->getLength())
{
stringstream ss;
ss << "start=" << start << ", end=" << end << " invalid for sequence"
<< " id=" << origID << " with length " << seq->getLength();
throw runtime_error(ss.str());
}
opts << "?start=" << start << "\\&end=" << end;
queryLen = end - start;
}
string path = opts.str();
// Send the Request
const char* result = _download.getRequest(_url + path,
vector<string>());
// Parse the JSON output into a string
JSON2SG parser;
int ret = parser.parseBases(result, outBases);
if (ret == -1)
{
stringstream ss;
ss << "Error: GET request for Bases for seqid=" << sgSeqID
<< " returned " << result;
throw runtime_error(ss.str());
}
if (outBases.length() != queryLen)
{
stringstream ss;
ss << "Tried to download " << queryLen << " bases for sequence "
<< origID << " but got " << outBases.length() << " bases.";
throw runtime_error(ss.str());
}
return outBases.length();
}
int SGClient::downloadJoins(vector<const SGJoin*>& outJoins,
int pageToken, int pageSize,
int referenceSetID, int variantSetID)
{
string postOptions = getJoinPostOptions(pageToken, pageSize, referenceSetID,
variantSetID);
string path = "/joins/search";
// Send the Request
const char* result = _download.postRequest(_url + path,
vector<string>(1, CTHeader),
postOptions);
// Parse the JSON output into a Joins array and add it to the side graph
JSON2SG parser;
vector<SGJoin*> joins;
int nextPageToken = -2;
int ret = parser.parseJoins(result, joins, nextPageToken);
if (ret == -1 || nextPageToken <= -2)
{
stringstream ss;
ss << "Error: POST request for Joins returned " << result;
throw runtime_error(ss.str());
}
if (nextPageToken >= 0 && pageToken + joins.size() != nextPageToken)
{
stringstream ss;
ss << "Error: nextPageToken=" << nextPageToken << " returned does not "
<< "equal number of joins returned (" << joins.size()
<< ") + pageToken=" << pageToken;
throw runtime_error(ss.str());
}
for (int i = 0; i < joins.size(); ++i)
{
verifyInJoin(*joins[i]);
mapSeqIDsInJoin(*joins[i]);
outJoins.push_back(_sg->addJoin(joins[i]));
}
return nextPageToken;
}
int SGClient::downloadAllelePaths(vector<NamedPath>& outPaths,
int pageToken, int pageSize,
int sequenceID,
const vector<int>* variantSetIDs,
int start, int end)
{
string postOptions = getAllelePostOptions(pageToken, pageSize, sequenceID,
variantSetIDs, start, end);
string path = "/alleles/search";
// Send the Request
const char* result = _download.postRequest(_url + path,
vector<string>(1, CTHeader),
postOptions);
// POST Request doesn't return paths for some reason. So we scrape out
// all the allele ID's from the result:
JSON2SG parser;
vector<int> alleleIDs;
int nextPageToken = -2;
int ret = parser.parseAlleleIDs(result, alleleIDs, nextPageToken);
if (ret == -1 || nextPageToken <= -2)
{
stringstream ss;
ss << "Error: POST request for Alleles returned " << result;
throw runtime_error(ss.str());
}
if (nextPageToken >= 0 && pageToken + alleleIDs.size() != nextPageToken)
{
stringstream ss;
ss << "Error: nextPageToken=" << nextPageToken << " returned does not "
<< "equal number of alleles returned (" << alleleIDs.size()
<< ") + pageToken=" << pageToken;
throw runtime_error(ss.str());
}
// With IDs in hand, we call downloadAllele on each one to get the path.
NamedPath allelePath;
int alleleVariantSetID;
for (int i = 0; i < alleleIDs.size(); ++i)
{
allelePath.first.clear();
allelePath.second.clear();
ret = downloadAllele(alleleIDs[i], allelePath.second, alleleVariantSetID,
allelePath.first);
if (ret >= 0)
{
outPaths.push_back(allelePath);
}
// Note: if ret < 0, then downloadAllele spits warning
}
return nextPageToken;
}
int SGClient::downloadAllele(int alleleID, vector<SGSegment>& outPath,
int& outVariantSetID, string& outName)
{
outPath.clear();
stringstream opts;
opts << "/alleles/" << alleleID;
string path = opts.str();
const char* result = _download.getRequest(_url + path,
vector<string>());
int outID;
JSON2SG parser;
int ret = parser.parseAllele(result, outID, outPath, outVariantSetID,
outName);
if (ret >=0 && outID != alleleID)
{
throw runtime_error("AlleleID mismatch");
}
if (ret < 0)
{
os() << "Warning: Unable to download allele with ID " << alleleID
<< ". Server returned: " << result << endl;
}
try
{
verifyInPath(alleleID, outPath);
mapSeqIDsInPath(outPath);
}
catch (exception& e)
{
os() << "Warning: Skipping allele path " << outName
<< " because validation produced the following error: " << e.what()
<< endl;
ret = -1;
}
return ret;
}
void SGClient::verifyInJoin(const SGJoin& join) const
{
const SGPosition& pos1 = join.getSide1().getBase();
const SGPosition& pos2 = join.getSide2().getBase();
sg_int_t sgid1 = getSGSeqID(pos1.getSeqID());
sg_int_t sgid2 = getSGSeqID(pos2.getSeqID());
if (sgid1 < 0 || sgid2 < 0)
{
stringstream ss;
ss << "Invalid input join: " << join << ". Sequence ID "
<< (sgid1 < 0 ? pos1.getSeqID() : pos2.getSeqID())
<< " not found in input graph";
throw runtime_error(ss.str());
}
const SGSequence* seq1 = _sg->getSequence(sgid1);
const SGSequence* seq2 = _sg->getSequence(sgid2);
if (pos1.getPos() < 0 || pos1.getPos() >= seq1->getLength())
{
stringstream ss;
ss << "Invalid input join: " << join << ". Position of Side 1"
<< " (" << pos1.getPos() << ") "
<< "not within Sequence with ID=" << pos1.getSeqID()
<< " which has length=" << seq1->getLength();
throw runtime_error(ss.str());
}
if (pos2.getPos() < 0 || pos2.getPos() >= seq2->getLength())
{
stringstream ss;
ss << "Invalid input join: " << join << ". Position of Side 2"
<< " (" << pos2.getPos() << ") "
<< "not within Sequence with ID=" << pos2.getSeqID()
<< " which has length=" << seq2->getLength();
throw runtime_error(ss.str());
}
}
void SGClient::verifyInPath(int alleleID, const vector<SGSegment>& path) const
{
for (int i = 0; i < path.size(); ++i)
{
const SGSegment& seg = path[i];
const SGPosition& pos = seg.getSide().getBase();
sg_int_t sgid = getSGSeqID(pos.getSeqID());
if (sgid < 0)
{
stringstream ss;
ss << "Segment " << i << " of allele path " << alleleID
<< " has unknown sequence ID " << pos.getSeqID();
throw runtime_error(ss.str());
}
const SGSequence* seq = _sg->getSequence(sgid);
if (seg.getMinPos().getPos() < 0 ||
seg.getMaxPos().getPos() >= seq->getLength())
{
stringstream ss;
ss << "Segment " << i << " of allele path " << alleleID
<< " runs from " << seg.getInSide().getBase().getPos() << " to "
<< seg.getOutSide().getBase().getPos() << ", inclusive."
<< " This range is invalid as it spans bases not in "
<< "sequence with ID=" << pos.getSeqID() << " and length="
<< seq->getLength();
throw runtime_error(ss.str());
}
if (i > 0)
{
const SGSegment& prev = path[i - 1];
const SGSide& fromSide = prev.getOutSide();
const SGSide& toSide = seg.getInSide();
SGJoin join(fromSide, toSide);
mapSeqIDsInJoin(join);
if (!join.isTrivial() && _sg->getJoin(&join) == NULL)
{
stringstream ss;
ss << "Join, ["
<< " (Seq:" << fromSide.getBase().getSeqID()
<< ", Pos:" << fromSide.getBase().getPos()
<< ", " << (fromSide.getForward() == true ?
"POS_STRAND" : "NEG_STRAND")
<< ") -> "
<< " (Seq:" << toSide.getBase().getSeqID()
<< ", Pos:" << toSide.getBase().getPos()
<< ", " << (toSide.getForward() == true ?
"POS_STRAND" : "NEG_STRAND") << ") ]"
<< ", implied by segment " << (i - 1) << " = ["
<< " Seq:" << prev.getSide().getBase().getSeqID()
<< ", Pos:" << prev.getSide().getBase().getPos()
<< ", Len:" << prev.getLength()
<< ", " << (prev.getSide().getForward() == true ?
"POS_STRAND" : "NEG_STRAND")
<< " ]"
<< " and segment " << i << " = ["
<< " Seq:" << seg.getSide().getBase().getSeqID()
<< ", Pos:" << seg.getSide().getBase().getPos()
<< ", Len:" << seg.getLength()
<< ", " << (seg.getSide().getForward() == true ?
"POS_STRAND" : "NEG_STRAND")
<< " ]"
<< " of allele path "
<< alleleID << " not found in input graph";
throw runtime_error(ss.str());
}
}
}
}
string SGClient::getSequencePostOptions(int pageToken,
int pageSize,
int referenceSetID,
int variantSetID,
bool getBases) const
{
// Build JSON POST Options
Document doc;
doc.Parse("{}");
Value nv;
assert(nv.IsNull());
doc.AddMember("pageSize", nv, doc.GetAllocator());
doc["pageSize"].SetInt64(pageSize);
doc.AddMember("pageToken", nv, doc.GetAllocator());
if (pageToken > 0)
{
stringstream ss;
ss << pageToken;
doc["pageToken"].SetString(ss.str().c_str(), ss.str().length(),
doc.GetAllocator());
}
doc.AddMember("referenceSetId", nv, doc.GetAllocator());
if (referenceSetID >= 0)
{
doc["referenceSetId"].SetInt64(referenceSetID);
}
doc.AddMember("variantSetId", nv, doc.GetAllocator());
if (variantSetID >= 0)
{
doc["variantSetId"].SetInt64(variantSetID);
}
Value listBases;
listBases.SetBool(getBases);
doc.AddMember("listBases", listBases, doc.GetAllocator());
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
doc.Accept(writer);
return buffer.GetString();
}
string SGClient::getReferencePostOptions(int pageToken,
int pageSize,
int referenceSetID,
const vector<int>& seqIDs,
const vector<string>& md5s,
const vector<string>& accs,
const vector<string>& rnames) const
{
// Build JSON POST Options
Document doc;
doc.Parse("{}");
Value nv;
assert(nv.IsNull());
doc.AddMember("pageSize", nv, doc.GetAllocator());
doc["pageSize"].SetInt64(pageSize);
doc.AddMember("pageToken", nv, doc.GetAllocator());
if (pageToken > 0)
{
stringstream ss;
ss << pageToken;
doc["pageToken"].SetString(ss.str().c_str(), ss.str().length(),
doc.GetAllocator());
}
doc.AddMember("referenceSetId", nv, doc.GetAllocator());
if (referenceSetID >= 0)
{
doc["referenceSetId"].SetInt64(referenceSetID);
}
if (seqIDs.size() > 0 ||
md5s.size() > 0 ||
accs.size() > 0 ||
rnames.size() > 0)
{
throw runtime_error("NOT IMPLEMENTED");
}
Value jsonIds;
jsonIds.SetArray();
doc.AddMember("sequenceIds", jsonIds, doc.GetAllocator());
Value jsonMd5s;
jsonMd5s.SetArray();
doc.AddMember("md5checksums", jsonMd5s, doc.GetAllocator());
Value jsonAccessions;
jsonAccessions.SetArray();
doc.AddMember("accessions", jsonAccessions, doc.GetAllocator());
Value jsonNames;
jsonNames.SetArray();
doc.AddMember("referenceNames", jsonNames, doc.GetAllocator());
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
doc.Accept(writer);
return buffer.GetString();
}
string SGClient::getJoinPostOptions(int pageToken,
int pageSize,
int referenceSetID,
int variantSetID) const
{
// Build JSON POST Options
Document doc;
doc.Parse("{}");
Value nv;
assert(nv.IsNull());
doc.AddMember("pageSize", nv, doc.GetAllocator());
doc["pageSize"].SetInt64(pageSize);
doc.AddMember("pageToken", nv, doc.GetAllocator());
if (pageToken > 0)
{
stringstream ss;
ss << pageToken;
doc["pageToken"].SetString(ss.str().c_str(), ss.str().length(),
doc.GetAllocator());
}
doc.AddMember("referenceSetId", nv, doc.GetAllocator());
if (referenceSetID >= 0)
{
doc["referenceSetId"].SetInt64(referenceSetID);
}
doc.AddMember("variantSetId", nv, doc.GetAllocator());
if (variantSetID >= 0)
{
doc["variantSetId"].SetInt64(variantSetID);
}
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
doc.Accept(writer);
return buffer.GetString();
}
string SGClient::getAllelePostOptions(int pageToken,
int pageSize,
int sequenceID,
const vector<int>* variantSetIDs,
int start,
int end) const
{
// Build JSON POST Options
Document doc;
doc.Parse("{}");
Value nv;
assert(nv.IsNull());
doc.AddMember("pageSize", nv, doc.GetAllocator());
doc["pageSize"].SetInt64(pageSize);
doc.AddMember("pageToken", nv, doc.GetAllocator());
if (pageToken > 0)
{
stringstream ss;
ss << pageToken;
doc["pageToken"].SetString(ss.str().c_str(), ss.str().length(),
doc.GetAllocator());
}
// default to empty string (if -1) unlike pagetoken which defaults to null.
Value jsonSequenceID;
string sequenceIDString;
if (sequenceID >= 0)
{
stringstream ss;
ss << sequenceID;
sequenceIDString = ss.str();
}
jsonSequenceID.SetString(sequenceIDString.c_str(), sequenceIDString.length(),
doc.GetAllocator());
doc.AddMember("sequenceId", jsonSequenceID, doc.GetAllocator());
Value jsonVariantSetIDs;
jsonVariantSetIDs.SetArray();
doc.AddMember("variantSetIds", jsonVariantSetIDs, doc.GetAllocator());
if (variantSetIDs != NULL)
{
for (int i = 0; i < variantSetIDs->size(); ++i)
{
Value id;
id.SetInt64(variantSetIDs->at(i));
jsonVariantSetIDs.PushBack(id, doc.GetAllocator());
}
}
doc.AddMember("start", nv, doc.GetAllocator());
doc["start"].SetInt64(start);
doc.AddMember("end", nv, doc.GetAllocator());
doc["end"].SetInt64(end);
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
doc.Accept(writer);
return buffer.GetString();
}