Esempio n. 1
0
    def show_specific(self):
        """Show datasets for an individual cell, depends on fieldtype"""

        protein = self.form.getfirst('protein')
        rectype = self.form.getfirst('rectype')
        col = self.form.getfirst('column')
        sys.stdout.flush()

        DB = self.DB = self.connect()
        ptmodel = PEATTableModel(DB)
        ekincols = ptmodel.ekintypes
        fieldtype = DB['userfields'][col]['field_type']
        protname = DB[protein]['name']

        self.showHeader(title=str(protname+': '+col), menu=1)
        print '<div><a>Protein: %s </a><br>' %protname
        print 'Column data: %s </div>' %col
        from PEATDB.Ekin.Web import EkinWeb

        if fieldtype in ekincols:
            fieldtype = 'ekindata'
        #plot ekin data curves, if fieldtype is ekindata
        if fieldtype == 'ekindata':
            E = DB[protein][col]
            EW = EkinWeb()
            EW.showEkinPlots(project=E, datasets='ALL', path=self.plotsdir,
                             imgpath=self.imagepath)
            print '</table>'
        #save the pdb structure to a file and provide url link to it
        elif fieldtype == 'structure':
            recdata = DB[protein][col]
            for k in recdata:
                print '<br><a> %s</a>' %str(k)
        elif fieldtype == 'Notes':
            recdata = DB[protein][col]
            print '<br><a> %s</a>' %recdata['text']
        elif fieldtype == 'File':
            recdata = DB[protein][col]
            print '<p> This record contains a data file. Click link to open.'
            print '<a href="http://peat.ucd.ie/titration_db/%s">Open link</a>' %recdata['location']
        else:
            print '<br>Sorry, no handler for this data right now..</br>'
        self.footer()
        return
Esempio n. 2
0
def estimateExpUncertainty(ekindata,
                           fitdata,
                           xuncert=0,
                           yuncert=0,
                           runs=10,
                           doplots=False,
                           callback=None,
                           guess=True,
                           **kwargs):
    """Chresten's method for estimating the error on each parameter due
       to the effect of the exp uncertainty, which is user provided.
       If the datapoints have x or y error values, we use that instead"""
    '''we iterate over runs and re-fit the data, each time adjusting x-y points
       then accumulate a fit val for each parameter and get mean/stdev.
       Note: errors are added +/- values'''

    from PEATDB.Ekin.Dataset import EkinDataset
    grad = 1e-6
    conv = 1e-6
    for k in kwargs:
        if k == 'conv':
            conv = kwargs[k]
        if k == 'grad':
            grad = kwargs[k]
    if fitdata == None or len(fitdata) == 0:
        return None
    model = fitdata['model']

    F = getFitter(model=model)
    try:
        varnames = F.getVarNames()
    except:
        return None
    fitvars = {}
    for v in varnames:
        fitvars[v] = []  #group lists by variable name

    xd, yd, a, xerrs, yerrs = ekindata.getAll()
    estdata = {}
    estdata['__datatabs_fits__'] = {}
    for r in range(runs):
        mut_x = []
        mut_y = []
        #if the datapoint has x or y error values, we use that instead
        for i in range(len(xd)):
            if xerrs != None and xerrs[i] > 0:
                mut_x.append(xd[i] + random.uniform(-xerrs[i], xerrs[i]))
            else:
                mut_x.append(xd[i] + random.uniform(-xuncert, xuncert))
            if yerrs != None and yerrs[i] > 0:
                mut_y.append(yd[i] + random.uniform(-yerrs[i], yerrs[i]))
            else:
                mut_y.append(yd[i] + random.uniform(-yuncert, yuncert))

        ek = EkinDataset(xy=[mut_x, mut_y], active=a)
        fitres, X = doFit(ek,
                          model=model,
                          fitdata=fitdata,
                          grad=1e-6,
                          conv=1e-6,
                          silent=True,
                          guess=guess,
                          noiter=100)
        vrs = X.getVariables()

        for n in range(len(varnames)):
            fitvars[varnames[n]].append(vrs[n])
        estdata[str(r)] = ek
        estdata['__datatabs_fits__'][str(r)] = fitres

        if callback != None:
            fitstats = {}
            for v in varnames:
                fitstats[v] = numpy.mean(fitvars[v]), numpy.std(fitvars[v])
            callback(r, fitstats)

    fitstats = {}
    for v in varnames:
        #print fitvars[v]
        err = numpy.std(fitvars[v]) / 2
        #print v, numpy.mean(fitvars[v]), err
        #we store the final error as +/- half the stdev.
        fitstats[v] = numpy.mean(fitvars[v]), err
    if doplots == True:
        from PEATDB.Ekin.Web import EkinWeb
        ew = EkinWeb()
        ew.showEkinPlots(ekindata=estdata,
                         outfile='est_exp_err.html',
                         columns=3,
                         normalise=False,
                         showfitvars=True,
                         imgpath='.')

    return fitstats
Esempio n. 3
0
def estimateExpUncertainty(ekindata, fitdata, xuncert=0, yuncert=0,
                                 runs=10, doplots=False, callback=None, guess=True,
                                 **kwargs):
    """Chresten's method for estimating the error on each parameter due
       to the effect of the exp uncertainty, which is user provided.
       If the datapoints have x or y error values, we use that instead"""

    '''we iterate over runs and re-fit the data, each time adjusting x-y points
       then accumulate a fit val for each parameter and get mean/stdev.
       Note: errors are added +/- values'''
    
    from PEATDB.Ekin.Dataset import EkinDataset
    grad=1e-6; conv=1e-6
    for k in kwargs:
        if k=='conv':
            conv=kwargs[k]
        if k=='grad':
            grad=kwargs[k]
    if fitdata == None or len(fitdata) == 0:
        return None
    model = fitdata['model']

    F = getFitter(model=model)
    try:
        varnames = F.getVarNames()
    except:
        return None
    fitvars = {}
    for v in varnames:
        fitvars[v] = []     #group lists by variable name
    
    xd,yd,a,xerrs,yerrs = ekindata.getAll()
    estdata={}; estdata['__datatabs_fits__']={}
    for r in range(runs):
        mut_x=[];mut_y=[]
        #if the datapoint has x or y error values, we use that instead
        for i in range(len(xd)):
            if xerrs != None and xerrs[i] > 0:
                mut_x.append(xd[i] + random.uniform(-xerrs[i], xerrs[i]))
            else:
                mut_x.append(xd[i] + random.uniform(-xuncert, xuncert))
            if yerrs != None and yerrs[i] > 0:
                mut_y.append(yd[i] + random.uniform(-yerrs[i], yerrs[i]))
            else:
                mut_y.append(yd[i] + random.uniform(-yuncert,yuncert))
       
        ek = EkinDataset(xy=[mut_x,mut_y],active=a)
        fitres, X = doFit(ek, model=model, fitdata=fitdata, grad=1e-6, conv=1e-6,
                                silent=True, guess=guess, noiter=100)
        vrs = X.getVariables()

        for n in range(len(varnames)):
            fitvars[varnames[n]].append(vrs[n])
        estdata[str(r)] = ek
        estdata['__datatabs_fits__'][str(r)] = fitres

        if callback != None:
            fitstats = {}
            for v in varnames:
                fitstats[v] = numpy.mean(fitvars[v]), numpy.std(fitvars[v])
            callback(r, fitstats)

    fitstats = {}       
    for v in varnames:
        #print fitvars[v]
        err = numpy.std(fitvars[v])/2
        #print v, numpy.mean(fitvars[v]), err
        #we store the final error as +/- half the stdev.
        fitstats[v] = numpy.mean(fitvars[v]), err
    if doplots == True:           
        from PEATDB.Ekin.Web import EkinWeb
        ew = EkinWeb()
        ew.showEkinPlots(ekindata=estdata, outfile='est_exp_err.html',
                            columns=3, normalise=False, showfitvars=True,
                            imgpath='.')

    return fitstats
Esempio n. 4
0
    def do_search(self, globalop, proteinop, proteins, datasets):
        """Do searches for the various parameters, name, pka etc"""

        import re, urllib
        from PEATDB.PEATTables import PEATTableModel
        from PEATDB.Ekin.Fitting import Fitting
        from PEATDB.Ekin.Web import EkinWeb
        DB = self.DB
        EW = EkinWeb()
        ekincols = DB.ekintypes

        found=[]
        protlist = proteins.split(' ')
        residuelist = residues.split(' ')

        def createPhrase(items, op):
            if op == 'or':
                logicsymbol = '|'
            else:
                logicsymbol = '+'
            itemlist = items.split(' ')
            phrase =''
            c=1
            for i in itemlist:
                if c == len(itemlist):
                    phrase = phrase + i
                else:
                    phrase = phrase + i + logicsymbol
                c=c+1
            return re.compile(phrase, re.IGNORECASE)

        #if there is a protein name entered, get the list of proteins/records to use
        #otherwise use all records to search for the other keys
        names = []
        keywords = {}
        for p in DB.getRecs():
            name = DB[p].name
            names.append((name,p))
            if hasattr(DB[p], 'keywords'):
                keywords[name] = DB[p].keywords
            else:
                keywords[name] = ''
        names.sort()

        #create search expressions
        s_protein = createPhrase(proteins, proteinop)
        s_residue = createPhrase(residues, 'or')
        pkarange = pka.split('-')

        #do search
        for name in names:
            proteinname = name[0]
            protein = name[1]
            if s_protein.search(proteinname) or s_protein.search(keywords[proteinname]):
                found.append(protein)

        if len(found)==0:
            print '<h2>Sorry, no proteins found that match this name. <h2>'
            return
        elif residues == '' and pka == '':
            self.show_DB(selected=found)
            return found

        #now search for the other keys inside selected proteins if needed
        ekinfound={}; foundfits={}
        ignore =['__fit_matches__', '__Exp_Meta_Dat__', '__datatabs_fits__']

        kys = list(DB.getRecs())
        kys.sort()
        if len(found) == 0 or globalop == 'or':
            found = DB.getRecs()

        print '<table id="mytable" cellspacing=0 align=center>'
        '''search recs for residue and pkarange
           and add dataset names to new ekinfound dict'''
        for protein in found:
            for col in DB[protein].keys():
                if nucleus != 'any':
                    if nucleus not in col:
                        continue
                E = DB[protein][col]
                if DB['userfields'].has_key(col):
                    fieldtype=DB['userfields'][col]['field_type']
                else:
                    fieldtype=None
                if fieldtype in ekincols:
                    dk = E.datasets
                    fits = E.__datatabs_fits__
                    for k in dk:
                        meta = E.getMetaData(k)
                        try:
                            thisres = meta['residue']
                        except:
                            thisres = k
                        if thisres == None:
                            thisres = k
                        if residues != '':
                            if s_residue.search(thisres) and not k in ignore:
                                if not ekinfound.has_key(protein+'$'+col):
                                    ekinfound[protein+'$'+col]=[]
                                ekinfound[protein+'$'+col].append(k)

                        if pka != '':
                            foundpka = 0
                            if k in fits.keys():
                                if fits[k] == None:
                                    continue
                                if fits[k].has_key('model'):
                                    model = fits[k]['model']
                                else:
                                    continue
                                if not foundfits.has_key(protein+'$'+col):
                                    foundfits[protein+'$'+col]={}
                                X = Fitting.getFitter(model)
                                pnames = X.names
                                i=0
                                #check if first num is larger, then swap
                                try:
                                    pk1=float(pkarange[0])
                                    pk2=float(pkarange[1])
                                except:
                                    print '<h2> Error: pka values are not valid </h2>'
                                    return
                                if pk1 > pk2:
                                    tmp = pk1
                                    pk1 = pk2
                                    pk2 = tmp

                                #iterate thru parameter names and match any pK fields
                                #this code is not that efficient!
                                for p in pnames:
                                    if 'pK' in p:
                                        pka = fits[k][i]
                                        if pka >= pk1 and pka <= pk2:
                                            foundpka=1
                                    i=i+1
                                #if match is 'ANY', just append dataset if not there already
                                #also for case if no residue value entered
                                if globalop == 'or' or residues == '':
                                    if foundpka == 1:
                                        if not ekinfound.has_key(protein+'$'+col):
                                            ekinfound[protein+'$'+col]=[]
                                        if not k in ekinfound[protein+'$'+col]:
                                            ekinfound[protein+'$'+col].append(k)
                                #if match is 'ALL', need to check dataset already found
                                #and if no pka found, remove it. if both are true keep it
                                elif globalop == 'and':
                                    if foundpka == 0:
                                        if ekinfound.has_key(protein+'$'+col) and k in ekinfound[protein+'$'+col]:
                                            #print 'removing', protein, col
                                            ekinfound[protein+'$'+col].remove(k)

                                foundfits[protein+'$'+col][k]=fits[k]
        #check for empty fields in ekinfound dict and delete them..
        for d in ekinfound.keys():
            if len(ekinfound[d])==0:
                del(ekinfound[d])

        #if no results, just say that and return
        if len(ekinfound)==0:
            print '<h2> Sorry, no records found that match these parameters. </h2>'
            return
        #top display button and options
        print '<form name="resultsform" action="%s/main.cgi" METHOD="POST" ENCTYPE="multipart/form-data">' %self.bindir
        self.write_sessionkey('show_datasets')
        print '<td valign=top align=right colspan=3> <input type=submit value="display selected" name=submit>'
        print '<label><input type=checkbox name="plotoption" value=3>single graph</label>'
        print '<label><input type=checkbox name="normalise" value=1>normalise</label>'
        print '<label><input type=checkbox name="logx" value=1>log-x</label>'
        print '<label><input type=checkbox name="logy" value=1>log-y</label>'
        print '<label><input type=checkbox name="legend" value=1>legend</label>'
        print '<input type=button value="Check All" onClick="checkAll(document.resultsform.residue)">'
        print '<input type=button value="Uncheck All" onClick="uncheckAll(document.resultsform.residue)"></td>'
        print '</tr>'
        #header
        cols=['protein','column','residues']
        for c in cols:
            print '<th>'+c+'</th>'
        print '</tr>'
        ekys = ekinfound.keys()
        ekys.sort()

        r=1
        for k in ekys:
            if r % 2 == 0:
                cls = "spec"
            else:
                cls = ""

            fields = k.split('$')
            protein = fields[0]
            column = fields[1]

            proteinname = DB[protein]['name']
            print '<tr>'
            print '<th class="spec">%s</th> <td> %s </td>' %(proteinname, column)
            print '<td>'
            for residue in ekinfound[k]:
                residuefield = k+"$"+residue
                fithtml = ''
                try:
                    print '<input type=checkbox id="residue" name="residue" value=%s>' %("'"+residuefield+"'")
                except:
                    print 'UnicodeEncodeError'
                    continue

                urlfields = urllib.urlencode({'login':self.user,'project':self.project,
                                           'residue': residuefield,'action': 'show_datasets'})
                print '<a href="/cgi-bin/titration_db/main.cgi?%s" target="_blank">%s</a>'\
                          % (urlfields, residue)

            print '</tr>'
            r=r+1
        print '</form>'
        print '</table>'
        self.footer()

        return
Esempio n. 5
0
    def show_datasets(self):
        """Show single or multiple dataset plot from search results"""
        plotoption = self.form.getfirst('plotoption')
        norm = bool(self.form.getfirst('normalise'))
        logx = bool(self.form.getfirst('logx'))
        logy = bool(self.form.getfirst('logy'))
        legend = bool(self.form.getfirst('legend'))

        DB = self.DB = self.connect()
        from PEATDB.Ekin.Web import EkinWeb
        ekincols = DB.ekintypes

        self.showHeader(menu=1)
        self.menu()
        sys.stdout.flush()

        #get dataset name(s) from form
        residuefield = self.form.getvalue('residue')

        if not isinstance(residuefield, list):
            residuefield = [residuefield]

        #prepare data, if more than one dataset from seperate proteins,
        print '</div>'
        print '<table id="mytable" cellspacing=0>'
        print '<tr><th>'
        print 'plotoption:', plotoption
        print 'norm:', str(norm)
        print 'log-x', logx
        print 'log-y', logy
        print 'legend', legend
        print '</table>'

        ekindata={}
        ekindata['__datatabs_fits__']={}
        ekindata['__meta_data__']={}
        #extracting seperate datasets and put them in ekindata
        for r in residuefield:
            fields = r.split('$')
            protein = fields[0]
            col = fields[1]
            d = fields[2]
            E = DB[protein][col]
            fits = E.__datatabs_fits__
            meta = E.__meta_data__
            protname = DB[protein]['name']
            ekindata['__datatabs_fits__'][d+'_'+col+'_'+protname] = fits[d]
            ekindata['__meta_data__'][d+'_'+col+'_'+protname] = meta[d]
            ekindata[d+'_'+col+'_'+protname] = E.getDataset(d)

        if plotoption == None:
            plotoption = 1
        else:
            plotoption = 3

        EW = EkinWeb()
        EW.showEkinPlots(ekindata, 'ALL', path=self.plotsdir,
                        imgpath=self.imagepath,
                        plotoption=plotoption, normalise=norm, legend=legend,
                        logx=logx, logy=logy)

        return
Esempio n. 6
0
    def plotpKDCalcs(self, calcs, Ed=None, option=1):
        """Do pKD calcs with exp data plots"""
        from PEATDB.Ekin.Web import EkinWeb
        from PEATDB.Ekin.Base import EkinProject
        from PEATDB.Ekin.Convert import EkinConvert
        from PEATDB.Ekin.Titration import TitrationAnalyser
        import PEATDB.Ekin.Utils as Utils
        t = TitrationAnalyser()
        c=calcs

        EW = EkinWeb()

        if option == '2':
            print '<a>Just showing experimental data</a>'
            EW.showEkinPlots(project=Ed, datasets='ALL',
                              path=self.plotsdir,
                              imgpath=self.imagepath)
            return
        #create ekin proj from pKD titcurves
        Ec = EkinProject()
        for r in c.keys():
            xd=[];yd=[]
            for i in c[r]:
                if type(i) is types.StringType:
                    continue
                xd.append(i)
                yd.append(c[r][i])
            edata=EkinConvert.xy2ekin([xd,yd])
            Ec.insertDataset(edata, r)

        print '<a>Please wait, fitting calcated curves...</a>'
        sys.stdout.flush()
        Ec.fitDatasets(models=['1 pKa 2 Chemical shifts'], silent=True)

        if option == '3':
            print '<a>Just showing pKD data</a>'
            EW.showEkinPlots(project=Ec, datasets='ALL',
                              path=self.plotsdir,
                              imgpath=self.imagepath)
            return

        #transform exp data names to match pKD ones
        s=':'
        usechainid = True
        #if pKD names have no chain id, we don't need one for exp names
        if Ec.datasets[0].startswith(':'):
            usechainid=False
        for d in Ed.datasets[:]:
            r = Ed.getMetaData(d)
            if r != None:
                if r['chain_id'] == None or usechainid == False:
                    chain = ''
                else:
                    chain = r['chain_id']
                new = chain+s+Utils.leadingZeros(r['res_num'],4)+s+r['residue']
                if new in Ed.datasets:
                    atom = r['atom']
                    new = new + '_' + atom
                Ed.renameDataset(d, new)

        #now we overlay the same datasets in Ed and Ec
        #also handles cases where same residue multiple times for diff atoms in exp data
        for d in Ed.datasets:
            if d in Ec.datasets:
                Ep = EkinProject()
                cdata = Ec.getDataset(d)
                Ep.insertDataset(cdata, d+'_pKD')
                Ep.setFitData(d+'_pKD', Ec.getFitData(d))
                ddata = Ed.getDataset(d)
                Ep.insertDataset(ddata, d+'_exp')
                Ep.setFitData(d+'_exp', Ed.getFitData(d))
                EW.showEkinPlots(project=Ep, datasets='ALL', plotoption=3,
                                 normalise=True, legend=True,
                                 path=self.plotsdir,
                                 imgpath=self.imagepath)

        return