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: mulAlphabet.cpp 6420 2009-06-25 11:17:08Z adist $
#include "mulAlphabet.h"
#include "distribution.h"
#include "errorMsg.h"
#include <iostream>
#include "logFile.h"
mulAlphabet::mulAlphabet(const alphabet* baseAlphabet, int mulFactor) :
_baseAlphabet(baseAlphabet->clone()),
_mulFactor(mulFactor),
_size(baseAlphabet->size() * mulFactor)
{}
mulAlphabet::mulAlphabet(const mulAlphabet& other) :
_baseAlphabet(other._baseAlphabet->clone()),
_mulFactor(other._mulFactor),
_size(other._size)
{}
mulAlphabet::~mulAlphabet()
{
if (_baseAlphabet) delete (_baseAlphabet);
}
mulAlphabet& mulAlphabet::operator=(const mulAlphabet &other)
{
if (_baseAlphabet) delete (_baseAlphabet);
_baseAlphabet = other._baseAlphabet->clone();
_mulFactor = other._mulFactor;
_size = other._size;
return (*this);
}
int mulAlphabet::unknown() const
{
return (convertFromBasedAlphaInt(_baseAlphabet->unknown()));
}
int mulAlphabet::gap() const
{
return (convertFromBasedAlphaInt(_baseAlphabet->gap()));
}
int mulAlphabet::stringSize() const
{
return _baseAlphabet->stringSize();
}
bool mulAlphabet::isSpecific(const int id) const
{
if (id >= _size)
return false;
else
return _baseAlphabet->isSpecific(convertToBasedAlphaInt(id));
}
/* The first _size characters should be first. The rest of the characters aren't multiplied.
For example, when using nucleotides as the based alphabet and _mulFactor = 2 :
0 A0
1 C0
2 G0
3 T0
4 A1
5 C1
6 G1
7 T1
8 A
9 C
10 G
11 T
12 U
13 R
14 Y
15 K
16 M
17 S
18 W
19 B
20 D
21 H
22 V
23 N
-1 -
*/
string mulAlphabet::fromInt(const int id) const
{
// category and categoryName are for debug purpose
int category(_mulFactor);
if (id>=0)
category = min(id / _baseAlphabet->size() , _mulFactor) ;
string categoryName("");
categoryName = int2string(category);
int inCategoryId = convertToBasedAlphaInt(id);
return (_baseAlphabet->fromInt(inCategoryId) + categoryName);
}
int mulAlphabet::convertFromBasedAlphaInt(int id) const
{
if (id < 0)
return (id);
return (id + _size);
}
int mulAlphabet::fromChar(const string& str, const int pos) const
{
int id = _baseAlphabet->fromChar(str,pos);
return (convertFromBasedAlphaInt(id));
}
vector<int> mulAlphabet::fromString(const string &str) const
{
vector<int> result = _baseAlphabet->fromString(str);
vector<int>::iterator itr = result.begin();
for (; itr != result.end(); ++itr)
*itr = convertFromBasedAlphaInt(*itr);
return (result);
}
int mulAlphabet::convertToBasedAlphaInt(int id) const
{
if (id<0)
return (id);
if (id >= _size)
return (id - _size);
return (id % _baseAlphabet->size());
}
int mulAlphabet::relations(const int charInSeq, const int charToCheck) const
{
int baseAlphabetSize = _baseAlphabet->size();
int categoryInSeq(_mulFactor);
if (charInSeq>=0)
categoryInSeq = min(charInSeq/baseAlphabetSize , _mulFactor);
int categoryToCheck(_mulFactor);
if (charToCheck>=0)
categoryToCheck = min(charToCheck/baseAlphabetSize , _mulFactor);
if (categoryToCheck == _mulFactor)
LOG(4,<<"mulAlphabet::relations charToCheck should belong to category < _mulFactor = " << _mulFactor << endl);
if ((categoryInSeq == categoryToCheck) || (categoryInSeq == _mulFactor))
return _baseAlphabet->relations(convertToBasedAlphaInt(charInSeq),convertToBasedAlphaInt(charToCheck));
return 0;
}
int mulAlphabet::compareCategories(int charA, int charB) const
{
// TO DO should combine code by calling mulAlphabet::rateShiftType mulAlphabet::compareCategories
int baseAlphabetSize = _baseAlphabet->size();
int categoryA(_mulFactor);
if (categoryA>=0)
categoryA = min(charA/baseAlphabetSize,_mulFactor);
int categoryB(_mulFactor);
if (categoryB>=0)
categoryB = min(charB/baseAlphabetSize,_mulFactor);
if (categoryA<categoryB)
return 1;
else if (categoryB<categoryA)
return -1;
return (0);
}
mulAlphabet::rateShiftType mulAlphabet::compareCategories(int charA, int charB, int baseAlphabetSize, int multiplicationFactor)
{
int categoryA(multiplicationFactor);
if (categoryA>=0)
categoryA = min(charA/baseAlphabetSize,multiplicationFactor);
int categoryB(multiplicationFactor);
if (categoryB>=0)
categoryB = min(charB/baseAlphabetSize,multiplicationFactor);
if (categoryA<categoryB)
return acceleration;
else if (categoryB<categoryA)
return deceleration;
return noRateShift;
}
|