1 | #include "muscle.h" |
---|
2 | #include "msa.h" |
---|
3 | #include "textfile.h" |
---|
4 | |
---|
5 | const int BLOCKSIZE = 60; |
---|
6 | |
---|
7 | static char FixChar(char c) |
---|
8 | { |
---|
9 | switch (c) |
---|
10 | { |
---|
11 | case '(': |
---|
12 | case ')': |
---|
13 | case '[': |
---|
14 | case ']': |
---|
15 | case ':': |
---|
16 | case ';': |
---|
17 | case ',': |
---|
18 | return '_'; |
---|
19 | } |
---|
20 | if (!isprint(c)) |
---|
21 | return '_'; |
---|
22 | return c; |
---|
23 | } |
---|
24 | |
---|
25 | static void FixName(char Name[]) |
---|
26 | { |
---|
27 | while (char c = *Name) |
---|
28 | *Name++ = FixChar(c); |
---|
29 | } |
---|
30 | |
---|
31 | void MSA::ToPhySequentialFile(TextFile &File) const |
---|
32 | { |
---|
33 | const unsigned SeqCount = GetSeqCount(); |
---|
34 | const unsigned ColCount = GetColCount(); |
---|
35 | |
---|
36 | File.PutFormat("%d %d\n", SeqCount, ColCount); |
---|
37 | |
---|
38 | if (0 == ColCount) |
---|
39 | return; |
---|
40 | |
---|
41 | for (unsigned Seq = 0; Seq < SeqCount; ++Seq) |
---|
42 | { |
---|
43 | char Name[11]; |
---|
44 | const char *ptrName = GetSeqName(Seq); |
---|
45 | size_t n = strlen(ptrName); |
---|
46 | if (n > 10) |
---|
47 | n = 10; |
---|
48 | memcpy(Name, ptrName, n); |
---|
49 | Name[n] = 0; |
---|
50 | FixName(Name); |
---|
51 | File.PutFormat("%-10.10s", Name); |
---|
52 | |
---|
53 | int BlockIndex = 0; |
---|
54 | int Col = 0; |
---|
55 | for (;;) |
---|
56 | { |
---|
57 | const unsigned MaxCols = (BlockIndex == 0) ? (BLOCKSIZE - 10) : BLOCKSIZE; |
---|
58 | for (unsigned ColsThisBlock = 0; ColsThisBlock < MaxCols; ++ColsThisBlock) |
---|
59 | { |
---|
60 | if (Col == ColCount) |
---|
61 | break; |
---|
62 | if (ColsThisBlock%10 == 0 && (BlockIndex == 0 || ColsThisBlock > 0)) |
---|
63 | File.PutChar(' '); |
---|
64 | char c = GetChar(Seq, Col); |
---|
65 | if (isalpha(c)) |
---|
66 | c = toupper(c); |
---|
67 | File.PutChar(c); |
---|
68 | ++Col; |
---|
69 | } |
---|
70 | File.PutChar('\n'); |
---|
71 | if (Col == ColCount) |
---|
72 | break; |
---|
73 | ++BlockIndex; |
---|
74 | } |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | void MSA::ToPhyInterleavedFile(TextFile &File) const |
---|
79 | { |
---|
80 | const unsigned SeqCount = GetSeqCount(); |
---|
81 | const unsigned ColCount = GetColCount(); |
---|
82 | |
---|
83 | File.PutFormat("%d %d\n", SeqCount, ColCount); |
---|
84 | |
---|
85 | if (0 == ColCount) |
---|
86 | return; |
---|
87 | |
---|
88 | int Col = 0; |
---|
89 | for (;;) |
---|
90 | { |
---|
91 | const unsigned ColBlockStart = Col; |
---|
92 | const unsigned MaxCols = (ColBlockStart == 0) ? (BLOCKSIZE - 10) : BLOCKSIZE; |
---|
93 | |
---|
94 | for (unsigned Seq = 0; Seq < SeqCount; ++Seq) |
---|
95 | { |
---|
96 | if (0 == ColBlockStart) |
---|
97 | { |
---|
98 | char Name[11]; |
---|
99 | const char *ptrName = GetSeqName(Seq); |
---|
100 | size_t n = strlen(ptrName); |
---|
101 | if (n > 10) |
---|
102 | n = 10; |
---|
103 | memcpy(Name, ptrName, n); |
---|
104 | Name[n] = 0; |
---|
105 | FixName(Name); |
---|
106 | File.PutFormat("%-10.10s", Name); |
---|
107 | } |
---|
108 | |
---|
109 | Col = ColBlockStart; |
---|
110 | for (unsigned ColsThisBlock = 0; ColsThisBlock < MaxCols; ++ColsThisBlock) |
---|
111 | { |
---|
112 | if (Col == ColCount) |
---|
113 | break; |
---|
114 | if (ColsThisBlock%10 == 0 && (0 == ColBlockStart || ColsThisBlock > 0)) |
---|
115 | File.PutChar(' '); |
---|
116 | char c = GetChar(Seq, Col); |
---|
117 | if (isalpha(c)) |
---|
118 | c = toupper(c); |
---|
119 | File.PutChar(c); |
---|
120 | ++Col; |
---|
121 | } |
---|
122 | File.PutChar('\n'); |
---|
123 | } |
---|
124 | if (Col == ColCount) |
---|
125 | break; |
---|
126 | File.PutChar('\n'); |
---|
127 | } |
---|
128 | } |
---|