-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntronQueue.C
311 lines (230 loc) · 6.78 KB
/
IntronQueue.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
/****************************************************************
IntronQueue.C
Copyright (C)2013 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 "BOOM/Constants.H"
#include "IntronQueue.H"
#include <iostream>
#include "genezilla.H"
IntronQueue::IntronQueue(ContentType contentType,
int capacity,
BOOM::Array1D<SinglePhaseComparator*> &comp)
: SignalQueue(contentType)
{
for(int i=0 ; i<3 ; ++i)
lists[i]=new FixedSizePriorityList<SignalPtr>(capacity,comp[i]);
accumulator[1]=accumulator[2]=NEGATIVE_INFINITY;
}
IntronQueue::~IntronQueue()
{
delete lists[0];
delete lists[1];
delete lists[2];
// do not delete signals here! they are deleted elsewhere!
}
void IntronQueue::switchComparator(BOOM::Array1D<SinglePhaseComparator*> &cmp)
{
for(int i=0 ; i<3 ; ++i)
lists[i]->changeComparator(cmp[i]);
}
void IntronQueue::switchToIsochore(Isochore *isochore)
{
ContentType contentType=getContentType();
BOOM::Array1D<SinglePhaseComparator*> *cmp=
isochore->intronComparators[contentType];
switchComparator(*cmp);
}
void IntronQueue::resetQueue(Isochore *isochore)
{
mainQueue.clear();
holdingQueue.clear();
for(int i=0 ; i<3 ; ++i)
lists[i]->clear();
membershipCounter.clear();
ContentType contentType=getContentType();
BOOM::Array1D<SinglePhaseComparator*> *cmp=
isochore->intronComparators[contentType];
switchComparator(*cmp);
accumulator.resetScoresForNoncoding(); // [0,-inf,-inf]
}
void IntronQueue::purge(int beforePosition)
{
for(int i=0 ; i<3 ; ++i) {
FixedSizePriorityList<SignalPtr> &q=*lists[i];
for(FixedSizePriorityList<SignalPtr>::iterator cur=q.begin(), next,
end=q.end() ; cur!=end ; ) {
SignalPtr s=*cur;
next=cur; ++next;
if(s->getContextWindowEnd()<=beforePosition) {
membershipCounter.decrement(s);
mainQueue.erase(cur);
}
cur=next;
}
}
}
void IntronQueue::addSignal(SignalPtr signal)
{
Propagator &propagator=signal->getPropagator(contentType);
int propagatorPosition=propagator.getPosition();
if(propagatorPosition <= accumulator.getPosition())
// ^-- only true for the "left terminus" signals
for(int i=0 ; i<3 ; ++i)
{
lists[i]->push_back(signal);
membershipCounter.increment(signal);
}
else
holdingQueue.push_back(signal);
}
void IntronQueue::updateHoldingQueue(int position)
{
// Checks whether any signals in holdingQueue have been passed by
// the accumulator. If so, calls flushAccumulator() and then moves
// those signals into the main queue.
int accumulatorPosition=accumulator.getPosition();
// Iterate through the holding queue
bool changes=false;
BOOM::List<SignalPtr >::iterator cur=holdingQueue.begin(),
end=holdingQueue.end();
bool first=true;
while(cur!=end)
{
SignalPtr signal=*cur;
Propagator &newProp=signal->getPropagator(contentType);
if(newProp.getPosition() <= accumulatorPosition)
{
changes=true;
// This signal is ready to be shifted into the main queue.
// Make sure any signals already in the main queue are propagated
// up to this point before proceding with the promotion (so that
// inductive scores will be comparable):
if(first) {flushAccumulator();first=false;}
// Remove it from the holding queue
BOOM::List<SignalPtr >::iterator graduate=cur;
++cur;
holdingQueue.erase(graduate);
// Add it to the three phase-specific priority queues
for(int phase=0 ; phase<3 ; ++phase)
{
double newScore=newProp[phase];
if(isinf(newScore)) continue;
FixedSizePriorityList<SignalPtr > *q=lists[phase];
SignalPtr displaced;
if(q->size()==q->getCapacity()) displaced=*q->begin();
bool inserted=q->insert(signal);
//may reject it if score is too low
if(inserted)
{
membershipCounter.increment(signal);
if(displaced)
membershipCounter.decrement(displaced);
}
}
}
else ++cur;
}
}
BOOM::Iterator<SignalPtr > &IntronQueue::begin()
{
iter=IntronQueueIterator(lists,false); // false = not past-the-end
return iter;
}
BOOM::Iterator<SignalPtr > &IntronQueue::end()
{
iterEnd=IntronQueueIterator(lists,true); // true = past-the-end
return iterEnd;
}
int IntronQueue::numElements()
{
int n=0;
for(int i=0 ; i<3 ; ++i)
n+=lists[i]->size();
return n;
}
void IntronQueue::drop(BOOM::Iterator<SignalPtr > &victim)
{
SignalPtr signal=*victim;
membershipCounter.decrement(signal);
IntronQueueIterator &iqi=static_cast<IntronQueueIterator&>(victim);
lists[iqi.getIndex()]->erase(iqi.getNativeIterator());
}
void IntronQueue::flushAccumulator()
{
// Adds accumulator contents to propagators of all signals in the
// main queue and then resets accumulator scores to 0.
// Update the signal propagators:
Strand strand=getStrand();
BOOM::Map<SignalPtr ,int>::iterator cur=membershipCounter.begin(),
end=membershipCounter.end();
for(; cur!=end ; ++cur)
{
SignalPtr signal=(*cur).first;
Propagator &propagator=signal->getPropagator(contentType);
propagator.update(accumulator,strand,false,signal);
}
// Reset accumulator scores:
accumulator.resetScoresForNoncoding(); // [0,-inf,-inf]
}
//==============================================================
// IntronQueueIterator methods
//==============================================================
IntronQueueIterator::IntronQueueIterator(FixedSizePriorityList<SignalPtr >
*lists[3],
bool pastTheEnd)
{
if(pastTheEnd)
{
index=2;
iterators[2]=lists[2]->end();
}
else
{
index=0;
for(int i=0 ; i<3 ; ++i)
{
iterators[i]=lists[i]->begin();
ends[i]=lists[i]->end();
}
}
}
void IntronQueueIterator::operator++()
{
increment();
}
void IntronQueueIterator::increment()
{
FixedSizePriorityList<SignalPtr>::iterator &iter=iterators[index];
++iter;
if(iter==ends[index] && index<2) ++index;
}
void IntronQueueIterator::operator++(int)
{
increment();
}
SignalPtr &IntronQueueIterator::operator*()
{
return *iterators[index];
}
bool IntronQueueIterator::operator==(const BOOM::Iterator<SignalPtr> &other) const
{
const IntronQueueIterator &iqiOther=
dynamic_cast<const IntronQueueIterator&>(other);
return iterators[index]==iqiOther.iterators[index];
}
FixedSizePriorityList<SignalPtr>::iterator
IntronQueueIterator::getNativeIterator()
const
{
return iterators[index];
}
int IntronQueueIterator::getIndex() const
{
return index;
}
BOOM::Iterator<SignalPtr> &IntronQueueIterator::clone()
{
return *new IntronQueueIterator(*this);
}