📄 nwsmall.cpp
字号:
#include "muscle.h"
#include <math.h>
#include "pwpath.h"
#include "profile.h"
#include <stdio.h>
// NW small memory
#define TRACE 0
#if TRACE
extern bool g_bKeepSimpleDP;
extern SCORE *g_DPM;
extern SCORE *g_DPD;
extern SCORE *g_DPI;
extern char *g_TBM;
extern char *g_TBD;
extern char *g_TBI;
#endif
#if TRACE
#define ALLOC_TRACE() \
const SCORE UNINIT = MINUS_INFINITY; \
const size_t LM = uPrefixCountA*uPrefixCountB; \
\
SCORE *DPM_ = new SCORE[LM]; \
SCORE *DPD_ = new SCORE[LM]; \
SCORE *DPI_ = new SCORE[LM]; \
\
char *TBM_ = new char[LM]; \
char *TBD_ = new char[LM]; \
char *TBI_ = new char[LM]; \
\
memset(TBM_, '?', LM); \
memset(TBD_, '?', LM); \
memset(TBI_, '?', LM); \
\
for (unsigned i = 0; i <= uLengthA; ++i) \
for (unsigned j = 0; j <= uLengthB; ++j) \
{ \
DPM(i, j) = UNINIT; \
DPD(i, j) = UNINIT; \
DPI(i, j) = UNINIT; \
}
#else
#define ALLOC_TRACE()
#endif
#if TRACE
#define SetDPM(i, j, x) DPM(i, j) = x
#define SetDPD(i, j, x) DPD(i, j) = x
#define SetDPI(i, j, x) DPI(i, j) = x
#define SetTBM(i, j, x) TBM(i, j) = x
#define SetTBD(i, j, x) TBD(i, j) = x
#define SetTBI(i, j, x) TBI(i, j) = x
#else
#define SetDPM(i, j, x) /* empty */
#define SetDPD(i, j, x) /* empty */
#define SetDPI(i, j, x) /* empty */
#define SetTBM(i, j, x) /* empty */
#define SetTBD(i, j, x) /* empty */
#define SetTBI(i, j, x) /* empty */
#endif
#define RECURSE_D(i, j) \
{ \
SCORE DD = DRow[j] + e; \
SCORE MD = MPrev[j] + PA[i-1].m_scoreGapOpen;\
if (DD > MD) \
{ \
DRow[j] = DD; \
SetTBD(i, j, 'D'); \
} \
else \
{ \
DRow[j] = MD; \
/* SetBitTBD(TB, i, j, 'M'); */ \
TBRow[j] &= ~BIT_xD; \
TBRow[j] |= BIT_MD; \
SetTBD(i, j, 'M'); \
} \
SetDPD(i, j, DRow[j]); \
}
#define RECURSE_D_ATerm(j) RECURSE_D(uLengthA, j)
#define RECURSE_D_BTerm(j) RECURSE_D(i, uLengthB)
#define RECURSE_I(i, j) \
{ \
Iij += e; \
SCORE MI = MCurr[j-1] + PB[j-1].m_scoreGapOpen;\
if (MI >= Iij) \
{ \
Iij = MI; \
/* SetBitTBI(TB, i, j, 'M'); */ \
TBRow[j] &= ~BIT_xI; \
TBRow[j] |= BIT_MI; \
SetTBI(i, j, 'M'); \
} \
else \
SetTBI(i, j, 'I'); \
SetDPI(i, j, Iij); \
}
#define RECURSE_I_ATerm(j) RECURSE_I(uLengthA, j)
#define RECURSE_I_BTerm(j) RECURSE_I(i, uLengthB)
#define RECURSE_M(i, j) \
{ \
SCORE DM = DRow[j] + PA[i-1].m_scoreGapClose; \
SCORE IM = Iij + PB[j-1].m_scoreGapClose; \
SCORE MM = MCurr[j]; \
TB[i+1][j+1] &= ~BIT_xM; \
if (MM >= DM && MM >= IM) \
{ \
MNext[j+1] += MM; \
SetDPM(i+1, j+1, MNext[j+1]); \
SetTBM(i+1, j+1, 'M'); \
/* SetBitTBM(TB, i+1, j+1, 'M'); */ \
TB[i+1][j+1] |= BIT_MM; \
} \
else if (DM >= MM && DM >= IM) \
{ \
MNext[j+1] += DM; \
SetDPM(i+1, j+1, MNext[j+1]); \
SetTBM(i+1, j+1, 'D'); \
/* SetBitTBM(TB, i+1, j+1, 'D'); */ \
TB[i+1][j+1] |= BIT_DM; \
} \
else \
{ \
assert(IM >= MM && IM >= DM); \
MNext[j+1] += IM; \
SetDPM(i+1, j+1, MNext[j+1]); \
SetTBM(i+1, j+1, 'I'); \
/* SetBitTBM(TB, i+1, j+1, 'I'); */ \
TB[i+1][j+1] |= BIT_IM; \
} \
}
#if TRACE
static bool LocalEq(BASETYPE b1, BASETYPE b2)
{
if (b1 < -100000 && b2 < -100000)
return true;
double diff = fabs(b1 - b2);
if (diff < 0.0001)
return true;
double sum = fabs(b1) + fabs(b2);
return diff/sum < 0.005;
}
static char Get_M_Char(char Bits)
{
switch (Bits & BIT_xM)
{
case BIT_MM:
return 'M';
case BIT_DM:
return 'D';
case BIT_IM:
return 'I';
}
Quit("Huh?");
return '?';
}
static char Get_D_Char(char Bits)
{
return (Bits & BIT_xD) ? 'M' : 'D';
}
static char Get_I_Char(char Bits)
{
return (Bits & BIT_xI) ? 'M' : 'I';
}
static bool DPEq(char c, SCORE *g_DP, SCORE *DPD_,
unsigned uPrefixCountA, unsigned uPrefixCountB)
{
SCORE *DPM_ = g_DP;
for (unsigned i = 0; i < uPrefixCountA; ++i)
for (unsigned j = 0; j < uPrefixCountB; ++j)
if (!LocalEq(DPM(i, j), DPD(i, j)))
{
Log("***DPDIFF*** DP%c(%d, %d) Simple = %.2g, Fast = %.2g\n",
c, i, j, DPM(i, j), DPD(i, j));
return false;
}
return true;
}
static bool CompareTB(char **TB, char *TBM_, char *TBD_, char *TBI_,
unsigned uPrefixCountA, unsigned uPrefixCountB)
{
SCORE *DPM_ = g_DPM;
bool Eq = true;
for (unsigned i = 0; i < uPrefixCountA; ++i)
for (unsigned j = 0; j < uPrefixCountB; ++j)
{
char c1 = TBM(i, j);
char c2 = Get_M_Char(TB[i][j]);
if (c1 != '?' && c1 != c2 && DPM(i, j) > -100000)
{
Log("TBM(%d, %d) Simple = %c, NW = %c\n", i, j, c1, c2);
Eq = false;
goto D;
}
}
D:
SCORE *DPD_ = g_DPD;
for (unsigned i = 0; i < uPrefixCountA; ++i)
for (unsigned j = 0; j < uPrefixCountB; ++j)
{
char c1 = TBD(i, j);
char c2 = Get_D_Char(TB[i][j]);
if (c1 != '?' && c1 != c2 && DPD(i, j) > -100000)
{
Log("TBD(%d, %d) Simple = %c, NW = %c\n", i, j, c1, c2);
Eq = false;
goto I;
}
}
I:
SCORE *DPI_ = g_DPI;
for (unsigned i = 0; i < uPrefixCountA; ++i)
for (unsigned j = 0; j < uPrefixCountB; ++j)
{
char c1 = TBI(i, j);
char c2 = Get_I_Char(TB[i][j]);
if (c1 != '?' && c1 != c2 && DPI(i, j) > -100000)
{
Log("TBI(%d, %d) Simple = %c, NW = %c\n", i, j, c1, c2);
Eq = false;
goto Done;
}
}
Done:
if (Eq)
Log("TB success\n");
return Eq;
}
static const char *LocalScoreToStr(SCORE s)
{
static char str[16];
if (s < -100000)
return " *";
sprintf(str, "%6.1f", s);
return str;
}
static void LogDP(const SCORE *DPM_, const ProfPos *PA, const ProfPos *PB,
unsigned uPrefixCountA, unsigned uPrefixCountB)
{
Log(" ");
for (unsigned uPrefixLengthB = 0; uPrefixLengthB < uPrefixCountB; ++uPrefixLengthB)
{
char c = ' ';
if (uPrefixLengthB > 0)
c = ConsensusChar(PB[uPrefixLengthB - 1]);
Log(" %4u:%c", uPrefixLengthB, c);
}
Log("\n");
for (unsigned uPrefixLengthA = 0; uPrefixLengthA < uPrefixCountA; ++uPrefixLengthA)
{
char c = ' ';
if (uPrefixLengthA > 0)
c = ConsensusChar(PA[uPrefixLengthA - 1]);
Log("%4u:%c ", uPrefixLengthA, c);
for (unsigned uPrefixLengthB = 0; uPrefixLengthB < uPrefixCountB; ++uPrefixLengthB)
Log(" %s", LocalScoreToStr(DPM(uPrefixLengthA, uPrefixLengthB)));
Log("\n");
}
}
static void LogBitTB(char **TB, const ProfPos *PA, const ProfPos *PB,
unsigned uPrefixCountA, unsigned uPrefixCountB)
{
Log(" ");
for (unsigned uPrefixLengthB = 0; uPrefixLengthB < uPrefixCountB; ++uPrefixLengthB)
{
char c = ' ';
if (uPrefixLengthB > 0)
c = ConsensusChar(PB[uPrefixLengthB - 1]);
Log(" %4u:%c", uPrefixLengthB, c);
}
Log("\n");
Log("Bit TBM:\n");
for (unsigned uPrefixLengthA = 0; uPrefixLengthA < uPrefixCountA; ++uPrefixLengthA)
{
char c = ' ';
if (uPrefixLengthA > 0)
c = ConsensusChar(PA[uPrefixLengthA - 1]);
Log("%4u:%c ", uPrefixLengthA, c);
for (unsigned uPrefixLengthB = 0; uPrefixLengthB < uPrefixCountB; ++uPrefixLengthB)
{
char c = Get_M_Char(TB[uPrefixLengthA][uPrefixLengthB]);
Log(" %6c", c);
}
Log("\n");
}
Log("\n");
Log("Bit TBD:\n");
for (unsigned uPrefixLengthA = 0; uPrefixLengthA < uPrefixCountA; ++uPrefixLengthA)
{
char c = ' ';
if (uPrefixLengthA > 0)
c = ConsensusChar(PA[uPrefixLengthA - 1]);
Log("%4u:%c ", uPrefixLengthA, c);
for (unsigned uPrefixLengthB = 0; uPrefixLengthB < uPrefixCountB; ++uPrefixLengthB)
{
char c = Get_D_Char(TB[uPrefixLengthA][uPrefixLengthB]);
Log(" %6c", c);
}
Log("\n");
}
Log("\n");
Log("Bit TBI:\n");
for (unsigned uPrefixLengthA = 0; uPrefixLengthA < uPrefixCountA; ++uPrefixLengthA)
{
char c = ' ';
if (uPrefixLengthA > 0)
c = ConsensusChar(PA[uPrefixLengthA - 1]);
Log("%4u:%c ", uPrefixLengthA, c);
for (unsigned uPrefixLengthB = 0; uPrefixLengthB < uPrefixCountB; ++uPrefixLengthB)
{
char c = Get_I_Char(TB[uPrefixLengthA][uPrefixLengthB]);
Log(" %6c", c);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -