forked from PacificBiosciences/DAZZ_DB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QV.c
1406 lines (1193 loc) · 37.7 KB
/
QV.c
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
/************************************************************************************\
* *
* Copyright (c) 2014, Dr. Eugene W. Myers (EWM). All rights reserved. *
* *
* Redistribution and use in source and binary forms, with or without modification, *
* are permitted provided that the following conditions are met: *
* *
* · Redistributions of source code must retain the above copyright notice, this *
* list of conditions and the following disclaimer. *
* *
* · Redistributions in binary form must reproduce the above copyright notice, this *
* list of conditions and the following disclaimer in the documentation and/or *
* other materials provided with the distribution. *
* *
* · The name of EWM may not be used to endorse or promote products derived from *
* this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY EWM ”AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, *
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND *
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EWM BE LIABLE *
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN *
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
* *
* For any issues regarding this software and its use, contact EWM at: *
* *
* Eugene W. Myers Jr. *
* Bautzner Str. 122e *
* 01099 Dresden *
* GERMANY *
* Email: [email protected] *
* *
\************************************************************************************/
/*******************************************************************************************
*
* Compressor/decompressor for .quiv files: customized Huffman codes for each stream based on
* the histogram of values occuring in a given file. The two low complexity streams
* (deletionQV and substitutionQV) use a Huffman coding of the run length of the prevelant
* character.
*
* Author: Gene Myers
* Date: Jan 18, 2014
* Modified: July 25, 2014
*
********************************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include "DB.h"
#undef DEBUG
#define MIN_BUFFER 1000
#define HUFF_CUTOFF 16 // This cannot be larger than 16 !
/*******************************************************************************************
*
* Endian flipping routines
*
********************************************************************************************/
static int LittleEndian; // Little-endian machine ?
// Referred by: Decode & Decode_Run
static int Flip; // Flip endian of all coded shorts and ints
// Referred by: Decode & Decode_Run & Read_Scheme
static void Set_Endian(int flip)
{ uint32 x = 3;
uint8 *b = (uint8 *) (&x);
Flip = flip;
LittleEndian = (b[0] == 3);
}
static void Flip_Long(void *w)
{ uint8 *v = (uint8 *) w;
uint8 x;
x = v[0];
v[0] = v[3];
v[3] = x;
x = v[1];
v[1] = v[2];
v[2] = x;
}
static void Flip_Short(void *w)
{ uint8 *v = (uint8 *) w;
uint8 x;
x = v[0];
v[0] = v[1];
v[1] = x;
}
/*******************************************************************************************
*
* Routines for computing a Huffman Encoding Scheme
*
********************************************************************************************/
typedef struct
{ int type; // 0 => normal, 1 => normal but has long codes, 2 => truncated
uint32 codebits[256]; // If type = 2, then code 255 is the special code for
int codelens[256]; // non-Huffman exceptions
int lookup[0x10000]; // Lookup table (just for decoding)
} HScheme;
typedef struct _HTree
{ struct _HTree *lft, *rgt;
uint64 count;
} HTree;
// Establish heap property from node s down (1 is root, siblings of n are 2n and 2n+1)
// assuming s is the only perturbation in the tree.
static void Reheap(int s, HTree **heap, int hsize)
{ int c, l, r;
HTree *hs, *hr, *hl;
c = s;
hs = heap[s];
while ((l = 2*c) <= hsize)
{ r = l+1;
hl = heap[l];
hr = heap[r];
if (r > hsize || hr->count > hl->count)
{ if (hs->count > hl->count)
{ heap[c] = hl;
c = l;
}
else
break;
}
else
{ if (hs->count > hr->count)
{ heap[c] = hr;
c = r;
}
else
break;
}
}
if (c != s)
heap[c] = hs;
}
// Given Huffman tree build a table of codes from it, the low-order codelens[s] bits
// of codebits[s] contain the code for symbol s.
static void Build_Table(HTree *node, int code, int len, uint32 *codebits, int *codelens)
{ if (node->rgt == NULL)
{ uint64 symbol = (uint64) (node->lft);
codebits[symbol] = code;
codelens[symbol] = len;
}
else
{ code <<= 1;
len += 1;
Build_Table(node->lft,code,len,codebits,codelens);
Build_Table(node->rgt,code+1,len,codebits,codelens);
}
}
// For the non-zero symbols in hist, compute a huffman tree over them, and then
// build a table of the codes. If inscheme is not NULL, then place all symbols
// with code 255 or with more than HUFF_CUTOFF bits in the encoding by inscheme
// as a single united entity, whose code signals that the value of these symbols
// occur explicitly in 8 (values) or 16 (run lengths) bits following the code.
// All the symbols in this class will have the same entry in the code table and
// 255 is always in this class.
static HScheme *Huffman(uint64 *hist, HScheme *inscheme)
{ HScheme *scheme;
HTree *heap[259];
HTree node[512];
int hsize;
HTree *lft, *rgt;
int value, range;
int i;
scheme = (HScheme *) Malloc(sizeof(HScheme),"Allocating Huffman scheme record");
if (scheme == NULL)
return (NULL);
hsize = 0; // Load heap
value = 0;
if (inscheme != NULL)
{ node[0].count = 0;
node[0].lft = (HTree *) (uint64) 255;
node[0].rgt = NULL;
heap[++hsize] = node+(value++);
}
for (i = 0; i < 256; i++)
if (hist[i] > 0)
{ if (inscheme != NULL && (inscheme->codelens[i] > HUFF_CUTOFF || i == 255))
node[0].count += hist[i];
else
{ node[value].count = hist[i];
node[value].lft = (HTree *) (uint64) i;
node[value].rgt = NULL;
heap[++hsize] = node+(value++);
}
}
for (i = hsize/2; i >= 1; i--) // Establish heap property
Reheap(i,heap,hsize);
range = value; // Merge pairs with smallest count until have a tree
for (i = 1; i < value; i++)
{ lft = heap[1];
heap[1] = heap[hsize--];
Reheap(1,heap,hsize);
rgt = heap[1];
node[range].lft = lft;
node[range].rgt = rgt;
node[range].count = lft->count + rgt->count;
heap[1] = node+(range++);
Reheap(1,heap,hsize);
}
for (i = 0; i < 256; i++) // Build the code table
{ scheme->codebits[i] = 0;
scheme->codelens[i] = 0;
}
Build_Table(node+(range-1),0,0,scheme->codebits,scheme->codelens);
if (inscheme != NULL) // Set scheme type and if truncated (2), map truncated codes
{ scheme->type = 2; // to code and length for 255
for (i = 0; i < 255; i++)
if (inscheme->codelens[i] > HUFF_CUTOFF || scheme->codelens[i] > HUFF_CUTOFF)
{ scheme->codelens[i] = scheme->codelens[255];
scheme->codebits[i] = scheme->codebits[255];
}
}
else
{ scheme->type = 0;
for (i = 0; i < 256; i++)
{ if (scheme->codelens[i] > HUFF_CUTOFF)
scheme->type = 1;
}
}
return (scheme);
}
#ifdef DEBUG
// For debug, show the coding table
static void Print_Table(HScheme *scheme, uint64 *hist, int infosize)
{ uint64 total_bits;
uint32 specval, mask, code, *bits;
int speclen, clen, *lens;
int i, k;
total_bits = 0;
bits = scheme->codebits;
lens = scheme->codelens;
if (scheme->type == 2)
{ specval = bits[255];
speclen = lens[255];
}
else
specval = speclen = 0x7fffffff;
printf("\nCode Table:\n");
for (i = 0; i < 256; i++)
if (lens[i] > 0)
{ clen = lens[i];
mask = (1 << clen);
code = bits[i];
printf(" %3d: %2d ",i,clen);
for (k = 0; k < clen; k++)
{ mask >>= 1;
if (code & mask)
printf("1");
else
printf("0");
}
if (code == specval && clen == speclen)
{ printf(" ***");
if (hist != NULL)
total_bits += (clen+infosize)*hist[i];
}
else if (hist != NULL)
total_bits += clen*hist[i];
printf("\n");
}
if (hist != NULL)
printf("\nTotal Bytes = %lld\n",(total_bits-1)/8+1);
}
// For debug, show the histogram
static void Print_Histogram(uint64 *hist)
{ int i, low, hgh;
uint64 count;
for (hgh = 255; hgh >= 0; hgh--)
if (hist[hgh] != 0)
break;
for (low = 0; low < 256; low++)
if (hist[low] != 0)
break;
count = 0;
for (i = low; i <= hgh; i++)
count += hist[i];
for (i = hgh; i >= low; i--)
printf(" %3d: %8llu %5.1f%%\n",i,hist[i],(hist[i]*100.)/count);
}
#endif
/*******************************************************************************************
*
* Read and Write Huffman Schemes
*
********************************************************************************************/
// Write the code table to out.
static void Write_Scheme(HScheme *scheme, FILE *out)
{ int i;
uint8 x;
uint32 *bits;
int *lens;
lens = scheme->codelens;
bits = scheme->codebits;
x = (uint8) (scheme->type);
fwrite(&x,1,1,out);
for (i = 0; i < 256; i++)
{ x = (uint8) (lens[i]);
fwrite(&x,1,1,out);
if (x > 0)
fwrite(bits+i,sizeof(uint32),1,out);
}
}
// Allocate and read a code table from in, and return a pointer to it.
static HScheme *Read_Scheme(FILE *in)
{ HScheme *scheme;
int *look, *lens;
uint32 *bits, base;
int i, j, powr;
uint8 x;
scheme = (HScheme *) Malloc(sizeof(HScheme),"Allocating Huffman scheme record");
if (scheme == NULL)
return (NULL);
lens = scheme->codelens;
bits = scheme->codebits;
look = scheme->lookup;
if (fread(&x,1,1,in) != 1)
{ EPRINTF(EPLACE,"Could not read scheme type byte (Read_Scheme)\n");
free(scheme);
return (NULL);
}
scheme->type = x;
for (i = 0; i < 256; i++)
{ if (fread(&x,1,1,in) != 1)
{ EPRINTF(EPLACE,"Could not read length of %d'th code (Read_Scheme)\n",i);
return (NULL);
}
lens[i] = x;
if (x > 0)
{ if (fread(bits+i,sizeof(uint32),1,in) != 1)
{ EPRINTF(EPLACE,"Could not read bit encoding of %d'th code (Read_Scheme)\n",i);
free(scheme);
return (NULL);
}
}
else
bits[i] = 0;
}
if (Flip)
{ for (i = 0; i < 256; i++)
Flip_Long(bits+i);
}
for (i = 0; i < 256; i++)
{ if (lens[i] > 0)
{ base = (bits[i] << (16-lens[i]));
powr = (1 << (16-lens[i]));
for (j = 0; j < powr; j++)
look[base+j] = i;
}
}
return (scheme);
}
/*******************************************************************************************
*
* Encoders and Decoders
*
********************************************************************************************/
// Encode read[0..rlen-1] according to scheme and write to out
static void Encode(HScheme *scheme, FILE *out, uint8 *read, int rlen)
{ uint32 x, c, ocode;
int n, k, olen, llen;
int *nlens;
uint32 *nbits;
uint32 nspec;
int nslen;
nlens = scheme->codelens;
nbits = scheme->codebits;
if (scheme->type == 2)
{ nspec = nbits[255];
nslen = nlens[255];
}
else
nspec = nslen = 0x7fffffff;
#define OCODE(L,C) \
{ int len = olen + (L); \
uint32 code = (C); \
\
llen = olen; \
if (len >= 32) \
{ olen = len-32; \
ocode |= (code >> olen); \
fwrite(&ocode,sizeof(uint32),1,out); \
if (olen > 0) \
ocode = (code << (32-olen)); \
else \
ocode = 0; \
} \
else \
{ olen = len; \
ocode |= (code << (32-olen));; \
} \
}
llen = 0;
olen = 0;
ocode = 0;
for (k = 0; k < rlen; k++)
{ x = read[k];
n = nlens[x];
c = nbits[x];
OCODE(n,c);
if (c == nspec && n == nslen)
OCODE(8,x);
}
if (olen > 0) // Tricky: must pad so decoder does not read past
{ fwrite(&ocode,sizeof(uint32),1,out); // last integer int the coded output.
if (llen > 16 && olen > llen)
fwrite(&ocode,sizeof(uint32),1,out);
}
else if (llen > 16)
fwrite(&ocode,sizeof(uint32),1,out);
}
// Encode read[0..rlen-1] according to non-rchar table neme, and run-length table reme for
// runs of rchar characters. Write to out.
static void Encode_Run(HScheme *neme, HScheme *reme, FILE *out, uint8 *read, int rlen, int rchar)
{ uint32 x, c, ocode;
int n, h, k, olen, llen;
int *nlens, *rlens;
uint32 *nbits, *rbits;
uint32 nspec, rspec;
int nslen, rslen;
nlens = neme->codelens;
nbits = neme->codebits;
rlens = reme->codelens;
rbits = reme->codebits;
if (neme->type == 2)
{ nspec = nbits[255];
nslen = nlens[255];
}
else
nspec = nslen = 0x7fffffff;
rspec = rbits[255];
rslen = rlens[255];
llen = 0;
olen = 0;
ocode = 0;
k = 0;
while (k < rlen)
{ h = k;
while (k < rlen && read[k] == rchar)
k += 1;
if (k-h >= 255)
x = 255;
else
x = k-h;
n = rlens[x];
c = rbits[x];
OCODE(n,c);
if (c == rspec && n == rslen)
OCODE(16,k-h);
if (k < rlen)
{ x = read[k];
n = nlens[x];
c = nbits[x];
OCODE(n,c);
if (c == nspec && n == nslen)
OCODE(8,x);
k += 1;
}
}
if (olen > 0)
{ fwrite(&ocode,sizeof(uint32),1,out);
if (llen > 16 && olen > llen)
fwrite(&ocode,sizeof(uint32),1,out);
}
else if (llen > 16)
fwrite(&ocode,sizeof(uint32),1,out);
}
// Read and decode from in, the next rlen symbols into read according to scheme
static int Decode(HScheme *scheme, FILE *in, char *read, int rlen)
{ int *look, *lens;
int signal, ilen;
uint64 icode;
uint32 *ipart;
uint16 *xpart;
uint8 *cpart;
int j, n, c;
if (LittleEndian)
{ ipart = ((uint32 *) (&icode));
xpart = ((uint16 *) (&icode)) + 2;
cpart = ((uint8 *) (&icode)) + 5;
}
else
{ ipart = ((uint32 *) (&icode)) + 1;
xpart = ((uint16 *) (&icode)) + 1;
cpart = ((uint8 *) (&icode)) + 2;
}
if (scheme->type == 2)
signal = 255;
else
signal = 256;
lens = scheme->codelens;
look = scheme->lookup;
#define GET \
if (n > ilen) \
{ icode <<= ilen; \
if (fread(ipart,sizeof(uint32),1,in) != 1) \
{ EPRINTF(EPLACE,"Could not read more bits (Decode)\n"); \
return (1); \
} \
ilen = n-ilen; \
icode <<= ilen; \
ilen = 32-ilen; \
} \
else \
{ icode <<= n; \
ilen -= n; \
}
#define GETFLIP \
if (n > ilen) \
{ icode <<= ilen; \
if (fread(ipart,sizeof(uint32),1,in) != 1) \
{ EPRINTF(EPLACE,"Could not read more bits (Decode)\n"); \
return (1); \
} \
Flip_Long(ipart); \
ilen = n-ilen; \
icode <<= ilen; \
ilen = 32-ilen; \
} \
else \
{ icode <<= n; \
ilen -= n; \
}
n = 16;
ilen = 0;
icode = 0;
if (Flip)
for (j = 0; j < rlen; j++)
{ GETFLIP
c = look[*xpart];
n = lens[c];
if (c == signal)
{ GETFLIP
c = *cpart;
n = 8;
}
read[j] = (char) c;
}
else
for (j = 0; j < rlen; j++)
{ GET
c = look[*xpart];
n = lens[c];
if (c == signal)
{ GET
c = *cpart;
n = 8;
}
read[j] = (char) c;
}
return (0);
}
// Read and decode from in, the next rlen symbols into read according to non-rchar scheme
// neme, and the rchar runlength shceme reme
static int Decode_Run(HScheme *neme, HScheme *reme, FILE *in, char *read,
int rlen, int rchar)
{ int *nlook, *nlens;
int *rlook, *rlens;
int nsignal, ilen;
uint64 icode;
uint32 *ipart;
uint16 *xpart;
uint8 *cpart;
int j, n, c, k;
if (LittleEndian)
{ ipart = ((uint32 *) (&icode));
xpart = ((uint16 *) (&icode)) + 2;
cpart = ((uint8 *) (&icode)) + 5;
}
else
{ ipart = ((uint32 *) (&icode)) + 1;
xpart = ((uint16 *) (&icode)) + 1;
cpart = ((uint8 *) (&icode)) + 2;
}
if (neme->type == 2)
nsignal = 255;
else
nsignal = 256;
nlens = neme->codelens;
nlook = neme->lookup;
rlens = reme->codelens;
rlook = reme->lookup;
n = 16;
ilen = 0;
icode = 0;
if (Flip)
for (j = 0; j < rlen; j++)
{ GETFLIP
c = rlook[*xpart];
n = rlens[c];
if (c == 255)
{ GETFLIP
c = *xpart;
n = 16;
}
for (k = 0; k < c; k++)
read[j++] = (char) rchar;
if (j < rlen)
{ GETFLIP
c = nlook[*xpart];
n = nlens[c];
if (c == nsignal)
{ GETFLIP
c = *cpart;
n = 8;
}
read[j] = (char) c;
}
}
else
for (j = 0; j < rlen; j++)
{ GET
c = rlook[*xpart];
n = rlens[c];
if (c == 255)
{ GET
c = *xpart;
n = 16;
}
for (k = 0; k < c; k++)
read[j++] = (char) rchar;
if (j < rlen)
{ GET
c = nlook[*xpart];
n = nlens[c];
if (c == nsignal)
{ GET
c = *cpart;
n = 8;
}
read[j] = (char) c;
}
}
return (0);
}
/*******************************************************************************************
*
* Histogrammers
*
********************************************************************************************/
// Histogram runlengths of symbol runChar in stream[0..rlen-1] into run.
static void Histogram_Seqs(uint64 *hist, uint8 *stream, int rlen)
{ int k;
for (k = 0; k < rlen; k++)
hist[stream[k]] += 1;
}
static void Histogram_Runs(uint64 *run, uint8 *stream, int rlen, int runChar)
{ int k, h;
k = 0;
while (k < rlen)
{ h = k;
while (k < rlen && stream[k] == runChar)
k += 1;
if (k-h >= 256)
run[255] += 1;
else
run[k-h] += 1;
if (k < rlen)
k += 1;
}
}
/*******************************************************************************************
*
* Reader
*
********************************************************************************************/
static char *Read = NULL; // Referred by: QVentry, Read_Lines, QVcoding_Scan,
static int Rmax = -1; // Compress_Next_QVentry
static int Nline; // Referred by: QVcoding_Scan
char *QVentry()
{ return (Read); }
// If nlines == 1 trying to read a single header, nlines = 5 trying to read 5 QV/fasta lines
// for a sequence. Place line j at Read+j*Rmax and the length of every line is returned
// unless eof occurs in which case return -1. If any error occurs return -2.
int Read_Lines(FILE *input, int nlines)
{ int i, rlen;
int tmax;
char *tread;
char *other;
if (Read == NULL)
{ tmax = MIN_BUFFER;
tread = (char *) Malloc(5*tmax,"Allocating QV entry read buffer");
if (tread == NULL)
EXIT(-2);
Rmax = tmax;
Read = tread;
}
Nline += 1;
if (fgets(Read,Rmax,input) == NULL)
return (-1);
rlen = strlen(Read);
while (Read[rlen-1] != '\n')
{ tmax = ((int) 1.4*Rmax) + MIN_BUFFER;
tread = (char *) Realloc(Read,5*tmax,"Reallocating QV entry read buffer");
if (tread == NULL)
EXIT(-2);
Rmax = tmax;
Read = tread;
if (fgets(Read+rlen,Rmax-rlen,input) == NULL)
{ EPRINTF(EPLACE,"Line %d: Last line does not end with a newline !\n",Nline);
EXIT(-2);
}
rlen += strlen(Read+rlen);
}
other = Read;
for (i = 1; i < nlines; i++)
{ other += Rmax;
Nline += 1;
if (fgets(other,Rmax,input) == NULL)
{ EPRINTF(EPLACE,"Line %d: incomplete last entry of .quiv file\n",Nline);
EXIT(-2);
}
if (rlen != (int) strlen(other))
{ EPRINTF(EPLACE,"Line %d: Lines for an entry are not the same length\n",Nline);
EXIT(-2);
}
}
return (rlen-1);
}
/*******************************************************************************************
*
* Tag compression and decompression routines
*
********************************************************************************************/
// Keep only the symbols in tags[0..rlen-1] for which qvs[k] != rchar and
// return the # of symbols kept.
static int Pack_Tag(char *tags, char *qvs, int rlen, int rchar)
{ int j, k;
j = 0;
for (k = 0; k < rlen; k++)
if (qvs[k] != rchar)
tags[j++] = tags[k];
tags[j] = '\0';
return (j);
}
// Count the # of non-rchar symbols in qvs[0..rlen-1]
static int Packed_Length(char *qvs, int rlen, int rchar)
{ int k, clen;
clen = 0;
for (k = 0; k < rlen; k++)
if (qvs[k] != rchar)
clen += 1;
return (clen);
}
// Unpack tags by moving its i'th char to position k where qvs[k] is the i'th non-rchar
// symbol in qvs. All other chars are set to rchar. rlen is the length of qvs and
// the unpacked result, clen is the initial length of tags.
static void Unpack_Tag(char *tags, int clen, char *qvs, int rlen, int rchar)
{ int j, k;
j = clen-1;
for (k = rlen-1; k >= 0; k--)
{ if (qvs[k] == rchar)
tags[k] = 'n';
else
tags[k] = tags[j--];
}
}
/*******************************************************************************************
*
* Statistics Scan and Scheme creation and write
*
********************************************************************************************/
// Read .quiva file from input, recording stats in the histograms. If zero is set then
// start the stats anew with this file.
static uint64 delHist[256], insHist[256], mrgHist[256], subHist[256], delRun[256], subRun[256];
static uint64 totChar;
static int delChar, subChar;
// Referred by: QVcoding_Scan, Create_QVcoding
int QVcoding_Scan(FILE *input)
{ char *slash;
int rlen;
// Zero histograms
bzero(delHist,sizeof(uint64)*256);
bzero(mrgHist,sizeof(uint64)*256);
bzero(insHist,sizeof(uint64)*256);
bzero(subHist,sizeof(uint64)*256);
{ int i;
for (i = 0; i < 256; i++)
delRun[i] = subRun[i] = 1;
}
totChar = 0;
delChar = -1;
subChar = -1;
// Make a sweep through the .quiva entries, histogramming the relevant things
// and figuring out the run chars for the deletion and substition streams
Nline = 0;
while (1)
{ int well, beg, end, qv;
rlen = Read_Lines(input,1);
if (rlen == -2)
EXIT(1);
if (rlen < 0)
break;
if (rlen == 0 || Read[0] != '@')
{ EPRINTF(EPLACE,"Line %d: Header in quiv file is missing\n",Nline);
EXIT(1);
}
slash = index(Read+1,'/');
if (slash == NULL)
{ EPRINTF(EPLACE,"%s: Line %d: Header line incorrectly formatted ?\n",
Prog_Name,Nline);
EXIT(1);
}
if (sscanf(slash+1,"%d/%d_%d RQ=0.%d\n",&well,&beg,&end,&qv) != 4)
{ EPRINTF(EPLACE,"%s: Line %d: Header line incorrectly formatted ?\n",
Prog_Name,Nline);
EXIT(1);
}
rlen = Read_Lines(input,5);
if (rlen < 0)
{ if (rlen == -1)
EPRINTF(EPLACE,"Line %d: incomplete last entry of .quiv file\n",Nline);
EXIT(1);
}
Histogram_Seqs(delHist,(uint8 *) (Read),rlen);
Histogram_Seqs(insHist,(uint8 *) (Read+2*Rmax),rlen);
Histogram_Seqs(mrgHist,(uint8 *) (Read+3*Rmax),rlen);
Histogram_Seqs(subHist,(uint8 *) (Read+4*Rmax),rlen);
if (delChar < 0)
{ int k;
char *del = Read+Rmax;
for (k = 0; k < rlen; k++)
if (del[k] == 'n' || del[k] == 'N')
{ delChar = Read[k];
break;
}
}
if (delChar >= 0)
Histogram_Runs( delRun,(uint8 *) (Read),rlen,delChar);
totChar += rlen;
if (subChar < 0)
{ if (totChar >= 100000)
{ int k;
subChar = 0;
for (k = 1; k < 256; k++)
if (subHist[k] > subHist[subChar])
subChar = k;
}
}
if (subChar >= 0)
Histogram_Runs( subRun,(uint8 *) (Read+4*Rmax),rlen,subChar);
}
return (0);
}
// Using the statistics in the global stat tables, create the Huffman schemes and write
// them to output. If lossy is set, then create a lossy table for the insertion and merge
// QVs.
QVcoding *Create_QVcoding(int lossy)
{ static QVcoding coding;
HScheme *delScheme, *insScheme, *mrgScheme, *subScheme;
HScheme *dRunScheme, *sRunScheme;
delScheme = NULL;
dRunScheme = NULL;
insScheme = NULL;