-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMarkovChainCompiler.C
196 lines (172 loc) · 4.83 KB
/
MarkovChainCompiler.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
/****************************************************************
MarkovChainCompiler.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 "MarkovChainCompiler.H"
#include <math.h>
#include <iostream>
#include "BOOM/Alphabet.H"
#include "NthOrderStringIterator.H"
extern Alphabet alphabet;
const double LOG_OF_1=0.0;
FastMarkovChain *MarkovChainCompiler::compile(ContentSensor &mc)
{
FastMarkovChain *fmc=compileForward(mc);
if(mc.getContentType()!=INTERGENIC)
fmc->revComp=static_cast<FastMarkovChain*>
(compileReverse(*mc.reverseComplement()));
return fmc;
}
FastMarkovChain *MarkovChainCompiler::compileForward(ContentSensor &mc)
{
int order=mc.getOrder();
FastMarkovChain *fmc=new FastMarkovChain(order,mc.getContentType());
int alphabetSize=alphabet.getNumElements();
// ===========================
// Install state probabilities
// ===========================
Sequence seq;
Symbol s;
fmc->setProb(0,LOG_OF_1);
int state=1;
for(int i=0 ; i<=order ; ++i)
{
NthOrderStringIterator stateGen(i+1,alphabet);
while(!stateGen.done())
{
BOOM::String stateString=stateGen.getNextString();
char c=stateString[i];
double score=mc.scoreSingleBase(seq,stateString,i,s,c);
fmc->setProb(state,score);
++state;
}
}
// ===================
// Install transitions
// ===================
for(int i=0 ; i<alphabetSize ; ++i)
fmc->setTrans(0,i,i+1); // 0 is the initial state
state=1;
for(int i=0 ; i<=order ; ++i)
{
NthOrderStringIterator stateGen(i+1,alphabet);
while(!stateGen.done())
{
BOOM::String stateString=stateGen.getNextString();
BOOM::String history=
(stateString.length()<=order ?
stateString : // at beginning of substrate; no drop necessary
stateString.substring(1,order));// drop one off to make room
for(Symbol s=0 ; s<alphabetSize ; ++s)
{
BOOM::String nextStateString=history+alphabet.lookup(s);
int nextState=stringToState(nextStateString,*fmc);
fmc->setTrans(state,s,nextState);
}
++state;
}
}
return fmc;
}
FastMarkovChain *MarkovChainCompiler::compileReverse(ContentSensor &mc)
{
int order=mc.getOrder();
FastMarkovChain *fmc=new FastMarkovChain(order,mc.getContentType());
int alphabetSize=alphabet.getNumElements();
// ===========================
// Install state probabilities
// ===========================
Sequence seq;//dummy
Symbol s;//dummy
char c;//dummy
fmc->setProb(0,LOG_OF_1);
int state=1;
for(int i=0 ; i<=order ; ++i)
{
NthOrderStringIterator stateGen(i+1,alphabet);
while(!stateGen.done())
{
BOOM::String stateString=stateGen.getNextString();
double score=mc.scoreSingleBase(seq,stateString,0,s,c);
fmc->setProb(state,score);
++state;
}
}
// ===================
// Install transitions
// ===================
for(int i=0 ; i<alphabetSize ; ++i)
fmc->setTrans(0,i,0); // 0 is the final state, can never leave it
state=1;
for(int i=0 ; i<=order ; ++i)
{
NthOrderStringIterator stateGen(i+1,alphabet);
while(!stateGen.done())
{
BOOM::String stateString=stateGen.getNextString();
if(stateString.length()>order)
{
BOOM::String history=
stateString.substring(1,order);// drop one off to make room
for(Symbol s=0 ; s<alphabetSize ; ++s)
{
BOOM::String nextStateString=history+alphabet.lookup(s);
int nextState=stringToState(nextStateString,*fmc);
fmc->setTrans(state,s,nextState);
}
}
++state;
}
}
return fmc;
}
int MarkovChainCompiler::stringToState(const BOOM::String &s,
FastMarkovChain &fmc)
{
int len=s.length();
int state=1;
for(int i=0 ; i<len-1 ; ++i)
state+=fmc.statesOfOrder(i);
int alphabetSize=alphabet.getNumElements();
int factor=1;
for(int i=0 ; i<len ; ++i)
{
state+=(alphabet.lookup(s[len-i-1]) * factor);
factor*=alphabetSize;
}
return state;
}
BOOM::String MarkovChainCompiler::stateToString(int state,
FastMarkovChain &fmc)
{
int alphabetSize=alphabet.getNumElements();
int order=0;
int firstStateOfOrder=1;
for(; 1 ; ++order)
{
if(state<firstStateOfOrder) break;
firstStateOfOrder+=fmc.statesOfOrder(order);
}
--order;
firstStateOfOrder=1;
for(int i=0 ; i<order ; ++i)
firstStateOfOrder+=fmc.statesOfOrder(i);
state-=firstStateOfOrder;
BOOM::String str;
for(int i=0 ; i<=order ; ++i)
{
int factor=static_cast<int>(pow((float)alphabetSize,order-i));
for(int s=alphabetSize-1 ; s>=0 ; --s)
{
if(state>=s*factor)
{
str+=alphabet.lookup(s);
state-=s*factor;
break;
}
}
}
return str;
}