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
|
/*
This file is a part of KMC software distributed under GNU GPL 3 licence.
The homepage of the KMC project is http://sun.aei.polsl.pl/kmc
Authors: Marek Kokot
Version: 2.3.0
Date : 2015-08-21
*/
#ifndef _KMC1_DB_WRITER_H
#define _KMC1_DB_WRITER_H
#include "defs.h"
#include "config.h"
#include "queues.h"
#include <string>
#include <vector>
//************************************************************************************************************
// CKMC1SufixFileWriter - thread for writing sufixes' parts
//************************************************************************************************************
class CKMC1SufixFileWriter
{
public:
CKMC1SufixFileWriter(CSufWriteQueue& input_queue, FILE* kmc_suf) :
input_queue(input_queue),
kmc_suf(kmc_suf)
{
}
void operator()()
{
uchar* buf;
uint32 size;
while (input_queue.pop(buf, size))
{
if (fwrite(buf, 1, size, kmc_suf) != size)
{
std::cout << "Error while writing to kmc_suf file\n";
exit(1);
}
delete[] buf;
}
}
private:
CSufWriteQueue& input_queue;
FILE* kmc_suf;
};
//************************************************************************************************************
// CKMC1DbWriter - writer of KMC1 database
//************************************************************************************************************
template<unsigned SIZE> class CKMC1DbWriter
{
public:
CKMC1DbWriter(CBundle<SIZE>* bundle);
~CKMC1DbWriter();
bool Process();
private:
static const uint32 PRE_BUFF_SIZE_BYTES = KMC1_DB_WRITER_PREFIX_BUFF_BYTES;
static const uint32 SUF_BUFF_SIZE_BYTES = KMC1_DB_WRITER_SUFIX_BUFF_BYTES;
CConfig& config;
CBundle<SIZE>* bundle;
FILE* kmc_pre, *kmc_suf;
uint32 lut_prefix_len;
uint32 current_prefix;
uint32 counter_size;
uint32 pre_buff_size;
uint32 suf_buff_size;
uint64* pre_buff;
uchar* suf_buff;
uint64 added_kmers;
uint32 sufix_rec_bytes;
uint32 suf_pos, pre_pos;
void store_pre_buf();
void send_suf_buf_to_queue();
void start_writing();
inline void add_kmer(CKmer<SIZE>& kmer, uint32 counter);
void finish_writing();
template<typename T> void write_header_part(T data);
void calc_lut_prefix_len();
CCircularQueue<SIZE> bundles_queue;
CSufWriteQueue suf_buf_queue;
};
/*****************************************************************************************************************************/
/******************************************************** CONSTRUCTOR ********************************************************/
/*****************************************************************************************************************************/
/*****************************************************************************************************************************/
template <unsigned SIZE> CKMC1DbWriter<SIZE>::CKMC1DbWriter(CBundle<SIZE>* bundle) :
config(CConfig::GetInstance()),
bundle(bundle),
bundles_queue(DEFAULT_CIRCULAL_QUEUE_CAPACITY)
{
kmc_pre = NULL;
kmc_suf = NULL;
pre_buff = NULL;
suf_buff = NULL;
std::string kmc_pre_file_name = config.output_desc.file_src + ".kmc_pre";
std::string kmc_suf_file_name = config.output_desc.file_src + ".kmc_suf";
kmc_pre = fopen(kmc_pre_file_name.c_str(), "wb");
setvbuf(kmc_pre, NULL, _IONBF, 0);
if (!kmc_pre)
{
std::cout << "Error: cannot open file : " << kmc_pre_file_name << "\n";
exit(1);
}
kmc_suf = fopen(kmc_suf_file_name.c_str(), "wb");
setvbuf(kmc_suf, NULL, _IONBF, 0);
if (!kmc_suf)
{
fclose(kmc_pre);
std::cout << "Error: cannot open file : " << kmc_suf_file_name << "\n";
exit(1);
}
setvbuf(kmc_pre, NULL, _IONBF, 0);
setvbuf(kmc_suf, NULL, _IONBF, 0);
// Calculate LUT size
calc_lut_prefix_len();
counter_size = MIN(BYTE_LOG(config.output_desc.counter_max), BYTE_LOG(config.output_desc.cutoff_max));
sufix_rec_bytes = (config.kmer_len - lut_prefix_len) / 4 + counter_size;
current_prefix = 0;
added_kmers = 0;
pre_buff_size = PRE_BUFF_SIZE_BYTES / sizeof(uint64);
suf_buff_size = SUF_BUFF_SIZE_BYTES / sufix_rec_bytes;
suf_pos = pre_pos = 0;
pre_buff = new uint64[pre_buff_size];
pre_buff[pre_pos++] = 0;
suf_buff = new uchar[suf_buff_size * sufix_rec_bytes];
suf_buf_queue.init(suf_buff_size * sufix_rec_bytes, SUFIX_WRITE_QUEUE_CAPACITY);
}
/*****************************************************************************************************************************/
/********************************************************** PUBLIC ***********************************************************/
/*****************************************************************************************************************************/
/*****************************************************************************************************************************/
template<unsigned SIZE> bool CKMC1DbWriter<SIZE>::Process()
{
start_writing();
//Converts bundles to output buffers, sufix buffer is placed to another queue and write in separate thread (sufix_writer)
std::thread preparing_thread([this]{
CBundleData<SIZE> bundle_data;
while (bundles_queue.pop(bundle_data))
{
while (!bundle_data.Empty())
{
add_kmer(bundle_data.TopKmer(), bundle_data.TopCounter());
bundle_data.Pop();
}
}
suf_buf_queue.push(suf_buff, sufix_rec_bytes * suf_pos);
suf_buf_queue.mark_completed();
});
CKMC1SufixFileWriter sufix_writer(suf_buf_queue, kmc_suf);
std::thread suf_buf_writing_thread(std::ref(sufix_writer));
#ifdef ENABLE_LOGGER
CTimer timer;
#endif
while (!bundle->Finished())
{
#ifdef ENABLE_LOGGER
timer.start();
#endif
bundles_queue.push(bundle->Data());
#ifdef ENABLE_LOGGER
CLoger::GetLogger().log_operation("dodawanie do kolejki wyjsciowej bundla", this, timer.get_time());
#endif
}
bundles_queue.mark_completed();
preparing_thread.join();
suf_buf_writing_thread.join();
finish_writing();
return true;
}
/*****************************************************************************************************************************/
template<unsigned SIZE> CKMC1DbWriter<SIZE>::~CKMC1DbWriter()
{
delete[] suf_buff;
delete[] pre_buff;
}
/*****************************************************************************************************************************/
/********************************************************** PRIVATE **********************************************************/
/*****************************************************************************************************************************/
/*****************************************************************************************************************************/
template <unsigned SIZE> template <typename T> void CKMC1DbWriter<SIZE>::write_header_part(T data)
{
for (uint32 i = 0; i < sizeof(T); ++i)
{
char c = (data >> (i << 3)) & 0xff;
if (putc(c, kmc_pre) == EOF)
{
std::cout << "Error while writing header of kmc1\n";
exit(1);
}
}
}
/*****************************************************************************************************************************/
template<unsigned SIZE> void CKMC1DbWriter<SIZE>::start_writing()
{
if (fwrite("KMCP", 1, 4, kmc_pre) != 4)
{
std::cout << "Error while writing starting KMCP marker";
exit(1);
}
if (fwrite("KMCS", 1, 4, kmc_suf) != 4)
{
std::cout << "Error while writing starting KMCS marker";
exit(1);
}
}
/*****************************************************************************************************************************/
template<unsigned SIZE> void CKMC1DbWriter<SIZE>::finish_writing()
{
uint32 max_prefix = (1 << 2 * lut_prefix_len);
while (current_prefix < max_prefix - 1)
{
pre_buff[pre_pos++] = added_kmers;
++current_prefix;
if (pre_pos == pre_buff_size)
store_pre_buf();
}
store_pre_buf();
send_suf_buf_to_queue();
//store header
write_header_part(config.kmer_len);
write_header_part(config.headers.front().mode);
write_header_part(counter_size);
write_header_part(lut_prefix_len);
write_header_part(config.output_desc.cutoff_min);
write_header_part(config.output_desc.cutoff_max);
write_header_part(added_kmers);
bool both_stands = false;
for (auto& input : config.headers)
both_stands = both_stands || input.both_strands; //if any input database is in both strands, output is also in both strands
write_header_part(!both_stands);
for (uint32 i = 0; i < 31; ++i)
write_header_part(uchar(0));
write_header_part((uint32)64);
if (fwrite("KMCP", 1, 4, kmc_pre) != 4)
{
std::cout << "Error while writing end KMCP marker";
exit(1);
}
if (fwrite("KMCS", 1, 4, kmc_suf) != 4)
{
std::cout << "Error while writing end KMCS marker";
exit(1);
}
fclose(kmc_pre);
fclose(kmc_suf);
}
/*****************************************************************************************************************************/
template<unsigned SIZE> void CKMC1DbWriter<SIZE>::add_kmer(CKmer<SIZE>& kmer, uint32 counter)
{
if (counter < config.output_desc.cutoff_min || counter > config.output_desc.cutoff_max)
return;
if (counter > config.output_desc.counter_max)
counter = config.output_desc.counter_max;
uint64 kmer_prefix = kmer.remove_suffix((config.kmer_len - lut_prefix_len) * 2);
while (current_prefix < kmer_prefix)
{
pre_buff[pre_pos++] = added_kmers;
++current_prefix;
if (pre_pos == pre_buff_size)
store_pre_buf();
}
uchar* rec = suf_buff + suf_pos * sufix_rec_bytes;
kmer.store(rec, sufix_rec_bytes - counter_size);
for (uint32 i = 0; i < counter_size; ++i)
*rec++ = counter >> (i << 3);
++suf_pos;
if (suf_pos == suf_buff_size)
send_suf_buf_to_queue();
++added_kmers;
}
/*****************************************************************************************************************************/
template<unsigned SIZE> void CKMC1DbWriter<SIZE>::store_pre_buf()
{
if (fwrite(pre_buff, sizeof(uint64), pre_pos, kmc_pre) != pre_pos)
{
std::cout << "Error while writing to kmc_pre file\n";
exit(1);
}
pre_pos = 0;
}
/*****************************************************************************************************************************/
template<unsigned SIZE> void CKMC1DbWriter<SIZE>::send_suf_buf_to_queue()
{
suf_buf_queue.push(suf_buff, sufix_rec_bytes * suf_pos);
suf_pos = 0;
}
/*****************************************************************************************************************************/
template<unsigned SIZE> void CKMC1DbWriter<SIZE>::calc_lut_prefix_len()
{
std::vector<uint32> best_lut_prefix_len_inputs(config.headers.size());
for (uint32 i = 0; i < config.headers.size(); ++i)
{
uint32 best_lut_prefix_len = 0;
uint64 best_mem_amount = 1ull << 62;
for (lut_prefix_len = 6; lut_prefix_len < 16; ++lut_prefix_len)
{
uint32 suffix_len = config.headers[i].kmer_len - lut_prefix_len;
if (suffix_len % 4)
continue;
uint64 suf_mem = config.headers[i].total_kmers * suffix_len / 4;
uint64 lut_mem = (1ull << (2 * lut_prefix_len)) * sizeof(uint64);
if (suf_mem + lut_mem < best_mem_amount)
{
best_lut_prefix_len = lut_prefix_len;
best_mem_amount = suf_mem + lut_mem;
}
}
best_lut_prefix_len_inputs[i] = best_lut_prefix_len;
}
//TODO poki co jako lut size biore najwieszy z najlepszych dla baz wejsciowych
lut_prefix_len = *std::max_element(best_lut_prefix_len_inputs.begin(), best_lut_prefix_len_inputs.end());
}
#endif
// ***** EOF
|