Debian Codegraph @

src/abpoa_plot.c

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "abpoa.h"
5 #include "abpoa_graph.h"
6 #include "utils.h"
7
8 /* example of dot file for graphviz */
9 /*
10 digraph test1 {
11 a -> b -> c;
12 a -> {x y};
13 b [shape=box];
14 c [label="hello\nworld",color=blue,fontsize=24,
15 fontname="Palatino-Italic",fontcolor=red,style=filled];
16 a -> z [label="hi", weight=100];
17 x -> z [label="multi-line\nlabel"];
18 edge [style=dashed,color=red];
19 b -> x;
20 {rank=same; b x}
21 }
22 graph test2 {
23 a -- b -- c [style=dashed];
24 a -- {x y};
25 x -- c [w=10.0];
26 x -- y [w=5.0,len=3];
27 }
28 */
29
30
31 extern char ab_nt256_table[256];
32 // base (index, rank, node_id)
33 // A (1, 1, 2) A: base 1: index 1: rank 2: node_id
34 void abpoa_dump_pog(abpoa_t *ab, abpoa_para_t *abpt) {
35 char PROG[20] = "abpoa"; int font_size=24;
36
37 abpoa_graph_t *abg = ab->abg;
38 if (abg->is_topological_sorted == 0) abpoa_topological_sort(abg, abpt);
39
40 // all settings
41 // char node_color[5][10] = {"purple3", "red3", "seagreen4", "gold2", "gray"}; // ACGTN
42 char node_color[5][10] = {"pink1", "red1", "gold2", "seagreen4", "gray"}; // ACGTN
43 // float dpi_size = 3000, graph_width = 100, graph_height = 6;
44 float node_width=1;
45 char rankdir[5] = "LR", node_style[10]="filled", node_fixedsize[10]="true", node_shape[10]="circle";
46 int show_aligned_mismatch = 1;
47
48 int i, j, id, index, out_id; char base;
49 char **node_label = (char**)_err_malloc(abg->node_n * sizeof(char*));
50 for (i = 0; i < abg->node_n; ++i) node_label[i] = (char*)_err_malloc(sizeof(char) * 128);
51
52 char *dot_fn = (char*)malloc(strlen(abpt->out_pog) + 10);
53 strcpy(dot_fn, abpt->out_pog);
54 FILE *fp = xopen(strcat(dot_fn, ".dot"), "w");
55 fprintf(fp, "// %s graph dot file.\n// %d nodes.\n", PROG, abg->node_n);
56 // fprintf(fp, "digraph ABPOA_graph {\n\tgraph [dpi=%f]; size=\"%f,%f\";\n\trankdir=\"%s\";\n\tnode [width=%f, style=%s, fixedsize=%s, shape=%s];\n", dpi_size, graph_width, graph_height, rankdir, node_width, node_style, node_fixedsize, node_shape);
57 fprintf(fp, "digraph ABPOA_graph {\n\tgraph [rankdir=\"%s\"];\n\tnode [width=%f, style=%s, fixedsize=%s, shape=%s];\n", rankdir, node_width, node_style, node_fixedsize, node_shape);
58
59 for (i = 0; i < abg->node_n; ++i) {
60 id = abpoa_graph_index_to_node_id(abg, i);
61 index = i;
62 if (id == ABPOA_SRC_NODE_ID) {
63 base = 'S';
64 //sprintf(node_label[id], "\"%c\n(%d,%d,%d)\"", base, index, rank, id);
65 // only show seq
66 sprintf(node_label[id], "\"%c\n%d\"", base,index);
67 fprintf(fp, "%s [color=%s, fontsize=%d]\n", node_label[id], node_color[4], font_size);
68 } else if (id == ABPOA_SINK_NODE_ID) {
69 base = 'E';
70 //sprintf(node_label[id], "\"%c\n(%d,%d,%d)\"", base, index, rank, id);
71 // only show seq
72 sprintf(node_label[id], "\"%c\n%d\"", base,index);
73 fprintf(fp, "%s [color=%s, fontsize=%d]\n", node_label[id], node_color[4], font_size);
74 } else {
75 base = ab_nt256_table[abg->node[id].base];
76 //sprintf(node_label[id], "\"%c\n(%d,%d,%d)\"", base, index, rank, id);
77 // only show seq
78 sprintf(node_label[id], "\"%c\n%d\"", base,index);
79 fprintf(fp, "%s [color=%s, fontsize=%d]\n", node_label[id], node_color[abg->node[id].base], font_size);
80 }
81 }
82 int x_index = -1;
83 for (i = 0; i < abg->node_n; ++i) {
84 id = abpoa_graph_index_to_node_id(abg, i);
85 // out_edge
86 for (j = 0; j < abg->node[id].out_edge_n; ++j) {
87 out_id = abg->node[id].out_id[j];
88 fprintf(fp, "\t%s -> %s [label=\"%d\", fontsize=20, fontcolor=red, penwidth=%d]\n", node_label[id], node_label[out_id], abg->node[id].out_edge_weight[j], abg->node[id].out_edge_weight[j]+1);
89 }
90 if (abg->node[id].aligned_node_n > 0) {
91 fprintf(fp, "\t{rank=same; %s ", node_label[id]);
92 for (j = 0; j < abg->node[id].aligned_node_n; ++j)
93 fprintf(fp, "%s ", node_label[abg->node[id].aligned_node_id[j]]);
94 fprintf(fp, "};\n");
95 if (show_aligned_mismatch) {
96 if (i > x_index) {
97 x_index = i;
98 // mismatch dashed line
99 fprintf(fp, "\t{ edge [style=dashed, arrowhead=none]; %s ", node_label[id]);
100 for (j = 0; j < abg->node[id].aligned_node_n; ++j) {
101 fprintf(fp, "-> %s ", node_label[abg->node[id].aligned_node_id[j]]);
102 index = abpoa_graph_node_id_to_index(abg, abg->node[id].aligned_node_id[j]);
103 x_index = index > x_index ? index : x_index;
104 }
105 fprintf(fp, "}\n");
106 }
107 }
108 }
109 }
110 fprintf(fp, "}\n");
111
112 for (i = 0; i < abg->node_n; ++i) free(node_label[i]); free(node_label);
113 err_fclose(fp);
114
115 char cmd[1024];
116 char *type = strrchr(abpt->out_pog, '.');
117 if (strcmp(type+1, "pdf") != 0 && strcmp(type+1, "png") != 0)
118 err_fatal_simple("POG can only be dump to .pdf/.png file");
119 sprintf(cmd, "dot %s -T%s > %s", dot_fn, type+1, abpt->out_pog);
120 free(dot_fn);
121 if (system(cmd) != 0) err_fatal(__func__, "Fail to plot %s DAG.", PROG);
122 }