-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbarplot4structure.m
268 lines (232 loc) · 10.6 KB
/
barplot4structure.m
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
function [sumout] = barplot4structure(C, sortbyq, classin)
% Population Genetics and Evolution Toolbox (PGEToolbox)
% Author: James Cai
% (c) Texas A&M University
%
% $LastChangedDate: 2013-01-06 13:39:38 -0600 (Sun, 06 Jan 2013) $
% $LastChangedRevision: 331 $
% $LastChangedBy: jcai $
if nargin < 3
classin = [];
end
sumout.memo = {'C--Inferred clusters', 'idvnames--names of individuals', ...
'sortidx--order when sorted by Q', 'LnP--Estimated Ln Prob of Data'};
if nargin < 2
sortbyq = 1;
end
if nargin < 1
disp('Using example C...');
%Inferred clusters
C = [0.002, 0.000, 0.002, 0.001, 0.994, 0.000, 0.001; ...
0.001, 0.000, 0.001, 0.000, 0.996, 0.000, 0.001; ...
0.003, 0.000, 0.001, 0.001, 0.990, 0.001, 0.005; ...
0.001, 0.000, 0.001, 0.001, 0.997, 0.000, 0.000; ...
0.001, 0.001, 0.001, 0.001, 0.997, 0.000, 0.001; ...
0.001, 0.000, 0.001, 0.001, 0.996, 0.000, 0.001; ...
0.001, 0.000, 0.001, 0.001, 0.997, 0.000, 0.001; ...
0.008, 0.000, 0.073, 0.006, 0.911, 0.000, 0.001; ...
0.033, 0.000, 0.004, 0.003, 0.954, 0.001, 0.004; ...
0.010, 0.000, 0.002, 0.001, 0.986, 0.000, 0.002; ...
0.003, 0.000, 0.001, 0.001, 0.994, 0.000, 0.001; ...
0.001, 0.000, 0.000, 0.000, 0.997, 0.000, 0.001; ...
0.064, 0.000, 0.017, 0.009, 0.908, 0.001, 0.001; ...
0.016, 0.000, 0.001, 0.000, 0.980, 0.001, 0.001; ...
0.155, 0.000, 0.038, 0.001, 0.804, 0.001, 0.001; ...
0.048, 0.000, 0.035, 0.002, 0.914, 0.000, 0.001; ...
0.002, 0.000, 0.001, 0.002, 0.992, 0.001, 0.002; ...
0.002, 0.002, 0.058, 0.004, 0.934, 0.000, 0.000; ...
0.011, 0.001, 0.001, 0.002, 0.982, 0.001, 0.002; ...
0.010, 0.000, 0.001, 0.000, 0.986, 0.001, 0.001; ...
0.001, 0.000, 0.004, 0.000, 0.993, 0.000, 0.001; ...
0.009, 0.000, 0.002, 0.000, 0.988, 0.000, 0.001; ...
0.002, 0.000, 0.000, 0.000, 0.996, 0.000, 0.001; ...
0.003, 0.001, 0.012, 0.004, 0.977, 0.002, 0.002; ...
0.010, 0.002, 0.002, 0.001, 0.982, 0.001, 0.002; ...
0.003, 0.001, 0.003, 0.002, 0.986, 0.002, 0.003; ...
0.001, 0.001, 0.213, 0.003, 0.779, 0.001, 0.002; ...
0.001, 0.001, 0.195, 0.001, 0.801, 0.000, 0.001; ...
0.001, 0.001, 0.006, 0.002, 0.985, 0.000, 0.004; ...
0.004, 0.002, 0.001, 0.001, 0.987, 0.001, 0.004; ...
0.017, 0.012, 0.003, 0.008, 0.957, 0.000, 0.003; ...
0.001, 0.000, 0.001, 0.001, 0.996, 0.000, 0.001; ...
0.001, 0.000, 0.001, 0.001, 0.995, 0.001, 0.001; ...
0.002, 0.001, 0.001, 0.001, 0.992, 0.000, 0.003; ...
0.997, 0.000, 0.001, 0.001, 0.001, 0.000, 0.001; ...
0.993, 0.000, 0.003, 0.001, 0.001, 0.000, 0.002; ...
0.997, 0.000, 0.000, 0.001, 0.000, 0.000, 0.001; ...
0.997, 0.000, 0.001, 0.000, 0.000, 0.000, 0.001; ...
0.993, 0.000, 0.002, 0.002, 0.001, 0.000, 0.001; ...
0.996, 0.000, 0.001, 0.001, 0.001, 0.000, 0.001; ...
0.994, 0.000, 0.001, 0.001, 0.003, 0.001, 0.001; ...
0.976, 0.000, 0.015, 0.001, 0.004, 0.002, 0.001; ...
0.995, 0.000, 0.001, 0.000, 0.002, 0.000, 0.001; ...
0.973, 0.000, 0.008, 0.001, 0.001, 0.000, 0.015; ...
0.975, 0.001, 0.013, 0.001, 0.006, 0.001, 0.004; ...
0.922, 0.000, 0.002, 0.001, 0.073, 0.000, 0.001; ...
0.953, 0.000, 0.001, 0.001, 0.039, 0.001, 0.005; ...
0.012, 0.000, 0.985, 0.001, 0.001, 0.001, 0.001; ...
0.002, 0.000, 0.996, 0.001, 0.001, 0.000, 0.001; ...
0.090, 0.001, 0.906, 0.001, 0.001, 0.000, 0.001; ...
0.116, 0.001, 0.745, 0.002, 0.132, 0.001, 0.004; ...
0.002, 0.001, 0.802, 0.000, 0.193, 0.000, 0.001; ...
0.004, 0.000, 0.821, 0.001, 0.164, 0.001, 0.009; ...
0.012, 0.000, 0.969, 0.001, 0.015, 0.000, 0.003; ...
0.007, 0.001, 0.689, 0.001, 0.302, 0.001, 0.001; ...
0.058, 0.000, 0.496, 0.001, 0.443, 0.001, 0.002; ...
0.007, 0.000, 0.716, 0.001, 0.274, 0.000, 0.001; ...
0.762, 0.000, 0.001, 0.001, 0.234, 0.000, 0.001; ...
0.991, 0.000, 0.002, 0.001, 0.004, 0.001, 0.001; ...
0.997, 0.001, 0.000, 0.000, 0.001, 0.000, 0.001; ...
0.997, 0.000, 0.001, 0.000, 0.000, 0.000, 0.001; ...
0.996, 0.000, 0.001, 0.001, 0.001, 0.000, 0.001; ...
0.996, 0.000, 0.001, 0.001, 0.001, 0.000, 0.000; ...
0.983, 0.008, 0.001, 0.001, 0.001, 0.007, 0.000; ...
0.996, 0.001, 0.001, 0.001, 0.001, 0.000, 0.001; ...
0.996, 0.000, 0.001, 0.001, 0.001, 0.000, 0.001; ...
0.997, 0.000, 0.001, 0.000, 0.000, 0.000, 0.001; ...
0.979, 0.001, 0.005, 0.004, 0.007, 0.002, 0.003; ...
0.001, 0.001, 0.728, 0.009, 0.259, 0.001, 0.001; ...
0.001, 0.000, 0.120, 0.003, 0.000, 0.000, 0.875; ...
0.001, 0.000, 0.104, 0.001, 0.000, 0.001, 0.893; ...
0.012, 0.001, 0.006, 0.005, 0.003, 0.002, 0.971; ...
0.005, 0.000, 0.009, 0.001, 0.002, 0.000, 0.983; ...
0.001, 0.000, 0.001, 0.001, 0.001, 0.005, 0.991; ...
0.001, 0.000, 0.000, 0.000, 0.001, 0.000, 0.997; ...
0.001, 0.001, 0.001, 0.000, 0.001, 0.000, 0.995; ...
0.016, 0.001, 0.004, 0.002, 0.048, 0.011, 0.918; ...
0.006, 0.001, 0.120, 0.002, 0.001, 0.000, 0.870; ...
0.087, 0.001, 0.004, 0.001, 0.002, 0.001, 0.904; ...
0.001, 0.000, 0.000, 0.000, 0.000, 0.000, 0.997; ...
0.001, 0.000, 0.000, 0.000, 0.000, 0.000, 0.998; ...
0.994, 0.000, 0.001, 0.001, 0.002, 0.000, 0.001; ...
0.996, 0.000, 0.001, 0.000, 0.001, 0.000, 0.001; ...
0.005, 0.115, 0.007, 0.868, 0.005, 0.000, 0.001; ...
0.003, 0.000, 0.001, 0.994, 0.001, 0.000, 0.000; ...
0.078, 0.001, 0.002, 0.916, 0.001, 0.001, 0.001; ...
0.006, 0.001, 0.004, 0.965, 0.002, 0.001, 0.021; ...
0.005, 0.119, 0.005, 0.800, 0.023, 0.010, 0.038; ...
0.016, 0.127, 0.001, 0.846, 0.005, 0.005, 0.001; ...
0.001, 0.001, 0.196, 0.799, 0.001, 0.001, 0.001; ...
0.006, 0.078, 0.244, 0.653, 0.001, 0.001, 0.016; ...
0.001, 0.000, 0.000, 0.997, 0.000, 0.000, 0.001; ...
0.001, 0.000, 0.001, 0.912, 0.086, 0.000, 0.000; ...
0.054, 0.299, 0.004, 0.616, 0.001, 0.001, 0.024; ...
0.001, 0.333, 0.006, 0.650, 0.000, 0.008, 0.001; ...
0.977, 0.000, 0.001, 0.001, 0.018, 0.001, 0.003; ...
0.001, 0.000, 0.001, 0.000, 0.997, 0.000, 0.001; ...
0.995, 0.000, 0.000, 0.000, 0.002, 0.000, 0.001; ...
0.001, 0.000, 0.001, 0.000, 0.996, 0.000, 0.001; ...
0.995, 0.000, 0.002, 0.000, 0.001, 0.001, 0.001; ...
0.001, 0.000, 0.000, 0.000, 0.000, 0.998, 0.000; ...
0.000, 0.000, 0.000, 0.000, 0.000, 0.998, 0.000; ...
0.001, 0.000, 0.000, 0.000, 0.000, 0.998, 0.000; ...
0.001, 0.000, 0.000, 0.000, 0.000, 0.998, 0.000; ...
0.001, 0.000, 0.000, 0.000, 0.000, 0.998, 0.000; ...
0.000, 0.000, 0.000, 0.000, 0.000, 0.998, 0.000; ...
0.000, 0.000, 0.000, 0.000, 0.000, 0.998, 0.000; ...
0.000, 0.000, 0.000, 0.000, 0.000, 0.998, 0.000; ...
0.000, 0.000, 0.000, 0.000, 0.000, 0.998, 0.000; ...
0.001, 0.000, 0.001, 0.000, 0.001, 0.996, 0.000; ...
0.001, 0.000, 0.001, 0.000, 0.000, 0.998, 0.000; ...
0.039, 0.000, 0.957, 0.001, 0.001, 0.000, 0.001; ...
0.994, 0.000, 0.003, 0.001, 0.001, 0.000, 0.001; ...
0.970, 0.000, 0.001, 0.001, 0.025, 0.001, 0.001; ...
0.993, 0.001, 0.002, 0.001, 0.001, 0.000, 0.002; ...
0.993, 0.000, 0.002, 0.001, 0.002, 0.001, 0.001];
end
%%
if ischar(C)
filename = C; % 'structure_result.txt';
%[C]=readstructureoutput(filename);
disp(['Reading STRUCTURE output file ', filename, ' ...'])
txt = textread(filename, '%s', 'delimiter', '\n', 'whitespace', '');
idx1 = find(cellfun(@isempty, strfind(txt, 'Inferred ancestry of individuals:')) == 0);
idx2 = find(cellfun(@isempty, strfind(txt, 'Estimated Allele Frequencies in each cluster')) == 0);
idx3 = find(cellfun(@isempty, strfind(txt, 'Estimated Ln Prob of Data =')) == 0);
LnPtxt = txt{idx3};
LnPtxt = LnPtxt(31:end);
sumout.LnP = str2double(LnPtxt);
txt = txt(idx1+2:idx2-3);
C = [];
for k = 1:length(txt)
linetxt = txt{k};
linefrc = strread(linetxt(strfind(linetxt, ':')+2:end), '%f')';
[itemp, iname] = strread(linetxt(1:strfind(linetxt, '(')-1), '%d%s');
sumout.idvnames{k} = iname{1};
C = [C; linefrc];
end
%sumout.idvnames=
else
for k = 1:size(C, 1)
sumout.idvnames{k} = sprintf('idv_%d', k);
end
sumout.LnP = nan;
end
sumout.C = C;
for k = 1:length(sumout.idvnames)
sumout.classnames{k} = sumout.idvnames{k}(1:4);
end
if any(sum(C, 2) < 0.99)
error('Wrong input C (Inferred clusters)');
end
[n, K] = size(C);
sumout.K = K;
%figure;
%subplot(2,1,1)
if ~sortbyq
sumout.orderidx = 1:size(C, 1);
if nargout < 1
bar(C, 1, 'stack', 'edgecolor', 'none');
ylim([0, 1])
xlim([0, size(C, 1) + 1])
axis ij;
title(sprintf('K=%d; No. of samples=%d; Original order', K, n))
end
else
if ~isempty(classin)
[~, idx] = max(C, [], 2); % y is the idx of the max per row
[~, idx] = sort(idx);
C = C(idx, :);
classin = classin(idx);
[iclass, iclassname] = grp2idx(classin);
[x, ~] = max(C, [], 2);
else
[x, iclass] = max(C, [], 2); % y is the idx of the max per row
iclassname = sprintf('K%d|', 1:K);
end
[~, idx] = sortrows([iclass, x], [1, -2]);
sumout.orderidx = idx;
C = C(idx, :);
if nargout < 1
bar(C, 1, 'stack', 'edgecolor', 'none');
ylim([0, 1])
xlim([0, size(C, 1) + 1])
axis ij;
if ~isempty(classin)
title(sprintf('K=%d, lnPr(X|K)=%.2f, n=%d; Sort by (prior) populations', K, sumout.LnP, n))
else
title(sprintf('K=%d, lnPr(X|K)=%.2f, n=%d; Sort by Q', K, sumout.LnP, n))
end
%inames=sumout.idvnames(sumout.orderidx);
inames = sumout.idvnames;
seprt = [];
for sk = 1:length(unique(iclass))
idx = find(iclass == sk);
seprt = [seprt, length(idx)];
iname = inames(idx);
%xname{sk}=iname;
fprintf('%d\t', sk);
for nk = 1:length(iname)
fprintf('%s ', iname{nk});
end
fprintf('\n');
end
sprt = cumsum(seprt);
vline(sprt+0.5, 'w-')
%sprt
%iclassname
if ~isempty(classin)
set(gca, 'XTick', sprt-0.5, 'XTickLabel', iclassname)
end
end
end