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