In [1]:
import numpy
blob = open( 'Spaceplate1_48.txt').read()
block = blob.split('End')[0] # different versions of the plate reader software put End tokens in different places
#stupid_delimeter = '\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\n'
stupid_delimeter = '\t\t\n\t\t\n'
ttimes = []
ttemps = []
frames = []
stop_at = 273 # in case the run was not completed, stop here
for tn,tpoint in enumerate(block.split(stupid_delimeter)[:stop_at]) :
if tn == 0 : # skip header stuff
tpoint = tpoint.split('12\t\t\n')[1]
if tn == 9 : break # stop after nine frames
frame = numpy.zeros((8,12))
for rn,row in enumerate(tpoint.split('\n')) :
for cn,col in enumerate(row.split('\t')) :
if rn != 0 and cn == 0 : continue
if rn != 0 and cn == 1 : continue
if cn == 14 : continue
if cn == 15 : continue
if rn == 0 and cn == 0 : ttimes.append(col); continue
if rn == 0 and cn == 1 : ttemps.append(col); continue
#print tn,rn,cn-2,col
frame[rn,cn-2] = float(col)
frames.append(frame)
frames = numpy.array(frames)
times = arange( 0, len(frames) / 96.0, 1/96.0 ) # not actual times here...
rownames = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' ]
n = 0
fig, ax = plt.subplots( 8, 12, figsize=(16,7))
fig.suptitle('Space Plate 1, T=48', fontsize=12, x=0.05,y=1 )
for i in range(8) :
for j in range(12) :
subplot(8,12,n+1)
title( rownames[i] + str(j+1) )
#ylim( (0,max_val) )
ylim( ( 0, 1.85 ) )
#xlim( ( 0, 45 ) )
xlim( 0, 10 )
xticks( [] )
yticks( linspace( 0.5, 1.5, 3 ) )
n = n+1
bar( arange(0.5,9.5,1), frames[:,i,j], width=0.6, color='black', alpha=0.4, lw=0)
fig.tight_layout()
savefig( 'Spaceplate1_48.png', format='png', dpi=100 )
savefig( 'Spaceplate1_48.pdf', format='pdf', dpi=100 )
In [109]:
import numpy
import csv
# import species metadata and plate map
spacebugs = []
with open('spacebugs.tsv', 'rb') as csvfile :
csvreader = csv.reader(csvfile, delimiter='\t', quotechar='|')
for row in csvreader:
spacebugs.append( { 'source' : row[1], 'species' : row[2], 'name' : row[3], 'data' : [] } )
platemap = zeros((8,12),dtype=numpy.int)
with open('platemap.tsv', 'rb') as csvfile :
csvreader = csv.reader(csvfile, delimiter='\t', quotechar='|')
for i,row in enumerate(csvreader) :
for j,n in enumerate(map(int,row)) :
platemap[i,j] = n
# import plate data
plateN = '3'
expN = 'S'
name = 'Space Plate 3'
prefix = 'Spaceplate3_timeseries'
files = [ ( 0, 'Spaceplate3_0.txt'),
( 24, 'Spaceplate3_24.txt'),
( 48, 'Spaceplate3_48.txt'),
( 72, 'Spaceplate3_72.txt'),
( 96, 'Spaceplate3_96.txt') ]
#stupid_delimeter = '\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\n'
stupid_delimeter = '\t\t\n\t\t\n'
stop_at = 273 # in case the run was not completed, stop here
data = []
for TT,file in files :
blob = open( file ).read()
block = blob.split('End')[0] # different versions of the plate reader software put End tokens in different places
ttimes = []
ttemps = []
frames = []
for tn,tpoint in enumerate(block.split(stupid_delimeter)[:stop_at]) :
if tn == 0 : # skip header stuff
tpoint = tpoint.split('12\t\t\n')[1]
if tn == 9 : break # stop after nine frames
frame = numpy.zeros((8,12))
for rn,row in enumerate(tpoint.split('\n')) :
for cn,col in enumerate(row.split('\t')) :
if rn != 0 and cn == 0 : continue
if rn != 0 and cn == 1 : continue
if cn == 14 : continue
if cn == 15 : continue
if rn == 0 and cn == 0 : ttimes.append(col); continue
if rn == 0 and cn == 1 : ttemps.append(col); continue
#print tn,rn,cn-2,col
frame[rn,cn-2] = float(col)
frames.append(frame)
frames = numpy.array(frames)
times = arange( 0, len(frames) / 96.0, 1/96.0 ) # not actual times here...
data.append(frames)
# collect replicates into spacebugs vector
for i,bug in enumerate(spacebugs) :
for j in range(len(data)) :
spacebugs[i]['data'].append([])
for TT,run in enumerate(data) :
for i in range(8) :
for j in range(12) :
n = platemap[i,j]-1
spacebugs[n]['data'][TT] = spacebugs[n]['data'][TT] + list(run[:,i,j])
In [142]:
# build figure
rownames = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' ]
n = 0
fig, ax = plt.subplots( 8, 12, figsize=(16,9))
fig.suptitle( name, fontsize=12, x=0.05,y=1 )
for i in range(8) :
for j in range(12) :
subplot(8,12,n+1)
#title( rownames[i] + str(j+1) )
title( expN + plateN + ':' + spacebugs[ platemap[i,j] - 1 ]['name'])
#ylim( (0,max_val) )
ylim( ( 0, 2.5 ) )
#xlim( ( 0, 45 ) )
xlim( 0, 10 )
xticks( [] )
yticks( linspace( 0.5, 2.0, 3 ) )
n = n+1
timepoint_data = []
for run in data :
timepoint_data.append(run[:,i,j])
boxplot(timepoint_data)
fig.tight_layout()
# save figure
savefig( prefix+'.png', format='png', dpi=100 )
savefig( prefix+'.pdf', format='pdf', dpi=100 )
In [111]:
from scipy import optimize
def N(n,a,x) :
return n*(1-exp(-a*x))
def residuals( p, y, x ) :
n, a = p
err = y - N(n,a,x)
return err
def Neval(x) :
return N(N0,a,x)
fig, ax = plt.subplots( 4, 12, figsize=(16,5))
fig.suptitle( name, fontsize=12, x=0.05,y=1 )
best_huddle = ('',0,0)
best_tipoff = ('',0,0)
best_alpha = ('',0,0)
best_Hsat = ('',10000,0)
best_N0 = ('',0,0)
for n,bug in enumerate(spacebugs) :
subplot(4,12,n+1)
ylim((0,2.5))
yticks( linspace( 0.5, 2.0, 3 ) )
boxplot(bug['data'])
medians = map( median, bug['data'] )
bug['huddle'] = max(medians) - min(medians)
bug['tipoff'] = medians[1] - medians[0]
# least squares fit to exponential saturation model
N0, a = optimize.leastsq(residuals, [1,1], args=(array(medians),array([1,2,3,4,5])))[0]
Hsat = -log(e**(-a)/2.0)/a
bug['alpha'] = a # time constant
bug['N0'] = N0 # saturation value
bug['Hsat'] = Hsat
if bug['Hsat'] > 1.6 and bug['alpha'] < 1.5 :
title(bug['name'])
bug['alive'] = True
else :
title(bug['name'], alpha=0.2)
bug['alive'] = False
if best_huddle[1] < bug['huddle'] and bug['alive'] : best_huddle = ( bug['name'], bug['huddle'], n )
if best_tipoff[1] < bug['tipoff'] and bug['alive'] : best_tipoff = ( bug['name'], bug['tipoff'], n )
if best_alpha[1] < bug['alpha'] and bug['alive'] : best_alpha = ( bug['name'], bug['alpha'], n )
if best_N0[1] < bug['N0'] and bug['alive'] : best_N0 = ( bug['name'], bug['N0'], n )
if best_Hsat[1] > bug['Hsat'] and bug['alive'] : best_Hsat = ( bug['name'], bug['Hsat'], n )
plot( [1,2,3,4,5], medians, 'green')
plot(linspace(1,5),Neval(linspace(1,5)),'red')
axvline(bug['Hsat'], color='black', linestyle=':')
subplot(4,12,best_huddle[2]+1)
title(best_huddle[0],color='red')
subplot(4,12,best_tipoff[2]+1)
title(best_tipoff[0],color='blue')
subplot(4,12,best_alpha[2]+1)
title(best_Hsat[0],color='green')
fig.tight_layout()
# save figure
savefig( prefix+'_names.png', format='png', dpi=100 )
savefig( prefix+'_names.pdf', format='pdf', dpi=100 )
In [92]:
for n,bug in enumerate(spacebugs) :
print n, bug['name'], bug['alpha'], bug['Hsat']
spacebugs[43]
Out[92]:
In [ ]: