-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathClustering.h
More file actions
1941 lines (1824 loc) · 69.9 KB
/
Clustering.h
File metadata and controls
1941 lines (1824 loc) · 69.9 KB
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
#ifndef CLUSTERING_H_
#define CLUSTERING_H_
#include "Options.h"
#include <vector>
#include <climits>
#include <list>
#include <regex>
#include <string>
#include <unordered_map>
#include "Timing.h"
using namespace std;
class ClusterCoordinates {
public:
int start;
int end;
bool strand;
char *seq;
GenomePos qStart, qEnd, tStart, tEnd;
int chromIndex;
int coarseSubCluster;
int clusterIndex;
ClusterCoordinates() {
clusterIndex=-1;
qStart=-1;
qEnd=0;
tStart=-1;
tEnd=0;
seq=NULL;
chromIndex=0;
start=0;
end=0;
strand=-1;
coarseSubCluster = -1;
}
bool EncompassesInRectangle(const ClusterCoordinates &b, const float frac) {
int qovp=0;
if (b.qStart >= qStart and b.qStart < qEnd) {
qovp=min(qEnd, b.qEnd)-b.qStart;
}
else if (b.qEnd > qStart and b.qEnd <= qEnd) {
qovp=b.qEnd-max(qStart, b.qStart);
}
else if (b.qStart <= qStart and b.qEnd > qEnd) {
qovp=qEnd-qStart;
}
int tovp=0;
if (b.tStart >= tStart and b.tStart < tEnd) {
tovp=min(tEnd, b.tEnd)-b.tStart;
}
else if (b.tEnd > tStart and b.tEnd <= tEnd) {
tovp=b.tEnd-max(tStart, b.tStart);
}
else if (b.tStart <= tStart and b.tEnd > tEnd) {
tovp=tEnd-tStart;
}
//cerr << "encompass: " << float(qovp)/(b.qEnd-b.qStart) << "\t" << float(tovp)/(b.tEnd-b.tStart) << endl;
return (float(qovp)/(b.qEnd-b.qStart) > frac) and (float(tovp)/(b.tEnd-b.tStart) > frac);
}
bool Encompasses(const ClusterCoordinates &b, const float frac) const {
int qovp=0;
if (b.qStart >= qStart and b.qStart < qEnd) {
qovp=min(qEnd, b.qEnd)-b.qStart;
}
else if (b.qEnd > qStart and b.qEnd <= qEnd) {
qovp=b.qEnd-max(qStart, b.qStart);
}
else if (b.qStart <= qStart and b.qEnd > qEnd) {
qovp=qEnd-qStart;
}
int tovp=0;
if (b.tStart >= tStart and b.tStart < tEnd) {
tovp=min(tEnd, b.tEnd)-b.tStart;
}
else if (b.tEnd > tStart and b.tEnd <= tEnd) {
tovp=b.tEnd-max(tStart, b.tStart);
}
else if (b.tStart <= tStart and b.tEnd > tEnd) {
tovp=tEnd-tStart;
}
return ((float)qovp/(qEnd-qStart) > frac) or ((float)tovp/(tEnd-tStart) > frac);
}
bool OverlapsFracB(const ClusterCoordinates &b, float frac) const {
int ovp=0;
if (b.qStart >= qStart and b.qStart < qEnd) {
ovp=min(qEnd, b.qEnd)-b.qStart;
}
else if (b.qEnd > qStart and b.qEnd < qEnd) {
ovp=b.qEnd-max(qStart, b.qStart);
}
else if (b.qStart <= qStart and b.qEnd > qEnd) {
ovp=qEnd-qStart;
}
float denomA=qEnd-qStart;
float denomB=b.qEnd-b.qStart;
// cerr << "ovp: " << denomA << "\t" << denomB << "\t" << ovp << "\t" << ovp/denomB << endl;
if ( ovp/denomB > frac) { return true; }
else { return false; }
}
bool Overlaps(const ClusterCoordinates &b, float frac) const {
int ovp=0;
if (b.qStart >= qStart and b.qStart < qEnd) {
ovp=min(qEnd, b.qEnd)-b.qStart;
}
else if (b.qEnd > qStart and b.qEnd < qEnd) {
ovp=b.qEnd-max(qStart, b.qStart);
}
else if (b.qStart <= qStart and b.qEnd > qEnd) {
ovp=qEnd-qStart;
}
float denomA=qEnd-qStart;
float denomB=b.qEnd-b.qStart;
if ( max(ovp/denomA, ovp/denomB) > frac) { return true; }
else { return false; }
}
int Overlaps(const ClusterCoordinates &b) const {
int ovp=0;
if (b.qStart >= qStart and b.qStart < qEnd) {
ovp=min(qEnd, b.qEnd)-b.qStart;
}
else if (b.qEnd > qStart and b.qEnd < qEnd) {
ovp=b.qEnd-max(qStart, b.qStart);
}
else if (b.qStart <= qStart and b.qEnd > qEnd) {
ovp=qEnd-qStart;
}
return ovp;
}
float OverlapsRate(const ClusterCoordinates &b) const {
int ovp=0;
if (b.qStart >= qStart and b.qStart < qEnd) {
ovp=min(qEnd, b.qEnd)-b.qStart;
}
else if (b.qEnd > qStart and b.qEnd < qEnd) {
ovp=b.qEnd-max(qStart, b.qStart);
}
else if (b.qStart <= qStart and b.qEnd > qEnd) {
ovp=qEnd-qStart;
}
float denomA=qEnd-qStart;
float denomB=b.qEnd-b.qStart;
return max(ovp/denomA, ovp/denomB);
}
bool OverlapsOnRead(int & ReadLength, float frac) const {
if (((float)(qEnd - qStart))/ReadLength > frac) {return true;}
else { return false;}
}
ClusterCoordinates(int s,int e) : start(s), end(e) {
qStart=qEnd=tStart=tEnd=strand=0;
seq=NULL;
chromIndex=0;
}
ClusterCoordinates(int s,int e, int st) : start(s), end(e), strand(st) {
qStart=qEnd=tStart=tEnd=0;
seq=NULL;
chromIndex=0;
}
ClusterCoordinates(int s, int e,
GenomePos qs, GenomePos qe,
GenomePos ts, GenomePos te,
int st) : start(s), end(e), strand(st), qStart(qs), qEnd(qe), tStart(ts), tEnd(te) {
chromIndex=-1;
seq=NULL;
}
ClusterCoordinates(int s, int e,
GenomePos qs, GenomePos qe,
GenomePos ts, GenomePos te,
int st, int coarseSC) : start(s), end(e), strand(st), qStart(qs), qEnd(qe), tStart(ts), tEnd(te), coarseSubCluster(coarseSC) {
chromIndex=-1;
seq=NULL;
}
};
class Cluster : public ClusterCoordinates {
public:
GenomePairs matches;
vector<int> strands; // stores the strand of every GenomePair in matches
vector<int> splitmatchindex; // stores the index for the split clusters
int64_t maxDiagNum;
int64_t minDiagNum; // maxDiagNum and minDiagNum defines diagonal band boundary of the current cluster
int coarse;
int Val; // Val stores the value of each Cluster;(using in SDP)
int NumofAnchors0; // NumofAnchors0 stores the # of anchors of each splitcluster
vector<int> matchesLengths; // store the length of each anchor
bool refined; // refined == 0 means this Cluster has not been refined yet
bool refinespace; // refinespace == 0 means this Cluster has not been add anchors in the step of RefineBtwnSpace;
int outerCluster;
int rank;
bool split; // whether to enter the splitting step
float anchorfreq;
float refineEffiency;
int matchStart;
vector<bool> overlap;
bool flip = 0;
Cluster() {refined=0; refinespace = 0; coarse=-1; rank=-1; flip = 0; split = 0;}
Cluster(int s, int e) : ClusterCoordinates(s,e) { coarse=-1; refined=0; flip = 0; split= 0;}
Cluster(int s, int e, int st) : ClusterCoordinates(s,e,st) { coarse=-1; refined=0; refinespace = 0; flip = 0; split = 0;}
Cluster(int s, int e,
GenomePos qs, GenomePos qe,
GenomePos ts, GenomePos te,
int st) : ClusterCoordinates(s,e,qs,qe,ts,te,st) { coarse=-1; refinespace = 0; refined=0; flip = 0; split = 0;}
Cluster(int s, int e,
GenomePos qs, GenomePos qe,
GenomePos ts, GenomePos te,
int st, int cs) : ClusterCoordinates(s,e,qs,qe,ts,te,st) { coarse=cs; refined=0; refinespace = 0; flip = 0; split = 0;}
Cluster(int s, int e,
GenomePos qs, GenomePos qe,
GenomePos ts, GenomePos te, int st,
GenomePairs::iterator gpBegin, GenomePairs::iterator gpEnd) : ClusterCoordinates(s,e,qs,qe,ts,te,st) {
copy(gpBegin, gpEnd, back_inserter(matches));
coarse=-1;
maxDiagNum=0;
minDiagNum=0;
refined = 0;
refinespace = 0;
Val = 0;
NumofAnchors0 = 0;
flip = 0;
split = 0;
}
Cluster(int s, int e,
GenomePos qs, GenomePos qe,
GenomePos ts, GenomePos te, int st,
GenomePairs::iterator gpBegin, GenomePairs::iterator gpEnd, vector<int>::iterator stBegin, vector<int>::iterator stEnd) : ClusterCoordinates(s,e,qs,qe,ts,te,st) {
copy(gpBegin, gpEnd, back_inserter(matches));
copy(stBegin, stEnd, back_inserter(strands));
coarse=-1;
maxDiagNum=0;
minDiagNum=0;
Val = 0;
NumofAnchors0 = 0;
flip = 0;
refined = 0;
refinespace = 0;
split = 0;
}
Cluster(GenomePos qs, GenomePos qe, GenomePos ts, GenomePos te, int st, int coa) {
qStart = qs;
qEnd = qe;
tStart = ts;
tEnd = te;
strand = st;
coarse = coa;
Val = 0;
NumofAnchors0 = 0;
split = 0;
flip = 0;
refined = 0;
refinespace = 0;
}
Cluster(int st, GenomePairs::iterator gpBegin, GenomePairs::iterator gpEnd, vector<int>::iterator stBegin, vector<int>::iterator stEnd) {
strand = st;
copy(gpBegin, gpEnd, back_inserter(matches));
}
bool OverlapsPrevious(const Cluster &prev) {
//
// Assume clusters are sorted by target.
//
if (prev.strand == strand and
prev.tEnd >= tStart and
prev.tStart < tStart and
prev.qEnd >= qStart and
prev.qStart < qStart) {
return true;
}
else {
return false;
}
}
void UpdateBoundaries(const Cluster &rhs) {
tEnd = max(tEnd, rhs.tEnd);
tStart = min(tStart, rhs.tStart);
qEnd = max(qEnd, rhs.qEnd);
qStart = min(qStart, rhs.qStart);
}
int size() const {
return matches.size();
}
int operator<(const Cluster &rhs) const {
if (strand != rhs.strand) {
return strand != rhs.strand;
}
else if (tStart != rhs.tStart) {
return tStart < rhs.tStart;
}
else {
return qStart < rhs.qStart;
}
}
void SetClusterBoundariesFromMatches (const Options &opts, bool append_prev_cluster=0) {
if (!append_prev_cluster) {
qStart = matches[0].first.pos;
qEnd = qStart + opts.globalK;
tStart = matches[0].second.pos;
tEnd = tStart + opts.globalK;
}
for (int i = 1; i < matches.size(); i++) {
tEnd = max(tEnd, matches[i].second.pos + opts.globalK);
tStart = min(tStart, matches[i].second.pos);
qEnd = max(qEnd, matches[i].first.pos + opts.globalK);
qStart = min(qStart, matches[i].first.pos);
}
refineEffiency = ((float) matches.size()) / min(qEnd - qStart, tEnd - tStart);
}
//
// This function decide the chromIndex
//
bool CHROMIndex(Genome & genome) {
if (matches.size() == 0) return 1;
int firstChromIndex = genome.header.Find(tStart + 1);
int lastChromIndex;
lastChromIndex = genome.header.Find(tEnd);
if (firstChromIndex != lastChromIndex) {
return 1;
}
chromIndex = firstChromIndex;
return 0;
}
GenomePos GetqStart(int i) {
return matches[i].first.pos;
}
GenomePos GetqEnd(int i) {
return matches[i].first.pos + matchesLengths[i];
}
GenomePos GettStart(int i) {
return matches[i].second.pos;
}
GenomePos GettEnd(int i) {
return matches[i].second.pos + matchesLengths[i];
}
};
class Cluster_SameDiag {
public:
vector<int> start;
vector<int> end;
Cluster * cluster;
GenomePos tStart, tEnd, qStart, qEnd;
bool strand;
int matchStart;
int coarse;
float anchorfreq;
int chromIndex;
Cluster_SameDiag() {}
Cluster_SameDiag(Cluster * c) : cluster(c) {
tStart = cluster->tStart;
tEnd = cluster->tEnd;
qStart = cluster->qStart;
qEnd = cluster->qEnd;
strand = cluster->strand;
matchStart = -1;
coarse = -1;
anchorfreq = cluster->anchorfreq;
chromIndex = cluster->chromIndex;
}
int length(int i) {
if (cluster->matches[end[i] - 1].first.pos + cluster->matchesLengths[end[i] - 1] >= cluster->matches[start[i]].first.pos) {
return (cluster->matches[end[i] - 1].first.pos + cluster->matchesLengths[end[i] - 1] - cluster->matches[start[i]].first.pos);
}
else {return 0;}
}
GenomePos GetqStart(int i) {
return cluster->matches[start[i]].first.pos;
}
GenomePos GetqEnd(int i) {
return cluster->matches[end[i] - 1].first.pos + length(i);
}
GenomePos GettStart(int i) {
if (strand == 0) return cluster->matches[start[i]].second.pos;
else return cluster->matches[end[i] - 1].second.pos;
}
GenomePos GettEnd(int i) {
if (strand == 0) return cluster->matches[start[i]].second.pos + length(i);
return cluster->matches[end[i] - 1].second.pos + length(i);
}
int size() {
return start.size();
}
float OverlaprateOnGenome(const Cluster_SameDiag *b) const {
int ovp=0;
if (tEnd <= b->tStart or b->tEnd <= tStart) {
return 0;
}
ovp = min(tEnd, b->tEnd) - max(tStart, b->tStart);
float denomA = tEnd - tStart;
// float denomB = b->tEnd - b->tStart;
return ovp/denomA;
}
int OverlapOnGenome(const Cluster_SameDiag *b) const {
int ovp=0;
if (tEnd <= b->tStart or b->tEnd <= tStart) {
return 0;
}
ovp = min(tEnd, b->tEnd) - max(tStart, b->tStart);
return ovp;
}
};
class OrderClusterBySize {
public:
int operator()(const Cluster &a, const Cluster &b) {
return a.size() > b.size();
}
};
class ClusterOrder {
public:
vector<Cluster> *clusters;
vector<int> index;
int orderType;
ClusterOrder(vector<Cluster> *c, int t=0) : clusters(c), orderType(t) {
index.resize(clusters->size());
for (int i=0;i<index.size();i++) { index[i]=i;}
Sort();
}
//
// Cartesian sort of clusters.
//
int operator()(const int i, const int j) {
if (orderType == 1) {
return (*clusters)[i].size() > (*clusters)[j].size();
}
else {
assert((*clusters)[i].strand == 0 or (*clusters)[i].strand == 1);
assert((*clusters)[j].strand == 0 or (*clusters)[j].strand == 1);
if ((*clusters)[i].tStart != (*clusters)[j].tStart) {
return (*clusters)[i].tStart < (*clusters)[j].tStart;
}
else {
return (*clusters)[i].qStart < (*clusters)[j].qStart;
}
}
}
template<typename t>
void Sort() {
sort(index.begin(), index.end(), t());
}
void Sort() {
sort(index.begin(), index.end(), *this);
}
Cluster & operator[](int i) {
return (*clusters)[index[i]];
}
int size() {
return index.size();
}
};
class Clusters_valueOrder {
public:
vector<float> *clusters_value;
vector<int> index;
Clusters_valueOrder(vector<float> *c) : clusters_value(c) {
index.resize((*clusters_value).size());
for (int i=0;i<index.size();i++) { index[i]=i;}
Sort();
}
int operator()(const int i, const int j) {
assert(i < clusters_value->size());
assert(j < clusters_value->size());
return (*clusters_value)[i] > (*clusters_value)[j];
}
void Sort() {
sort(index.begin(), index.end(), *this);
}
float & operator[](int i) {
return (*clusters_value)[index[i]];
}
int size() {
return index.size();
}
};
template<typename Tup>
long DiagonalDifference(Tup &a, Tup &b, int strand=0) {
if (strand == 0) { // Matches
long aDiag = (long)a.second.pos - (long)a.first.pos;
long bDiag = (long)b.second.pos - (long)b.first.pos;
return aDiag - bDiag;
}
else { // revMathches
long aDiag = a.first.pos + a.second.pos;
long bDiag= b.first.pos + b.second.pos;
return aDiag - bDiag;
}
}
template<typename Tup>
int DiagonalDrift(long curDiag, Tup &t, int strand=0) {
long drift;
if (strand == 0) drift= abs(curDiag - ((long)t.second.pos - (long)t.first.pos));
else drift= abs(curDiag - ((long)t.first.pos + (long)t.second.pos));
return drift;
}
template<typename Tup>
long maxGapDifference(Tup &a, Tup &b) {
long aDiff = abs((long)b.first.pos - (long)a.first.pos);
long bDiff = abs((long)b.second.pos - (long)a.second.pos);
return max(aDiff, bDiff);
}
template<typename Tup>
long minGapDifference (Tup &a, Tup &b) {
long aDiff = abs((long)b.first.pos - (long)a.first.pos);
long bDiff = abs((long)b.second.pos - (long)a.second.pos);
return min(aDiff, bDiff);
}
template<typename Tup>
long GapDifference (Tup &a, Tup &b, const int len) {
long Diff = abs((long)b.first.pos - ((long)a.first.pos + len));
return Diff;
}
bool sign(int val) {
if (val >= 0) return true;
return false;
}
template<typename Tup>
void AVGfreq(int as, int ae, vector<pair<Tup, Tup> > &matches, float &avgfreq) {
unordered_map<Tuple, int> miniCount;
for (int r = as; r < ae; r++) {
auto got = miniCount.find(matches[r].first.t);
if (got == miniCount.end()) {
miniCount[matches[r].first.t] = 1;
}
else { got->second += 1;}
}
// for (auto& x: miniCount) {
// if (x.second > 1) {nonunique++;}
// }
avgfreq = (float)(ae - as) / miniCount.size();
}
template<typename Tup>
void CleanOffDiagonal(Genome &genome, vector<Cluster> &clusters, vector<pair<Tup, Tup> > &matches, vector<float> &matches_freq, const Options &opts, Read &read, int strand=0, int diagOrigin=-1, int diagDrift=-1) {
if (matches.size() == 0) {
return;
}
vector<bool> onDiag(matches.size(), false);
int nOnDiag=0;
if (matches.size() > 1 and abs(DiagonalDifference(matches[0], matches[1], strand)) < opts.cleanMaxDiag and (diagOrigin == -1 or DiagonalDrift(diagOrigin, matches[0], strand) < diagDrift)) {
onDiag[0] = true;
nOnDiag++;
}
for (int i = 1; i < matches.size(); i++) {
if (abs(DiagonalDifference(matches[i], matches[i-1], strand)) < opts.cleanMaxDiag and (diagOrigin == -1 or DiagonalDrift(diagOrigin, matches[i],strand) < diagDrift)) {
// onDiag[i] = true;
onDiag[i-1] = true;
nOnDiag++;
}
}
bool prevOnDiag = false;
int diagStart;
int Largest_ClusterNum = 0;
bool diagStartSet=false;
for (int i = 0; i < matches.size(); i++) {
if (prevOnDiag == false and onDiag[i] == true) {
diagStart = i;
diagStartSet=true;
}
if (prevOnDiag == true and onDiag[i] == false) {
Largest_ClusterNum = max(Largest_ClusterNum, i - diagStart + 1); // [diagStart, i]
}
prevOnDiag = onDiag[i];
}
int sz = matches.size();
if (diagStartSet == false) {
matches.clear();
return;
}
assert(diagStartSet==true);
Largest_ClusterNum = max(Largest_ClusterNum, sz - diagStart);
int minDiagCluster = (int) floor(Largest_ClusterNum/10);
if (minDiagCluster >= opts.minDiagCluster) minDiagCluster = opts.minDiagCluster;
// cerr << "Largest_ClusterNum: " << Largest_ClusterNum << " minDiagCluster: " << minDiagCluster << endl;
// if (opts.readType == Options::contig) {minDiagCluster = 30;}
//
// Remove bins with number of anchors <= minDiagCluster
//
vector<int> count(onDiag.size(), -1);
vector<bool> Second_onDiag(onDiag.size(), false); // cannot change onDiag in the second round
int counter = 0;
prevOnDiag = false;
if (minDiagCluster >= 0) {
for (int i = 0; i < matches.size(); i++) {
if (prevOnDiag == false and onDiag[i] == true) { diagStart = i; }
if (prevOnDiag == true and onDiag[i] == false) {
float avgfreq = 0; int nonunique = 0;
if (i - diagStart + 1 < minDiagCluster) { // used to be minDiagCluster not opts.minDiagCluster
for (int j = diagStart; j <= i; j++) { // throw away!
Second_onDiag[j] = false;
}
}
else {
AVGfreq(diagStart, i + 1, matches, avgfreq);
for (int j = diagStart; j <= i; j++) {
matches_freq[j] = avgfreq;
}
int MinDiagCluster = 0;
if (opts.bypassClustering) {
if (avgfreq >= 3.0f and i - diagStart + 1 < 10) {
for (int j = diagStart; j <= i; j++) { // throw away
Second_onDiag[j] = false;
}
}
else if (avgfreq >= 2.0f and i - diagStart + 1 >= opts.cleanClustersize) {
MinDiagCluster = opts.SecondCleanMinDiagCluster + floor((avgfreq - 1.5f)/ 1.0f) * opts.punish_anchorfreq +
floor((i - diagStart + 1 - opts.cleanClustersize)/opts.cleanClustersize) * opts.anchorPerlength;
SecondRoundCleanOffDiagonal(count, counter, matches, MinDiagCluster, opts.SecondCleanMaxDiag, Second_onDiag, diagStart, i + 1, opts, avgfreq, strand, diagOrigin, diagDrift);
}
else if (avgfreq >= 1.5f and i - diagStart + 1 >= opts.cleanClustersize) {
MinDiagCluster = opts.SecondCleanMinDiagCluster + floor((avgfreq - 1.5f)/ 1.5f) * opts.punish_anchorfreq +
floor((i - diagStart + 1 - opts.cleanClustersize)/opts.cleanClustersize) * opts.anchorPerlength;
SecondRoundCleanOffDiagonal(count, counter, matches, MinDiagCluster, opts.SecondCleanMaxDiag, Second_onDiag, diagStart, i + 1, opts, avgfreq, strand, diagOrigin, diagDrift);
}
else {
for (int j = diagStart; j <= i; j++) {
Second_onDiag[j] = true;
count[j] = counter;
}
}
}
else {
if (avgfreq >= 3.0f and i - diagStart + 1 < 10) {
for (int j = diagStart; j <= i; j++) {
Second_onDiag[j] = false;
}
}
else if (avgfreq >= 4.0f and i - diagStart + 1 >= opts.cleanClustersize) {
MinDiagCluster = opts.SecondCleanMinDiagCluster + floor((avgfreq - 1.5f)/ 1.0f) * opts.punish_anchorfreq +
floor((i - diagStart + 1 - opts.cleanClustersize)/opts.cleanClustersize) * opts.anchorPerlength;
SecondRoundCleanOffDiagonal(count, counter, matches, MinDiagCluster, opts.SecondCleanMaxDiag, Second_onDiag, diagStart, i + 1, opts, avgfreq, strand, diagOrigin, diagDrift);
}
else if (avgfreq >= 1.5f and i - diagStart + 1 >= opts.cleanClustersize) {
MinDiagCluster = opts.SecondCleanMinDiagCluster + floor((avgfreq - 1.5f)/ 1.5f) * opts.punish_anchorfreq +
floor((i - diagStart + 1 - opts.cleanClustersize)/opts.cleanClustersize) * opts.anchorPerlength;
SecondRoundCleanOffDiagonal(count, counter, matches, MinDiagCluster, opts.SecondCleanMaxDiag, Second_onDiag, diagStart, i + 1, opts, avgfreq, strand, diagOrigin, diagDrift);
}
else if (avgfreq > 1.0f and i - diagStart + 1 >= opts.cleanClustersize) {
MinDiagCluster = opts.SecondCleanMinDiagCluster - (5 - floor((avgfreq - 1.0f) / 0.1f)) * (opts.punish_anchorfreq/2) +
floor((i - diagStart + 1 - opts.cleanClustersize)/opts.cleanClustersize) * (opts.anchorPerlength / 2);
SecondRoundCleanOffDiagonal(count, counter, matches, MinDiagCluster, opts.SecondCleanMaxDiag, Second_onDiag, diagStart, i + 1, opts, avgfreq, strand, diagOrigin, diagDrift);
}
else if (avgfreq > 1.0f) {
MinDiagCluster = opts.SecondCleanMinDiagCluster - (5 - floor((avgfreq - 1.0f) / 0.1f)) * (opts.punish_anchorfreq/2) -
floor((opts.cleanClustersize - i + diagStart - 1)/15) * (opts.anchorPerlength / 2);
SecondRoundCleanOffDiagonal(count, counter, matches, MinDiagCluster, opts.SecondCleanMaxDiag, Second_onDiag, diagStart, i + 1, opts, avgfreq, strand, diagOrigin, diagDrift);
}
else {
for (int j = diagStart; j <= i; j++) {
Second_onDiag[j] = true;
count[j] = counter;
}
}
}
if (read.name == opts.readname) cerr << "avgfreq: " << avgfreq << " MinDiagCluster: " << MinDiagCluster << endl;
}
if (opts.dotPlot and !opts.readname.empty() and read.name == opts.readname and strand == 0) {
ofstream fclust("for-matches_cleanoffdiagonal.dots", ofstream::app);
for (int j = diagStart; j <= i; j++) {
fclust << matches[j].first.pos << "\t" << matches[j].second.pos << "\t" << opts.globalK + matches[j].first.pos << "\t"
<< matches[j].second.pos + opts.globalK << "\t" << counter << "\t" << avgfreq << "\t" << Second_onDiag[j] << endl;
}
fclust.close();
}
if (opts.dotPlot and !opts.readname.empty() and read.name == opts.readname and strand == 1){
ofstream rclust("rev-matches_cleanoffdiagonal.dots", ofstream::app);
for (int j = diagStart; j <= i; j++) {
rclust << matches[j].first.pos << "\t" << matches[j].second.pos + opts.globalK << "\t" << opts.globalK + matches[j].first.pos << "\t"
<< matches[j].second.pos << "\t" << counter << "\t" << avgfreq << "\t" << Second_onDiag[j] << endl;
}
rclust.close();
}
counter++;
}
prevOnDiag = onDiag[i];
}
}
else {
for (int i = 0; i < matches.size(); i++) {Second_onDiag[i] = false;}
}
int c = 0;
for (int i = 0; i < matches.size(); i++) {
if (Second_onDiag[i]) {
matches[c] = matches[i];
matches_freq[c] = matches_freq[i];
assert(count[i] != -1);
count[c] = count[i];
c++;
}
}
matches.resize(c);
matches_freq.resize(c);
count.resize(c);
if (opts.ExtractDiagonalFromClean) {
//
// Store diagonal in clusters
//
int count_s = 0; int c = 1;
while (c < count.size()) {
if (count[c] == count[c-1]) {c++; continue;}
GenomePos qStart = matches[count_s].first.pos,
qEnd = matches[count_s].first.pos + opts.globalK,
tStart = matches[count_s].second.pos,
tEnd = matches[count_s].second.pos + opts.globalK;
for (int b = count_s; b < c; b++) {
qStart = min(qStart, matches[b].first.pos);
qEnd = max(qEnd, matches[b].first.pos + opts.globalK);
tStart = min(tStart, matches[b].second.pos);
tEnd = max(tEnd, matches[b].second.pos + opts.globalK);
}
assert(count_s < c);
if (opts.bypassClustering) { // needs to insert matches
clusters.push_back(Cluster(count_s, c, qStart, qEnd, tStart, tEnd, strand));
for (int b = count_s; b < c; b++) {
clusters.back().matches.push_back(matches[b]);
}
clusters.back().anchorfreq = matches_freq[count_s];
clusters.back().SetClusterBoundariesFromMatches(opts);
int rci = genome.header.Find(clusters.back().tStart);
clusters.back().chromIndex = rci;
}
else {
clusters.push_back(Cluster(count_s, c, qStart, qEnd, tStart, tEnd, strand));
clusters.back().anchorfreq = matches_freq[count_s];
}
count_s = c;
c++;
}
if (c == count.size() and count_s < c) {
GenomePos qStart = matches[count_s].first.pos,
qEnd = matches[count_s].first.pos + opts.globalK,
tStart = matches[count_s].second.pos,
tEnd = matches[count_s].second.pos + opts.globalK;
for (int b = count_s; b < c; b++) {
qStart = min(qStart, matches[b].first.pos);
qEnd = max(qEnd, matches[b].first.pos + opts.globalK);
tStart = min(tStart, matches[b].second.pos);
tEnd = max(tEnd, matches[b].second.pos + opts.globalK);
}
assert(count_s < c);
if (opts.bypassClustering) { // needs to insert matches
clusters.push_back(Cluster(count_s, c, qStart, qEnd, tStart, tEnd, strand));
for (int b = count_s; b < c; b++) {
clusters.back().matches.push_back(matches[b]);
}
clusters.back().anchorfreq = matches_freq[count_s];
clusters.back().SetClusterBoundariesFromMatches(opts);
int rci = genome.header.Find(clusters.back().tStart);
clusters.back().chromIndex = rci;
}
else {
clusters.push_back(Cluster(count_s, c, qStart, qEnd, tStart, tEnd, strand));
clusters.back().anchorfreq = matches_freq[count_s];
}
}
}
}
template<typename Tup>
void SecondRoundCleanOffDiagonal(vector<int> &count, int out_counter, vector<pair<Tup, Tup> > &matches, int MinDiagCluster,int CleanMaxDiag, vector<bool> &OriginalOnDiag, int os, int oe,
const Options &opts, float avgfreq, int strand=0, int diagOrigin=-1, int diagDrift=-1) {
if (MinDiagCluster >= oe - os) return;
if (MinDiagCluster <= 0) {
for (int i = os; i < oe; i++) {
OriginalOnDiag[i] = true;
count[i] = out_counter;
}
return;
}
if (oe - os <= 1) return;
//
// starting from forward order
//
int nOnDiag2=0;
vector<bool> foward_onDiag(oe - os, false);
vector<bool> reverse_onDiag(oe - os, false);
for (int i = os + 1; i < oe; i++) {
if (abs(DiagonalDifference(matches[i], matches[i - 1], strand)) < CleanMaxDiag
and (diagOrigin == -1 or DiagonalDrift(diagOrigin, matches[i],strand) < diagDrift)) {
foward_onDiag[i - 1 - os] = true;
nOnDiag2++;
}
}
bool prevOnDiag = false; int diagStart; int counter = 0;
for (int i = os; i < oe; i++) {
if (prevOnDiag == false and foward_onDiag[i - os] == true) { diagStart = i;}
if (prevOnDiag == true and foward_onDiag[i - os] == false) {
if (i - diagStart + 1 < MinDiagCluster) { // [diagStart, i]
for (int j = diagStart; j <= i; j++) { foward_onDiag[j - os] = false; }
}
else { foward_onDiag[i - os] = true; }
counter++;
}
prevOnDiag = foward_onDiag[i - os];
}
//
// starting from reverse order
//
for (int i = oe - 2; i >= os; i--) {
if (abs(DiagonalDifference(matches[i], matches[i + 1], strand)) < CleanMaxDiag
and (diagOrigin == -1 or DiagonalDrift(diagOrigin, matches[i],strand) < diagDrift)) {
reverse_onDiag[i + 1 - os] = true;
nOnDiag2++;
}
}
prevOnDiag = false; counter = 0;
for (int i = oe - 1; i >= os; i--) {
if (prevOnDiag == false and reverse_onDiag[i - os] == true) { diagStart = i;}
if (prevOnDiag == true and reverse_onDiag[i - os] == false) {
if (diagStart - i + 1 < MinDiagCluster) { // [diagStart, i]
for (int j = i; j <= diagStart; j++) { reverse_onDiag[j - os] = false; }
}
else { reverse_onDiag[i - os] = true; }
counter++;
}
prevOnDiag = reverse_onDiag[i - os];
}
for (int i = os; i < oe; i++) {
if (foward_onDiag[i - os] == true and reverse_onDiag[i - os] == true) {
OriginalOnDiag[i] = true;
count[i] = out_counter;
}
else {OriginalOnDiag[i] = false;}
}
}
template<typename Tup>
void PrintDiagonal(vector<pair<Tup, Tup> > &matches, int strand=0) {
for (int m=1; m < matches.size(); m++) {
long d=DiagonalDifference(matches[m], matches[m-1], strand);
cerr << matches[m-1].first.pos << "\t" << matches[m].first.pos << "\t" << matches[m-1].second.pos << "\t" << matches[m].second.pos << "\t" << d << endl;
}
}
template<typename Tup>
long GetDiag(pair<Tup, Tup> &match, int strand, const Options &opts) {
if (strand == 0) return (long) match.second.pos - (long) ceil(opts.slope*match.first.pos);
else return (long) ceil(opts.slope*match.first.pos) + (long) match.second.pos;
}
template<typename Tup>
long GetDiag(pair<Tup, Tup> &match, int &len, bool strand) {
if (strand == 0) return (long) match.second.pos - (long) match.first.pos;
else return (long) match.first.pos + (long) match.second.pos + len;
}
template<typename Tup>
void StoreFineClusters(int ri, vector<pair<Tup, Tup> > &matches, float anchorfreq, vector<Cluster> &clusters, const Options &opts, vector<int> &splitmatchindex,
Genome &genome, Read &read, GenomePos readLength, int strand=0, int outerIteration=0) {
//
// StoreFineClusters based on unique part
// The first step: store the number of matches corresponding to each minimizer from the read; -- a linear pass
// matches are sorted in cartesian order; (first, second)
//
if (splitmatchindex.size() == 1) return;
if (fabs(anchorfreq - 1.0f) <= 0.005) {
clusters.push_back(Cluster(0, 0, strand));
for (int i = 0; i < splitmatchindex.size(); i++) {
clusters.back().matches.push_back(matches[splitmatchindex[i]]);
}
clusters.back().SetClusterBoundariesFromMatches(opts);
clusters.back().chromIndex = ri;
clusters.back().anchorfreq = 1.0f;
// cerr << "clusters.back().anchorfreq: " << clusters.back().anchorfreq << endl;
if (clusters.back().CHROMIndex(genome) == 1) {
clusters.pop_back();
}
if (opts.dotPlot and !opts.readname.empty() and read.name == opts.readname) {
ofstream fineclusters("fineclusters_byunique.tab", std::ofstream::app);
if (clusters.size() > 0) {
for (int h = 0; h < clusters.back().matches.size(); h++) {
if (strand == 0) {
fineclusters << clusters.back().matches[h].first.pos << "\t"
<< clusters.back().matches[h].second.pos << "\t"
<< clusters.back().matches[h].first.pos + opts.globalK << "\t"
<< clusters.back().matches[h].second.pos + opts.globalK << "\t"
<< outerIteration << "\t"
<< clusters.size() - 1 << "\t"
<< strand << endl;
}
else {
fineclusters << clusters.back().matches[h].first.pos << "\t"
<< clusters.back().matches[h].second.pos + opts.globalK << "\t"
<< clusters.back().matches[h].first.pos + opts.globalK << "\t"
<< clusters.back().matches[h].second.pos<< "\t"
<< outerIteration << "\t"
<< clusters.size() - 1 << "\t"
<< strand << endl;
}
}
}
fineclusters.close();
}
return;
}
vector<int> match_num;
vector<int> pos_start;
int oc = 1;
int us = 0;
for (int i = 1; i < splitmatchindex.size(); i++) {
if (matches[splitmatchindex[i]].first.pos == matches[splitmatchindex[i - 1]].first.pos) {
oc++;
}
else {
match_num.push_back(oc);
pos_start.push_back(us);
us = i;
oc = 1;
}
if (i == splitmatchindex.size() - 1) {
match_num.push_back(oc);
pos_start.push_back(us);
}
}
//
// Find stretches of "1" on a diagonal in match_num; -- a linear pass
//
int u_start = 0, u_end = 0, u_maxstart = 0, u_maxend = 0;
int max_pos = 0;
bool reset = 0; // reset == 0 means a new stretch of "1";
vector<int> Start;
vector<int> End;
if (match_num.size() == 1) {
assert(pos_start[0] == 0);
u_maxstart = 0;
u_maxend = 1;
Start.push_back(u_maxstart);
End.push_back(u_maxend);
}
else {
int k = 0;
while (k < match_num.size() - 1) {
// Find a start of a stretch
while (k < match_num.size() - 1 and match_num[k] != 1) {
// (Check) if reset == 1, push u_start and u_end. reset = 0
k++;
}
assert(reset == 0);
u_start = k;
u_end = k + 1;
reset = 1;
while (k < match_num.size() - 1 and match_num[k+1] == match_num[k]
and abs(DiagonalDifference(matches[splitmatchindex[pos_start[k+1]]], matches[splitmatchindex[pos_start[k]]], strand)) < opts.maxDiag
and minGapDifference(matches[splitmatchindex[pos_start[k+1]]], matches[splitmatchindex[pos_start[k]]]) <= opts.maxGap) {
u_end = k + 2;
k++;
// reset = 1;
}
// insert a new stretch of unique minimizers
Start.push_back(u_start);
End.push_back(u_end);
reset = 0;
k++;
if ((u_maxstart == 0 and u_maxend == 0) or (u_maxend - u_maxstart < u_end - u_start)) {
u_maxstart = u_start;