Snippets

Dénes Türei pypath, Entrez IDs and pathways

Created by Dénes Türei last modified
#!/usr/bin/python

#
# Denes Turei, turei.denes@gmail.com
#

import pypath

pa = pypath.PyPath()
pa.load_omnipath()
m = pa.mapper
pa.graph.vs['entrez'] = list(map(lambda v: m.map_name(v['name'], 'uniprot', 'entrez', ncbi_tax_id = 9606), pa.graph.vs))
pa.load_all_pathways() # this loads pathway annotations from KEGG, Signor and SignaLink; NetPath is currently not included

# here you already have everything loaded
# and you can access the data by the attributes of the igraph object,
# for example:

header = [
    'entrezA', 'entrezB', 'is_stimulation', 'is_inhibition',
    'KEGG_pathways_A', 'SignaLink_pathways_A', 'Signor_pathways_A',
    'KEGG_pathways_B', 'SignaLink_pathways_B', 'Signor_pathways_B'
]

with open('pathways.tab', 'w') as outf:
    
    _ = outf.write('%s\n' % '\t'.join(header))
    
    for e in pa.graph.es:
        
        uniprots = {
            pa.graph.vs[e.source]['name']: e.source,
            pa.graph.vs[e.target]['name']: e.target
        }
        _dir = e['dirs']
        directions = _dir.which_dirs()
        directed = _dir.is_directed()
        
        if not len(directions):
            directions.append(tuple(uniprots.keys()))
        
        for d in directions:
            
            positive = _dir.is_stimulation(d)
            negative = _dir.is_inhibition(d)
            
            unknown_effect = not positive and not negative
            source = pa.graph.vs[uniprots[d[0]]]
            target = pa.graph.vs[uniprots[d[1]]]
            
            for entrez1 in source['entrez']:
                
                for entrez2 in target['entrez']:
                    
                    _ = outf.write('%s\n' % \
                       '\t'.join([
                        entrez1,
                        entrez2,
                        str(int(positive)),
                        str(int(negative)),
                        ';'.join(source['kegg_pathways']),
                        ';'.join(source['signalink_pathways']),
                        ';'.join(source['signor_pathways']),
                        ';'.join(target['kegg_pathways']),
                        ';'.join(target['signalink_pathways']),
                        ';'.join(target['signor_pathways'])
                        # and add other attributes you wish to export
                        ])
                    )

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.