-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFast3PMC.C
194 lines (142 loc) · 3.99 KB
/
Fast3PMC.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
/****************************************************************
Fast3PMC.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 "Fast3PMC.H"
#include <iostream>
#include "MarkovChainCompiler.H"
Fast3PMC::Fast3PMC(ThreePeriodicMarkovChain &slowModel)
: revComp(NULL)
{
ContentType contentType=slowModel.getContentType();
setContentType(contentType);
setStrand(::getStrand(contentType));
compileFrom(slowModel);
}
Fast3PMC::Fast3PMC(ThreePeriodicIMM &slowModel)
: revComp(NULL)
{
ContentType contentType=slowModel.getContentType();
setContentType(contentType);
setStrand(::getStrand(contentType));
compileFrom(slowModel);
}
Fast3PMC::Fast3PMC(ContentType contentType)
: revComp(NULL)
{
setContentType(contentType);
setStrand(::getStrand(contentType));
chains[0]=chains[1]=chains[2]=NULL;
}
Fast3PMC::Fast3PMC(const BOOM::String &filename)
: revComp(NULL)
{
load(filename);
}
Fast3PMC::Fast3PMC(BOOM::File &file)
: revComp(NULL)
{
load(file);
}
double Fast3PMC::scoreSingleBase(const Sequence &seq,
const BOOM::String &str,
int index,Symbol s,char c)
{
throw "attempt to use Fast3PMC on a non-periodic feature";
}
void Fast3PMC::scoreSingleBase(const Sequence &seq,
const BOOM::String &str,int index,
Symbol s,char c,double &scorePhase0,
double &scorePhase1,double &scorePhase2)
{
scorePhase0=chains[0]->scoreSingleBase(seq,str,index,s,c);
scorePhase1=chains[1]->scoreSingleBase(seq,str,index,s,c);
scorePhase2=chains[2]->scoreSingleBase(seq,str,index,s,c);
}
double Fast3PMC::scoreSubsequence(const Sequence &seq,
const BOOM::String &str,
int begin,int length,int phase)
{
char c;
double score=0;
int end=begin+length;
double s0, s1, s2;
for(int pos=begin ; pos<end ; ++pos)
{
scoreSingleBase(seq,str,pos,seq[pos],c,s0,s1,s2);
switch(phase)
{
case 0: score+=s0; break;
case 1: score+=s1; break;
case 2: score+=s2; break;
}
phase=(phase+1)%3;
}
return score;
}
ContentSensor *Fast3PMC::reverseComplement()
{
if(!revComp)
{
revComp=new Fast3PMC(::reverseComplement(getContentType()));
revComp->revComp=this;
for(int i=0 ; i<3 ; ++i)
revComp->chains[i]=
static_cast<FastMarkovChain*>(chains[i]->reverseComplement());
}
return revComp;
}
bool Fast3PMC::save(const BOOM::String &filename)
{
BOOM::File file(filename,"w");
return save(file);
}
bool Fast3PMC::save(ostream &os)
{
throw "Attempt to save Fast3PMC into ASCII file -- please use binary files";
}
bool Fast3PMC::save(BOOM::File &file)
{
file << "Fast3PMC" << static_cast<int>(getContentType());
for(int i=0 ; i<3 ; ++i)
if(!chains[i]->save(file)) return false;
return true;
}
void Fast3PMC::load(const BOOM::String &filename)
{
BOOM::File file(filename);
load(file);
}
void Fast3PMC::load(BOOM::File &file)
{
BOOM::String modelType;
file >> modelType;
if(modelType!="Fast3PMC")
throw BOOM::String("Attempt to load a model of type ")+modelType+
" into Fast3PMC (optimized 3-period markov chain)";
int contentType;
file >> contentType;
setContentType(static_cast<ContentType>(contentType));
setStrand(::getStrand(static_cast<ContentType>(contentType)));
for(int i=0 ; i<3 ; ++i)
chains[i]=new FastMarkovChain(file);
}
void Fast3PMC::reset(const Sequence &seq,const BOOM::String &str,int pos)
{
for(int i=0 ; i<3 ; ++i)
chains[i]->reset(seq,str,pos);
}
void Fast3PMC::compileFrom(ThreePeriodicMarkovChain &slowModel)
{
int order=slowModel.getOrder();
for(int i=0 ; i<3 ; ++i)
chains[i]=MarkovChainCompiler::compile(*slowModel.chains[i]);
}
void Fast3PMC::compileFrom(ThreePeriodicIMM &slowModel)
{
int order=slowModel.getOrder();
for(int i=0 ; i<3 ; ++i)
chains[i]=MarkovChainCompiler::compile(*slowModel.chains[i]);
}