ARB
DI_save_matr.cxx
Go to the documentation of this file.
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;
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 maxnds = 0;
39  bool tabbed = (type == DI_SAVE_TABBED);
40  double min = matrix->get(1, 0) * 100.0;
41  double max = min;
42  double sum = 0.0;
43  NDS_Labeler labeler(NDS_OUTPUT_LEAFTEXT_UNLIMITED);
44 
45  // create all NDS strings
46  char **nds_results = new char*[nentries];
47 
48  for (col=0; col<nentries; col++) {
49  const char *buf = entries[col]->name;
50  GBDATA *gb_species = GBT_find_species_rel_species_data(gb_species_data, buf);
51  if (gb_species) {
52  buf = labeler.speciesLabel(gb_main, gb_species, NULp, NULp);
53  while (buf[0] == ' ') ++buf;
54  }
55 
56  nds_results[col] = ARB_strdup(buf);
57  maxnds = std::max(maxnds, strlen(buf));
58  }
59 
60 #if defined(DEBUG) && 0
61  fprintf(out, "maxnds=%zu\n", maxnds);
62 #endif // DEBUG
63 
64  // print column headers :
65  if (tabbed) {
66  for (col=0; col<nentries; col++) {
67  if (!tabbed && (col%4) == 0) fputc('\t', out);
68  fprintf(out, "\t%s", nds_results[col]);
69  }
70  fputc('\n', out);
71  }
72  else {
73  bool *eos_reached = new bool[nentries];
74  for (col = 0; col<nentries; ++col) eos_reached[col] = false;
75 
76  for (row = 0; row<maxnds; ++row) {
77  for (col = 0; col<(maxnds+2); ++col) fputc(' ', out);
78 
79  for (col = 0; col<nentries; ++col) {
80  if (!tabbed && (col%4) == 0) fputs(" ", out);
81 
82  char c = ' ';
83  if (!eos_reached[col]) {
84  c = nds_results[col][row];
85  if (!c) { c = ' '; eos_reached[col] = true; }
86  }
87  fprintf(out, " %c ", c);
88  }
89  fputc('\n', out);
90  }
91 
92  delete [] eos_reached;
93  }
94 
95  // print data lines :
96  for (row = 0; row<nentries; row++) {
97  if (!tabbed && (row%4) == 0) fprintf(out, "\n"); // empty line after 4 lines of data
98 
99  if (tabbed) {
100  fprintf(out, "%s", nds_results[row]);
101  }
102  else {
103  fprintf(out, "%-*s ", int(maxnds), nds_results[row]);
104  }
105 
106  // print the matrix :
107  for (col=0; col<nentries; col++) {
108  if (tabbed) {
109  fputc('\t', out);
110  }
111  else if ((col%4) == 0) { // empty column after 4 columns
112  fprintf(out, " ");
113  }
114 
115  double val2 = matrix->get(row, col) * 100.0;
116  {
117  char buf[20];
118  if (val2 > 99.9 || row == col) sprintf(buf, "%4.0f", val2);
119  else sprintf(buf, "%4.1f", val2);
120 
121  if (tabbed) { // convert '.' -> ','
122  char *dot = strchr(buf, '.');
123  if (dot) *dot = ',';
124  }
125  fputs(buf, out);
126  }
127 
128  if (!tabbed) fputc(' ', out);
129 
130  if (val2 > max) max = val2;
131  if (val2 < min) min = val2;
132 
133  sum += val2; // ralf: before this added 'val' (which was included somehow)
134  }
135  fprintf(out, "\n");
136  }
137 
138  fputc('\n', out);
139 
140  fprintf(out, "Minimum:\t%f\n", min);
141  fprintf(out, "Maximum:\t%f\n", max);
142  fprintf(out, "Average:\t%f\n", sum/(nentries*nentries));
143 
144  for (col=0; col<nentries; col++) free(nds_results[col]);
145  delete [] nds_results;
146  break;
147  }
148  default:
149  error = GB_export_error("Unknown Save Type");
150  }
151  fclose(out);
152  return error;
153 }
GB_TYPES type
static void dot(double **i, double **j, double **k)
Definition: trnsprob.cxx:59
char * ARB_strdup(const char *str)
Definition: arb_string.h:27
GB_ERROR GB_export_error(const char *error)
Definition: arb_msg.cxx:257
DI_SAVE_TYPE
Definition: di_matr.hxx:93
static void error(const char *msg)
Definition: mkptypes.cxx:96
fputc('\n', stderr)
char * name
Definition: di_matr.hxx:89
GBDATA * GBT_find_species_rel_species_data(GBDATA *gb_species_data, const char *name)
Definition: aditem.cxx:133
AP_smatrix * matrix
Definition: di_matr.hxx:153
fputs(TRACE_PREFIX, stderr)
DI_ENTRY ** entries
Definition: di_matr.hxx:151
size_t nentries
Definition: di_matr.hxx:152
#define NULp
Definition: cxxforward.h:116
T get(size_t i, size_t j) const
Definition: matrix.h:52
const char * save(const char *filename, enum DI_SAVE_TYPE type)
GB_transaction ta(gb_var)
GBDATA * gb_main
Definition: adname.cxx:32
#define min(a, b)
Definition: f2c.h:153
#define max(a, b)
Definition: f2c.h:154