-
Notifications
You must be signed in to change notification settings - Fork 1
/
pattern.cpp
1501 lines (1361 loc) · 41 KB
/
pattern.cpp
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
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* This programm calculates the variance of a set of pattern with the same length and weight.
* It is possible to improve your patternset, estimate values for p, q and S, l_hom, li, lj
* from a multiple alignment file in fasta format, and also read patterns from a file.
*
* pattern object file
*
* For theory please have a look at:
*
* B. Morgenstern, B. Zhu, S. Horwege, C.-A Leimeister (2015)
* Estimating evolutionary distances between genomic sequences from spaced-word matches
* Algorithms for Molecular Biology 10, 5. (http://www.almob.org/content/10/1/5/abstract)
*
*
* @author: Lars Hahn - 03.06.2015, Georg-August-Universitaet Goettingen
* @version: 1.0 06/2015
*/
#include "pattern.h"
/*---Variables---------------------------------------------------------------*/
std::default_random_engine generator(std::random_device{}());
/*===Main-Part===============================================================*/
/*---Constructor-& Init------------------------------------------------------*/
/**
* Default constructor, sets the default vaulues, pattern will be generated automatically.
*/
pattern::pattern(){
this->size = 10;
this->length = 14;
this->weight = 8;
pattern(NULL, NULL, size, length, weight, 10000, 10000, 10000, 0.9, 0.25);
}
/**
* File constructor, sets values only from files; resets automatically if there are problems.
*/
pattern::pattern(char* pattern_file, char* align_file){
this->pattern_file = pattern_file;
this->align_file = align_file;
pattern(pattern_file, align_file, 10, 14, 8, 10000, 10000, 10000, 0.9, 0.25);
}
/**
* Long constructor, sets the values; resets automatically, if there are problems.
*
* @param pattern_file
* File, that may contains submitted pattern
*
* @param align_file
* File, that may contains an alignment file to estimate p, q, l_hom, li and lj
*
* @param size
* The amount of patterns; pattern number.
*
* @param length
* The pattern length for each pattern of the pattern set.
*
* @param weigth
* The weight (match positions; '1') for each pattern of the pattern set.
*
* @param l_hom
* In theory, the amount of homologous positions of two sequences in an multiple alignment.
*
* @param l1
* In theory, the first(represents each sequence i) sequence length of two observed sequences.
*
* @param l2
* In theory, the second(represents each sequence j) sequence length of two observed sequences.
*
* @param p
* The match probability ( = #matches / #l_hom)
*
* @param q
* The background probability for each nucleotide A,C,G,T
*/
pattern::pattern(char* pattern_file, char* align_file, int size, int length, int weight, int l_hom, int l1, int l2, double p, double q){
this->pattern_file = pattern_file;
this->align_file = align_file;
this->size = size;
this->length = length;
this->weight = weight;
this->l_hom = l_hom;
this->l1 = l1;
this->l2 = l2;
this->p = p;
this->q = q;
this->variance = 0;
this->best_variance = 0;
this->quiet = false;
this->silent = false;
this->secure = false;
ReinitPattern();
}
/**
* Short constructor, sets some default vaulues, just pattern dimension is set.
*
* @param size
* The amount of patterns; pattern number.
*
* @param length
* The pattern length for each pattern of the pattern set.
*
* @param weigth
* The weight (match positions; '1') for each pattern of the pattern set.
*/
pattern::pattern(int size, int length, int weight){
pattern(NULL,NULL,size,length,weight, 10000, 10000, 10000, 0.9, 0.25);
}
/**
* Default destructor, deletes all vectors and matrices in the object.
*/
pattern::~pattern(){
for(unsigned int i = 0; i < seq_matrix.size(); i++){
l_hom_val[i].clear();
p_values[i].clear();
seq_matrix[i].clear();
}
l_hom_val.clear();
p_values.clear();
seq_matrix.clear();
seq_leng.clear();
for(unsigned int i = 0; i < q_values.size(); i++){
q_values[i].clear();
}
q_values.clear();
for(unsigned int i = 0; i < var_sum.size(); i++){
var_sum[i].clear();
}
var_sum.clear();
pattern_set.clear();
best_pattern.clear();
}
/**
* Creates for the submitted or default values a set of pattern and calculates the first variance.
* If possible estimates p, q, all sequences lengths and all combinations of homologous sequence positions
*
*Also reset Pattern if needed, not necessary to create new object
*/
void pattern::ReinitPattern(){
std::ifstream patternfile, check_align;
std::vector<std::string> pattern_tmp;
std::string tmp;
char tokens[4] = {'.',' ',',',';'}; /*These tokens are allowed to seperate patterns*/
bool start, flag_align;
size_t f_size;
start = false;
flag_align = false;
pattern_set.clear();
best_pattern.clear();
if(pattern_file != NULL){
patternfile.open(pattern_file);
patternfile.seekg(0,std::ios::end);
f_size = patternfile.tellg();
if(!patternfile){
SecureMessage("file", -1);
}
else if(f_size == 0){ /*[FILE].eof() does not recognize empty files...*/
SecureMessage("empty", -1);
}
else{
patternfile.close(); /*..therefore it has also to be closed and opened -.-** */
patternfile.open(pattern_file);
//std::cout << "Reading pattern from submitted patternfile ...\n" << std::endl;
patternfile >> tmp;
while(!patternfile.eof()){
if(!ValidatePatternsFormat(tmp)){
patternfile >> tmp; /*Ignoring each incorrect pattern. It is easier to calculate with all the rests*/
}
else{
pattern_tmp = SplitString(tmp, tokens);
for(unsigned int i = 0; i < pattern_tmp.size(); i++){
pattern_set.push_back(pattern_tmp[i]); /*Each pattern needs to be saved in its own std::string for comparison*/
best_pattern.push_back(pattern_tmp[i]);
}
patternfile >> tmp;
}
}
if(size > 0){ /*For an empty set we do not habe to validate*/
start = ValidatePatternConditions();
}
if(!start){ /*Some conditions, like changed weight is too much much amount of work*/
SecureMessage("pattern", -1); /*Therefore we just use our default values*/
this->size = 10;
this->weight = 8;
this->length = 14;
pattern_set.clear(); /*Do not forget to reset, or the pattern set will not replaces, just increased*/
best_pattern.clear();
}
else{
this->size = pattern_set.size();
this->length = pattern_set[0].length();
this->weight = PatternWeight(pattern_set[0]);
Print();
//std::cout << "\n... Done!\n" << std::endl;
}
}
patternfile.close();
}
if(size <= 0){ /*Coping with all ...*/
SecureMessage("patsize", -1);
this->size = 10;
}
if(weight <= 0 || size <= 0 || length <= 1){ /*... possible wrong ...*/
SecureMessage("nkl",-1);
this->size = 10;
this->weight = 8;
this->length = 14;
this->pattern_set.clear();
this->best_pattern.clear();
}
if(weight > length){ /*..by the user submitted conditions for weight, length etc.*/
SecureMessage("weight_pat", -1);
this->weight = 8;
this->length = 14;
}
if(size >= MaxNumberPattern(weight-2, length-2)){ /*Match positions at the start and end do not alterate, therefore -2*/
size = MaxNumberPattern(weight-2, length-2);
SecureMessage("max_number_pattern",size); /*We can create all patterns directly*/
this->improve = false;
pattern_set.clear();
}
else{
this->improve = true;
}
if(improve && (length < 4 || weight == length || weight < 3)){
SecureMessage("noimprove",-1); /*The improvement switches positions with '1' and '0'*/
this->improve = false; /*At least 2 positions + end '1' and start '0' are nedeed*/
}
if(pattern_set.size() == 0){ /*worst case, if not wished, generating autopattern*/
//std::cout << "Generating autopatterns ...\n" << std::endl;
this->pattern_set = CreateRandomPattern();
for(int i = 0; i < size; i++){
ChangePatternRandom(i); /*Creating just uniq Pattern!*/
}
this->best_pattern = PatternCopy(pattern_set);
Print();
//std::cout << "\n ... Done!\n" << std::endl;
}
if(align_file != NULL){ /*Mayby alignfile set, but if it doesnt work, we want to use the normal... */
check_align.open(align_file);
check_align.seekg(0,std::ios::end); /*... variance calculation. There will be no sequences matrix*/
f_size = check_align.tellg();
if(!check_align){
SecureMessage("align", -1);
flag_align = false;
}
else if(f_size == 0){
SecureMessage("empty", -1);
flag_align = false;
}
else{
check_align.close();
check_align.open(align_file);
check_align >> tmp;
if(tmp[0] != '>'){
SecureMessage("fasta", -1);
flag_align = false;
}
else{
flag_align = true; /*shows the used start variance calculation */
}
}
check_align.close();
}
InitMatrix();
if(!flag_align){
this->variance = CalcVariance();
this->best_variance = variance;
this->best_pattern = PatternCopy(pattern_set);
}
else{
ReadAlign();
InitPValues(); /*Neccessary just with an alginment file...*/
InitQValues(); /*... otherwise too much RAM and junk*/
this->variance = CalcVarianceAlign();
this->best_variance = variance;
this->best_pattern = PatternCopy(pattern_set);
}
return;
}
/**
* By if it may used more than once, this method is saved.
*/
void pattern::InitMatrix(){
std::vector<double> tmp;
for(int i = 0; i < size; i++){
tmp.push_back(0.0);
}
for(int i = 0; i < size; i++){
var_sum.push_back(tmp);
}
}
/*---Get-&-SetFunc-----------------------------------------------------------*/
/**
* Returns complete pattern set
*
* @return complete pattern set
*/
std::vector<std::string> pattern::GetPattern(){
return pattern_set;
}
/**
* Returns complete best pattern set
*
* @return complete best pattern set
*/
std::vector<std::string> pattern::GetBestPattern(){
return best_pattern;
}
/**
* Returns a specific pattern of the pattern set
*
* @return pattern_set[number], specific string
*/
std::string pattern::GetPattern(int number){
if(number >=size){
SecureMessage("wrongindex", number);
return NULL;
}
else{
return pattern_set[number];
}
}
/**
* Returns a specific pattern of the best pattern set
*
* @return best_pattern[number], specific string
*/
std::string pattern::GetBestPattern(int number){
if(number >=size){
SecureMessage("wrongindex", number);
return NULL;
}
else{
return best_pattern[number];
}
}
/**
* Returns the current variance
*
* @return returns variance
*/
double pattern::GetVariance(){
return variance;
}
/**
* Returns the current best variance
*
* @return returns best variance
*/
double pattern::GetBestVariance(){
return best_variance;
}
/**
* Returns the current variance, normalized
*
* @return returns norm_variance
*/
double pattern::GetNormVariance(){
return variance/Gauss();
}
/**
* Returns the current best variance, normalized
*
* @return returns best norm_variance
*/
double pattern::GetBestNormVariance(){
return best_variance/Gauss();
}
/**
* Returns the weight of each pattern, the match positions
*
* @return returns weight
*/
int pattern::GetWeight(){
return weight;
}
/**
* Returns the amount of patters; number of patterns
*
* @return returns size
*/
int pattern::GetSize(){
return size;
}
/**
* Returns the length of each pattern
*
* @return returns length
*/
int pattern::GetLength(){
return length;
}
/**
* Returns the length of the homologous sequence pair
*
* @return returns homologous positions
*/
int pattern::GetLHom(){
return l_hom;
}
/**
* Returns the length of the first observed sequence
*
* @return returns length sequence 1
*/
int pattern::GetL1(){
return l1;
}
/**
* Returns the length of the second observed sequence
*
* @return returns length sequence 2
*/
int pattern::GetL2(){
return l2;
}
/**
* Returns the match probability
*
* @return returns p value
*/
double pattern::GetP(){
return p;
}
/**
* Returns the background probabillity, summation over all nucleotids
*
* @return returns q value
*/
double pattern::GetQ(){
return q;
}
/**
* Returns the position of the worst pattern, estimated by the maximum variancepart
* for each pattern pair
*
* @return returns position worst matrix by max_value
*/
int pattern::GetWorstPatMaxVal(){
return WorstPattern_max_val();
}
/**
* Returns the position of the worst pattern, estimated by the summation for each pattern Pi
* and the corresponding variance parts.
*
* @return returns position worst matrix by max_pat
*/
int pattern::GetWorstPatMaxPat(){
return WorstPattern_max_pat();
}
/*---PatternCreateFunc-------------------------------------------------------*/
/**
* Splits a string, read by a pattern file. Mayby each pattern does not get
* a new line, it has to be parsed, when a pattern starts and ends
*
* @param pattern_split
* The string containing a few pattern
*
* @param tokens
* The allowed tokens which can be used to seperate patterns in a line
*/
std::vector<std::string> pattern::SplitString(std::string pattern_split, char* tokens){
std::vector<std::string> patternset;
std::string tmp = "";
bool flag_token = false;
for(unsigned int i = 0; i < pattern_split.length(); i++){
for(unsigned int j = 0; j < strlen(tokens); j++){
if(pattern_split[i]==tokens[j]){
flag_token = true;
}
}
if(flag_token){ /*token found, which means in one line more patterns --> start new pattern, save last pattern*/
patternset.push_back(tmp);
tmp = "";
}
else{
tmp = tmp + pattern_split[i]; /*concatenating patternparts*/
}
flag_token = false;
}
patternset.push_back(tmp);
return patternset;
}
/**
* Validates a pattern, if it contains only pattern symbols and seperation tokens
*
* @param pattern_form
* The pattern which has to be investigate for symbols and tokens
*
* @return Returns true if this one pattern is in right format, false else
*/
bool pattern::ValidatePatternsFormat(std::string pattern_form){
bool flag = true;
for(unsigned int i = 0; i < pattern_form.length(); i++){ /*allowed tokens in patternformat, also separating tokens*/
if(pattern_form[i] != '1' && pattern_form[i] != '0' && pattern_form[i] != ' ' && pattern_form[i] != ',' && pattern_form[i] != '.' && pattern_form[i] != ';'){
flag=false;
SecureMessage("format", -1);
break;
}
}
if(pattern_form[0] != '1' || pattern_form[pattern_form.length()-1] != '1'){
flag=false;
SecureMessage("startend", -1);
}
return flag;
}
/**
* Validates a pattern set, if all patterns have the same length and weight
*
* @return Returns true if this one pattern is in right format, false else
*/
bool pattern::ValidatePatternConditions(){
int com_length, com_weight, com_size, leng;
bool condition;
com_size = pattern_set.size();
com_length = pattern_set[0].length();
com_weight = PatternWeight(pattern_set[0]); /*as fixpoint saving first patternweight*/
condition = true;
for(int i = 1; i < com_size; i++){
leng = pattern_set[i].length();
if(leng != com_length){ /*for the variance each pattern should have the same weight*/
SecureMessage("size", i);
condition = false;
}
if(PatternWeight(pattern_set[i]) != com_weight){
SecureMessage("weight", i);
condition = false;
}
}
return condition;
}
/**
* Estimates the weight of a pattern
*
* @param pattern_str
* The pattern which has to be investigate for the weight
*
* @return Returns true if this one pattern is in right format, false else
*/
int pattern::PatternWeight(std::string pattern_wght){
int str_weight;
int str_length;
str_weight = 0;
str_length = pattern_wght.length();
for(int i = 0; i < str_length; i++){
if(pattern_wght[i] == '1'){
str_weight++;
}
}
return str_weight;
}
/**
* Creates random a set of pattern. For convention a pattern has to start and end with '1'
* Reason 10010 ~ 1001
*
* @return Returns a randomly created pattern set
*/
std::vector<std::string> pattern::CreateRandomPattern(){
std::vector<std::string> pattern;
std::string prototype(length,'0'), tmp;
int position, counter;
prototype[0] = '1';
prototype[length-1] = '1';
std::uniform_int_distribution<int> distribution(1,length-2); /*better random generater then time, uses likelihood for evenly random distribution*/
for(int i = 0; i < size; i++){
counter = 2;
tmp = prototype; /*Prototype which saves the start and end '1'*/
while(counter < weight){
position = distribution(generator);
if(tmp[position] == '0'){ /*have fun and fill with '1' randomly :D */
counter ++;
tmp[position] = '1';
}
}
pattern.push_back(tmp);
}
return pattern;
}
/**
* Copy the pattern set
*
* @param old_pattern
* The pattern which has to be copied
*
* @return A new created copy of the pattern set
*/
std::vector<std::string> pattern::PatternCopy(std::vector<std::string>old_pattern){
std::vector<std::string> new_pattern;
for(unsigned int i = 0; i < old_pattern.size(); i++){
new_pattern.push_back(old_pattern[i]);
}
return new_pattern;
}
/**
* Changes two different positions ('1' and '0') in a specific pattern
* Start and end are excluded
*
* @param number
* The pattern which has to be modified
*/
void pattern::ChangePatternRandom(int number){
int pos1, pos2;
bool flag = true;
char c;
std::uniform_int_distribution<int> distribution(1,length-2);
while(flag && improve){
pos1 = distribution(generator);
pos2 = distribution(generator);
if(pattern_set[number][pos1] != pattern_set[number][pos2]){ /*Changes two positions, switching '1' and 1' or '0' and '0'...*/
flag = false; /*... does not make really sense ;) */
c = pattern_set[number][pos1];
pattern_set[number][pos1] = pattern_set[number][pos2];
pattern_set[number][pos2] = c;
}
if(!UniqPattern(number)){
flag = true;
}
}
return;
}
/**
* Scans if there is another pattern in the same format
*
* @return returns boolean if there is another same pattern
*/
bool pattern::UniqPattern(int number){
bool uniq = true;
for(int i = 0; i < size; i++){
if( number != i){
if(pattern_set[i] == pattern_set[number]){
uniq = false;
}
}
}
return uniq;
}
/*---Variance-----------------------------------------------------------------*/
/**
* Wether there is an alignment or not it decides on its own which variance
* calculation is now neccessary
*
* @return returns current variance
*/
double pattern::Variance(){
if(seq_matrix.size() < 2){ /*At least 2 sequences are neccessary for an alignment!*/
return CalcVariance();
}
else{
return CalcVarianceAlign();
}
}
/**
* Calculates the variance for a pattern set
* In this case l_hom = l1 = l2 = l (commandline parameter S)
*
* @return Calculates and returns current variance
*/
double pattern::CalcVariance(){
double homologue, background, var_hom, var_bac;
int shift;
homologue = 0.0;
background = 0.0;
for(int i = 0; i < size; i++){ /*i and j represents Pi and Pj of the set of pattern*/
for(int j = i; j < size; j++){
var_hom = 0.0;
var_bac = 0.0;
for(int s = -1*length+1; s < length; s++){ /*As in the formula, the shift goes from max shift left to max shift right*/
shift = ShiftPos(i, j, s); /*At least one position has to overlap*/
var_hom += (pow(p, shift) - pow(p, 2*weight)); /*summation of the homologue first part*/
var_bac += (pow(q, shift) - pow(q, 2*weight)); /*summation of the background second part*/
}
var_sum[i][j]=(l_hom - length + 1)*var_hom + (l1 - length + 1)*(l2 - length)*var_bac; /*For each pair Pi and Pj this is the direct share of the complete variance...*/
var_sum[j][i]=var_sum[i][j]; /*...which we can use to estimate a "worst" pattern*/
/*... and save it in an size x size matrix*/
homologue += var_hom;
background += var_bac;
}
}
this->variance = (l_hom - length + 1)*homologue + (l1 - length + 1)*(l2 - length)*background;
if(this->variance < this->best_variance){
this->best_pattern = PatternCopy(pattern_set);
this->best_variance = variance;
}
return variance;
}
/**
* Calculates the variance for a pattern set with alignment file
* In this case l_hom, l1, l2, p and q are estimated
*
* @return Calculates and returns current variance
*/
double pattern::CalcVarianceAlign(){
double var_value;
int s_size, counter;
s_size = seq_matrix.size();
counter = 0;
var_value = 0.0;
for(int i = 0; i < s_size-1; i++){
for(int j = i+1; j < s_size; j++){
q = 0.0;
for(int k = 0; k < 4; k++){
q += q_values[i][k]*q_values[j][k];
}
l_hom = l_hom_val[i][j]; /*Previously estimated ....*/
p = p_values[i][j]; /*... so that by improving ...*/
l1 = seq_leng[i]; /*it is already calculated and does not take ane time for another caculation*/
l2 = seq_leng[j];
var_value += CalcVariance();
counter++;
}
}
variance = var_value / counter++;
if(variance < best_variance){
best_pattern = PatternCopy(pattern_set);
best_variance = variance;
}
return variance;
}
/**
* Shifts to pattern to see the number of maximum match positions
*
* @param p1
* Position of the first used pattern of the pattern set
*
* @param p2
* Position of the second used pattern of the pattern set
* NOTE: possible is p1 = p2
*
* @param s
* The shift of the second pattern, s < 0 := shift left pattern 2, s > := shift right pattern 2
* @return Calculates and returns current variance
*/
int pattern::ShiftPos(int p1, int p2, int s){
int counter;
std::string pat1, pat2;
counter = 0;
if(s < 0){
s = 0 - s;
pat1 = pattern_set[p2]; /*If s < 0 for pat2 it is in the point of view for pat1 like s > 0*/
pat2 = pattern_set[p1]; /*Therefore changing the pattern and take the absolute value from s is easier*/
}
else{
pat1 = pattern_set[p1];
pat2 = pattern_set[p2];
}
/*Afterwards the second pattern (may changed) is only shifted left, its easier*/
for(int i = 0; i < s; i++){ /*The pattern section from only pattern 1*/
if(pat1[i] == '1'){
counter++;
}
}
for(int i = 0; i < (length-s); i++){ /*The common section of the pattern*/
if(pat1[i+s] == '1' || pat2[i] == '1'){
counter++;
}
}
for(int i = length-s; i < length; i++){ /*The pattern section from only pattern 2*/
if(pat2[i] == '1'){
counter++;
}
}
return counter;
}
/**
* Returns the position of the worst pattern, estimated by the maximum variancepart
* for each pattern pair
*
* @return returns position worst matrix by max_value
*/
int pattern::WorstPattern_max_val(){
double max_var_val, i_sum, j_sum;
int i_max; int j_max;
max_var_val = 0.0;
i_sum = 0.0;
j_sum = 0.0;
i_max = 0;
j_max = 0;
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
if(var_sum[i][j] > max_var_val){
max_var_val = var_sum[i][j]; /*looking for the maximum value and so for the maximum pattern pair*/
i_max = i;
j_max = j;
}
}
}
for(int i = 0; i < size; i++){
i_sum += var_sum[i][j_max]; /*If Pi has the higher summation, the part variance of Pi with each corresponding pattern is higher, so more worse*/
}
for(int j = 0; j < size; j++){
j_sum += var_sum[i_max][j]; /*If Pj has the higher summation, the part variance of PJ with each corresponding pattern is higher, so more worse*/
}
if(i_sum > j_sum){
return i_max;
}
else{
return j_max;
}
}
/**
* Returns the position of the worst pattern, estimated by the maximum summation
* for each pattern with each corresponding variance pattern part
*
* @return returns position worst matrix by max_pattern
*/
int pattern::WorstPattern_max_pat(){
double max_var_pat, i_sum;
int i_max;
max_var_pat = 0.0;
i_max = 0;
for(int i = 0; i < size; i++){
i_sum = 0.0;
for(int j = 0; j < size; j++){ /*If the summation for Pi has the higest value, by adding the corresponding variance part of each other pattern,...*/
i_sum+=var_sum[i][j]; /*...Pi has the highest part at the variance summation. In this case if Pi is changed mayby the variance parts to...*/
} /*...each other pattern is lowered, therefor the variance, which means the variance is better*/
if(max_var_pat < i_sum){
max_var_pat = i_sum;
i_max = i;
}
}
return i_max;
}
/**
* Standardmethod for improvment, using the WorstPattern_max_pat optimization
*
* @param limit
* Number of allowed improvement steps/position changes for all pattern
*/
void pattern::Improve(int limit){
DoImprove(limit, false, true, false);
}
/**
* Method for improvment, using the loop optimization, just start with
* first pattern, try to improve, go on to the next till end, then
* start again with first Pattern.
*
* @param limit
* Number of allowed improvement steps/position changes for all pattern
*/
void pattern::ImproveLoop(int limit){
DoImprove(limit, false, false, true);
}
/**
* Method for improvment, using the WorstPattern_max_val optimization
*
* @param limit
* Number of allowed improveement steps/position changes for all pattern
*/
void pattern::ImproveMaxValue(int limit){
DoImprove(limit, true, false, false);
}
/**
* Method for improvement, using both the WorstPattern_max_pat and
* WorstPattern_max_val optimization. Might be slow.
*
* @param limit
* Number of allowed improvement steps/position changes for all pattern
*/
void pattern::ImproveMaxValuePattern(int limit){
DoImprove(limit, true, true, false);
}
/**
* Activating secure mode: rebuild best_patternset, if improved pattern is
* not better than best_patternset, for each improvement step.
*/
void pattern::ImproveSecure(){
this->secure = true;
}
/**
* The improvement method, using the submitted booleans for correct used improvement
* option.
* @param limit
* Number of allowed improvement steps/position changes for all pattern
*
* @param max_val
* Possible for the WorstPattern_max_val mode
*
* @param max_pat
* Possible for the WorstPattern_max_pat mode
*
* @param loop
* Possible for the loop mode
*/
void pattern::DoImprove(int limit, bool max_val, bool max_pat, bool loop){
std::string patsave1_new, patsave1_old, patsave2_new, patsave2_old, least_pattern;
double tmp_variance, tmp_best_variance, var1, var2;
int worst_max_val, worst_max_pat, least_pos, steps, pat_modulo, counter_best_pat, better_pattern;
bool flag_better;
tmp_best_variance = GetBestVariance();
tmp_variance = GetVariance();
better_pattern = 0;
counter_best_pat = 0;
pat_modulo = counter_best_pat % size;
steps = 0;
if(!silent && quiet){
//std::cout << "First variance: \t" << GetBestVariance() << std::endl;
//std::cout << "First norm_variance: \t" << GetBestNormVariance() << std::endl << std::endl;
}
if(improve){
for(int i = 1; i <= limit; i++){
if(!loop){ /*!!!--> have a look at: int pattern::WorstPattern_max_pat() and int pattern::WorstPattern_max_val() */
flag_better = false;