-
Notifications
You must be signed in to change notification settings - Fork 21
/
database.cc
1537 lines (1258 loc) · 34.6 KB
/
database.cc
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
/*
SWIPE
Smith-Waterman database searches with Inter-sequence Parallel Execution
Copyright (C) 2008-2013 Torbjorn Rognes, University of Oslo,
Oslo University Hospital and Sencel Bioinformatics AS
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Contact: Torbjorn Rognes <[email protected]>,
Department of Informatics, University of Oslo,
PO Box 1080 Blindern, NO-0316 Oslo, Norway
*/
#include "swipe.h"
/* http://selab.janelia.org/people/farrarm/blastdbfmtv4/blastdbfmt.html */
char symtype_disp[] = "npxtzs";
unsigned int decompress_nt[256];
typedef struct al_info
{
char * title;
long dblist_len;
char * * dblist;
long oidlist_len;
char * * oidlist;
long memb_bit;
long length;
long maxoid;
long nseq;
} al_info_t;
typedef struct db_main_s
{
long volumecount;
char * path;
char * basename;
long symtype;
long version;
char * title;
char * time;
long seqcount;
long longest;
long symcount;
long masked_seqcount;
long masked_symcount;
long memb_bit;
char * taxid_filename;
FILE * taxid_file;
unsigned char * taxid_bitmap_address;
long taxid_bitmap_size;
} db_main_t;
typedef struct db_volume_s
{
// the underlying unmasked volume
char * basename;
long symtype;
long version;
char * title;
char * time;
long seqcount;
long longest;
long symcount;
// the masked volume - for masked files (swissprot, pdbaa, pdbnt)
char * masked_title;
long masked_length;
long masked_nseq;
long masked_maxoid;
long masked_memb_bit;
char * masked_mskfile;
//
long offset_xhr;
long offset_xsq;
long offset_amb;
int fd_xin; // entire mapped
int fd_xsq; // partially mapped
int fd_xhr; // open for normal read
int fd_msk; // mapped
long len_xin;
long len_xsq;
long len_xhr;
long len_msk;
char * adr_xin; // mapped address of xin file
unsigned char * adr_msk;
char * map_seq_address;
long map_seq_length;
long map_seq_offset;
char * map_hdr_address;
long map_hdr_length;
long map_hdr_offset;
} db_volume_t;
typedef struct db_map_s
{
char * map_address; // address in mem of mapped region (multiple of pagesize)
db_volume_t * map_volume; // volume mapped
long map_offset; // offset in file of the mapped region
long map_length; // size of memory mapped region
} db_map_t;
typedef struct db_map_s db_map_t;
typedef db_map_t * mapp;
typedef struct db_thread_s
{
mapp map_seq;
mapp map_hdr;
apt parser;
char * ntbuffer[16];
char * xxbuffer[16];
long ntbuffersize[16];
long xxbuffersize[16];
} db_thread_t;
void db_print_seq_map(char * address, long length, const char * map)
{
long linelength = 80;
long i = 0;
while (i<length)
{
long end = i + linelength;
if (length < end)
end = length;
while(i<end)
{
putc(map[(int)(address[i])], out);
i++;
}
fprintf(out, "\n");
}
}
mapp db_map_create()
{
mapp m = (mapp) xmalloc(sizeof(struct db_map_s));
m->map_volume = 0;
m->map_offset = 0;
m->map_address = 0;
m->map_length = 0;
return m;
}
void db_map_destruct(mapp m)
{
if (m->map_address)
munmap(m->map_address, m->map_length);
free(m);
}
db_thread_t * db_thread_create()
{
struct db_thread_s * t = (struct db_thread_s *) xmalloc(sizeof(struct db_thread_s));
t->map_seq = db_map_create();
t->map_hdr = db_map_create();
t->parser = parser_create();
for(int c=0; c<16; c++)
{
t->ntbuffersize[c] = 0;
t->ntbuffer[c] = 0;
t->xxbuffersize[c] = 0;
t->xxbuffer[c] = 0;
}
return t;
}
void db_thread_destruct(struct db_thread_s * t)
{
parser_destruct(t->parser);
db_map_destruct(t->map_seq);
db_map_destruct(t->map_hdr);
for(int c=0; c<16; c++)
{
if (t->ntbuffer[c])
free(t->ntbuffer[c]);
t->ntbuffersize[c] = 0;
t->ntbuffer[c] = 0;
if (t->xxbuffer[c])
free(t->xxbuffer[c]);
t->xxbuffersize[c] = 0;
t->xxbuffer[c] = 0;
}
free(t);
}
#define MAXVOLUMES 256
db_main_t db_main;
db_volume_t db_volume[MAXVOLUMES];
void db_volume_init(db_volume_t * v)
{
if (v - db_volume >= MAXVOLUMES)
fatal("Too many database volumes.");
v->symtype = -1;
v->version = 0;
v->title = NULL;
v->time = NULL;
v->seqcount = 0;
v->longest = 0;
v->symcount = 0;
v->masked_title = NULL;
v->masked_length = 0;
v->masked_nseq = 0;
v->masked_maxoid = 0;
v->masked_memb_bit = 0;
v->masked_mskfile = NULL;
v->offset_xhr = 0;
v->offset_xsq = 0;
v->offset_amb = 0;
v->fd_xin = 0;
v->fd_xsq = 0;
v->fd_xhr = 0;
v->fd_msk = 0;
v->len_xin = 0;
v->len_xsq = 0;
v->len_xhr = 0;
v->len_msk = 0;
v->adr_xin = NULL;
v->adr_msk = NULL;
v->map_seq_address = 0;
v->map_seq_length = 0;
v->map_seq_offset = 0;
v->map_hdr_address = 0;
v->map_hdr_length = 0;
v->map_hdr_offset = 0;
}
void db_init(db_main_t * v)
{
v->volumecount = 0;
v->basename = NULL;
v->symtype = -1;
v->version = 0;
v->title = NULL;
v->time = NULL;
v->seqcount = 0;
v->longest = 0;
v->symcount = 0;
v->taxid_bitmap_address = 0;
v->taxid_bitmap_size = 0;
v->taxid_filename = 0;
v->taxid_file = 0;
}
long getnames(char * line, char * * * names)
{
char ws[] = " \t\r\n\"";
long n = 0;
long namecount = 0;
char * p = line;
while (1)
{
long wslen = strspn(p, ws);
long namelen = strcspn(p + wslen, ws);
if (namelen > 0)
{
namecount++;
p += wslen + namelen;
}
else
break;
}
* names = (char**) xmalloc(namecount * sizeof(char*));
while (n < namecount)
{
long wslen = strspn(line, ws);
long namelen = strcspn(line + wslen, ws);
char * name = (char*) xmalloc(namelen + 1);
strncpy(name, line+wslen, namelen);
name[namelen] = 0;
(*names)[n] = name;
n++;
line += wslen + namelen;
}
return namecount;
}
void show_alias_info(al_info_t * ai)
{
if (ai->title)
fprintf(stderr, "TITLE: %s\n", ai->title);
fprintf(stderr, "VOLUMES: %ld\n", ai->dblist_len);
for(long i = 0; i < ai->dblist_len; i++)
fprintf(stderr, "DBLIST(%ld): %s\n", i, ai->dblist[i]);
// for masked files
for(long i = 0; i < ai->oidlist_len; i++)
fprintf(stderr, "OIDLIST(%ld): %s\n", i, ai->oidlist[i]);
if (ai->length >= 0)
fprintf(stderr, "LENGTH: %ld\n", ai->length);
if (ai->nseq >= 0)
fprintf(stderr, "NSEQ: %ld\n", ai->nseq);
if (ai->maxoid >= 0)
fprintf(stderr, "MAXOID: %ld\n", ai->maxoid);
if (ai->memb_bit >= 0)
fprintf(stderr, "MEMB_BIT: %ld\n", ai->memb_bit);
}
void show_db_info(db_main_t * vol)
{
fprintf(stderr, "DB info:\n");
fprintf(stderr, "volumecount: %ld\n", vol->volumecount);
fprintf(stderr, "basename: %s\n", vol->basename);
fprintf(stderr, "path: %s\n", vol->path);
fprintf(stderr, "symtype: %c\n", symtype_disp[vol->symtype]);
fprintf(stderr, "version: %ld\n", vol->version);
fprintf(stderr, "title: %s\n", vol->title);
fprintf(stderr, "time: %s\n", vol->time);
fprintf(stderr, "seqcount: %ld\n", vol->seqcount);
fprintf(stderr, "longest: %ld\n", vol->longest);
fprintf(stderr, "symcount: %ld\n", vol->symcount);
fprintf(stderr, "memb_bit: %ld\n", vol->memb_bit);
fprintf(stderr, "masked_seqcount: %ld\n", vol->masked_seqcount);
fprintf(stderr, "masked_symcount: %ld\n", vol->masked_symcount);
fprintf(stderr, "taxid_filename: %s\n", vol->taxid_filename);
fprintf(stderr, "taxid_file: %p\n", vol->taxid_file);
fprintf(stderr, "taxid_bitmap_address: %p\n", vol->taxid_bitmap_address);
fprintf(stderr, "taxid_bitmap_size: %ld\n", vol->taxid_bitmap_size);
fprintf(stderr, "\n");
}
void show_volume_info(db_volume_t * vol)
{
fprintf(stderr, "Volume info:\n");
fprintf(stderr, "basename: %s\n", vol->basename);
fprintf(stderr, "symtype: %c\n", symtype_disp[vol->symtype]);
fprintf(stderr, "version: %ld\n", vol->version);
fprintf(stderr, "title: %s\n", vol->title);
fprintf(stderr, "time: %s\n", vol->time);
fprintf(stderr, "seqcount: %ld\n", vol->seqcount);
fprintf(stderr, "longest: %ld\n", vol->longest);
fprintf(stderr, "symcount: %ld\n", vol->symcount);
fprintf(stderr, "masked_title: %s\n", vol->masked_title);
fprintf(stderr, "masked_length: %ld\n", vol->masked_length);
fprintf(stderr, "masked_nseq: %ld\n", vol->masked_nseq);
fprintf(stderr, "masked_maxoid: %ld\n", vol->masked_maxoid);
fprintf(stderr, "masked_memb_bit: %ld\n", vol->masked_memb_bit);
fprintf(stderr, "masked_mskfile: %s\n", vol->masked_mskfile);
fprintf(stderr, "offset_xhr: %ld\n", vol->offset_xhr);
fprintf(stderr, "offset_xsq: %ld\n", vol->offset_xsq);
fprintf(stderr, "offset_amb: %ld\n", vol->offset_amb);
fprintf(stderr, "len_xin: %ld\n", vol->len_xin);
fprintf(stderr, "len_xsq: %ld\n", vol->len_xsq);
fprintf(stderr, "len_xhr: %ld\n", vol->len_xhr);
fprintf(stderr, "adr_xin: %p\n", vol->adr_xin);
fprintf(stderr, "\n");
}
al_info_t * db_read_alias(long symtype, const char * basename)
{
// open an alias file and read contents
char * filename = (char*)xmalloc(strlen(basename)+5);
strcpy(filename, basename);
strcat(filename, ((symtype==1)||(symtype==2)||(symtype==5)) ? ".pal" : ".nal");
FILE * db_file_xal = fopen(filename, "r");
free(filename);
if (db_file_xal)
{
// al file exists
al_info_t * al_info = (al_info_t *) xmalloc(sizeof(al_info_t));
al_info->dblist_len = 0;
al_info->oidlist_len = 0;
al_info->title = NULL;
al_info->dblist = NULL;
al_info->oidlist = NULL;
al_info->length = 0;
al_info->nseq = 0;
al_info->maxoid = 0;
al_info->memb_bit = 0;
char line[10000];
while (fgets(line, 10000, db_file_xal))
{
if (strncmp(line, "TITLE ", 6)== 0)
{
long start = strspn(line+6, " \t");
long titlelen = strcspn(line+6+start, "\r\n");
al_info->title = (char*) xmalloc(titlelen + 1);
strncpy(al_info->title, line+6+start, titlelen);
al_info->title[titlelen] = 0;
}
else if (strncmp(line, "DBLIST", 6) == 0)
{
al_info->dblist_len = getnames(line+6, & al_info->dblist);
}
else if (strncmp(line, "OIDLIST", 7) == 0)
{
al_info->oidlist_len = getnames(line+7, & al_info->oidlist);
}
else if (strncmp(line, "GILIST", 6) == 0)
{
// not implemented
fatal("GILIST in database alias files not implemented.");
}
else if (strncmp(line, "LENGTH ", 7) == 0)
{
al_info->length = atol(line+7);
}
else if (strncmp(line, "NSEQ ", 5) == 0)
{
al_info->nseq = atol(line+5);
}
else if (strncmp(line, "MAXOID ", 7) == 0)
{
al_info->maxoid = atol(line+7);
}
else if (strncmp(line, "MEMB_BIT ", 9) == 0)
{
al_info->memb_bit = atol(line+9);
}
}
if (!al_info->title)
al_info->title = strdup(basename);
fclose(db_file_xal);
// show_alias_info(al_info);
return al_info;
}
else
{
return NULL;
}
}
void db_close_al(al_info_t * a)
{
if (a->title)
{
free(a->title);
a->title = NULL;
}
if (a->dblist)
{
for (long i=0; i<a->dblist_len; i++)
free(a->dblist[i]);
free(a->dblist);
a->dblist = NULL;
}
if (a->oidlist)
{
for (long i=0; i<a->oidlist_len; i++)
free(a->oidlist[i]);
free(a->oidlist);
a->oidlist = NULL;
}
}
long db_open_xin(long symtype, const char * basename, db_volume_t * volume)
{
db_volume_init(volume);
volume->basename = strdup(basename);
char * name_pin = (char*)xmalloc(strlen(basename)+5);
strcpy(name_pin, basename);
char * name_phr = (char*)xmalloc(strlen(basename)+5);
strcpy(name_phr, basename);
char * name_psq = (char*)xmalloc(strlen(basename)+5);
strcpy(name_psq, basename);
if ((symtype==1)||(symtype==2)||(symtype==5))
{
strcat(name_pin, ".pin");
strcat(name_phr, ".phr");
strcat(name_psq, ".psq");
}
else
{
strcat(name_pin, ".nin");
strcat(name_phr, ".nhr");
strcat(name_psq, ".nsq");
}
volume->fd_xin = open(name_pin, O_RDONLY);
if (volume->fd_xin < 0)
fatal("Unable to open file %s.", name_pin);
volume->len_xin = lseek(volume->fd_xin, 0, SEEK_END);
volume->adr_xin = (char *) mmap(0, volume->len_xin, PROT_READ, MAP_SHARED, volume->fd_xin, 0);
if (volume->adr_xin == MAP_FAILED)
fatal("Unable to map file %s in memory. It may be empty or too large.", name_pin);
volume->fd_xhr = open(name_phr, O_RDONLY);
if (volume->fd_xhr < 0)
fatal("Unable to open file %s.", name_phr);
volume->len_xhr = lseek(volume->fd_xhr, 0, SEEK_END);
volume->fd_xsq = open(name_psq, O_RDONLY, 0);
if (volume->fd_xsq < 0)
fatal("Unable to open file %s.\n", name_psq);
volume->len_xsq = lseek(volume->fd_xsq, 0, SEEK_END);
char * p = (char*) volume->adr_xin;
volume->version = bswap_32(*(UINT32*)p);
if (volume->version != 4)
fatal("Illegal database version (must be 4).");
p += 4;
volume->symtype = bswap_32(*(UINT32*)p);
p += 4;
long titlelen = bswap_32(*(UINT32*)p);
p += 4;
volume->title = (char*) xmalloc(titlelen+1);
strncpy(volume->title, p, titlelen);
volume->title[titlelen] = 0;
p += titlelen;
unsigned datelen = bswap_32(*(UINT32*)p);
p += 4;
volume->time = (char*) xmalloc(datelen+1);
strncpy(volume->time, p, datelen);
volume->time[datelen] = 0;
p += datelen;
if ((long)p & 3)
p++;
if ((long)p & 3)
p++;
if ((long)p & 3)
p++;
volume->seqcount = bswap_32(*(UINT32*)p);
p += 4;
volume->symcount = *(unsigned long*)p;
p += 8;
volume->longest = bswap_32(*(UINT32*)p);
p += 4;
volume->offset_xhr = p - (char*)volume->adr_xin;
volume->offset_xsq = volume->offset_xhr + 4 * (volume->seqcount + 1);
volume->offset_amb = volume->offset_xsq + 4 * (volume->seqcount + 1);
free(name_pin);
free(name_phr);
free(name_psq);
return 1;
}
char * get_path(const char * basename)
{
const char * p = basename;
char * path;
long pathlen = 0;
while (char c = *p++)
if (c == '/')
pathlen = p - basename;
path = (char*) xmalloc(pathlen + 1);
strncpy(path, basename, pathlen);
path[pathlen] = 0;
return path;
}
char * addpath(char * path, char * base)
{
long pathlen = strlen(path);
long baselen = strlen(base);
char * both = (char*) xmalloc(pathlen + baselen + 1);
strcpy(both, path);
strcat(both, base);
return both;
}
void seqno_volume(long seqno, long * sp, db_volume_t * * vp)
{
// find the volume that seqno belongs to
// linear search
long s = seqno;
db_volume_t * v = db_volume;
db_volume_t * e = v + db_main.volumecount;
while(v < e)
{
if (s < v->seqcount)
{
*vp = v;
*sp = s;
return;
}
s -= v->seqcount;
v++;
}
*vp = 0;
*sp = 0;
fatal("Cant find database volume.");
}
long db_getvolume(long seqno)
{
long dummy;
db_volume_t * vp;
seqno_volume(seqno, & dummy, & vp);
return vp - db_volume;
}
void db_open_msk(db_volume_t * v)
{
// fprintf(stderr, "Opening msk file: %s\n", v->masked_mskfile);
// fprintf(stderr, "Maxoid: %ld\n", v->masked_maxoid);
v->fd_msk = open(v->masked_mskfile, O_RDONLY);
if (v->fd_msk < 0)
fatal("Unable to open msk file %s.", v->masked_mskfile);
v->len_msk = lseek(v->fd_msk, 0, SEEK_END);
v->adr_msk = (unsigned char *) mmap(0, v->len_msk, PROT_READ, MAP_SHARED, v->fd_msk, 0);
if (v->adr_msk == MAP_FAILED)
fatal("Unable to mmap msk file %s.", v->masked_mskfile);
}
long db_check_msk(long seqno)
{
long s;
db_volume_t * v;
long member = 1;
if (db_main.memb_bit)
{
member = 0;
seqno_volume(seqno, & s, & v);
if (s <= v->masked_maxoid)
{
long byteno = s >> 3;
long bitno = s & 7;
long byte = *(v->adr_msk + 4 + byteno);
member = (byte >> (7-bitno)) & 1;
}
}
return member;
}
void db_set_masked_info(db_volume_t * v, al_info_t * ai, char * mskfile)
{
v->masked_mskfile = addpath(db_main.path, mskfile);
v->masked_title = strdup(ai->title);
v->masked_length = ai->length;
v->masked_nseq = ai->nseq;
v->masked_maxoid = ai->maxoid;
v->masked_memb_bit = ai->memb_bit;
}
long db_check_taxid(long taxid)
{
if (db_main.taxid_bitmap_address)
{
long byteno = taxid / 8;
long bitno = taxid & 7;
if (byteno < db_main.taxid_bitmap_size)
return (db_main.taxid_bitmap_address[byteno] >> bitno) & 1;
else
return 0;
}
else
return 1;
}
void db_read_taxid_file(char * filename)
{
db_main.taxid_filename = strdup(filename);
db_main.taxid_file = fopen(filename, "r");
if (!db_main.taxid_file)
fatal("Unable to open taxid file %s.", filename);
db_main.taxid_bitmap_size = 64*1024;
db_main.taxid_bitmap_address = (unsigned char*) xmalloc(db_main.taxid_bitmap_size);
memset(db_main.taxid_bitmap_address, 0, db_main.taxid_bitmap_size);
long lines = 0;
unsigned long taxid;
while(fscanf(db_main.taxid_file, "%lu\n", & taxid) > 0)
{
// fprintf(stderr, "read taxid: %lu\n", taxid);
long byteno = taxid / 8;
long bitno = taxid & 7;
if (byteno >= db_main.taxid_bitmap_size)
{
long old = db_main.taxid_bitmap_size;
db_main.taxid_bitmap_size = byteno+1;
db_main.taxid_bitmap_address = (unsigned char *)
xrealloc(db_main.taxid_bitmap_address,
db_main.taxid_bitmap_size);
memset(db_main.taxid_bitmap_address+old, 0, db_main.taxid_bitmap_size-old);
}
unsigned char v = db_main.taxid_bitmap_address[byteno];
db_main.taxid_bitmap_address[byteno] = (unsigned char)(v | (1 << bitno));
lines++;
}
// fprintf(out, "Read %ld taxid's.\n", lines);
fclose(db_main.taxid_file);
}
void db_open(long symtype, const char * basename, char * taxidfilename)
{
al_info_t * ai = NULL;
db_init(& db_main);
db_main.basename = strdup(basename);
db_main.symtype = symtype;
db_main.path = get_path(basename);
long vol = 0;
if ((ai = db_read_alias(symtype, basename)))
{
db_main.title = strdup(ai->title);
db_main.memb_bit = ai->memb_bit;
for(long i=0; i<ai->dblist_len; i++)
{
al_info_t * ai2 = NULL;
char * basename2 = addpath(db_main.path, ai->dblist[i]);
if ((ai2 = db_read_alias(symtype, basename2)))
{
if (ai->memb_bit)
{
if ((ai2->oidlist_len != 1) || (ai2->dblist_len != 1))
fatal("Illegal alias file (2).");
}
for(long j=0; j < ai2->dblist_len; j++)
{
char * basename3 = addpath(db_main.path, ai2->dblist[j]);
db_volume_init(db_volume + vol);
db_open_xin(symtype, basename3, db_volume + vol);
if (ai->memb_bit)
{
db_set_masked_info(db_volume + vol, ai2, ai2->oidlist[j]);
db_open_msk(db_volume + vol);
}
// show_volume_info(db_volume+vol);
db_main.seqcount += db_volume[vol].seqcount;
db_main.symcount += db_volume[vol].symcount;
db_main.masked_seqcount += db_volume[vol].masked_nseq;
db_main.masked_symcount += db_volume[vol].masked_length;
if ( db_volume[vol].longest > db_main.longest )
db_main.longest = db_volume[vol].longest;
vol++;
free(basename3);
basename3 = NULL;
}
db_close_al(ai2);
free(ai2);
ai2 = NULL;
}
else
{
if (ai->oidlist_len == 0)
{
ai->memb_bit = 0;
db_main.memb_bit = 0;
}
if (ai->memb_bit)
{
if ((ai->oidlist_len != 1) || (ai->dblist_len != 1))
fatal("Illegal alias file (1).");
}
db_volume_init(db_volume + vol);
db_open_xin(symtype, basename2, db_volume+vol);
if (ai->memb_bit)
{
db_set_masked_info(db_volume + vol, ai, ai->oidlist[i]);
db_open_msk(db_volume + vol);
}
// show_volume_info(db_volume+vol);
db_main.seqcount += db_volume[vol].seqcount;
db_main.symcount += db_volume[vol].symcount;
db_main.masked_seqcount += db_volume[vol].masked_nseq;
db_main.masked_symcount += db_volume[vol].masked_length;
if ( db_volume[vol].longest > db_main.longest )
db_main.longest = db_volume[vol].longest;
vol++;
}
free(basename2);
basename2 = NULL;
}
db_close_al(ai);
free(ai);
ai = NULL;
}
else
{
db_volume_init(db_volume);
db_open_xin(symtype, basename, db_volume);
// show_volume_info(db_volume+vol);
vol++;
db_main.memb_bit = 0;
db_main.title = strdup(db_volume[0].title);
db_main.seqcount = db_volume[0].seqcount;
db_main.symcount = db_volume[0].symcount;
db_main.longest = db_volume[0].longest;
}
db_main.volumecount = vol;
db_main.version = db_volume[0].version;
db_main.time = strdup(db_volume[0].time);
if(!db_main.memb_bit)
{
db_main.masked_seqcount = db_main.seqcount;
db_main.masked_symcount = db_main.symcount;
}
// show_db_info(&db_main);
/* prepare nucleotide decompression table */
for(int b=0; b<256; b++)
{
unsigned int unpacked;
for(long i=0; i<4; i++)
((unsigned char*)(&unpacked))[i] = (unsigned char)(1 << ((b >> ((3-(i&3))<<1)) & 3));
decompress_nt[b] = unpacked;
}
if (taxidfilename)
db_read_taxid_file(taxidfilename);
}
void db_volume_close(db_volume_t * v)
{
if(v->basename)
{
free(v->basename);
v->basename = NULL;
}
if(v->title)
{
free(v->title);
v->title = NULL;
}
if(v->time)
{
free(v->time);
v->time = NULL;
}
if(v->masked_title)
{
free(v->masked_title);
v->masked_title = NULL;
}
if(v->masked_mskfile)
{
free(v->masked_mskfile);
v->masked_mskfile = NULL;
}
munmap(v->adr_xin, v->len_xin);
if (v->fd_msk)
{
munmap(v->adr_msk, v->len_msk);
close(v->fd_msk);
}
if (v->map_seq_address)
{
munmap(v->map_seq_address, v->map_seq_length);
v->map_seq_address = NULL;
}
if (v->map_hdr_address)
{
munmap(v->map_hdr_address, v->map_hdr_length);
v->map_hdr_address = NULL;
}
close(v->fd_xin);
close(v->fd_xhr);
close(v->fd_xsq);
}
void db_close()
{
for(long i=0; i<db_main.volumecount;i++)
{
db_volume_close(db_volume + i);
}
if (db_main.path)
{
free(db_main.path);
db_main.path = NULL;
}
if (db_main.basename)
{
free(db_main.basename);
db_main.basename = NULL;
}
if (db_main.title)
{
free(db_main.title);
db_main.title = NULL;
}