annotate rpviz/sbml2lists.py @ 25:2f8af738e139 draft

planemo upload commit c74b3ff2329f69ac7b309cc7d9bdf7b9d78106fb-dirty
author pablocarb
date Fri, 05 Jul 2019 17:24:48 -0400
parents fe78fd6b315a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
1 #!/usr/bin/env python
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
2 # coding: utf-8
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
3 '''To visualize a SBML file'''
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
4 # In[3]:
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
5
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
6
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
7 import libsbml
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
8 import os
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
9 from .smile2picture import picture
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
10 #import networkx as nx
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
11 #import matplotlib.pyplot as plt
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
12
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
13
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
14
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
15 # In[4]:
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
16 def sbml2list(file):
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
17
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
18 #open the SBML using libsbml
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
19
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
20 doc = libsbml.readSBML(file)
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
21 name=os.path.basename(file)
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
22
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
23
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
24 # In[8]:
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
25
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
26
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
27 #return the model from the SBML document using libsbml
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
28 model = doc.model
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
29
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
30
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
31 # In[9]:
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
32
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
33
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
34 #we will use the groups package to return the retropath pathway
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
35 #that is all the reactions that are associated with the heterologous
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
36 #pathway
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
37 groups = model.getPlugin('groups')
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
38
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
39
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
40 # In[10]:
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
41
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
42
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
43 #in the rpFBA script, rp_pathway is the default name
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
44 rp_pathway = groups.getGroup('rp_pathway')
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
45
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
46
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
47 # In[63]:
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
48
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
49 rlist=[]
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
50 LR=[]
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
51
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
52 heterologous_pathway_dG_prime_o = {}
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
53
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
54
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
55 #loop through all the members of the rp_pathway
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
56 for member in rp_pathway.getListOfMembers():
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
57 #fetch the reaction according to the member id
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
58 reaction = model.getReaction(member.getIdRef())
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
59 rlist.append(reaction)
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
60 LR.append(str(reaction))
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
61 #get the annotation of the reaction
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
62 #includes the MIRIAM annotation and the IBISBA ones
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
63 annotation = reaction.getAnnotation()
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
64 ibisba_annotation = annotation.getChild('RDF').getChild('Ibisba').getChild('ibisba')
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
65 #extract one of the ibisba annotation values
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
66 heterologous_pathway_dG_prime_o[member.getIdRef()] = ibisba_annotation.getChild('dG_prime_o').getAttrValue('value')
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
67
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
68
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
69
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
70 # In[64]:
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
71
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
72 heterologous_pathway_dG_prime_o
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
73
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
74 Lreact=[]
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
75 Lprod=[]
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
76
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
77 for reaction in range(len(rlist)):
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
78 Lreact.append([p.species for p in rlist[reaction].reactants]) #get list of reactants id
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
79 Lprod.append([p.species for p in rlist[reaction].products]) #get list of products id
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
80
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
81 mem = []
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
82 for member in rp_pathway.getListOfMembers():
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
83 reac = model.getReaction(member.getIdRef())
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
84 for pro in reac.getListOfProducts():
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
85 mem.append(pro.getSpecies())
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
86 Lprod.append(pro.getSpecies())
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
87 for rea in reac.getListOfReactants():
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
88 mem.append(rea.getSpecies())
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
89 Lreact.append(rea.getSpecies())
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
90
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
91 #mem = list(set([i for i in mem if i[0:3]!='MNX']))
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
92 species_smiles = {}
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
93 species_links={}
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
94 species_names={}
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
95 #loop through all the members of the rp_pathway
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
96 for member in list(set([i for i in mem])):
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
97 #fetch the species according to the member id
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
98 reaction = model.getSpecies(member)
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
99 spname=reaction.getName()
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
100 if spname:
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
101 species_names[member]=spname
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
102 #get the annotation of the species
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
103 #includes the MIRIAM annotation and the IBISBA ones
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
104 annotation = reaction.getAnnotation()
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
105 ibisba_annotation = annotation.getChild('RDF').getChild('Ibisba').getChild('ibisba')
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
106 #extract one of the ibisba annotation values
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
107 smiles = ibisba_annotation.getChild('smiles').getChild(0).toXMLString()
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
108 if smiles:
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
109 species_smiles[member] = smiles
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
110 link_annotation=annotation.getChild('RDF').getChild('Description').getChild('is').getChild('Bag')
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
111 for i in range(link_annotation.getNumChildren()):
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
112 str_annot = link_annotation.getChild(i).getAttrValue(0) #Here we get the attribute at location "0". It works since there is only one
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
113 if str_annot.split('/')[-2]=='metanetx.chemical':
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
114 species_links[member]=str_annot #here is the MNX code returned
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
115
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
116
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
117 # In[64]:
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
118
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
119 image=picture(species_smiles)
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
120
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
121 return(LR, Lreact, Lprod, name, species_smiles,image, species_names, species_links)
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
122
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
123
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
124
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
125
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
126 #Cytoscape Network
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
127 #from nxvisualizer import network
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
128 #network(LR,Lreact,Lprod,name,species_smiles,image)
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
129
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
130 #Convert network to json file
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
131 #from network2json import network2 #to convert the lists in a json network
fe78fd6b315a planemo upload commit 87db86a34f2d92eb2c9756bf9ee53ae2970554d5
pablocarb
parents:
diff changeset
132 #network2(LR,Lreact,Lprod,name,species_smiles,image,species_names,species_links)