Debian Codegraph @

libkmer/existDB-state.C

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include "existDB.H"
6 #include "bio++.H"
7
8
9 const char magic[16] = { 'e', 'x', 'i', 's', 't', 'D', 'B', '2',
10 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
11
12
13 void
14 existDB::saveState(char const *filename) {
15 char cigam[16] = { 0 };
16
17 errno = 0;
18 FILE *F = fopen(filename, "wb");
19 if (errno) {
20 fprintf(stderr, "Can't open '%s' for writing\n%s\n", filename, strerror(errno));
21 exit(1);
22 }
23
24 strncpy(cigam, magic, 16);
25
26 if (_compressedHash)
27 cigam[8] = 'h';
28 if (_compressedBucket)
29 cigam[9] = 'b';
30 if (_compressedCounts)
31 cigam[10] = 'c';
32
33 if (_isForward)
34 cigam[11] = 'F';
35 if (_isCanonical)
36 cigam[11] = 'C';
37
38 fwrite(cigam, sizeof(char), 16, F);
39
40 fwrite(&_merSizeInBases, sizeof(uint32), 1, F);
41 fwrite(&_shift1, sizeof(uint32), 1, F);
42 fwrite(&_shift2, sizeof(uint32), 1, F);
43 fwrite(&_mask1, sizeof(uint64), 1, F);
44 fwrite(&_mask2, sizeof(uint64), 1, F);
45 fwrite(&_hshWidth, sizeof(uint32), 1, F); // only valid if _compressedHash
46 fwrite(&_chkWidth, sizeof(uint32), 1, F); // only valid if _compressedBucket
47 fwrite(&_cntWidth, sizeof(uint32), 1, F); // only valid if _compressedCounts
48
49 fwrite(&_hashTableWords, sizeof(uint64), 1, F);
50 fwrite(&_bucketsWords, sizeof(uint64), 1, F);
51 fwrite(&_countsWords, sizeof(uint64), 1, F);
52
53 fwrite(_hashTable, sizeof(uint64), _hashTableWords, F);
54 fwrite(_buckets, sizeof(uint64), _bucketsWords, F);
55 fwrite(_counts, sizeof(uint64), _countsWords, F);
56
57 fclose(F);
58
59 if (errno) {
60 fprintf(stderr, "existDB::saveState()-- Write failure.\n%s\n", strerror(errno));
61 exit(1);
62 }
63 }
64
65
66
67 bool
68 existDB::loadState(char const *filename,
69 bool beNoisy,
70 bool loadData) {
71 char cigam[16];
72
73 errno = 0;
74 FILE *F = fopen(filename, "rb");
75 if (errno) {
76 //fprintf(stderr, "Can't open '%s' for reading pre-built existDB\n%s\n", strerror(errno));
77 return(false);
78 }
79
80 fread(cigam, sizeof(char), 16, F);
81
82 _compressedHash = false;
83 _compressedBucket = false;
84 _compressedCounts = false;
85 _isForward = false;
86 _isCanonical = false;
87
88 if (cigam[8] == 'h')
89 _compressedHash = true;
90 if (cigam[9] == 'b')
91 _compressedBucket = true;
92 if (cigam[10] == 'c')
93 _compressedCounts = true;
94
95 if (cigam[11] == 'F')
96 _isForward = true;
97 if (cigam[11] == 'C')
98 _isCanonical = true;
99
100 cigam[ 8] = ' ';
101 cigam[ 9] = ' ';
102 cigam[10] = ' ';
103 cigam[11] = ' ';
104
105 if (strncmp(magic, cigam, 16) != 0) {
106 if (beNoisy) {
107 fprintf(stderr, "existDB::loadState()-- Not an existDB binary file, maybe a sequence file?\n");
108 fprintf(stderr, "existDB::loadState()-- Read '%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c'\n",
109 cigam[0], cigam[1], cigam[2], cigam[3],
110 cigam[4], cigam[5], cigam[6], cigam[7],
111 cigam[8], cigam[9], cigam[10], cigam[11],
112 cigam[12], cigam[13], cigam[14], cigam[15]);
113 fprintf(stderr, "existDB::loadState()-- Expected '%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c'\n",
114 magic[0], magic[1], magic[2], magic[3],
115 magic[4], magic[5], magic[6], magic[7],
116 magic[8], magic[9], magic[10], magic[11],
117 magic[12], magic[13], magic[14], magic[15]);
118 }
119
120 fclose(F);
121 return(false);
122 }
123
124 fread(&_merSizeInBases, sizeof(uint32), 1, F);
125 fread(&_shift1, sizeof(uint32), 1, F);
126 fread(&_shift2, sizeof(uint32), 1, F);
127 fread(&_mask1, sizeof(uint64), 1, F);
128 fread(&_mask2, sizeof(uint64), 1, F);
129 fread(&_hshWidth, sizeof(uint32), 1, F); // only valid if _compressedHash
130 fread(&_chkWidth, sizeof(uint32), 1, F); // only valid if _compressedBucket
131 fread(&_cntWidth, sizeof(uint32), 1, F); // only valid if _compressedCounts
132
133 fread(&_hashTableWords, sizeof(uint64), 1, F);
134 fread(&_bucketsWords, sizeof(uint64), 1, F);
135 fread(&_countsWords, sizeof(uint64), 1, F);
136
137 _hashTable = 0L;
138 _buckets = 0L;
139 _counts = 0L;
140
141 if (loadData) {
142 _hashTable = new uint64 [_hashTableWords];
143 _buckets = new uint64 [_bucketsWords];
144
145 if (_countsWords > 0)
146 _counts = new uint64 [_countsWords];
147
148 fread(_hashTable, sizeof(uint64), _hashTableWords, F);
149 fread(_buckets, sizeof(uint64), _bucketsWords, F);
150
151 if (_countsWords > 0)
152 fread(_counts, sizeof(uint64), _countsWords, F);
153 }
154
155 fclose(F);
156
157 if (errno) {
158 fprintf(stderr, "existDB::loadState()-- Read failure.\n%s\n", strerror(errno));
159 exit(1);
160 }
161
162 return(true);
163 }
164
165
166 void
167 existDB::printState(FILE *stream) {
168
169 fprintf(stream, "merSizeInBases: "uint32FMT"\n", _merSizeInBases);
170 fprintf(stream, "tableBits "uint32FMT"\n", 2 * _merSizeInBases - _shift1);
171 fprintf(stream, "-----------------\n");
172 fprintf(stream, "_hashTableWords "uint64FMT" ("uint64FMT" KB)\n", _hashTableWords, _hashTableWords >> 7);
173 fprintf(stream, "_bucketsWords "uint64FMT" ("uint64FMT" KB)\n", _bucketsWords, _bucketsWords >> 7);
174 fprintf(stream, "_countsWords "uint64FMT" ("uint64FMT" KB)\n", _countsWords, _countsWords >> 7);
175 fprintf(stream, "-----------------\n");
176 fprintf(stream, "_shift1: "uint32FMT"\n", _shift1);
177 fprintf(stream, "_shift2 "uint32FMT"\n", _shift2);
178 fprintf(stream, "_mask1 "uint64HEX"\n", _mask1);
179 fprintf(stream, "_mask2 "uint64HEX"\n", _mask2);
180
181 if (_compressedHash) {
182 fprintf(stream, "_compressedHash true\n");
183 fprintf(stream, "_hshWidth "uint32FMT"\n", _hshWidth);
184 } else {
185 fprintf(stream, "_compressedHash false\n");
186 fprintf(stream, "_hshWidth undefined\n");
187 }
188
189 if (_compressedBucket) {
190 fprintf(stream, "_compressedBucket true\n");
191 fprintf(stream, "_chkWidth "uint32FMT"\n", _chkWidth);
192 } else {
193 fprintf(stream, "_compressedBucket false\n");
194 fprintf(stream, "_chkWidth undefined\n");
195 }
196
197 if (_compressedCounts) {
198 fprintf(stream, "_compressedCount true\n");
199 fprintf(stream, "_cntWidth "uint32FMT"\n", _cntWidth);
200 } else {
201 fprintf(stream, "_compressedCount false\n");
202 fprintf(stream, "_cntWidth undefined\n");
203 }
204 }
205