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
|
#include "stdafx.h"
/*
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: Sebastian Deorowicz, Agnieszka Debudaj-Grabysz, Marek Kokot
Version: 3.1.1
Date : 2019-05-19
*/
#include <algorithm>
#include <numeric>
#include <iostream>
#include "kb_completer.h"
using namespace std;
extern uint64 total_reads;
//************************************************************************************************************
// CKmerBinCompleter
//************************************************************************************************************
//----------------------------------------------------------------------------------
// Assign queues and monitors
CKmerBinCompleter::CKmerBinCompleter(CKMCParams &Params, CKMCQueues &Queues)
{
mm = Queues.mm;
file_name = Params.output_file_name;
kq = Queues.kq;
bd = Queues.bd;
s_mapper = Queues.s_mapper;
memory_bins = Queues.memory_bins;
bbkpq = Queues.bbkpq;
use_strict_mem = Params.use_strict_mem;
kmer_file_name = file_name + ".kmc_suf";
lut_file_name = file_name + ".kmc_pre";
kmer_len = Params.kmer_len;
signature_len = Params.signature_len;
cutoff_min = Params.cutoff_min;
cutoff_max = (uint32)Params.cutoff_max;
counter_max = (uint32)Params.counter_max;
lut_prefix_len = Params.lut_prefix_len;
both_strands = Params.both_strands;
without_output = Params.without_output;
kmer_t_size = Params.KMER_T_size;
}
//----------------------------------------------------------------------------------
CKmerBinCompleter::~CKmerBinCompleter()
{
}
//----------------------------------------------------------------------------------
// Store sorted and compacted bins to the output file (stage first)
void CKmerBinCompleter::ProcessBinsFirstStage()
{
int32 bin_id = 0;
uchar *data = nullptr;
//uint64 data_size = 0;
list<pair<uint64, uint64>> data_packs;
uchar *lut = nullptr;
uint64 lut_size = 0;
counter_size = 0;
sig_map_size = (1 << (signature_len * 2)) + 1;
sig_map = new uint32[sig_map_size];
fill_n(sig_map, sig_map_size, 0);
lut_pos = 0;
counter_size = min(BYTE_LOG(cutoff_max), BYTE_LOG(counter_max));
if (!without_output)
{
// Open output file
out_kmer = fopen(kmer_file_name.c_str(), "wb");
if (!out_kmer)
{
cerr << "Error: Cannot create " << kmer_file_name << "\n";
exit(1);
return;
}
out_lut = fopen(lut_file_name.c_str(), "wb");
if (!out_lut)
{
cerr << "Error: Cannot create " << lut_file_name << "\n";
fclose(out_kmer);
exit(1);
return;
}
}
n_recs = 0;
_n_unique = _n_cutoff_min = _n_cutoff_max = _n_total = 0;
n_unique = n_cutoff_min = n_cutoff_max = n_total = 0;
char s_kmc_pre[] = "KMCP";
char s_kmc_suf[] = "KMCS";
if (!without_output)
{
// Markers at the beginning
fwrite(s_kmc_pre, 1, 4, out_lut);
fwrite(s_kmc_suf, 1, 4, out_kmer);
}
// Process priority queue of ready-to-output bins
while (!kq->empty())
{
// Get the next bin
if (!kq->pop(bin_id, data, data_packs, lut, lut_size, _n_unique, _n_cutoff_min, _n_cutoff_max, _n_total))
continue;
// Decrease memory size allocated by stored bin
string name;
uint64 n_rec;
uint64 n_plus_x_recs;
uint64 n_super_kmers;
uint64 raw_size;
CMemDiskFile *file;
bd->read(bin_id, file, name, raw_size, n_rec, n_plus_x_recs, n_super_kmers);
uint64 lut_recs = lut_size / sizeof(uint64);
if (!without_output)
{
for (auto& e : data_packs)
{
// Write bin data to the output file
#ifdef WIN32 //fwrite bug https://connect.microsoft.com/VisualStudio/feedback/details/755018/fwrite-hangs-with-large-size-count
uint64 write_offset = e.first;
uint64 left_to_write = e.second - e.first;
while (left_to_write)
{
uint64 current_to_write = MIN(left_to_write, (4ull << 30) - 1);
fwrite(data + write_offset, 1, current_to_write, out_kmer);
write_offset += current_to_write;
left_to_write -= current_to_write;
}
#else
fwrite(data + e.first, 1, e.second - e.first, out_kmer);
#endif
}
}
memory_bins->free(bin_id, CMemoryBins::mba_suffix);
if (!without_output)
{
uint64 *ulut = (uint64*)lut;
for (uint64 i = 0; i < lut_recs; ++i)
{
uint64 x = ulut[i];
ulut[i] = n_recs;
n_recs += x;
}
fwrite(lut, lut_recs, sizeof(uint64), out_lut);
}
//fwrite(&n_rec, 1, sizeof(uint64), out_lut);
memory_bins->free(bin_id, CMemoryBins::mba_lut);
n_unique += _n_unique;
n_cutoff_min += _n_cutoff_min;
n_cutoff_max += _n_cutoff_max;
n_total += _n_total;
for (uint32 i = 0; i < sig_map_size; ++i)
{
if (s_mapper->get_bin_id(i) == bin_id)
{
sig_map[i] = lut_pos;
}
}
++lut_pos;
}
}
//----------------------------------------------------------------------------------
// Store sorted and compacted bins to the output file (stage second)
void CKmerBinCompleter::ProcessBinsSecondStage()
{
char s_kmc_pre[] = "KMCP";
char s_kmc_suf[] = "KMCS";
if (use_strict_mem)
{
int32 bin_id;
uchar *data = nullptr;
uint64 data_size = 0;
uchar *lut = nullptr;
uint64 lut_size = 0;
bool last_in_bin = false;
while (bbkpq->pop(bin_id, data, data_size, lut, lut_size, _n_unique, _n_cutoff_min, _n_cutoff_max, _n_total, last_in_bin))
{
if (data_size)
{
if(!without_output)
fwrite(data, 1, data_size, out_kmer);
sm_pmm_merger_suff->free(data);
}
if (lut_size)
{
uint64 lut_recs = lut_size / sizeof(uint64);
uint64* ulut = (uint64*)lut;
for (uint64 i = 0; i < lut_recs; ++i)
{
uint64 x = ulut[i];
ulut[i] = n_recs;
n_recs += x;
}
if(!without_output)
fwrite(lut, lut_recs, sizeof(uint64), out_lut);
sm_pmm_merger_lut->free(lut);
}
if(last_in_bin)
{
n_unique += _n_unique;
n_cutoff_min += _n_cutoff_min;
n_cutoff_max += _n_cutoff_max;
n_total += _n_total;
for (uint32 i = 0; i < sig_map_size; ++i)
{
if (s_mapper->get_bin_id(i) == bin_id)
{
sig_map[i] = lut_pos;
}
}
++lut_pos;
}
}
}
if (!without_output)
{
// Marker at the end
fwrite(s_kmc_suf, 1, 4, out_kmer);
fclose(out_kmer);
fwrite(&n_recs, 1, sizeof(uint64), out_lut);
//store signature mapping
fwrite(sig_map, sizeof(uint32), sig_map_size, out_lut);
// Store header
uint32 offset = 0;
store_uint(out_lut, kmer_len, 4); offset += 4;
store_uint(out_lut, (uint32)0, 4); offset += 4; // mode: 0 (counting), 1 (Quake-compatibile counting) which is now not supported
store_uint(out_lut, counter_size, 4); offset += 4;
store_uint(out_lut, lut_prefix_len, 4); offset += 4;
store_uint(out_lut, signature_len, 4); offset += 4;
store_uint(out_lut, cutoff_min, 4); offset += 4;
store_uint(out_lut, cutoff_max, 4); offset += 4;
store_uint(out_lut, n_unique - n_cutoff_min - n_cutoff_max, 8); offset += 8;
store_uint(out_lut, both_strands ? 0 : 1, 1); offset++;
// Space for future use
for (int32 i = 0; i < 27; ++i)
{
store_uint(out_lut, 0, 1);
offset++;
}
store_uint(out_lut, 0x200, 4);
offset += 4;
store_uint(out_lut, offset, 4);
// Marker at the end
fwrite(s_kmc_pre, 1, 4, out_lut);
fclose(out_lut);
}
cerr << "\n";
delete[] sig_map;
}
//----------------------------------------------------------------------------------
// Return statistics
void CKmerBinCompleter::GetTotal(uint64 &_n_unique, uint64 &_n_cutoff_min, uint64 &_n_cutoff_max, uint64 &_n_total)
{
_n_unique = n_unique;
_n_cutoff_min = n_cutoff_min;
_n_cutoff_max = n_cutoff_max;
_n_total = n_total;
}
//----------------------------------------------------------------------------------
// Store single unsigned integer in LSB fashion
bool CKmerBinCompleter::store_uint(FILE *out, uint64 x, uint32 size)
{
for(uint32 i = 0; i < size; ++i)
putc((x >> (i * 8)) & 0xFF, out);
return true;
}
//----------------------------------------------------------------------------------
//Init memory pools for 2nd stage
void CKmerBinCompleter::InitStage2(CKMCParams& /*Params*/, CKMCQueues& Queues)
{
sm_pmm_merger_lut = Queues.sm_pmm_merger_lut;
sm_pmm_merger_suff = Queues.sm_pmm_merger_suff;
}
//************************************************************************************************************
// CWKmerBinCompleter
//************************************************************************************************************
//----------------------------------------------------------------------------------
// Constructor
CWKmerBinCompleter::CWKmerBinCompleter(CKMCParams &Params, CKMCQueues &Queues)
{
kbc = new CKmerBinCompleter(Params, Queues);
}
void CWKmerBinCompleter::InitStage2(CKMCParams& Params, CKMCQueues& Queues)
{
kbc->InitStage2(Params, Queues);
}
//----------------------------------------------------------------------------------
// Destructor
CWKmerBinCompleter::~CWKmerBinCompleter()
{
delete kbc;
}
//----------------------------------------------------------------------------------
// Execution
void CWKmerBinCompleter::operator()(bool first_stage)
{
if(first_stage)
kbc->ProcessBinsFirstStage();
else
kbc->ProcessBinsSecondStage();
}
//----------------------------------------------------------------------------------
// Return statistics
void CWKmerBinCompleter::GetTotal(uint64 &_n_unique, uint64 &_n_cutoff_min, uint64 &_n_cutoff_max, uint64 &_n_total)
{
if(kbc)
kbc->GetTotal(_n_unique, _n_cutoff_min, _n_cutoff_max, _n_total);
}
// ***** EOF
|