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
145
146
147
148
|
function [ ] = modifyEdges( pre_old, pre_new )
% This function will allow users to add or delete edges from the network.
%
% Input:
% pre_oldstructure_input.txt-- old structure file that is just used to make sure the node order is consistent
% pre_oldmodify_edge.txt-- file with network structure after modification
%
% Output:
% pre_newstructure_input.txt-- structure file with edges added/deleted.
% pre_newstructure_input_temp.txt-- structure file with model averaging scores.
% September 16, 2020: I am only writing the file with model averaging scores
% if no edges were added to the network.
%
% open file for input, include error handling
dfile=strcat(pre_old,'structure_input.txt');
fin = fopen(dfile,'r');
if fin < 0
error(['Could not open ',dfile,' for input']);
end
% open file for input, include error handling
mfile=strcat(pre_old,'modify_edge.txt');
fin2 = fopen(mfile,'r');
if fin2 < 0
error(['Could not open ',mfile,' for input']);
end
% Read in first line to get the number of nodes
nnodes = str2num(fgetl(fin2));
% Read in first line of old structure file to get the node labels.
buffer = fgetl(fin); %get header line as a string
labels = cell(1,nnodes);
for j=1:nnodes
[next,buffer] = strtok(buffer);
labels{j} = next;
end
%Read in network structure information
buffer = fgetl(fin2);
buffer = buffer(2:end-1);
buffer = strrep(buffer,"\"","");
labels2 = cell(1,nnodes);
for j=1:nnodes
[next,buffer] = strtok(buffer,",");
labels2{j} = next;
end
nedges = str2num(fgetl(fin2));
buffer = fgetl(fin2);
buffer = buffer(2:end-1);
buffer = strrep(buffer,"\"","");
sources = cell(1,nedges);
for j=1:nedges
[next,buffer] = strtok(buffer,",");
sources{j} = str2num(next);
end
buffer = fgetl(fin2);
buffer = buffer(2:end-1);
buffer = strrep(buffer,"\"","");
targets = cell(1,nedges);
for j=1:nedges
[next,buffer] = strtok(buffer,",");
targets{j} = str2num(next);
end
buffer = fgetl(fin2);
buffer = buffer(2:end-1);
weights = cell(1,nedges);
for j=1:nedges
[next,buffer] = strtok(buffer,",");
weights{j} = next;
end
%label_map is the index in "labels" that corresponds to each label in "labels2"
label_map = cell(1,nnodes);
for i = 1:nnodes
current = labels2{i};
for j = 1:nnodes
if strcmp(current,labels{j})
label_map{i} = j;
endif
end
end
edges_out = cell(nnodes,nnodes);
scores_out = cell(nnodes,nnodes);
for i = 1:nedges
source_i = label_map{sources{i}};
target_i = label_map{targets{i}};
scores_out(source_i,target_i) = weights{i};
edges_out(source_i,target_i) = "1";
end
tf = cellfun('isempty',edges_out);
edges_out(tf) = {"0"};
scores_out(tf) = {"0"};
test_score = 1;
for i=1:nnodes
for j=1:nnodes
if strcmp(scores_out{i,j},"1.1")
test_score = 0;
end
end
end
if test_score == 1
outfile = strcat(pre_new,'structure_input_temp.txt');
fout = fopen(outfile,'w');
fprintf(fout,'%s\t',labels{1:end-1});
fprintf(fout,'%s\t\n',labels{end});
for i = 1:nnodes
fprintf(fout,'%s\t',scores_out{i,1:end-1});
fprintf(fout,'%s\t\n',scores_out{i,end});
end
fclose(fout);
end
outfile2 = strcat(pre_new,'structure_input.txt');
fout2 = fopen(outfile2,'w');
fprintf(fout2,'%s\t',labels{1:end-1});
fprintf(fout2,'%s\t\n',labels{end});
for i = 1:nnodes
fprintf(fout2,'%s\t',edges_out{i,1:end-1});
fprintf(fout2,'%s\t\n',edges_out{i,end});
end
fclose(fout2);
end
|