-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathcreatedb.cpp
More file actions
executable file
·399 lines (366 loc) · 16.4 KB
/
createdb.cpp
File metadata and controls
executable file
·399 lines (366 loc) · 16.4 KB
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
/*
* createdb
* written by Martin Steinegger <martin.steinegger@snu.ac.kr>.
* modified by Maria Hauser <mhauser@genzentrum.lmu.de> (splitting into sequences/headers databases)
* modified by Milot Mirdita <milot@mirdita.de>
*/
#include "FileUtil.h"
#include "DBWriter.h"
#include "Debug.h"
#include "Util.h"
#include "KSeqWrapper.h"
#include "itoa.h"
int createdb(int argc, const char **argv, const Command& command) {
Parameters &par = Parameters::getInstance();
par.parseParameters(argc, argv, command, true, Parameters::PARSE_VARIADIC, 0);
std::vector<std::string> filenames(par.filenames);
std::string dataFile = filenames.back();
filenames.pop_back();
if (Util::endsWith(".tsv", filenames[0])) {
if (filenames.size() > 1) {
Debug(Debug::ERROR) << "Only one tsv file can be given\n";
EXIT(EXIT_FAILURE);
}
std::string tsv = filenames.back();
filenames.pop_back();
FILE* file = FileUtil::openFileOrDie(tsv.c_str(), "r", true);
char* line = NULL;
size_t len = 0;
ssize_t read;
while ((read = getline(&line, &len, file)) != -1) {
if (line[read - 1] == '\n') {
line[read - 1] = '\0';
read--;
}
filenames.push_back(line);
}
free(line);
fclose(file);
}
for (size_t i = 0; i < filenames.size(); i++) {
if (FileUtil::directoryExists(filenames[i].c_str()) == true) {
Debug(Debug::ERROR) << "File " << filenames[i] << " is a directory\n";
EXIT(EXIT_FAILURE);
}
}
bool dbInput = false;
if (FileUtil::fileExists(par.db1dbtype.c_str()) == true) {
if (filenames.size() > 1) {
Debug(Debug::ERROR) << "Only one database can be used with database input\n";
EXIT(EXIT_FAILURE);
}
dbInput = true;
par.createdbMode = Parameters::SEQUENCE_SPLIT_MODE_HARD;
}
int dbType = -1;
if (par.dbType == 2) {
dbType = Parameters::DBTYPE_NUCLEOTIDES;
} else if(par.dbType == 1) {
dbType = Parameters::DBTYPE_AMINO_ACIDS;
}
std::string indexFile = dataFile + ".index";
if (par.createdbMode == Parameters::SEQUENCE_SPLIT_MODE_SOFT && par.shuffleDatabase) {
Debug(Debug::WARNING) << "Shuffle database cannot be combined with --createdb-mode 0\n";
Debug(Debug::WARNING) << "We recompute with --shuffle 0\n";
par.shuffleDatabase = false;
}
if (par.createdbMode == Parameters::SEQUENCE_SPLIT_MODE_SOFT && par.filenames[0] == "stdin") {
Debug(Debug::WARNING) << "Stdin input cannot be combined with --createdb-mode 0\n";
Debug(Debug::WARNING) << "We recompute with --createdb-mode 1\n";
par.createdbMode = Parameters::SEQUENCE_SPLIT_MODE_HARD;
}
const unsigned int shuffleSplits = par.shuffleDatabase ? 32 : 1;
if (par.createdbMode == Parameters::SEQUENCE_SPLIT_MODE_SOFT && par.compressed) {
Debug(Debug::WARNING) << "Compressed database cannot be combined with --createdb-mode 0\n";
Debug(Debug::WARNING) << "We recompute with --compressed 0\n";
par.compressed = 0;
}
std::string hdrDataFile = dataFile + "_h";
std::string hdrIndexFile = dataFile + "_h.index";
unsigned int entries_num = 0;
size_t sampleCount = 0;
const char newline = '\n';
const size_t testForNucSequence = 100;
size_t isNuclCnt = 0;
Debug::Progress progress;
std::vector<unsigned int>* sourceLookup = new std::vector<unsigned int>[shuffleSplits]();
for (size_t i = 0; i < shuffleSplits; ++i) {
sourceLookup[i].reserve(16384);
}
Debug(Debug::INFO) << "Converting sequences\n";
std::string sourceFile = dataFile + ".source";
redoComputation:
FILE *source = fopen(sourceFile.c_str(), "w");
if (source == NULL) {
Debug(Debug::ERROR) << "Cannot open " << sourceFile << " for writing\n";
EXIT(EXIT_FAILURE);
}
DBWriter hdrWriter(hdrDataFile.c_str(), hdrIndexFile.c_str(), shuffleSplits, par.compressed, Parameters::DBTYPE_GENERIC_DB);
hdrWriter.open();
DBWriter seqWriter(dataFile.c_str(), indexFile.c_str(), shuffleSplits, par.compressed, (dbType == -1) ? Parameters::DBTYPE_OMIT_FILE : dbType );
seqWriter.open();
size_t headerFileOffset = 0;
size_t seqFileOffset = 0;
size_t fileCount = filenames.size();
DBReader<unsigned int>* reader = NULL;
if (dbInput == true) {
reader = new DBReader<unsigned int>(par.db1.c_str(), par.db1Index.c_str(), 1, DBReader<unsigned int>::USE_DATA | DBReader<unsigned int>::USE_INDEX | DBReader<unsigned int>::USE_LOOKUP);
reader->open(DBReader<unsigned int>::LINEAR_ACCCESS);
fileCount = reader->getSize();
}
for (size_t fileIdx = 0; fileIdx < fileCount; fileIdx++) {
unsigned int numEntriesInCurrFile = 0;
std::string header;
header.reserve(1024);
std::string sourceName;
if (dbInput == true) {
unsigned int dbKey = reader->getDbKey(fileIdx);
size_t lookupId = reader->getLookupIdByKey(dbKey);
sourceName = reader->getLookupEntryName(lookupId);
} else {
sourceName = FileUtil::baseName(filenames[fileIdx]);
}
char buffer[4096];
size_t len = snprintf(buffer, sizeof(buffer), "%zu\t%s\n", fileIdx, sourceName.c_str());
int written = fwrite(buffer, sizeof(char), len, source);
if (written != (int) len) {
Debug(Debug::ERROR) << "Cannot write to source file " << sourceFile << "\n";
EXIT(EXIT_FAILURE);
}
KSeqWrapper* kseq = NULL;
if (dbInput == true) {
kseq = new KSeqBuffer(reader->getData(fileIdx, 0), reader->getEntryLen(fileIdx) - 1);
} else {
kseq = KSeqFactory(filenames[fileIdx].c_str());
}
bool resetNotFile = par.createdbMode == Parameters::SEQUENCE_SPLIT_MODE_SOFT && kseq->type != KSeqWrapper::KSEQ_FILE;
if (resetNotFile) {
Debug(Debug::WARNING) << "Only uncompressed fasta files can be used with --createdb-mode 0\n";
Debug(Debug::WARNING) << "We recompute with --createdb-mode 1\n";
}
bool resetIncorrectNewline = false;
if (par.createdbMode == Parameters::SEQUENCE_SPLIT_MODE_SOFT && kseq->type == KSeqWrapper::KSEQ_FILE) {
// get last byte from filenames[fileIdx].c_str()
FILE* fp = fopen(filenames[fileIdx].c_str(), "rb");
if (fp == NULL) {
Debug(Debug::ERROR) << "Cannot open file " << filenames[fileIdx] << "\n";
EXIT(EXIT_FAILURE);
}
int res = fseek(fp, -1, SEEK_END);
if (res != 0) {
Debug(Debug::ERROR) << "Cannot seek at the end of file " << filenames[fileIdx] << "\n";
EXIT(EXIT_FAILURE);
}
int lastChar = fgetc(fp);
if (lastChar == EOF) {
Debug(Debug::ERROR) << "Error reading from " << filenames[fileIdx] << "\n";
EXIT(EXIT_FAILURE);
}
if (fclose(fp) != 0) {
Debug(Debug::ERROR) << "Error closing " << filenames[fileIdx] << "\n";
EXIT(EXIT_FAILURE);
}
if (lastChar != '\n') {
Debug(Debug::WARNING) << "Last byte is not a newline. We recompute with --createdb-mode 1\n";
resetIncorrectNewline = true;
}
}
if (resetNotFile || resetIncorrectNewline) {
par.createdbMode = Parameters::SEQUENCE_SPLIT_MODE_HARD;
progress.reset(SIZE_MAX);
hdrWriter.close();
seqWriter.close();
delete kseq;
if (fclose(source) != 0) {
Debug(Debug::ERROR) << "Cannot close file " << sourceFile << "\n";
EXIT(EXIT_FAILURE);
}
for (size_t i = 0; i < shuffleSplits; ++i) {
sourceLookup[i].clear();
}
goto redoComputation;
}
while (kseq->ReadEntry()) {
progress.updateProgress();
const KSeqWrapper::KSeqEntry &e = kseq->entry;
if (e.name.l == 0) {
Debug(Debug::ERROR) << "Fasta entry " << numEntriesInCurrFile << " is invalid\n";
EXIT(EXIT_FAILURE);
}
// header
if (par.createdbMode == Parameters::SEQUENCE_SPLIT_MODE_HARD) {
header.append(e.name.s, e.name.l);
if (e.comment.l > 0) {
header.append(" ", 1);
header.append(e.comment.s, e.comment.l);
}
std::string headerId = Util::parseFastaHeader(header.c_str());
if (headerId.empty()) {
// An identifier is necessary for these two cases, so we should just give up
Debug(Debug::WARNING) << "Cannot extract identifier from entry " << numEntriesInCurrFile << "\n";
}
header.push_back('\n');
}
unsigned int id = par.identifierOffset + entries_num;
if (dbType == -1) {
// check for the first 10 sequences if they are nucleotide sequences
if (sampleCount < 10 || (sampleCount % 100) == 0) {
if (sampleCount < testForNucSequence) {
size_t cnt = 0;
for (size_t i = 0; i < e.sequence.l; i++) {
switch (toupper(e.sequence.s[i])) {
case 'T':
case 'A':
case 'G':
case 'C':
case 'U':
case 'N':
cnt++;
break;
}
}
const float nuclDNAFraction = static_cast<float>(cnt) / static_cast<float>(e.sequence.l);
if (nuclDNAFraction > 0.9) {
isNuclCnt += true;
}
}
sampleCount++;
}
}
if (par.createdbMode == Parameters::SEQUENCE_SPLIT_MODE_SOFT) {
if (e.newlineCount != 1) {
if (e.newlineCount == 0) {
Debug(Debug::WARNING) << "Fasta entry " << numEntriesInCurrFile << " has no newline character\n";
} else if (e.newlineCount > 1) {
Debug(Debug::WARNING) << "Multiline fasta can not be combined with --createdb-mode 0\n";
}
Debug(Debug::WARNING) << "We recompute with --createdb-mode 1\n";
par.createdbMode = Parameters::SEQUENCE_SPLIT_MODE_HARD;
progress.reset(SIZE_MAX);
hdrWriter.close();
seqWriter.close();
delete kseq;
if (fclose(source) != 0) {
Debug(Debug::ERROR) << "Cannot close file " << sourceFile << "\n";
EXIT(EXIT_FAILURE);
}
for (size_t i = 0; i < shuffleSplits; ++i) {
sourceLookup[i].clear();
}
goto redoComputation;
}
}
// Finally write down the entry
unsigned int splitIdx = id % shuffleSplits;
sourceLookup[splitIdx].emplace_back(fileIdx);
if (par.createdbMode == Parameters::SEQUENCE_SPLIT_MODE_SOFT) {
// +2 to emulate the \n\0
hdrWriter.writeIndexEntry(id, headerFileOffset + e.headerOffset, (e.sequenceOffset-e.headerOffset)+1, 0);
seqWriter.writeIndexEntry(id, seqFileOffset + e.sequenceOffset, e.sequence.l+2, 0);
} else {
hdrWriter.writeData(header.c_str(), header.length(), id, splitIdx);
seqWriter.writeStart(splitIdx);
seqWriter.writeAdd(e.sequence.s, e.sequence.l, splitIdx);
seqWriter.writeAdd(&newline, 1, splitIdx);
seqWriter.writeEnd(id, splitIdx, true);
}
entries_num++;
numEntriesInCurrFile++;
header.clear();
}
if (numEntriesInCurrFile == 0) {
Debug(Debug::WARNING) << "File " << sourceName << " is empty or invalid and was ignored\n";
}
delete kseq;
if (filenames.size() > 1 && par.createdbMode == Parameters::SEQUENCE_SPLIT_MODE_SOFT) {
size_t fileSize = FileUtil::getFileSize(filenames[fileIdx].c_str());
headerFileOffset += fileSize;
seqFileOffset += fileSize;
}
}
Debug(Debug::INFO) << "\n";
if (fclose(source) != 0) {
Debug(Debug::ERROR) << "Cannot close file " << sourceFile << "\n";
EXIT(EXIT_FAILURE);
}
hdrWriter.close(true, false);
seqWriter.close(true, false);
if (dbType == -1) {
if (isNuclCnt == sampleCount) {
dbType = Parameters::DBTYPE_NUCLEOTIDES;
} else {
dbType = Parameters::DBTYPE_AMINO_ACIDS;
}
seqWriter.writeDbtypeFile(seqWriter.getDataFileName(), dbType ,par.compressed);
}
Debug(Debug::INFO) << "Database type: " << Parameters::getDbTypeName(dbType) << "\n";
if (dbInput == true) {
reader->close();
delete reader;
}
if (entries_num == 0) {
Debug(Debug::ERROR) << "The input files have no entry: ";
for (size_t fileIdx = 0; fileIdx < filenames.size(); fileIdx++) {
Debug(Debug::ERROR) << " - " << filenames[fileIdx] << "\n";
}
Debug(Debug::ERROR) << "Please check your input files. Only files in fasta/fastq[.gz|bz2] are supported\n";
EXIT(EXIT_FAILURE);
}
// fix ids
if (par.shuffleDatabase == true) {
DBWriter::createRenumberedDB(dataFile, indexFile, "", "", DBReader<unsigned int>::LINEAR_ACCCESS);
DBWriter::createRenumberedDB(hdrDataFile, hdrIndexFile, "", "", DBReader<unsigned int>::LINEAR_ACCCESS);
}
if (par.createdbMode == Parameters::SEQUENCE_SPLIT_MODE_SOFT) {
if (filenames.size() == 1) {
FileUtil::symlinkAbs(filenames[0], dataFile);
FileUtil::symlinkAbs(filenames[0], hdrDataFile);
} else {
for (size_t fileIdx = 0; fileIdx < filenames.size(); fileIdx++) {
FileUtil::symlinkAbs(filenames[fileIdx], dataFile + "." + SSTR(fileIdx));
FileUtil::symlinkAbs(filenames[fileIdx], hdrDataFile + "." + SSTR(fileIdx));
}
}
}
if (par.writeLookup == true) {
DBReader<unsigned int> readerHeader(hdrDataFile.c_str(), hdrIndexFile.c_str(), 1, DBReader<unsigned int>::USE_DATA | DBReader<unsigned int>::USE_INDEX);
readerHeader.open(DBReader<unsigned int>::NOSORT);
// create lookup file
std::string lookupFile = dataFile + ".lookup";
FILE* file = FileUtil::openAndDelete(lookupFile.c_str(), "w");
std::string buffer;
buffer.reserve(2048);
unsigned int splitIdx = 0;
unsigned int splitCounter = 0;
DBReader<unsigned int>::LookupEntry entry;
for (unsigned int id = 0; id < readerHeader.getSize(); id++) {
size_t splitSize = sourceLookup[splitIdx].size();
if (splitSize == 0 || splitCounter > sourceLookup[splitIdx].size() - 1) {
splitIdx++;
splitCounter = 0;
}
char *header = readerHeader.getData(id, 0);
entry.id = id;
entry.entryName = Util::parseFastaHeader(header);
if (entry.entryName.empty()) {
Debug(Debug::WARNING) << "Cannot extract identifier from entry " << entries_num << "\n";
}
entry.fileNumber = sourceLookup[splitIdx][splitCounter];
readerHeader.lookupEntryToBuffer(buffer, entry);
int written = fwrite(buffer.c_str(), sizeof(char), buffer.size(), file);
if (written != (int)buffer.size()) {
Debug(Debug::ERROR) << "Cannot write to lookup file " << lookupFile << "\n";
EXIT(EXIT_FAILURE);
}
buffer.clear();
splitCounter++;
}
if (fclose(file) != 0) {
Debug(Debug::ERROR) << "Cannot close file " << lookupFile << "\n";
EXIT(EXIT_FAILURE);
}
readerHeader.close();
}
delete[] sourceLookup;
return EXIT_SUCCESS;
}