-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathkff_io.cpp
More file actions
1702 lines (1390 loc) · 46.1 KB
/
kff_io.cpp
File metadata and controls
1702 lines (1390 loc) · 46.1 KB
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 <cstdint>
#include <iostream>
#include <cassert>
#include <cstring>
#include <sstream>
#include <cstdint>
#include <math.h>
#include <map>
#include <vector>
#include "kff_io.hpp"
using namespace std;
// Utils
template<typename T>
void store_big_endian(uint8_t * buff, size_t size, const T& data) {
for (int b = size - 1; b >= 0; --b) {
*buff++ = data >> (8 * b);
}
}
template<typename T>
void load_big_endian(uint8_t * buff, size_t size, T& data) {
data = 0;
for (uint b=0 ; b < size; b++) {
data <<= 8;
data |= buff[b];
}
}
uint64_t bytes_from_bit_array(uint64_t bits_per_elem, uint64_t nb_elem) {
if (bits_per_elem == 0 or nb_elem == 0)
return 0;
else
return ((bits_per_elem * nb_elem - 1) / 8) + 1;
}
static void leftshift8(uint8_t * bitarray, size_t length, size_t bitshift);
static void rightshift8(uint8_t * bitarray, size_t length, size_t bitshift);
static uint8_t fusion8(uint8_t left_bits, uint8_t right_bits, size_t merge_index);
// ----- Open / Close functions -----
Kff_file::Kff_file(const string filename, const string mode) {
// Variable init
this->filename = filename;
this->is_writer = false;
this->is_reader = false;
this->writing_started = false;
this->next_free = 0;
this->buffer_size = 1 << 10; // 1 KB
// this->buffer_size = 1 << 4;
this->max_buffer_size = 1 << 20; // 1 MB
// this->max_buffer_size = 1 << 6;
this->file_buffer = new uint8_t[this->buffer_size];
this->file_size = 0;
this->delete_on_destruction = false;
this->open(mode);
}
void Kff_file::open(string mode) {
this->writing_started = false;
this->current_position = 0;
// Determine the mode and open the file
if (mode[0] == 'w') {
this->is_writer = true;
this->file_size = 0;
this->next_free = 0;
} else if (mode[0] == 'r') {
this->is_reader = true;
// If no info on the file
if (this->file_size == 0 and this->next_free == 0) {
// Open the fp
this->fs.open(this->filename, fstream::binary | fstream::in);
if (this->fs.fail()) {
std::string msg = "Cannot open file " + this->filename;
throw std::runtime_error(msg);
}
// Compute the file length
long position = this->fs.tellp();
this->fs.seekg(0, this->fs.end);
this->file_size = (long)(this->fs.tellp()) - position;
// Go back to the beginning
this->fs.seekg(0, this->fs.beg);
}
} else {
cerr << "Unsupported mode " << mode << endl;
exit(1);
}
this->tmp_closed = false;
this->header_over = false;
this->indexed = false;
this->footer = nullptr;
this->footer_discovery_ended = true;
// Write the signature and the version at the beginning of the file
if (this->is_writer) {
uint8_t default_encoding = 0b00011110;
// Signature
uint8_t buff[] = { 'K', 'F', 'F',
KFF_VERSION_MAJOR, KFF_VERSION_MINOR,
default_encoding,
0 /*uniqueness*/, 0 /*canonicity*/
};
this->write(buff, 8);
this->indexed = true;
this->end_position = 0;
}
// Read the header
else if (this->is_reader) {
// Header integrity marker
uint8_t buff[4];
this->read(buff, 3);
if (buff[0] != 'K' or buff[1] != 'F' or buff[2] != 'F') {
cerr << "Absent KFF signature at the beginning of the file." << endl;
cerr << "Please check that the file is not corrupted" << endl;
throw "Absent signature at the beginning";
}
// Version reading
this->read(&this->major_version, 1);
this->read(&this->minor_version, 1);
if (KFF_VERSION_MAJOR < this->major_version or (KFF_VERSION_MAJOR == this->major_version and KFF_VERSION_MINOR < this->minor_version)) {
cerr << "The software version " << (uint)KFF_VERSION_MAJOR << "." << (uint)KFF_VERSION_MINOR << " can't read files writen in version " << (uint)this->major_version << "." << (uint)this->minor_version << endl;
throw "Unexpected version number";
}
// Encoding load
this->read_encoding();
// Read global flags
uint8_t flag;
this->read(&flag, 1);
this->uniqueness = flag != 0;
this->read(&flag, 1);
this->canonicity = flag != 0;
// Read metadata size
this->read(buff, 4);
load_big_endian(buff, 4, this->metadata_size);
// Footer integrity marker
unsigned long saved_position = this->tellp();
this->jump_to(3, true);
this->end_position = this->tellp();
this->read(buff, 3);
this->jump_to(saved_position);
if (buff[0] != 'K' or buff[1] != 'F' or buff[2] != 'F') {
cerr << "Absent KFF signature at the end of the file." << endl;
cerr << "Please check that the file is not corrupted" << endl;
throw "Absent signature at the end";
}
// Back to the start
this->footer_discovery_ended = false;
// Discover footer
this->footer_discovery();
this->index_discovery();
}
}
void Kff_file::close(bool write_buffer) {
if (this->is_writer) {
// Write the index
if (this->indexed)
this->write_footer();
// Write the signature
char signature[] = {'K', 'F', 'F'};
this->write((uint8_t *)signature, 3);
// Write the end of the file
if (write_buffer) {
// The file was never opened
if (not this->writing_started) {
this->writing_started = true;
this->fs.open(this->filename, fstream::binary | fstream::out);
} else if (this->tmp_closed) {
this->reopen();
}
// Write the buffer
this->fs.write((char *)this->file_buffer, this->next_free);
if (this->fs.fail()) {
cerr << "Filesystem problem during buffer disk saving" << endl;
exit(1);
}
this->file_size += this->next_free;
this->next_free = 0;
} else {
this->delete_on_destruction = true;
}
// cout << "delete_on_destruction " << delete_on_destruction << endl;
// cout << this->filename << endl;
if (this->fs.is_open())
this->fs.close();
}
else if (this->is_reader) {
}
this->tmp_closed = false;
this->is_writer = false;
this->is_reader = false;
}
Kff_file::~Kff_file() {
this->close();
delete[] this->file_buffer;
if (this->delete_on_destruction and this->file_size > 0) {
remove(this->filename.c_str());
}
if (this->footer != nullptr)
delete this->footer;
for (Section_Index * si : this->index)
delete si;
}
void Kff_file::set_indexation(bool indexed) {
if (this->is_writer)
this->indexed = indexed;
}
void Kff_file::register_position(char section_type) {
if (this->is_writer and this->indexed) {
this->section_positions[this->tellp()] = section_type;
}
}
void Kff_file::complete_header() {
if (this->header_over)
return;
// If the metadata has not been read, jump over
if (this->is_reader) {
this->jump(this->metadata_size);
}
// If metadata has not been write, write a 0 byte one.
else if (this->is_writer) {
this->write_metadata(0, nullptr);
}
this->header_over = true;
}
void Kff_file::footer_discovery() {
long current_pos = this->tellp();
// Look at the footer
this->jump_to(23, true);
// Try to extract the footer size
stringstream ss;
char c = 'o';
for (uint i=0 ; i<11 ; i++) {
this->read((uint8_t *)&c, 1);
ss << c;
}
if (ss.str().compare("footer_size") != 0) {
return;
}
this->jump(1); // remove the '\0'
uint64_t size = 0;
uint8_t buff[8];
this->read(buff, 8);
load_big_endian(buff, 8, size);
// Jump to value section start
this->jump_to(size+3, true);
this->footer = new Section_GV(this);
this->footer->close();
this->footer_discovery_ended = true;
this->jump_to(current_pos);
}
void Kff_file::index_discovery() {
long current_pos = this->tellp();
bool header_over = this->header_over;
this->complete_header();
// Search in footer
if (this->footer != nullptr and this->footer->vars.find("first_index") != this->footer->vars.end()) {
this->indexed = true;
this->read_index((long)this->footer->vars["first_index"]);
}
// Search first section
if (not this->indexed) {
char type = this->fs.peek();
if (type == 'i') {
this->indexed = true;
this->read_index(this->tellp());
}
}
this->header_over = header_over;
this->index_discovery_ended = true;
this->jump_to(current_pos);
}
void Kff_file::read_index(long position) {
long init_pos = this->tellp();
while (position != 0) {
// Move to the beginning
this->jump_to(position);
// read the local index content
Section_Index * si = new Section_Index(this);
this->index.push_back(si);
si->close();
// Update index position to the next index section
if (si->next_index == 0)
position = 0;
else {
position = this->tellp() + si->next_index;
}
}
this->jump_to(init_pos);
}
void Kff_file::read(uint8_t * bytes, unsigned long size) {
if (not this->is_reader) {
cerr << "Cannot read a file in writing mode." << endl;
exit(1);
}
// Read in the file
if (this->current_position < this->file_size) {
// Read the end of the file and the beginning of the buffer
if (this->current_position + size > this->file_size) {
uint64_t fs_read_size = this->file_size - this->current_position;
this->read(bytes, fs_read_size);
this->read(bytes + fs_read_size, size - fs_read_size);
return;
}
// Read inside of the file
else {
// File not opened
if (not this->fs.is_open())
this->fs.open(this->filename, fstream::binary | fstream::in);
// long tp = this->fs.tellp();
this->fs.read((char *)bytes, size);
if (this->fs.fail()) {
// cout << tp << endl;
cerr << "Impossible to read the file " << this->filename << " on disk." << endl;
exit(1);
}
}
}
// Read in the buffer
else {
// Compute the buffer positions to read
uint64_t buffer_position = this->current_position - this->file_size;
if (buffer_position + size > this->next_free) {
string error = string("Read out of the file, Byte ") + to_string(this->file_size + this->next_free);
throw out_of_range(error);
exit(1);
}
memcpy(bytes, this->file_buffer + buffer_position, size);
}
this->current_position += size;
}
void Kff_file::write(const uint8_t * bytes, unsigned long size) {
if (not this->is_writer) {
if (this->is_reader)
cerr << "Cannot write a file in reading mode." << endl;
else
cerr << "Cannot write a closed file" << endl;
exit(1);
}
unsigned long buff_space = this->buffer_size - this->next_free;
// Resize buffer
while (buff_space < size and this->buffer_size < this->max_buffer_size) {
// Enlarge the buffer
this->buffer_size *= 2;
uint8_t * next_buffer = new uint8_t[this->buffer_size];
// Copy the previous values
memcpy(next_buffer, this->file_buffer, this->next_free);
buff_space = this->buffer_size - this->next_free;
// Fill the empty part with 0s
memset(next_buffer + this->next_free, 0, buff_space);
// remove the previous space
delete[] this->file_buffer;
this->file_buffer = next_buffer;
}
// fill the buffer
if (buff_space >= size) {
memcpy(this->file_buffer + this->next_free, bytes, size);
this->next_free += size;
}
// Not enought space, write the file
else {
// Open the file if needed
if (not this->writing_started) {
this->fs.open(this->filename, fstream::binary | fstream::out);
this->writing_started = true;
} else if (this->tmp_closed) {
this->reopen();
}
this->fs.write((char*)this->file_buffer, this->next_free);
this->fs.write((char*)bytes, size);
this->file_size += this->next_free + size;
this->next_free = 0;
if (this->fs.fail()) {
cerr << "File system error while writing " << this->filename << endl;
exit(1);
}
}
this->current_position += size;
}
void Kff_file::write_at(const uint8_t * bytes, unsigned long size, unsigned long position) {
if (not this->is_writer) {
if (this->is_reader)
cerr << "Cannot write a file in reading mode." << endl;
else
cerr << "Cannot write a closed file" << endl;
exit(1);
}
if (position > this->file_size + this->next_free) {
cerr << "Cannot write after the last byte of the file." << endl;
exit(1);
}
// Write the file on disk
if (position < this->file_size) {
// Only in file
if (position + size <= this->file_size) {
if (this->tmp_closed) {
this->reopen();
}
this->fs.seekp(position);
this->fs.write((char*)bytes, size);
if (this->fs.fail()) {
cerr << "File system error while writing " << this->filename << " at position " << position << endl;
exit(1);
}
this->fs.seekp(this->file_size);
}
// On both file and buffer
else {
unsigned long in_file_size = this->file_size - position;
// Write the file part
this->write_at(bytes, in_file_size, position);
// Write the buffer part
this->write_at(bytes + in_file_size, size - in_file_size, position + in_file_size);
}
}
// Write the buffer in RAM
else {
unsigned long corrected_position = position - this->file_size;
// Write in the current buffer space
if (corrected_position + size <= this->next_free) {
memcpy(this->file_buffer + corrected_position, bytes, size);
}
// Spillover the buffer
else {
this->next_free = corrected_position;
this->write(bytes, size);
}
}
}
unsigned long Kff_file::tellp() {
return this->current_position;
}
void Kff_file::jump(long size) {
// cout << "Jump " << this->current_position << " " << size << " / " << this->file_size << " " << this->next_free << endl;
this->jump_to(this->current_position + size);
}
void Kff_file::jump_to(unsigned long position, bool from_end) {
if (this->file_size + this->next_free < position) {
cerr << "Jump out of the file." << endl;
exit(1);
}
// Determine absolute position
if (from_end) {
position = this->file_size + this->next_free - position;
}
// cout << "position " << position << endl;
// Jump into the written file
if (position < this->file_size) {
this->fs.seekp(position);
}
// Jump into the buffer
else /*if (this->current_position < this->file_size)*/ {
this->fs.seekg(0, this->fs.end);
}
this->current_position = position;
}
void Kff_file::tmp_close() {
if (this->is_writer and this->fs.is_open()) {
this->fs.close();
this->fs.clear();
this->tmp_closed = true;
}
}
void Kff_file::reopen() {
if (this->tmp_closed) {
auto streammode = fstream::binary | fstream::out | fstream::in | fstream::ate;
// Open the file
this->fs.open(this->filename, streammode);
this->tmp_closed = false;
}
}
void Kff_file::write_footer() {
Section_Index si(this);
// Compute end position
long position = si.beginning + 17 + 9 * this->section_positions.size();
// Add the values
for (auto & it : this->section_positions) {
si.register_section(it.second, it.first - position);
}
si.close();
// Write a value section to register everything
Section_GV sgv(this);
sgv.write_var("first_index", si.beginning);
sgv.write_var("footer_size", 9 + 2 * (12 + 8));
sgv.close();
}
// ----- Header functions -----
void Kff_file::write_encoding(uint8_t a, uint8_t c, uint8_t g, uint8_t t) {
// Value masking
a &= 0b11; c &= 0b11; g &= 0b11; t &= 0b11;
// Verification of the differences.
assert(a != c); assert(a != g); assert(a != t);
assert(c != g); assert(g != t);
assert(g != t);
// set values
this->encoding[0] = a;
this->encoding[1] = c;
this->encoding[2] = g;
this->encoding[3] = t;
// Write to file
uint8_t code = (a << 6) | (c << 4) | (g << 2) | t;
this->write_at(&code, 1, 5);
}
void Kff_file::set_uniqueness(bool uniqueness) {
uint8_t bit_uniq = uniqueness ? 1 : 0;
this->write_at(&bit_uniq, 1, 6);
}
void Kff_file::set_canonicity(bool canonicity) {
uint8_t bit_canon = canonicity ? 1 : 0;
this->write_at(&bit_canon, 1, 7);
}
void Kff_file::write_encoding(uint8_t * encoding) {
this->write_encoding(encoding[0], encoding[1], encoding[2], encoding[3]);
}
void Kff_file::read_encoding() {
uint8_t code, a, c, g, t;
// Get code values
this->read(&code, 1);
// Split each nucleotide encoding
this->encoding[0] = a = (code >> 6) & 0b11;
this->encoding[1] = c = (code >> 4) & 0b11;
this->encoding[2] = g = (code >> 2) & 0b11;
this->encoding[3] = t = code & 0b11;
// Verification of the encoding
if (a == c or a == g or a == t or c == g or c == t or g == t) {
throw "Wrong encoding. The 4 2-bits values must be different.";
}
}
void Kff_file::write_metadata(uint32_t size, const uint8_t * data) {
if (this->header_over) {
cerr << "The metadata have to be written prior to other content." << endl;
exit(1);
}
uint8_t buff[4];
store_big_endian(buff, 4, size);
this->write(buff, 4);
this->write(data, size);
this->header_over = true;
}
void Kff_file::read_metadata(uint8_t * data) {
this->read(data, this->metadata_size);
this->header_over = true;
}
bool Kff_file::jump_next_section() {
if (not is_reader)
return false;
char section_type = read_section_type();
if (this->current_position == this->file_size + this->next_free)
return false;
if (section_type == 'r' or section_type == 'm') {
Block_section_reader * section = Block_section_reader::construct_section(this);
section->jump_section();
delete section;
return true;
}
return false;
}
// ----- Sections -----
char Kff_file::read_section_type() {
// Verify that header has been read.
if (not this->header_over) {
this->complete_header();
}
if (this->current_position < this->file_size) {
return this->fs.peek();
}
else {
return (char)this->file_buffer[this->current_position - this->file_size];
}
}
Section::Section(Kff_file * file) {
this->file = file;
if (not file->header_over and file->footer_discovery_ended) {
file->complete_header();
}
this->beginning = file->tellp();
}
void Section::close() {
this->file = nullptr;
}
Section * SectionBuilder::build(Kff_file * file) {
char type = file->read_section_type();
switch (type) {
case 'i':
return new Section_Index(file);
case 'v':
return new Section_GV(file);
case 'r':
return new Section_Raw(file);
case 'm':
return new Section_Minimizer(file);
default:
cerr << "Unknown section " << type << "(" << (uint)type << ")" << endl;
throw std::runtime_error("Unknown section type " + std::string(1, type));
}
}
// ----- Global variables sections -----
Section_GV::Section_GV(Kff_file * file) : Section(file) {
this->nb_vars = 0;
this->file->global_vars.clear();
if (this->file->is_reader) {
this->read_section();
}
if (file->is_writer) {
if (file->indexed)
file->register_position('v');
char type = 'v';
this->file->write((uint8_t *)&type, 1);
}
}
void Section_GV::write_var(const string & var_name, uint64_t value) {
this->nb_vars += 1;
this->vars[var_name] = value;
this->file->global_vars[var_name] = value;
}
void Section_GV::read_section() {
char type = '\0';
this->file->read((uint8_t *)&type, 1);
if (type != 'v')
throw "The section do not start with the 'v' char, you can't open a Global Variable section.";
uint8_t buff[8];
this->file->read(buff, 8);
load_big_endian(buff, 8, this->nb_vars);
for (uint64_t i=0 ; i<nb_vars ; i++) {
this->read_var();
}
}
void Section_GV::read_var() {
if (file->tellp() >= file->end_position)
throw "eof reached before the end of the variable section";
// Name reading
stringstream ss;
char c = 'o';
this->file->read((uint8_t *)&c, 1);
while (c != '\0') {
ss << c;
this->file->read((uint8_t *)&c, 1);
}
// Value reading
uint64_t value = 0;
uint8_t buff[8];
this->file->read(buff, 8);
load_big_endian(buff, 8, value);
// Saving
string name = ss.str();
this->vars[name] = value;
this->file->global_vars[name] = value;
}
void Section_GV::copy(Kff_file * file) {
// Remove empty variable sections
if (this->vars.size() == 0)
return;
// Open the copy
Section_GV sgv(file);
// Copy all the variables
for (const auto & it : this->vars) {
sgv.write_var(it.first, it.second);
}
// Clos the copy
sgv.close();
}
void Section_GV::close() {
if (file->is_writer) {
uint8_t buff[8];
// write the number of block values
store_big_endian(buff, 8, this->nb_vars);
this->file->write(buff, 8);
// Write the variables
for (std::map<std::string,uint64_t>::iterator var_tuple=this->vars.begin() ; var_tuple != this->vars.end() ; var_tuple++) {
const string & name = var_tuple->first;
this->file->write((const uint8_t *)name.c_str(), name.length()+1);
store_big_endian(buff, 8, var_tuple->second);
this->file->write(buff, 8);
}
}
Section::close();
}
Section_Index::Section_Index(Kff_file * file) : Section(file) {
char type;
uint8_t buff[8];
this->next_index = 0;
if (this->file->is_reader) {
this->file->read((uint8_t *)&type, 1);
if (type != 'i')
throw "The section do not start with the 'i' char, you can not open an Index section.";
uint64_t nb_vars;
this->file->read(buff, 8);
load_big_endian(buff, 8, nb_vars);
for (uint64_t i=0 ; i<nb_vars ; i++) {
int64_t idx = 0;
this->file->read((uint8_t *)&type, 1);
this->file->read(buff, 8);
load_big_endian(buff, 8, idx);
this->index[idx] = type;
}
if (nb_vars != this->index.size())
throw "index collision in i section";
this->file->read(buff, 8);
load_big_endian(buff, 8, this->next_index);
}
}
void Section_Index::register_section(char section_type, int64_t pos) {
this->index[pos] = section_type;
}
void Section_Index::set_next_index(int64_t index) {
this->next_index = index;
}
// void Section_Index::copy(Kff_file * file) {
// cerr << "You are trying to copy an index from a file to another. ";
// cerr << "As the positions can be different between the files, this operation is not allowed." << endl;
// exit(2);
// }
void Section_Index::close() {
if (this->file->is_writer) {
uint8_t buff[8];
// Section header
char type = 'i';
this->file->write((uint8_t *)&type, 1);
store_big_endian(buff, 8, this->index.size());
this->file->write(buff, 8);
// Write index
for (std::map<int64_t, char>::iterator it=this->index.begin(); it!=this->index.end(); ++it) {
// Section type
type = it->second;
this->file->write((uint8_t *)&type, 1);
// Section index
store_big_endian(buff, 8, it->first);
this->file->write(buff, 8);
}
store_big_endian(buff, 8, this->next_index);
this->file->write(buff, 8);
}
Section::close();
}
Block_section_reader * Block_section_reader::construct_section(Kff_file * file) {
// Very and complete if needed the header
file->complete_header();
char type = file->read_section_type();
if (type == 'r') {
return new Section_Raw(file);
} else if (type == 'm') {
return new Section_Minimizer(file);
} else
return nullptr;
}
// ----- Raw sequence section -----
Section_Raw::Section_Raw(Kff_file * file) : Section(file){
if (file->global_vars.find("k") == file->global_vars.end())
throw "Impossible to read the raw section due to missing k variable";
if(file->global_vars.find("max") == file->global_vars.end())
throw "Impossible to read the raw section due to missing max variable";
if(file->global_vars.find("data_size") == file->global_vars.end())
throw "Impossible to read the raw section due to missing data_size variable";
uint64_t k = file->global_vars["k"];
uint64_t max = file->global_vars["max"];
uint64_t data_size = file->global_vars["data_size"];
this->nb_blocks = 0;
this->k = k;
this->max = max;
this->data_size = data_size;
// Computes the number of bytes needed to store the number of kmers in each block
uint64_t nb_bits = static_cast<uint64_t>(ceil(log2(max)));
this->nb_kmers_bytes = static_cast<uint8_t>(bytes_from_bit_array(nb_bits, 1));
if (file->is_reader) {
this->read_section_header();
}
if (file->is_writer) {
if (file->indexed)
file->register_position('r');
char type = 'r';
this->file->write((uint8_t *)&type, 1);
this->file->write((uint8_t *)&this->nb_blocks, 8);
}
}
uint32_t Section_Raw::read_section_header() {
char type;
this->file->read((uint8_t *)&type, 1);
if (type != 'r')
throw "The section do not start with the 'r' char, you can't open a Raw sequence section.";
uint8_t buff[8];
this->file->read(buff, 8);
load_big_endian(buff, 8, this->nb_blocks);
this->remaining_blocks = this->nb_blocks;
return this->nb_blocks;
}
void Section_Raw::write_compacted_sequence(uint8_t* seq, uint64_t seq_size, uint8_t * data_array) {
uint8_t buff[8];
// 1 - Write nb kmers
uint64_t nb_kmers = seq_size - k + 1;
store_big_endian(buff, this->nb_kmers_bytes, nb_kmers);
this->file->write(buff, this->nb_kmers_bytes);
// 2 - Write sequence
uint64_t seq_bytes_needed = (seq_size + 3) / 4;
this->file->write(seq, seq_bytes_needed);
// 3 - Write data
uint64_t data_bytes_needed = data_size * nb_kmers;
this->file->write(data_array, data_bytes_needed);
this->nb_blocks += 1;
}
uint64_t Section_Raw::read_compacted_sequence(uint8_t* seq, uint8_t* data) {
uint8_t buff[8];
uint64_t nb_kmers_in_block = 1;
// 1 - Read the number of kmers in the sequence
if (nb_kmers_bytes != 0) {
file->read(buff, this->nb_kmers_bytes);
load_big_endian(buff, this->nb_kmers_bytes, nb_kmers_in_block);
}
// 2 - Read the sequence
size_t seq_size = nb_kmers_in_block + k - 1;
size_t seq_bytes_needed = (seq_size + 3) / 4;
this->file->read(seq, seq_bytes_needed);
// 3 - Read the data.
uint64_t data_bytes_used = data_size * nb_kmers_in_block;
this->file->read(data, data_bytes_used);
this->remaining_blocks -= 1;
return nb_kmers_in_block;
}
uint64_t Section_Raw::read_compacted_sequence(uint8_t* seq_data) {
uint8_t buff[8];
uint64_t nb_kmers_in_block = 1;
// 1 - Read the number of kmers in the sequence
if (nb_kmers_bytes != 0) {
file->read(buff, this->nb_kmers_bytes);
load_big_endian(buff, this->nb_kmers_bytes, nb_kmers_in_block);
}
// 2 - Read the sequence
size_t seq_size = nb_kmers_in_block + k - 1;
size_t seq_bytes_needed = (seq_size + 3) / 4;
uint64_t data_bytes_used = data_size * nb_kmers_in_block;
this->file->read(seq_data, seq_bytes_needed + data_bytes_used);
this->remaining_blocks -= 1;