-
Notifications
You must be signed in to change notification settings - Fork 13
/
GapAssem.cpp
1413 lines (1352 loc) · 38.3 KB
/
GapAssem.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
#include "GapAssem.h"
const unsigned char GA_flag_IS_REF=0;
const unsigned char GA_flag_HAS_PARENT=1;
const unsigned char GA_flag_BAD_ALIGN=7;
const unsigned char GA_flag_PREPPED=5;
//bool GASeq::debug=false;
bool MSAColumns::removeConsGaps = true;
bool MSAColumns::refineClipping = true;
//unsigned int GSeqAlign::counter = 0;
int qsortnuc(const void* p1, const void* p2) {
GAlnColumn::NucCount* c1 = (GAlnColumn::NucCount*) p1;
GAlnColumn::NucCount* c2 = (GAlnColumn::NucCount*) p2;
return (c1->count > c2->count) ? -1 : ((c1->count < c2->count) ? 1 : 0);
}
int compareOrdnum(void* p1, void* p2) {
int v1 = ((GSeqAlign*) p1)->ordnum;
int v2 = ((GSeqAlign*) p2)->ordnum;
return (v1 < v2) ? -1 : ((v1 > v2) ? 1 : 0);
}
int compareCounts(void* p1, void* p2) {
int v1 = ((GSeqAlign*) p1)->Count();
int v2 = ((GSeqAlign*) p2)->Count();
return (v1 > v2) ? -1 : ((v1 < v2) ? 1 : 0);
}
GASeq::GASeq(const char* sname, const char* sdescr, const char* sseq, int slen, int soffset) :
FastaSeq(sname, sdescr, sseq, slen),
numgaps(0), ofs(NULL), delops(false, true, false),flags(0),msa(NULL),
msaidx(-1), seqlen(slen), offset(soffset),
ng_ofs(soffset),revcompl(0), ext5(0), ext3(0),
clp5(0), clp3(0) {
seqlen = len; //FastaSeq constructor settles it
if (seqlen>0) {
GCALLOC(ofs, seqlen * sizeof(short));
#ifdef ALIGN_COVERAGE_DATA
GCALLOC(cov,seqlen*sizeof(int));
#endif
}
}
GASeq::GASeq(const char* sname, int soffset, int slen, int sclipL, int sclipR,
char rev) : FastaSeq(sname),
numgaps(0), ofs(NULL), delops(false, true, false),flags(0),msa(NULL),
msaidx(-1), seqlen(slen), offset(soffset),
ng_ofs(soffset),revcompl(rev), ext5(0), ext3(0),
clp5(sclipL), clp3(sclipR) {
if (seqlen>0) {
GCALLOC(ofs, seqlen * sizeof(short));
#ifdef ALIGN_COVERAGE_DATA
GCALLOC(cov,seqlen*sizeof(int));
#endif
}
}
GASeq::GASeq(GASeq& aseq): FastaSeq(aseq.id, aseq.descr, aseq.seq, aseq.len),
numgaps(0), ofs(NULL), delops(false, true, false),flags(0),msa(NULL),
msaidx(-1), seqlen(aseq.len), offset(0),
ng_ofs(0),revcompl(0), ext5(0), ext3(0),
clp5(0), clp3(0) {
if (seqlen>0) {
GCALLOC(ofs, seqlen * sizeof(short));
#ifdef ALIGN_COVERAGE_DATA
GCALLOC(cov,seqlen*sizeof(int));
#endif
}
}
GASeq::GASeq(FastaSeq& faseq, bool takeover):FastaSeq(faseq, takeover),
numgaps(0), ofs(NULL), delops(false, true, false),flags(0),msa(NULL),
msaidx(-1), seqlen(len), offset(0),
ng_ofs(0),revcompl(0), ext5(0), ext3(0),
clp5(0), clp3(0) {
if (seqlen>0) {
GCALLOC(ofs, seqlen * sizeof(short));
#ifdef ALIGN_COVERAGE_DATA
GCALLOC(cov,seqlen*sizeof(int));
#endif
}
}
GASeq::~GASeq() {
GFREE(ofs);
#ifdef ALIGN_COVERAGE_DATA
GFREE(cov);
#endif
}
/*
void GASeq::loadProcessing() {
//process all delops
for (int i = 0; i < delops->Count(); i++) {
SeqDelOp& delop = *delops->Get(i);
int pos = delop.revcompl ? len - delop.pos - 1 : delop.pos;
removeBase(pos);
}
if (revcompl == 1)
reverseComplement();
}
*/
void GASeq::finalize() {
if (this->len==0) GError("Error: sequence for %s not loaded!\n",this->getId());
if (!this->hasFlag(GA_flag_PREPPED)) this->prepSeq();
}
void GASeq::prepSeq() {
//should only be called once (use hasFlag() before calling)
//apply all deletions to the sequence
for (int i = 0; i < delops.Count(); i++) {
SeqDelOp& delop = *delops.Get(i);
int pos = delop.revcompl ? len - delop.pos - 1 : delop.pos;
removeBase(pos);
}
if (revcompl == 1)
reverseComplement();
setFlag(GA_flag_PREPPED);
}
//set the gap length in this position
void GASeq::setGap(int pos, short gaplen) {
if (pos < 0 || pos >= seqlen)
GError("Error: invalid gap position (%d) given for sequence %s\n", pos + 1,
id);
numgaps -= ofs[pos];
ofs[pos] = gaplen;
numgaps += gaplen;
}
//add to the existing gap length in this position
void GASeq::addGap(int pos, short gapadd) {
if (pos < 0 || pos >= seqlen)
GError("Error: invalid gap position (%d) given for sequence %s\n", pos + 1,
id);
numgaps += gapadd;
ofs[pos] += gapadd;
}
void GASeq::removeBase(int pos) {
if (pos < 0 || pos >= seqlen)
GError("Error: invalid gap position (%d) given for sequence %s\n", pos + 1,
id);
//if there is a gap at that position, remove the gap
//otherwise, remove the actual nucleotide!
//if (ofs[pos]>0) {
ofs[pos]--;
numgaps--;
// return;
// }
/* if it's end base or within clipping --
// don't remove, just adjust clipping
if (revcompl!=0) { //reversed in this alignment
if (pos<=clp3) {
if (pos==clp3) clp3++;
offset--;
return;
}
if (pos>=seqlen-clp5-1) {
if (pos==seqlen-clp5-1) clp5++;
return;
}
}
else {//forward
if (pos<=clp5) {
if (pos==clp5) clp5++;
offset--;
return;
}
if (pos>=seqlen-clp3-1) {
if (pos==seqlen-clp3-1) clp3++;
return;
}
}
*/
//-- couldn't just this do it ?
/*
ofs[pos]--;
numgaps--;
return;
*/
/*
//===========================================
// worst case: modify/rebuild the whole sequence..
//short* newofs;
seqlen--;
memmove(ofs+pos, ofs+pos+1, seqlen-pos);
//do the same for the actual sequence, if loaded
if (len>0) {//sequence loaded
len--;
memmove(seq+pos, seq+pos+1, len-pos);
endSeq();
}
else { //push this sequence editing information on a stack for later processing
delops->Add(new SeqDelOp(pos, revcompl!=0));
}
*/
}
void GASeq::refineClipping(GDynArray<char>& cons, int cpos, bool skipDels) {
//check if endings match consensus..
//adjust clipping as appropriate
//int clipL, clipR;
if (clp3 == 0 && clp5 == 0)
return;
int& clipL = (revcompl != 0) ? clp3 : clp5;
int& clipR = (revcompl != 0) ? clp5 : clp3;
//build the gapped sequence string in memory
char* gseq;
int glen = seqlen + numgaps;
int allocsize = glen;
int gclipR = clipR;
int gclipL = clipL;
if (skipDels) {
for (int i = 1; i <= clipR; i++) {
if (ofs[seqlen - i] < 0)
allocsize++;
else
gclipR += ofs[seqlen - i];
}
for (int i = 0; i < clipL; i++) {
if (ofs[i] < 0)
allocsize++;
else
gclipL += ofs[i];
}
} else {
for (int i = 1; i <= clipR; i++)
gclipR += ofs[seqlen - i];
for (int i = 0; i < clipL; i++)
gclipL += ofs[i];
}
int* gxpos; //mapping of positions from gseq to seq
GMALLOC(gxpos, allocsize * sizeof(int));
GMALLOC(gseq, allocsize + 1);
gseq[allocsize] = 0;
int gseqpos = 0;
for (int i = 0; i < seqlen; i++) {
//bool notClip=(i>=clipL && i<seqlen-clipR);
if (ofs[i] < 0) {
if (!skipDels)
continue; //always skip gaps
if (i >= clipL && i < seqlen - clipR) //in non-clipped region
continue; //skip gaps
else
glen++;
}
for (int j = 0; j < ofs[i]; j++) {
gseq[gseqpos] = '*';
gseqpos++;
}
gseq[gseqpos] = seq[i];
gxpos[gseqpos] = i;
gseqpos++;
}
gseq[allocsize] = 0;
if (glen != allocsize)
GError(
"Length mismatch (allocsize %d vs. glen %d) while refineClipping for seq %s !\n",
allocsize, glen, id);
//adjust end clipping, using a simple X-drop algorithm
// match_reward=1, mismatch_penalty=-3
#define XDROP -16
#define MATCH_SC 1
#define MISMATCH_SC -3
if (clipR > 0) {
//-------------- clipR -------------------------
// actual span of clipped regions in the alignment
// could be larger then clipR/clipL due to gaps propagated
// WITHIN the clipped regions
//******** right end adjust
// cp = last "matching" position on consensus
int cp = cpos + glen - gclipR - 1;
// sp = corresponding last match position on read
int sp = glen - gclipR - 1;
//we could be on a mismatch or a gap
// so, first go backward to find the first match
while (gseq[sp] != cons[cp] || gseq[sp] == '*') {
if (gseq[sp] != '*')
clipR++;
sp--;
cp--;
if (sp < gclipL) {
GMessage(
"Warning: reached clipL trying to find an initial match on %s!\n",
id);
GFREE(gseq);
return; //break
}
}
//now go forward for as much as we can, using the dropoff test
int score = MATCH_SC;
int maxscore = MATCH_SC;
int startpos = sp;
int bestpos = sp; //new Right clipping position for maxscore
while (score > XDROP && ++cp < (int)cons.Count() && ++sp < glen) {
if (gseq[sp] == cons[cp]) {
if (gseq[sp] != '*') { //real match
score += MATCH_SC;
//clpinc++;
if (score > maxscore) {
bestpos = sp;
maxscore = score;
} //better score than before
} //real match
} //match
else { //mismatch
if (gseq[sp] != '*') {
score += MISMATCH_SC; //clipinc++;
}
}
} //while XDROP
if (bestpos > startpos)
clipR = seqlen - gxpos[bestpos] - 1;
} //<------- was clipR
// ******** left end adjust
if (clipL > 0) {
// cp = last "matching" position on consensus
int cp = cpos + gclipL;
// sp = corresponding last match position on read
int sp = gclipL;
// we could be on a mismatch or a gap
// so, first go backward to find the first match
while (gseq[sp] != cons[cp] || gseq[sp] == '*') {
if (gseq[sp] != '*')
clipL++;
sp++;
cp++;
if (sp >= glen - gclipR) {
GMessage(
"Warning: reached clipR trying to find an initial match on %s!\n",
id);
GFREE(gseq);
return; //break
}
}
//-- now go backward for as much as we can, using the dropoff test
int score = MATCH_SC;
int maxscore = MATCH_SC;
int startpos = sp;
int bestpos = sp;
while (score > XDROP && --cp >= 0 && --sp >= 0) {
if (gseq[sp] == cons[cp]) {
if (gseq[sp] != '*') { //real match
score += MATCH_SC;
if (score > maxscore) {
bestpos = sp;
maxscore = score;
} //better score than before
} //real match
} //match
else { //mismatch
if (gseq[sp] != '*') {
score += MISMATCH_SC;
}
}
} //while XDROP
if (bestpos < startpos)
clipL = gxpos[bestpos];
} //is clipL
GFREE(gseq);
GFREE(gxpos);
}
void GASeq::reverseGaps() {
//--when reading mgblast alignments and gap info
//the gap positions are reversed starting and shifted by 1
//because the first ofs is always 0
int l = 1;
int r = seqlen - 1;
while (l < r) {
short c = ofs[l];
ofs[l] = ofs[r];
ofs[r] = c;
l++;
r--;
}
}
void GASeq::revComplement(int alignlen) {
if (alignlen > 0) { //sequence is in an alignment
offset = alignlen - endOffset();
if (msa != NULL) {
ng_ofs = msa->ng_len - endNgOffset();
if (msa->minoffset > offset)
msa->minoffset = offset;
if (msa->ng_minofs > ng_ofs)
msa->ng_minofs = ng_ofs;
}
}
revcompl = !revcompl;
if (len == seqlen) //sequence is loaded, complement it
reverseComplement();
reverseGaps();
//-- also reverse the coverage array:
#ifdef ALIGN_COVERAGE_DATA
int l=0;int r=seqlen-1;
while (l<r) {
int c=cov[l];
cov[l]=cov[r];
cov[r]=c;
l++;r--;
}
#endif
}
#ifdef ALIGN_COVERAGE_DATA
void GASeq::addCoverage(GASeq* s) {
// add coverage information from the same sequence involved
// in another pairwise alignment
if (seqlen!=s->seqlen)
GError("GSeqAlign Error: invalid addCoverage %s(len %d) vs %s(len %d)\n",
name(),seqlen, s->name(), s->seqlen);
if (s->revcompl!=revcompl) {
for (int i=0;i<seqlen;i++)
cov[i]+=s->cov[seqlen-i-1];
}
else
for (int i=0;i<seqlen;i++)
cov[i]+=s->cov[i];
}
#endif
void GASeq::printGappedSeq(FILE* f, int baseoffs) {
// for now, simple console printing of short sequences -- for testing
if (len == 0 || len != seqlen)
GError(
"GASeq print Error: invalid sequence data '%s' (len=%d, seqlen=%d)\n",
id, len, seqlen);
int i;
int clipL, clipR;
if (revcompl != 0) {
clipL = clp3;
clipR = clp5;
} else {
clipL = clp5;
clipR = clp3;
}
for (i = 0; i < (offset - baseoffs); i++)
fprintf(f, " ");
for (i = 0; i < seqlen; i++) {
if (ofs[i] < 0)
continue; //deleted base
for (int j = 0; j < ofs[i]; j++)
fprintf(f, "-");
char c = seq[i];
if (i < clipL || i >= seqlen - clipR)
c = (char) tolower(c);
fprintf(f, "%c", c);
} //for each base
fprintf(f, "\n");
}
void GASeq::printGappedFasta(FILE* f) {
if (len == 0 || len != seqlen)
GError(
"GASeq print Error: invalid sequence data '%s' (len=%d, seqlen=%d)\n",
id, len, seqlen);
int i;
/* //FIXME TESTME - original mblaor had this uncommented!
int clipL, clipR;
if (revcompl != 0) {
clipL = clp3;
clipR = clp5;
} else {
clipL = clp5;
clipR = clp3;
}
*/
int printed = 0;
for (i = 0; i < seqlen; i++) {
if (ofs[i] < 0)
continue; //deleted base
for (int j = 0; j < ofs[i]; j++) {
fprintf(f, "*");
printed++;
if (printed == 60) {
fprintf(f, "\n");
printed = 0;
}
}
char c = seq[i];
printed++;
if (printed == 60) {
fprintf(f, "%c\n", c);
printed = 0;
} else
fprintf(f, "%c", c);
} //for each base
if (printed < 60)
fprintf(f, "\n");
}
void GASeq::printMFasta(FILE* f, int llen) {
if (len == 0 || len != seqlen)
GError("GASeq print Error: invalid sequence data '%s' (len=%d, seqlen=%d)\n",
id, len, seqlen);
if (this->descrlen>0) fprintf(f, ">%s %s\n", id, descr);
else
fprintf(f, ">%s\n", id);
int i;
int printed = 0;
for (i=0;i<offset;i++) {
fprintf(f, "-");
printed++;
if (printed == llen) {
fprintf(f, "\n");
printed = 0;
}
}
for (i = 0; i < seqlen; i++) {
if (ofs[i] < 0)
continue; //deleted base
for (int j = 0; j < ofs[i]; j++) {
fprintf(f, "-");
printed++;
if (printed == llen) {
fprintf(f, "\n");
printed = 0;
}
}
char c = seq[i];
printed++;
if (printed == llen) {
fprintf(f, "%c\n", c);
printed = 0;
} else
fprintf(f, "%c", c);
} //for each base
if (printed < llen)
fprintf(f, "\n");
}
int GASeq::removeClipGaps() { //remove gaps within clipped regions
//offset is also corrected appropriately!
int clipL;
int clipR;
if (revcompl != 0) {
clipL = clp3;
clipR = clp5;
} else {
clipL = clp5;
clipR = clp3;
}
int delgapsL = 0;
int delgapsR = 0;
for (int i = 0; i < seqlen; i++) {
if (i <= clipL) { // within left clipping
delgapsL += ofs[i];
ofs[i] = 0;
continue;
} //within left clipping
if (i >= seqlen - clipR) { //with right clipping
delgapsR += ofs[i];
ofs[i] = 0;
}
} //for
offset += delgapsL;
numgaps -= (delgapsL + delgapsR);
return delgapsL + delgapsR;
}
void GASeq::toMSA(MSAColumns& msacols, int nucValue) {
if (len == 0 || len != seqlen)
GError(
"GASeq::toMSA Error: invalid sequence data '%s' (len=%d, seqlen=%d)\n",
id, len, seqlen);
int i;
int clipL, clipR;
if (revcompl != 0) {
clipL = clp3;
clipR = clp5;
} else {
clipL = clp5;
clipR = clp3;
}
int mincol = INT_MAX;
int maxcol = 0;
//for (i=0;i<(offset-msa.baseoffset);i++) col++;
int col = offset - msa->minoffset;
for (i = 0; i < seqlen; i++) {
bool clipped;
if (i < clipL || i >= seqlen - clipR)
clipped = true;
else {
clipped = false;
if (mincol == INT_MAX)
mincol = col;
}
for (int j = 0; j < ofs[i]; j++) {
//storing gap
if (!clipped)
msacols[col].addGap(nucValue);
col++;
}
msacols[col].addNuc(this, i, clipped, nucValue);
if (!clipped)
maxcol = col;
col++;
} //for each base
//update msa's min-max
msacols.updateMinMax(mincol, maxcol);
}
//=================================== GSeqAlign ===============================
// -- creation from a pairwise alignment
// l1-r1 = coordinates of alignment region on s1
// l2-r2 = coordinates of alignment region on s2
// coordinates MUST be 0-based
#ifdef ALIGN_COVERAGE_DATA
GSeqAlign::GSeqAlign(GASeq* s1, int l1, int r1,
GASeq* s2, int l2, int r2)
//:GList<GASeq>(true,true,false) {
:GList<GASeq>(false,true,false) {
#else
GSeqAlign::GSeqAlign(GASeq* s1, GASeq* s2) :
GList<GASeq>(false, true, false), length(0), minoffset(0),
refinedMSA(false), msacolumns(NULL), ordnum(0),
ng_len(0),ng_minofs(0), badseqs(0), consensus(512), consensus_bq(512) {
#endif
s1->msa = this;
s2->msa = this;
//the offset for at least one sequence is 0
this->Add(s1);
this->Add(s2);
minoffset = GMIN(s1->offset, s2->offset);
ng_minofs = minoffset; //no gaps in the clipped regions for now
length = GMAX(s1->endOffset(), s2->endOffset());
length -= minoffset;
ng_len = GMAX(s1->endNgOffset(), s2->endNgOffset());
ng_len -= ng_minofs;
//-- according to the alignment, update the coverage for each sequence
//-- overlaps are granted +1 bonus
#ifdef ALIGN_COVERAGE_DATA
for (int i=l1;i<r1;i++) s1->cov[i]++;
for (int i=l2;i<r2;i++) s2->cov[i]++;
//-- mismatch regions at the left end
int msml=(l2>l1) ? l1:l2;
for (int i=1;i<=msml;i++) {
s1->cov[l1-msml]--;
s2->cov[l2-msml]--;
}
//-- mismatch regions at the right end
int cr1=s1->seqlen-r1-1;
int cr2=s2->seqlen-r2-1;
int msmr=(cr2>cr1) ? cr1:cr2;
for (int i=1;i<=msmr;i++) {
s1->cov[r1+msmr]--;
s2->cov[r2+msmr]--;
}
#endif
}
//merge other alignment omsa into this msa
//seq->id MUST be the same with oseq->id
bool GSeqAlign::addAlign(GASeq* seq, GSeqAlign* omsa, GASeq* oseq) {
//error checking -- could be disabled to speed it up a bit
if (seq->seqlen != oseq->seqlen)
GError("GSeqAlign Error: invalid merge %s(len %d) vs %s(len %d)\n",
seq->getName(), seq->seqlen, oseq->getName(), oseq->seqlen);
// for this merge to work, the shared sequence MUST have
// the same orientation in both MSAs
if (seq->revcompl != oseq->revcompl)
omsa->revComplement(); //reverse-complement all sequences in omsa
#ifdef ALIGN_COVERAGE_DATA
//add coverage values:
seq->addCoverage(oseq);
#endif
//--- now propagate gaps as appropriate
for (int i = 0; i < seq->seqlen; i++) {
int d = seq->gap(i) - oseq->gap(i);
if (d > 0) { //extra gap in seq
//propagate into all omsa
omsa->injectGap(oseq, i, d);
continue;
} //extra gap in seq
if (d < 0) { //extra gap in oseq
//propagate into all msa
injectGap(seq, i, -d);
continue;
} //extra gap in oseq
} //--for each base position
//--now add the sequences from omsa to this MSA
for (int i = 0; i < omsa->Count(); i++) {
GASeq* s = omsa->Get(i);
if (s == oseq)
continue;
//adjust offset -- which can be extended by gaps in seq BEFORE s->offset
//the offsets had been adjusted already (by injectGap() method)
// to account for propagated gaps in both MSAs!
//--add this sequence
//cluster minoffset and length will be updated too!
addSeq(s, seq->offset + s->offset - oseq->offset,
seq->ng_ofs + s->ng_ofs - oseq->ng_ofs);
}
omsa->setFreeItem(false);
//delete omsa; //we no longer need this alignment
delete oseq; //also deletes oseq
return true;
}
//just to automatically set the offset, msa,
//and to update the MSA length if needed
void GSeqAlign::addSeq(GASeq* s, int soffs, int ngofs) {
s->offset = soffs;
s->ng_ofs = ngofs;
s->msa = this;
this->Add(s);
//keep track of minimum offset
//this also adjusts length!
if (soffs < minoffset) {
length += minoffset - soffs;
minoffset = soffs;
}
if (ngofs < ng_minofs) {
ng_len += ng_minofs - ngofs;
ng_minofs = ngofs;
}
//adjust length of alignment if very long sequences were added
if (s->endOffset() - minoffset > length)
length = s->endOffset() - minoffset;
if (s->endNgOffset() - ng_minofs > ng_len)
ng_len = s->endNgOffset() - ng_minofs;
}
//propagate a gap in a sequence into the whole alignment containing it
// offsets of all seqs after the gap MUST be adjusted too!
void GSeqAlign::injectGap(GASeq* seq, int pos, int xgap) {
//find the actual alignment position of this pos in the layout
int alpos = seq->offset + pos;
for (int i = 0; i <= pos; i++)
alpos += seq->gap(i);
//now alpos = the exact offset of seq[pos] in this MSA
for (int i = 0; i < Count(); i++) {
GASeq* s = Get(i);
int spos = 0; // finding out position of gap in seq s
if (s == seq)
spos = pos;
else {
//walk to lpos on sequence s
int salpos = s->offset;
if (salpos >= alpos) {
//s->offset is AFTER this gap, so only the offset is affected
s->offset += xgap;
continue;
}
while (spos < s->seqlen) {
salpos += 1 + s->gap(spos);
if (salpos > alpos)
break;
spos++;
}
if (spos >= s->seqlen) //spos is AFTER the end of sequence s
continue; // s not affected
//--it is a valid position for this sequence
//--TO DO: clipping? first/last positions?
}
s->addGap(spos, xgap);
} //for each sequense in MSA
length += xgap;
}
void GSeqAlign::removeColumn(int column) {
int alpos = column + minoffset;
for (int i = 0; i < Count(); i++) {
GASeq* s = Get(i);
int spos = 0; // finding out position of this base in seq s
int salpos = s->offset;
if (salpos >= alpos) {
//s->offset is AFTER this gap, so only the offset is affected
s->offset--; //deletion of 1
continue;
}
while (spos < s->seqlen) {
salpos += 1 + s->gap(spos);
if (salpos > alpos)
break;
spos++;
}
if (spos >= s->seqlen) //spos is AFTER the end of sequence s
continue; // s not affected
//--now spos is a valid position for this sequence
// }
s->removeBase(spos);
} //for each sequence in MSA
length--;
}
void GSeqAlign::removeBase(GASeq* seq, int pos) {
//find the actual alignment position of this pos in the layout
int alpos = seq->offset + pos;
for (int i = 0; i <= pos; i++)
alpos += seq->gap(i);
//now alpos = the exact offset of seq[pos] in this MSA
for (int i = 0; i < Count(); i++) {
GASeq* s = Get(i);
int spos = 0; // finding out position of this base in seq s
if (s == seq)
spos = pos;
else { //walk to lpos on sequence s
int salpos = s->offset;
if (salpos >= alpos) {
//s->offset is AFTER this gap, so only the offset is affected
s->offset--; //deletion of 1
continue;
}
while (spos < s->seqlen) {
salpos += 1 + s->gap(spos);
if (salpos > alpos)
break;
spos++;
}
if (spos >= s->seqlen) //spos is AFTER the end of sequence s
continue; // s not affected
//--now spos is a valid position for this sequence
}
s->removeBase(spos);
} //for each sequence in MSA
length--;
}
void GSeqAlign::applyClipping(AlnClipOps& clipops) {
for (int i = 0; i < clipops.Count(); i++) {
SeqClipOp& cop = *clipops.Get(i);
if (cop.clp[0] >= 0)
cop.seq->clp5 = cop.clp[0];
if (cop.clp[1] >= 0)
cop.seq->clp3 = cop.clp[1];
}
}
bool GSeqAlign::evalClipping(GASeq* seq, int c5, int c3, float clipmax,
AlnClipOps& clipops) {
//propagate trimming of a read to the rest of this container MSA
//-- returns false if any of the reads in this MSA are clipped too much!
//GList<SeqClipOp> clipops(false,true,false);
if (c5 >= 0) {
//the position of the first/last non-clipped letter
int pos = (seq->revcompl != 0) ? seq->seqlen - c5 - 1 : c5;
//find the actual alignment position of this pos in the layout
int alpos = seq->offset + pos;
for (int i = 0; i <= pos; i++)
alpos += seq->gap(i);
//alpos = the position of seq[pos] in this MSA
for (int i = 0; i < Count(); i++) {
GASeq* s = Get(i);
if (s == seq) {
if (!clipops.add5(s, c5, clipmax))
return false;
continue;
}
int spos = 0; // finding out position in seq s
//walk to lpos on sequence s
//salpos is going to be the position of seq[pos] in s
int salpos = s->offset;
if (salpos >= alpos) {
//-- s starts AFTER this alpos position
if (seq->revcompl != 0) { //clipping right side
// which means ALL of s is to the right => clipped entirely!
// !!! TODO:
return false;
//clipops.Add(new SeqClipOp(s, s->seqlen));
}
continue;
}
while (spos < s->seqlen) {
salpos += 1 + s->gap(spos);
if (salpos > alpos)
break;
spos++;
}
if (spos >= s->seqlen) {
//s ends BEFORE this alpos position
if (seq->revcompl == 0) { //clipping left side
// which means ALL of s is to the left => clipped entirely!
return false;
}
continue; // s not affected
}
//--it is a valid position for this sequence
//now spos is in the corresponding position of pos
//trim s here
if (seq->revcompl != 0) { //trim right side in this msa
if (s->revcompl != 0) {
if (!clipops.add5(s, s->seqlen - spos - 1, clipmax))
return false;
/*if (s->clp5<newclp) {
if (badClipping(s,newclp,s->clp3,clipmax)) return false;
clipops.Add(new SeqClipOp(s,newclp));
}*/
} else {
if (!clipops.add3(s, s->seqlen - spos - 1, clipmax))
return false;
/*if (s->clp3<newclp) {
if (badClipping(s,s->clp5,newclp,clipmax)) return false;
clipops.Add(new SeqClipOp(s,-1,newclp));
}*/
}
} else { //trim left side in this msa
if (s->revcompl != 0) {
if (!clipops.add3(s, spos, clipmax))
return false;
/*if (s->clp3<spos) {
if (badClipping(s,s->clp5,spos,clipmax)) return false;
clipops.Add(new SeqClipOp(s,-1,spos));
}*/
} else {
if (!clipops.add5(s, spos, clipmax))
return false;
/*if (s->clp5<spos) {
if (badClipping(s,spos,s->clp3,clipmax)) return false;
clipops.Add(new SeqClipOp(s,spos));
}*/
}
}
} //for each sequense in MSA
} // 5' clipping case
//---------------
if (c3 >= 0) {
//the position of the first/last non-clipped letter
int pos = (seq->revcompl != 0) ? c3 : seq->seqlen - c3 - 1;
//find the actual alignment position of this pos in the layout
int alpos = seq->offset + pos;
for (int i = 0; i <= pos; i++)
alpos += seq->gap(i);
//now alpos = the exact offset of seq[pos] in this MSA
for (int i = 0; i < Count(); i++) {
GASeq* s = Get(i);
if (s == seq) {
if (!clipops.add3(s, c3, clipmax))
return false;
/*if (s->clp3<c3) {
if (badClipping(s,s->clp5,c3,clipmax)) return false;
clipops.Add(new SeqClipOp(s,-1,c3));
}*/
continue;
}
int spos = 0; // finding out position in seq s
//walk to lpos on sequence s
int salpos = s->offset;
if (salpos >= alpos) {
//-- s starts AFTER this alpos position
if (seq->revcompl == 0) { //clipping right side
// which means ALL of s is to the right => clipped entirely!
return false;
//clipops.Add(new SeqClipOp(s, s->seqlen));
}
continue;
}
while (spos < s->seqlen) {
salpos += 1 + s->gap(spos);
if (salpos > alpos)
break;
spos++;
}
if (spos >= s->seqlen) {
//s ends BEFORE this alpos position
if (seq->revcompl != 0) { //clipping left side
// which means ALL of s is to the left => clipped entirely!
return false;
//clipops.Add(new SeqClipOp(s, s->seqlen));
}
continue; // s not affected
}
//--it is a valid position for this sequence
//now spos is in the corresponding position of pos
//trim s here
if (seq->revcompl != 0) { //trim left side in this msa
if (s->revcompl != 0) {
if (!clipops.add3(s, spos, clipmax))
return false;
/*if (s->clp3<spos) {
if (badClipping(s,s->clp5,spos,clipmax)) return false;
clipops.Add(new SeqClipOp(s,-1, spos));
}*/
} else {
if (!clipops.add5(s, spos, clipmax))
return false;
/*if (s->clp5<spos) {
if (badClipping(s,spos,s->clp3,clipmax)) return false;
clipops.Add(new SeqClipOp(s,spos));
}*/
}
} else { //trim right side in this msa
//int newclp=s->seqlen-spos-1;
if (s->revcompl != 0) {
if (!clipops.add5(s, s->seqlen - spos - 1, clipmax))
return false;