1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : DI_save_matr.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // =============================================================== // |
---|
10 | |
---|
11 | #include "di_matr.hxx" |
---|
12 | #include <nds.h> |
---|
13 | |
---|
14 | GB_ERROR DI_MATRIX::save(const char *filename, enum DI_SAVE_TYPE type) { |
---|
15 | FILE *out; |
---|
16 | GB_ERROR error = NULp; |
---|
17 | |
---|
18 | out = fopen(filename, "w"); |
---|
19 | if (!out) return "Cannot save your File"; |
---|
20 | |
---|
21 | size_t row, col; |
---|
22 | switch (type) { |
---|
23 | case DI_SAVE_PHYLIP_COMP: { |
---|
24 | fprintf(out, " %zu\n", nentries); |
---|
25 | for (row = 0; row<nentries; row++) { |
---|
26 | fprintf(out, "%-10s ", entries[row]->name); |
---|
27 | for (col=0; col<row; col++) { |
---|
28 | fprintf(out, "%6f ", matrix->get(row, col)); |
---|
29 | } |
---|
30 | fprintf(out, "\n"); |
---|
31 | } |
---|
32 | break; |
---|
33 | } |
---|
34 | case DI_SAVE_READABLE: |
---|
35 | case DI_SAVE_TABBED: { |
---|
36 | GBDATA *gb_main = get_gb_main(); |
---|
37 | GB_transaction ta(gb_main); |
---|
38 | size_t app_size = 200; // maximum width for NDS output (and max. height for vertical one) |
---|
39 | size_t maxnds = 0; |
---|
40 | bool tabbed = (type == DI_SAVE_TABBED); |
---|
41 | double min = matrix->get(1, 0) * 100.0; |
---|
42 | double max = min; |
---|
43 | double sum = 0.0; |
---|
44 | |
---|
45 | make_node_text_init(gb_main); |
---|
46 | |
---|
47 | // create all NDS strings |
---|
48 | char **nds_results = new char*[nentries]; |
---|
49 | for (col=0; col<nentries; col++) { |
---|
50 | const char *buf = entries[col]->name; |
---|
51 | GBDATA *gb_species = GBT_find_species_rel_species_data(gb_species_data, buf); |
---|
52 | if (gb_species) { |
---|
53 | buf = make_node_text_nds(gb_main, gb_species, NDS_OUTPUT_LEAFTEXT, NULp, NULp); |
---|
54 | while (buf[0] == ' ') ++buf; |
---|
55 | } |
---|
56 | |
---|
57 | nds_results[col] = ARB_strdup(buf); |
---|
58 | |
---|
59 | size_t slen = strlen(buf); |
---|
60 | if (slen>app_size) slen = app_size; |
---|
61 | if (slen>maxnds) maxnds = slen; |
---|
62 | } |
---|
63 | |
---|
64 | #if defined(DEBUG) && 0 |
---|
65 | fprintf(out, "maxnds=%zu\n", maxnds); |
---|
66 | #endif // DEBUG |
---|
67 | |
---|
68 | // print column headers : |
---|
69 | if (tabbed) { |
---|
70 | for (col=0; col<nentries; col++) { |
---|
71 | if (!tabbed && (col%4) == 0) fputc('\t', out); |
---|
72 | fprintf(out, "\t%s", nds_results[col]); |
---|
73 | } |
---|
74 | fputc('\n', out); |
---|
75 | } |
---|
76 | else { |
---|
77 | bool *eos_reached = new bool[nentries]; |
---|
78 | for (col = 0; col<nentries; ++col) eos_reached[col] = false; |
---|
79 | |
---|
80 | for (row = 0; row<maxnds; ++row) { |
---|
81 | for (col = 0; col<(maxnds+2); ++col) fputc(' ', out); |
---|
82 | |
---|
83 | for (col = 0; col<nentries; ++col) { |
---|
84 | if (!tabbed && (col%4) == 0) fprintf(out, " "); |
---|
85 | if (!eos_reached[col]) { |
---|
86 | char c = nds_results[col][row]; |
---|
87 | if (c) fprintf(out, " %c ", c); |
---|
88 | else eos_reached[col] = true; |
---|
89 | } |
---|
90 | } |
---|
91 | fputc('\n', out); |
---|
92 | } |
---|
93 | |
---|
94 | delete [] eos_reached; |
---|
95 | } |
---|
96 | |
---|
97 | // print data lines : |
---|
98 | for (row = 0; row<nentries; row++) { |
---|
99 | if (!tabbed && (row%4) == 0) fprintf(out, "\n"); // empty line after 4 lines of data |
---|
100 | |
---|
101 | if (tabbed) { |
---|
102 | fprintf(out, "%s", nds_results[row]); |
---|
103 | } |
---|
104 | else { |
---|
105 | fprintf(out, "%-*s ", int(maxnds), nds_results[row]); |
---|
106 | } |
---|
107 | |
---|
108 | // print the matrix : |
---|
109 | for (col=0; col<nentries; col++) { |
---|
110 | if (tabbed) { |
---|
111 | fputc('\t', out); |
---|
112 | } |
---|
113 | else if ((col%4) == 0) { // empty column after 4 columns |
---|
114 | fprintf(out, " "); |
---|
115 | } |
---|
116 | |
---|
117 | double val2 = matrix->get(row, col) * 100.0; |
---|
118 | { |
---|
119 | char buf[20]; |
---|
120 | if (val2 > 99.9 || row == col) sprintf(buf, "%4.0f", val2); |
---|
121 | else sprintf(buf, "%4.1f", val2); |
---|
122 | |
---|
123 | if (tabbed) { // convert '.' -> ',' |
---|
124 | char *dot = strchr(buf, '.'); |
---|
125 | if (dot) *dot = ','; |
---|
126 | } |
---|
127 | fputs(buf, out); |
---|
128 | } |
---|
129 | |
---|
130 | if (!tabbed) fputc(' ', out); |
---|
131 | |
---|
132 | if (val2 > max) max = val2; |
---|
133 | if (val2 < min) min = val2; |
---|
134 | |
---|
135 | sum += val2; // ralf: before this added 'val' (which was included somehow) |
---|
136 | } |
---|
137 | fprintf(out, "\n"); |
---|
138 | } |
---|
139 | |
---|
140 | fputc('\n', out); |
---|
141 | |
---|
142 | fprintf(out, "Minimum:\t%f\n", min); |
---|
143 | fprintf(out, "Maximum:\t%f\n", max); |
---|
144 | fprintf(out, "Average:\t%f\n", sum/(nentries*nentries)); |
---|
145 | |
---|
146 | for (col=0; col<nentries; col++) free(nds_results[col]); |
---|
147 | delete [] nds_results; |
---|
148 | break; |
---|
149 | } |
---|
150 | default: |
---|
151 | error = GB_export_error("Unknown Save Type"); |
---|
152 | } |
---|
153 | fclose(out); |
---|
154 | return error; |
---|
155 | } |
---|