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
|
#include "muscle.h"
#include "textfile.h"
#define TRACE 0
const int MAX_LINE = 4096;
const int MAX_HEADINGS = 32;
static char Heading[MAX_HEADINGS];
static unsigned HeadingCount = 0;
static float Mx[32][32];
static void LogMx()
{
Log("Matrix\n");
Log(" ");
for (int i = 0; i < 20; ++i)
Log(" %c", LetterToChar(i));
Log("\n");
for (int i = 0; i < 20; ++i)
{
Log("%c ", LetterToChar(i));
for (int j = 0; j < 20; ++j)
Log("%5.1f", Mx[i][j]);
Log("\n");
}
Log("\n");
}
static unsigned MxCharToLetter(char c)
{
for (unsigned Letter = 0; Letter < HeadingCount; ++Letter)
if (Heading[Letter] == c)
return Letter;
Quit("Letter '%c' has no heading", c);
return 0;
}
PTR_SCOREMATRIX ReadMx(TextFile &File)
{
// Find column headers
char Line[MAX_LINE];
for (;;)
{
bool EndOfFile = File.GetLine(Line, sizeof(Line));
if (EndOfFile)
Quit("Premature EOF in matrix file");
if (Line[0] == '#')
continue;
else if (Line[0] == ' ')
break;
else
Quit("Invalid line in matrix file: '%s'", Line);
}
// Read column headers
HeadingCount = 0;
for (char *p = Line; *p; ++p)
{
char c = *p;
if (!isspace(c))
Heading[HeadingCount++] = c;
}
if (HeadingCount > 0 && Heading[HeadingCount-1] == '*')
--HeadingCount;
if (HeadingCount < 20)
Quit("Error in matrix file: < 20 headers, line='%s'", Line);
#if TRACE
{
Log("ReadMx\n");
Log("%d headings: ", HeadingCount);
for (unsigned i = 0; i < HeadingCount; ++i)
Log("%c", Heading[i]);
Log("\n");
}
#endif
// Zero out matrix
for (int i = 0; i < MAX_ALPHA; ++i)
for (int j = 0; j < MAX_ALPHA; ++j)
Mx[i][j] = 0.0;
// Read data lines
for (unsigned RowIndex = 0; RowIndex < HeadingCount; ++RowIndex)
{
bool EndOfFile = File.GetTrimLine(Line, sizeof(Line));
if (EndOfFile)
Quit("Premature EOF in matrix file");
#if TRACE
Log("Line=%s\n", Line);
#endif
if (Line[0] == '#')
continue;
char c = Line[0];
#if TRACE
Log("Row char=%c\n", c);
#endif
if (!IsResidueChar(c))
continue;
unsigned RowLetter = CharToLetter(c);
#if TRACE
Log("Row letter = %u\n", RowLetter);
#endif
char *p = Line + 1;
char *maxp = p + strlen(Line);
for (unsigned Col = 0; Col < HeadingCount - 1; ++Col)
{
if (p >= maxp)
Quit("Too few fields in line of matrix file: '%s'", Line);
while (isspace(*p))
++p;
char *Value = p;
while (!isspace(*p))
++p;
float v = (float) atof(Value);
char HeaderChar = Heading[Col];
if (IsResidueChar(HeaderChar))
{
unsigned ColLetter = CharToLetter(HeaderChar);
Mx[RowLetter][ColLetter] = v;
}
p += 1;
}
}
// Sanity check for symmetry
for (int i = 0; i < 20; ++i)
for (int j = 0; j < i; ++j)
{
if (Mx[i][j] != Mx[j][i])
{
Warning("Matrix is not symmetrical, %c->%c=%g, %c->%c=%g",
CharToLetter(i),
CharToLetter(j),
Mx[i][j],
CharToLetter(j),
CharToLetter(i),
Mx[j][i]);
goto ExitLoop;
}
}
ExitLoop:;
if (g_bVerbose)
LogMx();
return &Mx;
}
|