-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjigsawaln.cpp
executable file
·4366 lines (3629 loc) · 149 KB
/
jigsawaln.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 <list>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdint.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "bntseq.h"
#include "bwaseqio.h"
//#include "bwtaln.h"
#include "bwase.h"
#include "bwtgap.h"
#include "utils.h"
#include "splicesitemap.h"
#include "jigsawaln.h"
#include "kstring.h"
#include "splicescore.h"
#ifdef HAVE_PTHREAD
#define THREAD_BLOCK_SIZE 1024
#include <pthread.h>
static pthread_mutex_t g_seq_lock = PTHREAD_MUTEX_INITIALIZER;
#endif
gap_opt_t *gap_init_opt()
{
gap_opt_t *o;
o = (gap_opt_t*)calloc(1, sizeof(gap_opt_t));
/* IMPORTANT: s_mm*10 should be about the average base error
rate. Voilating this requirement will break pairing! */
o->word_size = 15;
o->word_max_overlap = 1;
o->max_word_diff = 0;
o->max_word_occ = -1 ; // will reset this in jigsaw.cpp
o->min_anchor = 8;
o->known_junc_min_anchor = 5;
o->max_overhang = 6;
o->junction_file = 0;
o->regression_file = 0;
o->min_exon_size = 9;
o->max_intron_size = 500000;
o->min_intron_size = 20;
o->single_anchor_search = 1;
o->allow_rep_anchor = 0;
o->non_denovo_search = 0;
o->strand_mode = 3;
o->min_logistic_prob = 0.5;
o->splice_site_map = 0;
o->s_mm = 3; o->s_gapo = 11; o->s_gape = 4;
o->max_diff = 4; o->max_gapo = 1; o->max_gape = 6;
o->report_best_only = 0;
o->indel_end_skip = 5; o->max_del_occ = 10; o->max_entries = 2000000;
o->n_batch = 0x40000;
o->max_report_multi = 20;
o->mode = BWA_MODE_GAPE | BWA_MODE_COMPREAD;
//o->seed_len = 32; o->max_seed_diff = 2;
o->fnr = 0.06;
o->n_threads = 1;
o->max_top2 = 30;
o->trim_qual = 0;
o->rg = 0;
o->verbose = 0;
return o;
}
int bwa_cal_maxdiff(int l, double err, double thres)
{
double elambda = exp(-l * err);
double sum, y = 1.0;
int k, x = 1;
for (k = 1, sum = elambda; k < 1000; ++k) {
y *= l * err;
x *= k;
sum += elambda * y / x;
if (1.0 - sum < thres) return k;
}
return 2;
}
// width must be filled as zero
static int bwt_cal_width(const bwt_t *rbwt, int len, const ubyte_t *str, bwt_width_t *width)
{
bwtint_t k, l, ok, ol;
int i, bid;
bid = 0;
k = 0; l = rbwt->seq_len;
for (i = 0; i < len; ++i) {
ubyte_t c = str[i];
if (c < 4) {
bwt_2occ(rbwt, k - 1, l, c, &ok, &ol);
k = rbwt->L2[c] + ok + 1;
l = rbwt->L2[c] + ol;
}
if (k > l || c > 3) { // then restart
k = 0;
l = rbwt->seq_len;
++bid;
}
width[i].w = l - k + 1;
width[i].bid = bid;
}
width[len].w = 0;
width[len].bid = ++bid;
return bid;
}
/*calculate the SA interval of one query sequence
*/
void jigsaw_cal_sa_reg_gap(bwt_t *const bwt[2], bwa_seq_t *seq, gap_stack_t *stack, const gap_opt_t *opt)
{
bwt_width_t *w[2]; //, *seed_w[2];
const ubyte_t *s[2];
bwa_seq_t *p = seq;
p->sa = 0; p->type = BWA_TYPE_NO_MATCH; p->c1 = p->c2 = 0; p->n_aln = 0; p->aln = 0;
//sense_strand is init' here too: 0 +, 1 -, 2 .while opt->strand_mode 1 +, 2 -, 3 .
p->sense_strand = 2; //opt->strand_mode - 1;
s[0] = p->seq; s[1] = p->rseq;
w[0] = w[1] = 0;
w[0] = (bwt_width_t*)realloc(w[0], (p->len + 1) * sizeof(bwt_width_t));
w[1] = (bwt_width_t*)realloc(w[1], (p->len + 1) * sizeof(bwt_width_t));
memset(w[0], 0, (p->len + 1) * sizeof(bwt_width_t));
memset(w[1], 0, (p->len + 1) * sizeof(bwt_width_t));
//note that seq[0] is the forward sequence without reverse now, and seq[1] is the reverse complementary sequence
//both need to be aligned to bwt[0], and bwt[1] should be used to calculate the bound
bwt_cal_width(bwt[1], p->len, s[0], w[0]);
bwt_cal_width(bwt[1], p->len, s[1], w[1]);
// core function
p->aln = bwt_match_gap(bwt[0], p->len, s, w, opt, &p->n_aln, stack);
// store the alignment
free(w[0]); free(w[1]);
}
bool jigsaw_check_seq_complexity (jigsaw_anchor_seq_t *anchor_seq)
{
//if the seq is repetitive, return a 1, otherwise, return a 0
bool is_repetitive = 1;
ubyte_t *di_nt = (ubyte_t*) calloc (2, sizeof(ubyte_t) );
di_nt[0] = anchor_seq->seq[0];
di_nt[1] = anchor_seq->seq[1];
for (int i = 2; i<anchor_seq->len; i++){
if (anchor_seq->seq[i] != di_nt[i%2] ){
is_repetitive = 0;
break;
}
}
free(di_nt);
return is_repetitive;
}
/*
this function returns a list of all hits of an anchor sequence
for single anchor search
*/
void jigsaw_collect_anchor_hits (bwt_t *const bwt[2], jigsaw_anchor_seq_t *anchor_seq, const int *g_log_n,
int64_t l_pac, const ubyte_t *pacseq, const ubyte_t *ntpac,
int max_word_occ, const gap_opt_t *opt, list<jigsaw_word_hit_t*> *hits)
{
//check if the anchor_seq is low-complexity, those seqs will highly affect the mapping speed.
//e.g. GTGTGTGTGT, ACACACACAC
if ( ( ! opt->allow_rep_anchor ) && jigsaw_check_seq_complexity (anchor_seq) ) return;
gap_opt_t local_opt = *opt;
//local_opt.max_diff = opt->max_word_diff;
local_opt.max_diff = 0;//do not allow mismatch in single anchor search for now
if( anchor_seq ->len >=18 && opt->max_diff >1) local_opt.max_diff = 1; // allow mismatch when the anchor is long
//TODO: allow mismatch later, and set it as a parameter.
local_opt.max_gapo = local_opt.max_gape = 0;
gap_stack_t *stack = gap_init_stack(local_opt.max_diff, local_opt.max_gapo, local_opt.max_gape, &local_opt);
// all the SA will be saved in anchor_seq->aln
jigsaw_cal_sa_reg_gap (bwt, anchor_seq, stack, &local_opt);
jigsaw_collect_word_hits_core (bwt[0], anchor_seq, hits);
free(anchor_seq->aln);
gap_destroy_stack(stack);
}
/*
* return a list of all hits of all non-repetitive words,
* and also record the occurrence of each word in n_occ
*
* max_word_occ: the maximum number of occurrence allowed for a non-repetitive word
* n_hits: return the number of hits found
*
*/
void jigsaw_collect_word_hits (bwt_t *const bwt[2], jigsaw_word_t *words, int n_words, const int *g_log_n,
int64_t l_pac, const ubyte_t *pacseq, const ubyte_t *ntpac,
int max_word_occ, const gap_opt_t *opt, list<jigsaw_word_hit_t*> *hits)
{
int i;
gap_opt_t local_opt = *opt;
local_opt.max_diff = opt->max_word_diff;
local_opt.max_gapo = local_opt.max_gape = 0;
//the alignment of words need to be perfect, so we create a local (smaller) stack to
//avoid unnecessarily big stack
gap_stack_t *stack = gap_init_stack(local_opt.max_diff, local_opt.max_gapo, local_opt.max_gape, &local_opt);
//find perfect match for each word
//jigsaw_word_hit_t *hits = 0;
int total_occ = 0;
for (i = 0; i != n_words; ++i) {
jigsaw_word_t *w = words+i;
/*find all SA intervals stored at w->aln*/
jigsaw_cal_sa_reg_gap (bwt, w, stack, &local_opt);
/*the total number of hits of this word*/
int word_occ = jigsaw_word_hits_total (w);
w->n_occ = word_occ;
if (word_occ <= max_word_occ) {
//hits = (jigsaw_word_hit_t*)realloc (hits, (total_occ + word_occ) * sizeof (jigsaw_word_hit_t));
/*calculate the query and target coordinates for all hits of the word, and save it to "hits"*/
jigsaw_collect_word_hits_core (bwt[0], w, hits);
total_occ += word_occ;
}
}
//*n_hits = total_occ;
gap_destroy_stack(stack);
//return hits;
}
/*compare two hits by diagonal coordinates
*/
bool jigsaw_word_hits_comp_diagonal (const jigsaw_word_hit_t *a, const jigsaw_word_hit_t *b)
{
int64_t da = a->pos_t - a->pos_q;
int64_t db = b->pos_t - b->pos_q;
return da < db ? true : false;
}
/* sort hits (ascendingly) by diagonal coordinates
*/
void jigsaw_sort_word_hits_by_diagonal (list<jigsaw_word_hit_t*> *hits)
{
hits->sort (jigsaw_word_hits_comp_diagonal);
}
/* compare hits by strand, and then by diagonal
*/
int jigsaw_word_hits_comp_strand_diagonal (const void *a, const void *b)
{
jigsaw_word_hit_t *ha = (jigsaw_word_hit_t*) a;
jigsaw_word_hit_t *hb = (jigsaw_word_hit_t*) b;
if (ha->strand > hb->strand) return 1;
else if (ha->strand < hb->strand) return -1;
else {//the same strand compare diagonal
int64_t da = ha->pos_t - ha->pos_q;
int64_t db = hb->pos_t - hb->pos_q;
return da > db ? 1 : (da < db ? -1 : 0);
}
}
/* sort hits (ascendingly) by diagonal coordinates
*/
void jigsaw_sort_word_hits_by_strand_diagonal (jigsaw_word_hit_t *hits, int n_hits)
{
qsort (hits, n_hits, sizeof (jigsaw_word_hit_t), jigsaw_word_hits_comp_strand_diagonal);
}
/* compare hits by query coordinates
*/
bool jigsaw_word_hits_comp_pos_q (const jigsaw_word_hit_t *a, const jigsaw_word_hit_t *b)
{
return a->pos_q < b->pos_q ? true : false;
}
/* sort hits (ascendingly) by query coordinates
*/
void jigsaw_sort_word_hits_by_pos_q (list<jigsaw_word_hit_t*> *hits)
{
hits->sort (jigsaw_word_hits_comp_pos_q);
}
/* compare hits by strand, and then by pos_t
*/
bool jigsaw_word_hits_comp_strand_pos_t (const jigsaw_word_hit_t *a, const jigsaw_word_hit_t *b)
{
//jigsaw_word_hit_t *ha = (jigsaw_word_hit_t*) a;
//jigsaw_word_hit_t *hb = (jigsaw_word_hit_t*) b;
if (a->strand < b->strand) return true;
else if (a->strand > b->strand) return false;
else {//the same strand compare pos_t
return a->pos_t < b->pos_t ? true : false;
}
}
/* sort hits (ascendingly) by diagonal coordinates
*/
void jigsaw_sort_word_hits_by_strand_pos_t (list<jigsaw_word_hit_t*> *hits)
{
hits->sort(jigsaw_word_hits_comp_strand_pos_t);
//qsort (hits, n_hits, sizeof (jigsaw_word_hit_t), jigsaw_word_hits_comp_strand_pos_t);
}
/*sort exons by target coordinates
*/
bool jigsaw_exon_comp_pos_t (const jigsaw_exon_t *a, const jigsaw_exon_t *b)
{
//colinearity, descending
if (a->colinear > b->colinear) return true;
else if (a->colinear < b->colinear) return false;
else return a->start_t < b->start_t ? true : false; //target coordinates, ascending
}
/* sort exons by target coordinates
*/
void jigsaw_sort_exons_by_pos_t (list<jigsaw_exon_t*> *exons)
{
//sort all exons
exons->sort (jigsaw_exon_comp_pos_t);
}
/*check if alignment of hits is colinear
* hits must have been sorted according to pos_q (or equivalently wid)
* so for a colinear clump, pos_t must be non-decreasing
*/
uint32_t jigsaw_hits_colinear (list<jigsaw_word_hit_t*> *hits)
{
uint32_t colinear = 1;
list<jigsaw_word_hit_t*>::iterator iter;
iter = hits->begin();
jigsaw_word_hit_t *a = *iter; ++iter;
for (; iter != hits->end(); ++iter) {
jigsaw_word_hit_t *b = *iter;
if (a->pos_t > b->pos_t) {
colinear = 0;
break;
}
a = b;
}
return colinear;
}
/*calculate the coverage of hits in each exon
* hits in the exon must have been sorted according to pos_q (or equivalently wid)
*/
float jigsaw_exon_coverage (jigsaw_exon_t *exon)
{
int n_hits = exon->hits->size();
jigsaw_word_hit_t *first, *last;
list<jigsaw_word_hit_t *>::iterator iter = exon->hits->begin(); first = *iter;
iter = exon->hits->end (); --iter; last = *iter;
int n_words = last->wid - first->wid +1;
return ((float)n_hits)/n_words;
}
/*1.for each exon, sort hits according to target coordinates
*2. evaluate each exon in terms of colinearity, the number of hits, coverage, and uniqueness of words
*/
void jigsaw_eval_exons (list<jigsaw_exon_t*> *exons, int word_size)
{
//int i;
list<jigsaw_exon_t *>::iterator iter;
for (iter = exons->begin(); iter != exons->end(); iter++) {
jigsaw_exon_t *p = *iter;
if (p->hits->size() == 0) continue; //this should never happen
jigsaw_sort_word_hits_by_pos_q (p->hits);
p->colinear = jigsaw_hits_colinear (p->hits);
p->coverage = jigsaw_exon_coverage (p);
//p->score = jigsaw_clump_uniqueness (p, words, l_pac); //log E-value of the clump
jigsaw_word_hit_t *first = p->hits->front();
jigsaw_word_hit_t *last = p->hits->back();
p->strand = first->strand;
p->start_q = first->pos_q;
p->end_q = last->pos_q + word_size - 1;
p->start_t = first->pos_t;
p->end_t = last->pos_t + word_size - 1;
//TODO: more properties?
}
}
/* cluster hits according to diagonal coordinates
* the idea borrowed from BLAT
*/
void jigsaw_group_hits_to_exons (list <jigsaw_word_hit_t*> *hits, int max_diff, int word_size, int n_words, list <jigsaw_exon_t *> *exons)
{
int local_max_diff = int (max_diff);
if( local_max_diff > 2 ) local_max_diff = 2;
jigsaw_exon_t *curr_exon = 0;
int64_t prev_hit_diag = 0, curr_hit_diag;
uint32_t prev_strand = 2;
//illegal value so that a new clump will be created at the very beginning
list <jigsaw_word_hit_t *>::iterator iter;
uint32_t current_exon_id = 0;
//require each word to have unique occurrence in each cluster
int* word_uniqueness = (int *)calloc(n_words, sizeof(int));
for (iter = hits->begin(); iter != hits->end(); ++iter)
{
int wid = abs ((*iter)->wid);
word_uniqueness[wid]++;
}
//fprintf (stderr, "group exons ...\n");
for (iter = hits->begin(); iter != hits->end(); ++iter)
{
jigsaw_word_hit_t *p = *iter;
if (word_uniqueness[abs(p->wid)]>1)
continue;
curr_hit_diag = p->pos_t - p->pos_q;
if (p->strand != prev_strand || curr_hit_diag - prev_hit_diag > local_max_diff) {
//create a new exon
//initialization
curr_exon = (jigsaw_exon_t *) calloc (1, sizeof (jigsaw_exon_t));
curr_exon->n_mm = curr_exon->n_gapo_t = curr_exon->n_gapo_q = curr_exon->n_gape_t = curr_exon->n_gape_q = 0;
curr_exon->exon_id = current_exon_id++;
//fprintf(stderr, "create a new exon id=%d\n", curr_exon->exon_id);
curr_exon->hits = new list<jigsaw_word_hit_t*>;
curr_exon->is_first = curr_exon->is_last = 1;
exons->push_back (curr_exon);
}
//add the hits to the current clump
//double the capacity of the array if necessary
curr_exon->hits->push_back (p);
//fprintf (stderr, "exon_id=%d, start_t=%d, start_q=%d\n", curr_exon->exon_id, p->pos_t, p->pos_q);
prev_hit_diag = curr_hit_diag;
prev_strand = p->strand;
}
free (word_uniqueness);
//fprintf (stderr, "%d exons identified\n", exons->size());
//evaluate exons: calculate colinearity, etc
jigsaw_eval_exons (exons, word_size);
}
/*copy one clump
*/
/*
void jigsaw_copy_clump (jigsaw_clump_t *to, const jigsaw_clump_t *from)
{
*to = *from;
to->hits = calloc (from->n_hits, sizeof (jigsaw_word_hit_t));
memcpy (to->hits, from->hits, from->n_hits * sizeof (jigsaw_word_hit_t));
}
*/
/*filter clumps according to colinearity and score
* colinear clumps with a score <= max_score is copied to a new array and returned
*/
/*
jigsaw_clump_t* jigsaw_filter_clumps (jigsaw_clump_t* clumps, int n_clumps, float max_score, int *n_filtered_clumps)
{
int i, max_clumps = 4;
jigsaw_clump_t *filtered_clumps = (jigsaw_clump_t*) calloc (max_clumps, sizeof (jigsaw_clump_t));
int _n_filtered_clumps = 0;
for (i = 0; i != n_clumps; ++i)
{
jigsaw_clump_t *p = clumps + i;
if (p->colinear != 1 || p->score > max_score) continue;
if (max_clumps <= _n_filtered_clumps) { //double the capacity of the array if necessary
max_clumps <<= 1;
filtered_clumps = (jigsaw_clump_t*) realloc (filtered_clumps, max_clumps * sizeof (jigsaw_clump_t));
}
jigsaw_copy_clump (filtered_clumps+_n_filtered_clumps, p);
++_n_filtered_clumps;
}
*n_filtered_clumps = _n_filtered_clumps;
return filtered_clumps;
}
*/
/*extend a hit on both ends by exact matches
* direction: 0-extend on the right, 1-extend on the left, 2-extend both side
*/
void jigsaw_extend_exon_exact (jigsaw_exon_t *exon, bwa_seq_t *seq, int direction,
int64_t l_pac, const ubyte_t *pacseq, const ubyte_t *ntpac)
{
ubyte_t *query_seq = exon->strand ? seq->rseq : seq->seq;
jigsaw_exon_t *p = exon;
//extend on the left
if (direction != 0) {
int i = 1;
while (p->start_q -i >= 0 && p->start_t -i >= 0) {
int64_t kq = p->start_q -i, kt = p->start_t -i;
ubyte_t q = query_seq [kq];
ubyte_t t = get_pacseq_base (pacseq, kt);
//ubyte_t t = pacseq[kt>>2] >> ((~kt&3)<<1) & 3;
if (q != t) break;
++i;
}
--i;
p->start_q -= i; p->start_t -= i;
}
//extend on the right
if (direction != 1) {
int i = 1;
while (p->end_q +i < seq->len && p->end_t +i < l_pac) {
int64_t kq = p->end_q +i, kt = p->end_t +i;
ubyte_t q = query_seq [kq];
ubyte_t t = get_pacseq_base (pacseq, kt);
//ubyte_t t = pacseq[kt>>2] >> ((~kt&3)<<1) & 3;
if (q != t) break;
++i;
}
--i;
p->end_q += i; p->end_t += i;
}
}
/*extend a hit on both ends by inexact matches allowing for substitutions but not indels
* direction: 0-extend on the right, 1-extend on the left, 2-extend both side
*/
void jigsaw_extend_exon_inexact (jigsaw_exon_t *exon, bwa_seq_t *seq, int direction,
int64_t l_pac, const ubyte_t *pacseq, const ubyte_t *ntpac, bool stop_if_neg_score, int opt_max_diff)
{
ubyte_t *query_seq = exon->strand ? seq->rseq : seq->seq;
jigsaw_exon_t *p = exon;
int match = 1, mismatch = -3, max_diff = 2;
if (opt_max_diff <2) max_diff = opt_max_diff;
//TODO: implicitly require 4 matches at the end
//extend on the left
if (direction != 0) {
int i = 0, end = 0;
int score = 0, max_score = 0, diff = 0, min_diff = max_diff;
while (p->start_q -i >= 0 && p->start_t -i >= 0) {
int64_t kq = p->start_q -i, kt = p->start_t -i;
ubyte_t q = query_seq [kq];
ubyte_t t = get_pacseq_base (pacseq, kt);
//ubyte_t t = pacseq[kt>>2] >> ((~kt&3)<<1) & 3;
if (q == t) score += match;
else {
score += mismatch;
diff++;
if (diff > max_diff) break;
}
if (stop_if_neg_score && score < 0) break;
//if the score is not the optimal, but it can extend to the very end of the read
//we still want to accept the extension
if (score > max_score || i == p->start_q) {
max_score = score; end = i; min_diff = diff;
}
++i;
}
//if (end > 0) --end; //extendible
p->start_q -= end; p->start_t -= end;
p->n_mm += min_diff;
}
//extend on the right
if (direction != 1) {
int i = 0, end = 0;
int score = 0, max_score = 0, diff = 0, min_diff = max_diff;
while (p->end_q +i < seq->len && p->end_t +i < l_pac) {
int64_t kq = p->end_q +i, kt = p->end_t +i;
ubyte_t q = query_seq [kq];
//ubyte_t t = pacseq[kt>>2] >> ((~kt&3)<<1) & 3;
ubyte_t t = get_pacseq_base (pacseq, kt);
if (q == t) score += match;
else {
score += mismatch;
diff++;
if (diff > max_diff) break;
}
if (stop_if_neg_score && score < 0) break;
//if the score is not the optimal, but it can extend to the very end of the read
//we still want to accept the extension
if (score > max_score || p->end_q+i == seq->len - 1) {
max_score = score; end = i; min_diff = diff;
}
++i;
}
//if (end > 0) --end; //extendible
p->end_q += end; p->end_t += end;
p->n_mm += min_diff;
}
}
void jigsaw_extend_exons (list<jigsaw_exon_t*> *exons, bwa_seq_t *seq,
int64_t l_pac, const ubyte_t *pacseq, const ubyte_t *ntpac, uint32_t exact, bool stop_if_neg_score, int opt_max_diff)
{
list<jigsaw_exon_t*>::iterator iter;
for (iter = exons->begin(); iter != exons->end (); ++iter) {
jigsaw_exon_t *p = *iter;
exact ? jigsaw_extend_exon_exact (p, seq, 2, l_pac, pacseq, ntpac) : jigsaw_extend_exon_inexact (p, seq, 2, l_pac, pacseq, ntpac, stop_if_neg_score, opt_max_diff);
}
}
/* fill in gaps in an exon to the reference genome
* TODO: now the mismataches are counted only in the gaps, but now in the words themselves
*/
void jigsaw_refine_exon_aln_core (jigsaw_exon_t *exon, bwa_seq_t *seq,
int64_t l_pac, const ubyte_t *pacseq, const ubyte_t *ntpac, int band_width)
{
int path_len;
jigsaw_exon_t *p = exon;
AlnParam ap = aln_param_bwa;
//TODO: need more sophisticated rules; the bound is too loose here
ap.band_width = band_width>1 ? band_width: 1;
//TODO, band_width > 1 for now, avoid crashes in aln_global_core
//if (strcmp (seq->name, "HWI-ST155_05162111902026#0") == 0)
//{
// fprintf (stderr, "found\n");
//}
//initialization
//p->n_mm = p->n_gapo_t = p->n_gapo_q = p->n_gape_t = p->n_gape_q = 0;
//the line above is unnessary now since there is inexact extending step -j
//need to find a way to sum up the mismatches...
if (p->hits->size() < 2) return;
//fprintf (stderr, "refine exons ...\n");
list<jigsaw_word_hit_t *>::iterator prev, next;
prev = next = p->hits->begin(); ++next;
for (; next != p->hits->end (); ++prev, ++next) {
int64_t ref_start = (*prev)->pos_t + seq->word_size;
int ref_len = (*next)->pos_t - ref_start;
int start = (*prev)->pos_q + seq->word_size;
int len = (*next)->pos_q - start;
//fprintf (stderr, "exon_id=%d, start_t1=%d, start_q1=%d, start_t2=%d, start_q2=%d\n", exon->exon_id, (*prev)->pos_t, (*prev)->pos_q, (*next)->pos_t, (*next)->pos_q);
//TODO: double check if there are bugs in the following lines in rare cases
if (ref_len <= 0 && len <= 0 && len == ref_len ) continue;
else if (ref_len <= 0 && len > ref_len ) {
//no nucleotide in reference
p->n_gapo_t += 1; p->n_gape_t += (len - ref_len - 1);
continue;
}
else if (len <= 0 && ref_len > len) {
//no nucleotide in query
p->n_gapo_q += 1; p->n_gape_q += (ref_len - len - 1);
continue;
}
//both are not empty
ubyte_t* ref_seq = (ubyte_t*)calloc(ref_len, sizeof(ubyte_t));
int64_t k, l;
for (k = ref_start, l= 0; k < ref_start + ref_len; ++k)
ref_seq[l++] = get_pacseq_base (pacseq, k); //pacseq[k>>2] >> ((~k&3)<<1) & 3;
ubyte_t *query_seq = (exon->strand ? seq->rseq : seq->seq) + start;
path_t *path = (path_t*)calloc(ref_len+len, sizeof(path_t));
aln_global_core(ref_seq, ref_len, query_seq, len, &ap, path, &path_len);
//calculate the number of mismatches and gaps
uint32_t n_mm, n_gapo_t, n_gapo_q, n_gape_t, n_gape_q;
jigsaw_cal_diff (path, path_len, ref_seq, ref_len, query_seq, len,
&n_mm, &n_gapo_t, &n_gapo_q, &n_gape_t, &n_gape_q);
free (ref_seq); free (path);
p->n_mm += n_mm;
p->n_gapo_t += n_gapo_t; p->n_gapo_q += n_gapo_q;
p->n_gape_t += n_gape_t; p->n_gape_q += n_gape_q;
}
}
void jigsaw_refine_exon_aln (list<jigsaw_exon_t*> *exons, bwa_seq_t *seq,
int64_t l_pac, const ubyte_t *pacseq, const ubyte_t *ntpac, int band_width)
{
list<jigsaw_exon_t*>::iterator iter;
for (iter = exons->begin(); iter != exons->end(); ++iter) {
jigsaw_refine_exon_aln_core (*iter, seq, l_pac, pacseq, ntpac, band_width);
}
}
/*an approximate estimate of E-value
* count each word only once if multiple hits exists in the alignment
*/
float jigsaw_spliced_aln_cluster_uniqueness (jigsaw_spliced_aln_cluster_t *aln,
jigsaw_word_t *words, int n_words, int64_t l_pac)
{
float log_occ = 0;
uint32_t *u = (uint32_t *) calloc (n_words, sizeof(uint32_t));
int n_hits_uniq = 0;
list <jigsaw_word_hit_t *>::iterator iter;
for (iter = aln->hits->begin(); iter != aln->hits->end(); ++iter) {
jigsaw_word_hit_t *h = *iter;
int wid = abs(h->wid);
if (u[wid]) continue;
++u[wid];
++n_hits_uniq;
jigsaw_word_t* w = words + wid;
log_occ += log10((float)w->n_occ);
}
free (u);
return log_occ - (n_hits_uniq -1) * log10 ((float)l_pac);
}
/*evaluate candidate alignment according to uniqueness of words
*/
static void jigsaw_eval_spliced_aln_clusters (list<jigsaw_spliced_aln_cluster_t *> *clusters,
jigsaw_word_t *words, int n_words, int word_size, int64_t l_pac)
{
list<jigsaw_spliced_aln_cluster_t *>::iterator iter;
for (iter = clusters->begin(); iter != clusters->end(); ++iter) {
jigsaw_spliced_aln_cluster_t *p = *iter;
p->score = jigsaw_spliced_aln_cluster_uniqueness (p, words, n_words, l_pac); //log E-value of the clump
//p->strand = p->hits[0].strand;
//p->start_q = p->hits[0].pos_q;
//p->end_q = p->hits[p->n_hits-1].pos_q + word_size - 1;
//p->start_t = p->hits[0].pos_t;
//p->end_t = p->hits[p->n_hits-1].pos_t + word_size - 1;
//TODO: more properties?
}
}
/*
* not in use
uint32_t jigsaw_spliced_aln_cluster_unique_hits (jigsaw_spliced_aln_cluster_t *aln, int n_words)
{
uint32_t *u = (uint32_t *) calloc (n_words, sizeof(uint32_t));
uint32_t n_hits_uniq = 0;
list <jigsaw_word_hit_t *>::iterator iter;
for (iter = aln->hits->begin(); iter != aln->hits->end(); ++iter) {
jigsaw_word_hit_t *h = *iter;
int wid = abs(h->wid);
if (u[wid]) continue;
++u[wid];
++n_hits_uniq;
}
free (u);
return n_hits_uniq;
}
*/
/*hits should have been sorted by strand and then by pos_t before calling this function*/
void jigsaw_group_hits_to_spliced_aln_clusters (list<jigsaw_word_hit_t*> *hits, int max_intron_size,
jigsaw_word_t *words, int n_words, int word_size, int64_t l_pac, list<jigsaw_spliced_aln_cluster_t *> *clusters)
{
jigsaw_spliced_aln_cluster_t *curr_clust = 0;
int64_t prev_pos_t = 0, curr_pos_t;
uint32_t prev_strand = 2;
//illegal value so that a new clump will be created at the very beginning
int prev_wid_direction = 0, curr_wid_direction = 0; //this is to make sure hits->wid in the cluster are in the same direction, 0 means not determined.
int prev_wid = -1, curr_wid;
list<jigsaw_word_hit_t *>::iterator iter;
uint32_t curr_cluster_id = 0;
for (iter = hits->begin (); iter != hits->end(); ++iter)
{
jigsaw_word_hit_t *p = *iter;
curr_pos_t = p->pos_t;
curr_wid = abs(p->wid);
if(prev_wid != -1)
{
int wid_diff = curr_wid - prev_wid;
if ( wid_diff != 0) {curr_wid_direction = abs( wid_diff)/ wid_diff;}
else {curr_wid_direction = 2;} // 2 means the same direction
}
if (p->strand != prev_strand || curr_pos_t - prev_pos_t > 2 * max_intron_size ||
curr_wid_direction ==2 ||
(prev_wid_direction != 0 && prev_wid_direction != curr_wid_direction ) ) {
//2 times maxintron size because of possible inner exons
//TODO: after increasing this, we missed some alignments, maybe because the uniqueness issue
/*
if (curr_clust && jigsaw_spliced_aln_cluster_unique_hits (curr_clust, n_words) < (uint32_t) n_words/2)
{
if (curr_clust->hits) delete hits;
//free (curr_clust);
clusters->pop_back();
}
*/
//prev_wid_direction = 0;
curr_wid_direction = 0;
//prev_wid = -1;
//curr_wid = -1;
//create a new candidate alignment
curr_clust = (jigsaw_spliced_aln_cluster_t*) calloc (1, sizeof (jigsaw_spliced_aln_cluster_t));
curr_clust->cluster_id = curr_cluster_id++;
//initialization
//curr_clust->n_junctions = 0;
curr_clust->junctions = 0;
curr_clust->hits = new list<jigsaw_word_hit_t *>;
//add to the list
clusters->push_back (curr_clust);
}
//add the hit to the current alignment
//double the capacity of the array if necessary
curr_clust->hits->push_back (p);
prev_wid_direction = curr_wid_direction;
prev_pos_t = p->pos_t;
prev_wid = curr_wid;
prev_strand = p->strand;
}
//*n_clusters = _n_clusters;
//evaluate aln: calculate start, end and score
jigsaw_eval_spliced_aln_clusters (clusters, words, n_words, word_size, l_pac);
}
/*compare spliced alignment by score (ascending)
*/
bool jigsaw_spliced_aln_cluster_comp_score (const jigsaw_spliced_aln_cluster_t *a, const jigsaw_spliced_aln_cluster_t *b)
{
return a->score < b->score ? true : false;
}
/*compare spliced alignment by score (ascending)
*
*/
void jigsaw_sort_spliced_aln_cluster_by_score (list<jigsaw_spliced_aln_cluster_t*> *clusters)
{
//sort all clumps
clusters->sort (jigsaw_spliced_aln_cluster_comp_score);
}
/*release the memory of spliced alignment
*/
void jigsaw_destroy_word_hits (list<jigsaw_word_hit_t*> *hits)
{
list<jigsaw_word_hit_t*>::iterator iter;
for (iter = hits->begin(); iter != hits->end(); ++iter) {
jigsaw_word_hit_t *p = *iter;
free(p);
//no need to remove each hit here
}
//delete (hits);
}
void jigsaw_destroy_exons (list<jigsaw_exon_t*> *exons)
{
list<jigsaw_exon_t*>::iterator iter;
for (iter = exons->begin(); iter != exons->end(); ++iter) {
jigsaw_exon_t *p = *iter;
delete (p->hits); free(p);
//no need to remove each hit here
}
//delete (exons);
}
void jigsaw_destroy_junctions (list<jigsaw_junction_t*> *junctions)
{
list<jigsaw_junction_t*>::iterator iter;
for (iter = junctions->begin(); iter != junctions->end(); ++iter) {
jigsaw_junction_t *p = *iter;
free(p);
//no need to remove each hit here
}
//delete (junctions);
}
void jigsaw_destroy_spliced_aln_clusters (list<jigsaw_spliced_aln_cluster_t*> *clusters)
{
list<jigsaw_spliced_aln_cluster_t*>::iterator iter;
for (iter = clusters->begin(); iter != clusters->end(); ++iter) {
jigsaw_spliced_aln_cluster_t *p = *iter;
if (p->hits) delete (p->hits); //no need to delete each hit here
if (p->exons) {
jigsaw_destroy_exons (p->exons); delete (p->exons);
}
if (p->junctions) {
jigsaw_destroy_junctions (p->junctions); delete (p->junctions);
}
free (p);
}
//delete (clusters);
}
void jigsaw_destroy_spliced_aln (list<jigsaw_spliced_aln_t*> *aln)
{
list<jigsaw_spliced_aln_t*>::iterator iter;
for (iter = aln->begin(); iter != aln->end(); ++iter) {
jigsaw_spliced_aln_t *p = *iter;
if(p->junctions != NULL) delete (p->junctions);