Debian Codegraph @

libkmer/positionDB-file.C

raw
1 #include "positionDB.H"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <errno.h>
7
8 static
9 char magic[16] = { 'p', 'o', 's', 'i', 't', 'i', 'o', 'n', 'D', 'B', '.', 'v', '1', ' ', ' ', ' ' };
10 static
11 char faild[16] = { 'p', 'o', 's', 'i', 't', 'i', 'o', 'n', 'D', 'B', 'f', 'a', 'i', 'l', 'e', 'd' };
12
13 void
14 positionDB::saveState(char const *filename) {
15
16 fprintf(stderr, "Saving positionDB to '%s'\n", filename);
17
18 errno = 0;
19 int F = open(filename, O_RDWR | O_CREAT | O_LARGEFILE,
20 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
21 if (errno) {
22 fprintf(stderr, "Can't open '%s' for writing positionDB.\n%s\n", filename, strerror(errno));
23 exit(1);
24 }
25
26 bool magicFirst = false;
27
28 // Test if this is a pipe. If so, we write the magic first,
29 // otherwise we write the magic last.
30 //
31 errno = 0;
32 lseek(F, 0, SEEK_SET);
33 if (errno == ESPIPE)
34 magicFirst = true;
35
36 if (magicFirst)
37 write(F, magic, sizeof(char) * 16);
38 else
39 write(F, faild, sizeof(char) * 16);
40
41 if (errno) {
42 fprintf(stderr, "positionDB::saveState()-- Write failure on magic first.\n%s\n", strerror(errno));
43 exit(1);
44 }
45
46 // If only to be completely annoying and anal, we clear the
47 // pointers before we write the data. Sure, we could just write
48 // the stuff we care about, but this is easier. This is easier.
49 // Before you go rip out this stuff, remember that you can now
50 // checksum the resulting files. So don't do it.
51 //
52 uint32 *bs = _bucketSizes;
53 uint64 *cb = _countingBuckets;
54 uint64 *hp = _hashTable_BP;
55 uint32 *hw = _hashTable_FW;
56 uint64 *bu = _buckets;
57 uint64 *ps = _positions;
58 uint64 *he = _hashedErrors;
59
60 _bucketSizes = 0L;
61 _countingBuckets = 0L;
62 _hashTable_BP = (uint64 *)((_hashTable_BP) ? uint64ONE : uint64ZERO);
63 _hashTable_FW = (uint32 *)((_hashTable_FW) ? uint32ONE : uint32ZERO);
64 _buckets = 0L;
65 _positions = 0L;
66 _hashedErrors = 0L;
67
68 safeWrite(F, this, "this", sizeof(positionDB) * 1);
69
70 _bucketSizes = bs;
71 _countingBuckets = cb;
72 _hashTable_BP = hp;
73 _hashTable_FW = hw;
74 _buckets = bu;
75 _positions = ps;
76 _hashedErrors = he;
77
78 if (_hashTable_BP) {
79 safeWrite(F, _hashTable_BP, "_hashTable_BP", sizeof(uint64) * (_tableSizeInEntries * _hashWidth / 64 + 1));
80 } else {
81 safeWrite(F, _hashTable_FW, "_hashTable_FW", sizeof(uint32) * (_tableSizeInEntries + 1));
82 }
83
84 safeWrite(F, _buckets, "_buckets", sizeof(uint64) * (_numberOfDistinct * _wFin / 64 + 1));
85 safeWrite(F, _positions, "_positions", sizeof(uint64) * (_numberOfEntries * _posnWidth / 64 + 1));
86 safeWrite(F, _hashedErrors, "_hashedErrors", sizeof(uint64) * (_hashedErrorsLen));
87
88 if (magicFirst == false) {
89 lseek(F, 0, SEEK_SET);
90 if (errno) {
91 fprintf(stderr, "positionDB::saveState()-- Failed to seek to start of file -- write failed.\n%s\n", strerror(errno));
92 exit(1);
93 }
94
95 write(F, magic, sizeof(char) * 16);
96 if (errno) {
97 fprintf(stderr, "positionDB::saveState()-- Write failure on magic last.\n%s\n", strerror(errno));
98 exit(1);
99 }
100 }
101
102 close(F);
103 }
104
105
106 bool
107 positionDB::loadState(char const *filename, bool beNoisy, bool loadData) {
108 char cigam[16] = { 0 };
109
110 fprintf(stderr, "Loading positionDB from '%s'\n", filename);
111
112 errno = 0;
113 int F = open(filename, O_RDONLY | O_LARGEFILE, 0);
114 if (errno) {
115 fprintf(stderr, "Can't open '%s' for reading pre-built positionDB: %s\n", filename, strerror(errno));
116 return(false);
117 }
118
119 safeRead(F, cigam, "Magic Number", sizeof(char) * 16);
120
121 if (strncmp(faild, cigam, 16) == 0) {
122 if (beNoisy) {
123 fprintf(stderr, "positionDB::loadState()-- Incomplete positionDB binary file.\n");
124 fprintf(stderr, "positionDB::loadState()-- Read '%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c'\n",
125 cigam[0], cigam[1], cigam[2], cigam[3],
126 cigam[4], cigam[5], cigam[6], cigam[7],
127 cigam[8], cigam[9], cigam[10], cigam[11],
128 cigam[12], cigam[13], cigam[14], cigam[15]);
129 fprintf(stderr, "positionDB::loadState()-- Expected '%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c'\n",
130 magic[0], magic[1], magic[2], magic[3],
131 magic[4], magic[5], magic[6], magic[7],
132 magic[8], magic[9], magic[10], magic[11],
133 magic[12], magic[13], magic[14], magic[15]);
134 }
135 close(F);
136 return(false);
137 } else if (strncmp(magic, cigam, 16) != 0) {
138 if (beNoisy) {
139 fprintf(stderr, "positionDB::loadState()-- Not a positionDB binary file, maybe a sequence file?\n");
140 fprintf(stderr, "positionDB::loadState()-- Read '%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c'\n",
141 cigam[0], cigam[1], cigam[2], cigam[3],
142 cigam[4], cigam[5], cigam[6], cigam[7],
143 cigam[8], cigam[9], cigam[10], cigam[11],
144 cigam[12], cigam[13], cigam[14], cigam[15]);
145 fprintf(stderr, "positionDB::loadState()-- Expected '%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c'\n",
146 magic[0], magic[1], magic[2], magic[3],
147 magic[4], magic[5], magic[6], magic[7],
148 magic[8], magic[9], magic[10], magic[11],
149 magic[12], magic[13], magic[14], magic[15]);
150 }
151
152 close(F);
153 return(false);
154 }
155
156 safeRead(F, this, "positionDB", sizeof(positionDB) * 1);
157
158 _bucketSizes = 0L;
159 _countingBuckets = 0L;
160 _buckets = 0L;
161 _positions = 0L;
162 _hashedErrors = 0L;
163
164 if (loadData) {
165 uint64 hs = _tableSizeInEntries * _hashWidth / 64 + 1;
166 uint64 bs = _numberOfDistinct * _wFin / 64 + 1;
167 uint64 ps = _numberOfEntries * _posnWidth / 64 + 1;
168
169 if (_hashTable_BP) {
170 _hashTable_BP = new uint64 [hs];
171 _hashTable_FW = 0L;
172 safeRead(F, _hashTable_BP, "_hashTable_BP", sizeof(uint64) * hs);
173 } else {
174 _hashTable_BP = 0L;
175 _hashTable_FW = new uint32 [_tableSizeInEntries + 1];
176 safeRead(F, _hashTable_FW, "_hashTable_FW", sizeof(uint32) * (_tableSizeInEntries + 1));
177 }
178
179 _buckets = new uint64 [bs];
180 _positions = new uint64 [ps];
181 _hashedErrors = new uint64 [_hashedErrorsMax];
182
183 safeRead(F, _buckets, "_buckets", sizeof(uint64) * bs);
184 safeRead(F, _positions, "_positions", sizeof(uint64) * ps);
185 safeRead(F, _hashedErrors, "_hashedErrors", sizeof(uint64) * _hashedErrorsLen);
186 }
187
188 close(F);
189
190 return(true);
191 }
192
193
194
195 void
196 positionDB::printState(FILE *stream) {
197 fprintf(stream, "merSizeInBases: "uint32FMT"\n", _merSizeInBases);
198 fprintf(stream, "merSkipInBases: "uint32FMT"\n", _merSkipInBases);
199 fprintf(stream, "tableSizeInBits: "uint32FMT"\n", _tableSizeInBits);
200 fprintf(stream, "tableSizeInEntries: "uint64FMT"\n", _tableSizeInEntries);
201 fprintf(stream, "hashWidth: "uint32FMT"\n", _hashWidth);
202 fprintf(stream, "chckWidth: "uint32FMT"\n", _chckWidth);
203 fprintf(stream, "posnWidth: "uint32FMT"\n", _posnWidth);
204 fprintf(stream, "numberOfMers: "uint64FMT"\n", _numberOfMers);
205 fprintf(stream, "numberOfPositions: "uint64FMT"\n", _numberOfPositions);
206 fprintf(stream, "numberOfDistinct: "uint64FMT"\n", _numberOfDistinct);
207 fprintf(stream, "numberOfUnique: "uint64FMT"\n", _numberOfUnique);
208 fprintf(stream, "numberOfEntries: "uint64FMT"\n", _numberOfEntries);
209 fprintf(stream, "maximumEntries: "uint64FMT"\n", _maximumEntries);
210 }
211