<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 FLUORESCENCE BINDING ASSAY ANALYSIS
Experiment date
Step1:  Calculating Molar Fluorescence (MF) of Free Ligand
1. Maximum likelihood curve-fitting
Find the maximum likelihood estimate, $\theta^$, i.e. the curve that minimizes the squared error $\theta^ = \text{argmin} \sum_i |y_i - f_\theta(x_i)|^2$ (assuming i.i.d. Gaussian noise)
Y = MF*L + BKG
Y
Step2:  Curve-fitting to binding saturation curve
Fluorescence intensity vs added ligand
LR= ((X+Rtot+KD)-SQRT((X+Rtot+KD)^2-4XRtot))/2
L= X - LR
Y= BKG + MFL + FRMF*LR
Constants
Rtot
<ASSISTANT_TASK:>
Python Code:
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
# 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
file_lig1="MI_FLU_hsa_lig1_20150922_150518.xml"
file_name = os.path.splitext(file_lig1)[0]
label = file_name[0:25]
print label
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']
#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()
# To generate cvs file
# df_topread.transpose().to_csv(label + Sections[0].attrib['Name']+ ".csv")
Explanation: FLUORESCENCE BINDING ASSAY ANALYSIS
Experiment date: 2015/09/22
Protein: HSA
Fluorescent ligand : dansylamide (lig1)
Xml parsing parts adopted from Sonya's assaytools/examples/fluorescence-binding-assay/Src-gefitinib fluorescence simple.ipynb
End of explanation
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
# Since I have 4 replicates
L=np.concatenate((lig1, lig1, lig1, lig1))
len(L)
# Fluorescence read
df_topread.loc[:,("B - Buffer", "D - Buffer", "F - Buffer", "H - Buffer")]
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()))
(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))
Explanation: Calculating Molar Fluorescence (MF) of Free Ligand
1. Maximum likelihood curve-fitting
Find the maximum likelihood estimate, $\theta^$, i.e. the curve that minimizes the squared error $\theta^ = \text{argmin} \sum_i |y_i - f_\theta(x_i)|^2$ (assuming i.i.d. Gaussian noise)
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)
End of explanation
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)
# Total HSA concentration (uM)
Rtot = 0.5
#Total ligand titration
X = L
len(X)
# Fluorescence read
df_topread.loc[:,("A - HSA", "C - HSA", "E - HSA", "G - HSA")]
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)
(Kd,FR),_ = optimize.curve_fit(model2, X, Y, p0=(5,1))
print('Kd: {0:.3f}, Fr: {1:.3f}'.format(Kd,FR))
Explanation: Curve-fitting to binding saturation curve
Fluorescence intensity vs added ligand
LR= ((X+Rtot+KD)-SQRT((X+Rtot+KD)^2-4XRtot))/2
L= X - LR
Y= BKG + MFL + FRMF*LR
Constants
Rtot: receptor concentration (uM)
BKG: background fluorescence without ligand (Flu unit)
MF: molar fluorescence of free ligand (Flu unit/ uM)
Parameters to fit
Kd: dissociation constant (uM)
FR: Molar fluorescence ratio of complex to free ligand (unitless)
    complex flurescence = FRMFLR
Experimental data
Y: fluorescence measurement
X: total ligand concentration
L: free ligand concentration
End of explanation
<END_TASK>