comparison get_infos.py @ 10:dc1167469d62 draft

planemo upload for repository https://github.com/brsynth/synbiocad-galaxy-wrappers commit 94f89a44e330ccfccc8d94b5e7acf583c9d39343
author tduigou
date Thu, 27 Mar 2025 09:27:06 +0000
parents 6a2871e89352
children 062f51695ae0
comparison
equal deleted inserted replaced
9:6a2871e89352 10:dc1167469d62
1 from argparse import ArgumentParser 1 from argparse import ArgumentParser
2 from libsbml import ( 2 from libsbml import readSBMLFromFile
3 readSBMLFromFile
4 )
5 from taxonid import get_taxonid 3 from taxonid import get_taxonid
4 from requests import get as r_get
6 5
7 6
8 def get_biomass_rxn(sbml_doc): 7 def get_biomass_rxn(sbml_doc):
9 ''' 8 """
10 Returns the biomass reaction of the model 9 Returns the biomass reaction of the model
11 10
12 Parameters 11 Parameters
13 ---------- 12 ----------
14 sbml_doc: libsbml.SBMLDocument 13 sbml_doc: libsbml.SBMLDocument
15 SBML model 14 SBML model
16 15
17 Returns 16 Returns
18 ------- 17 -------
19 biomass_rxn: libsbml.Reaction 18 biomass_rxn: libsbml.Reaction
20 Biomass reaction 19 Biomass reaction
21 ''' 20 """
22 reactions = sbml_doc.getModel().getListOfReactions() 21 reactions = sbml_doc.getModel().getListOfReactions()
23 # Search for 'biomass' keyword in reaction name 22 # Search for 'biomass' keyword in reaction name
24 for rxn in reactions: 23 for rxn in reactions:
25 if 'biomass' in rxn.getName().lower(): 24 if "biomass" in rxn.getName().lower():
26 return rxn 25 return rxn
27 # Search for 'biomass' keyword in products 26 # Search for 'biomass' keyword in products
28 # AND not in reactants 27 # AND not in reactants
29 for rxn in reactions: 28 for rxn in reactions:
30 in_reactants = False 29 in_reactants = False
31 for reac in rxn.getListOfReactants(): 30 for reac in rxn.getListOfReactants():
32 if 'biomass' in reac.getSpecies().lower(): 31 if "biomass" in reac.getSpecies().lower():
33 in_reactants = True 32 in_reactants = True
34 break 33 break
35 if not in_reactants: 34 if not in_reactants:
36 for prod in rxn.getListOfProducts(): 35 for prod in rxn.getListOfProducts():
37 if 'biomass' in prod.getSpecies().lower(): 36 if "biomass" in prod.getSpecies().lower():
38 return rxn 37 return rxn
39 return None 38 return None
40 39
41 40
42 def args(): 41 def args():
43 parser = ArgumentParser('Returns cell informations') 42 parser = ArgumentParser("Returns cell informations")
44 parser.add_argument( 43 parser.add_argument("infile", type=str, help="SBML input file (xml)")
45 'infile', 44 parser.add_argument("--hostname-or-id", type=str, help="Hostname or model ID")
46 type=str, 45 parser.add_argument("--comp", type=str, help="Path to store cell compartments")
47 help='SBML input file (xml)' 46 parser.add_argument("--biomass", type=str, help="Path to store biomass reaction ID")
48 ) 47 parser.add_argument("--biomass-id", type=str, help="ID of biomass reaction")
49 # argument to tag file from BiGG 48 parser.add_argument("--taxid", type=str, help="Path to store host taxonomy ID")
50 parser.add_argument(
51 '--bigg',
52 action='store_true',
53 help='Tag file from BiGG'
54 )
55 parser.add_argument(
56 '--comp',
57 type=str,
58 help='Path to store cell compartments'
59 )
60 parser.add_argument(
61 '--biomass',
62 type=str,
63 help='Path to store biomass reaction ID'
64 )
65 parser.add_argument(
66 '--biomass-id',
67 type=str,
68 help='ID of biomass reaction'
69 )
70 parser.add_argument(
71 '--hostname',
72 type=str,
73 help='Name of the host organism'
74 )
75 parser.add_argument(
76 '--taxid',
77 type=str,
78 help='Path to store host taxonomy ID'
79 )
80 params = parser.parse_args() 49 params = parser.parse_args()
81 return params 50 return params
82 51
83 52
84 def get_taxon_id(hostid: str, bigg: bool): 53 def get_organism_from_bigg_model(model_id):
85 ''' 54 """Try to retrieve organism info from BiGG Models for a given model ID."""
86 Returns the taxonomy ID of the host organism 55 url = f"http://bigg.ucsd.edu/api/v2/models/{model_id}"
56 try:
57 response = r_get(url)
58 if response.status_code == 200:
59 data = response.json()
60 organism = data.get("organism")
61 return organism
62 except Exception as e:
63 print(f"Error querying BiGG: {e}")
64 return None
65
66 def get_taxon_id(input_name):
67 """Try BiGG model name first, then NCBI directly."""
68 print(f"Trying input: {input_name}")
87 69
88 Parameters 70 # Try resolving as a BiGG model
89 ---------- 71 organism = get_organism_from_bigg_model(input_name)
90 hostid: str 72 if organism:
91 Extended name of the host organism or host ID if from BiGG 73 print(f"Model '{input_name}' maps to organism: {organism}")
92 bigg: bool 74 taxon_id = get_taxonid(organism)
93 True if the model is from BiGG 75 if taxon_id:
94 76 return taxon_id
95 Returns
96 -------
97 taxid: str
98 Taxonomy ID of the host organism
99 '''
100 if not bigg:
101 return get_taxonid(hostid)
102 77
103 hostname = '' 78 # If not a model, try directly as an organism name
104 # Extended Name 79 print(f"Trying NCBI search with input: {input_name}")
105 server = 'http://bigg.ucsd.edu/api/v2/models/' 80 return get_taxonid(input_name)
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
131 81
132 82
133 def entry_point(): 83 def entry_point():
84 params = args()
134 85
135 params = args() 86 # test if the file exists
87 with open(params.infile):
88 pass
136 89
137 sbml_doc = readSBMLFromFile(params.infile) 90 sbml_doc = readSBMLFromFile(params.infile)
138 91
139 compartments = sbml_doc.getModel().getListOfCompartments() 92 compartments = sbml_doc.getModel().getListOfCompartments()
140 comp_str = '' 93 comp_str = ""
141 for comp in compartments: 94 for comp in compartments:
142 comp_str += f'{comp.getId()}\t{comp.getName()}\n' 95 comp_str += f"{comp.getId()}\t{comp.getName()}\n"
96 print("Compartments:")
97 for comp in compartments:
98 print(f"{comp.getId()}\t{comp.getName()}".replace("\n", " | "))
143 if params.comp: 99 if params.comp:
144 with open(params.comp, 'w') as f: 100 with open(params.comp, "w") as f:
145 f.write('#ID\tNAME\n') 101 f.write("#ID\tNAME\n")
146 f.write(comp_str) 102 f.write(comp_str)
147 else:
148 print('Compartments:')
149 for comp in compartments:
150 print(f'{comp.getId()}\t{comp.getName()}'.replace('\n', ' | '))
151 103
152 if params.biomass_id: 104 if params.biomass_id:
153 biomass_rxn = sbml_doc.getModel().getReaction(params.biomass_id) 105 biomass_rxn = sbml_doc.getModel().getReaction(params.biomass_id)
154 else: 106 else:
155 biomass_rxn = get_biomass_rxn(sbml_doc) 107 biomass_rxn = get_biomass_rxn(sbml_doc)
156 if not biomass_rxn: 108 if not biomass_rxn:
157 print('Warning: unable to retrieve biomass reaction') 109 print("Warning: unable to retrieve biomass reaction")
158 biomass_id = '' 110 biomass_id = ""
159 else: 111 else:
160 biomass_id = biomass_rxn.getId() 112 biomass_id = biomass_rxn.getId()
113 print(f"Biomass reaction ID: {biomass_id}")
161 if params.biomass: 114 if params.biomass:
162 with open(params.biomass, 'w') as f: 115 with open(params.biomass, "w") as f:
163 f.write('#ID\n') 116 f.write("#ID\n")
164 f.write(f'{biomass_id}\n') 117 f.write(f"{biomass_id}\n")
118
119 if params.hostname_or_id:
120 taxid = get_taxon_id(params.hostname_or_id)
165 else: 121 else:
166 print(f'Biomass reaction ID: {biomass_id}') 122 model_id = sbml_doc.getModel().getId()
167 123 taxid = -1
168 taxid = get_taxon_id(params.hostname, params.bigg) 124 if model_id:
125 taxid = get_taxon_id(sbml_doc.getModel().getId())
126 if taxid == -1:
127 # Try with model name
128 model_name = sbml_doc.getModel().getName()
129 if model_name:
130 taxid = get_taxon_id(sbml_doc.getModel().getName())
131 print(f"Taxonomy ID: {taxid}")
169 132
170 if params.taxid: 133 if params.taxid:
171 with open(params.taxid, 'w') as f: 134 with open(params.taxid, "w") as f:
172 f.write('#ID\n') 135 f.write("#ID\n")
173 f.write(f'{taxid}\n') 136 f.write(f"{taxid}\n")
174 else:
175 print(f'Taxonomy ID: {taxid}')
176 137
177 138
178 if __name__ == "__main__": 139 if __name__ == "__main__":
179 entry_point() 140 entry_point()