source: tags/ms_r16q3/DIST/DI_save_matr.cxx

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