-
Notifications
You must be signed in to change notification settings - Fork 3
/
node.c
executable file
·1600 lines (1485 loc) · 59 KB
/
node.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
/*******************************************************************************
PRODIGAL (PROkaryotic DynamIc Programming Genefinding ALgorithm)
Copyright (C) 2007-2016 University of Tennessee / UT-Battelle
Code Author: Doug Hyatt
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
#include "node.h"
/*******************************************************************************
Adds nodes to the node list. Genes must be >=90bp in length, unless they
run off the edge, in which case they only have to be 50bp.
*******************************************************************************/
int add_nodes(unsigned char *seq, unsigned char *rseq, int slen, struct _node
*nodes, int closed, mask *mlist, int nm, struct _training
*tinf) {
int i, nn = 0, last[3], saw_start[3], min_dist[3];
int slmod = 0;
/* Forward strand nodes */
slmod = slen%3;
for(i = 0; i < 3; i++) {
last[(i+slmod)%3] = slen+i;
saw_start[i%3] = 0;
min_dist[i%3] = MIN_EDGE_GENE;
if(closed == 0) while(last[(i+slmod)%3]+2 > slen-1) last[(i+slmod)%3]-=3;
}
for(i = slen-3; i >= 0; i--) {
if(is_stop(seq, i, tinf)==1) {
if(saw_start[i%3] == 1) {
if(is_stop(seq, last[i%3], tinf) == 0) nodes[nn].edge = 1;
nodes[nn].ndx = last[i%3];
nodes[nn].type = STOP;
nodes[nn].strand = 1;
nodes[nn++].stop_val = i;
}
min_dist[i%3] = MIN_GENE;
last[i%3]=i;
saw_start[i%3] = 0;
continue;
}
if(last[i%3] >= slen) continue;
if(is_start(seq, i, tinf) == 1 && is_atg(seq, i)==1 && ((last[i%3]-i+3)
>= min_dist[i%3]) && cross_mask(i, last[i%3], mlist, nm) == 0) {
nodes[nn].ndx = i;
nodes[nn].type = ATG;
saw_start[i%3] = 1;
nodes[nn].stop_val = last[i%3];
nodes[nn++].strand = 1;
}
else if(is_start(seq, i, tinf) == 1 && is_gtg(seq, i)==1 && ((last[i%3]-i+3)
>= min_dist[i%3]) && cross_mask(i, last[i%3], mlist, nm) == 0) {
nodes[nn].ndx = i;
nodes[nn].type = GTG;
saw_start[i%3] = 1;
nodes[nn].stop_val = last[i%3];
nodes[nn++].strand = 1;
}
else if(is_start(seq, i, tinf) == 1 && is_ttg(seq, i)==1 && ((last[i%3]-i+3)
>= min_dist[i%3]) && cross_mask(i, last[i%3], mlist, nm) == 0) {
nodes[nn].ndx = i;
nodes[nn].type = TTG;
saw_start[i%3] = 1;
nodes[nn].stop_val = last[i%3];
nodes[nn++].strand = 1;
}
else if(i <= 2 && closed == 0 && ((last[i%3]-i) > MIN_EDGE_GENE) &&
cross_mask(i, last[i%3], mlist, nm) == 0) {
nodes[nn].ndx = i;
nodes[nn].type = ATG;
saw_start[i%3] = 1;
nodes[nn].edge = 1;
nodes[nn].stop_val = last[i%3];
nodes[nn++].strand = 1;
}
}
for(i = 0; i < 3; i++) {
if(saw_start[i%3] == 1) {
if(is_stop(seq, last[i%3], tinf) == 0) nodes[nn].edge = 1;
nodes[nn].ndx = last[i%3];
nodes[nn].type = STOP;
nodes[nn].strand = 1;
nodes[nn++].stop_val = i-6;
}
}
/* Reverse strand nodes */
for(i = 0; i < 3; i++) {
last[(i+slmod)%3] = slen+i;
saw_start[i%3] = 0;
min_dist[i%3] = MIN_EDGE_GENE;
if(closed == 0) while(last[(i+slmod)%3]+2 > slen-1) last[(i+slmod)%3]-=3;
}
for(i = slen-3; i >= 0; i--) {
if(is_stop(rseq, i, tinf)==1) {
if(saw_start[i%3] == 1) {
if(is_stop(rseq, last[i%3], tinf) == 0) nodes[nn].edge = 1;
nodes[nn].ndx = slen-last[i%3]-1;
nodes[nn].type = STOP;
nodes[nn].strand = -1;
nodes[nn++].stop_val = slen-i-1;
}
min_dist[i%3] = MIN_GENE;
last[i%3]=i;
saw_start[i%3] = 0;
continue;
}
if(last[i%3] >= slen) continue;
if(is_start(rseq, i, tinf) == 1 && is_atg(rseq, i)==1 && ((last[i%3]-i+3)
>= min_dist[i%3]) && cross_mask(slen-last[i%3]-1, slen-i-1, mlist, nm) ==
0) {
nodes[nn].ndx = slen - i - 1;
nodes[nn].type = ATG;
saw_start[i%3] = 1;
nodes[nn].stop_val = slen-last[i%3]-1;
nodes[nn++].strand = -1;
}
else if(is_start(rseq, i, tinf) == 1 && is_gtg(rseq, i)==1 &&
((last[i%3]-i+3) >= min_dist[i%3]) && cross_mask(slen-last[i%3]-1,
slen-i-1, mlist, nm) == 0) {
nodes[nn].ndx = slen - i - 1;
nodes[nn].type = GTG;
saw_start[i%3] = 1;
nodes[nn].stop_val = slen-last[i%3]-1;
nodes[nn++].strand = -1;
}
else if(is_start(rseq, i, tinf) == 1 && is_ttg(rseq, i)==1 &&
((last[i%3]-i+3) >= min_dist[i%3]) && cross_mask(slen-last[i%3]-1,
slen-i-1, mlist, nm) == 0) {
nodes[nn].ndx = slen - i - 1;
nodes[nn].type = TTG;
saw_start[i%3] = 1;
nodes[nn].stop_val = slen-last[i%3]-1;
nodes[nn++].strand = -1;
}
else if(i <= 2 && closed == 0 && ((last[i%3]-i) > MIN_EDGE_GENE) &&
cross_mask(slen-last[i%3]-1, slen-i-1, mlist, nm) == 0) {
nodes[nn].ndx = slen - i - 1;
nodes[nn].type = ATG;
saw_start[i%3] = 1;
nodes[nn].edge = 1;
nodes[nn].stop_val = slen-last[i%3]-1;
nodes[nn++].strand = -1;
}
}
for(i = 0; i < 3; i++) {
if(saw_start[i%3] == 1) {
if(is_stop(rseq, last[i%3], tinf) == 0) nodes[nn].edge = 1;
nodes[nn].ndx = slen - last[i%3] - 1;
nodes[nn].type = STOP;
nodes[nn].strand = -1;
nodes[nn++].stop_val = slen-i+5;
}
}
return nn;
}
/* Simple routine to zero out the node scores */
void reset_node_scores(struct _node *nod, int nn) {
int i, j;
for(i = 0; i < nn; i++) {
for(j = 0; j < 3; j++) {
nod[i].star_ptr[j] = 0;
nod[i].gc_score[j] = 0.0;
}
for(j = 0; j < 2; j++) nod[i].rbs[j] = 0;
nod[i].score = 0.0;
nod[i].cscore = 0.0;
nod[i].sscore = 0.0;
nod[i].rscore = 0.0;
nod[i].tscore = 0.0;
nod[i].uscore = 0.0;
nod[i].traceb = -1;
nod[i].tracef = -1;
nod[i].ov_mark = -1;
nod[i].elim = 0;
nod[i].gc_bias = 0;
memset(&nod[i].mot, 0, sizeof(struct _motif));
}
}
/*******************************************************************************
Since dynamic programming can't go 'backwards', we have to record
information about overlapping genes in order to build the models. So, for
example, in cases like 5'->3', 5'-3' overlapping on the same strand, we
record information about the 2nd 5' end under the first 3' end's
information. For every stop, we calculate and store all the best starts
that could be used in genes that overlap that 3' end.
*******************************************************************************/
void record_overlapping_starts(struct _node *nod, int nn, struct _training
*tinf, int flag) {
int i, j;
double max_sc;
for(i = 0; i < nn; i++) {
for(j = 0; j < 3; j++) nod[i].star_ptr[j] = -1;
if(nod[i].type != STOP || nod[i].edge == 1) continue;
if(nod[i].strand == 1) {
max_sc = -100;
for(j = i+3; j >= 0; j--) {
if(j >= nn || nod[j].ndx > nod[i].ndx+2) continue;
if(nod[j].ndx + MAX_SAM_OVLP < nod[i].ndx) break;
if(nod[j].strand == 1 && nod[j].type != STOP) {
if(nod[j].stop_val <= nod[i].ndx) continue;
if(flag == 0 && nod[i].star_ptr[(nod[j].ndx)%3] == -1)
nod[i].star_ptr[(nod[j].ndx)%3] = j;
else if(flag == 1 && (nod[j].cscore + nod[j].sscore +
intergenic_mod(&nod[i], &nod[j], tinf) > max_sc)) {
nod[i].star_ptr[(nod[j].ndx)%3] = j;
max_sc = nod[j].cscore + nod[j].sscore +
intergenic_mod(&nod[i], &nod[j], tinf);
}
}
}
}
else {
max_sc = -100;
for(j = i-3; j < nn; j++) {
if(j < 0 || nod[j].ndx < nod[i].ndx-2) continue;
if(nod[j].ndx - MAX_SAM_OVLP > nod[i].ndx) break;
if(nod[j].strand == -1 && nod[j].type != STOP) {
if(nod[j].stop_val >= nod[i].ndx) continue;
if(flag == 0 && nod[i].star_ptr[(nod[j].ndx)%3] == -1)
nod[i].star_ptr[(nod[j].ndx)%3] = j;
else if(flag == 1 && (nod[j].cscore + nod[j].sscore +
intergenic_mod(&nod[j], &nod[i], tinf) > max_sc)) {
nod[i].star_ptr[(nod[j].ndx)%3] = j;
max_sc = nod[j].cscore + nod[j].sscore +
intergenic_mod(&nod[j], &nod[i], tinf);
}
}
}
}
}
}
/*******************************************************************************
This routine goes through all the ORFs and counts the relative frequency of
the most common frame for G+C content. In high GC genomes, this tends to be
the third position. In low GC genomes, this tends to be the first position.
Genes will be selected as a training set based on the nature of this bias
for this particular organism.
*******************************************************************************/
void record_gc_bias(int *gc, struct _node *nod, int nn, struct _training
*tinf) {
int i, j, ctr[3][3], last[3], frmod, fr, mfr, len;
double tot = 0.0;
if(nn == 0) return;
for(i = 0; i < 3; i++) for(j = 0; j < 3; j++) ctr[i][j] = 0;
for(i = nn-1; i >= 0; i--) {
fr = (nod[i].ndx)%3; frmod = 3 - fr;
if(nod[i].strand == 1 && nod[i].type == STOP) {
for(j = 0; j < 3; j++) ctr[fr][j] = 0;
last[fr] = nod[i].ndx;
ctr[fr][(gc[nod[i].ndx] + frmod)%3] = 1;
}
else if(nod[i].strand == 1) {
for(j = last[fr]-3; j >= nod[i].ndx; j-=3) ctr[fr][(gc[j] + frmod)%3] ++;
mfr = max_fr(ctr[fr][0], ctr[fr][1], ctr[fr][2]);
nod[i].gc_bias = mfr;
for(j = 0; j < 3; j++) {
nod[i].gc_score[j] = (3.0*ctr[fr][j]);
nod[i].gc_score[j] /= (1.0*(nod[i].stop_val - nod[i].ndx + 3));
}
last[fr] = nod[i].ndx;
}
}
for(i = 0; i < nn; i++) {
fr = (nod[i].ndx)%3; frmod = fr;
if(nod[i].strand == -1 && nod[i].type == STOP) {
for(j = 0; j < 3; j++) ctr[fr][j] = 0;
last[fr] = nod[i].ndx;
ctr[fr][((3-gc[nod[i].ndx]) + frmod)%3] = 1;
}
else if(nod[i].strand == -1) {
for(j = last[fr]+3; j <= nod[i].ndx; j+=3)
ctr[fr][((3-gc[j]) + frmod)%3]++;
mfr = max_fr(ctr[fr][0], ctr[fr][1], ctr[fr][2]);
nod[i].gc_bias = mfr;
for(j = 0; j < 3; j++) {
nod[i].gc_score[j] = (3.0*ctr[fr][j]);
nod[i].gc_score[j] /= (1.0*(nod[i].ndx - nod[i].stop_val + 3));
}
last[fr] = nod[i].ndx;
}
}
for(i = 0; i < 3; i++) tinf->bias[i] = 0.0;
for(i = 0; i < nn; i++) {
if(nod[i].type != STOP) {
len = abs(nod[i].stop_val-nod[i].ndx)+1;
tinf->bias[nod[i].gc_bias]+= (nod[i].gc_score[nod[i].gc_bias]*len)/1000.0;
}
}
tot = tinf->bias[0] + tinf->bias[1] + tinf->bias[2];
for(i = 0; i < 3; i++) tinf->bias[i] *= (3.0/tot);
}
/*******************************************************************************
Simple routine that calculates the dicodon frequency in genes and in the
background, and then stores the log likelihood of each 6-mer relative to the
background.
*******************************************************************************/
void calc_dicodon_gene(struct _training *tinf, unsigned char *seq, unsigned
char *rseq, int slen, struct _node *nod, int dbeg) {
int i, path, counts[4096], glob = 0;
int left, right, in_gene;
double prob[4096], bg[4096];
for(i = 0; i < 4096; i++) { counts[i] = 0; prob[i] = 0.0; bg[i] = 0.0; }
left = -1; right = -1;
calc_mer_bg(6, seq, rseq, slen, bg);
path = dbeg; in_gene = 0;
while(path != -1) {
if(nod[path].strand == -1 && nod[path].type != STOP) {
in_gene = -1;
left = slen-nod[path].ndx-1;
}
if(nod[path].strand == 1 && nod[path].type == STOP) {
in_gene = 1;
right = nod[path].ndx+2;
}
if(in_gene == -1 && nod[path].strand == -1 && nod[path].type == STOP) {
right = slen-nod[path].ndx+1;
for(i = left; i < right-5; i+=3) {
counts[mer_ndx(6, rseq, i)]++;
glob++;
}
in_gene = 0;
}
if(in_gene == 1 && nod[path].strand == 1 && nod[path].type != STOP) {
left = nod[path].ndx;
for(i = left; i < right-5; i+=3) { counts[mer_ndx(6, seq, i)]++; glob++; }
in_gene = 0;
}
path = nod[path].traceb;
}
for(i = 0; i < 4096; i++) {
prob[i] = (counts[i]*1.0)/(glob*1.0);
if(prob[i] == 0 && bg[i] != 0) tinf->gene_dc[i] = -5.0;
else if(bg[i] == 0) tinf->gene_dc[i] = 0.0;
else tinf->gene_dc[i] = log(prob[i]/bg[i]);
if(tinf->gene_dc[i] > 5.0) tinf->gene_dc[i] = 5.0;
if(tinf->gene_dc[i] < -5.0) tinf->gene_dc[i] = -5.0;
}
}
/*******************************************************************************
Scoring function for all the start nodes. This score has two factors: (1)
Coding, which is a composite of coding score and length, and (2) Start
score, which is a composite of RBS score and ATG/TTG/GTG.
*******************************************************************************/
void score_nodes(unsigned char *seq, unsigned char *rseq, int slen,
struct _node *nod, int nn, struct _training *tinf,
int closed, int is_meta) {
int i, j;
double negf, posf, rbs1, rbs2, sd_score, edge_gene, min_meta_len;
/* Step 1: Calculate raw coding potential for every start-stop pair. */
calc_orf_gc(seq, rseq, slen, nod, nn, tinf);
raw_coding_score(seq, rseq, slen, nod, nn, tinf);
/* Step 2: Calculate raw RBS Scores for every start node. */
if(tinf->uses_sd == 1) rbs_score(seq, rseq, slen, nod, nn, tinf);
else {
for(i = 0; i < nn; i++) {
if(nod[i].type == STOP || nod[i].edge == 1) continue;
find_best_upstream_motif(tinf, seq, rseq, slen, &nod[i], 2);
}
}
/* Step 3: Score the start nodes */
for(i = 0; i < nn; i++) {
if(nod[i].type == STOP) continue;
/* Does this gene run off the edge? */
edge_gene = 0;
if(nod[i].edge == 1) edge_gene++;
if((nod[i].strand == 1 && is_stop(seq, nod[i].stop_val,
tinf) == 0) || (nod[i].strand == -1 && is_stop(rseq, slen-1-
nod[i].stop_val, tinf) == 0)) edge_gene++;
/* Edge Nodes : stops with no starts, give a small bonus */
if(nod[i].edge == 1) {
nod[i].tscore = EDGE_BONUS*tinf->st_wt/edge_gene;
nod[i].uscore = 0.0;
nod[i].rscore = 0.0;
}
else {
/* Type Score */
nod[i].tscore = tinf->type_wt[nod[i].type] * tinf->st_wt;
/* RBS Motif Score */
rbs1 = tinf->rbs_wt[nod[i].rbs[0]];
rbs2 = tinf->rbs_wt[nod[i].rbs[1]];
sd_score = dmax(rbs1, rbs2) * tinf->st_wt;
if(tinf->uses_sd == 1) nod[i].rscore = sd_score;
else {
nod[i].rscore = tinf->st_wt*nod[i].mot.score;
if(nod[i].rscore < sd_score && tinf->no_mot > -0.5)
nod[i].rscore = sd_score;
}
/* Upstream Score */
if(nod[i].strand == 1)
score_upstream_composition(seq, slen, &nod[i], tinf);
else score_upstream_composition(rseq, slen, &nod[i], tinf);
/****************************************************************
** Penalize upstream score if choosing this start would stop **
** the gene from running off the edge. **
****************************************************************/
if(closed == 0 && nod[i].ndx <= 2 && nod[i].strand == 1)
nod[i].uscore += EDGE_UPS*tinf->st_wt;
else if(closed == 0 && nod[i].ndx >= slen-3 && nod[i].strand == -1)
nod[i].uscore += EDGE_UPS*tinf->st_wt;
else if(i < 500 && nod[i].strand == 1) {
for(j = i-1; j >= 0; j--)
if(nod[j].edge == 1 && nod[i].stop_val == nod[j].stop_val) {
nod[i].uscore += EDGE_UPS*tinf->st_wt;
break;
}
}
else if(i >= nn-500 && nod[i].strand == -1) {
for(j = i+1; j < nn; j++)
if(nod[j].edge == 1 && nod[i].stop_val == nod[j].stop_val) {
nod[i].uscore += EDGE_UPS*tinf->st_wt;
break;
}
}
}
/* Convert starts at base 1 and slen to edge genes if closed = 0 */
if(((nod[i].ndx <= 2 && nod[i].strand == 1) || (nod[i].ndx >= slen-3 &&
nod[i].strand == -1)) && nod[i].edge == 0 && closed == 0) {
edge_gene++;
nod[i].edge = 1;
nod[i].tscore = 0.0;
nod[i].uscore = EDGE_BONUS*tinf->st_wt/edge_gene;
nod[i].rscore = 0.0;
}
/* Penalize starts with no stop codon */
if(nod[i].edge == 0 && edge_gene == 1)
nod[i].uscore -= 0.5*EDGE_BONUS*tinf->st_wt;
/* Penalize non-edge genes < 250bp */
if(edge_gene == 0 && abs(nod[i].ndx-nod[i].stop_val) < 250) {
negf = 250.0/(float)abs(nod[i].ndx-nod[i].stop_val);
posf = (float)abs(nod[i].ndx-nod[i].stop_val)/250.0;
if(nod[i].rscore < 0) nod[i].rscore *= negf;
if(nod[i].uscore < 0) nod[i].uscore *= negf;
if(nod[i].tscore < 0) nod[i].tscore *= negf;
if(nod[i].rscore > 0) nod[i].rscore *= posf;
if(nod[i].uscore > 0) nod[i].uscore *= posf;
if(nod[i].tscore > 0) nod[i].tscore *= posf;
}
/**************************************************************/
/* Coding Penalization in Metagenomic Fragments: Internal */
/* genes must have a score of 5.0 and be >= 120bp. High GC */
/* genes are also penalized. */
/**************************************************************/
if(is_meta == 1 && slen < 3000 && edge_gene == 0 &&
(nod[i].cscore < 5.0 || abs(nod[i].ndx-nod[i].stop_val) < 120)) {
nod[i].cscore -= META_PEN*dmax(0, (3000-slen)/2700.0);
}
/* Base Start Score */
nod[i].sscore = nod[i].tscore + nod[i].rscore + nod[i].uscore;
/**************************************************************/
/* Penalize starts if coding is negative. Larger penalty for */
/* edge genes, since the start is offset by a smaller amount */
/* of coding than normal. */
/**************************************************************/
if(nod[i].cscore < 0.0) {
if(edge_gene > 0 && nod[i].edge == 0) {
if(is_meta == 0 || slen > 1500) nod[i].sscore -= tinf->st_wt;
else nod[i].sscore -= (10.31 - 0.004*slen);
}
else if(is_meta == 1 && slen < 3000 && nod[i].edge == 1) {
min_meta_len = sqrt(slen)*5.0;
if(abs(nod[i].ndx-nod[i].stop_val) >= min_meta_len) {
if(nod[i].cscore >= 0) nod[i].cscore = -1.0;
nod[i].sscore = 0.0;
nod[i].uscore = 0.0;
}
}
else nod[i].sscore -= 0.5;
}
else if(nod[i].cscore < 5.0 && is_meta == 1 && abs(nod[i].ndx-
nod[i].stop_val) < 120 && nod[i].sscore < 0.0)
nod[i].sscore -= tinf->st_wt;
}
}
/* Calculate the GC Content for each start-stop pair */
void calc_orf_gc(unsigned char *seq, unsigned char *rseq, int slen, struct
_node *nod, int nn, struct _training *tinf) {
int i, j, last[3], fr;
double gc[3], gsize = 0.0;
/* Go through each start-stop pair and calculate the %GC of the gene */
for(i = 0; i < 3; i++) gc[i] = 0.0;
for(i = nn-1; i >= 0; i--) {
fr = (nod[i].ndx)%3;
if(nod[i].strand == 1 && nod[i].type == STOP) {
last[fr] = nod[i].ndx;
gc[fr] = is_gc(seq, nod[i].ndx) + is_gc(seq, nod[i].ndx+1) +
is_gc(seq, nod[i].ndx+2);
}
else if(nod[i].strand == 1) {
for(j = last[fr]-3; j >= nod[i].ndx; j-=3)
gc[fr] += is_gc(seq, j) + is_gc(seq, j+1) + is_gc(seq, j+2);
gsize = (float)(abs(nod[i].stop_val-nod[i].ndx)+3.0);
nod[i].gc_cont = gc[fr]/gsize;
last[fr] = nod[i].ndx;
}
}
for(i = 0; i < 3; i++) gc[i] = 0.0;
for(i = 0; i < nn; i++) {
fr = (nod[i].ndx)%3;
if(nod[i].strand == -1 && nod[i].type == STOP) {
last[fr] = nod[i].ndx;
gc[fr] = is_gc(seq, nod[i].ndx) + is_gc(seq, nod[i].ndx-1) +
is_gc(seq, nod[i].ndx-2);
}
else if(nod[i].strand == -1) {
for(j = last[fr]+1; j <= nod[i].ndx; j+=3)
gc[fr] += is_gc(seq, j) + is_gc(seq, j+1) + is_gc(seq, j+2);
gsize = (float)(abs(nod[i].stop_val-nod[i].ndx)+3.0);
nod[i].gc_cont = gc[fr]/gsize;
last[fr] = nod[i].ndx;
}
}
}
/*******************************************************************************
Score each candidate's coding. We also sharpen coding/noncoding thresholds
to prevent choosing interior starts when there is strong coding continuing
upstream.
*******************************************************************************/
void raw_coding_score(unsigned char *seq, unsigned char *rseq, int slen, struct
_node *nod, int nn, struct _training *tinf) {
int i, j, last[3], fr;
double score[3], lfac, no_stop, gsize = 0.0;
if(tinf->trans_table != 11) { /* TGA or TAG is not a stop */
no_stop = ((1-tinf->gc)*(1-tinf->gc)*tinf->gc)/8.0;
no_stop += ((1-tinf->gc)*(1-tinf->gc)*(1-tinf->gc))/8.0;
no_stop = (1 - no_stop);
}
else {
no_stop = ((1-tinf->gc)*(1-tinf->gc)*tinf->gc)/4.0;
no_stop += ((1-tinf->gc)*(1-tinf->gc)*(1-tinf->gc))/8.0;
no_stop = (1 - no_stop);
}
/* Initial Pass: Score coding potential (start->stop) */
for(i = 0; i < 3; i++) score[i] = 0.0;
for(i = nn-1; i >= 0; i--) {
fr = (nod[i].ndx)%3;
if(nod[i].strand == 1 && nod[i].type == STOP) {
last[fr] = nod[i].ndx;
score[fr] = 0.0;
}
else if(nod[i].strand == 1) {
for(j = last[fr]-3; j >= nod[i].ndx; j-=3)
score[fr] += tinf->gene_dc[mer_ndx(6, seq, j)];
nod[i].cscore = score[fr];
last[fr] = nod[i].ndx;
}
}
for(i = 0; i < 3; i++) score[i] = 0.0;
for(i = 0; i < nn; i++) {
fr = (nod[i].ndx)%3;
if(nod[i].strand == -1 && nod[i].type == STOP) {
last[fr] = nod[i].ndx;
score[fr] = 0.0;
}
else if(nod[i].strand == -1) {
for(j = last[fr]+3; j <= nod[i].ndx; j+=3)
score[fr] += tinf->gene_dc[mer_ndx(6, rseq, slen-j-1)];
nod[i].cscore = score[fr];
last[fr] = nod[i].ndx;
}
}
/* Second Pass: Penalize start nodes with ascending coding to their left */
for(i = 0; i < 3; i++) score[i] = -10000.0;
for(i = 0; i < nn; i++) {
fr = (nod[i].ndx)%3;
if(nod[i].strand == 1 && nod[i].type == STOP) score[fr] = -10000.0;
else if(nod[i].strand == 1) {
if(nod[i].cscore > score[fr]) score[fr] = nod[i].cscore;
else nod[i].cscore -= (score[fr] - nod[i].cscore);
}
}
for(i = 0; i < 3; i++) score[i] = -10000.0;
for(i = nn-1; i >= 0; i--) {
fr = (nod[i].ndx)%3;
if(nod[i].strand == -1 && nod[i].type == STOP) score[fr] = -10000.0;
else if(nod[i].strand == -1) {
if(nod[i].cscore > score[fr]) score[fr] = nod[i].cscore;
else nod[i].cscore -= (score[fr] - nod[i].cscore);
}
}
/* Third Pass: Add length-based factor to the score */
/* Penalize start nodes based on length to their left */
for(i = 0; i < nn; i++) {
fr = (nod[i].ndx)%3;
if(nod[i].strand == 1 && nod[i].type == STOP) score[fr] = -10000.0;
else if(nod[i].strand == 1) {
gsize = ((float)(abs(nod[i].stop_val-nod[i].ndx)+3.0))/3.0;
if(gsize > 1000.0) {
lfac = log((1-pow(no_stop, 1000.0))/pow(no_stop, 1000.0));
lfac -= log((1-pow(no_stop, 80))/pow(no_stop, 80));
lfac *= (gsize - 80) / 920.0;
}
else {
lfac = log((1-pow(no_stop, gsize))/pow(no_stop, gsize));
lfac -= log((1-pow(no_stop, 80))/pow(no_stop, 80));
}
if(lfac > score[fr]) score[fr] = lfac;
else lfac -= dmax(dmin(score[fr] - lfac, lfac), 0);
if(lfac > 3.0 && nod[i].cscore < 0.5*lfac) nod[i].cscore = 0.5*lfac;
nod[i].cscore += lfac;
}
}
for(i = nn-1; i >= 0; i--) {
fr = (nod[i].ndx)%3;
if(nod[i].strand == -1 && nod[i].type == STOP) score[fr] = -10000.0;
else if(nod[i].strand == -1) {
gsize = ((float)(abs(nod[i].stop_val-nod[i].ndx)+3.0))/3.0;
if(gsize > 1000.0) {
lfac = log((1-pow(no_stop, 1000.0))/pow(no_stop, 1000.0));
lfac -= log((1-pow(no_stop, 80))/pow(no_stop, 80));
lfac *= (gsize - 80) / 920.0;
}
else {
lfac = log((1-pow(no_stop, gsize))/pow(no_stop, gsize));
lfac -= log((1-pow(no_stop, 80))/pow(no_stop, 80));
}
if(lfac > score[fr]) score[fr] = lfac;
else lfac -= dmax(dmin(score[fr] - lfac, lfac), 0);
if(lfac > 3.0 && nod[i].cscore < 0.5*lfac) nod[i].cscore = 0.5*lfac;
nod[i].cscore += lfac;
}
}
}
/*******************************************************************************
Examines the results of the SD motif search to determine if this organism
uses an SD motif or not. Some motif of 3-6bp has to be good or we set
uses_sd to 0, which will cause Prodigal to run the non-SD motif finder for
starts.
*******************************************************************************/
void determine_sd_usage(struct _training *tinf) {
tinf->uses_sd = 1;
if(tinf->rbs_wt[0] >= 0.0) tinf->uses_sd = 0;
if(tinf->rbs_wt[16] < 1.0 && tinf->rbs_wt[13] < 1.0 && tinf->rbs_wt[15] < 1.0
&& (tinf->rbs_wt[0] >= -0.5 || (tinf->rbs_wt[22] < 2.0 && tinf->rbs_wt[24]
< 2.0 && tinf->rbs_wt[27] < 2.0)))
tinf->uses_sd = 0;
}
/*******************************************************************************
RBS Scoring Function: Calculate the RBS motif and then multiply it by the
appropriate weight for that motif (determined in the start training
function).
*******************************************************************************/
void rbs_score(unsigned char *seq, unsigned char *rseq, int slen, struct _node
*nod, int nn, struct _training *tinf) {
int i, j;
int cur_sc[2];
/* Scan all starts looking for RBS's */
for(i = 0; i < nn; i++) {
if(nod[i].type == STOP || nod[i].edge == 1) continue;
nod[i].rbs[0] = 0;
nod[i].rbs[1] = 0;
if(nod[i].strand == 1) {
for(j = nod[i].ndx - 20; j <= nod[i].ndx - 6; j++) {
cur_sc[0] = shine_dalgarno_exact(seq, j, nod[i].ndx, tinf->rbs_wt);
cur_sc[1] = shine_dalgarno_mm(seq, j, nod[i].ndx, tinf->rbs_wt);
if(cur_sc[0] > nod[i].rbs[0]) nod[i].rbs[0] = cur_sc[0];
if(cur_sc[1] > nod[i].rbs[1]) nod[i].rbs[1] = cur_sc[1];
}
}
else if(nod[i].strand == -1) {
for(j = slen - nod[i].ndx - 21; j <= slen - nod[i].ndx - 7; j++) {
cur_sc[0] = shine_dalgarno_exact(rseq, j, slen-1-nod[i].ndx,
tinf->rbs_wt);
cur_sc[1] = shine_dalgarno_mm(rseq, j, slen-1-nod[i].ndx,
tinf->rbs_wt);
if(cur_sc[0] > nod[i].rbs[0]) nod[i].rbs[0] = cur_sc[0];
if(cur_sc[1] > nod[i].rbs[1]) nod[i].rbs[1] = cur_sc[1];
}
}
}
}
/*******************************************************************************
Iterative Algorithm to train starts. It begins with all the highest coding
starts in the model, scans for RBS/ATG-GTG-TTG usage, then starts moving
starts around attempting to match these discoveries. This start trainer is
for Shine-Dalgarno motifs only.
*******************************************************************************/
void train_starts_sd(unsigned char *seq, unsigned char *rseq, int slen,
struct _node *nod, int nn, struct _training *tinf) {
int i, j, fr, rbs[3], type[3], bndx[3], max_rb;
double sum, wt, rbg[28], rreal[28], best[3], sthresh = 35.0;
double tbg[3], treal[3];
wt = tinf->st_wt;
for(j = 0; j < 3; j++) tinf->type_wt[j] = 0.0;
for(j = 0; j < 28; j++) tinf->rbs_wt[j] = 0.0;
for(i = 0; i < 32; i++) for(j = 0; j < 4; j++) tinf->ups_comp[i][j] = 0.0;
/* Build the background of random types */
for(i = 0; i < 3; i++) tbg[i] = 0.0;
for(i = 0; i < nn; i++) {
if(nod[i].type == STOP) continue;
tbg[nod[i].type] += 1.0;
}
sum = 0.0;
for(i = 0; i < 3; i++) sum += tbg[i];
for(i = 0; i < 3; i++) tbg[i] /= sum;
/* Iterate 10 times through the list of nodes */
/* Converge upon optimal weights for ATG vs GTG vs TTG and RBS motifs */
/* (convergence typically takes 4-5 iterations, but we run a few */
/* extra to be safe) */
for(i = 0; i < 10; i++) {
/* Recalculate the RBS motif background */
for(j = 0; j < 28; j++) rbg[j] = 0.0;
for(j = 0; j < nn; j++) {
if(nod[j].type == STOP || nod[j].edge == 1) continue;
if(tinf->rbs_wt[nod[j].rbs[0]] > tinf->rbs_wt[nod[j].rbs[1]]+1.0 ||
nod[j].rbs[1] == 0)
max_rb = nod[j].rbs[0];
else if(tinf->rbs_wt[nod[j].rbs[0]] < tinf->rbs_wt[nod[j].rbs[1]]-1.0 ||
nod[j].rbs[0] == 0)
max_rb = nod[j].rbs[1];
else max_rb = (int)dmax(nod[j].rbs[0], nod[j].rbs[1]);
rbg[max_rb] += 1.0;
}
sum = 0.0;
for(j = 0; j < 28; j++) sum += rbg[j];
for(j = 0; j < 28; j++) rbg[j] /= sum;
for(j = 0; j < 28; j++) rreal[j] = 0.0;
for(j = 0; j < 3; j++) treal[j] = 0.0;
/* Forward strand pass */
for(j = 0; j < 3; j++) {
best[j] = 0.0; bndx[j] = -1; rbs[j] = 0; type[j] = 0;
}
for(j = 0; j < nn; j++) {
if(nod[j].type != STOP && nod[j].edge == 1) continue;
fr = (nod[j].ndx)%3;
if(nod[j].type == STOP && nod[j].strand == 1) {
if(best[fr] >= sthresh && nod[bndx[fr]].ndx%3 == fr) {
rreal[rbs[fr]] += 1.0;
treal[type[fr]] += 1.0;
if(i == 9) count_upstream_composition(seq, slen, 1,
nod[bndx[fr]].ndx, tinf);
}
best[fr] = 0.0; bndx[fr] = -1; rbs[fr] = 0; type[fr] = 0;
}
else if(nod[j].strand == 1) {
if(tinf->rbs_wt[nod[j].rbs[0]] > tinf->rbs_wt[nod[j].rbs[1]]+1.0 ||
nod[j].rbs[1] == 0)
max_rb = nod[j].rbs[0];
else if(tinf->rbs_wt[nod[j].rbs[0]] < tinf->rbs_wt[nod[j].rbs[1]]-1.0 ||
nod[j].rbs[0] == 0)
max_rb = nod[j].rbs[1];
else max_rb = (int)dmax(nod[j].rbs[0], nod[j].rbs[1]);
if(nod[j].cscore + wt*tinf->rbs_wt[max_rb] +
wt*tinf->type_wt[nod[j].type] >= best[fr]) {
best[fr] = nod[j].cscore + wt*tinf->rbs_wt[max_rb];
best[fr] += wt*tinf->type_wt[nod[j].type];
bndx[fr] = j;
type[fr] = nod[j].type;
rbs[fr] = max_rb;
}
}
}
/* Reverse strand pass */
for(j = 0; j < 3; j++) {
best[j] = 0.0; bndx[j] = -1; rbs[j] = 0; type[j] = 0;
}
for(j = nn-1; j >= 0; j--) {
if(nod[j].type != STOP && nod[j].edge == 1) continue;
fr = (nod[j].ndx)%3;
if(nod[j].type == STOP && nod[j].strand == -1) {
if(best[fr] >= sthresh && nod[bndx[fr]].ndx%3 == fr) {
rreal[rbs[fr]] += 1.0;
treal[type[fr]] += 1.0;
if(i == 9) count_upstream_composition(rseq, slen, -1,
nod[bndx[fr]].ndx, tinf);
}
best[fr] = 0.0; bndx[fr] = -1; rbs[fr] = 0; type[fr] = 0;
}
else if(nod[j].strand == -1) {
if(tinf->rbs_wt[nod[j].rbs[0]] > tinf->rbs_wt[nod[j].rbs[1]]+1.0 ||
nod[j].rbs[1] == 0)
max_rb = nod[j].rbs[0];
else if(tinf->rbs_wt[nod[j].rbs[0]] < tinf->rbs_wt[nod[j].rbs[1]]-1.0 ||
nod[j].rbs[0] == 0)
max_rb = nod[j].rbs[1];
else max_rb = (int)dmax(nod[j].rbs[0], nod[j].rbs[1]);
if(nod[j].cscore + wt*tinf->rbs_wt[max_rb] +
wt*tinf->type_wt[nod[j].type] >= best[fr]) {
best[fr] = nod[j].cscore + wt*tinf->rbs_wt[max_rb];
best[fr] += wt*tinf->type_wt[nod[j].type];
bndx[fr] = j;
type[fr] = nod[j].type;
rbs[fr] = max_rb;
}
}
}
sum = 0.0;
for(j = 0; j < 28; j++) sum += rreal[j];
if(sum == 0.0) for(j = 0; j < 28; j++) tinf->rbs_wt[j] = 0.0;
else {
for(j = 0; j < 28; j++) {
rreal[j] /= sum;
if(rbg[j] != 0) tinf->rbs_wt[j] = log(rreal[j]/rbg[j]);
else tinf->rbs_wt[j] = -4.0;
if(tinf->rbs_wt[j] > 4.0) tinf->rbs_wt[j] = 4.0;
if(tinf->rbs_wt[j] < -4.0) tinf->rbs_wt[j] = -4.0;
}
}
sum = 0.0;
for(j = 0; j < 3; j++) sum += treal[j];
if(sum == 0.0) for(j = 0; j < 3; j++) tinf->type_wt[j] = 0.0;
else {
for(j = 0; j < 3; j++) {
treal[j] /= sum;
if(tbg[j] != 0) tinf->type_wt[j] = log(treal[j]/tbg[j]);
else tinf->type_wt[j] = -4.0;
if(tinf->type_wt[j] > 4.0) tinf->type_wt[j] = 4.0;
if(tinf->type_wt[j] < -4.0) tinf->type_wt[j] = -4.0;
}
}
if(sum <= (double)nn/2000.0) sthresh /= 2.0;
}
/* Convert upstream base composition to a log score */
for(i = 0; i < 32; i++) {
sum = 0.0;
for(j = 0; j < 4; j++) sum += tinf->ups_comp[i][j];
if(sum == 0.0) for(j = 0; j < 4; j++) tinf->ups_comp[i][j] = 0.0;
else {
for(j = 0; j < 4; j++) {
tinf->ups_comp[i][j] /= sum;
if(tinf->gc > 0.1 && tinf->gc < 0.9) {
if(j == 0 || j == 3)
tinf->ups_comp[i][j] = log(tinf->ups_comp[i][j]*2.0/(1.0-tinf->gc));
else
tinf->ups_comp[i][j] = log(tinf->ups_comp[i][j]*2.0/tinf->gc);
}
else if(tinf->gc <= 0.1) {
if(j == 0 || j == 3)
tinf->ups_comp[i][j] = log(tinf->ups_comp[i][j]*2.0/0.90);
else
tinf->ups_comp[i][j] = log(tinf->ups_comp[i][j]*2.0/0.10);
}
else {
if(j == 0 || j == 3)
tinf->ups_comp[i][j] = log(tinf->ups_comp[i][j]*2.0/0.10);
else
tinf->ups_comp[i][j] = log(tinf->ups_comp[i][j]*2.0/0.90);
}
if(tinf->ups_comp[i][j] > 4.0) tinf->ups_comp[i][j] = 4.0;
if(tinf->ups_comp[i][j] < -4.0) tinf->ups_comp[i][j] = -4.0;
}
}
}
/* Start training info: kept this in since it's useful information */
/* to print sometimes. */
/* fprintf(stderr, "\nLOG WTS\n");
for(i = 0; i < 3; i++) fprintf(stderr, "%f ", tinf->type_wt[i]);
fprintf(stderr, "\n");
for(i = 0; i < 28; i++) fprintf(stderr, "%f ", tinf->rbs_wt[i]);
fprintf(stderr, "\n\nSTART DIST: ");
for(i = 0; i < 3; i++) fprintf(stderr, "%f ", treal[i]);
fprintf(stderr, "\n");
sum = 0.0;
for(i = 0; i < 28; i++) { fprintf(stderr, "%f ", rreal[i]); sum+= rreal[i]; }
fprintf(stderr, "sum is %f\n", sum);
fprintf(stderr, "\n\nUPS COMP: ");
for(i = 0; i < 32; i++) { fprintf(stderr, "%d", i); for(j = 0; j < 4; j++) { fprintf(stderr, "\t%.2f", tinf->ups_comp[i][j]); } fprintf(stderr, "\n"); }
exit(0); */
}
/*******************************************************************************
Iterative Algorithm to train starts. It begins with all the highest coding
starts in the model, scans for RBS/ATG-GTG-TTG usage, then starts moving
starts around attempting to match these discoveries. Unlike the SD
algorithm, it allows for any popular motif to be discovered.
*******************************************************************************/
void train_starts_nonsd(unsigned char *seq, unsigned char *rseq, int slen,
struct _node *nod, int nn, struct _training *tinf) {
int i, j, k, l, fr, bndx[3], mgood[4][4][4096], stage;
double sum, ngenes, wt = tinf->st_wt, best[3], sthresh = 35.0;
double tbg[3], treal[3];
double mbg[4][4][4096], mreal[4][4][4096], zbg, zreal;
for(i = 0; i < 32; i++) for(j = 0; j < 4; j++) tinf->ups_comp[i][j] = 0.0;
/* Build the background of random types */
for(i = 0; i < 3; i++) tinf->type_wt[i] = 0.0;
for(i = 0; i < 3; i++) tbg[i] = 0.0;
for(i = 0; i < nn; i++) {
if(nod[i].type == STOP) continue;
tbg[nod[i].type] += 1.0;
}
sum = 0.0;
for(i = 0; i < 3; i++) sum += tbg[i];
for(i = 0; i < 3; i++) tbg[i] /= sum;
/* Iterate 20 times through the list of nodes */
/* Converge upon optimal weights for ATG vs GTG vs TTG and RBS motifs */
/* (convergence typically takes 4-5 iterations, but we run a few */
/* extra to be safe) */
for(i = 0; i < 20; i++) {
/* Determine which stage of motif finding we're in */
if(i < 4) stage = 0;
else if(i < 12) stage = 1;
else stage = 2;
/* Recalculate the upstream motif background and set 'real' counts to 0 */
for(j = 0; j < 4; j++) for(k = 0; k < 4; k++) for(l = 0; l < 4096; l++)
mbg[j][k][l] = 0.0;
zbg = 0.0;
for(j = 0; j < nn; j++) {
if(nod[j].type == STOP || nod[j].edge == 1) continue;
find_best_upstream_motif(tinf, seq, rseq, slen, &nod[j], stage);
update_motif_counts(mbg, &zbg, seq, rseq, slen, &(nod[j]), stage);
}
sum = 0.0;
for(j = 0; j < 4; j++) for(k = 0; k < 4; k++) for(l = 0; l < 4096; l++)
sum += mbg[j][k][l];
sum += zbg;
for(j = 0; j < 4; j++) for(k = 0; k < 4; k++) for(l = 0; l < 4096; l++)
mbg[j][k][l] /= sum;
zbg /= sum;
/* Reset counts of 'real' motifs/types to 0 */
for(j = 0; j < 4; j++) for(k = 0; k < 4; k++) for(l = 0; l < 4096; l++)
mreal[j][k][l] = 0.0;
zreal = 0.0;
for(j = 0; j < 3; j++) treal[j] = 0.0;
ngenes = 0.0;
/* Forward strand pass */
for(j = 0; j < 3; j++) { best[j] = 0.0; bndx[j] = -1; }
for(j = 0; j < nn; j++) {
if(nod[j].type != STOP && nod[j].edge == 1) continue;
fr = (nod[j].ndx)%3;
if(nod[j].type == STOP && nod[j].strand == 1) {
if(best[fr] >= sthresh) {
ngenes += 1.0;
treal[nod[bndx[fr]].type] += 1.0;
update_motif_counts(mreal, &zreal, seq, rseq, slen, &(nod[bndx[fr]]),