-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuppl_software2.m
111 lines (87 loc) · 2.89 KB
/
suppl_software2.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
% Zomorrodi, Hemez, et al., Computational design and construction of an Escherichia coli strain engineered to produce a non-standard amino acid
% Supplementary Software S2 – MATLAB script used to calculate doubling times from plate reader growth curve data.
function [DT, R, median] = lnDoublingTime(ODvals, timevals, points, varargin)
% Check if the OD series is a filler dataset
if ODvals(1) == ODvals(end)
DT = 0;
R = 0;
median = 0;
return
end
% Check that the number of points for linear fitting is odd
if mod(points, 2) ~= 1
error('Subset of points for linear regression must be odd')
end
% Check that the number of values in OD and time arrays is equal
if length(ODvals) ~= length(timevals)
error('ODVALS and TIMEVALS must have the same number of values')
end
plotter = 0;
datapts = length(ODvals);
if nargin == 4
if strcmp(varargin{1}, 'plot')
disp('PLOT function active')
plotter = 1;
figure
else
error('Optional fourth argument is invalid')
end
end
% Transform the data into logspace, transform inf to NaN
ODlog = log(ODvals);
ODlog(isinf(ODlog)) = NaN;
ODlog = real(ODlog);
% Perform linear regression to estimate doubling time
minRsq = 0.98; % R-squared value threshold
padding = floor(points/2);
maxSlp = 0; % Current highest slope
currVals = [0 0 0 0];
% currRsq: R-squared value of current highest slope
for i = (1+padding):(datapts-padding)
Tmin = i - padding;
Tmax = i + padding;
currTvals = timevals(Tmin:Tmax);
currODvals = ODlog(Tmin:Tmax);
% Check if the region is above the linearity threshold
currC = corrcoef(currTvals, currODvals);
currR = currC(1,2);
currRsq = currR^2;
% If region is above linearity threshold, calculate the slope
if currRsq >= minRsq
linfit = polyfit(currTvals, currODvals, 1); % linfit: [slope, y-int]
currSlp = linfit(1);
% Update maximum slope; keep
if currSlp > maxSlp
maxSlp = currSlp;
Yint = linfit(2);
currVals = [maxSlp Yint currRsq i];
end
end
end
maxVals = currVals;
% Do transformation on maximum values to get doubling time, centroid time
DT = log(2) / maxVals(1);
R = maxVals(3);
median = timevals(maxVals(4));
% Optional plotting function
if plotter == 1
subplot(1,2,1)
plot(timevals, ODvals, 'linewidth', 3)
grid on
xlabel('Time')
ylabel('OD_{600}')
title('Raw Data')
subplot(1,2,2)
hold on
% Plot log-transformed data
plot(timevals, ODlog, 'ok', 'markerfacecolor','k')
% Plot the region of linear fit used to calculate doubling time
fitTimes = timevals( (maxVals(4)-padding) : (maxVals(4)+padding) );
fitLnOD = (fitTimes.*maxVals(1)) + maxVals(2);
plot(fitTimes, fitLnOD, '-r', 'linewidth', 5)
grid on
xlabel('Time')
ylabel('ln(OD_{600})')
title('Log-Transformed Data')
end
end