-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.c
1751 lines (1517 loc) · 49.2 KB
/
map.c
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 "map.h"
#define max(x,y) ((x) > (y) ? (x) : (y))
#define min(x,y) ((x) < (y) ? (x) : (y))
typedef struct seedchain_t seedchain_t;
typedef struct aligncd_t aligncd_t;
#define MAX_MEM_SEED_LOCI 300
#define MAX_SKIP_CHAIN_SEEDS 10000
#define MAX_MINSCORE_REPEATS 2
#define MAX_ALIGN_MISMATCHES 30
#define MAX_CHAIN_INDEL_RATE 0.1
#ifdef DEBUG
int DEBUG_VERBOSE = 1;
#else
int DEBUG_VERBOSE = 0;
#endif
#define min(x,y) ((x) < (y) ? (x) : (y))
#define min3(x,y,z) (min(min(x,y),z))
struct seedchain_t {
size_t pos;
size_t max;
size_t loci;
int minscore;
int span;
seed_t * seed[];
};
struct aligncd_t {
size_t cnt;
align_t * align;
};
seedchain_t * seedchain_new (size_t max);
void seed_push (seed_t * mem, seedchain_t ** stackp);
void aln_push (aln_t aln, alnstack_t ** stackp);
int seed_by_refpos (const void * a, const void * b) {
return ((align_t *)a)->refpos > ((align_t *)b)->refpos;
};
int seed_by_span (const void * a, const void * b) {
align_t * sa = (align_t *)a;
align_t * sb = (align_t *)b;
return sa->span < sb->span;
};
int seed_by_start (const void * a, const void * b) {
return (*(seed_t **)a)->beg > (*(seed_t **)b)->beg;
};
int mem_by_loci (const void * a, const void * b) {
seed_t * sa = *(seed_t **)a;
seed_t * sb = *(seed_t **)b;
return (sa->range.top - sa->range.bot) > (sb->range.top - sb->range.bot);
};
int mem_by_span (const void * a, const void * b) {
seed_t * sa = *(seed_t **)a;
seed_t * sb = *(seed_t **)b;
return (sa->end - sa->beg) < (sb->end - sb->beg);
};
int seed_by_first_locus (const void * a, const void * b) {
const seed_t A = **(seed_t **) a;
const seed_t B = **(seed_t **) b;
return (A.sa[0] > B.sa[0]) - (A.sa[0] < B.sa[0]);
}
int SA_by_locus (const void *a, const void *b) {
const size_t locus_A = *(size_t *)a;
const size_t locus_B = *(size_t *)b;
return (locus_A > locus_B) - (locus_A < locus_B);
}
int minscore_then_span (const void * a, const void * b) {
int sa = (*(seedchain_t **)a)->minscore;
int sb = (*(seedchain_t **)b)->minscore;
if (sa > sb)
return 1;
else if (sa < sb)
return -1;
else
return (*(seedchain_t **)a)->span < (*(seedchain_t **)b)->span;
};
int align_minscore_then_span (const void * a, const void * b) {
int sa = ((align_t *)a)->minscore;
int sb = ((align_t *)b)->minscore;
if (sa > sb)
return 1;
else if (sa < sb)
return -1;
else
return ((align_t *)a)->span < ((align_t *)b)->span;
};
// Error-handling macros.
#define exit_on_memory_error(x) \
do { if ((x) == NULL) { fprintf(stderr, "memory error %s:%d:%s()\n", \
__FILE__, __LINE__, __func__); exit(EXIT_FAILURE); }} while(0)
int
nw
(
const char * seq1,
const char * seq2,
const int len1,
const int len2,
const int cutoff
)
// Place reference in seq1, you can add extra length to len1 to allocate insertions in seq2.
{
// Penalties (don't set i_p or d_p smaller than m_p!).
const int m_p = 1, i_p = 1, d_p = 1;
int len = min(len1, len2);
int * rowp = calloc(len1+2, sizeof(int));
int * colp = calloc(len2+2, sizeof(int));
// Comment for SW.
for (int i = 1; i < len1+2; i++) rowp[i] = rowp[i-1] + d_p;
for (int j = 1; j < len2+2; j++) colp[j] = colp[j-1] + i_p;
// Align row and col.
int * row = rowp + 1;
int * col = colp + 1;
int score = cutoff;
for (int pos = 0; pos < len; pos++) {
score = cutoff;
// Update row.
int i, left, diag;
for (i = pos, left = col[pos], diag = row[i-1]; i < len1 && row[i-1] <= cutoff; i++) {
int next_diag = row[i];
left = row[i] = min3(diag + (CAPS[(int)seq1[i]] != CAPS[(int)seq2[pos]])*m_p,
row[i] + i_p,
left + d_p);
diag = next_diag;
if (row[i] < score) score = row[i];
}
// Uncomment for SW.
// row[i] = row[i-1];
// Update column.
int j, up;
for (j = pos+1, up = row[pos], diag = col[pos]; j < len2 && col[j-1] <= cutoff; j++) {
int next_diag = col[j];
up = col[j] = min3(diag + (CAPS[(int)seq1[pos]] != CAPS[(int)seq2[j]])*m_p,
up + i_p,
col[j] + d_p);
diag = next_diag;
if (col[j] < score) score = col[j];
}
// Uncomment for SW.
// col[j] = col[j-1];
}
free(rowp);
free(colp);
return score;
}
void
recursive_mem_chain
(
wstack_t * mems,
size_t mem_pos,
size_t chain_pos,
seed_t ** chain,
wstack_t ** chain_stack
)
{
size_t mem_end = ((seed_t *) mems->ptr[mem_pos])->end;
// For MEMs overlapping MEM[pos].
for (size_t i = mem_pos; i < mems->pos && ((seed_t *)mems->ptr[i])->beg <= mem_end; i++) {
// Extend chain.
chain[chain_pos] = (seed_t *) mems->ptr[i];
// Get next nonoverlapping MEM.
size_t j;
for (j = i+1; j < mems->pos; j++) {
if (((seed_t *)mems->ptr[j])->beg > ((seed_t *) mems->ptr[i])->end)
break;
}
// We reached the chain end, store chain.
if (j >= mems->pos) {
seedchain_t * seedchain = seedchain_new(chain_pos+1);
// Push mems to chain and compute span.
int span = 0;
size_t loci = 0;
for (size_t k = 0; k <= chain_pos; k++) {
span += chain[k]->end - chain[k]->beg + 1;
loci += chain[k]->range.top - chain[k]->range.bot + 1;
seed_push(chain[k], &seedchain);
}
// Push mem chain to chain stack.
seedchain->span = span;
seedchain->loci = loci;
push(seedchain, chain_stack);
} else {
recursive_mem_chain(mems, j, chain_pos+1, chain, chain_stack);
}
}
}
seed_t *
new_one_locus_seed
(
seed_t * old,
size_t idx
)
{
seed_t * new = calloc(1, sizeof(seed_t));
exit_on_memory_error(new);
range_t range = (range_t) {
// One locus.
.bot = old->range.bot + idx,
.top = old->range.bot + idx,
};
new->beg = old->beg;
new->end = old->end;
new->range = range;
new->sa = malloc(sizeof(size_t));
exit_on_memory_error(new->sa);
new->sa[0] = old->sa[idx];
return new;
}
wstack_t *
merge_overlapping_seeds
(
wstack_t * skip_seeds,
const index_t idx
)
{
// Initialize stack of merged seeds.
wstack_t * merged = stack_new(64);
exit_on_memory_error(merged);
// Get SA values and sort by locus.
for (int i = 0 ; i < skip_seeds->pos ; i++) {
seed_t * s = (seed_t *) skip_seeds->ptr[i];
size_t nloci = s->range.top - s->range.bot + 1;
if (nloci > 32) {
// Keep at most 16 loci per seed.
s->range.top = s->range.bot + 31;
nloci = 32;
}
s->sa = query_csa_range(idx.csa, idx.bwt, idx.occ, s->range);
// Sort SA values by locus order.
qsort(s->sa, nloci, sizeof(size_t), SA_by_locus);
}
// Temporary arrays for the merge.
seed_t * databuffer_1[128] = {0};
seed_t * databuffer_2[128] = {0};
seed_t ** prev = databuffer_1;
seed_t ** next = databuffer_2;
// Assume seeds are sorted by start/end position.
// Start with the leftmost seed and create new one-locus seeds.
seed_t * leftmost_seed = (seed_t *) skip_seeds->ptr[skip_seeds->pos-1];
int n_prev_loci = leftmost_seed->range.top - leftmost_seed->range.bot + 1;
if (n_prev_loci > 128) n_prev_loci = 128;
for (int n = 0 ; n < n_prev_loci ; n++) {
// Calls 'malloc' and 'exit_on_memory_error'.
seed_t * seed = new_one_locus_seed(leftmost_seed, n);
push(seed, &merged);
prev[n] = seed;
}
for (int i = skip_seeds->pos-2 ; i >= 0 ; i--) {
seed_t * next_seed = (seed_t *) skip_seeds->ptr[i];
int n_next_loci = next_seed->range.top - next_seed->range.bot + 1;
// Left and right pointers.
int lidx = 0;
int ridx = 0;
int inc = 0;
while (inc < 128 && lidx < n_prev_loci && ridx < n_next_loci) {
seed_t * prev_seed = prev[lidx];
ssize_t next_locus = next_seed->sa[ridx];
const ssize_t dhit = next_seed->beg - prev_seed->beg;
if (next_locus > prev_seed->sa[0] + dhit) {
// The one-locus seed already exists.
if (prev_seed->end >= next_seed->beg) {
next[inc++] = prev_seed;
}
lidx++;
}
else if (next_locus < prev_seed->sa[0] + dhit) {
// Create new one-locus seed.
seed_t * new = new_one_locus_seed(next_seed, ridx);
push(new, &merged);
next[inc++] = new;
ridx++;
}
else {
// Found a match: extend the one-locus seed.
prev_seed->end = next_seed->end;
next[inc++] = prev_seed;
lidx++;
ridx++;
}
}
// At most one of the loop I or II is executed.
// Add remaining loci or seed / loci.
for ( ; inc < 128 && lidx < n_prev_loci ; lidx++) {
// Loop I.
seed_t * seed = prev[lidx];
if (seed->end >= next_seed->beg) {
next[inc++] = seed;
}
}
for ( ; inc < 128 && ridx < n_next_loci ; ridx++) {
// Loop II.
seed_t * new = new_one_locus_seed(next_seed, ridx);
exit_on_memory_error(new);
push(new, &merged);
next[inc++] = new;
}
// Update prev / next.
seed_t ** tmp = prev;
prev = next;
next = tmp;
n_prev_loci = inc;
}
// The 'merged' stack contains the seed / loci.
return merged;
}
wstack_t *
nonoverlapping_mems
(
wstack_t * mems
)
{
// Alloc.
wstack_t * chain_stack = stack_new(8);
if (mems->pos > 0) {
seed_t ** chain = malloc(mems->pos * sizeof(seed_t *));
exit_on_memory_error(chain);
// 1. Sort MEMs by start position.
qsort(mems->ptr, mems->pos, sizeof(seed_t *), seed_by_start);
// 2. Recursive call to mem group.
recursive_mem_chain(mems, 0, 0, chain, &chain_stack);
free(chain);
}
// 3. Return stack of non-overlapping MEM combinations.
return chain_stack;
}
int
mem_chain_min_score
(
seedchain_t * chain,
const int seqlen
)
{
// Commented lines remove the MEM masking bug.
// The code was not removed because the idea can
// be reused with fixed-length seeds.
int minscore = 0;
// Add mismatches at chain ends.
if (chain->seed[0]->beg > 0)
//minscore += max(0,chain->mem[0]->beg / gamma - 1) + 1;
minscore += 1;
if (chain->seed[chain->pos-1]->end < seqlen-1)
//minscore += max(0,(seqlen - 2 - chain->mem[chain->pos-1]->end)/gamma - 1) + 1;
minscore += 1;
// Add gap mismatches.
for (int i = 1; i < chain->pos; i++) {
// A gap implies one mismatch, even if it's a gap of length 0.
//int gap_size = chain->mem[i]->beg - chain->mem[i-1]->end - 1;
//minscore += max(0,gap_size / gamma - 1) + 1;
minscore += 1;
}
return minscore;
}
wstack_t *
chain_mems
(
int slen,
wstack_t * mems
)
{
// Find all non-overlapping MEM combinations.
wstack_t * chain_stack = nonoverlapping_mems(mems);
// Compute minimum alignment score given seed distribution.
for (int i = 0; i < chain_stack->pos; i++) {
seedchain_t * chain = (seedchain_t *)chain_stack->ptr[i];
chain->minscore = mem_chain_min_score(chain, slen);
}
// Sort mem chains by minscore(inc) then span(dec).
qsort(chain_stack->ptr, chain_stack->pos, sizeof(seedchain_t *), minscore_then_span);
// DEBUG VERBOSE
if (DEBUG_VERBOSE) {
fprintf(stdout,"MEM chains (%ld):\n", chain_stack->pos);
for(int i = 0; i < chain_stack->pos; i++) {
seedchain_t * c = (seedchain_t *) chain_stack->ptr[i];
fprintf(stdout, "Chain [%d] (mems: %ld, span: %d, minscore: %d):\n", i, c->pos, c->span, c->minscore);
for (int j = 0; j < 0; j++) {
seed_t * m = c->seed[j];
fprintf(stdout, "[%d] (%ld, %ld) range: (%ld, %ld)\n", i, m->beg, m->end, m->range.bot, m->range.top);
}
}
fprintf(stdout,"\n");
}
return chain_stack;
}
aligncd_t
chain_skip
(
size_t slen,
int gamma,
int skip,
wstack_t * seeds,
index_t idx
)
{
// Get all Suffix Arrays.
size_t nloc = 0;
for (size_t i = 0; i < seeds->pos; i++) {
seed_t * seed = (seed_t *)seeds->ptr[i];
size_t seed_loc = seed->range.top - seed->range.bot + 1;
if (seed_loc > MAX_SKIP_CHAIN_SEEDS)
return (aligncd_t){0, NULL};
nloc += seed_loc;
}
if (nloc == 0)
return (aligncd_t){0, NULL};
// Recompute seed positions
ssize_t b = slen - gamma;
size_t nbeg = b/skip + 1 + (b%skip > 0);
size_t * sbeg = malloc(nbeg * sizeof(size_t));
exit_on_memory_error(sbeg);
for (ssize_t i = nbeg-1; i >= 0; i--) {
sbeg[i] = b;
b = max(b - skip, 0);
}
// Allocate all suffix array positions
align_t * loc_list = malloc(nloc*sizeof(align_t));
exit_on_memory_error(loc_list);
// Make chained alignment candidates from seed genomic positions
size_t j = 0;
for (size_t i = 0; i < seeds->pos; i++) {
seed_t * seed = (seed_t *)seeds->ptr[i];
// Get genomic positions
seed->sa = query_csa_range(idx.csa, idx.bwt, idx.occ, seed->range);
for (int k = 0; k < seed->range.top - seed->range.bot + 1; k++) {
loc_list[j++] = (align_t){seed->sa[k], seed->beg, 1, seed};
}
}
// Sort loci
qsort(loc_list, j, sizeof(align_t), seed_by_refpos);
// Chain alignment positions
int max_indels = min(skip-1, slen*(MAX_CHAIN_INDEL_RATE));
size_t nchain = 0;
wstack_t * chain = stack_new(seeds->pos);
for (size_t n = 0; n < nloc; n++) {
// Skip consumed seeds
if (loc_list[n].minscore == -1)
continue;
// Append seed to chain
chain->pos = 0;
push(loc_list[n].seed, &chain);
// Find chain
int max_ref_dist = (slen - loc_list[n].span - gamma + 1 + max_indels);
ssize_t read_last = 0;
for (size_t j = n+1; j < nloc; j++) {
int gen_dist = ((ssize_t)loc_list[j].refpos - (ssize_t)loc_list[n].refpos);
// No more possible chaining
if (gen_dist > max_ref_dist)
break;
// Seed mislocation
if (loc_list[n].span >= loc_list[j].span || loc_list[j].span <= read_last)
continue;
// Compute distance between seeds
int read_dist = (ssize_t)loc_list[j].span - (ssize_t)loc_list[n].span;
// Chain gap too big
if (gen_dist > read_dist + max_indels || gen_dist < read_dist - max_indels)
continue;
// Append seed to chain
push(loc_list[j].seed, &chain);
read_last = loc_list[j].span;
// Mark seed as consumed
loc_list[j].minscore = -1;
}
// Chain min score
size_t gap = 0;
size_t pos = 0;
size_t span = 0;
int minscore = 0;
for (size_t i = 0; i < chain->pos; i++) {
seed_t * s = (seed_t *)chain->ptr[i];
// Gap found
if (s->beg > gap) {
// Set pos to first mismatched seed
while (sbeg[pos] + gamma <= gap)
pos++;
// Kill minimum number of seeds to produce this gap
while (sbeg[pos] < s->beg) {
minscore++;
// Skip overlapping seeds
size_t seedend = sbeg[pos] + gamma;
while (pos < nbeg && sbeg[pos] < s->beg && sbeg[pos] < seedend)
pos++;
}
// Span
span += gamma;
} else {
span += s->end - gap + 1;
}
gap = s->end+1;
}
// Trailing gap
if (gap < slen) {
while (sbeg[pos] + gamma <= gap)
pos++;
while (pos < nbeg) {
minscore++;
// Skip overlapping seeds
size_t seedend = sbeg[pos] + gamma;
while (pos < nbeg && sbeg[pos] < seedend)
pos++;
}
}
// Create alignment
loc_list[nchain++] = (align_t){loc_list[n].refpos, span, minscore, loc_list[n].seed};
}
free(chain);
free(sbeg);
loc_list = realloc(loc_list, nchain*sizeof(align_t));
exit_on_memory_error(loc_list);
// Sort by minscore then span.
qsort(loc_list, nchain, sizeof(align_t), align_minscore_then_span);
return (aligncd_t){nchain, loc_list};
}
void
extend_L1L2
(
const char * seq,
const int len,
const index_t idx,
seed_t * L1,
seed_t * L2
)
{
// Preallocate ranges
range_t newrange = {0};
int i;
size_t merid;
// L1.
L1->end = len-1;
// Look up the beginning (reverse) of the query in lookup table.
merid = 0;
for (int j = 0 ; j < LUTK ; j++) {
// Note: every "N" is considered "A".
uint8_t c = ENCODE[(uint8_t) seq[len-j-1]];
merid = c + (merid << 2);
}
range_t range = idx.lut->kmer[merid];
if (range.top < range.bot) {
range = (range_t) {.bot = 1, .top = idx.occ->txtlen-1};
i = len-1;
}
else {
i = len-1 - LUTK;
}
for ( ; i >= 0 ; i--) {
if (NONALPHABET[(uint8_t)seq[i]]) break;
int c = ENCODE[(uint8_t) seq[i]];
newrange.bot = get_rank(idx.occ, c, range.bot - 1);
newrange.top = get_rank(idx.occ, c, range.top) - 1;
// Stop if fewer than 2 hits.
if (newrange.top < newrange.bot + 1)
break;
range = newrange;
}
L1->beg = i+1;
L1->range = range;
// Check L1 result
if (L1->end - L1->beg + 1 == len) {
L2->beg = L1->beg;
L2->end = L1->end;
L2->range = L1->range;
return;
}
// L2.
L2->beg = 0;
// Look up the beginning (forward) of the query in lookup table.
merid = 0;
for (int j = 0 ; j < LUTK ; j++) {
// Note: every "N" is considered "A".
uint8_t c = REVCMP[(uint8_t) seq[j]];
merid = c + (merid << 2);
}
range = idx.lut->kmer[merid];
if (range.top < range.bot) {
range = (range_t) {.bot = 1, .top = idx.occ->txtlen-1};
i = 0;
}
else {
i = LUTK;
}
for ( ; i < len ; i++) {
if (NONALPHABET[(uint8_t)seq[i]]) break;
int c = REVCMP[(uint8_t) seq[i]];
newrange.bot = get_rank(idx.occ, c, range.bot - 1);
newrange.top = get_rank(idx.occ, c, range.top) - 1;
// Stop if fewer than 2 hist.
if (newrange.top < newrange.bot + 1)
break;
range = newrange;
}
L2->end = i-1;
L2->range = range;
return;
}
wstack_t *
mem_seeds
(
const char * seq,
const index_t idx,
const size_t gamma
)
{
int len = strlen(seq);
int end = len-1;
while (end > 0 && NONALPHABET[(uint8_t)seq[end]]) end--;
if (end == 0)
return stack_new(1);
// Initialize mem stack
wstack_t * mems = stack_new(32);
range_t range;
range_t newrange = {0};
// Iterate over all read positions
while (1) {
seed_t mem = {0};
mem.end = end;
// Backward <<<
range = (range_t) { .bot = 1, .top = idx.occ->txtlen-1 };
int mpos = end, mlen = 0;
// Query the beginning of the read in lookup table.
if (end >= LUTK - 1) {
size_t merid = 0;
for ( ; mlen < LUTK ; mlen++, mpos--) {
if (NONALPHABET[(uint8_t) seq[end-mlen]]) {
range.bot = 1;
range.top = 0;
break;
}
uint8_t c = ENCODE[(uint8_t) seq[end-mlen]];
merid = c + (merid << 2);
}
range = idx.lut->kmer[merid];
}
// Cancel if we went too far already.
if (range.top < range.bot) {
range = (range_t) { .bot = 1, .top = idx.occ->txtlen-1 };
mpos = end; mlen = 0;
}
for ( ; mpos >= 0 ; mpos--, mlen++) {
if (NONALPHABET[(uint8_t)seq[mpos]]) break;
int c = ENCODE[(uint8_t) seq[mpos]];
newrange.bot = get_rank(idx.occ, c, range.bot - 1);
newrange.top = get_rank(idx.occ, c, range.top) - 1;
// Stop if no hits.
if (newrange.top < newrange.bot)
break;
range = newrange;
}
mem.beg = ++mpos;
mem.range = range;
// Keep MEM if above minimum length.
if (mlen >= gamma) {
seed_t * m = malloc(sizeof(seed_t));
exit_on_memory_error(m);
memcpy(m, &mem, sizeof(seed_t));
push(m, &mems);
}
if (mem.beg < 1) break;
// Find new end position (forward).
end = mem.beg - 1;
if (NONALPHABET[(uint8_t) seq[end]]) {
while (end > 0 && NONALPHABET[(uint8_t) seq[end]]) end--;
} else {
// Use the lookup table.
size_t merid = 0;
for (int i = 0 ; i < LUTK ; i++) {
uint8_t c = REVCMP[(uint8_t) seq[end+i]];
merid = c + (merid << 2);
}
range = idx.lut->kmer[merid];
if (range.top < range.bot) {
// Cancel if we went too far already.
range = (range_t) { .bot = 1, .top = idx.occ->txtlen-1 };
}
else {
// Otherwise move end position.
end += LUTK;
}
while (1) {
int c = REVCMP[(uint8_t) seq[end]];
range.bot = get_rank(idx.occ, c, range.bot - 1);
range.top = get_rank(idx.occ, c, range.top) - 1;
if (range.top < range.bot) {
end--;
break;
}
end++;
}
}
if (end + 1 < gamma) break; // No more seeds.
}
// DEBUG VERBOSE
if (DEBUG_VERBOSE) {
fprintf(stdout,"\nMEMs (%ld):\n", mems->pos);
for(int i = 0; i < mems->pos; i++) {
seed_t * m = (seed_t *) mems->ptr[i];
fprintf(stdout, "[%d] (%ld, %ld) loci: %ld, range: (%ld, %ld)\n",
i, m->beg, m->end, m->range.top-m->range.bot+1, m->range.bot, m->range.top);
}
fprintf(stdout,"\n");
}
return mems;
}
wstack_t *
skip_seeds
(
const char * seq,
const index_t idx,
const size_t gamma,
const size_t skip
)
{
int len = strlen(seq);
int end = len-1;
while (end > 0 && NONALPHABET[(uint8_t)seq[end]]) end--;
if (end == 0)
return stack_new(1);
// Initialize mem stack
wstack_t * seeds = stack_new(32);
range_t range;
// Iterate over all read positions
while (end >= gamma - 1) {
seed_t seed = {0};
seed.end = end;
// Backward <<<
range = (range_t) { .bot = 1, .top = idx.occ->txtlen-1 };
int mpos = end, mlen = 0;
// Query the beginning of the read in lookup table.
if (end >= LUTK - 1) {
size_t merid = 0;
for ( ; mlen < LUTK ; mlen++, mpos--) {
if (NONALPHABET[(uint8_t) seq[end-mlen]]) {
range.bot = 1;
range.top = 0;
break;
}
uint8_t c = ENCODE[(uint8_t) seq[end-mlen]];
merid = c + (merid << 2);
}
range = idx.lut->kmer[merid];
}
// Cancel if we went too far already.
if (range.top < range.bot) {
range = (range_t) { .bot = 1, .top = idx.occ->txtlen-1 };
mpos = end; mlen = 0;
}
for ( ; mlen < gamma ; mpos--, mlen++) {
if (NONALPHABET[(uint8_t)seq[mpos]])
break;
int c = ENCODE[(uint8_t) seq[mpos]];
range.bot = get_rank(idx.occ, c, range.bot - 1);
range.top = get_rank(idx.occ, c, range.top) - 1;
// Stop if no hits.
if (range.top < range.bot)
break;
}
if (mlen == gamma) {
// Update seed info
seed.beg = seed.end - gamma + 1;
seed.range = range;
// Push seed to stack
seed_t * m = malloc(sizeof(seed_t));
exit_on_memory_error(m);
memcpy(m, &seed, sizeof(seed_t));
push(m, &seeds);
}
// Update end position
end -= (skip + 1);
if (end < gamma - 1 && end > gamma - 1 - skip)
end = gamma - 1;
}
return seeds;
}
aligncd_t
mem_alignments
(
seedchain_t * chain,
index_t idx,
size_t slen
)
{
// Allocate alignment candidates.
align_t * alncd = malloc(chain->loci * sizeof(align_t));
exit_on_memory_error(alncd);
// Get all genomic positions.
size_t nloc = 0;
for (int j = 0; j < chain->pos; j++) {
seed_t * seed = chain->seed[j];
if (seed->aligned) {
continue;
}
seed->aligned = 1;
// Compute SA positions.
if (!seed->sa)
seed->sa = query_csa_range(idx.csa, idx.bwt, idx.occ, seed->range);
// Make chained alignment candidates from seed genomic positions.
for (int k = 0; k < seed->range.top - seed->range.bot + 1; k++) {
alncd[nloc++] = (align_t){seed->sa[k], seed->end - seed->beg + 1, 0, seed};
}
}
if (nloc == 0) {
if (DEBUG_VERBOSE) {
fprintf(stdout, "[skip: chain] No alncd after removing alignment duplicates\n");
}
free(alncd);
return (aligncd_t){0, NULL};
}
if (DEBUG_VERBOSE)
fprintf(stdout, "[MEM chain] %ld alncd after removing alignment duplicates\n", nloc);
// Sort alncd by genomic position.
qsort(alncd, nloc, sizeof(align_t), seed_by_refpos);
// Chain alncd to avoid duplicated alignments.
int cur = 0;
size_t chain_gap_beg = alncd[cur].refpos + (alncd[cur].seed->end - alncd[cur].seed->beg + 1);
size_t chain_gap_end = alncd[cur].refpos + (slen - alncd[cur].seed->beg) - 1;
size_t nchain = 1;
for (int j = 1; j < nloc; j++) {
// Chain alncd if they are within 'slen' genomic nucleotides.
size_t seed_ref_beg = alncd[j].refpos;
size_t seed_ref_end = seed_ref_beg + (alncd[j].seed->end - alncd[j].seed->beg);
if (alncd[cur].seed != alncd[j].seed && chain_gap_beg <= seed_ref_beg && seed_ref_end <= chain_gap_end) {
// Seed j is within current chain.
// Update seed chain span.
alncd[cur].span += (alncd[j].seed->end - alncd[j].seed->beg + 1);
// Set seed j span to 0.
alncd[j].span = 0;
} else {
// Seed j is outside current chain.
// Update current chain.
cur = j;
// Update gap coordinates.
chain_gap_beg = alncd[cur].refpos + (alncd[cur].seed->end - alncd[cur].seed->beg + 1);
chain_gap_end = alncd[cur].refpos + (slen - alncd[cur].seed->beg) - 1;
// Update chain count.
nchain++;
}
}
if (DEBUG_VERBOSE)
fprintf(stdout, "[MEM chain] %ld alncd after chaining\n", nchain);
// Sort alncd by span and align them.
qsort(alncd, nloc, sizeof(align_t), seed_by_span);
return (aligncd_t){nchain, alncd};
}
void
align
(
align_t alignment,
const char * seq,
char * genome,
size_t genome_len,
int * best_score,
alnstack_t ** best
)
{
seed_t * seed = alignment.seed;
size_t slen = strlen(seq);
if (seed->beg > alignment.refpos)
return;
if (alignment.refpos + slen - seed->beg >= genome_len)
return;
int score = -1;
if (seed->beg == 0 && seed->end == slen-1) {