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