File: replacementModelSSRV.cpp

package info (click to toggle)
fastml 3.11-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,772 kB
  • sloc: cpp: 48,522; perl: 3,588; ansic: 819; makefile: 386; python: 83; sh: 55
file content (198 lines) | stat: -rw-r--r-- 4,865 bytes parent folder | download | duplicates (5)
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
// 	$Id: replacementModelSSRV.cpp 4165 2008-06-04 09:19:48Z osnatz $	

#include "replacementModelSSRV.h"
#include "logFile.h"
#include <iomanip>
#include <iostream>


replacementModelSSRV::replacementModelSSRV(const distribution* dist, const replacementModel* baseRM, MDOUBLE rateOfRate /*= 1 */) :
_dist(dist->clone()),
_baseRM(baseRM->clone()),
_rateOfRate(rateOfRate)
{
	if (_dist->categories() == 0)
		errorMsg::reportError("replacementModelSSRV::replacementModelSSRV : number of categories == 0");
	
	updateFreq();
	updateQ();

	
}

//// similar to goldmanYangModel.cpp 
//replacementModelSSRV::replacementModelSSRV(const replacementModelSSRV& other) :
//_dist(other._dist->clone()),
//_baseRM(other._baseRM->clone()),
//_rateOfRate(other._rateOfRate)
//{
//	int size = alphabetSize();
//	_Q.resize(size);
//	for (int z=0; z < _Q.size();++z) 
//		_Q[z].resize(size,0);
//	updateFreq();
//	updateQ();
//}

// Instead of calling updateQ here, like in goldmanYangModel.cpp,
// this method uses the copy constructor of q2pt and also copies _freq and _Q
replacementModelSSRV::replacementModelSSRV(const replacementModelSSRV& other) :
_dist(other._dist->clone()),
_baseRM(other._baseRM->clone()),	
_rateOfRate(other._rateOfRate),
_q2pt(other._q2pt),
_freq(other._freq),
_Q(other._Q)
{
}

replacementModelSSRV::~replacementModelSSRV()
{
	if (_dist) delete (_dist);
	if (_baseRM) delete (_baseRM);
}


replacementModelSSRV& replacementModelSSRV::operator=(const replacementModelSSRV &other)
{
	if (_dist) delete (_dist);
	if (_baseRM) delete (_baseRM);
	
	_dist = other._dist->clone();
	_baseRM = other._baseRM->clone();	
	_rateOfRate = other._rateOfRate;
	_q2pt = other._q2pt; //@@@@ why doesn't this work ? explicit ?
//	_q2pt.fillFromRateMatrix(other._freq,other._Q);
	_freq = other._freq;
	_Q = other._Q;

	return (*this);
}

const int replacementModelSSRV::alphabetSize() const
{
	return (_baseRM->alphabetSize() * _dist->categories());
}



// The freq of each mulCharacter is its freq in the _baseRM * the freq of the rate-category
void replacementModelSSRV::updateFreq()	
{
	_freq.clear();
	int size = alphabetSize();
	int numCategories = _dist->categories();
	_freq.resize(size);
	int idInCategory;
		
	for(idInCategory=0; idInCategory < _baseRM->alphabetSize() ; ++idInCategory)
	{
		for (int categoryNumber=0; categoryNumber < numCategories; ++categoryNumber)
			_freq[categoryNumber*_baseRM->alphabetSize() + idInCategory] = 
				_baseRM->freq(idInCategory) * _dist->ratesProb(categoryNumber);
	}
}


void replacementModelSSRV::updateQ()
{
	if (_rateOfRate < EPSILON) _rateOfRate = EPSILON; // Temporary - to overcome a bug in QL algorithm, when _rateOfRate == 0

	_Q.clear();
	int size = alphabetSize();
	_Q.resize(size);
	for (int z=0; z < _Q.size();++z) 
		_Q[z].resize(size,0.0);
	
	// fill Q
	int _BaseRM_alphabetSize = _baseRM->alphabetSize();
	int numCategories = _dist->categories();
	// i,j : go over all the base-alphabet.
	// z,w : go over all the categories.
	for (int i=0; i < _BaseRM_alphabetSize; ++i)
	{
		for (int j=0; j < _BaseRM_alphabetSize; ++j)
		{
			for (int z=0; z < numCategories; ++z)
			{
				for (int w=0; w < numCategories; ++w)
				{
					if (i!=j)
					{
						// different alphabet, same rate category
						if (z==w)
							_Q[z*_BaseRM_alphabetSize + i][z*_BaseRM_alphabetSize+j] 
							= _dist->rates(z) * _baseRM->dPij_dt(i,j,0);
					}
					else
					{
						// same alphabet, different rate category
						if (z!=w)
						{
							_Q[z*_BaseRM_alphabetSize+i][w*_BaseRM_alphabetSize+i] =  _rateOfRate * _dist->ratesProb(w);
						}
						// same alphabet, same rate category
						else
							_Q[z*_BaseRM_alphabetSize+i][z*_BaseRM_alphabetSize+i] = 
								_dist->rates(z) * _baseRM->dPij_dt(i,j,0) 
								- ( _rateOfRate * (1.0 - _dist->ratesProb(z)));
					}

				}
			}
		}
	}
	
//	// check OZ
//	LOG(4, <<"THE Q MATRIX IS: "<<endl ) ;
//	VVdouble::iterator itr1 = _Q.begin();
//	Vdouble::iterator itr2;
//	for (; itr1 != _Q.end(); ++itr1)
//	{
//		for (itr2 = itr1->begin(); itr2 != itr1->end(); ++itr2)
//			LOG(4,<< setprecision(3) <<  setw(5) << *itr2 <<'\t');
//		LOG(4,<<endl);
//	}
//	LOG (4,<<endl);
////	 end of check

	_q2pt.fillFromRateMatrix(_freq,_Q); 
	
}

void replacementModelSSRV::setDistribution(const distribution* dist)
 {
	 if (dist->categories() == 0)
		errorMsg::reportError("replacementModelSSRV::setDistribution : number of categories == 0");
	 if (_dist) delete (_dist);
		_dist=dist->clone();
	updateQ();
 }

MDOUBLE replacementModelSSRV::sumPijQij() const{
	MDOUBLE sum=0.0;
	for (int i=0; i < _Q.size(); ++i) {
		sum -= _Q[i][i]*_freq[i];
	}
	return sum;
}


//void replacementModelSSRV::norm(MDOUBLE scale){
//	
//	for (int i=0; i < _Q.size(); ++i) {
//		for (int j=0; j < _Q.size(); ++j) {
//			_Q[i][j]*=scale;
//		}
//	}
//	
//	_q2pt.fillFromRateMatrix(_freq,_Q);
//}