Xml parsing parts adopted from Sonya's assaytools/examples/fluorescence-binding-assay/Src-gefitinib fluorescence simple.ipynb
In [1]:
import numpy as np
import matplotlib.pyplot as plt
from lxml import etree
import pandas as pd
import os
import matplotlib.cm as cm
import seaborn as sns
%pylab inline
In [2]:
# Get read and position data of each fluorescence reading section
def get_wells_from_section(path):
reads = path.xpath("*/Well")
wellIDs = [read.attrib['Pos'] for read in reads]
data = [(float(s.text), r.attrib['Pos'])
for r in reads
for s in r]
datalist = {
well : value
for (value, well) in data
}
welllist = [
[
datalist[chr(64 + row) + str(col)]
if chr(64 + row) + str(col) in datalist else None
for row in range(1,9)
]
for col in range(1,13)
]
return welllist
In [6]:
file_lig1="MI_FLU_hsa_lig1_20150922_150518.xml"
file_name = os.path.splitext(file_lig1)[0]
label = file_name[0:25]
print label
In [8]:
root = etree.parse(file_lig1)
#find data sections
Sections = root.xpath("/*/Section")
much = len(Sections)
print "****The xml file " + file_lig1 + " has %s data sections:****" % much
for sect in Sections:
print sect.attrib['Name']
In [9]:
#Work with topread
TopRead = root.xpath("/*/Section")[0]
welllist = get_wells_from_section(TopRead)
df_topread = pd.DataFrame(welllist, columns = ['A - HSA','B - Buffer','C - HSA','D - Buffer', 'E - HSA','F - Buffer','G - HSA','H - Buffer'])
df_topread.transpose()
Out[9]:
In [10]:
# To generate cvs file
# df_topread.transpose().to_csv(label + Sections[0].attrib['Name']+ ".csv")
Y = MF*L + BKG
Y: Fluorescence read (Flu unit)
L: Total ligand concentration (uM)
BKG: background fluorescence without ligand (Flu unit)
MF: molar fluorescence of free ligand (Flu unit/ uM)
In [16]:
import numpy as np
from scipy import optimize
import matplotlib.pyplot as plt
%matplotlib inline
def model(x,slope,intercept):
''' 1D linear model in the format scipy.optimize.curve_fit expects: '''
return x*slope + intercept
# generate some data
#X = np.random.rand(1000)
#true_slope=1.0
#true_intercept=0.0
#noise = np.random.randn(len(X))*0.1
#Y = model(X,slope=true_slope,intercept=true_intercept) + noise
#ligand titration
lig1=np.array([200.0000,86.6000,37.5000,16.2000,7.0200, 3.0400, 1.3200, 0.5700, 0.2470, 0.1070, 0.0462, 0.0200])
lig1
Out[16]:
In [19]:
# Since I have 4 replicates
L=np.concatenate((lig1, lig1, lig1, lig1))
len(L)
Out[19]:
In [36]:
# Fluorescence read
df_topread.loc[:,("B - Buffer", "D - Buffer", "F - Buffer", "H - Buffer")]
Out[36]:
In [41]:
B=df_topread.loc[:,("B - Buffer")]
D=df_topread.loc[:,("D - Buffer")]
F=df_topread.loc[:,("F - Buffer")]
H=df_topread.loc[:,("H - Buffer")]
Y = np.concatenate((B.as_matrix(),D.as_matrix(),F.as_matrix(),H.as_matrix()))
In [44]:
(MF,BKG),_ = optimize.curve_fit(model,L,Y)
print('MF: {0:.3f}, BKG: {1:.3f}'.format(MF,BKG))
print('y = {0:.3f} * L + {1:.3f}'.format(MF, BKG))
Fluorescence intensity vs added ligand
LR= ((X+Rtot+KD)-SQRT((X+Rtot+KD)^2-4XRtot))/2
L= X - LR
Y= BKG + MFL + FRMF*LR
Rtot: receptor concentration (uM)
BKG: background fluorescence without ligand (Flu unit)
MF: molar fluorescence of free ligand (Flu unit/ uM)
Kd: dissociation constant (uM)
FR: Molar fluorescence ratio of complex to free ligand (unitless) complex flurescence = FRMFLR
Y: fluorescence measurement X: total ligand concentration L: free ligand concentration
In [82]:
def model2(x,kd,fr):
''' 1D linear model in the format scipy.optimize.curve_fit expects: '''
# lr =((x+rtot+kd)-((x+rtot+kd)**2-4*x*rtot)**(1/2))/2
# y = bkg + mf*(x - lr) + fr*mf*lr
bkg = 86.2
mf = 2.517
rtot = 0.5
return bkg + mf*(x - ((x+rtot+kd)-((x+rtot+kd)**2-4*x*rtot)**(1/2))/2) + fr*mf*(((x+rtot+kd)-((x+rtot+kd)**2-4*x*rtot)**(1/2))/2)
In [78]:
# Total HSA concentration (uM)
Rtot = 0.5
#Total ligand titration
X = L
len(X)
Out[78]:
In [79]:
# Fluorescence read
df_topread.loc[:,("A - HSA", "C - HSA", "E - HSA", "G - HSA")]
Out[79]:
In [80]:
A=df_topread.loc[:,("A - HSA")]
C=df_topread.loc[:,("C - HSA")]
E=df_topread.loc[:,("E - HSA")]
G=df_topread.loc[:,("G - HSA")]
Y = np.concatenate((A.as_matrix(),C.as_matrix(),E.as_matrix(),G.as_matrix()))
len(Y)
Out[80]:
In [81]:
(Kd,FR),_ = optimize.curve_fit(model2, X, Y, p0=(5,1))
print('Kd: {0:.3f}, Fr: {1:.3f}'.format(Kd,FR))
In [ ]: