about summary refs log tree commit diff
path: root/sourcecodes/parameter_learning/createSVG.m
blob: 47894c1bbdc0d885109fca37d5c0812be565027a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
function  [ ] = createSVG( pre )
   %  This function will create a JSON file for network visualization.
   %
   %  Input: 
   %   1) prestructure_input.txt
   %    Structure file.
   %   2) prestructure_input_temp.txt
   %    Structure file with model averaging scores.
   %   3) pregrviz_name_file.txt
   %    Node names in graph viz file.   
   %   4) pregraphviz.txt
   %    Original graphviz input file.   
   %
   %  Output: 
   %   pregraphviz_svg.txt-- Modified graphviz input file.
   %


%  open file for input, include error handling
dfile=strcat(pre,'structure_input.txt');

fin = fopen(dfile,'r');
if fin < 0
   error(['Could not open ',dfile,' for input']);
end

% Read in first line to get the number of nodes and the node labels.
buffer = fgetl(fin);    %get header line as a string
nnodes = numel(strfind(buffer,"\t")) + 1;
labels = cell(1,nnodes);
for j=1:nnodes
    [next,buffer] = strtok(buffer);
    labels{j} = next;
end


% Read in the edges
edges = cell(nnodes,nnodes);
for i = 1:nnodes
    buffer = fgetl(fin);
    for j = 1:nnodes
         [next,buffer] = strtok(buffer);
         edges{i,j} = next;
    end
end


% open file to read in model averaging scores
dfile2=strcat(pre,'structure_input_temp.txt');

%If it does not exist, then just read in the regular structure file
%%   as the model averaging score file
if exist(dfile2, 'file') != 2
   dfile2 = dfile;
   frewind(dfile2);
end

if (exist(dfile2) == 0)
  scores = cell(nnodes,nnodes);
  for i = 1:nnodes
    for j = 1:nnodes
      scores{i,j} = edges{i,j};
    end
  end
else
  fin2 = fopen(dfile2,'r');
  if fin2 < 0
    error(['Could not open ',dfile2,' for input']);
  end

  buffer = fgetl(fin2);    %get header line as a string

% Read in the scores
  scores = cell(nnodes,nnodes);
  for i = 1:nnodes
    buffer = fgetl(fin2);
    for j = 1:nnodes
         [next,buffer] = strtok(buffer);
         scores{i,j} = next;
    end
  end
end

% open file to get positions of variables
dfile3=strcat(pre,'grviz_name_file.txt');

fin3 = fopen(dfile3,'r');
if fin3 < 0
   error(['Could not open ',dfile3,' for input']);
end

grphviz_names = cell(nnodes);
for j=1:nnodes
    [next,buffer] = strtok(buffer);
    grphviz_names{j} = next;
end


nedges = 0;
sources = [];
targets = [];
scores1 = [];
for i = 1:nnodes
  for j = 1:nnodes
    if edges{i,j} == "1"
      nedges = nedges + 1;
      sources = [sources; i]; 
      targets = [targets; j]; 
      scores1 = [scores1; str2num(scores{i,j})]; 
    end
  end
end


outfile = strcat(pre,'graphviz_svg.txt');
fout = fopen(outfile,'w');
fprintf(fout,"digraph \"\" {\n");
%fprintf(fout,"size=\"10,10\"; ratio = fill;\n");
fprintf(fout,"size=\"10,10\"; remincross = true;\n");
fprintf(fout,"node [shape=box, fontsize=18, margin=\"0.05,0\", height=0.3, rounded=true];\n");
fprintf(fout,"edge [fontsize=12];\n");
for i = 1:nedges
  fprintf(fout,"\"%s\" -> \"%s\" [ label=\"%3.2f\", penwidth=\"%3.2f\" ];\n",labels{sources(i)},labels{targets(i)},scores1(i),2*scores1(i));
end
fprintf(fout,"}/n");
fclose(fout);


outfile2 = strcat(pre,'graphviz_svg_no_edge.txt');
fout2 = fopen(outfile2,'w');
fprintf(fout2,"digraph \"\" {\n");
%fprintf(fout,"size=\"10,10\"; ratio = fill;\n");
fprintf(fout2,"size=\"10,10\"; remincross = true;\n");
fprintf(fout2,"node [shape=box, fontsize=18, margin=\"0.05,0\", height=0.3,rounded=true];\n");
fprintf(fout2,"edge [fontsize=12];\n");
for i = 1:nedges
  fprintf(fout,"\"%s\" -> \"%s\" [ penwidth=\"%3.2f\" ];\n",labels{sources(i)},labels{targets(i)},2*scores1(i));
end
fprintf(fout2,"}/n");
fclose(fout2);



end