Repository navigation
Expand file tree
/
Copy pathFastTracker.m
More file actions
521 lines (443 loc) · 26.3 KB
/
Copy pathFastTracker.m
File metadata and controls
521 lines (443 loc) · 26.3 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
function FastTracker(filename,frame_rate,SPACE_UNITS,TIME_UNITS,minimum_track_length,every_n_frame,value_for_fitLogLogMSD,R2LIMIT,boundary_alpha_confined,boundary_alpha_directed)
%% Custom script to calculate the speed, velocities, distances, displacement, RoG and MSD of tracked objects in Imaris
% This script takes the Imaris export csv file where all the measurement
% are listed time point after time point and re-shuffle them into collated
% columns with one track per column
% This code allows the user to select a folder of csv files and then the
% column inside the csv file that will be collated into a single
% spreadsheet with one file per column.
%% Typical values for the user variables
% filename = 'Repeat 3 - YTS - Position.csv';
% frame_rate = 0.011; % Time interval betweent the frames in second
% SPACE_UNITS = 'µm'; % Space unit in which the measurements are imported
% TIME_UNITS = 's'; % Time unit in which the measurements are imported
% minimum_track_length = 100; % Threshold for including a track in the analysis (this is a frame number, not a duration!)
% every_n_frame = 1; % To use if you want to change the sampling rate
% value_for_fitLogLogMSD = 0.25; % This is the portion of the MSD curve used for the fit
% R2LIMIT = 0.75; % Threshold value for what is considered a good fit
% boundary_alpha_confined = 0.25; % Limit for alpha value between confined and diffusion
% boundary_alpha_directed = 1.5; % Limit for alpha value between diffusion and directed
%% Cleanup
close all
home
diary([filename,'_',mat2str(frame_rate),'_',mat2str(every_n_frame),'_',mat2str(value_for_fitLogLogMSD),'_',mat2str(R2LIMIT),'_log.txt']);
diary on
%% Loading and various parameters
% Header for the POSITION import table is expected as follows:
% Position X Position Y Position Z Unit Category Collection Birth [s] Death [s] TrackID ID OriginalID Original Component Name Original Component ID Original Image Name Original Image ID
% Convert as:
% {'PositionX','PositionY','PositionZ','Unit','Category','Collection','Birth','Death','TrackID','ID','OriginalID','OriginalComponentName','OriginalComponentID','OriginalImageName','OriginalImageID'}
%POOL = parpool('local',8);
tic
disp(' ');
disp('FastTracker - v1.1 - 2017-02-23');
disp(' ');
disp('Parameters for this analysis:');
disp([' Unit for space: ',SPACE_UNITS]);
disp([' Unit for time: ',TIME_UNITS]);
disp([' Frame rate: ',mat2str(frame_rate)]);
disp([' Sampling every nth frame: ',mat2str(every_n_frame)]);
disp([' Minimum track length: ',mat2str(minimum_track_length)]);
disp([' Maximum boundary for alpha value for confined: ',mat2str(boundary_alpha_confined)]);
disp([' Minimum boundary for alpha value for directed: ',mat2str(boundary_alpha_directed)]);
disp([' Portion of the MSD curve used for fitting: ',mat2str(value_for_fitLogLogMSD)]);
disp([' R2 limit for including the MSD fits in the analysis: ',mat2str(R2LIMIT)]);
disp(' ');
disp('Loading source csv file');
% [filename, path, ~] = uigetfile('.csv');
path = [pwd,'/'];
delimiter = ',';
startRow = 5; % note that sometimes, Imaris adds a padding line on the top!? So start at 5 if textscan encounters an error
formatSpec = '%f%f%f%s%s%s%f%f%s%s%s%s%s%s%s%[^\n\r]'; % Important: 'PositionX','PositionY','PositionZ','Birth','Death' are imported as a number, rest as strings
fileID = fopen([path filename],'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines' ,startRow-1, 'ReturnOnError', false);
fclose(fileID);
Extracted_data = table(dataArray{1:end-1}, 'VariableNames', {'PositionX','PositionY','PositionZ','Unit','Category','Collection','Birth','Death','TrackID','ID','OriginalID','OriginalComponentName','OriginalComponentID','OriginalImageName','OriginalImageID'});
disp(['File: ', filename]);
disp(['Total number of points: ', mat2str(height(Extracted_data))]);
clearvars delimiter startRow formatSpec fileID dataArray ans;
[~,filename,~] = fileparts(filename);
%% Location to save the files
%pathname = uigetdir('~/Desktop/', 'Directory where csv files will be stored');
%% Foolproofing the TrackID in case they are not unique by merging OriginalImageName and TrackID into TruelyUniqueName (16th column)
C_single = cell(size(Extracted_data,1),1); % Pre-allocation for speed
C_multiple = [Extracted_data.OriginalImageName Extracted_data.TrackID];
diary off
textprogressbar('Foolproofing the TrackIDs: ');
indicator_progress = 0;
for hh = 1:size(C_multiple,1)
C_single{hh,1} = strjoin(C_multiple(hh,:));
indicator_progress = indicator_progress + (100/size(C_multiple,1));
textprogressbar(indicator_progress);
end
textprogressbar(' Done');
diary on
C_single_table = cell2table(C_single,'VariableNames',{'TruelyUniqueName'});
Extracted_data = [Extracted_data C_single_table];
clear C_* hh indicator_progress
%% Creating a smaller table with only a few columns: {'PositionX','PositionY','PositionZ'}
disp('Extracting XYZ positions array');
Extracted_limited_data = table(Extracted_data.PositionX,Extracted_data.PositionY,Extracted_data.PositionZ,'VariableNames',{'X','Y','Z'});
%% Separate the main table into cells using their TruelyUniqueName
[TUN, ~, TUN_idx] = unique(Extracted_data(:,16),'stable');
disp(['Number of tracks: ', mat2str(height(TUN))]);
unique_idx = accumarray(TUN_idx(:),(1:length(TUN_idx))',[],@(x) {sort(x)});
diary off
textprogressbar('Re-constructing the tracks: ');
indicator_progress = 0;
all_the_tracks_all_values = cell(height(TUN),1); % Pre-allocation for speed
all_the_tracks_time_position = cell(height(TUN),1); % Pre-allocation for speed
for jj = 1:height(TUN)
temporary_table_1 = Extracted_data(unique_idx{jj},:);
timeline_1 = array2table(linspace(0,(height(temporary_table_1)-1)*frame_rate,height(temporary_table_1))','VariableNames',{'Time'});
temporary_table_2 = [timeline_1 temporary_table_1];
all_the_tracks_all_values{jj} = temporary_table_2;
temporary_table_3 = table2array(Extracted_limited_data(unique_idx{jj},:));
timeline_2 = linspace(0,(height(temporary_table_2)-1)*frame_rate,height(temporary_table_2))';
temporary_table_4 = [timeline_2 temporary_table_3];
all_the_tracks_time_position{jj} = temporary_table_4;
clear temporary_table_* timeline_*
indicator_progress = indicator_progress + (100/height(TUN));
textprogressbar(indicator_progress);
end
textprogressbar(' Done');
diary on
clear jj indicator_progress unique_idx TUN*
%% Adding a threshold parameter for the minimum number of frames per track and plot the distribution
disp(['Minimum track duration: ', mat2str(minimum_track_length), ' frames (', mat2str(minimum_track_length*frame_rate),' ',TIME_UNITS,').']);
index_minimum_length = false(length(all_the_tracks_all_values),1); % Pre-allocation for speed
track_duration = zeros(length(all_the_tracks_all_values),1); % Pre-allocation for speed
diary off
textprogressbar('Removing all the short tracks: ');
indicator_progress = 0;
for i=1:length(all_the_tracks_all_values)
index_minimum_length(i,1) = (height(all_the_tracks_all_values{i,1}) > minimum_track_length);
track_duration(i,1) = height(all_the_tracks_all_values{i,1});
indicator_progress = indicator_progress + (100/length(all_the_tracks_all_values));
textprogressbar(indicator_progress);
end
textprogressbar(' Done');
diary on
mini_length_tracks_all_values = all_the_tracks_all_values(index_minimum_length);
mini_length_tracks_time_position = all_the_tracks_time_position(index_minimum_length);
disp([mat2str((length(all_the_tracks_all_values)-length(mini_length_tracks_all_values))),' tracks out of ', mat2str(length(all_the_tracks_all_values)),' were deleted. ',mat2str(length(mini_length_tracks_all_values)),' tracks left.']);
disp(['Longest track is ', mat2str(max(track_duration)),' frames long (', mat2str(max(track_duration)*frame_rate),' ',TIME_UNITS,').']);
clear i indicator_progress index_minimum_length
%% Re-sampling the acquisition frequency (temporary use of skipping_* variables but results back into mini_length_* at the end
skipping_tracks_all_values = cell(length(mini_length_tracks_all_values),1); % Pre-allocation for speed
skipping_tracks_time_position = cell(length(mini_length_tracks_time_position),1); % Pre-allocation for speed
for track = 1:length(mini_length_tracks_all_values)
skipping_tracks_all_values{track,1} = mini_length_tracks_all_values{track,1}(1:every_n_frame:end,:);
skipping_tracks_time_position{track,1} = mini_length_tracks_time_position{track,1}(1:every_n_frame:end,:);
end
clear mini_length_tracks_*
mini_length_tracks_all_values = skipping_tracks_all_values;
mini_length_tracks_time_position = skipping_tracks_time_position;
clear skipping_tracks_*
%% Calculating additional parameters using only the X,Y,Z positions and time interval
diary off
disp('Pre-allocation');
textprogressbar('Progress: ');
indicator_progress = 0;
for track = 1:length(mini_length_tracks_all_values)
nrow = size(mini_length_tracks_all_values{track},1);
mini_length_tracks_all_values{track}.PTPDistance = zeros(nrow, 1);
mini_length_tracks_all_values{track}.CumulativeDistance = zeros(nrow, 1);
mini_length_tracks_all_values{track}.Displacement = zeros(nrow, 1);
mini_length_tracks_all_values{track}.InstantVelocity = zeros(nrow, 1);
mini_length_tracks_all_values{track}.CumulativeSpeed = zeros(nrow, 1);
mini_length_tracks_all_values{track}.AverageSpeed = zeros(nrow, 1);
mini_length_tracks_all_values{track}.RoG_ComponentX = zeros(nrow, 1);
mini_length_tracks_all_values{track}.RoG_ComponentY = zeros(nrow, 1);
mini_length_tracks_all_values{track}.RoG_ComponentZ = zeros(nrow, 1);
mini_length_tracks_all_values{track}.RadiusOfGyration2 = zeros(nrow, 1);
indicator_progress = indicator_progress + (100/length(mini_length_tracks_all_values));
textprogressbar(indicator_progress);
end
textprogressbar(' Done');
clear indicator_progress track nrow
disp('Computing distances, displacement and velocities...');
diary off
textprogressbar('Progress: ');
indicator_progress = 0;
for track = 1:length(mini_length_tracks_all_values)
for position = 2:(height(mini_length_tracks_all_values{track}))
mini_length_tracks_all_values{track}.PTPDistance(position) = sqrt((mini_length_tracks_all_values{track}.PositionX(position)-mini_length_tracks_all_values{track}.PositionX(position-1))^2 + (mini_length_tracks_all_values{track}.PositionY(position)-mini_length_tracks_all_values{track}.PositionY(position-1))^2 + (mini_length_tracks_all_values{track}.PositionZ(position)-mini_length_tracks_all_values{track}.PositionZ(position-1))^2);
mini_length_tracks_all_values{track}.CumulativeDistance(position) = mini_length_tracks_all_values{track}.PTPDistance(position) + mini_length_tracks_all_values{track}.CumulativeDistance(position-1);
mini_length_tracks_all_values{track}.Displacement(position) = sqrt((mini_length_tracks_all_values{track}.PositionX(end)-mini_length_tracks_all_values{track}.PositionX(1))^2 + (mini_length_tracks_all_values{track}.PositionY(end)-mini_length_tracks_all_values{track}.PositionY(1))^2 + (mini_length_tracks_all_values{track}.PositionZ(end)-mini_length_tracks_all_values{track}.PositionZ(1))^2);
mini_length_tracks_all_values{track}.InstantVelocity(position) = mini_length_tracks_all_values{track}.PTPDistance(position) / (frame_rate * every_n_frame);
mini_length_tracks_all_values{track}.CumulativeSpeed(position) = mini_length_tracks_all_values{track}.CumulativeDistance(position) / (frame_rate * every_n_frame * (position-1));
end
indicator_progress = indicator_progress + (100/length(mini_length_tracks_all_values));
textprogressbar(indicator_progress);
end
textprogressbar(' Done');
clear indicator_progress track position nrow
diary on
disp('Computing components for radius of gyration...');
diary off
textprogressbar('Progress: ');
indicator_progress = 0;
for track = 1:length(mini_length_tracks_all_values)
for position = 1:(height(mini_length_tracks_all_values{track}))
mini_length_tracks_all_values{track}.RoG_ComponentX(position) = (mini_length_tracks_all_values{track}.PositionX(position) - mean(mini_length_tracks_all_values{track}.PositionX(:)))^2;
mini_length_tracks_all_values{track}.RoG_ComponentY(position) = (mini_length_tracks_all_values{track}.PositionY(position) - mean(mini_length_tracks_all_values{track}.PositionY(:)))^2;
mini_length_tracks_all_values{track}.RoG_ComponentZ(position) = (mini_length_tracks_all_values{track}.PositionZ(position) - mean(mini_length_tracks_all_values{track}.PositionZ(:)))^2;
end
indicator_progress = indicator_progress + (100/length(mini_length_tracks_all_values));
textprogressbar(indicator_progress);
end
textprogressbar(' Done');
clear indicator_progress track position nrow
disp('Computing radius of gyration^2 and average speed...');
textprogressbar('Progress: ');
indicator_progress = 0;
for track = 1:length(mini_length_tracks_all_values)
mini_length_tracks_all_values{track}.AverageSpeed(:) = mean(mini_length_tracks_all_values{track}.InstantVelocity(:));
mini_length_tracks_all_values{track}.RadiusOfGyration2(:) = ( sum(mini_length_tracks_all_values{track}.RoG_ComponentX(:)) + sum(mini_length_tracks_all_values{track}.RoG_ComponentY(:)) + sum(mini_length_tracks_all_values{track}.RoG_ComponentZ(:))) / height(mini_length_tracks_all_values{track});
indicator_progress = indicator_progress + (100/length(mini_length_tracks_all_values));
textprogressbar(indicator_progress);
end
textprogressbar(' Done');
diary on
clear indicator_progress track position nrow
%% @msdanalyzer class loading and computing
ma = msdanalyzer(3, 'µm', 's'); % Build a new MSD analyzer object.
ma = ma.addAll(mini_length_tracks_time_position); % Load tracks to msdanalyzer.
% Compute drift
%
% ma = ma.computeDrift('velocity');
% ma.plotDrift
% ma.labelPlotTracks
% filesave = [filename,'_',mat2str(frame_rate),'_',mat2str(every_n_frame),'_',mat2str(value_for_fitLogLogMSD),'_',mat2str(R2LIMIT),'_drift.png'];
% print('-painters','-dpng',filesave);
% close all
ma = ma.computeMSD; % Compute MSD
disp(['Fitting of MSD is currently set to be done with a factor of ',mat2str(((max(track_duration)*frame_rate/(1*frame_rate))*value_for_fitLogLogMSD)/every_n_frame),' between smallest delay and the largest one.']);
if ((((max(track_duration)*frame_rate/(1*frame_rate))*value_for_fitLogLogMSD)/every_n_frame)<=1000)
disp('Power laws can only be determined reliably if they are sampled over at least 3 orders of magnitude.');
disp('This means that there must be at least a factor of 1000 between the smallest delay and the largest one.');
end
ma = ma.fitLogLogMSD(value_for_fitLogLogMSD); % Fit the individual MSD and store the alpha (slope), gamma (value at origin) and r2 (fitness) into the object
%% Alpha values and statistical analysis
disp('Extracting all the alpha/gamma/r2fit values from the MSD curves fit.');
alphas_MSD = ma.loglogfit.alpha;
%gammas_MSD = ma.loglogfit.gamma;
r2fits_MSD = ma.loglogfit.r2fit;
bad_fits = r2fits_MSD < R2LIMIT; % Remove bad fits
fprintf('Keeping %d fits out of %d (R2 > %.2f).\n', sum(~bad_fits), length(r2fits_MSD), R2LIMIT);
alphas_MSD(bad_fits) = [];
% [htest, pval] = ttest(alphas_MSD, 1, 0.05, 'left'); % T-test
% if ~htest
% [htest, pval] = ttest(alphas_MSD, 1, 0.05);
% end
%
% % Prepare string
% str = { [ '\alpha = ' sprintf('%.2f ± %.2f (mean ± std, N = %d)', mean(alphas_MSD), std(alphas_MSD), numel(alphas_MSD)) ] };
% if htest
% str{2} = sprintf('Significantly below 1, with p = %.2g', pval);
% else
% str{2} = sprintf('Not significantly different from 1, with p = %.2g', pval);
% end
% Creating indexes according to the alpha value and the goodness of the fit of the MSD
index_confined_MSD = ma.loglogfit.alpha < boundary_alpha_confined & ma.loglogfit.r2fit > R2LIMIT; % Confined
index_diffusive_MSD = boundary_alpha_confined <= ma.loglogfit.alpha & ma.loglogfit.alpha <= boundary_alpha_directed & ma.loglogfit.r2fit > R2LIMIT; % Diffusive
index_directed_MSD = ma.loglogfit.alpha > boundary_alpha_directed & ma.loglogfit.r2fit > R2LIMIT; % Directed
index_bad_fits = ma.loglogfit.r2fit <= R2LIMIT; % Bad fits
% Data preparation for the plots
array_categories_plot = [sum(index_confined_MSD) sum(index_diffusive_MSD) sum(index_directed_MSD)]; % For the "Mode of Motion" plot
average_speeds = zeros(length(mini_length_tracks_all_values),1); % Pre-allocation for the Granule velocity (mean) plot
radius_of_gyration2 = zeros(length(mini_length_tracks_all_values),1); % Pre-allocation for the Radius of gyration plot
for track = 1:length(mini_length_tracks_all_values)
average_speeds(track,1) = mini_length_tracks_all_values{track}.AverageSpeed(1); % For the Granule velocity (mean) plot
radius_of_gyration2(track,1) = mini_length_tracks_all_values{track}.RadiusOfGyration2(1); % For the Radius of gyration plot
end
clear htest pval bad_fits
%% Compute velocity autocorrelations
% ma = ma.computeVCorr; % Compute velocity autocorrelation
%% Plotting part 1
figure1 = figure('rend','painters','pos',[800 300 2000 1000]);
subplot(2,7,[1 2])
hold on
for track = 1:length(mini_length_tracks_all_values)
plot(mini_length_tracks_all_values{track}.PositionX,mini_length_tracks_all_values{track}.PositionY)
end
hold off
box on
axis equal
axis square
title('Trajectories','FontSize', 16,'FontName','Arial')
xlabel(SPACE_UNITS,'FontName','Arial')
ylabel(SPACE_UNITS,'FontName','Arial')
subplot(2,7,3)
hold on
plotSpread(average_speeds);
boxplot(average_speeds);
hold off
title('Granule velocity (mean)','FontSize', 16,'FontName','Arial')
ylabel([SPACE_UNITS,'/',TIME_UNITS],'FontName','Arial')
ylim([0,0.5]);
set(gca,'XTickLabel','')
subplot(2,7,4);
hold on
plotSpread(radius_of_gyration2);
boxplot(radius_of_gyration2);
hold off
title('Radius of gyration','FontSize', 16,'FontName','Arial')
ylabel([SPACE_UNITS,'^2'],'FontName','Arial')
ylim([0,0.15]);
set(gca,'XTickLabel','')
subplot(2,7,[5 7])
hold on
rectangle('position',[0 0 0.25 1],'facecolor',[0.729411780834198 0.831372559070587 0.95686274766922]);
rectangle('position',[0.25 0 1.25 1],'facecolor',[0.756862759590149 0.866666674613953 0.776470601558685]);
rectangle('position',[1.5 0 0.5 1],'facecolor',[0.925490200519562 0.839215695858002 0.839215695858002]);
histogram(alphas_MSD,40,'Normalization','probability');
xlabel('\alpha','FontName','Arial')
ylabel('Frequency','FontName','Arial')
% text(0.6, 0.14, str, ...
% 'HorizontalAlignment', 'left', ...
% 'VerticalAlignment', 'bottom', ...
% 'FontSize', 14)
xlim([0,2]);
ylim([0,0.25]);
title('\alpha values distribution','FontSize', 16,'FontName','Arial')
hold off
box on;
subplot(2,7,[8 9])
hold on
rectangle('position',[0 0.00001 minimum_track_length 1],'facecolor',[0.9 0.8 0.8]);
histogram(track_duration,100,'Normalization','probability');
box on;
axes1=gca;
set(axes1,'YMinorTick','on','YScale','log');
line([minimum_track_length minimum_track_length],get(axes1,'YLim'),'Color',[1 0 0],'LineWidth',2)
hold off
title('Distribution of track lengths','FontSize', 16,'FontName','Arial')
xlabel('Duration (Number of frames)','FontName','Arial')
ylabel('Frequency (log scale)','FontName','Arial')
subplot(2,7,[10 11]);
histogram(average_speeds,40,'Normalization','probability')
title('Distribution of mean granule velocity','FontSize', 16,'FontName','Arial')
xlabel(['Speed (',SPACE_UNITS,'/',TIME_UNITS,')'],'FontName','Arial');
ylabel('Frequency','FontName','Arial');
xlim([0,0.5]);
subplot(2,7,[12 13]);
histogram(radius_of_gyration2,40,'Normalization','probability')
title('Distribution of radius of gyration','FontSize', 16,'FontName','Arial')
xlabel(['Radius of gyration (',SPACE_UNITS,'^2)'],'FontName','Arial');
ylabel('Frequency','FontName','Arial')
xlim([0,0.15]);
subplot(2,7,14)
bar(array_categories_plot,'FaceColor',[0.4 0.67 0.84]);
box on
label_modes = {'Confined','Diffusive','Directed'};
set( gca(), 'XTickLabel', label_modes, 'FontName','Arial')
rotateXLabels( gca(), 45 )
ylabel('Count','FontName','Arial')
title('Modes of motion','FontSize', 16,'FontName','Arial')
suptitle([filename,' - ',mat2str(frame_rate),' - ',mat2str(every_n_frame),' - ',mat2str(value_for_fitLogLogMSD),' - ',mat2str(R2LIMIT)]);
filesave = [filename,'_',mat2str(frame_rate),'_',mat2str(every_n_frame),'_',mat2str(value_for_fitLogLogMSD),'_',mat2str(R2LIMIT),'_fig1.eps'];
print('-painters','-depsc','-loose',filesave);
filesave = [filename,'_',mat2str(frame_rate),'_',mat2str(every_n_frame),'_',mat2str(value_for_fitLogLogMSD),'_',mat2str(R2LIMIT),'_fig1.png'];
print('-painters','-dpng',filesave);
close(figure1)
clear track filesave str axes1 label_modes figure1
%% Plotting part 2
% Plotting and curve fitting
figure2 = figure('rend','painters','pos',[800 300 2000 900]);
subplot(2,5,1)
[~, ha1] = ma.plotMSD; % Plot the mean square displacement curves.
ma.labelPlotMSD(ha1); % A convenience method to set the axes labels.
xlim(ha1,[0 10]); ylim(ha1,[0.0001 1]);
set(gca,'YMinorTick','on','XScale','log','YScale','log');
title('MSD for all tracks','FontSize', 16,'FontName','Arial'); box on; axis square
if sum(index_bad_fits) ~= 0 % Required because if the index is empty, plotMSD plot ALL the MSD values by default!
subplot(2,5,2)
[~, ha2] = ma.plotMSD(gca,find(index_bad_fits)); % Plot the mean square displacement curves.
ma.labelPlotMSD(ha2); % A convenience method to set the axes labels.
xlim(ha2,[0 10]); ylim(ha2,[0.0001 1]);
set(gca,'YMinorTick','on','XScale','log','YScale','log');
title(['MSD for tracks with r2<',mat2str(R2LIMIT)],'FontSize', 16,'FontName','Arial'); box on; axis square
end
if sum(index_confined_MSD) ~= 0 % Required because if the index is empty, plotMSD plot ALL the MSD values by default!
subplot(2,5,3)
[~, ha3] = ma.plotMSD(gca,find(index_confined_MSD)); % Plot the mean square displacement curves.
ma.labelPlotMSD(ha3); % A convenience method to set the axes labels.
xlim(ha3,[0 10]); ylim(ha3,[0.0001 1]);
set(gca,'YMinorTick','on','XScale','log','YScale','log');
title('MSD for tracks \alpha<0.25','FontSize', 16,'FontName','Arial'); box on; axis square
end
if sum(index_diffusive_MSD) ~= 0 % Required because if the index is empty, plotMSD plot ALL the MSD values by default!
subplot(2,5,4)
[~, ha4] = ma.plotMSD(gca,find(index_diffusive_MSD)); % Plot the mean square displacement curves.
ma.labelPlotMSD(ha4); % A convenience method to set the axes labels.
xlim(ha4,[0 10]); ylim(ha4,[0.0001 1]);
set(gca,'YMinorTick','on','XScale','log','YScale','log');
title('MSD for tracks 0.25<\alpha<1.5','FontSize', 16,'FontName','Arial'); box on; axis square
end
if sum(index_directed_MSD) ~= 0 % Required because if the index is empty, plotMSD plot ALL the MSD values by default!
subplot(2,5,5)
[~, ha5] = ma.plotMSD(gca,find(index_directed_MSD)); % Plot the mean square displacement curves.
ma.labelPlotMSD(ha5); % A convenience method to set the axes labels.
xlim(ha5,[0 10]); ylim(ha5,[0.0001 1]);
set(gca,'YMinorTick','on','XScale','log','YScale','log');
title('MSD for tracks \alpha>1.5','FontSize', 16,'FontName','Arial'); box on; axis square
end
subplot(2,5,6)
[~, ha6] = ma.plotMeanMSD(gca,true); % Plot the MEAN mean square displacement curves.
ma.labelPlotMSD(ha6); % A convenience method to set the axes labels.
xlim(ha6,[0 10]); ylim(ha6,[0.0001 1]);
set(gca,'YMinorTick','on','XScale','log','YScale','log');
title('Mean MSD for all tracks','FontSize', 16,'FontName','Arial'); box on; axis square
if sum(index_bad_fits) ~= 0 % Required because if the index is empty, plotMSD plot ALL the MSD values by default!
subplot(2,5,7)
[~, ha7] = ma.plotMeanMSD(gca,true,find(index_bad_fits)); % Plot the mean square displacement curves.
ma.labelPlotMSD(ha7); % A convenience method to set the axes labels.
xlim(ha7,[0 10]); ylim(ha7,[0.0001 1]);
set(gca,'YMinorTick','on','XScale','log','YScale','log');
title(['MSD for tracks with r2<',mat2str(R2LIMIT)],'FontSize', 16,'FontName','Arial'); box on; axis square
end
if sum(index_confined_MSD) ~= 0 % Required because if the index is empty, plotMSD plot ALL the MSD values by default!
subplot(2,5,8)
[~, ha8] = ma.plotMeanMSD(gca,true,find(index_confined_MSD)); % Plot the MEAN mean square displacement curves.
ma.labelPlotMSD(ha8); % A convenience method to set the axes labels.
xlim(ha8,[0 10]); ylim(ha8,[0.0001 1]);
set(gca,'YMinorTick','on','XScale','log','YScale','log');
title('Mean MSD for tracks \alpha<0.25','FontSize', 16,'FontName','Arial'); box on; axis square
end
if sum(index_diffusive_MSD) ~= 0 % Required because if the index is empty, plotMSD plot ALL the MSD values by default!
subplot(2,5,9)
[~, ha9] = ma.plotMeanMSD(gca,true,find(index_diffusive_MSD)); % Plot the MEAN mean square displacement curves.
ma.labelPlotMSD(ha9); % A convenience method to set the axes labels.
xlim(ha9,[0 10]); ylim(ha9,[0.0001 1]);
set(gca,'YMinorTick','on','XScale','log','YScale','log');
title('Mean MSD for tracks 0.25<\alpha<1.5','FontSize', 16,'FontName','Arial'); box on; axis square
end
if sum(index_directed_MSD) ~= 0 % Required because if the index is empty, plotMSD plot ALL the MSD values by default!
subplot(2,5,10)
[~, ha10] = ma.plotMeanMSD(gca,true,find(index_directed_MSD)); % Plot the MEAN mean square displacement curves.
ma.labelPlotMSD(ha10); % A convenience method to set the axes labels.
xlim(ha10,[0 10]); ylim(ha10,[0.0001 1]);
set(gca,'YMinorTick','on','XScale','log','YScale','log');
title('Mean MSD for tracks \alpha>1.5','FontSize', 16,'FontName','Arial'); box on; axis square
end
suptitle([filename,' - ',mat2str(frame_rate),' - ',mat2str(every_n_frame),' - ',mat2str(value_for_fitLogLogMSD),' - ',mat2str(R2LIMIT)]);
filesave = [filename,'_',mat2str(frame_rate),'_',mat2str(every_n_frame),'_',mat2str(value_for_fitLogLogMSD),'_',mat2str(R2LIMIT),'_fig2.eps'];
print('-painters','-depsc','-loose',filesave);
filesave = [filename,'_',mat2str(frame_rate),'_',mat2str(every_n_frame),'_',mat2str(value_for_fitLogLogMSD),'_',mat2str(R2LIMIT),'_fig2.png'];
print('-painters','-dpng',filesave);
close(figure2)
clear track filesave ha* hmsd figure2 Extracted_*
%% Saving the log file and the variables
save([filename,'_',mat2str(frame_rate),'_',mat2str(every_n_frame),'_',mat2str(value_for_fitLogLogMSD),'_',mat2str(R2LIMIT),'_variables.mat']);
%% Clean up
clearvars
toc
diary off
end
%% Backup functions
% meanmsd = ma.getMeanMSD; % Compute the weighted mean of all MSD curves.
% meanvcorr = ma.getMeanVCorr; % Compute the weighted mean of velocity autocorrelation.
% instant_velocities = ma.getVelocities; % Generate and return the instantaneous velocities.
% ma = ma.fitMSD; % Fit all MSD curves by a linear function.