| 1 | #include "muscle.h" |
|---|
| 2 | #include <stdio.h> |
|---|
| 3 | #include <ctype.h> |
|---|
| 4 | #include "msa.h" |
|---|
| 5 | #include "textfile.h" |
|---|
| 6 | |
|---|
| 7 | const unsigned uCharsPerLine = 60; |
|---|
| 8 | const int MIN_NAME = 10; |
|---|
| 9 | const int MAX_NAME = 32; |
|---|
| 10 | |
|---|
| 11 | extern void AssignColors(const MSA &a, int **Colors); |
|---|
| 12 | |
|---|
| 13 | static int **MakeColors(const MSA &a) |
|---|
| 14 | { |
|---|
| 15 | const unsigned uSeqCount = a.GetSeqCount(); |
|---|
| 16 | const unsigned uColCount = a.GetColCount(); |
|---|
| 17 | |
|---|
| 18 | int **Colors = new int *[uSeqCount]; |
|---|
| 19 | for (unsigned i = 0; i < uSeqCount; ++i) |
|---|
| 20 | { |
|---|
| 21 | Colors[i] = new int[uColCount]; |
|---|
| 22 | memset(Colors[i], 0, uColCount*sizeof(int)); |
|---|
| 23 | } |
|---|
| 24 | AssignColors(a, Colors); |
|---|
| 25 | return Colors; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | static void ChangeColor(TextFile &File, int From, int To) |
|---|
| 29 | { |
|---|
| 30 | if (From == To) |
|---|
| 31 | return; |
|---|
| 32 | |
|---|
| 33 | #define COLOR_WHITE "FFFFFF" |
|---|
| 34 | #define COLOR_GRAY "C0C0C0" |
|---|
| 35 | #define COLOR_BLACK "000000" |
|---|
| 36 | #define COLOR_RED "FF0000" |
|---|
| 37 | #define COLOR_GREEN "00FF00" |
|---|
| 38 | #define COLOR_BLUE "5590FF" |
|---|
| 39 | #define COLOR_LIGHTBLUE "77FFFF" |
|---|
| 40 | |
|---|
| 41 | #define X(c) File.PutString("</SPAN><SPAN STYLE=\"background-color:#" c "\">"); |
|---|
| 42 | switch (To) |
|---|
| 43 | { |
|---|
| 44 | case 0: |
|---|
| 45 | X(COLOR_WHITE) |
|---|
| 46 | break; |
|---|
| 47 | case 1: |
|---|
| 48 | X(COLOR_GRAY) |
|---|
| 49 | break; |
|---|
| 50 | case 2: |
|---|
| 51 | X(COLOR_BLUE) |
|---|
| 52 | break; |
|---|
| 53 | case 3: |
|---|
| 54 | X(COLOR_LIGHTBLUE) |
|---|
| 55 | break; |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | #define COLOR_WINDOW "FFEEE0" |
|---|
| 60 | |
|---|
| 61 | void MSA::ToHTMLFile(TextFile &File) const |
|---|
| 62 | { |
|---|
| 63 | File.PutString("<HTML>\n"); |
|---|
| 64 | File.PutString("<BODY BGCOLOR=\"#" COLOR_WINDOW "\">\n"); |
|---|
| 65 | File.PutString("<PRE>"); |
|---|
| 66 | |
|---|
| 67 | int **Colors = MakeColors(*this); |
|---|
| 68 | |
|---|
| 69 | int iLongestNameLength = 0; |
|---|
| 70 | for (unsigned uSeqIndex = 0; uSeqIndex < GetSeqCount(); ++uSeqIndex) |
|---|
| 71 | { |
|---|
| 72 | const char *ptrName = GetSeqName(uSeqIndex); |
|---|
| 73 | const char *ptrBlank = strchr(ptrName, ' '); |
|---|
| 74 | int iLength; |
|---|
| 75 | if (0 != ptrBlank) |
|---|
| 76 | iLength = (int) (ptrBlank - ptrName); |
|---|
| 77 | else |
|---|
| 78 | iLength = (int) strlen(ptrName); |
|---|
| 79 | if (iLength > iLongestNameLength) |
|---|
| 80 | iLongestNameLength = iLength; |
|---|
| 81 | } |
|---|
| 82 | if (iLongestNameLength > MAX_NAME) |
|---|
| 83 | iLongestNameLength = MAX_NAME; |
|---|
| 84 | if (iLongestNameLength < MIN_NAME) |
|---|
| 85 | iLongestNameLength = MIN_NAME; |
|---|
| 86 | |
|---|
| 87 | unsigned uLineCount = (GetColCount() - 1)/uCharsPerLine + 1; |
|---|
| 88 | int CurrentColor = -1; |
|---|
| 89 | for (unsigned uLineIndex = 0; uLineIndex < uLineCount; ++uLineIndex) |
|---|
| 90 | { |
|---|
| 91 | File.PutString("\n"); |
|---|
| 92 | unsigned uStartColIndex = uLineIndex*uCharsPerLine; |
|---|
| 93 | unsigned uEndColIndex = uStartColIndex + uCharsPerLine - 1; |
|---|
| 94 | if (uEndColIndex >= GetColCount()) |
|---|
| 95 | uEndColIndex = GetColCount() - 1; |
|---|
| 96 | char Name[MAX_NAME+1]; |
|---|
| 97 | for (unsigned uSeqIndex = 0; uSeqIndex < GetSeqCount(); ++uSeqIndex) |
|---|
| 98 | { |
|---|
| 99 | const char *ptrName = GetSeqName(uSeqIndex); |
|---|
| 100 | const char *ptrBlank = strchr(ptrName, ' '); |
|---|
| 101 | int iLength; |
|---|
| 102 | if (0 != ptrBlank) |
|---|
| 103 | iLength = (int) (ptrBlank - ptrName); |
|---|
| 104 | else |
|---|
| 105 | iLength = (int) strlen(ptrName); |
|---|
| 106 | if (iLength > MAX_NAME) |
|---|
| 107 | iLength = MAX_NAME; |
|---|
| 108 | memset(Name, ' ', MAX_NAME); |
|---|
| 109 | memcpy(Name, ptrName, iLength); |
|---|
| 110 | Name[iLongestNameLength] = 0; |
|---|
| 111 | |
|---|
| 112 | // File.PutString("<FONT COLOR=\"#000000\">"); |
|---|
| 113 | CurrentColor = -1; |
|---|
| 114 | File.PutString("<SPAN STYLE=\"background-color:#" COLOR_WINDOW "\">"); |
|---|
| 115 | File.PutFormat("%s ", Name); |
|---|
| 116 | File.PutString("<SPAN STYLE=\"background-color:#FFFFFF\">"); |
|---|
| 117 | for (unsigned uColIndex = uStartColIndex; uColIndex <= uEndColIndex; |
|---|
| 118 | ++uColIndex) |
|---|
| 119 | { |
|---|
| 120 | const int Color = Colors[uSeqIndex][uColIndex]; |
|---|
| 121 | ChangeColor(File, CurrentColor, Color); |
|---|
| 122 | CurrentColor = Color; |
|---|
| 123 | const char c = GetChar(uSeqIndex, uColIndex); |
|---|
| 124 | if (Color == 0) |
|---|
| 125 | File.PutFormat("%c", tolower(c)); |
|---|
| 126 | else |
|---|
| 127 | File.PutFormat("%c", toupper(c)); |
|---|
| 128 | } |
|---|
| 129 | File.PutString("\n"); |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | File.PutString("</SPAN>\n"); |
|---|
| 133 | File.PutString("</PRE>\n"); |
|---|
| 134 | File.PutString("</BODY>\n"); |
|---|
| 135 | File.PutString("</HTML>\n"); |
|---|
| 136 | } |
|---|