-
Notifications
You must be signed in to change notification settings - Fork 33
/
DB.c
2923 lines (2503 loc) · 77.5 KB
/
DB.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
/*******************************************************************************************
*
* Compressed data base module. Auxiliary routines to open and manipulate a data base for
* which the sequence and read information are separated into two separate files, and the
* sequence is compressed into 2-bits for each base. Support for tracks of additional
* information, and trimming according to the current partition.
*
* Author : Gene Myers
* Date : July 2013
* Revised: April 2014
*
********************************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <dirent.h>
#include <limits.h>
#include <sys/stat.h>
#include "DB.h"
#ifdef HIDE_FILES
#define PATHSEP "/."
#else
#define PATHSEP "/"
#endif
/*******************************************************************************************
*
* GENERAL UTILITIES
*
********************************************************************************************/
char *Prog_Name;
#ifdef INTERACTIVE
char Ebuffer[1000];
#endif
int Count_Args(char *var)
{ int cnt, lev;
char *s;
cnt = 1;
lev = 0;
for (s = var; *s != '\0'; s++)
if (*s == ',')
{ if (lev == 0)
cnt += 1;
}
else if (*s == '(')
lev += 1;
else if (*s == ')')
lev -= 1;
return (cnt);
}
void *Malloc(int64 size, char *mesg)
{ void *p;
if ((p = malloc(size)) == NULL)
{ if (mesg == NULL)
EPRINTF(EPLACE,"%s: Out of memory\n",Prog_Name);
else
EPRINTF(EPLACE,"%s: Out of memory (%s)\n",Prog_Name,mesg);
}
return (p);
}
void *Realloc(void *p, int64 size, char *mesg)
{ if (size <= 0)
size = 1;
if ((p = realloc(p,size)) == NULL)
{ if (mesg == NULL)
EPRINTF(EPLACE,"%s: Out of memory\n",Prog_Name);
else
EPRINTF(EPLACE,"%s: Out of memory (%s)\n",Prog_Name,mesg);
}
return (p);
}
char *Strdup(char *name, char *mesg)
{ char *s;
if (name == NULL)
return (NULL);
if ((s = strdup(name)) == NULL)
{ if (mesg == NULL)
EPRINTF(EPLACE,"%s: Out of memory\n",Prog_Name);
else
EPRINTF(EPLACE,"%s: Out of memory (%s)\n",Prog_Name,mesg);
}
return (s);
}
FILE *Fopen(char *name, char *mode)
{ FILE *f;
if (name == NULL || mode == NULL)
return (NULL);
if ((f = fopen(name,mode)) == NULL)
EPRINTF(EPLACE,"%s: Cannot open %s for '%s'\n",Prog_Name,name,mode);
return (f);
}
char *PathTo(char *name)
{ char *path, *find;
if (name == NULL)
return (NULL);
if ((find = rindex(name,'/')) != NULL)
{ *find = '\0';
path = Strdup(name,"Extracting path from");
*find = '/';
}
else
path = Strdup(".","Allocating default path");
return (path);
}
char *Root(char *name, char *suffix)
{ char *path, *find, *dot;
int epos;
if (name == NULL)
return (NULL);
find = rindex(name,'/');
if (find == NULL)
find = name;
else
find += 1;
if (suffix == NULL)
{ dot = strchr(find,'.');
if (dot != NULL)
*dot = '\0';
path = Strdup(find,"Extracting root from");
if (dot != NULL)
*dot = '.';
}
else
{ epos = strlen(find);
epos -= strlen(suffix);
if (epos > 0 && strcasecmp(find+epos,suffix) == 0)
{ find[epos] = '\0';
path = Strdup(find,"Extracting root from");
find[epos] = suffix[0];
}
else
path = Strdup(find,"Allocating root");
}
return (path);
}
char *Catenate(char *path, char *sep, char *root, char *suffix)
{ static char *cat = NULL;
static int max = -1;
int len;
if (path == NULL || root == NULL || sep == NULL || suffix == NULL)
return (NULL);
len = strlen(path);
len += strlen(sep);
len += strlen(root);
len += strlen(suffix);
if (len > max)
{ max = ((int) (1.2*len)) + 100;
cat = (char *) realloc(cat,max+1);
if (cat == NULL)
{ EPRINTF(EPLACE,"%s: Out of memory (Making path name for %s)\n",Prog_Name,root);
return (NULL);
}
}
sprintf(cat,"%s%s%s%s",path,sep,root,suffix);
return (cat);
}
char *Numbered_Suffix(char *left, int num, char *right)
{ static char *sfx = NULL;
static int max = -1;
int len;
if (left == NULL || right == NULL)
return (NULL);
len = strlen(left);
len += strlen(right) + 40;
if (len > max)
{ max = ((int) (1.2*len)) + 100;
sfx = (char *) realloc(sfx,max+1);
if (sfx == NULL)
{ EPRINTF(EPLACE,"%s: Out of memory (Making number suffix for %d)\n",Prog_Name,num);
return (NULL);
}
}
sprintf(sfx,"%s%d%s",left,num,right);
return (sfx);
}
static char *MyCatenate(char *path, char *sep, char *root, char *suffix)
{ static char *cat = NULL;
static int max = -1;
int len;
if (path == NULL || root == NULL || sep == NULL || suffix == NULL)
return (NULL);
len = strlen(path);
len += strlen(sep);
len += strlen(root);
len += strlen(suffix);
if (len > max)
{ max = ((int) (1.2*len)) + 100;
cat = (char *) realloc(cat,max+1);
if (cat == NULL)
{ EPRINTF(EPLACE,"%s: Out of memory (Making path name for %s)\n",Prog_Name,root);
return (NULL);
}
}
sprintf(cat,"%s%s%s%s",path,sep,root,suffix);
return (cat);
}
static char *MyNumbered_Suffix(char *left, int num, char *right)
{ static char *sfx = NULL;
static int max = -1;
int len;
if (left == NULL || right == NULL)
return (NULL);
len = strlen(left);
len += strlen(right) + 40;
if (len > max)
{ max = ((int) (1.2*len)) + 100;
sfx = (char *) realloc(sfx,max+1);
if (sfx == NULL)
{ EPRINTF(EPLACE,"%s: Out of memory (Making number suffix for %d)\n",Prog_Name,num);
return (NULL);
}
}
sprintf(sfx,"%s%d%s",left,num,right);
return (sfx);
}
#define COMMA ','
// Print big integers with commas/periods for better readability
void Print_Number(int64 num, int width, FILE *out)
{ if (width == 0)
{ if (num < 1000ll)
fprintf(out,"%lld",num);
else if (num < 1000000ll)
fprintf(out,"%lld%c%03lld",num/1000ll,COMMA,num%1000ll);
else if (num < 1000000000ll)
fprintf(out,"%lld%c%03lld%c%03lld",num/1000000ll,
COMMA,(num%1000000ll)/1000ll,COMMA,num%1000ll);
else
fprintf(out,"%lld%c%03lld%c%03lld%c%03lld",num/1000000000ll,
COMMA,(num%1000000000ll)/1000000ll,
COMMA,(num%1000000ll)/1000ll,COMMA,num%1000ll);
}
else
{ if (num < 1000ll)
fprintf(out,"%*lld",width,num);
else if (num < 1000000ll)
{ if (width <= 4)
fprintf(out,"%lld%c%03lld",num/1000ll,COMMA,num%1000ll);
else
fprintf(out,"%*lld%c%03lld",width-4,num/1000ll,COMMA,num%1000ll);
}
else if (num < 1000000000ll)
{ if (width <= 8)
fprintf(out,"%lld%c%03lld%c%03lld",num/1000000ll,COMMA,(num%1000000ll)/1000ll,
COMMA,num%1000ll);
else
fprintf(out,"%*lld%c%03lld%c%03lld",width-8,num/1000000ll,COMMA,(num%1000000ll)/1000ll,
COMMA,num%1000ll);
}
else
{ if (width <= 12)
fprintf(out,"%lld%c%03lld%c%03lld%c%03lld",num/1000000000ll,COMMA,
(num%1000000000ll)/1000000ll,COMMA,
(num%1000000ll)/1000ll,COMMA,num%1000ll);
else
fprintf(out,"%*lld%c%03lld%c%03lld%c%03lld",width-12,num/1000000000ll,COMMA,
(num%1000000000ll)/1000000ll,COMMA,
(num%1000000ll)/1000ll,COMMA,num%1000ll);
}
}
}
// Return the number of digits, base 10, of num
int Number_Digits(int64 num)
{ int digit;
digit = 0;
while (num >= 1)
{ num /= 10;
digit += 1;
}
return (digit);
}
/*******************************************************************************************
*
* READ COMPRESSION/DECOMPRESSION UTILITIES
*
********************************************************************************************/
// Compress read into 2-bits per base (from [0-3] per byte representation
void Compress_Read(int len, char *s)
{ int i;
char c, d;
char *s0, *s1, *s2, *s3;
s0 = s;
s1 = s0+1;
s2 = s1+1;
s3 = s2+1;
c = s1[len];
d = s2[len];
s0[len] = s1[len] = s2[len] = 0;
for (i = 0; i < len; i += 4)
*s++ = (char ) ((s0[i] << 6) | (s1[i] << 4) | (s2[i] << 2) | s3[i]);
s1[len] = c;
s2[len] = d;
}
// Uncompress read form 2-bits per base into [0-3] per byte representation
void Uncompress_Read(int len, char *s)
{ int i, tlen, byte;
char *s0, *s1, *s2, *s3;
char *t;
s0 = s;
s1 = s0+1;
s2 = s1+1;
s3 = s2+1;
tlen = (len-1)/4;
t = s+tlen;
for (i = tlen*4; i >= 0; i -= 4)
{ byte = *t--;
s0[i] = (char) ((byte >> 6) & 0x3);
s1[i] = (char) ((byte >> 4) & 0x3);
s2[i] = (char) ((byte >> 2) & 0x3);
s3[i] = (char) (byte & 0x3);
}
s[len] = 4;
}
// Convert read in [0-3] representation to ascii representation (end with '\n')
void Lower_Read(char *s)
{ static char letter[4] = { 'a', 'c', 'g', 't' };
for ( ; *s != 4; s++)
*s = letter[(int) *s];
*s = '\0';
}
void Upper_Read(char *s)
{ static char letter[4] = { 'A', 'C', 'G', 'T' };
for ( ; *s != 4; s++)
*s = letter[(int) *s];
*s = '\0';
}
void Letter_Arrow(char *s)
{ static char letter[4] = { '1', '2', '3', '4' };
for ( ; *s != 4; s++)
*s = letter[(int) *s];
*s = '\0';
}
// Convert read in ascii representation to [0-3] representation (end with 4)
void Number_Read(char *s)
{ static char number[128] =
{ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 2,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 3, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 2,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 3, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
};
for ( ; *s != '\0'; s++)
*s = number[(int) *s];
*s = 4;
}
void Number_Arrow(char *s)
{ static char arrow[128] =
{ 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 0, 1, 2, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 2,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
};
for ( ; *s != '\0'; s++)
*s = arrow[(int) *s];
*s = 4;
}
void Change_Read(char *s)
{ static char change[128] =
{ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 'a', 0, 'c', 0, 0, 0, 'g',
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 't', 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 'A', 0, 'C', 0, 0, 0, 'G',
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 'T', 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
};
for ( ; *s != '\0'; s++)
*s = change[(int) *s];
}
/*******************************************************************************************
*
* DB STUB HANDLING ROUTINES
*
********************************************************************************************/
// Read the contents of the DB stub file at "path" and return it encoded in a DAZZ_STUB
// structure. This is allocated by the routine. "path" is assumed to be the complete
// name of the file.
DAZZ_STUB *Read_DB_Stub(char *path, int what)
{ FILE *dbfile;
DAZZ_STUB *stub;
char buf1[MAX_NAME+100];
char buf2[MAX_NAME+100];
int nread;
int i;
int nfiles;
int nblocks;
int64 size;
int all, cutoff;
dbfile = Fopen(path,"r");
if (dbfile == NULL)
EXIT(NULL);
stub = Malloc(sizeof(DAZZ_STUB),"Allocating DB stub record");
if (stub == NULL)
{ fclose(dbfile);
EXIT(NULL);
}
stub->nreads = NULL;
stub->fname = NULL;
stub->prolog = NULL;
stub->ublocks = NULL;
stub->tblocks = NULL;
if (fscanf(dbfile,DB_NFILE,&nfiles) != 1)
goto stub_trash;
if (what & DB_STUB_NREADS)
{ stub->nreads = (int *) Malloc(sizeof(int)*(nfiles+1),"Allocating DB stub record");
if (stub->nreads == NULL)
goto stub_error;
stub->nreads += 1;
}
if (what & DB_STUB_FILES)
{ stub->fname = (char **) Malloc(sizeof(char *)*(nfiles+1),"Allocating DB stub record");
if (stub->fname == NULL)
goto stub_error;
stub->fname += 1;
stub->nfiles = nfiles;
for (i = 0; i < nfiles; i++)
stub->fname[i] = NULL;
}
if (what & DB_STUB_PROLOGS)
{ stub->prolog = (char **) Malloc(sizeof(char *)*(nfiles+1),"Allocating DB stub record");
if (stub->prolog == NULL)
goto stub_error;
stub->prolog += 1;
for (i = 0; i < nfiles; i++)
stub->prolog[i] = NULL;
}
for (i = 0; i < nfiles; i++)
{ if (fscanf(dbfile,DB_FDATA,&nread,buf1,buf2) != 3)
goto stub_trash;
if (what & DB_STUB_NREADS)
stub->nreads[i] = nread;
if (what & DB_STUB_FILES)
{ stub->fname[i] = Strdup(buf1,"Alloacting DB stub record");
if (stub->fname[i] == NULL)
goto stub_error;
}
if (what & DB_STUB_PROLOGS)
{ stub->prolog[i] = Strdup(buf2,"Alloacting DB stub record");
if (stub->prolog[i] == NULL)
goto stub_error;
}
}
if (fscanf(dbfile,DB_NBLOCK,&nblocks) != 1)
goto stub_trash;
if (fscanf(dbfile,DB_PARAMS,&size,&cutoff,&all) != 3)
goto stub_trash;
if (what & DB_STUB_BLOCKS)
{ stub->ublocks = (int *) Malloc(sizeof(int)*(nblocks+1),"Allocating DB stub record");
stub->tblocks = (int *) Malloc(sizeof(int)*(nblocks+1),"Allocating DB stub record");
if (stub->ublocks == NULL || stub->tblocks == NULL)
goto stub_error;
for (i = 0; i <= nblocks; i++)
if (fscanf(dbfile,DB_BDATA,stub->ublocks+i,stub->tblocks+i) != 2)
goto stub_trash;
}
fclose(dbfile);
stub->nfiles = nfiles;
stub->all = all;
stub->cutoff = cutoff;
stub->bsize = size;
stub->nblocks = nblocks;
return (stub);
stub_trash:
EPRINTF(EPLACE,"%s: Stub file %s is junk\n",Prog_Name,path);
stub_error:
Free_DB_Stub(stub);
fclose(dbfile);
EXIT(NULL);
}
// Read the DB stub file "path" and extract the read index range [*first,*last)
// for block n, for the trimmed DB if trim is set, the untrimmed DB otherwise.
int Fetch_Block_Range(char *path, int trim, int n, int *first, int *last)
{ FILE *dbfile;
char buffer[2*MAX_NAME+100];
int nfiles;
int nblocks;
int64 size;
int all, cutoff;
int tfirst, tlast;
int ufirst, ulast;
int i;
dbfile = Fopen(path,"r");
if (dbfile == NULL)
EXIT(1);
if (fscanf(dbfile,DB_NFILE,&nfiles) != 1)
goto stub_error;
for (i = 0; i < nfiles; i++)
if (fgets(buffer,2*MAX_NAME+100,dbfile) == NULL)
goto stub_error;
if (fscanf(dbfile,DB_NBLOCK,&nblocks) != 1)
goto stub_error;
if (n < 0 || n >= nblocks)
{ *first = *last = -1;
return (0);
}
if (fscanf(dbfile,DB_PARAMS,&size,&cutoff,&all) != 3)
goto stub_error;
for (i = 1; i <= n; i++)
if (fscanf(dbfile,DB_BDATA,&ufirst,&tfirst) != 2)
goto stub_error;
if (fscanf(dbfile,DB_BDATA,&ulast,&tlast) != 2)
goto stub_error;
fclose(dbfile);
if (trim)
{ *first = tfirst;
*last = tlast;
}
else
{ *first = ufirst;
*last = ulast;
}
return (0);
stub_error:
fclose(dbfile);
EPRINTF(EPLACE,"%s: Stub file %s is junk\n",Prog_Name,path);
EXIT(1);
}
// Free a DAZZ_STUB data structure returned by Read_DB_Stub
void Free_DB_Stub(DAZZ_STUB *stub)
{ int i;
if (stub == NULL)
return;
if (stub->fname != NULL)
{ for (i = 0; i < stub->nfiles; i++)
free(stub->fname[i]);
free(stub->fname-1);
}
if (stub->prolog != NULL)
{ for (i = 0; i < stub->nfiles; i++)
free(stub->prolog[i]);
free(stub->prolog-1);
}
if (stub->nreads != NULL)
free(stub->nreads-1);
free(stub->ublocks);
free(stub->tblocks);
free(stub);
}
/*******************************************************************************************
*
* DB OPEN, TRIM, SIZE_OF, LIST_FILES & CLOSE ROUTINES
*
********************************************************************************************/
// Open the given database or dam, "path" into the supplied DAZZ_DB record "db". If the name has
// a part # in it then just the part is opened. The index array is allocated (for all or
// just the part) and read in.
// Return status of routine:
// -1: The DB could not be opened for a reason reported by the routine to EPLACE
// 0: Open of DB proceeded without mishap
// 1: Open of DAM proceeded without mishap
static char *atrack_name = ".@arw";
static char *qtrack_name = ".@qvs";
int Open_DB(char* path, DAZZ_DB *db)
{ DAZZ_DB dbcopy;
char *root, *pwd, *bptr, *fptr, *cat;
int nreads;
FILE *index, *dbvis, *bases;
int status, plen, isdam;
int part, cutoff, all;
int ufirst, tfirst, ulast, tlast;
status = -1;
dbcopy = *db;
plen = strlen(path);
if (strcmp(path+(plen-4),".dam") == 0)
{ root = Root(path,".dam");
isdam = 1;
}
else
{ if (strcmp(path+(plen-3),".db") == 0)
isdam = -1;
else
isdam = 0;
root = Root(path,".db");
}
pwd = PathTo(path);
bptr = rindex(root,'.');
if (bptr != NULL && bptr[1] != '\0' && bptr[1] != '-')
{ part = strtol(bptr+1,&fptr,10);
if (*fptr != '\0' || part == 0)
part = 0;
else
*bptr = '\0';
}
else
part = 0;
if (isdam > 0)
cat = MyCatenate(pwd,"/",root,".dam");
else
cat = MyCatenate(pwd,"/",root,".db");
if (cat == NULL)
return (-1);
if ((dbvis = fopen(cat,"r")) == NULL)
{ if (isdam < 0)
{ EPRINTF(EPLACE,"%s: Could not open DB %s\n",Prog_Name,path);
goto error;
}
if (isdam > 0)
{ EPRINTF(EPLACE,"%s: Could not open DAM %s\n",Prog_Name,path);
goto error;
}
cat = MyCatenate(pwd,"/",root,".dam");
if (cat == NULL)
return (-1);
if ((dbvis = fopen(cat,"r")) == NULL)
{ EPRINTF(EPLACE,"%s: Could not open %s as a DB or a DAM\n",Prog_Name,path);
goto error;
}
isdam = 1;
}
if (isdam < 0)
isdam = 0;
if ((index = Fopen(MyCatenate(pwd,PATHSEP,root,".idx"),"r")) == NULL)
goto error1;
if (fread(db,sizeof(DAZZ_DB),1,index) != 1)
{ EPRINTF(EPLACE,"%s: Index file (.idx) of %s is junk\n",Prog_Name,root);
goto error2;
}
{ int p, nblocks, nfiles;
int64 size;
char fname[MAX_NAME], prolog[MAX_NAME];
nblocks = 0;
if (fscanf(dbvis,DB_NFILE,&nfiles) != 1)
{ EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
goto error2;
}
for (p = 0; p < nfiles; p++)
if (fscanf(dbvis,DB_FDATA,&tlast,fname,prolog) != 3)
{ EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
goto error2;
}
if (fscanf(dbvis,DB_NBLOCK,&nblocks) != 1)
if (part == 0)
{ cutoff = 0;
all = DB_ALL;
}
else
{ EPRINTF(EPLACE,"%s: DB %s has not yet been partitioned, cannot request a block !\n",
Prog_Name,root);
goto error2;
}
else
{ if (fscanf(dbvis,DB_PARAMS,&size,&cutoff,&all) != 3)
{ EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
goto error2;
}
if (part > nblocks)
{ EPRINTF(EPLACE,"%s: DB %s has only %d blocks\n",Prog_Name,root,nblocks);
goto error2;
}
}
if (part > 0)
{ for (p = 1; p <= part; p++)
if (fscanf(dbvis,DB_BDATA,&ufirst,&tfirst) != 2)
{ EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
goto error2;
}
if (fscanf(dbvis,DB_BDATA,&ulast,&tlast) != 2)
{ EPRINTF(EPLACE,"%s: Stub file (.db) of %s is junk\n",Prog_Name,root);
goto error2;
}
}
else
{ ufirst = tfirst = 0;
ulast = db->ureads;
tlast = db->treads;
}
}
db->trimmed = 0;
db->tracks = NULL;
db->part = part;
db->cutoff = cutoff;
db->allarr |= all;
db->ufirst = ufirst;
db->tfirst = tfirst;
nreads = ulast-ufirst;
if (part <= 0)
{ db->reads = (DAZZ_READ *) Malloc(sizeof(DAZZ_READ)*(nreads+2),"Allocating Open_DB index");
if (db->reads == NULL)
goto error2;
db->reads += 1;
if (fread(db->reads,sizeof(DAZZ_READ),nreads,index) != (size_t) nreads)
{ EPRINTF(EPLACE,"%s: Index file (.idx) of %s is junk\n",Prog_Name,root);
free(db->reads-1);
goto error2;
}
}
else
{ DAZZ_READ *reads;
int i, r, maxlen;
int64 totlen;
reads = (DAZZ_READ *) Malloc(sizeof(DAZZ_READ)*(nreads+2),"Allocating Open_DB index");
if (reads == NULL)
goto error2;
reads += 1;
fseeko(index,sizeof(DAZZ_READ)*ufirst,SEEK_CUR);
if (fread(reads,sizeof(DAZZ_READ),nreads,index) != (size_t) nreads)
{ EPRINTF(EPLACE,"%s: Index file (.idx) of %s is junk\n",Prog_Name,root);
free(reads-1);
goto error2;
}
totlen = 0;
maxlen = 0;
for (i = 0; i < nreads; i++)
{ r = reads[i].rlen;
totlen += r;
if (r > maxlen)
maxlen = r;
}
db->maxlen = maxlen;
db->totlen = totlen;
db->reads = reads;
}
((int *) (db->reads))[-1] = ulast - ufirst; // Kludge, need these for DB part
((int *) (db->reads))[-2] = tlast - tfirst;
db->nreads = nreads;
db->path = Strdup(MyCatenate(pwd,PATHSEP,root,""),"Allocating Open_DB path");
if (db->path == NULL)
{ free(db->reads-1);
goto error2;
}
bases = Fopen(MyCatenate(db->path,"","",".bps"),"r");
if (bases == NULL)
{ free(db->path);
free(db->reads-1);
goto error2;
}
db->bases = (void *) bases;
db->loaded = 0;
status = isdam;
error2:
fclose(index);
error1:
fclose(dbvis);
error:
if (bptr != NULL)
*bptr = '.';
free(pwd);
free(root);
if (status < 0)
*db = dbcopy;
return (status);
}
// Trim the DB or part thereof and all opened tracks according to the cuttof and all settings
// of the current DB partition. Reallocate smaller memory blocks for the information kept
// for the retained reads.
void Trim_DB(DAZZ_DB *db)
{ int i, j, r, f;
int allflag, cutoff, css;
int64 totlen;
int maxlen, nreads;
DAZZ_TRACK *record;
DAZZ_READ *reads;
if (db->trimmed) return;
if (db->cutoff <= 0 && (db->allarr & DB_ALL) != 0) return;
{ int load_error;
load_error = db->loaded;
for (record = db->tracks; record != NULL; record = record->next)
if (record->name == atrack_name)
{ if (((DAZZ_ARROW *) record)->loaded)
load_error = 1;
}
else if (record->name != qtrack_name)
{ if (record->loaded)
load_error = 1;
}
if (load_error)
{ EPRINTF(EPLACE,"%s: Cannot load anything before trim (Trim_DB)\n",Prog_Name);
return;
}
}
cutoff = db->cutoff;
if ((db->allarr & DB_ALL) != 0)
allflag = 0;
else
allflag = DB_BEST;
reads = db->reads;
nreads = db->nreads;
for (record = db->tracks; record != NULL; record = record->next)
if (record->name == qtrack_name)
{ uint16 *table = ((DAZZ_QV *) record)->table;
j = 0;
for (i = 0; i < db->nreads; i++)
if ((reads[i].flags & DB_BEST) >= allflag && reads[i].rlen >= cutoff)
table[j++] = table[i];
}
else if (record->name == atrack_name)
{ DAZZ_ARROW *atrack = (DAZZ_ARROW *) record;
int64 *aoff = atrack->aoff;
for (j = i = 0; i < nreads; i++)
if ((reads[i].flags & DB_BEST) >= allflag && reads[i].rlen >= cutoff)
aoff[j++] = aoff[i];
atrack->aoff = Realloc(aoff,sizeof(int64)*j,NULL);
}
else
{ int size;
size = record->size;
if (record->data == NULL)
{ char *anno = (char *) record->anno;
j = 0;
for (i = r = 0; i < db->nreads; i++, r += size)
if ((reads[i].flags & DB_BEST) >= allflag && reads[i].rlen >= cutoff)
{ memmove(anno+j,anno+r,size);
j += size;
}
record->anno = Realloc(record->anno,record->size*j,NULL);
}
else if (size == 4)
{ int *anno4 = (int *) (record->anno);
int *alen = record->alen;
j = 0;
for (i = 0; i < db->nreads; i++)
if ((reads[i].flags & DB_BEST) >= allflag && reads[i].rlen >= cutoff)
{ anno4[j] = anno4[i];
alen[j] = alen[i];
j += 1;
}
record->alen = Realloc(record->alen,sizeof(int)*j,NULL);
record->anno = Realloc(record->anno,record->size*(j+1),NULL);
}
else // size == 8
{ int64 *anno8 = (int64 *) (record->anno);
int *alen = record->alen;
j = 0;
for (i = 0; i < db->nreads; i++)
if ((reads[i].flags & DB_BEST) >= allflag && reads[i].rlen >= cutoff)
{ anno8[j] = anno8[i];