forked from jbkinney/16_titeseq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
figure_3_procedure.py
333 lines (272 loc) · 11.4 KB
/
figure_3_procedure.py
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
#!/usr/bin/env python
import os
import glob
import re
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.colors import LogNorm
from matplotlib.ticker import MaxNLocator
from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size
from matplotlib import gridspec
import math
import readfcs
import sys
import pandas
import pdb
from matplotlib import path
from labeler import Labeler
def count_reads(ax, lib, name, ylabel, vmax, vmin):
# Always do this when working with a specific axes
plt.sca(ax)
all_N = np.zeros((12,4))
label_x = ['0', '10^-9.5', '10^-9', '10^-8.5', '10^-8', '10^-7.5', '10^-7', '10^-6.5', '10^-6', '10^-5.5', '10^-5']
for ii in range(len(label_x)):
for jj in range(4):
all_N[ii+1,jj] = int(lib['fluorescein'+label_x[ii]+'bin'+str(jj)].sum())
for jj in range(4):
all_N[0, jj] = int(lib['cmyc'+str(jj)].sum())
#for NA, NE in zip(lib['A'], lib['E']):
# all_N[0] += NE
# all_N[1:] += NA
im = ax.imshow(all_N, interpolation='nearest', cmap=mpl.cm.hot, norm=LogNorm(vmin=vmin, vmax=vmax))
if ylabel:
ax.set_ylabel('fluorescein [M]',labelpad=-10)
ticks = range(12)
labels = ['expression ', '0'] + ['$10^{%1.1f}$'%x for x in np.arange(-9.5,-4.5,0.5)]
ax.set_yticks(ticks)
ax.set_yticklabels(labels)
else:
ax.set_yticks([])
ax.set_xlabel('bin', labelpad=1)
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
plt.title(name,fontsize=mpl.rcParams['font.size'])
return im
def count_cells(ax, datafile, name, ylabel, vmax, vmin):
# Always do this when working with a specific axes
plt.sca(ax)
df = pandas.read_csv(datafile, delimiter='\t')
all_N = np.array(df.iloc[1:,1:]).astype(float)
im = ax.imshow(all_N, interpolation='nearest', cmap=mpl.cm.hot, norm=LogNorm(vmin=vmin, vmax=vmax))
if ylabel:
ax.set_ylabel('fluorescein [M]',labelpad=-10)
ticks = range(12)
labels = ['expression ', '0'] + ['$10^{%1.1f}$'%x for x in np.arange(-9.5,-4.5,0.5)]
ax.set_yticks(ticks)
ax.set_yticklabels(labels)
else:
ax.set_yticks([])
ax.set_xlabel('bin', labelpad=1)
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
plt.title(name,fontsize=mpl.rcParams['font.size'])
return im
def in_gate(data, gate, name1, name2):
x_data = data[name1]
y_data = data[name2]
x_data = [math.log10(max(x, 1e-10)) for x in x_data]
y_data = [math.log10(max(y, 1e-10)) for y in y_data]
poly = path.Path(gate)
data = np.array([x_data, y_data])
usethis = poly.contains_points(data.T)
return usethis
def filter_fsc_ssc(data):
sub_data = lambda indata, gate: indata[in_gate(indata, gate, 'FSC-A','SSC-A')]
gate = [(4.33487278721722, 4.336993061421767),
(4.004403501790057, 4.010254050207731),
(3.5539963164652484, 3.324117555127123),
(3.761167625032098, 3.10094159611305),
(4.797024167866345, 4.285490917033903),
(4.701406640835492, 4.474332113122737),
(4.33487278721722, 4.336993061421767)]
data = sub_data(data, gate)
return data
def filter_ps(data):
inv_data = lambda indata, gate: indata[[not ii for ii in in_gate(indata, gate, 'FITC-A', 'PE-A')]]
sub_data = lambda indata, gate: indata[[ii for ii in in_gate(indata, gate, 'FITC-A', 'PE-A')]]
gate = [(2.1356696655075864, 1.6073794088650155),
(2.8528011182389887, 1.6588815532528791),
(2.9802911542801267, 2.6717570595475224),
(4.637661622814923, 4.491499494585357),
(3.7133588615166713, 5.058023082851852),
(1.7850720663944564, 5.040855701389231),
(1.5141557398070378, 1.7790532234912266),
(1.6575820303533182, 1.487207738626668),
(2.1356696655075864, 1.6073794088650155)]
inside = sub_data(data, gate)
return inside
def plot_affinity(ax, data, gates):
plt.sca(ax)
colors = [(0,0,0),(1./3,0,0),(2./3,0,0),(1,0,0)]
x = np.array(data['PE-A'])
bins = np.logspace(gates[0][0],gates[3][1], np.round((gates[3][1]-gates[0][0])/0.01))
n, bins= np.histogram(x, bins)
norm = 1./np.max(n)
for gate, color in zip(gates, colors):
usethis = (x>10**gate[0])&(x<10**gate[1])
if sum(usethis):
bins = np.unique((x[usethis]))
bins = np.logspace(gate[0],gate[1], np.round((gate[1]-gate[0])/0.01))
N = np.sum(usethis)
n, bins, patches = plt.hist(x[usethis], bins, histtype='stepfilled', color=color, lw=0, weights = np.ones(N)*float(norm))
ax.set_xlabel(r'PE signal [au]',labelpad=3)
ax.set_yticks([])
#ax.set_ylabel(name)
ax.set_xscale('log')
ax.set_xlim([30,10**gates[-1,-1]])
ax.set_ylim([0,1])
# Print mean log x
usethis = (x>10)&(x<1E5)
#print '%s: %f'%(name,np.mean(np.log10(x[usethis])))
def plot_expression(ax, data, gates):
plt.sca(ax)
colors = [(0,0,0),(1./3,0,1./3),(2./3,0,2./3),(1,0,1)]
x = np.array(data['Brilliant Violet 421-A'])
bins = np.logspace(gates[0][0],gates[3][1], np.round((gates[3][1]-gates[0][0])/0.01))
n, bins= np.histogram(x, bins)
norm = 1./np.max(n)
for gate, color in zip(gates, colors):
usethis = (x>10**gate[0])&(x<10**gate[1])
if sum(usethis):
bins = np.unique((x[usethis]))
bins = np.logspace(gate[0],gate[1], np.round((gate[1]-gate[0])/0.01))
N = np.sum(usethis)
n, bins, patches = plt.hist(x[usethis], bins, histtype='stepfilled', color=color, lw=0, weights = np.ones(N)*float(norm))
ax.set_xlabel(r'BV signal [au]',labelpad=3)
ax.set_yticks([])
ax.set_xscale('log')
ax.set_xlim([30,10**gates[-1,-1]])
ax.set_ylim([0,1])
def get_filenames(path):
files = []
for infile in glob.glob( os.path.join(path, '*.fcs') ):
files.append(infile)
return files
out_names = ['./pdfs/figure_3_procedure.pdf', './pdfs/figure_S2_rep2.pdf', './pdfs/figure_S3_rep3.pdf']
rep1 = pandas.read_csv('data/replicate_1.csv')
rep2 = pandas.read_csv('data/replicate_2.csv')
rep3 = pandas.read_csv('data/replicate_3.csv')
reps = [rep1, rep2, rep3]
sort_counts = ['data/sort_counts_16.4.15.txt', 'data/sort_counts_16.4.19.txt', 'data/sort_counts_16.4.21.txt']
directories = ['data/fcs1/','data/fcs2/','data/fcs3/']
bin_vals_16_4_15 = np.array([[1.4775788014225415, 2.245106453825187],
[2.2459637507318497, 2.9683536647430646],
[2.96837636283699, 3.6663288893768895],
[3.6675170811229103, np.log10(3e4)]])
#April 19 gates
bin_vals_16_4_19 = np.array([[1.4775788014225415, 2.245106453825187],
[2.2459637507318497, 2.85027248745035],
[2.8650326346119392, 3.474446976276228],
[3.4849588153986915, np.log10(1e5)]])
#April 21 gates
bin_vals_16_4_21 = np.array([[1.4775788014225415,2.200826012340419],
[2.216443456408671,2.8355123402887603],
[2.8355123402887603,3.474446976276228],
[3.4849588153986915,np.log10(3e4)]])
aff_gates = [bin_vals_16_4_15, bin_vals_16_4_19, bin_vals_16_4_21]
exp_gate_16_4_15 = np.array([[1.4917982838529373, 3.337952074987111],
[3.358239479285288, 3.80681652987833],
[3.8214685440936806, 4.272299750719853],
[4.275680984769549, 5.]])
exp_gate_16_4_19 = np.array([[1.4917982838529373, 2.5027872647121274],
[2.523074669010305, 3.352604089202462],
[3.3672561034178123, 4.242995722289152],
[4.246376956338848, 5.345278022490142]])
exp_gate_16_4_21 = np.array([[1.4917982838529373,3.411212146063864],
[3.416847536146691,3.80681652987833],
[3.8214685440936806,4.242995722289152],
[4.246376956338848,5]])
exp_gates = [exp_gate_16_4_15, exp_gate_16_4_19, exp_gate_16_4_21]
for out_name, rep, sort_name, rep_number, directory, aff_gate, exp_gate in zip(out_names, reps, sort_counts, range(1,4), directories, aff_gates, exp_gates):
# Needed for proper focusing
plt.ion()
plt.close('all')
# Create figure with subplots and specified spacing
figsize=(3.5,5.6)
rows=14
cols=1
col = 1
fig, axes = plt.subplots(figsize=figsize)
gs = gridspec.GridSpec(28, 2)
plt.subplots_adjust(
bottom=0.06,
top=0.95,
left=0.17,
right=0.96,
wspace=0.6,
hspace=0.0)
# Make a labler to add labels to subplots
labeler = Labeler(xpad=.13,ypad=.01,fontsize=10)
# For CDR1H and CDR3H
conc_labels = ['$0$'] + \
['$10^{%1.1f}$'%x for x in np.arange(-9.5,-4.5,0.5)]
file_labels = ['0M'] + \
['10^%1.1fM'%x for x in np.arange(-9.5,-4.5,0.5)]
#csv_name = 'out.csv'
filenames = get_filenames(directory)
names = [re.search('Sort (\d+)', ii) for ii in filenames]
condition = [n.group(1) for n in names]
# Make plots
for [filename, well] in zip(filenames, condition):
well = int(well)
library_data = readfcs.readfcs(filename)
data = library_data.rename(columns={'SSC-W': 'SSC-H', 'SSC-H': 'SSC-W', 'FSC-W': 'FSC-H', 'FSC-H': 'FSC-W'})
data = filter_fsc_ssc(data) # filter by fsc and ssc
maskeddata = filter_ps(data) # pre-sort filter based on FITC and PE signal
# If expression bin, compute expression distribution
if well == 1:
ax = plt.subplot(gs[26:28,0])
name = 'expression'
plot_expression(ax, data, exp_gate)
labeler.label_subplot(ax,'B')
#plt.title('expression', \
# fontsize=mpl.rcParams['font.size'])
#data.to_csv('./data/expression.csv')
else:
ind = (well-2)*2
ax = plt.subplot(gs[ind:(ind+2),0])
name = 'bin %d'%well
plot_affinity(ax, maskeddata, aff_gate)
conc_label = conc_labels[well-2]
ax.set_ylabel(conc_label,rotation=0,ha='right', fontsize=mpl.rcParams['font.size'])
#maskeddata.to_csv('./data/'+file_labels[well-2]+'.csv')
if well!=12:
ax.set_xlabel('')
ax.set_xticks([])
if well == 7:
ax.text(-0.35,0.5,'fluorescein [M]', rotation=90,ha='right', fontsize=mpl.rcParams['font.size'],transform=ax.transAxes)
if well==2:
labeler.label_subplot(ax,'A')
#plt.title('affinity', \
# fontsize=mpl.rcParams['font.size'])
#plt.subplot(gs[12,0]).set_visible(False)
#plt.subplot(gs[11,0]).set_visible(False)
labeler = Labeler(xpad=.12,ypad=.01,fontsize=10)
vmin = 1e3
vmax = 1E7
#panel C
ax = plt.subplot(gs[0:12,1])
im = count_cells(ax,sort_name,'cells', \
ylabel=True, vmax=vmax, vmin=vmin)
labeler.label_subplot(ax,'C')
divider = make_axes_locatable(ax)
width = axes_size.AxesY(ax, aspect=1/30.)
pad = axes_size.Fraction(0.75, width)
cax = divider.append_axes("right", size=width, pad=pad)
#cax = fig.add_axes([0.92, 0.5375, 0.025, 0.4125])
cbar = fig.colorbar(im, cax=cax, orientation='vertical')
#cbar.set_label(r'number of sorted cells')
cbar.solids.set_rasterized(True)
# Panel D
ax = plt.subplot(gs[16:28,1])
im = count_reads(ax, rep,'reads' ,ylabel=True, vmax=vmax, vmin=vmin)
labeler.label_subplot(ax,'D')
divider = make_axes_locatable(ax)
width = axes_size.AxesY(ax, aspect=1/30.)
pad = axes_size.Fraction(0.75, width)
cax = divider.append_axes("right", size=width, pad=pad)
#cax = fig.add_axes([0.92, 0.06, 0.025, 0.38])
cbar = fig.colorbar(im, cax=cax, orientation='vertical')
#cbar.set_label(r'number of sequence reads')
cbar.solids.set_rasterized(True)
plt.show()
plt.savefig(out_name)