Codebase list augustus / debian/3.4.0+dfsg2-1 src / ncmodel.cc
debian/3.4.0+dfsg2-1

Tree @debian/3.4.0+dfsg2-1 (Download .tar.gz)

ncmodel.cc @debian/3.4.0+dfsg2-1raw · history · blame

  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
/*
 * ncmodel.cc
 *
 * License: Artistic License, see file LICENSE.TXT or 
 *          https://opensource.org/licenses/artistic-license-1.0
 */

#include "ncmodel.hh"

// project includes
#include "gene.hh"
#include "projectio.hh"
#include "motif.hh"
#include "intronmodel.hh" // so splice sites models can be reused here
#include "utrmodel.hh"    // so TSS/TTS signal models can be reused here
#include "exonmodel.hh"   // so coding exon length distributions can be reused here
#include "extrinsicinfo.hh"
#include "merkmal.hh"

#include <climits>

/*
 * Initialization of static data members
 */
Integer         NcModel::nccount = 0;
vector<Double>  NcModel::lenDistInternal;     // length distribution of non-single exons
vector<Double>  NcModel::lenDistSingle;       // length distribution of single exons
Boolean         NcModel::hasLenDist = false;
Boolean         NcModel::initAlgorithmsCalled = false;
Boolean         NcModel::haveSnippetProbs = false;
SegProbs*       NcModel::segProbs = NULL;
int             NcModel::boundSpacing = 10; // without hints 5' and 3' transcript end only every ttsSpacing bases, for speed
vector<Double>  NcModel::ttsProbPlus;
vector<Double>  NcModel::ttsProbMinus;
vector<Double>  NcModel::tssProbPlus;
vector<Double>  NcModel::tssProbMinus;

/*
 * NcModel constructor
 */
NcModel::NcModel() {
    nctype = toStateType( Properties::getProperty("/NcModel/type", nccount++) );
    strand = isOnFStrand(nctype)? plusstrand : minusstrand;
}

/*
 * destructor
 */
NcModel::~NcModel( ){
    if (--nccount == 0){
	if (segProbs)
	    delete segProbs;
    }
}

/*
 * NcModel initialization of class variables
 */
void NcModel::init() {
    try {
	if (!Properties::getBoolProperty(NONCODING_KEY)) // not run with --nc=On
	    return;
    } catch(ProjectError &e) {
	return;
    }
}

/*
 * registerPars
 */
void NcModel::registerPars (Parameters* parameters){
}

/*
 * readAllParameters
 */
void NcModel::readAllParameters(){
}

/*
 * buildModel
 */
void NcModel::buildModel( const AnnoSequence* annoseq, int parIndex){
}

/*
 * printProbabilities
 */
void NcModel::printProbabilities( int idx, BaseCount *bc, const char* suffix ){
}

/*
 * NcModel::initSnippetProbs
 */
void NcModel::initSnippetProbs() {
    if (segProbs)
	delete segProbs;
    segProbs = new SegProbs(sequence, IntronModel::k);
    haveSnippetProbs = true;
}

/*
 * NcModel::initAlgorithms
 *
 * makes a correction on the transition matrix "trans" and the vector of ancestors
 * this is called after initViterbiAlgorithms
 */
void NcModel::initAlgorithms( Matrix<Double>& trans, int cur){
    if (nctype == ncintron || nctype == rncintron)
	pIntron = trans[cur][cur].doubleValue();
    else 
	pIntron = 0.999;

    if (!initAlgorithmsCalled) {
	precomputeTxEndProbs();
	computeLengthDistributions();
    }
    eop.clear();
    initAlgorithmsCalled = true;
}

/*
 * NcModel::updateToLocalGC
 */
void NcModel::updateToLocalGC(int from, int to){
   segProbs->setEmiProbs(&IntronModel::emiprobs.probs, from, to);
}

/*
 * computeLengthDistributions
 * use a parametric distribution (negative binomial)
 */
void NcModel::computeLengthDistributions(){
    double mean = 200;
    double disp = 0.5; // > 0, the larger the dispersion, the larger the variance
    double r = 1/disp;
    double p = mean / (mean + r);
    int minSingleExonLen = 11; // a more or less random lower bound
    // number of successes until r failures occur, success prob is p
    // variance = mean + dispersion * mean^2
    // use recursive formular for NB-distribution to fill in the table
    lenDistSingle.resize(Constant::max_exon_len+1, 0);
    lenDistSingle[0] = pow(1-p, r);
    for (int k=1; k <= Constant::max_exon_len; k++)
	lenDistSingle[k] = lenDistSingle[k-1] * p * (k+r-1) / k;
    lenDistInternal = ExonModel::lenDistInternal; // was: = lenDistSingle; causes problems in distinguishing coding from nc
    for (int k=0; k < minSingleExonLen; k++)
	lenDistSingle[k] = 0;
}

/*
 * NcModel::viterbiForwardAndSampling
 */
void NcModel::viterbiForwardAndSampling( ViterbiMatrixType& viterbi,
					 ViterbiMatrixType& forward,
					 int state,
					 int base,
					 AlgorithmVariant algovar,
					 OptionListItem& oli) {
  /* 
   *             | [begin signal]|                        | [end signal] |
   *  pred.State |TSS            | markov chain           | TTS          |
   *              ASS                                       DSS
   *              rTTS                                      rTSS
   *              rDSS                                      rASS
   *  --------------------------------------------------------------------------
   *  predProb   |         notEndPartProb                 | endPartProb  |
   *              <--------------------- lenPartProb -------------------->
   */ 
    OptionsList *optionslist = NULL;
    Feature* extrinsicexons = NULL;
    int endOfPred, leftMostEndOfPred, rightMostEndOfPred, beginOfEndPart, endOfBioExon;
    Double maxPredProb, predProb, endPartProb, notEndPartProb;
    Double fwdsum, fwdsummand;
    Double emiProb, extrinsicQuot, transEmiProb;
    vector<Ancestor>::const_iterator it;
    maxPredProb = fwdsum = 0.0;
    extrinsicQuot = 1.0;
    if (algovar == doSampling)
	optionslist = new OptionsList();
    
    getEndPositions(base, beginOfEndPart, endOfBioExon);
    switch (nctype)
	{
	case ncsingle: case rncsingle:
	    leftMostEndOfPred = base - Constant::max_exon_len;
	    rightMostEndOfPred = base - 1;
	    break;
	case ncinit: case rncinit:
	    leftMostEndOfPred = base - (Constant::max_exon_len + DSS_MIDDLE + Constant::dss_end);
	    rightMostEndOfPred = base - Constant::dss_whole_size();
	    break;
	case ncinternal: case rncinternal:
	    leftMostEndOfPred = base - (Constant::max_exon_len + DSS_MIDDLE + Constant::dss_end + Constant::ass_upwindow_size + Constant::ass_start + ASS_MIDDLE);
	    rightMostEndOfPred = base - DSS_MIDDLE - Constant::dss_end - Constant::ass_upwindow_size - Constant::ass_start - ASS_MIDDLE - 1; // => minimum exon len = 1
	    break;
	case ncterm: case rncterm:
	    leftMostEndOfPred = base - (Constant::max_exon_len + Constant::ass_upwindow_size + Constant::ass_start + ASS_MIDDLE);
	    rightMostEndOfPred = base - Constant::ass_upwindow_size - Constant::ass_whole_size();
	    break;
	default: // 4 intron states
	    leftMostEndOfPred = base - 1;
	    rightMostEndOfPred = base - 1;
	}
    
    if (beginOfEndPart >= 0)
	endPartProb = endPartEmiProb(beginOfEndPart, base, endOfBioExon);
    else 
	endPartProb = 0;

    if (endPartProb == 0)
	return;
    
    if (nctype != ncintronvar && nctype != rncintronvar){
	if (leftMostEndOfPred < 0)
	    leftMostEndOfPred = 0;

	/*
	 * get the extrinsic exonpart information about parts falling in this range
	 */
	if (!(isNcIntron(nctype))){
	    extrinsicexons = seqFeatColl->getFeatureListOvlpingRange(A_SET_FLAG(exonF) | A_SET_FLAG(exonpartF),
								     leftMostEndOfPred, endOfBioExon, strand);
	    if (true) { // TODO option: allowOnlyExonHintedNCExons
	       // determine first hinted exon
	       int minE = rightMostEndOfPred + 1;
	       for (Feature *f = extrinsicexons; f != NULL; f = f->next)
		  if (f->start < minE)
		     minE = f->start;
	       if (minE > leftMostEndOfPred){
		  leftMostEndOfPred = minE;
		  if (leftMostEndOfPred > rightMostEndOfPred - 200){
		     leftMostEndOfPred = rightMostEndOfPred - 200;
		     if (leftMostEndOfPred < 0)
			leftMostEndOfPred = 0;
		  }
	       }
	    }
	}
	if (algovar == doBacktracking || algovar == doSampling)
	   eop.clear();
	eop.init(); // initialize iterator on list with possible endOfPreds
	
	for (endOfPred = rightMostEndOfPred; endOfPred >= leftMostEndOfPred; eop.decrement(endOfPred)){
	    const ViterbiColumnType& predVit = algovar == doSampling ? 
		(endOfPred > 0 ? forward[endOfPred] : forward[0]) :
		(endOfPred > 0 ? viterbi[endOfPred] : viterbi[0]);
	    // compute the maximum over the predecessor states probs times transition probability

	    // check whether the starting position has a positive entry at all
	    for (it = ancestor.begin(); it != ancestor.end() && predVit[it->pos]==0; ++it);
	    if (it == ancestor.end()) continue;

	    notEndPartProb = notEndPartEmiProb(endOfPred+1, beginOfEndPart-1, endOfBioExon, extrinsicexons);
	    if (notEndPartProb == 0)
	       continue;
	    try {
	       eop.update(endOfPred);
	    } catch (ProjectError &e) {
	       cerr << "Error in EOPList::update.\tbase=" << base << "\t" << stateTypeNames[nctype]
		    << "\tnotEndPartProb=" << notEndPartProb << endl;
	       throw e;
	    }

	    emiProb = notEndPartProb * endPartProb;
	    do {
		transEmiProb = it->val * emiProb;
		if ((nctype == ncintron || nctype == rncintron)
		    && (it->pos != state || endOfPred == 0)) // transitions into an intron are punished by malus
		    transEmiProb *= seqFeatColl->collection->malus(intronF);
		predProb = predVit[it->pos] * transEmiProb;
		if (needForwardTable(algovar)) { 
                    // endOfPred < 0 appears in left truncated state
		    fwdsummand = forward[endOfPred>=0? endOfPred:0].get(it->pos) * transEmiProb.heated();
		    fwdsum += fwdsummand;
		    if (algovar == doSampling && fwdsummand > 0)
			optionslist->add(it->pos, endOfPred, fwdsummand);
		} 
		if (predProb > maxPredProb) {
		    maxPredProb = predProb;
		    oli.state = it->pos;
		    oli.base = endOfPred;
		}
	    } while (++it != ancestor.end());
	}
    } else if (seqFeatColl){
	/*
	 * intron state with variable length. Only used for introns exactly matching an intron hint.
	 */
	int endOfBioIntron;
	if (nctype == ncintronvar)
	    endOfBioIntron = base + Constant::ass_upwindow_size + Constant::ass_start + ASS_MIDDLE;
	else
	    endOfBioIntron = base + Constant::dss_end + DSS_MIDDLE;

	Feature *intronList = seqFeatColl->getFeatureListAt(intronF, endOfBioIntron, 
							    isOnFStrand(nctype)? plusstrand : minusstrand);
	int oldEndOfPred = -INT_MAX;
	for (Feature *ihint = intronList; ihint != NULL; ihint = ihint->next) {
	    if (nctype == ncintronvar)
		endOfPred = ihint->start - 1 + DSS_MIDDLE + Constant::dss_end;
	    else
		endOfPred = ihint->start - 1 + Constant::ass_upwindow_size + Constant::ass_start + ASS_MIDDLE;
	    if (endOfPred < 0 ||
		ihint->end - ihint->start + 1 < Constant::ass_upwindow_size 
		+ Constant::ass_start + ASS_MIDDLE + DSS_MIDDLE + Constant::dss_end) 
		continue;
	    const ViterbiColumnType& predVit = viterbi[endOfPred];
	    emiProb = emiProbUnderModel(endOfPred+1, base);
	    if (endOfPred == oldEndOfPred)
		extrinsicQuot *= ihint->bonus;
	    else 
		extrinsicQuot = ihint->bonus;
	    emiProb *= extrinsicQuot; // option gets a bonus because it complies with the intron hint
	    for( it = ancestor.begin(); it != ancestor.end(); ++it ){
		transEmiProb = it->val * emiProb;
		predProb = predVit[it->pos] * transEmiProb;
		if (needForwardTable(algovar)) {
		    // ACHTUNG: this isn't correct in the model
		    // but the effect should be very small
		    fwdsummand = forward[endOfPred].get(it->pos) * transEmiProb.heated();
		    fwdsum += fwdsummand;
		    if (algovar==doSampling && fwdsummand > 0)
			optionslist->add(it->pos, endOfPred, fwdsummand);
		}
		if (predProb > maxPredProb) {
		    maxPredProb = predProb;
		    oli.state = it->pos;
		    oli.base = endOfPred;
		}
	    }
	    oldEndOfPred = endOfPred;
	}
    }

    switch (algovar) {
	case doSampling:
	    optionslist->prepareSampling();
	    try {
		oli = optionslist->sample();
	    } catch (ProjectError &e) {
		cerr << "Sampling error in noncoding model. state=" << state << " base=" << base << endl;
		throw e;
	    }
	    delete optionslist;
	    return;
	case doViterbiAndForward:
	    if (fwdsum > 0)
		forward[base][state] = fwdsum;
	case doViterbiOnly:
	    if (maxPredProb > 0)
		viterbi[base][state] = maxPredProb;
	    return;
	default:
	    // backtracking: do nothing here
	    return;
    }
}

/*
 * ===[ NcModel::endPartEmiProb ]=====================================
 *
 * Compute the probability that the end part ends at "end" in the test sequence.
 * Return 0 if it is impossible.   
 */
Double NcModel::endPartEmiProb(int begin, int end, int endOfBioExon) const {
    Double endPartProb = 1, extrinsicQuot = 1;
    switch (nctype) 
	{
	case ncsingle: case ncterm: 
	    endPartProb = ttsProbPlus[end]; break;
	case rncsingle: case rncinit: 
	    endPartProb = tssProbMinus[end]; break;
	case ncinit: case ncinternal:
	    endPartProb = IntronModel::dSSProb(begin, true);
	    break;
	case rncterm: case rncinternal:
	    endPartProb = IntronModel::aSSProb(begin, false);
	    break;
	case ncintronvar:
	    if (!isPossibleASS(end + Constant::ass_upwindow_size + Constant::ass_start + ASS_MIDDLE))
		endPartProb = 0.0;
	    break;
	case rncintronvar:
	    if (!isPossibleRDSS(end + Constant::dss_end + DSS_MIDDLE))
		endPartProb = 0.0;
	    break;
	default:
	    ;
    }
    
    if (endPartProb > 0.0) {
	/*
	 * dss hints
	 */
	if (nctype == ncinternal || nctype == ncinit){
	    Feature *feature = seqFeatColl->getFeatureListContaining(A_SET_FLAG(dssF), endOfBioExon+1, plusstrand);
	    if (feature)
		while (feature) {
		    extrinsicQuot *= feature->bonus;
		    feature = feature->next;
		}
	    else if (seqFeatColl->collection->hasHintsFile)
		extrinsicQuot *= seqFeatColl->collection->malus(dssF);
	}
	/*
	 * ass hints
	 */
	if (nctype == rncinternal || nctype == rncterm){
	    Feature *feature = seqFeatColl->getFeatureListContaining(A_SET_FLAG(assF), endOfBioExon+1, minusstrand);
	    if (feature)
		while (feature) {
		    extrinsicQuot *= feature->bonus;
		    feature = feature->next;
		}
	    else if (seqFeatColl->collection->hasHintsFile)
		extrinsicQuot *= seqFeatColl->collection->malus(assF);
	}

	/*
	 * intronpart bonus for the part of the intron that is handled in the exon states
	 */
	if ((nctype == ncinit || nctype == ncinternal || nctype == rncterm || nctype == ncinternal) // splice site follows
	    && endOfBioExon < end) {
	    /*
	     * an intron gets the bonus for each position covered by an intronpart hint 
	     * (counted multiply for overlapping hints)
	     */
	    Feature *part, *intronList = seqFeatColl->getFeatureListOvlpingRange(A_SET_FLAG(intronpartF) | A_SET_FLAG(nonexonpartF), endOfBioExon+1, end,
										 isOnFStrand(nctype)? plusstrand : minusstrand);
	    for (int i=endOfBioExon+1; i <= end; i++) {
		for (part = intronList; part!= NULL; part = part->next){
		    if (part->start<=i && part->end>=i){
			extrinsicQuot *= part->bonus;
		    }
		}
	    }
	}
    }
    return endPartProb * extrinsicQuot;
}

/*
 * notEndPartEmiProb(int begin, int end)
 * endOfMiddle is the position right before the downstream signal
 */
Double NcModel::notEndPartEmiProb(int begin, int endOfMiddle, int endOfBioExon, Feature *exonparts) const {
    Double beginPartProb = 1, middlePartProb = 1, lenProb = 1;
    Double extrinsicQuot = 1;
    int beginOfMiddle, beginOfBioExon=-1;
    Seq2Int s2i_intron(IntronModel::k+1);

    switch( nctype )
	{
	case ncsingle:
	    beginOfBioExon = begin;
	    beginPartProb = tssProbPlus[begin];
	    if (beginPartProb > 0) {
	       middlePartProb = segProbs->getSeqProb(begin, endOfMiddle);
	       lenProb = lenDistSingle[endOfBioExon - beginOfBioExon + 1];
	    }
	    break;
	case ncinit:
	    beginOfBioExon = begin;
	    beginPartProb = tssProbPlus[beginOfBioExon];
	    if (beginPartProb > 0) {
	       middlePartProb = segProbs->getSeqProb(begin, endOfMiddle);
	       lenProb = lenDistInternal[endOfBioExon - beginOfBioExon + 1];
	    }
	    break;
	case ncinternal:
	    beginPartProb = IntronModel::aSSProb(begin, true);
	    beginOfBioExon = begin + Constant::ass_upwindow_size + Constant::ass_start + ASS_MIDDLE;
	    if (beginPartProb > 0) {
		beginOfMiddle = begin + Constant::ass_upwindow_size + Constant::ass_whole_size();
		middlePartProb = segProbs->getSeqProb(beginOfMiddle, endOfMiddle);
		lenProb = lenDistInternal[endOfBioExon - beginOfBioExon + 1];
	    }
	    break;
	case ncterm:
	    beginOfBioExon = begin + Constant::ass_upwindow_size + Constant::ass_start + ASS_MIDDLE;	    
	    if (beginOfBioExon >= dnalen) 
		beginPartProb = 0;
	    else
		beginPartProb = IntronModel::aSSProb(begin, true);
	    if (beginPartProb > 0) {
		beginOfMiddle = begin + Constant::ass_upwindow_size + Constant::ass_whole_size();
		if (endOfMiddle - beginOfMiddle + 1 >= 0)
		    middlePartProb = segProbs->getSeqProb(beginOfMiddle, endOfMiddle);
		else {
		    middlePartProb = pow (4.0,  -(endOfMiddle - beginOfMiddle + 1));
		}
		lenProb = lenDistInternal[endOfBioExon - beginOfBioExon + 1];
	    }
	    break;
	case rncsingle:
	    beginOfBioExon = begin;
	    beginPartProb = ttsProbMinus[beginOfBioExon];
	    if (beginPartProb > 0) {
	       middlePartProb = segProbs->getSeqProb(begin, endOfMiddle);
	       lenProb = lenDistSingle[endOfBioExon - beginOfBioExon + 1];
	    }
	    break;
	case rncinternal:
	    beginPartProb = IntronModel::dSSProb(begin, false);
	    beginOfBioExon = begin + Constant::dss_end + DSS_MIDDLE;
	    if (beginPartProb > 0) {
		beginOfMiddle = begin + Constant::dss_whole_size();
		middlePartProb = segProbs->getSeqProb(beginOfMiddle, endOfMiddle);
		lenProb = lenDistInternal[endOfBioExon - beginOfBioExon + 1];
	    }
	    break;
	case rncterm:
	    beginOfBioExon = begin;
	    beginPartProb = ttsProbMinus[beginOfBioExon];
	    if (beginPartProb > 0) {
	       if (endOfMiddle - begin + 1 >= 0)
		  middlePartProb = segProbs->getSeqProb(begin, endOfMiddle);
	       else {
		  middlePartProb = pow (4.0,  -(endOfMiddle - begin + 1));
	       }
	       lenProb = lenDistInternal[endOfBioExon - beginOfBioExon + 1];
	    }
	    break;
	case rncinit:
	    beginPartProb = IntronModel::dSSProb(begin, false);
	    beginOfBioExon = begin + Constant::dss_end + DSS_MIDDLE;
	    if (beginPartProb > 0){
		beginOfMiddle = begin + Constant::dss_whole_size();
		middlePartProb = segProbs->getSeqProb(beginOfMiddle, endOfMiddle);
		lenProb = lenDistInternal[endOfBioExon - beginOfBioExon + 1];
	    }
	    break;
	case ncintron: case rncintron: // strand does not matter
	    for (int pos = begin; pos <= endOfMiddle; pos++)
		if (pos - IntronModel::k >= 0)
		    try {
			middlePartProb *= IntronModel::emiprobs.probs[s2i_intron(sequence + pos - IntronModel::k)]; 
		    } catch (InvalidNucleotideError &e) {
		       middlePartProb *= (float) 0.25;
		    }
		else
		   middlePartProb *= (float) 0.25;
	    break;
	case ncintronvar: case rncintronvar: // introns that are supported by a hint
	    {
   	        int intronLen = endOfBioExon - begin + 1;
	        if (nctype == ncintronvar)
		  intronLen += Constant::dss_end + DSS_MIDDLE;
		else 
		    intronLen += Constant::ass_upwindow_size + Constant::ass_start + ASS_MIDDLE;
		lenProb = IntronModel::lenDist[IntronModel::getD()] * pow(1.0 - 1.0/IntronModel::getMAL(), (int) (intronLen-IntronModel::getD()));
		// was before an independent distribution for nc introns:
		// int internalIntronLen = endOfMiddle - begin + 1;
		// lenProb = pow(pIntron, internalIntronLen - 1) * (1.0-pIntron);
		// it appears to be better for distinguishing coding from nc to use the same distribution
		middlePartProb = segProbs->getSeqProb(begin, endOfMiddle);
		break;
	    }
	default: ;
	}
    
    Double sequenceProb = beginPartProb * middlePartProb * lenProb;
    if (!(sequenceProb > 0))
       return sequenceProb;
    /*
     *                           extrinsicQuot
     */
    
    /*      EXON
     *
     * Multiply a bonus/malus to extrinsicQuot for every exonpart hint that is
     * covered by this biological exon
     */
    if (isExon(nctype)) {
        int numEPendingInExon=0, nep=0; // just used for malus
	bool exonFSupported = false;
	Double partBonus = 1;
	for (Feature *part = exonparts; part != NULL; part = part->next){
	    if (part->type == exonpartF){
		if (part->type == exonpartF && part->end >= beginOfBioExon && part->end <= endOfBioExon)
		    numEPendingInExon++;
		if (strand == part->strand || part->strand == STRAND_UNKNOWN){
		    if (part->start >= beginOfBioExon && part->end <= endOfBioExon) {
			partBonus *= part->bonus;
			nep += 1;// TODO add multiplicity of hint here
		    }
		}
	    }
	    if (part->type == exonF && strand == part->strand && part->start == beginOfBioExon && part->end == endOfBioExon) {
		extrinsicQuot *= part->bonus;
		exonFSupported = true;
	    }
	}
	extrinsicQuot *= partBonus;
	/*
	 * Malus computation
	 */
	if (seqFeatColl && nep >=1) { // was 5
	    int zeroCov = seqFeatColl->numZeroCov(beginOfBioExon, endOfBioExon, exonpartF, strand);
	    Double localPartMalus = seqFeatColl->collection->localPartMalus(exonpartF, zeroCov, partBonus, nep);
	    if (localPartMalus < 1.0/partBonus) // at least have ab initio probabilities
		localPartMalus = 1.0/partBonus;
	    extrinsicQuot *= localPartMalus;
	}
	if (seqFeatColl && seqFeatColl->collection->hasHintsFile) {
	    /* We have searched for extrinsic features.
	     * Then multiply the malus for each position of the exon.
	     * We should exclude those (few) positions where an exonpart ends.
	     * Exons longer than their exonpart hint have an incentive to become shorter.
	     */
	    if (endOfBioExon-beginOfBioExon + 1 - numEPendingInExon > 0)
		extrinsicQuot *= seqFeatColl->collection->partMalus(exonpartF, endOfBioExon-beginOfBioExon + 1 - numEPendingInExon);
	    if (!exonFSupported)
		extrinsicQuot *= seqFeatColl->collection->malus(exonF);
	}
	/*
	 * dss hints
	 */
	if (nctype == rncinternal || nctype == rncinit){
	    Feature *feature = seqFeatColl->getFeatureListContaining(A_SET_FLAG(dssF), beginOfBioExon-1, minusstrand);
	    if (feature)
		while (feature) {
		    extrinsicQuot *= feature->bonus;
		    feature = feature->next;
		}
	    else if (seqFeatColl->collection->hasHintsFile)
		extrinsicQuot *= seqFeatColl->collection->malus(dssF);
	}
	/*
	 * ass hints
	 */
	if (nctype == ncinternal || nctype == ncterm){
	    Feature *feature = seqFeatColl->getFeatureListContaining(A_SET_FLAG(assF), beginOfBioExon-1, plusstrand);
	    if (feature)
		while (feature) {
		    extrinsicQuot *= feature->bonus;
		    feature = feature->next;
		}
	    else if (seqFeatColl->collection->hasHintsFile)
		extrinsicQuot *= seqFeatColl->collection->malus(assF);
	}

    }

    /*
     *       INTRON
     */
    if (isIntron(nctype)) {
	/*
	 * an intron gets the bonus for each position covered by an intronpart hint 
	 * (counted multiply for overlapping hints)
	 */
	Feature *intronList = seqFeatColl->getFeatureListOvlpingRange(A_SET_FLAG(intronpartF) | A_SET_FLAG(nonexonpartF), begin, endOfMiddle, strand);
	for (int i=begin; i <= endOfMiddle; i++)
	    for (Feature *part = intronList; part!= NULL; part = part->next){
		if (part->start <= i && part->end >= i)
		    extrinsicQuot *= part->bonus;
	}
    } else if (nctype == ncinternal || nctype == ncterm || nctype == rncinternal || nctype == rncinit) {
	/*
	 * intronpart bonus for the part of the intron that is handled in the exon states
	 */
	Feature *part, *intronList = seqFeatColl->getFeatureListOvlpingRange(A_SET_FLAG(intronpartF) | A_SET_FLAG(nonexonpartF), begin, beginOfBioExon-1, strand);
	if (intronList){
	   for (int i=begin; i <= beginOfBioExon-1; i++) {
	      for (part = intronList; part != NULL; part = part->next){
		 if (part->start <= i && part->end >= i){
		    extrinsicQuot *= part->bonus;
		 }
	      }
	   } 
	}
    }
    
    return sequenceProb * extrinsicQuot;
}

// begin = endOfPred + 1
Double NcModel::emiProbUnderModel (int begin, int end) const {
    int beginOfEndPart, endOfBioExon;
    Feature *extrinsicexons = NULL;
    Double endProb, notEndProb, emiProb;

    getEndPositions(end, beginOfEndPart, endOfBioExon);

    if (!(isNcIntron(nctype)))
	extrinsicexons = seqFeatColl->getExonListInRange(begin, // to be safe for all cases
							 endOfBioExon,
							 isOnFStrand(nctype)? plusstrand : minusstrand);
    if (inCRFTraining){
	// TODO; make sure there are no old values reused
	// seqProb(-1, -1, false, -1); // forget all saved information in static variables
    }
    endProb = endPartEmiProb(beginOfEndPart, end, endOfBioExon);
    notEndProb = notEndPartEmiProb(begin, beginOfEndPart-1, endOfBioExon, extrinsicexons);
    emiProb = notEndProb * endProb;
    return emiProb;
}

// 'end' is the column in the DP table
void NcModel::getEndPositions (int end, int &beginOfEndPart, int &endOfBioExon) const {
    switch (nctype) {
    case ncsingle: case ncterm: // transcription end
	beginOfEndPart = end + 1; // no end signal
	endOfBioExon = end;
	break;
    case rncsingle: case rncinit: // transcription start
	beginOfEndPart = end + 1;
	endOfBioExon = end;
	break;
    case ncinternal: case ncinit: // donor splice site after exon
	beginOfEndPart = end - Constant::dss_whole_size() + 1;
	endOfBioExon = end - Constant::dss_end - DSS_MIDDLE;
	break;
    case rncinternal: case rncterm: // reverse acceptor splice site after exon
	beginOfEndPart = end - Constant::ass_whole_size() - Constant::ass_upwindow_size + 1;
	endOfBioExon = end - Constant::ass_upwindow_size - Constant::ass_start - ASS_MIDDLE;
	break;
    default: // an intron state: ncintron, ncintronvar, rncintron, rncintronvar
	    beginOfEndPart = end + 1;
	    endOfBioExon = end;
    }
}

/*
 * computes the probability of the emission of the sequence from left to right
 * left and right included
 *

Double NcModel::seqProb(int left, int right) const {
    if (left > right)
	return 1;
    
    return segProbs->getSeqProb(left, right);
    }*/

/*
 * Precomputes the probability of the transcription termination and initiation sites
 * on the whole sequence. Uses the tss and tts hints, spacing (% 10) for efficiency.
 * TODO: Disallow transcript ends that are nowhere near a hint: 
 * Exploit that nc genes unsupported by hints are not considered.
 */
void NcModel::precomputeTxEndProbs(){
    Feature *f;
    int pos;

    tssProbPlus.assign(dnalen+1, 0);
    tssProbMinus.assign(dnalen+1, 0);
    ttsProbPlus.assign(dnalen+1, 0);
    ttsProbMinus.assign(dnalen+1, 0);

    // allow and incentivize transcript boundaries when hinted to
    f = seqFeatColl->getAllActiveFeatures(tssF);
    while (f) {
	for (pos = f->start; pos <= f->end; pos++){
	    if (f->strand == plusstrand || f->strand == bothstrands){
		if (tssProbPlus[pos] == 0)
		    tssProbPlus[pos] = f->distance_faded_bonus(pos);
		else 
		    tssProbPlus[pos] *= f->distance_faded_bonus(pos);
	    }
	    if (f->strand == minusstrand || f->strand == bothstrands){
		if (tssProbMinus[pos] == 0)
		    tssProbMinus[pos] = f->distance_faded_bonus(pos);
		else 
		    tssProbMinus[pos] *= f->distance_faded_bonus(pos);
	    }	    
	}
	f = f->next;
    }
    f = seqFeatColl->getAllActiveFeatures(ttsF);
    while (f) {
	for (pos = f->start; pos <= f->end; pos++){
	    if (f->strand == plusstrand || f->strand == bothstrands){
		if (ttsProbPlus[pos] == 0)
		    ttsProbPlus[pos] = f->distance_faded_bonus(pos);
		else 
		    ttsProbPlus[pos] *= f->distance_faded_bonus(pos);
	    }
	    if (f->strand == minusstrand || f->strand == bothstrands){
		if (ttsProbMinus[pos] == 0)
		    ttsProbMinus[pos] = f->distance_faded_bonus(pos);
		else 
		    ttsProbMinus[pos] *= f->distance_faded_bonus(pos);
	    }	    
	}
	f = f->next;
    }

    // allow transcript boundaries every boundSpacing-th base for efficiency
    // but only near exonpart hint boundaries
    Double tssMalus = seqFeatColl->collection->malus(tssF);
    Double ttsMalus = seqFeatColl->collection->malus(ttsF);
    f = seqFeatColl->getAllActiveFeatures(exonpartF);
    vector<int> v;
    v.reserve(3);
    while (f) {
	// allow tss and tts near every exonpart boundary
	v.clear();
	v.push_back(f->start);
	v.push_back(boundSpacing * (f->start/boundSpacing));
	v.push_back(boundSpacing * (1 + f->start/boundSpacing));
	for (vector<int>::iterator p = v.begin(); p!= v.end(); ++p) {
	    if (*p >= 0 && *p <= dnalen){ // p a possible left boundary
		if (tssProbPlus[*p] == 0)
		    tssProbPlus[*p] = tssMalus;
		if (ttsProbMinus[*p] == 0)
		    ttsProbMinus[*p] = ttsMalus;
	    }
	}
	v.clear();
	v.push_back(f->end);
	v.push_back(boundSpacing * (f->end/boundSpacing));
	v.push_back(boundSpacing * (1 + f->end/boundSpacing));
	for (vector<int>::iterator p = v.begin(); p!= v.end(); ++p){
	    if (*p >= 0 && *p <= dnalen){ // p a possible right boundary
		if (tssProbMinus[*p] == 0)
		    tssProbMinus[*p] = tssMalus;
		if (ttsProbPlus[*p] == 0)
		    ttsProbPlus[*p] = ttsMalus;
	    }
	}
	f = f->next;
    }
}