Changeset 241


Ignore:
Timestamp:
Oct 21, 2009, 1:18:37 PM (10 years ago)
Author:
pvkouteren
Message:

Added transfac methods, removed bogus methods.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • web/IbidasAtomicServicesTemp.py

    r234 r241  
    3030                    iconfig['database.port'],iconfig['database.name'],\
    3131                    iconfig['database.user'],iconfig['database.password']).getConSet()
     32
     33    """
     34    Helper functions
     35    """
    3236
    3337    def _tableToSerializer(self, table):
     
    6569        return objects
    6670
     71    def _getItemIDs(self, ts):
     72        return self.cd.item[_.display_name.within(*ts)].select(_.item_id)().tolist()
     73   
     74    def _loadTransfac(self):
     75        if not hasattr(self, 'transfac'):
     76            from parsers.transfac import TransfacFactorParser, TransfacGeneParser, TransfacSiteParser
     77            self.transfac = {}
     78            # load factors
     79            transfac_dir = '/Users/patrick/Documents/Workspace/ibidas/data/transfac/'
     80            tfp = TransfacFactorParser(transfac_dir + 'factor.dat')
     81            tfp.parse(toCont = True)
     82            self.transfac['factor'] = tfp
     83           
     84            tgp = TransfacGeneParser(transfac_dir + 'gene.dat')
     85            tgp.parse(toCont = True)
     86            self.transfac['gene'] = tgp
     87           
     88            tsp = TransfacSiteParser(transfac_dir + 'site.dat')
     89            tsp.parse(toCont = True)
     90            self.transfac['site'] = tsp
     91           
     92   
     93    """
     94    Test
     95    """
     96
    6797    @soapmethod(_returns=String)
    68     def testConnection(self):
    69         """Function to test the connection, returns a string."""
    70         #print "Called testConnection!"
     98    def foo(self):
     99        """ Function to test the connection, returns a string. (Guess which..) """
    71100        time.sleep(5)
    72         return "blablablabla"
    73    
    74     @soapmethod(String,_returns=String)
    75     def say_hello_once(self, name):
    76         """ Say hello once. """
    77         return "Hello " + name
    78 
    79     @soapmethod(String,Integer,_returns=Array(String))
    80     def say_hello(self,name,times):
    81         """ Function to say hello a couple of times to someone. """
    82         results = []
    83         for i in range(0,times):
    84             results.append('Hello, %s'%name)
    85         return results
    86    
    87     @soapmethod(_returns=IbidasItem)
    88     def returnTestItem(self):
    89         """ Get a user-defined IbidasItem serializer """
    90         ii = IbidasItem(item_id=1, source_id=1, display_name="test", type_id=1)
    91         return ii
    92    
    93     @soapmethod(_returns=IbidasTerm)
    94     def returnTestTerm(self):
    95         """ Get a user-defined IbidasTerm serializer """
    96         it = IbidasTerm(source_id=1,term_id=1,identifier="Test",name="Test",description="Test")
    97         return it
    98    
    99     @soapmethod(_returns=IbidasMessage)
    100     def returnTestMessage(self):
    101         """ Get a user-defined IbidasMessage serializer """
    102         item = self.returnTestItem()
    103         term = self.returnTestTerm()
    104         im = IbidasMessage()
    105         im.add(item)
    106         im.add(term)
    107         return im
    108    
    109     @soapmethod(Array(IbidasItem), IbidasTerm, _returns=IbidasMessage)
    110     def addItemsAndTerm(self, items, term):
    111         """ Return a Message with some items and a term """
    112         im = IbidasMessage()
    113         for item in items:
    114             im.add(item)
    115         im.add(term)
    116         return im
    117        
    118     @soapmethod(IbidasItem, IbidasTerm, _returns=IbidasMessage)
    119     def addItemAndTerm(self, item, term):
    120         """ Return a Message with some items and a term """
    121         im = IbidasMessage()
    122         im.add(item)
    123         im.add(term)
    124         return im
     101        return "bar"
    125102         
    126     ''' More serious methods '''
     103    """
     104    General
     105    """
    127106   
    128107    @soapmethod(_returns=Array(IbidasSet))
     
    166145            result.append(identifiers)
    167146        return result
    168            
     147   
     148    @soapmethod(Array(String), _returns=Array(Integer))
     149    def getIbidasIDs(self, names):
     150        """ Return the IbidasItem identifiers for display names """
     151        print "names: " + str(names)
     152        return self._getItemIDs(names)
     153         
     154           
     155    """
     156    MSigDB
     157    """
     158     
    169159    @soapmethod(Array(String), _returns=Array(Array(String)))
    170160    def getMSigDBGroups(self, gene_list):
     
    195185        return result
    196186   
    197     def _getItemIDs(self, ts):
    198         return self.cd.item[_.display_name.within(*ts)].select(_.item_id)().tolist()
     187    @soapmethod(Integer, _returns=Array(String))
     188    def getMSigDBCategoryGenes(self, category_id):
     189        """ Get all genes from a particular MSigDB category """
     190        result = []
     191        g_res = self.cd.item_prop_term.join(self.cd.item, 'item_id', 'item_id').join(self.cd.term, 'term_id', 'term_id')[_.identifier.like("c"+str(category_id)+":%")].select('display_name')()
     192        return g_res
     193 
     194    """
     195    By DB
     196    """
    199197   
    200198    @soapmethod(Array(String), Array(String), String, Float, _returns=Boolean)
    201     def geneEnrichmentTest(self, top, back, reference, threshold):
     199    def geneEnrichmentTestByDB(self, top, back, reference, threshold):
    202200        """ Returns whether a geneset is enriched """
    203         result = self.calculateGeneEnrichment(top, back, reference)
     201        result = self.hypergeometricTestByDB(top, back, reference)
    204202        return result < threshold
    205203   
    206204    @soapmethod(Array(String), Array(String), String, _returns=Float)
    207     def calculateGeneEnrichment(self, top, back, reference):
     205    def hypergeometricTestByDB(self, top, back, reference):
    208206        """ Returns the value of the gene enrichment (hypergeometric) test """
     207        #print "calculateGeneEnrichment(" + str(top) + ", " + str(back) + ", " + str(reference) + ")"
    209208        top_ids  = self._getItemIDs(top)
     209        #print "top_ids: " + str(top_ids)
    210210        back_ids = self._getItemIDs(back)
    211         set_id = self.cd.set[_.name == reference].select(_.set_id)()
     211        #print "back_ids: " + str(back_ids)
     212        set_id = self.cd.set[_.name.like(reference)].select(_.set_id)()
     213        #print "set_id: " + str(set_id)
    212214        set_ids = self.cd.item_prop_term[_.source_id == set_id].select(_.item_id)().tolist()
    213215        h = Hypergeometric(top_ids, back_ids, set_ids)
    214216        return h.calculate()
    215217   
     218    @soapmethod(Array(String), Array(String), String, Float, _returns=Array(Integer))
     219    def returnEnrichedGenesByDB(self, top, back, reference, threshold):
     220        """ Returns a list of IbidasItem IDs if they are enriched """
     221        if self.geneEnrichmentTestByDB(top, back, reference, threshold):
     222            return self.getIbidasIDs(top)
     223        else:
     224            return None
     225   
    216226    @soapmethod(Array(Array(String)), Array(Array(String)), Array(String), Float, _returns=Array(Array(Array(Boolean))))
    217     def geneEnrichmentTests(self, tops, backs, references, threshold):
     227    def geneEnrichmentTestsByDBs(self, tops, backs, references, threshold):
    218228        """ Do multiple enrichment tests with multiple lists and multiple backgrounds on multiple reference sets """
    219229        tbsrs = []
     
    223233                reference_array = []
    224234                for reference in references:
    225                     reference_array.append(self.geneEnrichmentTest(top, back, reference, threshold))
     235                    reference_array.append(self.geneEnrichmentTestByDB(top, back, reference, threshold))
    226236                tbrs.append(reference_array)
    227237            tbsrs.append(tbrs)
    228238        return tbsrs
    229                    
     239   
     240    """
     241    By List
     242    """
     243   
     244    @soapmethod(Array(String), Array(String), Array(String), Float, _returns=Boolean)
     245    def geneEnrichmentTestByList(self, top, back, reference, threshold):
     246        """ Returns whether a geneset is enriched """
     247        result = self.hypergeometricTestByList(top, back, reference)
     248        return result < threshold
     249   
     250    @soapmethod(Array(String), Array(String), Array(String), _returns=Float)
     251    def hypergeometricTestByList(self, top, back, reference):
     252        """ Returns the value of the hypergeometric test """
     253        print "reference is " + str(reference)
     254        top_ids = self._getItemIDs(top)
     255        print "top_ids: " + str(top_ids)       
     256        back_ids = self._getItemIDs(back)
     257        print "back_ids: " + str(back_ids)
     258        ref_ids = self._getItemIDs(reference)
     259        print "ref_ids: " + str(ref_ids)
     260        h = Hypergeometric(top_ids, back_ids, ref_ids)
     261        return h.calculate()
     262       
     263    @soapmethod(Array(Array(String)), Array(Array(String)), Array(Array(String)), Float, _returns=Array(Array(Array(Boolean))))
     264    def geneEnrichmentTestsByLists(self, tops, backs, references, threshold):
     265        """ Do multiple enrichment tests with multiple lists and multiple backgrounds on multiple reference sets """
     266        tbsrs = []
     267        for top in tops:   
     268            tbrs = []
     269            for back in backs:
     270                reference_array = []
     271                for reference in references:
     272                    reference_array.append(self.geneEnrichmentTestByList(top, back, reference, threshold))
     273                tbrs.append(reference_array)
     274            tbsrs.append(tbrs)
     275        return tbsrs
     276             
     277    @soapmethod(Array(String), Array(String), Array(String), Float, _returns=Array(Integer))
     278    def returnEnrichedGenesByList(self, top, back, reference, threshold):
     279        """ Returns a list of IbidasItem IDs if they are enriched """
     280        if self.geneEnrichmentTestByList(top, back, reference, threshold):
     281            return self.getIbidasIDs(top)
     282        else:
     283            return None
     284       
     285       
     286       
     287       
     288    """
     289    Transfac services??
     290    """
     291   
     292    @soapmethod(String, _returns=Array(String))
     293    def getTransfacGeneID(self, gene):
     294        """ Return a transfac gene ID """
     295        self._loadTransfac()
     296        tg = self.transfac['gene']
     297        res = tg.accessions[_.accession == gene].select('ac_id')()
     298        if isinstance(res, basestring):
     299            return [res]
     300        else:
     301            return list(set(res.tolist()))
     302   
     303    @soapmethod(Array(String), _returns=Array(String))
     304    def getEncodedFactorIDs(self, genes):
     305        """ Return the transcription factor IDs which are encoded by particular genes """
     306        self._loadTransfac()
     307        tg = self.transfac['gene']
     308        res = tg.encoded_factors[_.gene_id.within(*genes)].select('tf_id')()
     309        if isinstance(res, basestring):
     310            return [res]
     311        else:
     312            return list(set(res.tolist()))
     313   
     314    @soapmethod(String, _returns=Array(String))
     315    def getTransfacFactorID(self, factor):
     316        """ Return a transfac factor ID """
     317        self._loadTransfac()
     318        tf = self.transfac['factor']
     319        return list(set(tf.accessions[_.accession == factor].select('ac_id')().tolist()))
     320   
     321    @soapmethod(Array(String), _returns=Array(String))
     322    def getTFsBindingsiteIDs(self,tfs):
     323        """ Return the bindingsites of the input transcription factors """
     324        self._loadTransfac()
     325        tf = self.transfac['factor']
     326        return list(set(tf.bindingsites[_.TF_id.within(*tfs)].select('site')().tolist()))
     327   
     328    @soapmethod(Array(String), _returns=Array(String))
     329    def getGenesByBindingsite(self, sites):
     330        """ Return the genes by binding site ID """
     331        self._loadTransfac()
     332        ts = self.transfac['site']
     333        return list(set(ts.accessions[_.ac_id.within(*sites)].select('accession')().tolist()))
     334   
     335    @soapmethod(Array(String), _returns=Array(String))
     336    def getGenesByTransfacAccessions(self, acs):
     337        """ Return genes by TRANSFAC accessions """
     338        self._loadTransfac()
     339        tg = self.transfac['gene']
     340        return list(set(tg.accessions[_.ac_id.within(*acs)].select('accession')().tolist()))
     341   
     342    @soapmethod(String, _returns=Array(String))
     343    def getTranscriptionFactorsOfGene_old(self, gene):
     344        """ Return a list of transcription factors for a gene """
     345        # Get the gene identifiers
     346        gene_ids = self.getTransfacGeneID(gene)
     347        # Get the transcription factor this
     348        encoded_factor_ids = self.getEncodedFactorIDs(gene_ids)
     349        binding_site_ids = self.getTFsBindingsiteIDs(encoded_factor_ids)
     350        res_gene_ids = self.getGenesByBindingsite(binding_site_ids)
     351        res_gene_names = self.getGenesByTransfacAccessions(res_gene_ids)
     352        return res_gene_names
     353   
     354    def _getGeneBindingsites(self, gene_ids):
     355        self._loadTransfac()
     356        ts = self.transfac['site']
     357        res = ts.accessions[_.accession.within(*gene_ids)].select('ac_id')()
     358        if isinstance(res, basestring):
     359            return [res]
     360        else:
     361            return list(set(res))
     362   
     363    def _getBindingTFs(self, bindingsite_ids):
     364        self._loadTransfac()
     365        tf = self.transfac['factor']
     366        res = tf.bindingsites[_.site.within(*bindingsite_ids)].select('TF_id')()
     367        if isinstance(res, basestring):
     368            return [res]
     369        else:
     370            return list(set(res))
     371   
     372    def _getEncodingGenes(self, tf_ids):
     373        self._loadTransfac()
     374        tf = self.transfac['factor']
     375        res = tf.encoding_genes[_.TF_id.within(*tf_ids)].select('gene')()
     376        if isinstance(res, basestring):
     377            return [res]
     378        else:
     379            return list(set(res))
     380       
     381    def _getTransfacGeneAccessions(self, gene_ids):
     382        self._loadTransfac()
     383        tg = self.transfac['gene']
     384        res = tg.accessions[_.ac_id.within(*gene_ids)].select('accession')()
     385        if isinstance(res, basestring):
     386            return [res]
     387        else:
     388            return list(set(res))
     389   
     390    @soapmethod(String, _returns=Array(String))
     391    def getTranscriptionFactorsOfGene(self, gene):
     392        """ Return a list of transcription factors for a gene """
     393        gene_ids = []
     394        bindingsite_ids = []
     395        binding_tf_ids = []
     396        res_gene_ids = []
     397        res_gene_acc = []
     398        # Get the gene identifiers
     399        gene_ids = self.getTransfacGeneID(gene)
     400        print "gene_ids: " + str(gene_ids)
     401        # Get the bindingsites of this gene
     402        bindingsite_ids = self._getGeneBindingsites(gene_ids)
     403        print "bindingsite_ids: " + str(bindingsite_ids)
     404        # Get TFs binding to this bindingsite
     405        binding_tf_ids = self._getBindingTFs(bindingsite_ids)
     406        print "binding_tf_ids: " + str(binding_tf_ids)
     407        # Get genes these TFs encode for
     408        res_gene_ids = self._getEncodingGenes(binding_tf_ids)
     409        print "res_gene_ids: " + str(res_gene_ids)
     410        # Get the gene accessions
     411        res_gene_acc = self._getTransfacGeneAccessions(res_gene_ids)
     412        print "res_gene_acc: " + str(res_gene_acc)
     413        return res_gene_acc
     414       
Note: See TracChangeset for help on using the changeset viewer.