Codebase list kmc / 60095252-c551-4abb-89a7-ca6cd934eeb0/upstream kmc_tools / parameters_parser.cpp
60095252-c551-4abb-89a7-ca6cd934eeb0/upstream

Tree @60095252-c551-4abb-89a7-ca6cd934eeb0/upstream (Download .tar.gz)

parameters_parser.cpp @60095252-c551-4abb-89a7-ca6cd934eeb0/upstreamraw · history · blame

  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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
/*
  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.1
  Date   : 2022-01-04
*/

#include "parameters_parser.h"
#include <iostream>
using namespace std;


uint32 CParametersParser::replace_zero(uint32 val, const char* param_name, uint32 value_to_set_if_zero)
{
	if (val == 0)
	{
		cerr << "Warning: min value for " << param_name << " is " << value_to_set_if_zero << ". Your value will be converted to " << value_to_set_if_zero << "\n";
		return value_to_set_if_zero;
	}
	return val;
}


void CParametersParser::parse_int_or_float(bool& force_float, bool& force_int, float& float_value, uint32& int_val, const char* param_name)
{
	if (strchr(argv[pos] + 3, '.'))
	{
		float_value = (float)atof(argv[pos++] + 3);
		if (float_value > 1.0f || float_value < 0.0f)
		{
			cerr << "Error: wrong value for fastq input parameter: "<< param_name <<"\n";
			exit(1);
		}
		if (force_int)
		{
			cerr << "Error: both -ci, -cx must be specified as real number [0;1] or as integer \n";
			exit(1);
		}
		force_float = true;
		config.filtering_params.use_float_value = true;
	}
	else
	{
		int_val = atoi(argv[pos++] + 3);
		if (force_float)
		{
			cerr << "Error: both -ci, -cx must be specified as real number [0;1] or as integer \n";
			exit(1);
		}
		force_int = true;
		config.filtering_params.use_float_value = false;
	}
}

void CParametersParser::parse_global_params()
{
	//defaults
	config.avaiable_threads = thread::hardware_concurrency();

	//override defaults if specified
	for( ; pos < argc && argv[pos][0] == '-' ; ++pos)
	{
		if (strncmp(argv[pos], "-t", 2) == 0)
		{
			if (strlen(argv[pos]) < 3)
			{
				std::cerr << "Error: -t require value\n";
				exit(1);
			}
			config.avaiable_threads = atoi(argv[pos] + 2);
			continue;
		}
		else if (argv[pos][1] == 'v')
		{
			config.verbose = true;
			continue;
		}
		else if (strncmp(argv[pos], "-hp", 3) == 0)
		{
			config.percent_progress.Hide();
			continue;
		}
		else
		{
			std::cerr << "Error: unknown global option " << argv[pos] << "\n";
			exit(1);
		}
	}
}

void CParametersParser::read_input_fastq_desc()
{
	if (pos >= argc)
	{
		cerr << "Error: Input fastq files(s) missed\n";
		exit(1);
	}
	if (strncmp(argv[pos], "-", 1) == 0)
	{
		cerr << "Error: Input fastq file(s) required, but " << argv[pos] << " found\n";
		exit(1);
	}
	string input_file_name = argv[pos++];
	if (input_file_name[0] != '@')
		config.filtering_params.input_srcs.push_back(input_file_name);
	else
	{
		ifstream in(input_file_name.c_str() + 1);
		if (!in.good())
		{
			cerr << "Error: No " << input_file_name.c_str() + 1 << " file\n";
			exit(1);
		}
		string s;
		while (getline(in, s))
		{
			if (s != "")
				config.filtering_params.input_srcs.push_back(s);
		}
		in.close();
	}

	bool force_float = false;
	bool force_int = false;
	
	for (int i = 0; i < 3 && pos < argc; ++i)
	{
		if(argv[pos][0] != '-')
			break;
		if (strncmp(argv[pos], "-ci", 3) == 0)
		{
			parse_int_or_float(force_float, force_int, config.filtering_params.f_min_kmers, config.filtering_params.n_min_kmers, "-ci");
		}
		else if (strncmp(argv[pos], "-cx", 3) == 0)
		{
			parse_int_or_float(force_float, force_int, config.filtering_params.f_max_kmers, config.filtering_params.n_max_kmers, "-cx");
		}
		else if (strncmp(argv[pos], "-f", 2) == 0)
		{
			switch (argv[pos++][2])
			{
			case 'a':
				config.filtering_params.input_file_type = CFilteringParams::file_type::fasta;
				break;
			case 'q':
				config.filtering_params.input_file_type = CFilteringParams::file_type::fastq;
				break;
			default:
				cerr << "Error: unknow parameter " << argv[pos - 1] << "\n";
				exit(1);
				break;
			}
		}
	}
	config.filtering_params.output_file_type = config.filtering_params.input_file_type;
}


void CParametersParser::read_output_fastq_desc()
{
	if (pos >= argc)
	{
		cerr << "Error: Output fastq source missed\n";
		exit(1);
	}
	if (strncmp(argv[pos], "-", 1) == 0)
	{
		cerr << "Error: Output fastq source required, but " << argv[pos] << "found\n";
		exit(1);
	}
	config.filtering_params.output_src = argv[pos++];

	while (pos < argc && argv[pos][0] == '-')
	{
		if (strncmp(argv[pos], "-f", 2) == 0)
		{
			switch (argv[pos][2])
			{
			case 'q':
				config.filtering_params.output_file_type = CFilteringParams::file_type::fastq;
				break;
			case 'a':
				config.filtering_params.output_file_type = CFilteringParams::file_type::fasta;
				break;
			default:
				cerr << "Error: unknown parameter " << argv[pos] << "\n";
				exit(1);
				break;
			}
			if (config.filtering_params.input_file_type == CFilteringParams::file_type::fasta && config.filtering_params.output_file_type == CFilteringParams::file_type::fastq)
			{
				cerr << "Error: cannot set -fq for output when -fa is set for input\n";
				exit(1);
			}
		}
		else
		{
			cerr << "Error: Unknown parameter: " << argv[pos] << "\n";
			exit(1);
		}
		++pos;
	}
}

void CParametersParser::read_filter_params()
{
	while (pos < argc && argv[pos][0] == '-')
	{
		if (strncmp(argv[pos], "-t", 2) == 0)
		{
			config.filtering_params.filter_mode = CFilteringParams::FilterMode::trim;
		}
		else if (strncmp(argv[pos], "-hm", 3) == 0)
		{
			config.filtering_params.filter_mode = CFilteringParams::FilterMode::hard_mask;
		}
		else
		{
			cerr << "Warning: Unknow parameter for filter operation: " << argv[pos] << "\n";
		}
		++pos;
	}
}

void CParametersParser::read_check_params()
{
	if (pos >= argc)
	{
		std::cerr << "Error: check operation require k-mer to check\n";
		exit(1);
	}
	config.check_params.kmer = argv[pos++];
}

void CParametersParser::read_input_desc()
{
	if (pos >= argc)
	{
		cerr << "Error: Input database source missed\n";
		exit(1);
	}
	if (strncmp(argv[pos], "-", 1) == 0)
	{
		cerr << "Error: Input database source required, but " << argv[pos] << "found\n";
		exit(1);
	}
	CInputDesc desc(argv[pos++]);
	config.input_desc.push_back(desc);
	for (int i = 0; i < 2 && pos < argc; ++i)
	{
		if (strncmp(argv[pos], "-", 1) != 0)
			break;
		if (strncmp(argv[pos], "-ci", 3) == 0)
		{
			config.input_desc.back().cutoff_min = replace_zero(atoi(argv[pos++] + 3), "-ci", 1);
		}
		else if (strncmp(argv[pos], "-cx", 3) == 0)
		{
			config.input_desc.back().cutoff_max = replace_zero(atoi(argv[pos++] + 3), "-cx", 1);
		}
		else
		{
			cerr << "Error: Unknow parameter: " << argv[pos];
			exit(1);
		}
	}
}

bool CParametersParser::read_output_for_transform()
{
	if (pos >= argc)
		return false;

	CTransformOutputDesc::OpType op_type;
	uint64 counter_value = 0; //for set_counts only
	if (strcmp(argv[pos], "sort") == 0)
	{
		op_type = CTransformOutputDesc::OpType::SORT;	
	}
	else if (strcmp(argv[pos], "reduce") == 0)
	{
		op_type = CTransformOutputDesc::OpType::REDUCE;
	}
	else if (strcmp(argv[pos], "compact") == 0)
	{
		op_type = CTransformOutputDesc::OpType::COMPACT;
	}
	else if (strcmp(argv[pos], "histogram") == 0)
	{
		op_type = CTransformOutputDesc::OpType::HISTOGRAM;
	}
	else if (strcmp(argv[pos], "dump") == 0)
	{
		op_type = CTransformOutputDesc::OpType::DUMP;
	}
	else if (strcmp(argv[pos], "set_counts") == 0)
	{
		op_type = CTransformOutputDesc::OpType::SET_COUNTS;
		++pos;
		if (pos >= argc)
		{
			cerr << "Error: set_counts operation requires count value\n";
			Usage();
			exit(1);
		}
		
		if (strncmp(argv[pos], "-", 1) == 0)
		{
			cerr << "Error: Count value expected, but " << argv[pos] << " found\n";
			exit(1);
		}

		char* ptr_end = nullptr;
		counter_value = strtoull(argv[pos], &ptr_end, 10);
		auto len = strlen(argv[pos]);
		if (argv[pos] + len != ptr_end)
		{
			cerr << "Error: Count value expected, but " << argv[pos] << " found\n";
			exit(1);
		}		

		if (counter_value > numeric_limits<uint32>::max())
		{
			cerr << "Error: currenlty kmc_tools supports counter values up to " << numeric_limits<uint32>::max() << "\n";
			exit(1);
		}

	}
	else
	{
		cerr << "Error: unknown operation: " << argv[pos] << "\n";
		Usage();
		exit(1);
	}

	config.transform_output_desc.emplace_back(op_type);

	++pos;

	//read op paramts
	while (pos < argc)
	{
		if (strncmp(argv[pos], "-", 1) != 0)
			break;
		if (strncmp(argv[pos], "-s", 3) == 0)
		{
			if (config.transform_output_desc.back().op_type == CTransformOutputDesc::OpType::DUMP)
			{
				config.transform_output_desc.back().sorted_output = true;
			}
			else
			{
				cerr << "Error: -s parameter allowed only for dump operation\n";
				Usage();
				exit(1);
			}
		}		
		else
		{
			cerr << "Error: unknown operation parameter: " << argv[pos] <<"\n";
			exit(1);
		}
		++pos;
	}

	if (pos >= argc)
	{
		cerr << "Error: Output database path missed\n";
		exit(1);
	}
	if (strncmp(argv[pos], "-", 1) == 0)
	{
		cerr << "Error: Output database path required, but " << argv[pos] << " found\n";
		exit(1);
	}

	config.transform_output_desc.back().file_src = argv[pos++];

	if (op_type == CTransformOutputDesc::OpType::SET_COUNTS)
		config.transform_output_desc.back().counter_value = counter_value;

	//read database params
	while (pos < argc)
	{
		if (strncmp(argv[pos], "-", 1) != 0)
			break;
		if (strncmp(argv[pos], "-ci", 3) == 0)
		{
			config.transform_output_desc.back().cutoff_min = replace_zero(atoi(argv[pos++] + 3), "-ci", 1);
		}
		else if (strncmp(argv[pos], "-cx", 3) == 0)
		{
			config.transform_output_desc.back().cutoff_max = replace_zero(atoi(argv[pos++] + 3), "-cx", 1);
		}
		else if (strncmp(argv[pos], "-cs", 3) == 0)
		{
			config.transform_output_desc.back().counter_max = replace_zero(atoi(argv[pos++] + 3), "-cs", 1);
		}
		else if (strncmp(argv[pos], "-o", 2) == 0)
		{
			auto& op_type = config.transform_output_desc.back().op_type;

			if (op_type == CTransformOutputDesc::OpType::COMPACT ||
				op_type == CTransformOutputDesc::OpType::REDUCE ||
				op_type == CTransformOutputDesc::OpType::SET_COUNTS ||
				op_type == CTransformOutputDesc::OpType::SORT)
			{
				if (strncmp(argv[pos] + 2, "kff", 3) == 0)
					config.transform_output_desc.back().output_type = OutputType::KFF1;
				else if (strncmp(argv[pos] + 2, "kmc", 3) == 0)
					config.transform_output_desc.back().output_type = OutputType::KMC1;
				else
				{
					cerr << "Error: Unknown output type\n";
					Usage();
					exit(1);
				}
				++pos;
			}
			else
			{
				cerr << "Error: -o parameter allowed only for compact, reduce, set_counts and sort operations\n";
				Usage();
				exit(1);
			}
		}
		else
		{
			cerr << "Error: Unknown parameter: " << argv[pos];
			Usage();
			exit(1);
		}
	}
	if (op_type == CTransformOutputDesc::OpType::COMPACT)
	{
		if (config.transform_output_desc.back().counter_max)
			cerr << "Warning: -cs can not be specified for compact operation, value specified will be ignored\n";
		config.transform_output_desc.back().counter_max = 1;
	}	
	if (op_type == CTransformOutputDesc::OpType::SET_COUNTS)
	{
		auto& tmp = config.transform_output_desc.back();
		if (tmp.counter_max || tmp.cutoff_max || tmp.cutoff_min)
			cerr << "Warning: -cs, -cx, -ci cannot be specified for set_counts operation, values will be ignored\n";
		tmp.counter_max = tmp.cutoff_max = numeric_limits<uint32>::max();
		tmp.cutoff_min = 1;
	}

	return true;
}

bool CParametersParser::read_output_desc_for_simple()
{
	if (pos >= argc)
		return false;

	//get op name
	CSimpleOutputDesc::OpType op_type;
	if (strcmp(argv[pos], "intersect") == 0)
	{
		op_type = CSimpleOutputDesc::OpType::INTERSECT;
	}
	else if (strcmp(argv[pos], "kmers_subtract") == 0)
	{
		op_type = CSimpleOutputDesc::OpType::KMERS_SUBTRACTION;
	}
	else if (strcmp(argv[pos], "counters_subtract") == 0)
	{
		op_type = CSimpleOutputDesc::OpType::COUNTERS_SUBTRACTION;
	}
	else if (strcmp(argv[pos], "union") == 0)
	{
		op_type = CSimpleOutputDesc::OpType::UNION;
	}
	else if (strcmp(argv[pos], "reverse_kmers_subtract") == 0)
	{
		op_type = CSimpleOutputDesc::OpType::REVERSE_KMERS_SUBTRACTION;
	}
	else if (strcmp(argv[pos], "reverse_counters_subtract") == 0)
	{
		op_type = CSimpleOutputDesc::OpType::REVERSE_COUNTERS_SUBTRACTION;
	}
	else
	{
		cerr << "Error: unknown operation: " << argv[pos] << "\n";
		Usage();
		exit(1);
	}

	++pos;
	if (pos >= argc)
	{
		cerr << "Error: Output database path missed\n";
		exit(1);
	}
	if (strncmp(argv[pos], "-", 1) == 0)
	{
		cerr << "Error: Output database path required, but " << argv[pos] << " found\n";
		exit(1);
	}
	config.simple_output_desc.emplace_back(op_type);
	config.simple_output_desc.back().file_src = argv[pos++];

	while (pos < argc)
	{
		if (strncmp(argv[pos], "-", 1) != 0)
			break;
		if (strncmp(argv[pos], "-ci", 3) == 0)
		{
			config.simple_output_desc.back().cutoff_min = replace_zero(atoi(argv[pos++] + 3), "-ci", 1);
		}
		else if (strncmp(argv[pos], "-cx", 3) == 0)
		{
			config.simple_output_desc.back().cutoff_max = replace_zero(atoi(argv[pos++] + 3), "-cx", 1);
		}
		else if (strncmp(argv[pos], "-cs", 3) == 0)
		{
			config.simple_output_desc.back().counter_max = replace_zero(atoi(argv[pos++] + 3), "-cs", 1);
		}
		else if (strncmp(argv[pos], "-oc", 3) == 0)
		{
			if (op_type == CSimpleOutputDesc::OpType::KMERS_SUBTRACTION || op_type == CSimpleOutputDesc::OpType::REVERSE_KMERS_SUBTRACTION)
			{
				cerr << "Error: -oc not allowed for kmers_subtract and reverse_kmers_subtract as it doesn't make sense (equal k-mers form both input will not be present in output)\n";
				exit(1);
			}
			char* mode = argv[pos] + 3;
			if (strcmp(mode, "min") == 0)
			{
				config.simple_output_desc.back().counter_op = CounterOpType::MIN;
			}
			else if (strcmp(mode, "max") == 0)
			{
				config.simple_output_desc.back().counter_op = CounterOpType::MAX;
			}
			else if (strcmp(mode, "sum") == 0)
			{
				config.simple_output_desc.back().counter_op = CounterOpType::SUM;
			}
			else if (strcmp(mode, "diff") == 0)
			{
				config.simple_output_desc.back().counter_op = CounterOpType::DIFF;
			}
			else if (strcmp(mode, "left") == 0)
			{
				config.simple_output_desc.back().counter_op = CounterOpType::FROM_DB1;
			}
			else if (strcmp(mode, "right") == 0)
			{
				config.simple_output_desc.back().counter_op = CounterOpType::FROM_DB2;
			}
			else
			{
				cerr << "Error: unknown counter calculation mode: " << mode << ". Allowed values: min, max, sum, diff, left, right\n";
				exit(1);
			}
			++pos;
		}
		else if (strncmp(argv[pos], "-o", 2) == 0)
		{			
			if (strncmp(argv[pos] + 2, "kff", 3) == 0)
				config.simple_output_desc.back().output_type = OutputType::KFF1;
			else if (strncmp(argv[pos] + 2, "kmc", 3) == 0)
				config.simple_output_desc.back().output_type = OutputType::KMC1;
			else
			{
				cerr << "Error: Unknown output type\n";
				Usage();
				exit(1);
			}
			++pos;
		}
		else
		{
			cerr << "Error: Unknow parameter: " << argv[pos];
			Usage();
			exit(1);
		}

	}
	return true;
}

void CParametersParser::Usage()
{
	CUsageDisplayerFactory disp(CConfig::GetInstance().mode);
	disp.GetUsageDisplayer().Display();
}

CParametersParser::CParametersParser(int argc, char** argv) :argc(argc), argv(argv), config(CConfig::GetInstance())
{
	pos = 0;
	if (argc < 2)
	{
		Usage();
		exit(1);
	}
}

void CParametersParser::Parse()
{
	pos = 1;
	parse_global_params();

	if (strcmp(argv[pos], "complex") == 0)
	{
		config.mode = CConfig::Mode::COMPLEX;
	}
	else if (strcmp(argv[pos], "transform") == 0)
	{
		config.mode = CConfig::Mode::TRANSFORM;
	}
	else if (strcmp(argv[pos], "simple") == 0)
	{
		config.mode = CConfig::Mode::SIMPLE_SET;
	}
	else if (strcmp(argv[pos], "compare") == 0)
	{
		config.mode = CConfig::Mode::COMPARE;
	}
	else if (strcmp(argv[pos], "filter") == 0)
	{
		config.mode = CConfig::Mode::FILTER;
	}
	else if (strcmp(argv[pos], "info") == 0)
	{
		config.mode = CConfig::Mode::INFO;
	}
	else if (strcmp(argv[pos], "check") == 0)
	{
		config.mode = CConfig::Mode::CHECK;
	}
	else
	{
		cerr << "Error: Unknow mode: " << argv[pos] << "\n";
		Usage();
		exit(1);
	}

	if (argc == 2)
	{
		Usage();
		exit(1);
	}

	pos++;
	if (config.mode == CConfig::Mode::FILTER)
	{
		read_filter_params();
		read_input_desc(); //kmc db
		read_input_fastq_desc(); //fastq input
		read_output_fastq_desc();
		if (config.filtering_params.use_float_value && config.filtering_params.filter_mode != CFilteringParams::FilterMode::normal)
		{
			cerr << "Error: trim (-t) and soft mask (-hm) are not compatibile with float values of cut off (-ci -cx)\n";
			exit(1);
		}
	}
	else if (config.mode == CConfig::Mode::COMPLEX)
	{
		if (strncmp(argv[pos], "-", 1) == 0)
		{
			cerr << "Error: operations description file expected but " << argv[2] << " found\n";
			exit(1);
		}		
		complex_parser = make_unique<CParser>(argv[pos]);
		complex_parser->ParseInputs();
		complex_parser->ParseOutput();
	}
	else if (config.mode == CConfig::Mode::TRANSFORM)
	{
		read_input_desc();
		while (read_output_for_transform())
			;

		if (config.transform_output_desc.size() == 0)
		{
			cerr << "Error: output missed\n";
			Usage();
			exit(1);
		}

	}
	else if (config.mode == CConfig::Mode::SIMPLE_SET)
	{
		read_input_desc(); //first input
		read_input_desc(); //second input
		while (read_output_desc_for_simple())
			;
		if (config.simple_output_desc.size() == 0)
		{
			cerr << "Error: output missed\n";
			Usage();
			exit(1);
		}
	}
	else if (config.mode == CConfig::Mode::INFO)
	{
		read_input_desc();
	}
	else if (config.mode == CConfig::Mode::CHECK)
	{
		read_input_desc();
		read_check_params();		
	}
	else if (config.mode == CConfig::Mode::COMPARE)
	{
		read_input_desc();
		read_input_desc();
	}
}

uint32 CParametersParser::get_min_cutoff_min()
{
	uint32 min_cutoff_min = config.input_desc.front().cutoff_min;
	for (uint32 i = 0; i < config.input_desc.size(); ++i)
	{
		if (config.input_desc[i].cutoff_min < min_cutoff_min)
			min_cutoff_min = config.input_desc[i].cutoff_min;
	}
	return min_cutoff_min;
}

uint64 CParametersParser::get_max_cutoff_max()
{
	uint64 max_cutoff_max = config.input_desc.front().cutoff_max;
	for (uint32 i = 0; i < config.input_desc.size(); ++i)
	{
		if (config.input_desc[i].cutoff_max > max_cutoff_max)
			max_cutoff_max = config.input_desc[i].cutoff_max;
	}
	return max_cutoff_max;
}

uint32 CParametersParser::get_max_counter_max()
{
	uint32 max_counter_max = config.headers.front().counter_size;
	for (uint32 i = 0; i < config.headers.size(); ++i)
	{
		if (config.headers[i].counter_size> max_counter_max)
			max_counter_max = config.headers[i].counter_size;
	}
	max_counter_max = (uint32)((1ull << (max_counter_max << 3)) - 1);
	if (max_counter_max == 0)
		max_counter_max = 1;
	return max_counter_max;
}

bool CParametersParser::validate_input_dbs()
{
	config.headers.push_back(CKmerFileHeader(config.input_desc.front().file_src));

	uint32 kmer_len = config.headers.front().kmer_len;
	uint32 mode = config.headers.front().mode;
	if (mode == 1)
	{
		cerr << "Error: quality counters are not supported in kmc tools\n"; 
		return false;
	}

	uint8_t encoding = config.headers.front().GetEncoding();

	for (uint32 i = 1; i < config.input_desc.size(); ++i)
	{
		config.headers.push_back(CKmerFileHeader(config.input_desc[i].file_src));
		CKmerFileHeader& h = config.headers.back();
		if (h.mode != mode)
		{
			cerr << "Error: quality/direct based counters conflict!\n";
			return false;
		}
		if (h.kmer_len != kmer_len)
		{
			cerr << "Database " << config.input_desc.front().file_src << " contains " << kmer_len << "-mers, but database " << config.input_desc[i].file_src << " contains " << h.kmer_len << "-mers\n";
			return false;
		}

		if (h.GetEncoding() != encoding)
		{
			cerr << "Error: different k-mers encodings across input databases\n";
			return false;
		}
	}

	for(auto& header : config.headers)
		if (header.counter_size == 0)
		{
			std::cerr << "Error: kmc_tools currently does not support k-mer sets without counters. It will be implemented soon. If needed faster please contact authors or post an issue at https://github.com/refresh-bio/KMC/issues.\n";
			exit(1);
		}

	config.kmer_len = kmer_len;

	//KMC output file format cannot be used if encoding is different than 00011011
	if (encoding != 0b00011011)
	{
		bool was_output_format_overridden = false;
		if (config.mode == CConfig::Mode::COMPLEX)
		{
			config.output_desc.encoding = encoding;
			if (config.output_desc.output_type == OutputType::KMC1)
			{
				was_output_format_overridden = true;
				config.output_desc.output_type = OutputType::KFF1;
			}
		}

		for (auto& c : config.simple_output_desc)
		{
			c.encoding = encoding;
			if (c.output_type == OutputType::KMC1)
			{
				was_output_format_overridden = true;
				c.output_type = OutputType::KFF1;
			}
		}
		for (auto& c : config.transform_output_desc)
		{
			c.encoding = encoding;
			//dump and histogram writes in text format
			if (c.op_type == CTransformOutputDesc::OpType::DUMP ||
				c.op_type == CTransformOutputDesc::OpType::HISTOGRAM)
				continue;

			if (c.output_type == OutputType::KMC1)
			{
				was_output_format_overridden = true;
				c.output_type = OutputType::KFF1;
			}
		}

		if (was_output_format_overridden)
		{
			std::cerr << "Warning: only A -> 0, C -> 1, G -> 2, T -> 3 encoding is supported by KMC format. Because different encoding was used for input database(s) KKF file format is enforced for each output\n";
		}
	}

	//update cutoff_min and coutoff_max if it was not set with parameters
	for (uint32 i = 0; i < config.input_desc.size(); ++i)
	{
		if (config.input_desc[i].cutoff_min == 0)
			config.input_desc[i].cutoff_min = config.headers[i].min_count;
		if (config.input_desc[i].cutoff_max == 0)
			config.input_desc[i].cutoff_max = config.headers[i].max_count;
	}

	//update output description if it was not set with parameters
	if (config.mode == CConfig::Mode::SIMPLE_SET)
	{
		uint32 min_cutoff_min = get_min_cutoff_min();
		uint64 max_cutoff_max = get_max_cutoff_max();
		uint32 max_counter_max = get_max_counter_max();
		
		for (auto& desc : config.simple_output_desc)
		{
			if (desc.cutoff_min == 0)
				desc.cutoff_min = min_cutoff_min;
			if (desc.cutoff_max == 0)
				desc.cutoff_max = max_cutoff_max;
			if (desc.counter_max == 0)
				desc.counter_max = max_counter_max;
		}
	}
	else if (config.mode == CConfig::Mode::TRANSFORM)
	{
		uint32 min_cutoff_min = get_min_cutoff_min();
		uint64 max_cutoff_max = get_max_cutoff_max();
		uint32 max_counter_max = get_max_counter_max();

		for (auto& desc : config.transform_output_desc)
		{
			if (desc.op_type == CTransformOutputDesc::OpType::SET_COUNTS)
				continue;

			if (desc.cutoff_min == 0)
				desc.cutoff_min = min_cutoff_min;
			if (desc.cutoff_max == 0)
			{
				if (desc.op_type == CTransformOutputDesc::OpType::HISTOGRAM)//for histogram default value differs				
				{
					desc.cutoff_max = MIN(config.headers.front().max_count, MIN(HISTOGRAM_MAX_COUNTER_DEFAULT, (uint32)((1ull << (8 * config.headers.front().counter_size)) - 1)));					
				}
				else
					desc.cutoff_max = max_cutoff_max;
			}
			if (desc.counter_max == 0)
				desc.counter_max = max_counter_max;
		}
	}
	else if (config.mode == CConfig::Mode::COMPLEX)
	{
		if (config.output_desc.cutoff_min == 0)
		{
			uint32 min_cutoff_min = get_min_cutoff_min();
			config.output_desc.cutoff_min = min_cutoff_min;
			if (config.verbose)
				cerr << "Warning: -ci was not specified for output. It will be set to " << min_cutoff_min << "\n";
		}
		if (config.output_desc.cutoff_max == 0)
		{
			uint64 max_cutoff_max = get_max_cutoff_max();
			config.output_desc.cutoff_max = max_cutoff_max;
			if (config.verbose)
				cerr << "Warning: -cx was not specified for output. It will be set to " << config.output_desc.cutoff_max << "\n";
		}
		if (config.output_desc.counter_max == 0)
		{
			uint32 max_counter_max = get_max_counter_max();
			config.output_desc.counter_max = max_counter_max;
			if (config.verbose)
				cerr << "Warning: -cs was not specified for output. It will be set to " << max_counter_max << "\n";
		}
	}
	return true;
}
	
void CParametersParser::SetThreads()
{
	uint32 threads_left = config.avaiable_threads;
	//threads distribution: as many as possible for kmc2 of kff database input, 1 thread for main thread which make operations calculation
	vector<reference_wrapper<CInputDesc>> kmc2_or_kff_desc;

	if (config.IsSeparateThreadForMainProcessingNeeded())
		threads_left = MAX(1, threads_left - 1);
	
	for (uint32 i = 0; i < config.headers.size(); ++i)
	{
		if (config.headers[i].kmer_file_type == KmerFileType::KMC2 || config.headers[i].kmer_file_type == KmerFileType::KFF1)
		{
			kmc2_or_kff_desc.push_back(ref(config.input_desc[i]));
		}
		else
		{
			if (threads_left > 2)
				threads_left -= 2; //2 threads for kmc1 input
			else
				threads_left = 1;
		}
	}
	if (kmc2_or_kff_desc.size())
	{
		uint32 per_signle_kmc2_input = MAX(1, (uint32)(threads_left / kmc2_or_kff_desc.size()));
		uint32 per_last_kmc2_input = MAX(1, (uint32)((threads_left + kmc2_or_kff_desc.size() - 1) / kmc2_or_kff_desc.size()));

		for (uint32 i = 0; i < kmc2_or_kff_desc.size() - 1; ++i)
			kmc2_or_kff_desc[i].get().threads = per_signle_kmc2_input;

		kmc2_or_kff_desc.back().get().threads = per_last_kmc2_input;
	}
}



// ***** EOF