-
Notifications
You must be signed in to change notification settings - Fork 4
/
RLZ.cpp
1626 lines (1424 loc) · 44.5 KB
/
RLZ.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
/* RLZ compress
* Copyright (C) 2011 Shanika Kuruppu
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* RLZ - Relative Lempel Ziv
* Implements the RLZ compression algorithm.
* Authors: Shanika Kuruppu (kuruppu@csse.unimelb.edu.au)
* Simon J. Puglisi (simon.puglisi@rmit.edu.au)
*/
#include <divsufsort64.h>
#include <BitSequenceSDArray.h>
#include <BitSequenceRRR.h>
#include "RLZ.h"
#include "alphabet.h"
using namespace std;
using namespace cds_utils;
using namespace cds_static;
RLZCompress::RLZCompress(char **filenames, uint64_t numfiles,
char encoding, bool isshort, bool isliss)
{
this->filenames = filenames;
this->numfiles = numfiles;
this->encoding = encoding;
this->isshort = isshort;
this->isliss = isliss;
this->idxname = NULL;
this->displayonly = false;
read_refseq_and_sa();
}
RLZCompress::RLZCompress(char **filenames, uint64_t numfiles,
char *idxname, bool displayonly)
{
this->filenames = filenames;
this->numfiles = numfiles;
this->encoding = 'i';
this->isshort = false;
this->isliss = false;
this->idxname = idxname;
this->displayonly = displayonly;
read_refseq_and_construct_sa();
}
void RLZCompress::read_refseq_and_construct_sa()
{
uint64_t i;
initialise_nucl_converters();
// Read reference sequence into memory since its needed by
// suffix arrey constructor
char *sequence = NULL;
uint64_t seqlen;
if (loadText(filenames[0], &sequence, &seqlen))
{
cerr << "Couldn't read reference sequence.\n";
exit(1);
}
// loadText places an extra byte at the end
refseqlen = seqlen-1;
// Read the reference sequence
refseq = new Array(refseqlen+1, ((unsigned)1<<BITSPERBASE)-1);
store_sequence(sequence, filenames[0], refseq, refseqlen);
// Construct suffix array
uint64_t *sufarray = new uint64_t[refseqlen+1];
if (divsufsort64((sauchar_t*)sequence, (saidx64_t*)sufarray,
refseqlen+1) < 0)
{
cerr << "Error in constructing suffix array.\n";
exit(1);
}
sa = new Array(refseqlen+1, refseqlen);
for (i=0; i<=refseqlen; i++)
{
sa->setField(i, sufarray[i]);
}
// Calculate the log of the reference sequence length
i = floor(log2(refseqlen));
logrefseqlen = ((unsigned)(1<<i) != refseqlen) ? i+1 : i;
//delete [] sequence;
delete [] sufarray;
}
void RLZCompress::read_refseq_and_sa()
{
initialise_nucl_converters();
uint64_t i;
char safilename[1024];
sprintf(safilename, "%s.sa", filenames[0]);
ifstream infile;
infile.open(safilename, ifstream::in);
// Need to construct suffix array
if (!infile.good())
{
infile.close();
// Read reference sequence into memory since its needed by
// suffix arrey constructor
char *sequence = NULL;
if (loadText(filenames[0], &sequence, &refseqlen))
{
cerr << "Couldn't read reference sequence.\n";
exit(1);
}
// loadText places an extra byte at the end
refseqlen--;
// Read the reference sequence
refseq = new Array(refseqlen+1, ((unsigned)1<<BITSPERBASE)-1);
store_sequence(sequence, filenames[0], refseq, refseqlen);
// Construct suffix array
uint64_t *sufarray = new uint64_t[refseqlen+1];
if (divsufsort64((sauchar_t*)sequence, (saidx64_t*)sufarray,
refseqlen+1) < 0)
{
cerr << "Error in constructing suffix array.\n";
exit(1);
}
sa = new Array(refseqlen+1, refseqlen);
for (i=0; i<=refseqlen; i++)
{
sa->setField(i, sufarray[i]);
}
// Write out suffix array to disk for later use
ofstream outfile(safilename);
sa->save(outfile);
outfile.close();
delete [] sequence;
delete [] sufarray;
}
else
{
// Load suffix array from saved suffix array file
sa = new Array(infile);
infile.close();
// Open reference sequence file
infile.open(filenames[0], ifstream::in);
if (!infile.good())
{
cerr << "Couldn't open file " << filenames[0] << ".\n";
exit(1);
}
// Get the reference sequence length
infile.seekg(0, ios::end);
refseqlen = infile.tellg();
infile.seekg(0, ios::beg);
// Read the reference sequence
refseq = new Array(refseqlen+1, ((unsigned)1<<BITSPERBASE)-1);
store_sequence(infile, filenames[0], refseq, refseqlen);
infile.close();
}
// Calculate the log of the reference sequence length
i = floor(log2(refseqlen));
logrefseqlen = ((unsigned)(1<<i) != refseqlen) ? i+1 : i;
}
RLZCompress::~RLZCompress()
{
delete refseq;
if (sa != NULL) delete sa;
}
void RLZ::store_sequence(char *sequence, char *filename,
Array *dest, uint64_t length)
{
uint64_t i;
unsigned int v;
for (i=0; i<length; i++)
{
v = nucl_to_int[(unsigned int)sequence[i]];
// Valid nucleotide
if (v > 0)
{
dest->setField(i, v);
}
else
{
cerr << "Invalid symbol " << sequence[i] << " at position " << i;
cerr << " of sequence in " << filename << ".\n";
exit(1);
}
}
dest->setField(i, 0);
return;
}
void RLZ::store_sequence(ifstream &infile, char *filename,
Array *dest, uint64_t length)
{
uint64_t i;
int c;
unsigned int v;
for (i=0; i<length; i++)
{
c = infile.get();
v = nucl_to_int[c];
// Valid nucleotide
if (v > 0)
{
dest->setField(i, v);
}
else
{
cerr << "Invalid symbol " << c << " at position " << i;
cerr << " of sequence in " << filename << ".\n";
exit(1);
}
}
dest->setField(i, 0);
return;
}
void RLZCompress::compress()
{
uint64_t i;
char outfilename[1024];
ifstream infile;
ofstream outfile;
FactorWriter *facwriter = NULL;
if (encoding == 'i')
{
// Open output file to write compressed sequence to
outfile.open(idxname, ofstream::out);
if (!outfile.good())
{
cerr << "Couldn't open file " << outfilename << ".\n";
exit(1);
}
facwriter = new FactorWriterIndex(outfile, refseq, sa,
refseqlen, logrefseqlen,
displayonly);
}
for (i=1; i<numfiles; i++)
{
// Open sequence file to be compressed
infile.open(filenames[i], ifstream::in);
if (!infile.good())
{
cerr << "Couldn't open file " << filenames[i] << ".\n";
exit(1);
}
// Intialise the factor writer
if (encoding != 'i')
{
// Open output file to write compressed sequence to
sprintf(outfilename, "%s.fac", filenames[i]);
outfile.open(outfilename, ofstream::out);
if (!outfile.good())
{
cerr << "Couldn't open file " << outfilename << ".\n";
exit(1);
}
facwriter = new FactorWriter(outfile, encoding, isshort,
isliss, this->refseq,
refseqlen, logrefseqlen);
}
relative_LZ_factorise(infile, filenames[i], *facwriter);
if (encoding != 'i')
{
delete facwriter;
outfile.close();
}
infile.close();
}
if (encoding == 'i')
{
((FactorWriterIndex*)facwriter)->write_index();
delete facwriter;
outfile.close();
}
}
void RLZCompress::relative_LZ_factorise(ifstream& infile,
char *filename,
FactorWriter &facwriter)
{
int c;
uint64_t i, len;
uint64_t pl, pr, cl, cr;
bool runofns;
i = 0;
runofns = false;
pl = 0; pr = refseqlen; len = 0;
while (1)
{
// EOF reached
if ((c = infile.get()) == EOF) break;
// Invalid symbol in file
if (nucl_to_int[c] == 0)
{
cerr << "Invalid symbol " << c << " at position " << i;
cerr << " of sequence in " << filename << ".\n";
exit(1);
}
// Part of a run of Ns
if (c == 'n')
{
// A new run of Ns so print earlier factor and reset suffix
// array boundaries
if (!runofns && len > 0)
{
facwriter.write_factor(sa->getField(pl), len);
pl = 0; pr = refseqlen; len = 0;
}
runofns = true;
len++;
}
else
{
// A run of Ns just ended so print the factor
if (runofns)
{
facwriter.write_factor(refseqlen, len);
runofns = false;
len = 0;
}
// Try to extend the current match
sa_binary_search(pl, pr, nucl_to_int[c], len, &cl, &cr);
// Couldn't extend current match so print factor
if (cl == (uint64_t)(-1) || cr == (uint64_t)(-1))
{
facwriter.write_factor(sa->getField(pl), len);
infile.unget();
pl = 0; pr = refseqlen; len = 0;
}
// Set the suffix array boundaries to narrow the search for
// the next symbol
else
{
pl = cl; pr = cr; len++;
}
}
i++;
}
// Print last factor
if (len > 0)
{
if (runofns)
facwriter.write_factor(refseqlen, len);
else
facwriter.write_factor(sa->getField(pl), len);
}
// Call this to indicate the end of input to the factor writer
facwriter.end_of_sequence();
}
void RLZCompress::sa_binary_search(uint64_t pl, uint64_t pr, int c,
uint64_t offset, uint64_t *cl,
uint64_t *cr)
{
uint64_t low, high, mid;
int midval, midvalleft, midvalright;
// Binary search left
low = pl; high = pr;
while (low <= high && high != (uint64_t)-1)
{
mid = (low + high) >> 1;
midval = refseq->getField(sa->getField(mid)+offset);
// Move left boundary to the middle
if (midval < c)
low = mid + 1;
// Move right boundary to the middle
else if (midval > c)
high = mid - 1;
else
{
// Mid is at the left boundary
if(mid == pl)
{
*cl = mid;
break;
}
midvalleft = refseq->getField(sa->getField(mid-1)+offset);
// Discard mid and values to the right of mid
if(midvalleft == midval)
high = mid - 1;
// Left-most occurrence found
else
{
*cl = mid;
break;
}
}
}
// Key not found so return not found symbols
if (low > high || high == (uint64_t)-1)
{
*cl = (uint64_t)(-1);
*cr = (uint64_t)(-1);
return;
}
// Binary search right
low = *cl; high = pr;
while (low <= high && high != (uint64_t)-1)
{
mid = (low + high) >> 1;
midval = refseq->getField(sa->getField(mid)+offset);
// Move left bounary to the middle
if (midval < c)
low = mid + 1;
// Move right boundary to the middle
else if (midval > c)
high = mid - 1;
else
{
// Rightmost occurrence of key found
if(mid == pr)
{
*cr = mid;
break;
}
midvalright = refseq->getField(sa->getField(mid+1)+offset);
// Discard mid and the ones to the left of mid
if(midvalright == midval)
low = mid + 1;
// Rightmost occurrence of key found
else
{
*cr = mid;
break;
}
}
}
// Key not found so return not found symbols
if (low > high || high == (uint64_t)-1)
{
*cl = (uint64_t)(-1);
*cr = (uint64_t)(-1);
return;
}
return;
}
RLZDecompress::RLZDecompress(char **filenames, uint64_t numfiles)
{
this->filenames = filenames;
this->numfiles = numfiles;
initialise_nucl_converters();
// Open reference sequence file
ifstream infile(filenames[0], ifstream::in);
if (!infile.good())
{
cerr << "Couldn't open file " << filenames[0] << ".\n";
exit(1);
}
// Get the reference sequence length
infile.seekg(0, ios::end);
refseqlen = infile.tellg();
infile.seekg(0, ios::beg);
// Read the reference sequence
refseq = new Array(refseqlen+1, ((unsigned)1<<BITSPERBASE)-1);
store_sequence(infile, filenames[0], refseq, refseqlen);
infile.close();
// Calculate the log of the reference sequence length
uint64_t i = floor(log2(refseqlen));
logrefseqlen = ((unsigned)(1<<i) != refseqlen) ? i+1 : i;
}
RLZDecompress::~RLZDecompress()
{
delete refseq;
}
void RLZDecompress::decompress()
{
uint64_t i;
char infilename[1024], outfilename[1024];
ifstream infile;
ofstream outfile;
for (i=1; i<numfiles; i++)
{
// Open sequence file to be decompressed
sprintf(infilename, "%s.fac", filenames[i]);
infile.open(infilename, ifstream::in);
if (!infile.good())
{
cerr << "Couldn't open file " << infilename << ".\n";
exit(1);
}
// Open output file to write decompressed sequence to
sprintf(outfilename, "%s.dec", filenames[i]);
outfile.open(outfilename, ofstream::out);
if (!outfile.good())
{
cerr << "Couldn't open file " << outfilename << ".\n";
exit(1);
}
// Intialise the factor reader
FactorReader *facreader = new FactorReader(infile, logrefseqlen);
relative_LZ_defactorise(*facreader, infilename, outfile);
delete facreader;
infile.close();
outfile.close();
}
}
void RLZDecompress::relative_LZ_defactorise(FactorReader& facreader,
char *filename,
ofstream& outfile)
{
uint64_t pos, len, i;
vector<char> substr;
try
{
// Read factors until EOF
while (facreader.read_factor(&pos, &len, substr))
{
// A short factor
if (substr.size() == len)
{
for (i=0; i<len; i++)
{
outfile << substr.at(i);
}
}
// Run length encoded Ns
else if (pos == refseqlen)
{
for (i=0; i<len; i++)
{
outfile << 'n';
}
}
// Standard factor
else
{
for (i=pos; i<pos+len; i++)
{
outfile << (char)int_to_nucl[refseq->getField(i)];
}
}
substr.clear();
}
}
catch (exception e)
{
cerr << e.what() << endl;
cerr << "Could not read from file " << filename << ".\n";
exit(1);
}
}
FactorWriter::FactorWriter() :
facwriter(NULL) {}
FactorWriter::FactorWriter(ofstream& outfile, char encoding,
bool isshort, bool isliss, Array *refseq,
uint64_t refseqlen, uint64_t logrefseqlen)
{
unsigned char encbyte = 0;
this->isshort = isshort;
this->isliss = isliss;
// The encoding format is tbsl---- where t=text, b=binary,
// s=shortfac encoding, l=liss encoding
if (isshort)
encbyte |= (unsigned)1<<5;
if (isliss)
encbyte |= (unsigned)1<<4;
if (encoding == 'b')
{
encbyte |= (unsigned)1<<6;
// Output the type of encoding
outfile << encbyte;
facwriter = new FactorWriterBinary(outfile, isshort, isliss,
refseq, refseqlen,
logrefseqlen);
}
else if (encoding == 't')
{
encbyte |= (unsigned)1<<7;
// Output the type of encoding
outfile << encbyte << endl;
facwriter = new FactorWriterText(outfile, isshort, isliss,
refseq, refseqlen,
logrefseqlen);
}
else
{
cerr << "Unknown encoding type.\n";
exit(1);
}
}
void FactorWriter::write_factor(uint64_t pos, uint64_t len)
{
// If LISS encoding then can't output factors yet so just store them
if (isliss)
{
positions.push_back(pos);
lengths.push_back(len);
return;
}
facwriter->write_factor(pos, len);
}
void FactorWriter::end_of_sequence()
{
vector<uint64_t> liss;
uint64_t i, j;
uint64_t prevpos, cumlen;
if (isliss)
{
find_LISS(positions, liss);
i = j = 0;
// Write standard factors until the first LISS factor
while (j < liss[i])
{
facwriter->write_factor(positions[j], lengths[j], false);
j++;
}
facwriter->write_factor(positions[j], lengths[j], true);
// Initialise the position and length accumulators to keep track
// of the last LISS factor encountered
prevpos = positions[j];
cumlen = lengths[j];
i++; j++;
while (i < liss.size())
{
// Write standard factors
while (j < liss[i])
{
facwriter->write_factor(positions[j], lengths[j],
false);
cumlen += lengths[j];
j++;
}
// Write LISS factor
facwriter->write_factor(positions[j]-(prevpos+cumlen),
lengths[j], true);
prevpos = positions[j];
cumlen = lengths[j];
i++; j++;
}
// Write the remaining standard factors
while (j<positions.size())
{
facwriter->write_factor(positions[j], lengths[j], false);
j++;
}
}
facwriter->end_of_sequence();
}
FactorWriter::~FactorWriter()
{
delete facwriter;
}
// Finds longest strictly increasing subsequence (LISS).
// O(n log k) algorithm, k is the length of the LISS.
void FactorWriter::find_LISS(vector<uint64_t>& a, vector<uint64_t>& b)
{
uint64_t asize = a.size();
vector<uint64_t> p(asize);
uint64_t u, v, c;
uint64_t i;
i = 0;
// Ignore factors that represent Ns
while (a[i] == refseqlen)
i++;
// Add the first factor
b.push_back(i);
for (; i < asize; i++)
{
// Ignore factors that represent Ns
if (a[i] == refseqlen)
continue;
// If the last inserted index in b has a position less than the
// position at the current index then no need to binary search so
// just store the values
if (a[b.back()] < a[i])
{
p[i] = b.back();
b.push_back(i);
continue;
}
// Binary search till you find the place where the current
// position will fit in b
for (u = 0, v = b.size()-1; u < v;)
{
c = (u + v) / 2;
if (a[b[c]] < a[i])
u=c+1;
else
v=c;
}
// Found a spot to insert the current position
if (a[i] < a[b[u]])
{
if (u > 0)
p[i] = b[u-1];
b[u] = i;
}
}
// Back-track to figure out which indices are part of the LISS
for (u = b.size(), v = b.back(); u>0; v = p[v], u--)
{
b[u-1] = v;
}
}
FactorWriterText::FactorWriterText(ofstream& outfile, bool isshort,
bool isliss, Array *refseq, uint64_t
refseqlen, uint64_t logrefseqlen) :
outfile(outfile)
{
this->refseq = refseq;
this->refseqlen = refseqlen;
this->logrefseqlen = logrefseqlen;
this->isshort = isshort;
this->isliss = isliss;
if (isshort)
{
// 2*len+len/GOLOMBDIVSHORT+(LOG2GOLOMBDIVSHORT+1) <
// logrefseqlen+len/GOLOMBDIV+(LOG2GOLOMBDIV+1)
SHORTFACTHRESH = (64.0/135)*(logrefseqlen+3);
}
// Initialise a flag needed to start LISS encoding
if (isliss)
{
firstliss = true;
}
}
void FactorWriterText::write_factor(uint64_t pos, uint64_t len)
{
uint64_t i;
// 2*len+len/8+4 < logrefseqlen+len/64+7
if (isshort && pos!=refseqlen && len <= SHORTFACTHRESH)
{
for (i=pos; i<pos+len; i++)
{
// Don't short factor encode if there are 'n's in the factor
if (int_to_nucl[refseq->getField(i)] == 'n')
{
outfile << pos << ' ' << len << endl;
return;
}
}
// Output a short factor, substring followed by the length
for (i=pos; i<pos+len; i++)
{
outfile << int_to_nucl[refseq->getField(i)];
}
outfile << ' ' << len << endl;
return;
}
// Just a standard factor
outfile << pos << ' ' << len << endl;
}
void FactorWriterText::write_factor(uint64_t pos, uint64_t len,
bool lissfac)
{
if (lissfac)
{
// Write the first LISS factor as a standard factor
if (firstliss)
{
outfile << "* " << pos << ' ' << len << endl;
firstliss = false;
return;
}
// Output the diff and the length of the factor
outfile << "* " << (int64_t)pos << ' ' << len << endl;
return;
}
// Standard factor
write_factor(pos, len);
}
void FactorWriterText::end_of_sequence()
{
}
FactorWriterBinary::FactorWriterBinary(ofstream& outfile, bool isshort,
bool isliss, Array *refseq,
uint64_t refseqlen,
uint64_t logrefseqlen)
{
bwriter = new BitWriter(outfile);
gcoder = new GolombCoder(*bwriter, GOLOMBDIV);
gcodershort = new GolombCoder(*bwriter, GOLOMBDIVSHORT);
this->isshort = isshort;
this->isliss = isliss;
this->refseq = refseq;
this->refseqlen = refseqlen;
this->logrefseqlen = logrefseqlen;
// Output the Golomb coding parameter
bwriter->int_to_binary(GOLOMBDIV, 8);
// Ouptut the Golomb coding parameter for short ints
if (isshort)
{
bwriter->int_to_binary(GOLOMBDIVSHORT, 8);
// 2*len+len/GOLOMBDIVSHORT+(LOG2GOLOMBDIVSHORT+1) <
// logrefseqlen+len/GOLOMBDIV+(LOG2GOLOMBDIV+1)
SHORTFACTHRESH = (64.0/135)*(logrefseqlen+3);
}
// Initialise a flag needed to start LISS encoding
if (isliss)
{
firstliss = true;
}
}
FactorWriterBinary::~FactorWriterBinary()
{
delete bwriter;
delete gcoder;
delete gcodershort;
}
void FactorWriterBinary::write_factor(uint64_t pos, uint64_t len)
{
uint64_t i;
// 2*len+len/8+4 < logrefseqlen+len/64+7
if (isshort && pos!=refseqlen && len <= SHORTFACTHRESH)
{
for (i=pos; i<pos+len; i++)
{
// Don't short factor encode if there are 'n's in the factor
if (int_to_nucl[refseq->getField(i)] == 'n')
{
// Indicate this is not a short factor
bwriter->write_bit(1);
// Encode pos and len pair
bwriter->int_to_binary(pos, logrefseqlen);
gcoder->golomb_encode(len);
return;
}
}
// Valid short factor so first output a 0 bit to indicate this
bwriter->write_bit(0);
// Encode length followed by 2bpb substring
gcodershort->golomb_encode(len);
for (i=pos; i<pos+len; i++)
{
bwriter->int_to_binary(int_to_2bpb[refseq->getField(i)], 2);
}
return;
}
// Not a short factor so output 1 bit to indicate this is not a
// short factor
if (isshort) bwriter->write_bit(1);
// Output factor as a standard factor
bwriter->int_to_binary(pos, logrefseqlen);
gcoder->golomb_encode(len);
}
void FactorWriterBinary::write_factor(uint64_t pos, uint64_t len,
bool lissfac)
{
if (lissfac)
{
// A bit to indicate that it's an LISS factor
bwriter->write_bit(1);
// Write the first LISS factor as a standard factor
if (firstliss)
{
if (isshort) bwriter->write_bit(1);
// Output factor as a standard factor
bwriter->int_to_binary(pos, logrefseqlen);
gcoder->golomb_encode(len);
firstliss = false;
return;
}
// The next LISS position can be inferred from the previous LISS
// position and the cumulative length of factors inbetween
if ((int64_t)pos == 0)
{
bwriter->write_bit(0);
// Only need the length
gcoder->golomb_encode(len);
}
// The next LISS position is to the left of the inferred LISS
// position
else if ((int64_t)pos < 0)
{
// 1 bit to say position is a diff
bwriter->write_bit(1);
// 0 bit to say the diff is negative
bwriter->write_bit(0);
gcoder->golomb_encode(abs((int64_t)pos));
gcoder->golomb_encode(len);