-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGZilla.C
2080 lines (1830 loc) · 57.2 KB
/
GZilla.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
/****************************************************************
GeneZilla
Copyright (C)2015 William H. Majoros ([email protected]).
This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
License (GPL) version 3, as described at www.opensource.org.
****************************************************************/
#include <fstream>
#include "GZilla.H"
#include "BOOM/FastaReader.H"
#ifdef REPORT_PROGRESS
#include "BOOM/Progress.H"
#include "BOOM/VectorSorter.H"
#endif
GeneZilla::GeneZilla(const BOOM::String &PROGRAM_NAME,
const BOOM::String &VERSION,EdgeFactory &edgeFactory,
int &transcriptId)
: intergenicSums(0),
seq(NULL),
seqStr(NULL),
transcriptId(transcriptId),
edgeFactory(edgeFactory),
#ifdef EXPLICIT_GRAPHS
parseGraph(*this),
oneTerminusOnly(false),
#endif
PROGRAM_NAME(PROGRAM_NAME),
VERSION(VERSION),
isochores(garbageCollector),
modelCpGislands(false),
GCregex("gc=(\\d+)"),
nextIsochoreIndex(0),
evidenceFilter(NULL),
useSignalScores(true), useContentScores(true), useDurationScores(true), prohibitPTCs(true)
{
// ctor
++transcriptId;
}
GeneZilla::~GeneZilla()
{
// dtor
/*
// Delete the signal sensors
BOOM::Vector<SignalSensor*>::iterator sscur=signalSensors.begin(),
ssend=signalSensors.end();
for(; sscur!=ssend ; ++sscur)
delete *sscur;
*/
// Delete the signal queues
BOOM::Vector<SignalQueue*>::iterator sqCur=signalQueues.begin(),
sqEnd=signalQueues.end();
for(; sqCur!=sqEnd ; ++sqCur) delete *sqCur;
/*
// Delete the length distributions
BOOM::Vector<DiscreteDistribution*>::iterator dCur=distributions.begin(),
dEnd=distributions.end();
for(; dCur!=dEnd ; ++dCur)
delete *dCur;
*/
}
#ifdef EXPLICIT_GRAPHS
ParseGraph &GeneZilla::parse(const BOOM::String &fastaFilename,
const BOOM::String &isochoreFilename,
float &gcContent)
{
ParseGraph &graph=parse(fastaFilename,isochoreFilename,seq,seqStr,
gcContent);
delete seq;
delete seqStr;
return graph;
}
#endif
#ifdef EXPLICIT_GRAPHS
ParseGraph &GeneZilla::parse(const BOOM::String &fastaFilename,
const BOOM::String &isochoreFilename,
const Sequence *&substrate,
const BOOM::String *&substrateString,
float &gcContent)
{
cerr << "Loading sequence from " << fastaFilename
<< "........................" << endl;
alphabet=DnaAlphabet::global();
substrate=Sequence::load(fastaFilename,alphabet,substrateId);
substrateString=substrate->toString(alphabet);
seqLen=substrate->getLength();
cerr << "Identifying isochore..." << endl;
gcContent=getGCcontent(*substrateString);
cerr << "\t(G+C)/(A+T+C+G)=" << gcContent << endl;
cerr << "Processing config file..." << endl;
processIsochoreFile(isochoreFilename,gcContent);
cerr << "Running the GeneZilla algorithm..." << endl;
cerr << "\tsequence length=" << substrate->getLength() << endl;
buildParseGraph(*substrate,*substrateString);
#ifdef REPORT_MEMORY_USAGE
MemoryProfiler::report("GeneZilla TOTAL MEMORY USAGE: ",cerr);
#endif
return parseGraph;
}
#endif
#ifdef EXPLICIT_GRAPHS
ParseGraph &GeneZilla::parse(const BOOM::String &isochoreFilename,
const Sequence &substrate,
const BOOM::String &substrateString,
float &gcContent)
{
alphabet=DnaAlphabet::global();
seqLen=substrate.getLength();
seq=&substrate;
seqStr=&substrateString;
cerr << "Identifying isochore..." << endl;
gcContent=getGCcontent(substrateString);
cerr << "\t(G+C)/(A+T+C+G)=" << gcContent << endl;
cerr << "Processing config file..." << endl;
processIsochoreFile(isochoreFilename,gcContent);
cerr << "Running the GeneZilla algorithm..." << endl;
cerr << "\tsequence length=" << substrate.getLength() << endl;
buildParseGraph(substrate,substrateString);
#ifdef REPORT_MEMORY_USAGE
MemoryProfiler::report(
" GeneZilla TOTAL MEMORY USAGE: ",cerr);
#endif
return parseGraph;
}
#endif
#ifdef EXPLICIT_GRAPHS
void GeneZilla::buildParseGraph(const Sequence &seq,const BOOM::String &str)
{
// Instantiate one signal of each type at the left terminus to act as
// anchors to which real signals can link back
instantiateLeftTermini();
// Make a single left-to-right pass across the sequence
intergenicSums.resize(seqLen);
const char *charPtr=str.c_str();
computeIntergenicSums(seq,str,charPtr);
for(int pos=0 ; pos<seqLen ; ++pos, ++charPtr)
{
Symbol base=seq[pos];
// Check whether any signals occur here
BOOM::Vector<SignalSensor*>::iterator cur=
isochore->signalSensors.begin(),
end=isochore->signalSensors.end();
for(; cur!=end ; ++cur )
{
SignalSensor &sensor=**cur;
if(pos+sensor.getContextWindowLength()>seqLen) continue;
SignalPtr signal=sensor.detect(seq,str,pos);
#ifdef FORCE_SPECIFIC_SIGNALS
if(!signal && forcedSignalCoords.isMember
(pos+sensor.getConsensusOffset()))
signal=sensor.detectWithNoCutoff(seq,str,pos);
#endif
if(signal) {
int begin=signal->getConsensusPosition();
int end=signal->posOfBaseFollowingConsensus();
bool supported=false;
if(evidenceFilter)
switch(signal->getSignalType())
{
case ATG:
case TAG:
case NEG_ATG:
case NEG_TAG:
supported=evidenceFilter->codingSignalSupported(begin,end);
break;
case GT:
case NEG_AG:
supported=evidenceFilter->spliceOutSupported(begin);
break;
case AG:
case NEG_GT:
supported=evidenceFilter->spliceInSupported(end);
break;
}
else supported=true;
//if(signal->getSignalType()==GT || signal->getSignalType()==AG || signal->getSignalType()==NEG_GT || signal->getSignalType()==NEG_AG) cout<<"SPLICE SITE "<<signal->getConsensusPosition()<<"-"<<signal->posOfBaseFollowingConsensus()<<" "<<signal->getStrand()<<" supp="<<supported<<endl;
if(supported) {
// Find optimal predecessor for this signal in all 3 phases
linkBack(str,signal);
// Add this signal to the appropriate queue(s)
enqueue(signal);
/*if(signal->getSignalType()==NEG_AG) {
BOOM::Set<Edge*> &edges=signal->getEdgesIn();
cout<<"IN: "<<edges.size()<<" for site at "<<signal->getConsensusPosition()<<endl;
}*/
}
}
}
// Check for stop codons & terminate reading frames when they are
// found.
// This check lags behind by two bases so that any stop codon we find
// won't overlap with a signal yet to be identified
// (consider TAGT=TAG+GT; the TAG should not stop any reading frames
// for the GT because when GT is used as a donor only the TA would be
// present during translation)
if(pos>1) handleStopCodons(str,pos-2);
// Propagate scores of all non-eclipsed signals up to this base
updateAccumulators(seq,str,pos,base,*charPtr);
}
// Instantiate an anchor signal of each type at the right terminus
// and link them back, to complete the dynamic programming evaluation:
double parseScore;
BOOM::Stack<SignalPtr> *path=
instantiateRightTermini(str,seqLen,parseScore);
delete path;
// Run the garbage collector & build the parse graph
BOOM::Vector<SignalPtr>::iterator rCur=rightTermini.begin(),
rEnd=rightTermini.end();
for(; rCur!=rEnd ; ++rCur)
{
SignalPtr s=*rCur;
garbageCollector.markLeft(s);
}
BOOM::Vector<SignalPtr>::iterator lCur=leftTermini.begin(),
lEnd=leftTermini.end();
for(; lCur!=lEnd ; ++lCur)
{
SignalPtr s=*lCur;
garbageCollector.markRight(s);
}
garbageCollector.sweep();
BOOM::Set<SignalPtr>::iterator sCur=garbageCollector.signalsBegin(),
sEnd=garbageCollector.signalsEnd();
while(sCur!=sEnd)
{
SignalPtr s=*sCur;
if(!useSignalScores) s->dropSignalScores();
if(!useContentScores) s->dropContentScores();
parseGraph.addVertex(s);
++sCur;
garbageCollector.drop(s);
}
}
#endif
int GeneZilla::main(int argc,char *argv[])
{
// Process command line
BOOM::CommandLine cmd(argc,argv,"r:");
if(cmd.numArgs()!=2)
throw string(
"\ngenezilla <*.iso> <*.fasta> [-r <*.gff>]\n\
where -r <*.gff> specifies a GFF file to load and score\n\n");
BOOM::String isochoreFilename=cmd.arg(0);
BOOM::String fastaFilename=cmd.arg(1);
cerr << "Loading sequence from " << fastaFilename
<< "........................" << endl;
alphabet=DnaAlphabet::global();
BOOM::FastaReader fastaReader(fastaFilename);
BOOM::String defline, substrateString;
fastaReader.nextSequence(defline,substrateString);
Sequence substrate(substrateString,DnaAlphabet::global());
seqLen=substrate.getLength();
/*
if(cmd.option('r'))
loadGFF(cmd.optParm('r'),substrate,substrateString);
*/
cerr << "Running the GeneZilla algorithm..." << endl;
cerr << "\tsequence length=" << substrate.getLength() << endl;
ofstream osGraph;
BOOM::Stack<SignalPtr> *path=mainAlgorithm(substrate,substrateString,
osGraph,false,"");
delete path;
#ifdef REPORT_MEMORY_USAGE
MemoryProfiler::report("TOTAL MEMORY USAGE: ",cerr);
#endif
return 0;
}
BOOM::Stack<SignalPtr> * GeneZilla::processChunk(const Sequence &substrate,
const BOOM::String &substrateString,
const BOOM::String &isoFilename,
const BOOM::String &substrateId,
ostream &osGraph,
bool dumpGraph,
String psaFilename)
{
seq=&substrate;
seqStr=&substrateString;
seqLen=substrate.getLength();
this->substrateId=substrateId;
nextIsochoreInterval.begin=-1;
if(!isochoreIntervals.isDefined(substrateId))
gcContent=getGCcontent(substrateString);
else
{
BOOM::Vector<IsochoreInterval> &intervals=
isochoreIntervals[substrateId];
if(intervals.size()>0)
{
IsochoreInterval &interval=intervals[0];
gcContent=interval.GC;
nextIsochoreIndex=1;
if(intervals.size()>1)
nextIsochoreInterval=intervals[1];
else
nextIsochoreInterval.begin=-1;
}
else
gcContent=getGCcontent(substrateString);
}
cerr<<"GC content = "<<gcContent<<endl;
if(isochores.getNumIsochores()==0)
{
// This is the first chunk we are seeing...so do some initialization
// first:
cerr << "Processing config file..." << endl;
processIsochoreFile(isoFilename,gcContent);
}
else
{
// This is not the first chunk...just switch the isochore:
switchIsochore(gcContent,0);
resetAccumulatorPositions();
BOOM::Vector<SignalQueue*>::iterator qCur=signalQueues.begin(),
qEnd=signalQueues.end();
for(; qCur!=qEnd ; ++qCur)
{
SignalQueue *queue=*qCur;
queue->resetQueue(isochore);
}
}
cerr << "\tsequence length=" << seqLen << endl;
return mainAlgorithm(substrate,substrateString,osGraph,dumpGraph,
psaFilename);
}
float GeneZilla::getGCcontent(const BOOM::String &seq)
{
int n=seq.length(), ATCG=0, GC=0;
const char *p=seq.c_str();
for(int i=0 ; i<n ; ++i)
switch(*p++)
{
case 'G':
case 'C':
++GC;
// no break...fall through:
case 'A':
case 'T':
++ATCG;
}
return GC/float(ATCG);
}
void GeneZilla::instantiateLeftTermini()
{
BOOM::Vector<SignalSensor*>::iterator cur=isochore->signalSensors.begin(),
end=isochore->signalSensors.end();
for(; cur!=end ; ++cur)
{
SignalSensor &s=**cur;
BOOM::Set<ContentType> &queues=s.belongsInWhichQueues();
BOOM::Set<ContentType>::iterator cur=queues.begin(), end=queues.end();
for(; cur!=end ; ++cur)
{
ContentType contentType=*cur;
double initialTransitionScore;
switch(contentType)
{
case INITIAL_EXON:
case INTERNAL_EXON:
case FINAL_EXON:
case SINGLE_EXON:
case NEG_INITIAL_EXON:
case NEG_INTERNAL_EXON:
case NEG_FINAL_EXON:
case NEG_SINGLE_EXON:
continue; // ### force GHMM to begin in a noncoding state
case NEG_INTRON:
case INTRON:
initialTransitionScore=isochore->pI;
break;
case INTERGENIC:
initialTransitionScore=isochore->pN;
break;
case UTR5_INITIAL:
case UTR5_INTERNAL:
case UTR5_FINAL:
case UTR5_SINGLE:
case NEG_UTR5_INITIAL:
case NEG_UTR5_INTERNAL:
case NEG_UTR5_FINAL:
case NEG_UTR5_SINGLE:
initialTransitionScore=isochore->pF;
break;
case UTR3_INITIAL:
case UTR3_INTERNAL:
case UTR3_FINAL:
case UTR3_SINGLE:
case NEG_UTR3_INITIAL:
case NEG_UTR3_INTERNAL:
case NEG_UTR3_FINAL:
case NEG_UTR3_SINGLE:
initialTransitionScore=isochore->pT;
break;
}
if(initialTransitionScore==0.0) continue;
initialTransitionScore=log(initialTransitionScore);// 9/15/05
SignalPtr terminus=s.getLeftTerminus(initialTransitionScore);
SignalQueue *queue=contentToQueue[contentType];
queue->addSignal(terminus);
for(int i=0 ; i<3 ;++i) terminus->getInductiveScore(i)=0;
#ifdef EXPLICIT_GRAPHS
if(!oneTerminusOnly ||
leftTermini.isEmpty() && contentType==INTERGENIC &&
terminus->getStrand()==REVERSE_STRAND)
leftTermini.push_back(terminus);
#endif
}
}
//cout<<leftTermini.size()<<" left termini"<<endl;
}
#ifdef EXPLICIT_GRAPHS
void GeneZilla::useOneTerminusOnly()
{
oneTerminusOnly=true;
}
#endif
BOOM::Stack<SignalPtr> *GeneZilla::instantiateRightTermini(
const BOOM::String &str,
int seqLen,
double &bestScore)
{
int bestPhase;
SignalPtr bestSignal;
bestScore=NEGATIVE_INFINITY;
bestSignal=NULL;
BOOM::Set<ContentType> instantiated;
BOOM::Vector<SignalSensor*>::iterator cur=isochore->signalSensors.begin(),
end=isochore->signalSensors.end();
for(; cur!=end ; ++cur)
{
// NOTE: we consider only one content-type below because all
// propagators of the right terminus are initialized identically,
// and since no propagation will occur past the right terminus,
// they will remain identical.
SignalSensor &s=**cur;
BOOM::Set<ContentType> &queues=s.linksBackToWhichQueues();
ContentType contentType=*queues.begin();
double finalTransitionScore;
switch(contentType)
{
case INITIAL_EXON:
case INTERNAL_EXON:
case FINAL_EXON:
case SINGLE_EXON:
case NEG_INITIAL_EXON:
case NEG_INTERNAL_EXON:
case NEG_FINAL_EXON:
case NEG_SINGLE_EXON:
continue; // ### force GHMM to end in noncoding state
case NEG_INTRON:
case INTRON:
finalTransitionScore=isochore->pI;
break;
case INTERGENIC:
finalTransitionScore=isochore->pN;
break;
case UTR5_INITIAL:
case UTR5_INTERNAL:
case UTR5_FINAL:
case UTR5_SINGLE:
case NEG_UTR5_INITIAL:
case NEG_UTR5_INTERNAL:
case NEG_UTR5_FINAL:
case NEG_UTR5_SINGLE:
finalTransitionScore=isochore->pF;
break;
case UTR3_INITIAL:
case UTR3_INTERNAL:
case UTR3_FINAL:
case UTR3_SINGLE:
case NEG_UTR3_INITIAL:
case NEG_UTR3_INTERNAL:
case NEG_UTR3_FINAL:
case NEG_UTR3_SINGLE:
finalTransitionScore=isochore->pT;
break;
}
if(finalTransitionScore==0.0) continue; // ### 1/13/05
finalTransitionScore=log(finalTransitionScore);// 9/15/05
SignalPtr rightTerminus=
s.getRightTerminus(seqLen,finalTransitionScore);
linkBack(str,rightTerminus);
Propagator &prop=rightTerminus->getPropagator(contentType);
for(int i=0 ; i<3 ; ++i)
{
double thisScore=prop[i];
if(thisScore>bestScore)
{
bestScore=thisScore;
bestSignal=rightTerminus;
bestPhase=i;
}
}
#ifdef EXPLICIT_GRAPHS
if(!oneTerminusOnly ||
rightTermini.isEmpty() && contentType==INTERGENIC &&
rightTerminus->getStrand()==REVERSE_STRAND)
rightTermini.push_back(rightTerminus);
#endif
}
//cout<<rightTermini.size()<<" right termini"<<endl;
return traceBack(bestSignal,bestPhase);
}
BOOM::Stack<SignalPtr> * GeneZilla::mainAlgorithm(const Sequence &seq,
const BOOM::String &str,
ostream &osGraph,
bool dumpGraph,
String psaFilename)
{
// Compute cumulative intergenic score at each base
const char *charPtr=str.c_str();
intergenicSums.resize(seqLen);
computeIntergenicSums(seq,str,charPtr);
if(psaFilename.length()>0) {
ofstream os(psaFilename.c_str());
for(int i=0 ; i<seqLen ; ++i)
os<<intergenicSums[i]<<endl;
}
#ifndef EXPLICIT_GRAPHS
// Instantiate one signal of each type at the left terminus to act as
// anchors to which real signals can link back
instantiateLeftTermini();
#ifdef REPORT_PROGRESS
BOOM::Progress progress;
progress.start(seqLen);
#endif
// Make a single left-to-right pass across the sequence
for(int pos=0 ; pos<seqLen ; ++pos, ++charPtr)
{
if(pos==nextIsochoreInterval.begin) crossIsochoreBoundary(pos);
Symbol base=seq[pos];
// Check whether any signals occur here
BOOM::Vector<SignalSensor*>::iterator cur=
isochore->signalSensors.begin(), end=isochore->signalSensors.end();
for(; cur!=end ; ++cur )
{
SignalSensor &sensor=**cur;
if(pos+sensor.getContextWindowLength()>seqLen) continue;
#ifdef FORCE_SPECIFIC_SIGNALS
SignalPtr signal=
(forcedSignalCoords.isMember(pos+sensor.getConsensusOffset()) ?
sensor.detectWithNoCutoff(seq,str,pos) :
sensor.detect(seq,str,pos));
#else
SignalPtr signal=sensor.detect(seq,str,pos);
#endif
if(signal)
{
// Find optimal predecessor for this signal in all 3 phases
linkBack(str,signal);
// Add this signal to the appropriate queue(s)
enqueue(signal);
}
}
// Check for stop codons & terminate reading frames when they
// are found. This check lags behind by two bases so that any
// stop codon we find won't overlap with a signal yet to be
// identified (consider TAGT=TAG+GT; the TAG should not stop
// any reading frames for the GT because when GT is used as a
// donor only the TA would be present during translation)
if(pos>1) handleStopCodons(str,pos-2);
// Propagate scores of all non-eclipsed signals up to this base
updateAccumulators(seq,str,pos,base,*charPtr);
#ifdef REPORT_PROGRESS
if((pos+1)%250000==0) cerr<<progress.getProgress(pos)<<endl;
#endif
}
// Instantiate an anchor signal of each type at the right terminus
// and link them back, to complete the dynamic programming evaluation:
double parseScore;
BOOM::Stack<SignalPtr> *path=
instantiateRightTermini(str,seqLen,parseScore);
// Output gene prediction in GFF format
generateGff(path,seqLen,parseScore);
#endif
#ifdef EXPLICIT_GRAPHS
//###DEBUGGING: do the prediction using the graph instead of the "trellis"
//const char *charPtr=str.c_str();
//intergenicSums.resize(seqLen);
//computeIntergenicSums(seq,str,charPtr);
buildParseGraph(seq,str);
double parseScore;
BOOM::Stack<SignalPtr> *path=parseGraph.findOptimalPath(parseScore);
generateGff(path,seqLen,parseScore);
if(dumpGraph) {
parseGraph.setVertexIndices();
osGraph<<parseGraph<<endl;
}
#endif
#ifdef REPORT_MEMORY_USAGE
MemoryProfiler::report("GeneZilla TOTAL MEMORY USAGE: ",cerr);
#endif
return path;
}
void GeneZilla::updateAccumulators(const Sequence &seq,
const BOOM::String &str,
int pos,Symbol base,char c)
{
double score, scorePhase0, scorePhase1, scorePhase2;
BOOM::Vector<ContentSensor*>::iterator cur=
isochore->contentSensors.begin(),
end=isochore->contentSensors.end();
for(; cur!=end ; ++cur)
{
ContentSensor &contentSensor=**cur;
bool isCoding=contentSensor.isCoding();
if(isCoding)
contentSensor.scoreSingleBase(seq,str,pos,base,c,scorePhase0,
scorePhase1,scorePhase2);
else
score=contentSensor.scoreSingleBase(seq,str,pos,base,c);
BOOM::Set<SignalQueue*> &queues=contentSensor.getSignalQueues();
BOOM::Set<SignalQueue*>::iterator cur=queues.begin(),
end=queues.end();
for(; cur!= end ; ++cur)
{
SignalQueue &queue=**cur;
if(isCoding)
queue.addToAccumulator(scorePhase0,scorePhase1,scorePhase2,pos);
else
queue.addToAccumulator(score);
}
}
}
void GeneZilla::linkBack(const BOOM::String &str,SignalPtr newSignal)
{
SignalPtr bestPred[3];
bestPred[0]=bestPred[1]=bestPred[2]=NULL;
double bestScore[3];
bestScore[0]=bestScore[1]=bestScore[2]=NEGATIVE_INFINITY;
int newConsPos=newSignal->getConsensusPosition();
Strand strand=newSignal->getStrand();
SignalType signalType=newSignal->getSignalType();
if(::endsCoding(signalType)) observeRecentStopCodons(str,newSignal);
// Consider all queues that newSignal could link back to
BOOM::Set<ContentType> &queues=newSignal->linksBackToWhichQueues();
BOOM::Set<ContentType>::iterator cur=queues.begin(), end=queues.end();
for(; cur!=end ; ++cur)
{
// Make sure the queue's signals are all propagated up to this point
ContentType contentType=*cur;
SignalQueue *queue=contentToQueue[contentType];
queue->flushAccumulator();
// Consider all of the queue's signals as potential predecessors
selectPredecessors(newConsPos,*queue,contentType,strand,
bestScore,bestPred,signalType,str,newSignal);
//###DEBUGGING
/*
if(!::isCoding(contentType))
selectPredecessors(newConsPos,*queue,contentType,strand,
bestScore,bestPred,signalType,str,newSignal);
*/
//###
}
// Install the selected predecessors as dyn. prog. links and update
// inductive scores for all propagators of this signal (identically)
BOOM::Set<ContentType> &nextQueues=newSignal->belongsInWhichQueues();
cur=nextQueues.begin();
end=nextQueues.end();
for(; cur!=end ; ++cur)
{
ContentType contentType=*cur;
Propagator &prop=newSignal->getPropagator(contentType);
prop[0]+=bestScore[0];
prop[1]+=bestScore[1];
prop[2]+=bestScore[2];
// At this point the propagators are all the same, so if we overwrite
// one set of inductive scores with those for another content type, it
// won't matter, because they are the same:
newSignal->getInductiveScore(0)=prop[0];
newSignal->getInductiveScore(1)=prop[1];
newSignal->getInductiveScore(2)=prop[2];
}
newSignal->setPredecessor(0,bestPred[0]);
newSignal->setPredecessor(1,bestPred[1]);
newSignal->setPredecessor(2,bestPred[2]);
}
inline void GeneZilla::selectPredecessors(int newConsPos,
SignalQueue &queue,
ContentType contentType,
Strand strand,
double bestScore[3],
SignalPtr bestPred[3],
SignalType toType,
const BOOM::String &substrate,
SignalPtr signal)
{
switch(contentType)
{
case UTR5_INITIAL:
case UTR5_INTERNAL:
case UTR5_FINAL:
case UTR5_SINGLE:
case NEG_UTR5_INITIAL:
case NEG_UTR5_INTERNAL:
case NEG_UTR5_FINAL:
case NEG_UTR5_SINGLE:
case UTR3_INITIAL:
case UTR3_INTERNAL:
case UTR3_FINAL:
case UTR3_SINGLE:
case NEG_UTR3_INITIAL:
case NEG_UTR3_INTERNAL:
case NEG_UTR3_FINAL:
case NEG_UTR3_SINGLE:
case INTERGENIC:
selectIntergenicPred(newConsPos,queue,strand,bestScore,bestPred,
contentType,toType,signal);
break;
case INTRON:
case NEG_INTRON:
selectIntronPred(newConsPos,queue,strand,bestScore,bestPred,
contentType,toType,substrate,signal);
break;
default:
selectCodingPred(newConsPos,queue,strand,bestScore,bestPred,
contentType,toType,signal);
}
}
void GeneZilla::selectCodingPred(int newConsPos,
SignalQueue &queue,
Strand strand,double bestScore[3],
SignalPtr bestPred[3],
ContentType contentType,
SignalType toType,
SignalPtr signal)
{
// Version #1 of 3: CODING. Phase is translated across the exon.
BOOM::Iterator<SignalPtr> &cur=queue.begin(), &end=queue.end();
for(; cur!=end ; ++cur)
{
SignalPtr pred=*cur;
//if(signal->getSignalType()==NEG_AG && pred->getSignalType()==NEG_TAG) cout<<"CONSIDERING LINKING AG@"<<signal->getConsensusPosition()<<" back to TAG@"<<pred->getConsensusPosition()<<endl;
Propagator &predProp=pred->getPropagator(contentType);
SignalType predType=pred->getSignalType();
int oldPos=pred->posOfBaseFollowingConsensus();
int length=newConsPos-oldPos;
if(length<0) continue;
double lengthScore=useDurationScores ?
isochore->contentToDistribution[contentType]->
getLogP(length+pred->getConsensusLength()) : 0;
int frameDelta=length % 3;
#ifdef EXPLICIT_GRAPHS
double linkScores[3];
#endif
for(int oldPhase=0 ; oldPhase<3 ; ++oldPhase)
{
if(isinf(predProp[oldPhase]))
{
#ifdef EXPLICIT_GRAPHS
linkScores[oldPhase]=NEGATIVE_INFINITY;
#endif
continue;
}
int newPhase=(strand==FORWARD_STRAND ?
(oldPhase+frameDelta) % 3 :
posmod(oldPhase-frameDelta));
if(recentlyEclipsedPhases[newPhase])
{
#ifdef EXPLICIT_GRAPHS
if(prohibitPTCs)
linkScores[oldPhase]=NEGATIVE_INFINITY;
#endif
continue;
}
// Implement phase-specific intron states:
double transScore=
scoreIntronPhases(predType,toType,oldPhase,newPhase);
double predScore=
predProp[oldPhase] + lengthScore + transScore;
double &bestPredScore=bestScore[newPhase];
#ifdef EXPLICIT_GRAPHS
linkScores[oldPhase]=
predScore-pred->posteriorInductiveScore(oldPhase);
#endif
if(finite(predScore) && predScore>bestPredScore)
{
bestPredScore=predScore;
bestPred[newPhase]=pred;
}
}
#ifdef EXPLICIT_GRAPHS
//if(signal->getSignalType()==NEG_AG && pred->getSignalType()==NEG_TAG) cout<<"\t\tSCORES="<<linkScores[0]<<","<<linkScores[1]<<","<<linkScores[2]<<endl;
if(finite(linkScores[0]) || finite(linkScores[1]) ||
finite(linkScores[2]))
{
// ### NEED TO CHECK PHASE IF TAG/ATG INVOLVED
SignalType t=signal->getSignalType();
if(t==ATG || t==NEG_TAG) {
int predPhase;
switch(strand)
{
case FORWARD_STRAND:
predPhase=posmod(0-length);
case REVERSE_STRAND:
predPhase=(0+length)%3;
}
if(!isFinite(linkScores[predPhase])) continue;
linkScores[(predPhase+1)%3]=linkScores[(predPhase+2)%3]=
NEGATIVE_INFINITY;
}
Edge *edge=
edgeFactory.newPhasedEdge(linkScores[0],linkScores[1],
linkScores[2],pred,signal);
if(edge) {
pred->addEdgeOut(edge);
signal->addEdgeIn(edge);
}
//else if(signal->getSignalType()==NEG_AG && pred->getSignalType()==NEG_TAG) cout<<" EDGE NOT MADE!!"<<endl;
}
#endif
}
}
inline void GeneZilla::selectIntronPred(int newConsPos,SignalQueue &q,
Strand strand,double bestScore[3],
SignalPtr bestPred[3],
ContentType contentType,
SignalType toType,
const BOOM::String &substrate,
SignalPtr signal)
{
// Version #2 of 3: INTRONS. Phase is not translated across the intron.
// Also, we look for stop codons interrupted by the intron and close their
// open reading frames.
SignalSensor *stopCodonSensor=isochore->stopCodonSensor;
SignalSensor *negStopCodonSensor=isochore->negStopCodonSensor;
int seqLen=substrate.length();
bool phaseIsOpen[3];
for(int i=0 ; i<3 ; ++i) phaseIsOpen[i]=true;
BOOM::String nextCodingBase,nextTwoCodingBases;
if(newConsPos<seqLen-2)
{
// ### this would be faster if I didn't use STL strings....
nextCodingBase=substrate.substring(newConsPos+2,1);
nextTwoCodingBases=substrate.substring(newConsPos+2,2);
}
IntronQueue &queue=(IntronQueue&)q;
BOOM::Map<SignalPtr,int>::iterator cur=queue.beginUniq(),
end=queue.endUniq();
for(; cur!=end ; ++cur)
{
SignalPtr pred=(*cur).first;
int oldConsPos=pred->getConsensusPosition();
// Handle stop codons interrupted by the intron
if(prohibitPTCs)
if(newConsPos<seqLen-2 && oldConsPos>1)
{
BOOM::String prevCodingBase=substrate.substring(oldConsPos-1,1);
BOOM::String prevTwoCodingBases=
substrate.substring(oldConsPos-2,2);
BOOM::String codon12=prevCodingBase+nextTwoCodingBases;
BOOM::String codon21=prevTwoCodingBases+nextCodingBase;
phaseIsOpen[0]=
strand==FORWARD_STRAND ?
true :
!negStopCodonSensor->consensusOccursAt(codon21,0);
phaseIsOpen[1]=
strand==FORWARD_STRAND ?
!stopCodonSensor->consensusOccursAt(codon12,0) :
!negStopCodonSensor->consensusOccursAt(codon12,0);
phaseIsOpen[2]=
strand==FORWARD_STRAND ?
!stopCodonSensor->consensusOccursAt(codon21,0) :
true;
}
double transScore=
isochore->transitionProbs->getLogP(pred->getSignalType(),toType);
int oldPos=pred->posOfBaseFollowingConsensus();
int length=newConsPos-oldPos;
if(length<0) continue;
double lengthScore=useDurationScores ?
isochore->contentToDistribution[contentType]->
getLogP(length+pred->getConsensusLength()) : 0;
Propagator &predProp=pred->getPropagator(contentType);
#ifdef EXPLICIT_GRAPHS
double linkScores[3];
#endif
for(int phase=0 ; phase<3 ; ++phase)
{