Mercurial > repos > tduigou > get_sbml_model
comparison get_infos.py @ 9:6a2871e89352 draft default tip
planemo upload for repository https://github.com/brsynth/synbiocad-galaxy-wrappers commit 537d7fefe58984b8f7ca66010153a2fdc35ddf4b
author | tduigou |
---|---|
date | Wed, 14 Feb 2024 15:25:38 +0000 |
parents | 768ed7cc0978 |
children |
comparison
equal
deleted
inserted
replaced
8:768ed7cc0978 | 9:6a2871e89352 |
---|---|
1 from argparse import ArgumentParser | 1 from argparse import ArgumentParser |
2 from libsbml import ( | 2 from libsbml import ( |
3 readSBMLFromFile | 3 readSBMLFromFile |
4 ) | 4 ) |
5 from requests import get as r_get | 5 from taxonid import get_taxonid |
6 | 6 |
7 | 7 |
8 def get_biomass_rxn(sbml_doc): | 8 def get_biomass_rxn(sbml_doc): |
9 ''' | 9 ''' |
10 Returns the biomass reaction of the model | 10 Returns the biomass reaction of the model |
37 if 'biomass' in prod.getSpecies().lower(): | 37 if 'biomass' in prod.getSpecies().lower(): |
38 return rxn | 38 return rxn |
39 return None | 39 return None |
40 | 40 |
41 | 41 |
42 def get_taxon_id(hostid): | |
43 ''' | |
44 Returns the taxonomy ID of the host organism | |
45 | |
46 Parameters | |
47 ---------- | |
48 hostid: str | |
49 Extended name of the host organism | |
50 | |
51 Returns | |
52 ------- | |
53 taxid: str | |
54 Taxonomy ID of the host organism | |
55 ''' | |
56 taxid = get_taxon_id(hostid) | |
57 hostname = '' | |
58 # Extended Name | |
59 server = 'http://bigg.ucsd.edu/api/v2/models/' | |
60 ext = hostid | |
61 r = r_get(server+ext, headers={ "Content-Type" : "application/json"}) | |
62 if not r.ok: | |
63 print(f"Warning: unable to retrieve host name for id {hostid}") | |
64 else: | |
65 try: | |
66 hostname = r.json()["organism"] | |
67 except KeyError: | |
68 print(f"Warning: unable to retrieve host name for id {hostid}") | |
69 if not hostname: | |
70 taxid = '' | |
71 else: | |
72 # TAXON ID | |
73 server = 'https://rest.ensembl.org' | |
74 ext = f'/taxonomy/id/{hostname}?' | |
75 r = r_get(server+ext, headers={ "Content-Type" : "application/json"}) | |
76 if not r.ok: | |
77 print(f"Warning: unable to retrieve taxonomy ID for host organism {hostname}") | |
78 else: | |
79 try: | |
80 taxid = r.json()["id"] | |
81 except KeyError: | |
82 print(f"Warning: unable to retrieve taxonomy ID for host organism {hostname}") | |
83 taxid = '' | |
84 return taxid | |
85 | |
86 | |
87 def args(): | 42 def args(): |
88 parser = ArgumentParser('Returns cell informations') | 43 parser = ArgumentParser('Returns cell informations') |
89 parser.add_argument( | 44 parser.add_argument( |
90 'infile', | 45 'infile', |
91 type=str, | 46 type=str, |
111 '--biomass-id', | 66 '--biomass-id', |
112 type=str, | 67 type=str, |
113 help='ID of biomass reaction' | 68 help='ID of biomass reaction' |
114 ) | 69 ) |
115 parser.add_argument( | 70 parser.add_argument( |
116 '--hostid', | 71 '--hostname', |
117 type=str, | 72 type=str, |
118 help='Extended name of the host organism' | 73 help='Name of the host organism' |
119 ) | 74 ) |
120 parser.add_argument( | 75 parser.add_argument( |
121 '--taxid', | 76 '--taxid', |
122 type=str, | 77 type=str, |
123 help='Path to store host taxonomy ID' | 78 help='Path to store host taxonomy ID' |
124 ) | 79 ) |
125 params = parser.parse_args() | 80 params = parser.parse_args() |
126 return params | 81 return params |
82 | |
83 | |
84 def get_taxon_id(hostid: str, bigg: bool): | |
85 ''' | |
86 Returns the taxonomy ID of the host organism | |
87 | |
88 Parameters | |
89 ---------- | |
90 hostid: str | |
91 Extended name of the host organism or host ID if from BiGG | |
92 bigg: bool | |
93 True if the model is from BiGG | |
94 | |
95 Returns | |
96 ------- | |
97 taxid: str | |
98 Taxonomy ID of the host organism | |
99 ''' | |
100 if not bigg: | |
101 return get_taxonid(hostid) | |
102 | |
103 hostname = '' | |
104 # Extended Name | |
105 server = 'http://bigg.ucsd.edu/api/v2/models/' | |
106 ext = hostid | |
107 r = r_get(server+ext, headers={ "Content-Type" : "application/json"}) | |
108 if not r.ok: | |
109 print(f"Warning: unable to retrieve host name for id {hostid}") | |
110 else: | |
111 try: | |
112 hostname = r.json()["organism"] | |
113 except KeyError: | |
114 print(f"Warning: unable to retrieve host name for id {hostid}") | |
115 if not hostname: | |
116 taxid = '' | |
117 else: | |
118 # TAXON ID | |
119 server = 'https://rest.ensembl.org' | |
120 ext = f'/taxonomy/id/{hostname}?' | |
121 r = r_get(server+ext, headers={ "Content-Type" : "application/json"}) | |
122 if not r.ok: | |
123 print(f"Warning: unable to retrieve taxonomy ID for host organism {hostname}") | |
124 else: | |
125 try: | |
126 taxid = r.json()["id"] | |
127 except KeyError: | |
128 print(f"Warning: unable to retrieve taxonomy ID for host organism {hostname}") | |
129 taxid = '' | |
130 return taxid | |
127 | 131 |
128 | 132 |
129 def entry_point(): | 133 def entry_point(): |
130 | 134 |
131 params = args() | 135 params = args() |
159 f.write('#ID\n') | 163 f.write('#ID\n') |
160 f.write(f'{biomass_id}\n') | 164 f.write(f'{biomass_id}\n') |
161 else: | 165 else: |
162 print(f'Biomass reaction ID: {biomass_id}') | 166 print(f'Biomass reaction ID: {biomass_id}') |
163 | 167 |
164 # Model from BiGG | 168 taxid = get_taxon_id(params.hostname, params.bigg) |
165 if params.bigg: | |
166 taxid = get_taxon_id(params.hostid) | |
167 # Model from user | |
168 else: | |
169 taxid = params.hostid | |
170 | 169 |
171 if params.taxid: | 170 if params.taxid: |
172 with open(params.taxid, 'w') as f: | 171 with open(params.taxid, 'w') as f: |
173 f.write('#ID\n') | 172 f.write('#ID\n') |
174 f.write(f'{taxid}\n') | 173 f.write(f'{taxid}\n') |