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 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
|
/*
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: 3.2.4
Date : 2024-02-09
*/
#ifndef _OPERATIONS_H
#define _OPERATIONS_H
#ifdef ENABLE_DEBUG
#include "config.h"
#endif
#ifdef ENABLE_LOGGER
#include "develop.h"
#endif
#include <iostream>
#include <vector>
#include "bundle.h"
//************************************************************************************************************
// C2ArgOper - abstract class representing 2 argument's operation
//************************************************************************************************************
template<unsigned SIZE> class C2ArgOper : public CInput<SIZE>
{
protected:
CBundle<SIZE>* input1, *input2;
CounterOpType counter_op_type;
public:
C2ArgOper(CBundle<SIZE>* input1, CBundle<SIZE>* input2, CounterOpType counter_op_type) :
input1(input1), input2(input2), counter_op_type(counter_op_type)
{
}
void EqualsToOuputBundle(CBundle<SIZE>& output_bundle)
{
switch (counter_op_type)
{
case CounterOpType::MIN:
output_bundle.Insert(input1->TopKmer(), MIN(input1->TopCounter(), input2->TopCounter()));
break;
case CounterOpType::MAX:
output_bundle.Insert(input1->TopKmer(), MAX(input1->TopCounter(), input2->TopCounter()));
break;
case CounterOpType::SUM:
output_bundle.Insert(input1->TopKmer(), input1->TopCounter() + input2->TopCounter());
break;
case CounterOpType::DIFF:
if (input1->TopCounter() > input2->TopCounter())
output_bundle.Insert(input1->TopKmer(), input1->TopCounter() - input2->TopCounter());
break;
case CounterOpType::FROM_DB1:
output_bundle.Insert(input1->TopKmer(), input1->TopCounter());
break;
case CounterOpType::FROM_DB2:
output_bundle.Insert(input1->TopKmer(), input2->TopCounter());
break;
case CounterOpType::NONE:
break;
default:
break;
}
}
void IgnoreRest() override
{
input1->IgnoreRest();
input2->IgnoreRest();
}
~C2ArgOper() override
{
delete input1;
delete input2;
}
};
//************************************************************************************************************
// CUnion - implementation of union operation on 2 k-mer's sets.
//************************************************************************************************************
template <unsigned SIZE> class CUnion : public C2ArgOper<SIZE>
{
public:
CUnion(CBundle<SIZE>* input1, CBundle<SIZE>* input2, CounterOpType counter_op_type) : C2ArgOper<SIZE>(input1, input2, counter_op_type)
{
}
void NextBundle(CBundle<SIZE>& bundle) override
{
while (!this->input1->Finished() && !this->input2->Finished())
{
if (bundle.Full())
{
return;
}
if (this->input1->TopKmer() == this->input2->TopKmer())
{
this->EqualsToOuputBundle(bundle);
this->input1->Pop();
this->input2->Pop();
}
else if (this->input1->TopKmer() < this->input2->TopKmer())
{
bundle.Insert(this->input1->TopKmer(), this->input1->TopCounter());
this->input1->Pop();
}
else
{
bundle.Insert(this->input2->TopKmer(), this->input2->TopCounter());
this->input2->Pop();
}
}
CBundle<SIZE>* non_empty_bundle = this->input1->Finished() ? this->input2 : this->input1;
while (!non_empty_bundle->Finished())
{
if (bundle.Full())
{
return;
}
bundle.Insert(non_empty_bundle->TopKmer(), non_empty_bundle->TopCounter());
non_empty_bundle->Pop();
}
this->finished = true;
}
};
//************************************************************************************************************
// CIntersection - implementation of intersection operation on 2 k-mer's sets.
//************************************************************************************************************
template<unsigned SIZE> class CIntersection : public C2ArgOper<SIZE>
{
public:
CIntersection(CBundle<SIZE>* input1, CBundle<SIZE>* input2, CounterOpType counter_op_type) : C2ArgOper<SIZE>(input1, input2, counter_op_type)
{
}
void NextBundle(CBundle<SIZE>& bundle) override
{
while (!this->input1->Finished() && !this->input2->Finished())
{
if (this->input1->TopKmer() == this->input2->TopKmer())
{
this->EqualsToOuputBundle(bundle);
this->input1->Pop();
this->input2->Pop();
if (bundle.Full())
return;
}
else if (this->input1->TopKmer() < this->input2->TopKmer())
this->input1->Pop();
else
this->input2->Pop();
}
if (!this->input1->Finished())
this->input1->IgnoreRest();
if (!this->input2->Finished())
this->input2->IgnoreRest();
this->finished = true;
}
};
//************************************************************************************************************
// CKmersSubtract - implementation of subtraction operation of 2 k-mer's sets.
// If k-mer exists in both input it is absent in result (counters does not matter).
//************************************************************************************************************
template<unsigned SIZE> class CKmersSubtract : public C2ArgOper<SIZE>
{
public:
CKmersSubtract(CBundle<SIZE>* input1, CBundle<SIZE>* input2) : C2ArgOper<SIZE>(input1, input2, CounterOpType::NONE)
{
}
void NextBundle(CBundle<SIZE>& bundle) override
{
while (!this->input1->Finished() && !this->input2->Finished())
{
if (this->input2->TopKmer() < this->input1->TopKmer())
this->input2->Pop();
else if (this->input2->TopKmer() == this->input1->TopKmer())
{
this->input1->Pop();
this->input2->Pop();
}
else
{
bundle.Insert(this->input1->TopKmer(), this->input1->TopCounter());
this->input1->Pop();
if (bundle.Full())
return;
}
}
if(!this->input2->Finished())
this->input2->IgnoreRest();
while (!this->input1->Finished())
{
if (bundle.Full())
return;
bundle.Insert(this->input1->TopKmer(), this->input1->TopCounter());
this->input1->Pop();
}
this->finished = true;
}
};
//************************************************************************************************************
// CCountersSubtract - implementation of subtraction operation of 2 k-mer's sets.
// If k-mer exists in both input their counters are subtracted.
//************************************************************************************************************
template<unsigned SIZE> class CCountersSubtract : public C2ArgOper<SIZE>
{
public:
CCountersSubtract(CBundle<SIZE>* input1, CBundle<SIZE>* input2, CounterOpType counter_op_type) : C2ArgOper<SIZE>(input1, input2, counter_op_type)
{
}
void NextBundle(CBundle<SIZE>& bundle) override
{
while (!this->input1->Finished() && !this->input2->Finished())
{
if (this->input2->TopKmer() < this->input1->TopKmer())
this->input2->Pop();
else if (this->input2->TopKmer() == this->input1->TopKmer())
{
this->EqualsToOuputBundle(bundle);
this->input1->Pop();
this->input2->Pop();
if (bundle.Full())
return;
}
else
{
bundle.Insert(this->input1->TopKmer(), this->input1->TopCounter());
this->input1->Pop();
if (bundle.Full())
return;
}
}
if (!this->input2->Finished())
this->input2->IgnoreRest();
while (!this->input1->Finished())
{
if (bundle.Full())
return;
bundle.Insert(this->input1->TopKmer(), this->input1->TopCounter());
this->input1->Pop();
}
this->finished = true;
}
};
template<unsigned SIZE> class CComparer
{
CBundle<SIZE>* input1, *input2;
public:
CComparer(CBundle<SIZE>* input1, CBundle<SIZE>* input2) : input1(input1), input2(input2)
{
}
bool Equals()
{
while (!this->input1->Finished() && !this->input2->Finished())
{
if (this->input1->TopCounter() != this->input2->TopCounter())
{
this->input1->IgnoreRest();
this->input2->IgnoreRest();
return false;
}
if (!(this->input1->TopKmer() == this->input2->TopKmer()))
{
this->input1->IgnoreRest();
this->input2->IgnoreRest();
return false;
}
this->input1->Pop();
this->input2->Pop();
}
if (!this->input1->Finished() || !this->input2->Finished())
{
std::cout << "one of input is not finished\n";
this->input1->IgnoreRest();
this->input2->IgnoreRest();
return false;
}
return true;
}
};
template<unsigned SIZE>
class CSimpleOperation
{
CBundle<SIZE>* input1, *input2;
std::vector<COutputBundle<SIZE>*>& outputs;
std::vector<COutputBundle<SIZE>*> outputs_for_equals;
std::vector<COutputBundle<SIZE>*> outputs_for_1st_lower;
std::vector<COutputBundle<SIZE>*> outputs_for_2nd_lower;
public:
CSimpleOperation(CBundle<SIZE>* input1, CBundle<SIZE>* input2, std::vector<COutputBundle<SIZE>*>& outputs) :
input1(input1),
input2(input2),
outputs(outputs)
{
for (auto o : outputs)
{
auto op = o->GetOpType();
if (op == CSimpleOutputDesc::OpType::INTERSECT || op == CSimpleOutputDesc::OpType::UNION || op == CSimpleOutputDesc::OpType::COUNTERS_SUBTRACTION || op == CSimpleOutputDesc::OpType::REVERSE_COUNTERS_SUBTRACTION)
outputs_for_equals.push_back(o);
if (op == CSimpleOutputDesc::OpType::UNION || op == CSimpleOutputDesc::OpType::KMERS_SUBTRACTION || op == CSimpleOutputDesc::OpType::COUNTERS_SUBTRACTION)
outputs_for_1st_lower.push_back(o);
if (op == CSimpleOutputDesc::OpType::UNION || op == CSimpleOutputDesc::OpType::REVERSE_KMERS_SUBTRACTION || op == CSimpleOutputDesc::OpType::REVERSE_COUNTERS_SUBTRACTION)
outputs_for_2nd_lower.push_back(o);
else
{
//should never be here
}
}
}
struct CEqNotifier
{
static void Notify(CSimpleOperation<SIZE>& operation, CKmer<SIZE>& kmer, uint32 counter1, uint32 counter2)
{
for (auto output : operation.outputs_for_equals)
{
uint32 c = 0;
if (output->GetOpType() == CSimpleOutputDesc::OpType::REVERSE_COUNTERS_SUBTRACTION)
c = output->GetCounter(counter2, counter1);
else
c = output->GetCounter(counter1, counter2);
output->InsertAndSendIfFull(kmer, c);
}
}
};
struct CEqNotifierEmpty
{
static void Notify(CSimpleOperation<SIZE>& /*operation*/, CKmer<SIZE>& /*kmer*/, uint32 /*counter1*/, uint32 /*counter2*/){}
};
struct C1stLowerNotifier
{
static void Notify(CSimpleOperation<SIZE>& operation, CKmer<SIZE>& kmer, uint32 counter)
{
for (auto output : operation.outputs_for_1st_lower)
output->InsertAndSendIfFull(kmer, counter);
}
};
struct CLowerNotifierEmpty
{
static void Notify(CSimpleOperation<SIZE>& /*operation*/, CKmer<SIZE>& /*kmer*/, uint32 /*counter*/){}
};
struct C2ndLowerNotifier
{
static void Notify(CSimpleOperation<SIZE>& operation, CKmer<SIZE>& kmer, uint32 counter)
{
for (auto output : operation.outputs_for_2nd_lower)
output->InsertAndSendIfFull(kmer, counter);
}
};
template<typename EQ_NOTIFIER, typename FIRST_LOWER_NOTIFIER, typename SECOND_LOWER_NOTIFIER>
void process_impl()
{
uint32 get1 = 0;
uint32 get2 = 0;
CKmer<SIZE> kmer2 = this->input2->data.kmers_with_counters[get2].kmer;
uint32 counter2 = this->input2->data.kmers_with_counters[get2].counter;
CKmer<SIZE> kmer1 = this->input1->data.kmers_with_counters[get1].kmer;
uint32 counter1 = this->input1->data.kmers_with_counters[get1].counter;
uint32 left1 = this->input1->NRecLeft();
uint32 left2 = this->input2->NRecLeft();
while (true)
{
if (kmer1 == kmer2)
{
EQ_NOTIFIER::Notify(*this, kmer1, counter1, counter2);
++get1;
++get2;
if (--left1)
{
kmer1 = this->input1->data.kmers_with_counters[get1].kmer;
counter1 = this->input1->data.kmers_with_counters[get1].counter;
}
else
{
this->input1->data.get_pos = get1;
if (!this->input1->Finished())
{
get1 = 0;
kmer1 = this->input1->data.kmers_with_counters[get1].kmer;
counter1 = this->input1->data.kmers_with_counters[get1].counter;
left1 = this->input1->NRecLeft();
}
else
break;
}
if (--left2)
{
kmer2 = this->input2->data.kmers_with_counters[get2].kmer;
counter2 = this->input2->data.kmers_with_counters[get2].counter;
}
else
{
this->input2->data.get_pos = get2;
if (!this->input2->Finished())
{
get2 = 0;
kmer2 = this->input2->data.kmers_with_counters[get2].kmer;
counter2 = this->input2->data.kmers_with_counters[get2].counter;
left2 = this->input2->NRecLeft();
}
else
break;
}
}
else if (kmer1 < kmer2)
{
FIRST_LOWER_NOTIFIER::Notify(*this, kmer1, counter1);
++get1;
if (--left1)
{
kmer1 = this->input1->data.kmers_with_counters[get1].kmer;
counter1 = this->input1->data.kmers_with_counters[get1].counter;
}
else
{
this->input1->data.get_pos = get1;
if (!this->input1->Finished())
{
get1 = 0;
kmer1 = this->input1->data.kmers_with_counters[get1].kmer;
counter1 = this->input1->data.kmers_with_counters[get1].counter;
left1 = this->input1->NRecLeft();
}
else
break;
}
}
else
{
SECOND_LOWER_NOTIFIER::Notify(*this, kmer2, counter2);
++get2;
if (--left2)
{
kmer2 = this->input2->data.kmers_with_counters[get2].kmer;
counter2 = this->input2->data.kmers_with_counters[get2].counter;
}
else
{
this->input2->data.get_pos = get2;
if (!this->input2->Finished())
{
get2 = 0;
kmer2 = this->input2->data.kmers_with_counters[get2].kmer;
counter2 = this->input2->data.kmers_with_counters[get2].counter;
left2 = this->input2->NRecLeft();
}
else
break;
}
}
}
this->input1->data.get_pos = get1;
this->input2->data.get_pos = get2;
}
void Process()
{
#ifdef ENABLE_LOGGER
CTimer timer;
timer.start();
#endif
bool notify_equal = outputs_for_equals.size() != 0;
bool notify_1st_lower = outputs_for_1st_lower.size() != 0;
bool notify_2nd_lower = outputs_for_2nd_lower.size() != 0;
if (!this->input1->Finished() && !this->input2->Finished())
{
if (!notify_equal && !notify_1st_lower && notify_2nd_lower)
{
process_impl<CEqNotifierEmpty, CLowerNotifierEmpty, C2ndLowerNotifier>();
}
else if (!notify_equal && notify_1st_lower && !notify_2nd_lower)
{
process_impl<CEqNotifierEmpty, C1stLowerNotifier, CLowerNotifierEmpty>();
}
else if (!notify_equal && notify_1st_lower && notify_2nd_lower)
{
process_impl<CEqNotifierEmpty, C1stLowerNotifier, C2ndLowerNotifier>();
}
else if (notify_equal && !notify_1st_lower && !notify_2nd_lower)
{
process_impl<CEqNotifier, CLowerNotifierEmpty, CLowerNotifierEmpty>();
}
else if (notify_equal && !notify_1st_lower && notify_2nd_lower)
{
process_impl<CEqNotifier, CLowerNotifierEmpty, C2ndLowerNotifier>();
}
else if (notify_equal && notify_1st_lower && !notify_2nd_lower)
{
process_impl<CEqNotifier, C1stLowerNotifier, CLowerNotifierEmpty>();
}
else if (notify_equal && notify_1st_lower && notify_2nd_lower)
{
process_impl<CEqNotifier, C1stLowerNotifier, C2ndLowerNotifier>();
}
else
{
//sould never be here
}
}
if (notify_1st_lower)
{
while (!this->input1->Finished())
{
for (auto output : outputs_for_1st_lower)
output->InsertAndSendIfFull(this->input1->TopKmer(), this->input1->TopCounter());
this->input1->Pop();
}
}
else
this->input1->IgnoreRest();
if (notify_2nd_lower)
{
while (!this->input2->Finished())
{
for (auto output : outputs_for_2nd_lower)
output->InsertAndSendIfFull(this->input2->TopKmer(), this->input2->TopCounter());
this->input2->Pop();
}
}
else
this->input2->IgnoreRest();
for (auto output : outputs)
output->NotifyFinish();
#ifdef ENABLE_LOGGER
CLoger::GetLogger().log_operation("Process", this, timer.get_time());
#endif
}
};
#endif
// ***** EOF
|