-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsg_motl_batch_tube.m
237 lines (181 loc) · 6.45 KB
/
sg_motl_batch_tube.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
function sg_motl_batch_tube(tomolist_name,motl_name,radlist_name,metadata_type,binning,l_dist,c_dist,padding,subset_list,helical_step)
%% sg_motl_batch_tube
% A function to batch generate motivelists for a set of tubes. Input data
% is parsed from a TOMOMAN tomolist and metadata type.
%
% The metadata subfolder should contain .em files, each containg tube
% centers and radii, as defined by Kun Qu's "pick particle" Chimera plugin.
%
% "binning" is the binning of the input files
%
% "l_dist" is the inter-particle along the length of the tube.
%
% "c_dist" is the inter-particle along the circumference of the tube.
%
% "rand_phi" randomizes the in-plane phi angles.
%
% "padding" removes positions that are within a certain number of voxels
% from the tomogram edges.
%
% "subset_list" is the name of an input plain-text file for a list of
% tomograms to work on. The list should contain the tomo_num of the
% tomograms to use.
%
% "helical_step" defines rotation between lateral rings on the tube
% surface.
%
%
% WW 08-2022
% % % %%%% DEBUG
% tomolist_name = 'tomolist.mat';
% motl_name = 'motl/tomo2_init_tube_2.star';
% radlist_name = 'motl/tomo2_init_radlist_2.txt';
% metadata_type = 'init_tube_2';
% binning = 4;
% l_dist = 4;
% c_dist = 3;
% padding = 32;
% subset_list = [];
%% Check check
% Check for subset list
if nargin < 9
subset = [];
elseif isempty(subset_list)
subset = [];
else
% Read subset list
subset = dlmread(subset_list);
end
% Check for helical rotation
if nargin <= 9
helical_step = [];
end
%% Initalize
% Read tomolist
tomolist = tm_read_tomolist([],tomolist_name);
n_tomos = numel(tomolist);
% Cell to hold motl from each tomogram
tomo_cell = cell(n_tomos,1);
tomo_cell_idx = false(n_tomos,1);
% Cell to hold radii for each tomogram
rad_cell = cell(n_tomos,1);
%% Generate spheres for each tomogram
m_idx_start = 1;
subtomo_num = 1;
% Loop through tomograms
for i = 1:n_tomos
% Check processing
process = true;
if tomolist(i).skip
process = false;
end
if ~isempty(subset)
if ~any(tomolist(i).tomo_num == subset)
process = false;
end
end
if ~process
continue
end
% Parse name of stack used for alignment
switch tomolist(i).alignment_stack
case 'unfiltered'
process_stack = tomolist(i).stack_name;
case 'dose-filtered'
process_stack = tomolist(i).dose_filtered_stack_name;
otherwise
error([p.name,'ACTHUNG!!! Unsuppored stack!!! Only "unfiltered" and "dose-filtered" supported!!!']);
end
[~,stack_name,~] = fileparts(process_stack);
disp(['Generating motivelist for ',stack_name,'...']);
% Parse center files
try
cen_idx = find(endsWith(tomolist(i).metadata.(metadata_type),'.em'));
catch
warning(['ACHTUNG!!! ',stack_name,' contains no .em files!!! Skipping to next tomogram...']);
continue
end
n_cen_files = numel(cen_idx);
% Read in center files
cen_cell = cell(n_cen_files,1);
t = 1; % Tube index counter
for j = 1:n_cen_files
% Read in center file
cen_name = [tomolist(i).stack_dir,'metadata/',metadata_type,'/',tomolist(i).metadata.(metadata_type){cen_idx(j)}];
cen_cell{j} = sg_emread(cen_name);
% Parse tube indices
tube_id = unique(cen_cell{j}(2,:));
% Update indices within tomogram
for k = 1:numel(tube_id)
temp_idx = cen_cell{j}(2,:) == tube_id(k);
cen_cell{j}(2,temp_idx) = t;
t = t+1;
end
end
% Concatenate centers
cens = [cen_cell{:}];
n_tubes = t-1;
% Initialize temporary motl for tomo
tube_cell = cell(n_tubes,1);
% Initialize temporary array for radii
tube_rad = zeros(n_tubes,3);
tube_rad(:,1) = tomolist(i).tomo_num;
for j = 1:n_tubes
% Parse tube motivelist
tube_idx = cens(2,:) == j;
tube_cens = cens(:,tube_idx);
% Parse and store radius
tube_rad(j,2) = j;
tube_rad(j,3) = tube_cens(3,1);
% Generate surface positions
temp_motl = sg_motl_generate_tube_function(cens(8:10,tube_idx),l_dist,c_dist,tube_rad(j,3),helical_step);
n_temp_motl = numel(temp_motl.motl_idx);
% Fill subtomo_num
temp_motl.subtomo_num = int32(subtomo_num:(subtomo_num + n_temp_motl-1))';
% Increment counter
subtomo_num = subtomo_num + n_temp_motl;
% Fill object number
temp_motl.object = ones(size(temp_motl.object),'int32').*j;
% Store motl
tube_cell{j} = temp_motl;
% %%% DEBUG
% debug_em = zeros(20,n_pos);
% debug_em(4,:) = 1:n_pos;
% debug_em(8:10,:) = cat(1,temp_motl.orig_x',temp_motl.orig_y',temp_motl.orig_z');
% debug_em(11:13,:) = cat(1,temp_motl.x_shift',temp_motl.y_shift',temp_motl.z_shift');
% debug_em(17:19,:) = cat(1,temp_motl.phi',temp_motl.psi',temp_motl.the');
% sg_emwrite('debug.em',debug_em);
end
% Concatenate and fill other fields
tomo_cell_idx(i) = true;
tomo_cell{i} = sg_motl_concatenate(false,tube_cell);
tomo_cell{i}.tomo_num = ones(size(tomo_cell{i}.tomo_num),'int32').*tomolist(i).tomo_num;
% Store radii
rad_cell{i} = tube_rad;
% Threshold list
dims = tm_parse_tomogram_dimensions(tomolist(i),binning);
tomo_cell{i} = sg_motl_check_tomo_edges(dims,tomo_cell{i},padding);
% % Concatenate and store tomo motl
% tomo_cell{i} = sg_motl_concatenate(false,tomo_motl);
%
% % Threshold list
% tomo_name = [tomo_dir,'/',num2str(tomo_num(i),fmt),'.mrc'];
% tomo_cell{i} = sg_motl_check_tomo_edges(tomo_name,tomo_cell{i},padding);
end
%% Generate full motivelist
% Remove empty cells
tomo_cell = tomo_cell(tomo_cell_idx);
rad_cell = rad_cell(tomo_cell_idx);
% Concatenate all tomos
allmotl = sg_motl_concatenate(false,tomo_cell);
n_motls = numel(allmotl.motl_idx);
% Fill remaining fields
allmotl.motl_idx = int32(1:n_motls)';
allmotl.subtomo_num = int32(1:n_motls)';
allmotl.class = ones(n_motls,1,'int32');
% Write motl
disp([num2str(n_motls),' motivelist entries generated...']);
sg_motl_write2(motl_name,allmotl);
% Write radii list
radii = vertcat(rad_cell{:}); % Concatenate radii
dlmwrite(radlist_name,radii);