1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : ColumnStat_2_gnuplot.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // =============================================================== // |
---|
10 | |
---|
11 | #include "NT_local.h" |
---|
12 | |
---|
13 | #include <ColumnStat.hxx> |
---|
14 | #include <AP_filter.hxx> |
---|
15 | #include <awt_filter.hxx> |
---|
16 | #include <aw_awars.hxx> |
---|
17 | #include <aw_file.hxx> |
---|
18 | #include <aw_msg.hxx> |
---|
19 | #include <aw_root.hxx> |
---|
20 | #include <aw_select.hxx> |
---|
21 | #include <arbdbt.h> |
---|
22 | #include <arb_file.h> |
---|
23 | |
---|
24 | #include <unistd.h> |
---|
25 | |
---|
26 | #define AWAR_CS2GP "tmp/ntree/colstat_2_gnuplot" |
---|
27 | |
---|
28 | #define AWAR_CS2GP_NAME AWAR_CS2GP "/name" |
---|
29 | #define AWAR_CS2GP_SUFFIX AWAR_CS2GP "/filter" |
---|
30 | #define AWAR_CS2GP_DIRECTORY AWAR_CS2GP "/directory" |
---|
31 | #define AWAR_CS2GP_FILENAME AWAR_CS2GP "/file_name" |
---|
32 | |
---|
33 | #define AWAR_CS2GP_SMOOTH_VALUES AWAR_CS2GP "/smooth_values" |
---|
34 | #define AWAR_CS2GP_SMOOTH_GNUPLOT AWAR_CS2GP "/smooth_gnuplot" |
---|
35 | #define AWAR_CS2GP_GNUPLOT_OVERLAY_PREFIX AWAR_CS2GP "/gnuplot_overlay_prefix" |
---|
36 | #define AWAR_CS2GP_GNUPLOT_OVERLAY_POSTFIX AWAR_CS2GP "/gnuplot_overlay_postfix" |
---|
37 | #define AWAR_CS2GP_FILTER_NAME AWAR_CS2GP "/ap_filter/name" |
---|
38 | #define AWAR_CS2GP_FILTER_ALIGNMENT AWAR_CS2GP "/ap_filter/alignment" |
---|
39 | #define AWAR_CS2GP_FILTER_FILTER AWAR_CS2GP "/ap_filter/filter" |
---|
40 | |
---|
41 | |
---|
42 | static GB_ERROR split_stat_filename(const char *fname, char **dirPtr, char **name_prefixPtr, char **name_postfixPtr) { |
---|
43 | // 'fname' is sth like 'directory/prefix.sth_gnu' |
---|
44 | // 'dirPtr' is set to a malloc-copy of 'directory' |
---|
45 | // 'name_prefixPtr' is set to a malloc-copy of 'prefix' (defaults to '*') |
---|
46 | // 'name_postfixPtr' is set to a malloc-copy of 'sth_gnu' (defaults to '*_gnu') |
---|
47 | |
---|
48 | *dirPtr = 0; |
---|
49 | *name_prefixPtr = 0; |
---|
50 | *name_postfixPtr = 0; |
---|
51 | |
---|
52 | const char *lslash = strrchr(fname, '/'); |
---|
53 | if (!lslash) return GB_export_errorf("'%s' has to contain a '/'", fname); |
---|
54 | |
---|
55 | char *dir = strdup(fname); |
---|
56 | dir[lslash-fname] = 0; // cut off at last '/' |
---|
57 | |
---|
58 | char *name_prefix = strdup(lslash+1); |
---|
59 | char *name_postfix = 0; |
---|
60 | char *ldot = strrchr(name_prefix, '.'); |
---|
61 | |
---|
62 | if (ldot) { |
---|
63 | ldot[0] = 0; |
---|
64 | name_postfix = strdup(ldot+1); |
---|
65 | } |
---|
66 | if (!ldot || name_prefix[0] == 0) freedup(name_prefix, "*"); // no dot or empty name_prefix |
---|
67 | if (!name_postfix || name_postfix[0] == 0) freedup(name_postfix, "*_gnu"); |
---|
68 | |
---|
69 | nt_assert(name_prefix); |
---|
70 | nt_assert(name_postfix); |
---|
71 | |
---|
72 | *dirPtr = dir; |
---|
73 | *name_prefixPtr = name_prefix; |
---|
74 | *name_postfixPtr = name_postfix; |
---|
75 | |
---|
76 | return 0; |
---|
77 | } |
---|
78 | |
---|
79 | static char * get_overlay_files(AW_root *awr, const char *fname, GB_ERROR& error) { |
---|
80 | nt_assert(!error); |
---|
81 | |
---|
82 | bool overlay_prefix = awr->awar(AWAR_CS2GP_GNUPLOT_OVERLAY_PREFIX)->read_int(); |
---|
83 | bool overlay_postfix = awr->awar(AWAR_CS2GP_GNUPLOT_OVERLAY_POSTFIX)->read_int(); |
---|
84 | |
---|
85 | char *dir, *name_prefix, *name_postfix; |
---|
86 | error = split_stat_filename(fname, &dir, &name_prefix, &name_postfix); |
---|
87 | |
---|
88 | char *found_files = 0; |
---|
89 | if (!error) { |
---|
90 | char *found_prefix_files = 0; |
---|
91 | char *found_postfix_files = 0; |
---|
92 | |
---|
93 | if (overlay_prefix || overlay_postfix) { |
---|
94 | char *mask = GBS_global_string_copy("%s.*_gnu", name_prefix); |
---|
95 | if (overlay_prefix) { |
---|
96 | #if defined(WARN_TODO) |
---|
97 | #warning change error handling for GB_find_all_files() - globally! |
---|
98 | #endif |
---|
99 | found_prefix_files = GB_find_all_files(dir, mask, false); |
---|
100 | if (!found_prefix_files) error = GB_get_error(); |
---|
101 | } |
---|
102 | free(mask); |
---|
103 | |
---|
104 | if (!error) { |
---|
105 | mask = GBS_global_string_copy("*.%s", name_postfix); |
---|
106 | if (overlay_postfix) { |
---|
107 | found_postfix_files = GB_find_all_files(dir, mask, false); |
---|
108 | if (!found_postfix_files) error = GB_get_error(); |
---|
109 | } |
---|
110 | free(mask); |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | if (!error) { |
---|
115 | if (found_prefix_files) { |
---|
116 | if (found_postfix_files) { |
---|
117 | found_files = GBS_global_string_copy("%s*%s", found_prefix_files, found_postfix_files); |
---|
118 | } |
---|
119 | else { // only found_prefix_files |
---|
120 | found_files = found_prefix_files; |
---|
121 | found_prefix_files = 0; |
---|
122 | } |
---|
123 | } |
---|
124 | else { |
---|
125 | found_files = found_postfix_files; |
---|
126 | found_postfix_files = 0; |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | free(found_postfix_files); |
---|
131 | free(found_prefix_files); |
---|
132 | } |
---|
133 | |
---|
134 | free(name_postfix); |
---|
135 | free(name_prefix); |
---|
136 | free(dir); |
---|
137 | |
---|
138 | return found_files; |
---|
139 | } |
---|
140 | |
---|
141 | enum PlotType { |
---|
142 | PT_GC_RATIO, |
---|
143 | PT_GA_RATIO, |
---|
144 | PT_RATE, |
---|
145 | PT_TT_RATIO, |
---|
146 | PT_MOST_FREQUENT_BASE, |
---|
147 | PT_SECOND_FREQUENT_BASE, |
---|
148 | PT_THIRD_FREQUENT_BASE, |
---|
149 | PT_LEAST_FREQUENT_BASE, |
---|
150 | PT_BASE_A, |
---|
151 | PT_BASE_C, |
---|
152 | PT_BASE_G, |
---|
153 | PT_BASE_TU, |
---|
154 | PT_HELIX, |
---|
155 | |
---|
156 | PT_PLOT_TYPES, |
---|
157 | PT_UNKNOWN |
---|
158 | }; |
---|
159 | |
---|
160 | static const char *plotTypeName[PT_PLOT_TYPES] = { |
---|
161 | "gc_gnu", |
---|
162 | "ga_gnu", |
---|
163 | "rate_gnu", |
---|
164 | "tt_gnu", |
---|
165 | "f1_gnu", |
---|
166 | "f2_gnu", |
---|
167 | "f3_gnu", |
---|
168 | "f4_gnu", |
---|
169 | "a_gnu", |
---|
170 | "c_gnu", |
---|
171 | "g_gnu", |
---|
172 | "tu_gnu", |
---|
173 | "helix_gnu" |
---|
174 | }; |
---|
175 | |
---|
176 | static const char *plotTypeDescription[PT_PLOT_TYPES] = { |
---|
177 | "G+C ratio", |
---|
178 | "G+A ratio", |
---|
179 | "Rate", |
---|
180 | "TT Ratio", |
---|
181 | "Most frequent base", |
---|
182 | "2nd frequent base", |
---|
183 | "3rd frequent base", |
---|
184 | "Least frequent base", |
---|
185 | "A ratio", |
---|
186 | "C ratio", |
---|
187 | "G ratio", |
---|
188 | "T/U ratio", |
---|
189 | "Helix" |
---|
190 | }; |
---|
191 | |
---|
192 | static PlotType string2PlotType(const char *type) { |
---|
193 | for (int pt = 0; pt<PT_PLOT_TYPES; ++pt) { |
---|
194 | if (strcmp(type, plotTypeName[pt]) == 0) { |
---|
195 | return PlotType(pt); |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | return PT_UNKNOWN; |
---|
200 | } |
---|
201 | |
---|
202 | static const char *makeTitle(const char *fname) { |
---|
203 | const char *rslash = strrchr(fname, '/'); |
---|
204 | if (rslash) rslash++; |
---|
205 | else rslash = fname; |
---|
206 | |
---|
207 | char *name = strdup(rslash); |
---|
208 | char *rdot = strrchr(name, '.'); |
---|
209 | |
---|
210 | PlotType pt = PT_UNKNOWN; |
---|
211 | if (rdot) pt = string2PlotType(rdot+1); |
---|
212 | |
---|
213 | static char *title = 0; |
---|
214 | freenull(title); |
---|
215 | |
---|
216 | if (pt == PT_UNKNOWN) { |
---|
217 | title = GBS_global_string_copy("%s (unknown type)", name); |
---|
218 | } |
---|
219 | else { |
---|
220 | rdot[0] = 0; |
---|
221 | title = GBS_global_string_copy("%s (%s)", name, plotTypeDescription[pt]); |
---|
222 | } |
---|
223 | |
---|
224 | free(name); |
---|
225 | |
---|
226 | return title; |
---|
227 | } |
---|
228 | |
---|
229 | // ------------------- |
---|
230 | // SortedFreq |
---|
231 | |
---|
232 | class SortedFreq : virtual Noncopyable { |
---|
233 | float *freq[4]; |
---|
234 | |
---|
235 | public: |
---|
236 | SortedFreq(const ColumnStat *column_stat); |
---|
237 | ~SortedFreq(); |
---|
238 | |
---|
239 | float get(PlotType plot_type, size_t pos) const { |
---|
240 | float f; |
---|
241 | switch (plot_type) { |
---|
242 | case PT_MOST_FREQUENT_BASE: f = freq[0][pos]; break; |
---|
243 | case PT_SECOND_FREQUENT_BASE: f = freq[1][pos]; break; |
---|
244 | case PT_THIRD_FREQUENT_BASE: f = freq[2][pos]; break; |
---|
245 | case PT_LEAST_FREQUENT_BASE: f = freq[3][pos]; break; |
---|
246 | default: nt_assert(0); f = 0; break; |
---|
247 | } |
---|
248 | return f; |
---|
249 | } |
---|
250 | }; |
---|
251 | |
---|
252 | SortedFreq::SortedFreq(const ColumnStat *column_stat) { |
---|
253 | size_t len = column_stat->get_length(); |
---|
254 | for (int i = 0; i<4; ++i) { // 4 best frequencies |
---|
255 | freq[i] = new float[len]; |
---|
256 | for (size_t p = 0; p<len; ++p) freq[i][p] = 0.0; // clear |
---|
257 | } |
---|
258 | |
---|
259 | for (unsigned int c = 0; c<256; ++c) { // all character stats |
---|
260 | const float *cfreq = column_stat->get_frequencies((unsigned char)c); |
---|
261 | if (cfreq) { |
---|
262 | for (size_t p = 0; p<len; ++p) { // all positions |
---|
263 | if (freq[3][p] < cfreq[p]) { |
---|
264 | freq[3][p] = cfreq[p]; // found higher freq |
---|
265 | |
---|
266 | for (int i = 3; i > 0; --i) { // bubble upwards to sort |
---|
267 | if (freq[i-1][p] >= freq[i][p]) break; // sorted! |
---|
268 | |
---|
269 | float f = freq[i][p]; |
---|
270 | freq[i][p] = freq[i-1][p]; |
---|
271 | freq[i-1][p] = f; |
---|
272 | } |
---|
273 | } |
---|
274 | } |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | #if defined(DEBUG) |
---|
279 | for (size_t p = 0; p<len; ++p) { // all positions |
---|
280 | nt_assert(freq[0][p] >= freq[1][p]); |
---|
281 | nt_assert(freq[1][p] >= freq[2][p]); |
---|
282 | nt_assert(freq[2][p] >= freq[3][p]); |
---|
283 | } |
---|
284 | #endif // DEBUG |
---|
285 | } |
---|
286 | SortedFreq::~SortedFreq() { |
---|
287 | for (int i = 0; i<4; ++i) delete [] freq[i]; |
---|
288 | } |
---|
289 | |
---|
290 | class Smoother : virtual Noncopyable { |
---|
291 | double *value; |
---|
292 | size_t *index; |
---|
293 | size_t size; |
---|
294 | size_t halfsize; |
---|
295 | size_t next; |
---|
296 | size_t added; |
---|
297 | double sum; |
---|
298 | |
---|
299 | size_t wrap(size_t idx) { return idx >= size ? idx-size : idx; } |
---|
300 | |
---|
301 | public: |
---|
302 | Smoother(size_t smooth_range) |
---|
303 | : size(smooth_range), |
---|
304 | halfsize(size/2), |
---|
305 | next(0), |
---|
306 | added(0), |
---|
307 | sum(0.0) |
---|
308 | { |
---|
309 | nt_assert(size>0); |
---|
310 | |
---|
311 | value = new double[size]; |
---|
312 | index = new size_t[size]; |
---|
313 | |
---|
314 | for (size_t i = 0; i<size; ++i) value[i] = 0.0; |
---|
315 | } |
---|
316 | ~Smoother() { |
---|
317 | delete [] value; |
---|
318 | delete [] index; |
---|
319 | } |
---|
320 | |
---|
321 | void print(FILE *out, size_t i, double v) { |
---|
322 | sum = sum+v-value[next]; |
---|
323 | |
---|
324 | index[next] = i; |
---|
325 | value[next] = v; |
---|
326 | |
---|
327 | next = wrap(next+1); |
---|
328 | |
---|
329 | if (added<size) ++added; |
---|
330 | if (added == size) { |
---|
331 | size_t printNext = wrap(next+halfsize); |
---|
332 | fprintf(out, "%zu %f\n", index[printNext], sum/size); |
---|
333 | } |
---|
334 | } |
---|
335 | }; |
---|
336 | |
---|
337 | static void colstat_2_gnuplot_cb(AW_window *aww, AW_CL cl_column_stat, AW_CL cl_mode) { |
---|
338 | // cl_mode = 0 -> write file |
---|
339 | // cl_mode = 1 -> write file and run gnuplot |
---|
340 | // cl_mode = 2 -> delete all files with same prefix |
---|
341 | |
---|
342 | GB_transaction ta(GLOBAL.gb_main); |
---|
343 | ColumnStat *column_stat = (ColumnStat *)cl_column_stat; |
---|
344 | GB_ERROR error = 0; |
---|
345 | int mode = int(cl_mode); |
---|
346 | |
---|
347 | if (mode != 2) { |
---|
348 | char *filterstring = aww->get_root()->awar(AWAR_CS2GP_FILTER_FILTER)->read_string(); |
---|
349 | char *alignment_name = aww->get_root()->awar(AWAR_CS2GP_FILTER_ALIGNMENT)->read_string(); |
---|
350 | long alignment_length = GBT_get_alignment_len(GLOBAL.gb_main, alignment_name); |
---|
351 | |
---|
352 | if (alignment_length<0) { |
---|
353 | GB_clear_error(); |
---|
354 | error = "Please select a valid alignment"; |
---|
355 | } |
---|
356 | else { |
---|
357 | AP_filter filter(filterstring, "0", alignment_length); |
---|
358 | |
---|
359 | free(alignment_name); |
---|
360 | free(filterstring); |
---|
361 | |
---|
362 | error = column_stat->calculate(&filter); |
---|
363 | |
---|
364 | if (!error && !column_stat->get_length()) error = "Please select column statistic"; |
---|
365 | } |
---|
366 | } |
---|
367 | |
---|
368 | if (!error) { |
---|
369 | char *fname = aww->get_root()->awar(AWAR_CS2GP_FILENAME)->read_string(); |
---|
370 | |
---|
371 | if (!strchr(fname, '/')) freeset(fname, GBS_global_string_copy("./%s", fname)); |
---|
372 | if (strlen(fname) < 1) error = "Please enter file name"; |
---|
373 | |
---|
374 | if (mode == 2) { // delete overlay files |
---|
375 | if (!error) { |
---|
376 | char *found_files = get_overlay_files(aww->get_root(), fname, error); |
---|
377 | |
---|
378 | if (found_files) { |
---|
379 | for (char *name = strtok(found_files, "*"); name; name = strtok(0, "*")) { |
---|
380 | printf("Deleting gnuplot file '%s'\n", name); |
---|
381 | if (unlink(name) != 0) printf("Can't delete '%s'\n", name); |
---|
382 | } |
---|
383 | free(found_files); |
---|
384 | aww->get_root()->awar(AWAR_CS2GP_DIRECTORY)->touch(); // reload file selection box |
---|
385 | } |
---|
386 | } |
---|
387 | } |
---|
388 | else { |
---|
389 | FILE *out = 0; |
---|
390 | if (!error) { |
---|
391 | out = fopen(fname, "w"); |
---|
392 | if (!out) error = GB_export_errorf("Cannot write to file '%s'", fname); |
---|
393 | } |
---|
394 | |
---|
395 | nt_assert(out || error); |
---|
396 | |
---|
397 | if (!error) { |
---|
398 | char *type = aww->get_root()->awar(AWAR_CS2GP_SUFFIX)->read_string(); |
---|
399 | long smoothSize = aww->get_root()->awar(AWAR_CS2GP_SMOOTH_VALUES)->read_int(); // 1 = discrete values |
---|
400 | size_t columns = column_stat->get_length(); |
---|
401 | |
---|
402 | enum { |
---|
403 | STAT_AMOUNT, |
---|
404 | STAT_SIMPLE_FLOAT, |
---|
405 | STAT_SIMPLE_BOOL, |
---|
406 | STAT_SORT, |
---|
407 | STAT_UNKNOWN |
---|
408 | } stat_type = STAT_UNKNOWN; |
---|
409 | union { |
---|
410 | struct { |
---|
411 | const float *A; |
---|
412 | const float *C; |
---|
413 | const float *G; |
---|
414 | const float *TU; |
---|
415 | } amount; // STAT_AMOUNT |
---|
416 | const float *floatVals; // STAT_SIMPLE_FLOAT |
---|
417 | const bool *boolVals; // STAT_SIMPLE_BOOL |
---|
418 | SortedFreq *sorted; // STAT_SORT |
---|
419 | } data; |
---|
420 | |
---|
421 | PlotType plot_type = string2PlotType(type); |
---|
422 | switch (plot_type) { |
---|
423 | case PT_GC_RATIO: |
---|
424 | case PT_GA_RATIO: |
---|
425 | case PT_BASE_A: |
---|
426 | case PT_BASE_C: |
---|
427 | case PT_BASE_G: |
---|
428 | case PT_BASE_TU: { |
---|
429 | stat_type = STAT_AMOUNT; |
---|
430 | |
---|
431 | data.amount.A = column_stat->get_frequencies('A'); |
---|
432 | data.amount.C = column_stat->get_frequencies('C'); |
---|
433 | data.amount.G = column_stat->get_frequencies('G'); |
---|
434 | data.amount.TU = column_stat->get_frequencies('U'); |
---|
435 | break; |
---|
436 | } |
---|
437 | case PT_RATE: |
---|
438 | stat_type = STAT_SIMPLE_FLOAT; |
---|
439 | data.floatVals = column_stat->get_rates(); |
---|
440 | break; |
---|
441 | |
---|
442 | case PT_TT_RATIO: |
---|
443 | stat_type = STAT_SIMPLE_FLOAT; |
---|
444 | data.floatVals = column_stat->get_ttratio(); |
---|
445 | break; |
---|
446 | |
---|
447 | case PT_HELIX: { |
---|
448 | stat_type = STAT_SIMPLE_BOOL; |
---|
449 | data.boolVals = column_stat->get_is_helix(); |
---|
450 | break; |
---|
451 | } |
---|
452 | case PT_MOST_FREQUENT_BASE: |
---|
453 | case PT_SECOND_FREQUENT_BASE: |
---|
454 | case PT_THIRD_FREQUENT_BASE: |
---|
455 | case PT_LEAST_FREQUENT_BASE: { |
---|
456 | stat_type = STAT_SORT; |
---|
457 | data.sorted = new SortedFreq(column_stat); |
---|
458 | break; |
---|
459 | } |
---|
460 | case PT_PLOT_TYPES: |
---|
461 | case PT_UNKNOWN: |
---|
462 | error = "Please select what to plot"; |
---|
463 | break; |
---|
464 | } |
---|
465 | |
---|
466 | const GB_UINT4 *weights = column_stat->get_weights(); |
---|
467 | |
---|
468 | if (!error) { |
---|
469 | Smoother smoother(smoothSize); |
---|
470 | |
---|
471 | for (size_t j=0; j<columns; ++j) { |
---|
472 | if (!weights[j]) continue; |
---|
473 | |
---|
474 | double val = 0; |
---|
475 | switch (stat_type) { |
---|
476 | case STAT_AMOUNT: { |
---|
477 | float A = data.amount.A[j]; |
---|
478 | float C = data.amount.C[j]; |
---|
479 | float G = data.amount.G[j]; |
---|
480 | float TU = data.amount.TU[j]; |
---|
481 | |
---|
482 | float amount = A+C+G+TU; |
---|
483 | |
---|
484 | switch (plot_type) { |
---|
485 | case PT_GC_RATIO: val = (G+C)/amount; break; |
---|
486 | case PT_GA_RATIO: val = (G+A)/amount; break; |
---|
487 | case PT_BASE_A: val = A/amount; break; |
---|
488 | case PT_BASE_C: val = C/amount; break; |
---|
489 | case PT_BASE_G: val = G/amount; break; |
---|
490 | case PT_BASE_TU: val = TU/amount; break; |
---|
491 | |
---|
492 | default: nt_assert(0); break; |
---|
493 | } |
---|
494 | break; |
---|
495 | } |
---|
496 | case STAT_SIMPLE_FLOAT: val = data.floatVals[j]; break; |
---|
497 | case STAT_SIMPLE_BOOL: val = data.boolVals[j]; break; |
---|
498 | case STAT_SORT: val = data.sorted->get(plot_type, j); break; |
---|
499 | |
---|
500 | case STAT_UNKNOWN: nt_assert(0); break; |
---|
501 | } |
---|
502 | |
---|
503 | smoother.print(out, j, val); |
---|
504 | } |
---|
505 | } |
---|
506 | |
---|
507 | if (stat_type == STAT_SORT) delete data.sorted; |
---|
508 | free(type); |
---|
509 | } |
---|
510 | |
---|
511 | if (out) { |
---|
512 | fclose(out); |
---|
513 | out = NULL; |
---|
514 | } |
---|
515 | |
---|
516 | if (!error) { |
---|
517 | aww->get_root()->awar(AWAR_CS2GP_DIRECTORY)->touch(); // reload file selection box |
---|
518 | |
---|
519 | if (mode == 1) { // run gnuplot ? |
---|
520 | char *command_file; |
---|
521 | char *command_name = GB_unique_filename("arb", "gnuplot"); |
---|
522 | |
---|
523 | out = GB_fopen_tempfile(command_name, "wt", &command_file); |
---|
524 | if (!out) error = GB_await_error(); |
---|
525 | else { |
---|
526 | char *smooth = aww->get_root()->awar(AWAR_CS2GP_SMOOTH_GNUPLOT)->read_string(); |
---|
527 | char *found_files = get_overlay_files(aww->get_root(), fname, error); |
---|
528 | |
---|
529 | fprintf(out, "set samples 1000\n"); |
---|
530 | |
---|
531 | bool plotted = false; // set to true after first 'plot' command (other plots use 'replot') |
---|
532 | const char *plot_command[] = { "plot", "replot" }; |
---|
533 | |
---|
534 | if (found_files) { |
---|
535 | for (char *name = strtok(found_files, "*"); name; name = strtok(0, "*")) { |
---|
536 | if (strcmp(name, fname) != 0) { // latest data file is done below |
---|
537 | fprintf(out, "%s \"%s\" %s title \"%s\"\n", plot_command[int(plotted)], name, smooth, makeTitle(name)); |
---|
538 | plotted = true; |
---|
539 | } |
---|
540 | } |
---|
541 | free(found_files); |
---|
542 | } |
---|
543 | |
---|
544 | fprintf(out, "%s \"%s\" %s title \"%s\"\n", plot_command[int(plotted)], fname, smooth, makeTitle(fname)); |
---|
545 | fprintf(out, "pause mouse any \"Any key or button will terminate gnuplot\"\n"); |
---|
546 | fclose(out); |
---|
547 | out = 0; |
---|
548 | |
---|
549 | if (mode == 1) { |
---|
550 | char *script = GBS_global_string_copy("gnuplot %s && rm -f %s", command_file, command_file); |
---|
551 | GB_xcmd(script, true, true); |
---|
552 | free(script); |
---|
553 | } |
---|
554 | else { |
---|
555 | nt_assert(mode == 2); |
---|
556 | GB_unlink_or_warn(command_file, &error); |
---|
557 | } |
---|
558 | free(smooth); |
---|
559 | } |
---|
560 | free(command_file); |
---|
561 | free(command_name); |
---|
562 | } |
---|
563 | } |
---|
564 | } |
---|
565 | free(fname); |
---|
566 | } |
---|
567 | |
---|
568 | if (error) aw_message(error); |
---|
569 | } |
---|
570 | |
---|
571 | static void colstat_ali_changed_cb(AW_root *root) { |
---|
572 | if (GLOBAL.gb_main) { |
---|
573 | long smooth_max = GBT_get_alignment_len(GLOBAL.gb_main, root->awar(AWAR_CS2GP_FILTER_ALIGNMENT)->read_char_pntr()); |
---|
574 | if (smooth_max<0) { // ali not found |
---|
575 | GB_clear_error(); |
---|
576 | smooth_max = 1; // min=max -> do not limit |
---|
577 | } |
---|
578 | root->awar(AWAR_CS2GP_SMOOTH_VALUES)->set_minmax(1, smooth_max); |
---|
579 | } |
---|
580 | } |
---|
581 | |
---|
582 | AW_window *NT_create_colstat_2_gnuplot_window(AW_root *root) { |
---|
583 | GB_transaction ta(GLOBAL.gb_main); |
---|
584 | |
---|
585 | AW_awar *awar_default_alignment = root->awar_string(AWAR_DEFAULT_ALIGNMENT, "", GLOBAL.gb_main); |
---|
586 | ColumnStat *column_stat = new ColumnStat(GLOBAL.gb_main, root, AWAR_CS2GP_NAME, awar_default_alignment); |
---|
587 | AW_window_simple *aws = new AW_window_simple; |
---|
588 | |
---|
589 | aws->init(root, "EXPORT_CSP_TO_GNUPLOT", "Export Column statistic to GnuPlot"); |
---|
590 | aws->load_xfig("cpro/csp_2_gnuplot.fig"); |
---|
591 | |
---|
592 | root->awar_int(AWAR_CS2GP_SMOOTH_VALUES, 1); |
---|
593 | root->awar_int(AWAR_CS2GP_GNUPLOT_OVERLAY_POSTFIX); |
---|
594 | root->awar_int(AWAR_CS2GP_GNUPLOT_OVERLAY_PREFIX); |
---|
595 | root->awar_string(AWAR_CS2GP_SMOOTH_GNUPLOT); |
---|
596 | |
---|
597 | root->awar_string(AWAR_CS2GP_FILTER_NAME); |
---|
598 | root->awar_string(AWAR_CS2GP_FILTER_FILTER); |
---|
599 | AW_awar *awar_ali = root->awar_string(AWAR_CS2GP_FILTER_ALIGNMENT); |
---|
600 | awar_ali->map(AWAR_DEFAULT_ALIGNMENT); // use current alignment for filter |
---|
601 | awar_ali->add_callback(colstat_ali_changed_cb); |
---|
602 | awar_ali->touch(); |
---|
603 | |
---|
604 | AW_create_fileselection_awars(root, AWAR_CS2GP, "", ".gc_gnu", "noname.gc_gnu"); |
---|
605 | |
---|
606 | aws->at("close"); aws->callback((AW_CB0)AW_POPDOWN); |
---|
607 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
608 | |
---|
609 | aws->at("help"); aws->callback(makeHelpCallback("csp_2_gnuplot.hlp")); |
---|
610 | aws->create_button("HELP", "HELP", "H"); |
---|
611 | |
---|
612 | AW_create_standard_fileselection(aws, AWAR_CS2GP); |
---|
613 | |
---|
614 | aws->at("csp"); |
---|
615 | COLSTAT_create_selection_list(aws, column_stat); |
---|
616 | |
---|
617 | aws->at("what"); |
---|
618 | AW_selection_list *plotTypeList = aws->create_selection_list(AWAR_CS2GP_SUFFIX, true); |
---|
619 | for (int pt = 0; pt<PT_PLOT_TYPES; ++pt) { |
---|
620 | plotTypeList->insert(plotTypeDescription[pt], plotTypeName[pt]); |
---|
621 | } |
---|
622 | plotTypeList->insert_default("<select one>", ""); |
---|
623 | plotTypeList->update(); |
---|
624 | |
---|
625 | adfiltercbstruct *filter = awt_create_select_filter(root, GLOBAL.gb_main, AWAR_CS2GP_FILTER_NAME); |
---|
626 | aws->at("ap_filter"); |
---|
627 | aws->callback(makeCreateWindowCallback(awt_create_select_filter_win, filter)); |
---|
628 | aws->create_button("SELECT_FILTER", AWAR_CS2GP_FILTER_NAME); |
---|
629 | |
---|
630 | aws->at("smooth"); |
---|
631 | aws->create_input_field(AWAR_CS2GP_SMOOTH_VALUES, 8); |
---|
632 | |
---|
633 | aws->at("smooth2"); |
---|
634 | aws->create_toggle_field(AWAR_CS2GP_SMOOTH_GNUPLOT, 1); |
---|
635 | aws->insert_default_toggle("None", "N", ""); |
---|
636 | aws->insert_toggle("Unique", "U", "smooth unique"); |
---|
637 | aws->insert_toggle("CSpline", "S", "smooth cspline"); |
---|
638 | aws->insert_toggle("Bezier", "B", "smooth bezier"); |
---|
639 | aws->update_toggle_field(); |
---|
640 | |
---|
641 | aws->auto_space(10, 10); |
---|
642 | aws->button_length(13); |
---|
643 | |
---|
644 | aws->at("save"); |
---|
645 | aws->callback(colstat_2_gnuplot_cb, (AW_CL)column_stat, (AW_CL)0); |
---|
646 | aws->create_button("SAVE", "Save"); |
---|
647 | |
---|
648 | aws->highlight(); |
---|
649 | aws->callback(colstat_2_gnuplot_cb, (AW_CL)column_stat, (AW_CL)1); |
---|
650 | aws->create_button("SAVE_AND_VIEW", "Save & View"); |
---|
651 | |
---|
652 | aws->at("overlay1"); |
---|
653 | aws->label("Overlay statistics with same prefix?"); |
---|
654 | aws->create_toggle(AWAR_CS2GP_GNUPLOT_OVERLAY_PREFIX); |
---|
655 | |
---|
656 | aws->at("overlay2"); |
---|
657 | aws->label("Overlay statistics with same postfix?"); |
---|
658 | aws->create_toggle(AWAR_CS2GP_GNUPLOT_OVERLAY_POSTFIX); |
---|
659 | |
---|
660 | aws->at("del_overlays"); |
---|
661 | aws->callback(colstat_2_gnuplot_cb, (AW_CL)column_stat, (AW_CL)2); |
---|
662 | aws->create_autosize_button("DEL_OVERLAYS", "Delete currently overlayed files", "D", 2); |
---|
663 | |
---|
664 | return (AW_window *)aws; |
---|
665 | } |
---|
666 | |
---|
667 | |
---|