forked from vgteam/vg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.cpp
1942 lines (1709 loc) · 69.8 KB
/
index.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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "index.hpp"
namespace vg {
using namespace std;
Index::Index(void) {
start_sep = '\x00';
end_sep = '\xff';
write_options = rocksdb::WriteOptions();
mem_env = false;
use_snappy = false;
//block_cache_size = 1024 * 1024 * 10; // 10MB
threads = 1;
#pragma omp parallel
{
#pragma omp master
threads = omp_get_num_threads();
}
}
rocksdb::Options Index::GetOptions(void) {
rocksdb::Options options;
if (mem_env) {
options.env = rocksdb::NewMemEnv(options.env);
}
options.create_if_missing = true;
options.max_open_files = -1;
options.compression = rocksdb::kSnappyCompression;
options.compaction_style = rocksdb::kCompactionStyleLevel;
// we are unlikely to reach either of these limits
options.IncreaseParallelism(threads);
options.max_background_flushes = threads;
options.max_background_compactions = threads;
options.num_levels = 2;
options.target_file_size_base = (long) 1024 * 1024 * 512; // ~512MB (bigger in practice)
options.write_buffer_size = 1024 * 1024 * 256; // ~256MB
// doesn't work this way
rocksdb::BlockBasedTableOptions topt;
topt.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, true));
topt.block_cache = rocksdb::NewLRUCache(512 * 1024 * 1024, 7);
topt.no_block_cache = true;
options.table_factory.reset(NewBlockBasedTableFactory(topt));
options.table_cache_numshardbits = 7;
options.allow_mmap_reads = true;
options.allow_mmap_writes = false;
if (bulk_load) {
options.PrepareForBulkLoad();
options.max_write_buffer_number = threads;
options.max_background_flushes = threads;
options.max_background_compactions = threads;
options.compaction_style = rocksdb::kCompactionStyleNone;
options.memtable_factory.reset(new rocksdb::VectorRepFactory(1000));
}
options.compression_per_level.resize(options.num_levels);
for (int i = 0; i < options.num_levels; ++i) {
if (i == 0 || use_snappy == true) {
options.compression_per_level[i] = rocksdb::kSnappyCompression;
} else {
options.compression_per_level[i] = rocksdb::kZlibCompression;
}
}
return options;
}
void Index::open(const std::string& dir, bool read_only) {
name = dir;
db_options = GetOptions();
rocksdb::Status s;
if (read_only) {
//s = rocksdb::DB::Open(db_options, name, &db);
s = rocksdb::DB::OpenForReadOnly(db_options, name, &db);
} else {
s = rocksdb::DB::Open(db_options, name, &db);
}
if (!s.ok()) {
throw indexOpenException();
}
is_open = true;
}
void Index::open_read_only(string& dir) {
bulk_load = false;
//mem_env = true;
open(dir, true);
}
void Index::open_for_write(string& dir) {
bulk_load = false;
open(dir, false);
}
void Index::open_for_bulk_load(string& dir) {
bulk_load = true;
open(dir, false);
}
Index::~Index(void) {
if (is_open) {
close();
}
}
void Index::close(void) {
flush();
delete db;
is_open = false;
}
void Index::flush(void) {
db->Flush(rocksdb::FlushOptions());
}
void Index::compact(void) {
db->CompactRange(NULL, NULL);
}
// todo: replace with union / struct
const string Index::key_for_node(int64_t id) {
string key;
id = htobe64(id);
key.resize(5*sizeof(char) + sizeof(int64_t));
char* k = (char*) key.c_str();
k[0] = start_sep;
k[1] = 'g'; // graph elements
k[2] = start_sep;
memcpy(k + sizeof(char)*3, &id, sizeof(int64_t));
k[3 + sizeof(int64_t)] = start_sep;
k[3 + sizeof(int64_t) + 1] = 'n';
return key;
}
const string Index::key_for_edge_on_start(int64_t node, int64_t other, bool backward) {
// reverse endianness for sorting
node = htobe64(node);
other = htobe64(other);
string key;
key.resize(8*sizeof(char) + 2*sizeof(int64_t));
char* k = (char*) key.c_str();
k[0] = start_sep;
k[1] = 'g'; // graph elements
k[2] = start_sep;
memcpy(k + sizeof(char)*3, &node, sizeof(int64_t));
k[3 + sizeof(int64_t)] = start_sep;
k[4 + sizeof(int64_t)] = 's'; // edge on start
k[5 + sizeof(int64_t)] = start_sep;
memcpy(k + sizeof(char)*6 + sizeof(int64_t), &other, sizeof(int64_t));
k[6 + 2*sizeof(int64_t)] = start_sep;
k[7 + 2*sizeof(int64_t)] = backward ? '1' : '0';
return key;
}
const string Index::key_for_edge_on_end(int64_t node, int64_t other, bool backward) {
// reverse endianness for sorting
node = htobe64(node);
other = htobe64(other);
string key;
key.resize(8*sizeof(char) + 2*sizeof(int64_t));
char* k = (char*) key.c_str();
k[0] = start_sep;
k[1] = 'g'; // graph elements
k[2] = start_sep;
memcpy(k + sizeof(char)*3, &node, sizeof(int64_t));
k[3 + sizeof(int64_t)] = start_sep;
k[4 + sizeof(int64_t)] = 'e'; // edge on end
k[5 + sizeof(int64_t)] = start_sep;
memcpy(k + sizeof(char)*6 + sizeof(int64_t), &other, sizeof(int64_t));
k[6 + 2*sizeof(int64_t)] = start_sep;
k[7 + 2*sizeof(int64_t)] = backward ? '1' : '0';
return key;
}
const string Index::key_for_kmer(const string& kmer, int64_t id) {
id = htobe64(id);
string key;
key.resize(4*sizeof(char) + kmer.size() + sizeof(int64_t));
char* k = (char*) key.c_str();
k[0] = start_sep;
k[1] = 'k'; // kmers
k[2] = start_sep;
memcpy(k + sizeof(char)*3, kmer.c_str(), kmer.size());
k[sizeof(char)*3 + kmer.size()] = start_sep;
memcpy(k + sizeof(char)*4 + kmer.size(), &id, sizeof(int64_t));
return key;
}
const string Index::key_for_node_path_position(int64_t node_id, int64_t path_id, int64_t path_pos, bool backward) {
node_id = htobe64(node_id);
path_id = htobe64(path_id);
path_pos = htobe64(path_pos);
string key;
key.resize(9*sizeof(char) + 3*sizeof(int64_t));
char* k = (char*) key.c_str();
k[0] = start_sep;
k[1] = 'g'; // graph elements
k[2] = start_sep;
memcpy(k + sizeof(char)*3, &node_id, sizeof(int64_t));
k[3 + sizeof(int64_t)] = start_sep;
k[4 + sizeof(int64_t)] = 'p';
k[5 + sizeof(int64_t)] = start_sep;
memcpy(k + sizeof(char)*6 + sizeof(int64_t), &path_id, sizeof(int64_t));
k[6 + 2*sizeof(int64_t)] = start_sep;
memcpy(k + sizeof(char)*7 + 2*sizeof(int64_t), &path_pos, sizeof(int64_t));
k[7 + 3*sizeof(int64_t)] = start_sep;
k[8 + 3*sizeof(int64_t)] = backward ? '1' : '0';
return key;
}
const string Index::key_prefix_for_node_path(int64_t node_id, int64_t path_id) {
node_id = htobe64(node_id);
path_id = htobe64(path_id);
string key;
key.resize(6*sizeof(char) + 2*sizeof(int64_t));
char* k = (char*) key.c_str();
k[0] = start_sep;
k[1] = 'g'; // graph elements
k[2] = start_sep;
memcpy(k + sizeof(char)*3, &node_id, sizeof(int64_t));
k[3 + sizeof(int64_t)] = start_sep;
k[4 + sizeof(int64_t)] = 'p';
k[5 + sizeof(int64_t)] = start_sep;
memcpy(k + sizeof(char)*6 + sizeof(int64_t), &path_id, sizeof(int64_t));
return key;
}
const string Index::key_for_path_position(int64_t path_id, int64_t path_pos, bool backward, int64_t node_id) {
node_id = htobe64(node_id);
path_id = htobe64(path_id);
path_pos = htobe64(path_pos);
string key;
key.resize(7*sizeof(char) + 3*sizeof(int64_t));
char* k = (char*) key.c_str();
k[0] = start_sep;
k[1] = 'p'; // graph elements
k[2] = start_sep;
memcpy(k + sizeof(char)*3, &path_id, sizeof(int64_t));
k[3 + sizeof(int64_t)] = start_sep;
memcpy(k + sizeof(char)*4 + sizeof(int64_t), &path_pos, sizeof(int64_t));
k[4 + 2*sizeof(int64_t)] = start_sep;
k[5 + 2*sizeof(int64_t)] = backward ? '1' : '0';
k[6 + 2*sizeof(int64_t)] = start_sep;
memcpy(k + sizeof(char)*7 + 2*sizeof(int64_t), &node_id, sizeof(int64_t));
return key;
}
const string Index::key_prefix_for_kmer(const string& kmer) {
string key;
key.resize(3*sizeof(char) + kmer.size());
char* k = (char*) key.c_str();
k[0] = start_sep;
k[1] = 'k'; // kmers
k[2] = start_sep;
memcpy(k + sizeof(char)*3, kmer.c_str(), kmer.size());
return key;
}
const string Index::key_for_metadata(const string& tag) {
string key;
key.resize(3*sizeof(char) + tag.size());
char* k = (char*) key.c_str();
k[0] = start_sep;
k[1] = 'm'; // metadata
k[2] = start_sep;
memcpy(k + sizeof(char)*3, tag.c_str(), tag.size());
return key;
}
const string Index::key_for_mapping_prefix(int64_t node_id) {
node_id = htobe64(node_id);
string key;
key.resize(3*sizeof(char) + sizeof(int64_t));
char* k = (char*) key.c_str();
k[0] = start_sep;
k[1] = 's'; // mappings (~sides)
k[2] = start_sep;
memcpy(k + sizeof(char)*3, &node_id, sizeof(int64_t));
return key;
}
const string Index::key_for_mapping(const Mapping& mapping) {
string data;
mapping.SerializeToString(&data);
const string prefix = key_for_mapping_prefix(mapping.position().node_id());
// use first 8 chars of sha1sum of object; space is 16^8 = 4294967296
const string hash = sha1head(data, 8);
string key = prefix;
key.resize(prefix.size() + sizeof(char) + hash.size());
char* k = (char*) key.c_str();
k[prefix.size()] = start_sep;
memcpy(k + prefix.size() + sizeof(char), hash.c_str(), hash.size());
return key;
}
const string Index::key_for_alignment_prefix(int64_t node_id) {
node_id = htobe64(node_id);
string key;
key.resize(3*sizeof(char) + sizeof(int64_t));
char* k = (char*) key.c_str();
k[0] = start_sep;
k[1] = 'a'; // alignments
k[2] = start_sep;
memcpy(k + sizeof(char)*3, &node_id, sizeof(int64_t));
return key;
}
const string Index::key_for_alignment(const Alignment& alignment) {
string data;
alignment.SerializeToString(&data);
const string prefix = key_for_alignment_prefix(alignment.path().mapping(0).position().node_id());
// use first 8 chars of sha1sum of object; space is 16^8 = 4294967296
// maybe this shouldn't be a hash, but a random nonce--- although keep in mind
// that for the alignment to be identical, it would need the same read name, quality score, sequence, mapping, etc.
// in other words it'd be an exact duplicate
const string hash = sha1head(data, 8);
string key = prefix;
key.resize(prefix.size() + sizeof(char) + hash.size());
char* k = (char*) key.c_str();
k[prefix.size()] = start_sep;
memcpy(k + prefix.size() + sizeof(char), hash.c_str(), hash.size());
return key;
}
const string Index::key_prefix_for_edges_on_node_start(int64_t node) {
string key = key_for_edge_on_start(node, 0, false);
return key.substr(0, key.size()-sizeof(int64_t)-2*sizeof(char));
}
const string Index::key_prefix_for_edges_on_node_end(int64_t node) {
string key = key_for_edge_on_end(node, 0, false);
return key.substr(0, key.size()-sizeof(int64_t)-2*sizeof(char));
}
char Index::graph_key_type(const string& key) {
return key.c_str()[4*sizeof(char) + sizeof(int64_t)];
}
string Index::entry_to_string(const string& key, const string& value) {
char type = key[1];
switch (type) {
case 'g':
return graph_entry_to_string(key, value);
break;
case 'k':
return kmer_entry_to_string(key, value);
break;
case 'p':
return path_position_to_string(key, value);
break;
case 'm':
return metadata_entry_to_string(key, value);
break;
case 's':
return mapping_entry_to_string(key, value);
break;
case 'a':
return alignment_entry_to_string(key, value);
break;
default:
break;
}
}
void Index::parse_node(const string& key, const string& value, int64_t& id, Node& node) {
const char* k = key.c_str();
memcpy(&id, (k + 3*sizeof(char)), sizeof(int64_t));
id = be64toh(id);
node.ParseFromString(value);
}
void Index::parse_edge(const string& key, char& type, int64_t& node_id, int64_t& other_id, bool& backward) {
// Parse the edge just out of the key
const char* k = key.c_str();
// Work out what type the key is ('s' or 'e' depending on if it's on the first node's start or end).
type = graph_key_type(key);
// Get the node IDs involved.
memcpy(&node_id, (k + 3*sizeof(char)), sizeof(int64_t));
memcpy(&other_id, (k + 6*sizeof(char)) + sizeof(int64_t), sizeof(int64_t));
node_id = be64toh(node_id);
other_id = be64toh(other_id);
// Is the relative orientation forward ('0') or backward ('1')?
char backward_char;
memcpy(&backward_char, (k + 7*sizeof(char)) + 2*sizeof(int64_t), sizeof(char));
backward = backward_char == '1';
}
void Index::parse_edge(const string& key, const string& value, char& type, int64_t& id1, int64_t& id2, Edge& edge) {
// We can take either of the two edge keys:
// +g+node_id+s+other_id+backward
// +g+node_id+e+other_id+backward
if(value.size() > 0) {
// We can just deserialize the edge.
edge.ParseFromString(value);
// But we still need to fill in our output parameters
type = graph_key_type(key);
id1 = edge.from();
id2 = edge.to();
} else {
// We have to synthesize an edge.
// Get what we can from the key. Arbitrarily say this node is the from.
bool backward;
parse_edge(key, type, id1, id2, backward);
// Work out if the edge should be from the start
bool from_start = type == 's';
// And if it should be to the end. We attach to the end of the other
// node when we attached to the start of this node and we want to be
// forward, or when we attached to the end of this node and we want to
// be backward. That works out to: XOR(on start, should be backward).
bool to_end = from_start != backward;
if(from_start && to_end) {
// If we got that it should be both, we can replace it with the
// normal end to start edge going the other way.
swap(id1, id2);
from_start = to_end = false;
}
// Build the edge
edge.set_from(id1);
edge.set_to(id2);
edge.set_from_start(from_start);
edge.set_to_end(to_end);
// TODO: get the edge data somehow in these cases instead of making up edges.
}
}
string Index::graph_entry_to_string(const string& key, const string& value) {
// do we have a node or edge?
stringstream s;
switch (graph_key_type(key)) {
case 'n': {
// it's a node
int64_t id;
Node node;
parse_node(key, value, id, node);
s << "{\"key\":\"+g+" << id << "+n\", \"value\":"<< pb2json(node) << "}";
} break;
case 's': {
Edge edge;
int64_t id1, id2;
char type;
bool backward;
if(value.size() > 0) {
edge.ParseFromString(value);
}
parse_edge(key, type, id1, id2, backward);
s << "{\"key\":\"+g+" << id1 << "+s+" << id2 << "+" << (backward ? '1' : '0')
<< "\", \"value\":"<< (value.size() > 0 ? pb2json(edge) : "") << "}";
} break;
case 'e': {
Edge edge;
int64_t id1, id2;
char type;
bool backward;
if(value.size() > 0) {
edge.ParseFromString(value);
}
parse_edge(key, type, id1, id2, backward);
s << "{\"key\":\"+g+" << id1 << "+e+" << id2 << "+" << (backward ? '1' : '0')
<< "\", \"value\":"<< (value.size() > 0 ? pb2json(edge) : "") << "}";
} break;
case 'p': {
s << node_path_to_string(key, value);
} break;
}
return s.str();
}
void Index::parse_kmer(const string& key, const string& value, string& kmer, int64_t& id, int32_t& pos) {
const char* k = key.c_str();
kmer = string(k+3*sizeof(char));
memcpy(&id, k+4*sizeof(char)+kmer.size(), sizeof(int64_t));
id = be64toh(id);
memcpy(&pos, (char*)value.c_str(), sizeof(int32_t));
}
string Index::kmer_entry_to_string(const string& key, const string& value) {
stringstream s;
int64_t id;
int32_t pos;
string kmer;
parse_kmer(key, value, kmer, id, pos);
s << "{\"key\":\"+k+" << kmer << "+" << id << "\", \"value\":"<< pos << "}";
return s.str();
}
void Index::parse_node_path(const string& key, const string& value,
int64_t& node_id, int64_t& path_id, int64_t& path_pos, bool& backward, Mapping& mapping) {
const char* k = key.c_str();
memcpy(&node_id, (k + 3*sizeof(char)), sizeof(int64_t));
memcpy(&path_id, (k + 6*sizeof(char)+sizeof(int64_t)), sizeof(int64_t));
memcpy(&path_pos, (k + 7*sizeof(char)+2*sizeof(int64_t)), sizeof(int64_t));
backward = (k[8 + 3*sizeof(int64_t)] == '1');
node_id = be64toh(node_id);
path_id = be64toh(path_id);
path_pos = be64toh(path_pos);
mapping.ParseFromString(value);
}
void Index::parse_path_position(const string& key, const string& value,
int64_t& path_id, int64_t& path_pos, bool& backward, int64_t& node_id, Mapping& mapping) {
const char* k = key.c_str();
memcpy(&path_id, (k + 3*sizeof(char)), sizeof(int64_t));
memcpy(&path_pos, (k + 4*sizeof(char)+sizeof(int64_t)), sizeof(int64_t));
backward = (k[5 + 2*sizeof(int64_t)] == '1');
memcpy(&node_id, (k + 7*sizeof(char)+2*sizeof(int64_t)), sizeof(int64_t));
node_id = be64toh(node_id);
path_id = be64toh(path_id);
path_pos = be64toh(path_pos);
mapping.ParseFromString(value);
}
void Index::parse_mapping(const string& key, const string& value, int64_t& node_id, string& hash, Mapping& mapping) {
const char* k = key.c_str();
memcpy(&node_id, (k + 3*sizeof(char)), sizeof(int64_t));
hash.resize(8);
memcpy((char*)hash.c_str(), (k + 4*sizeof(char) + sizeof(int64_t)), 8*sizeof(char));
node_id = be64toh(node_id);
mapping.ParseFromString(value);
}
void Index::parse_alignment(const string& key, const string& value, int64_t& node_id, string& hash, Alignment& alignment) {
const char* k = key.c_str();
memcpy(&node_id, (k + 3*sizeof(char)), sizeof(int64_t));
hash.resize(8);
memcpy((char*)hash.c_str(), (k + 4*sizeof(char) + sizeof(int64_t)), 8*sizeof(char));
node_id = be64toh(node_id);
alignment.ParseFromString(value);
}
string Index::node_path_to_string(const string& key, const string& value) {
Mapping mapping;
int64_t node_id, path_id, path_pos;
bool backward;
parse_node_path(key, value, node_id, path_id, path_pos, backward, mapping);
stringstream s;
s << "{\"key\":\"+g+" << node_id << "+p+" << path_id << "+" << path_pos << "+" << (backward ? '1' : '0')
<< "\", \"value\":"<< pb2json(mapping) << "}";
return s.str();
}
string Index::path_position_to_string(const string& key, const string& value) {
Mapping mapping;
int64_t node_id, path_id, path_pos;
bool backward;
parse_path_position(key, value, path_id, path_pos, backward, node_id, mapping);
stringstream s;
s << "{\"key\":\"+p+" << path_id << "+" << path_pos << "+" << (backward ? '1' : '0') << "+" << node_id
<< "\", \"value\":"<< pb2json(mapping) << "}";
return s.str();
}
string Index::metadata_entry_to_string(const string& key, const string& value) {
stringstream s;
string prefix = key.substr(3);
string val = value;
if (prefix == "max_path_id"
|| prefix.substr(0,9) == "path_name") {
stringstream v;
int64_t id;
memcpy(&id, (char*)value.c_str(), sizeof(int64_t));
v << id;
val = v.str();
} else if (prefix.substr(0,7) == "path_id") {
stringstream v;
int64_t id;
memcpy(&id, ((char*)prefix.c_str())+7, sizeof(int64_t));
v << id;
prefix = prefix.substr(0,7) + "+" + v.str();
}
s << "{\"key\":\"" << "+" << key[1] << "+" << prefix << "\", \"value\":\""<< val << "\"}";
return s.str();
}
string Index::mapping_entry_to_string(const string& key, const string& value) {
Mapping mapping;
int64_t node_id;
string hash;
parse_mapping(key, value, node_id, hash, mapping);
stringstream s;
s << "{\"key\":\"+s+" << node_id << "+" << hash << "\", \"value\":"<< pb2json(mapping) << "}";
return s.str();
}
string Index::alignment_entry_to_string(const string& key, const string& value) {
Alignment alignment;
int64_t node_id;
string hash;
parse_alignment(key, value, node_id, hash, alignment);
stringstream s;
s << "{\"key\":\"+a+" << node_id << "+" << hash << "\", \"value\":"<< pb2json(alignment) << "}";
return s.str();
}
void Index::dump(ostream& out) {
rocksdb::Iterator* it = db->NewIterator(rocksdb::ReadOptions());
for (it->SeekToFirst(); it->Valid(); it->Next()) {
out << entry_to_string(it->key().ToString(), it->value().ToString()) << endl;
}
assert(it->status().ok()); // Check for any errors found during the scan
delete it;
}
void Index::put_node(const Node* node) {
string data;
node->SerializeToString(&data);
string key = key_for_node(node->id());
db->Put(write_options, key, data);
}
void Index::batch_node(const Node* node, rocksdb::WriteBatch& batch) {
string data;
node->SerializeToString(&data);
string key = key_for_node(node->id());
batch.Put(key, data);
}
void Index::put_edge(const Edge* edge) {
// At least one edge key will hold the serialized edge data
string data;
edge->SerializeToString(&data);
// One will probably hold an empty string, unless this is a self loop somehow.
string null_data;
// only store serialized edge in the key linking the edge to the smaller
// node. If the two node IDs are equal, store in both keys (which might just actually be one key).
string& from_data = (edge->from() <= edge->to()) ? data : null_data;
string& to_data = (edge->to() <= edge->from()) ? data : null_data;
// Is the edge reversing relative node orientation?
bool backward = (edge->from_start() != edge->to_end());
if(edge->from_start()) {
// On the from node, we're on the start
db->Put(write_options, key_for_edge_on_start(edge->from(), edge->to(), backward), from_data);
} else {
// On the from node, we're on the end
db->Put(write_options, key_for_edge_on_end(edge->from(), edge->to(), backward), from_data);
}
if(edge->to_end()) {
// On the to node, we're on the end
db->Put(write_options, key_for_edge_on_end(edge->to(), edge->from(), backward), to_data);
} else {
// On the to node, we're on the start
db->Put(write_options, key_for_edge_on_start(edge->to(), edge->from(), backward), to_data);
}
}
void Index::batch_edge(const Edge* edge, rocksdb::WriteBatch& batch) {
// At least one edge key will hold the serialized edge data
string data;
edge->SerializeToString(&data);
// One will probably hold an empty string, unless this is a self loop somehow.
string null_data;
// only store serialized edge in the key linking the edge to the smaller
// node. If the two node IDs are equal, store in both keys (which might just actually be one key).
string& from_data = (edge->from() <= edge->to()) ? data : null_data;
string& to_data = (edge->to() <= edge->from()) ? data : null_data;
// Is the edge reversing relative node orientation?
bool backward = (edge->from_start() != edge->to_end());
if(edge->from_start()) {
// On the from node, we're on the start
batch.Put(key_for_edge_on_start(edge->from(), edge->to(), backward), from_data);
} else {
// On the from node, we're on the end
batch.Put(key_for_edge_on_end(edge->from(), edge->to(), backward), from_data);
}
if(edge->to_end()) {
// On the to node, we're on the end
batch.Put(key_for_edge_on_end(edge->to(), edge->from(), backward), to_data);
} else {
// On the to node, we're on the start
batch.Put(key_for_edge_on_start(edge->to(), edge->from(), backward), to_data);
}
}
void Index::put_metadata(const string& tag, const string& data) {
string key = key_for_metadata(tag);
db->Put(write_options, key, data);
}
void Index::put_node_path(int64_t node_id, int64_t path_id, int64_t path_pos, bool backward, const Mapping& mapping) {
string data;
mapping.SerializeToString(&data);
db->Put(write_options, key_for_node_path_position(node_id, path_id, path_pos, backward), data);
}
void Index::put_path_position(int64_t path_id, int64_t path_pos, bool backward, int64_t node_id, const Mapping& mapping) {
string data;
mapping.SerializeToString(&data);
db->Put(write_options, key_for_path_position(path_id, path_pos, backward, node_id), data);
}
void Index::put_mapping(const Mapping& mapping) {
string data;
mapping.SerializeToString(&data);
db->Put(write_options, key_for_mapping(mapping), data);
}
void Index::put_alignment(const Alignment& alignment) {
string data;
alignment.SerializeToString(&data);
db->Put(write_options, key_for_alignment(alignment), data);
}
void Index::load_graph(VG& graph) {
// a bit of a hack--- the logging only works with for_each_*parallel
// also the high parallelism may be causing issues
int thread_count = 1;
#pragma omp parallel
{
#pragma omp master
thread_count = omp_get_num_threads();
}
omp_set_num_threads(1);
graph.create_progress("indexing nodes of " + graph.name, graph.graph.node_size());
rocksdb::WriteBatch batch;
graph.for_each_node_parallel([this, &batch](Node* n) { batch_node(n, batch); });
graph.destroy_progress();
graph.create_progress("indexing edges of " + graph.name, graph.graph.edge_size());
graph.for_each_edge_parallel([this, &batch](Edge* e) { batch_edge(e, batch); });
rocksdb::Status s = db->Write(write_options, &batch);
omp_set_num_threads(thread_count);
}
void Index::load_paths(VG& graph) {
graph.destroy_progress();
graph.create_progress("indexing paths of " + graph.name, graph.paths._paths.size());
store_paths(graph);
graph.destroy_progress();
}
int64_t Index::get_max_path_id(void) {
string data;
int64_t id;
rocksdb::Status s = get_metadata("max_path_id", data);
if (!s.ok()) {
id = 0;
put_max_path_id(id);
} else {
memcpy(&id, data.c_str(), sizeof(int64_t));
}
return id;
}
void Index::put_max_path_id(int64_t id) {
string data;
data.resize(sizeof(int64_t));
memcpy((char*)data.c_str(), &id, sizeof(int64_t));
put_metadata("max_path_id", data);
}
int64_t Index::new_path_id(const string& path_name) {
int64_t max_id = get_max_path_id();
int64_t new_id = max_id + 1;
put_max_path_id(new_id);
put_path_id_to_name(new_id, path_name);
put_path_name_to_id(new_id, path_name);
return new_id;
}
string Index::path_name_prefix(const string& name) {
return "path_name" + start_sep + name;
}
string Index::path_id_prefix(int64_t id) {
string prefix = "path_id" + start_sep;
size_t prefix_size = prefix.size();
prefix.resize(prefix.size() + sizeof(int64_t));
memcpy((char*)prefix.c_str() + prefix_size, &id, sizeof(int64_t));
return prefix;
}
void Index::put_path_id_to_name(int64_t id, const string& name) {
put_metadata(path_id_prefix(id), name);
}
void Index::put_path_name_to_id(int64_t id, const string& name) {
string data;
data.resize(sizeof(int64_t));
memcpy((char*)data.c_str(), &id, sizeof(int64_t));
put_metadata(path_name_prefix(name), data);
}
string Index::get_path_name(int64_t id) {
string data;
get_metadata(path_id_prefix(id), data);
return data;
}
int64_t Index::get_path_id(const string& name) {
string data;
get_metadata(path_name_prefix(name), data);
int64_t id = 0;
memcpy(&id, (char*)data.c_str(), sizeof(int64_t));
return id;
}
void Index::store_paths(VG& graph) {
function<void(Path&)> lambda = [this, &graph](Path& path) {
store_path(graph, path);
};
graph.paths.for_each(lambda);
}
void Index::store_path(VG& graph, Path& path) {
// get a new path id
// if there is no name, cry
if (path.name().empty()) {
cerr << "[vg::Index] error, path has no name" << endl;
exit(1);
}
// check if the path name/id mapping already exists
int64_t path_id;
path_id = get_path_id(path.name());
// if it doesn't, create it
if (!path_id) {
path_id = new_path_id(path.name());
}
// keep track of position
int64_t path_pos = 0;
// for each node in the path
for (int64_t i = 0; i < path.mapping_size(); ++i) {
const Mapping& mapping = path.mapping(i);
// put an entry in the path table
put_path_position(path_id, path_pos, mapping.is_reverse(), mapping.position().node_id(), mapping);
// put an entry in the graph table
put_node_path(mapping.position().node_id(), path_id, path_pos, mapping.is_reverse(), mapping);
// get the node, to find the size of this step
Node node;
get_node(mapping.position().node_id(), node);
// TODO use the cigar... if there is one
path_pos += node.sequence().size();
graph.update_progress(graph.progress_count+1);
}
}
rocksdb::Status Index::get_metadata(const string& key, string& data) {
rocksdb::Status s = db->Get(rocksdb::ReadOptions(), key_for_metadata(key), &data);
return s;
}
rocksdb::Status Index::get_node(int64_t id, Node& node) {
string value;
rocksdb::Status s = db->Get(rocksdb::ReadOptions(), key_for_node(id), &value);
if (s.ok()) {
node.ParseFromString(value);
}
return s;
}
rocksdb::Status Index::get_edge(int64_t from, bool from_start, int64_t to, bool to_end, Edge& edge) {
// Are we looking for a reversing edge?
bool backward = from_start != to_end;
// What key do we need to look up to get the edge data?
string key;
// TODO: restructure keys so we don't need to do so much figuring to work out what to look up.
if(from < to) {
// We will find the edge data on the record for its attachment to the from node.
if(from_start) {
key = key_for_edge_on_start(from, to, backward);
} else {
key = key_for_edge_on_end(from, to, backward);
}
} else {
// We will find the edge data on the record for its attachment to the to node.
if(to_end) {
key = key_for_edge_on_end(to, from, backward);
} else {
key = key_for_edge_on_start(to, from, backward);
}
}
string value;
rocksdb::Status s = db->Get(rocksdb::ReadOptions(), key, &value);
if (s.ok()) {
edge.ParseFromString(value);
}
return s;
}
void Index::get_mappings(int64_t node_id, vector<Mapping>& mappings) {
string start = key_for_mapping_prefix(node_id);
string end = start + end_sep;
for_range(start, end, [this, &mappings](string& key, string& value) {
mappings.emplace_back();
Mapping& mapping = mappings.back();
mapping.ParseFromString(value);
});
}
void Index::get_alignments(int64_t node_id, vector<Alignment>& alignments) {
string start = key_for_alignment_prefix(node_id);
string end = start + end_sep;
for_range(start, end, [this, &alignments](string& key, string& value) {
alignments.emplace_back();
Alignment& alignment = alignments.back();
alignment.ParseFromString(value);
});
}
int Index::get_node_path(int64_t node_id, int64_t path_id, int64_t& path_pos, bool& backward, Mapping& mapping) {
string value;
string key = key_prefix_for_node_path(node_id, path_id);
string start = key + start_sep;
string end = key + end_sep;
// NB: uses the first position in the range
// apply to the range matching the kmer in the db
int count = 0;
for_range(start, end, [this, &count, &node_id, &path_id, &path_pos, &backward, &mapping](string& key, string& value) {
if (count == 0) {
parse_node_path(key, value,
node_id, path_id,
path_pos, backward, mapping);
}
++count;
});
return count;
}
pair<list<pair<int64_t, bool>>, pair<int64_t, bool>> Index::get_nearest_node_prev_path_member(
int64_t node_id, bool backward, int64_t path_id, int64_t& path_pos, bool& relative_orientation, int max_steps) {
list<pair<int64_t, bool>> nullpath;
list<pair<int64_t, bool>> bpath;
// Keeps a list of oriented nodes we can reach, by path taken to reach them
map<list<pair<int64_t, bool>>, pair<Node, bool>> nq;
{ // handle this node
// Put this node on the path
bpath.push_front(make_pair(node_id, backward));
// Load the node
Node& node = nq[bpath].first;
get_node(node_id, node);
nq[bpath].second = backward;
Mapping mapping;
bool backward_on_path;
if (get_node_path(node_id, path_id, path_pos, backward_on_path, mapping) > 0) {
// This node is on the target path.
// Report if we were looking at it backward relative to the path
relative_orientation = (backward != backward_on_path);
// Return a search path of just this node, and its ID. We inclue it
// in the search path (due to being the end of the search) even
// though it wouldn't normally be included (due to being the start
// of the search).
return make_pair(bpath, bpath.front());
}
// Otherwise, say we're at this node after taking the empty path.
Node n = node;
nq.clear();
nq[nullpath] = make_pair(n, backward);
}
// BFS back
int steps_back = 0;
while (steps_back++ < max_steps) {
// We're going to extend all the paths and populate this.
map<list<pair<int64_t, bool>>, pair<Node, bool>> cq;
for (auto& n : nq) {