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