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
|
// Copyright 2008, 2009, 2010, 2011, 2014 Martin C. Frith
#include "ScoreMatrix.hh"
#include "ScoreMatrixData.hh"
#include "qualityScoreUtil.hh"
#include "zio.hh"
#include <sstream>
#include <iomanip>
#include <algorithm> // min, max
#include <stdexcept>
#include <cassert>
#include <cctype> // toupper, tolower
#include <stddef.h> // size_t
//#include <iostream> // for debugging
#define ERR(x) throw std::runtime_error(x)
#define COUNTOF(a) (sizeof (a) / sizeof *(a))
static void makeUppercase(std::string& s) {
for (size_t i = 0; i < s.size(); ++i) {
unsigned char c = s[i];
s[i] = std::toupper(c);
}
}
namespace cbrc{
const char *ScoreMatrix::canonicalName( const std::string& name ){
for( size_t i = 0; i < COUNTOF(scoreMatrixNicknames); ++i )
if( name == scoreMatrixNicknames[i].nickname )
return scoreMatrixNicknames[i].realname;
return name.c_str();
}
std::string ScoreMatrix::stringFromName( const std::string& name ){
std::string n = canonicalName( name );
for( size_t i = 0; i < COUNTOF(scoreMatrices); ++i )
if( n == scoreMatrices[i].name )
return scoreMatrices[i].text;
return slurp( n.c_str() );
}
void ScoreMatrix::setMatchMismatch(int matchScore, int mismatchCost,
const std::string& symbols) {
rowSymbols.assign(symbols.begin(), symbols.end());
colSymbols.assign(symbols.begin(), symbols.end());
size_t size = symbols.size();
cells.resize(size);
for (size_t i = 0; i < size; ++i) {
cells[i].assign(size, -mismatchCost);
cells[i][i] = matchScore;
}
}
void ScoreMatrix::fromString( const std::string& matString ){
std::istringstream iss(matString);
iss >> *this;
if( !iss ) ERR( "can't read the score matrix" );
}
static unsigned s2i(const uchar symbolToIndex[], uchar c) {
return symbolToIndex[c];
}
static void upperAndLowerIndex(unsigned tooBig, const uchar symbolToIndex[],
char symbol, unsigned& upper, unsigned& lower) {
uchar s = symbol;
upper = symbolToIndex[s];
lower = symbolToIndex[std::tolower(s)];
if (upper >= tooBig || lower >= tooBig) {
ERR(std::string("bad letter in score matrix: ") + symbol);
}
}
void ScoreMatrix::init(const uchar symbolToIndex[]) {
assert( !rowSymbols.empty() && !colSymbols.empty() );
makeUppercase(rowSymbols);
makeUppercase(colSymbols);
minScore = cells[0][0];
maxScore = cells[0][0];
for( size_t i = 0; i < rowSymbols.size(); ++i ){
for( size_t j = 0; j < colSymbols.size(); ++j ){
minScore = std::min( minScore, cells[i][j] );
maxScore = std::max( maxScore, cells[i][j] );
}
}
// set default score = minScore:
for( unsigned i = 0; i < MAT; ++i ){
for( unsigned j = 0; j < MAT; ++j ){
caseSensitive[i][j] = minScore;
caseInsensitive[i][j] = minScore;
}
}
for( size_t i = 0; i < rowSymbols.size(); ++i ){
for( size_t j = 0; j < colSymbols.size(); ++j ){
unsigned iu, il, ju, jl;
upperAndLowerIndex(MAT, symbolToIndex, rowSymbols[i], iu, il);
upperAndLowerIndex(MAT, symbolToIndex, colSymbols[j], ju, jl);
caseSensitive[iu][jl] = std::min( cells[i][j], 0 );
caseSensitive[il][ju] = std::min( cells[i][j], 0 );
caseSensitive[il][jl] = std::min( cells[i][j], 0 );
caseSensitive[iu][ju] = cells[i][j]; // careful: maybe il==iu or jl==ju
caseInsensitive[iu][ju] = cells[i][j];
caseInsensitive[iu][jl] = cells[i][j];
caseInsensitive[il][ju] = cells[i][j];
caseInsensitive[il][jl] = cells[i][j];
}
}
// set a hugely negative score for the delimiter symbol:
uchar delimiter = ' ';
uchar z = symbolToIndex[delimiter];
assert( z < MAT );
for( unsigned i = 0; i < MAT; ++i ){
caseSensitive[z][i] = -INF;
caseSensitive[i][z] = -INF;
caseInsensitive[z][i] = -INF;
caseInsensitive[i][z] = -INF;
}
}
void ScoreMatrix::writeCommented( std::ostream& stream ) const{
int colWidth = colSymbols.size() < 20 ? 3 : 2;
stream << "# " << ' ';
for( size_t i = 0; i < colSymbols.size(); ++i ){
stream << ' ' << std::setw(colWidth) << colSymbols[i];
}
stream << '\n';
for( size_t i = 0; i < rowSymbols.size(); ++i ){
stream << "# " << rowSymbols[i];
for( size_t j = 0; j < colSymbols.size(); ++j ){
stream << ' ' << std::setw(colWidth) << cells[i][j];
}
stream << '\n';
}
}
std::istream& operator>>( std::istream& stream, ScoreMatrix& m ){
std::string tmpRowSymbols;
std::string tmpColSymbols;
std::vector< std::vector<int> > tmpCells;
std::string line;
int state = 0;
while( std::getline( stream, line ) ){
std::istringstream iss(line);
char c;
if( !(iss >> c) ) continue; // skip blank lines
if( state == 0 ){
if( c == '#' ) continue; // skip comment lines at the top
do{
tmpColSymbols.push_back(c);
}while( iss >> c );
state = 1;
}
else{
tmpRowSymbols.push_back(c);
tmpCells.resize( tmpCells.size() + 1 );
int score;
while( iss >> score ){
tmpCells.back().push_back(score);
}
if (tmpCells.back().size() != tmpColSymbols.size()) {
ERR("bad score matrix");
}
}
}
if( stream.eof() && !stream.bad() && !tmpRowSymbols.empty() ){
stream.clear();
m.rowSymbols.swap(tmpRowSymbols);
m.colSymbols.swap(tmpColSymbols);
m.cells.swap(tmpCells);
}
return stream;
}
const char *ambiguities[] = {
"M" "AC",
"S" "CG",
"K" "GT",
"W" "TA",
"R" "AG",
"Y" "CT",
"B" "CGT",
"D" "AGT",
"H" "ACT",
"V" "ACG"
};
const size_t numOfAmbiguousSymbols = COUNTOF(ambiguities);
static bool isIn(const std::string& s, char x) {
return find(s.begin(), s.end(), x) != s.end();
}
static const char *ambiguityList(char symbol, char scratch[]) {
for (size_t i = 0; i < numOfAmbiguousSymbols; ++i) {
if (ambiguities[i][0] == symbol) return ambiguities[i] + 1;
}
scratch[0] = symbol;
return scratch;
}
static double symbolProbSum(const uchar symbolToIndex[],
const char *symbols, const double probs[]) {
if (symbols[1] == 0) return 1;
double p = 0;
for (size_t i = 0; symbols[i]; ++i) {
unsigned x = s2i(symbolToIndex, symbols[i]);
p += probs[x];
}
return p;
}
static int jointScore(const uchar symbolToIndex[], int **fastMatrix,
double scale,
const double rowSymbolProbs[],
const double colSymbolProbs[],
const char *rSymbols, const char *cSymbols) {
bool isOneRowSymbol = (rSymbols[1] == 0);
bool isOneColSymbol = (cSymbols[1] == 0);
double p = 0;
for (size_t i = 0; rSymbols[i]; ++i) {
for (size_t j = 0; cSymbols[j]; ++j) {
unsigned x = s2i(symbolToIndex, rSymbols[i]);
unsigned y = s2i(symbolToIndex, cSymbols[j]);
double r = isOneRowSymbol ? 1 : rowSymbolProbs[x];
double c = isOneColSymbol ? 1 : colSymbolProbs[y];
p += r * c * probFromScore(scale, fastMatrix[x][y]);
}
}
double rowProbSum = symbolProbSum(symbolToIndex, rSymbols, rowSymbolProbs);
double colProbSum = symbolProbSum(symbolToIndex, cSymbols, colSymbolProbs);
return scoreFromProb(scale, p / (rowProbSum * colProbSum));
}
void ScoreMatrix::addAmbiguousScores(const uchar symbolToIndex[],
double scale,
const double rowSymbolProbs[],
const double colSymbolProbs[]) {
int *fastMatrix[MAT];
std::copy(caseInsensitive, caseInsensitive + MAT, fastMatrix);
char scratch[2] = {0};
for (size_t k = 0; k < numOfAmbiguousSymbols; ++k) {
char ambiguousSymbol = ambiguities[k][0];
if (isIn(colSymbols, ambiguousSymbol)) continue;
colSymbols.push_back(ambiguousSymbol);
for (size_t i = 0; i < rowSymbols.size(); ++i) {
const char *rSymbols = ambiguityList(rowSymbols[i], scratch);
const char *cSymbols = ambiguities[k] + 1;
int s = jointScore(symbolToIndex, fastMatrix, scale,
rowSymbolProbs, colSymbolProbs, rSymbols, cSymbols);
cells[i].push_back(s);
}
}
for (size_t k = 0; k < numOfAmbiguousSymbols; ++k) {
char ambiguousSymbol = ambiguities[k][0];
if (isIn(rowSymbols, ambiguousSymbol)) continue;
rowSymbols.push_back(ambiguousSymbol);
cells.resize(cells.size() + 1);
for (size_t j = 0; j < colSymbols.size(); ++j) {
const char *rSymbols = ambiguities[k] + 1;
const char *cSymbols = ambiguityList(colSymbols[j], scratch);
int s = jointScore(symbolToIndex, fastMatrix, scale,
rowSymbolProbs, colSymbolProbs, rSymbols, cSymbols);
cells.back().push_back(s);
}
}
}
}
|