Mercurial > repos > tduigou > get_sbml_model
comparison get_infos.py @ 8:06366f104f78 draft default tip
planemo upload for repository https://github.com/brsynth/synbiocad-galaxy-wrappers commit aa5a9ac47997d9c1718b9c7eaff1b83c63ac1d58
| author | tduigou |
|---|---|
| date | Fri, 29 Sep 2023 09:05:17 +0000 |
| parents | 33be348bf9d6 |
| children |
comparison
equal
deleted
inserted
replaced
| 7:33be348bf9d6 | 8:06366f104f78 |
|---|---|
| 3 readSBMLFromFile | 3 readSBMLFromFile |
| 4 ) | 4 ) |
| 5 from requests import get as r_get | 5 from requests import get as r_get |
| 6 | 6 |
| 7 | 7 |
| 8 def entry_point(): | 8 def get_biomass_rxn(sbml_doc): |
| 9 ''' | |
| 10 Returns the biomass reaction of the model | |
| 11 | |
| 12 Parameters | |
| 13 ---------- | |
| 14 sbml_doc: libsbml.SBMLDocument | |
| 15 SBML model | |
| 16 | |
| 17 Returns | |
| 18 ------- | |
| 19 biomass_rxn: libsbml.Reaction | |
| 20 Biomass reaction | |
| 21 ''' | |
| 22 reactions = sbml_doc.getModel().getListOfReactions() | |
| 23 # Search for 'biomass' keyword in reaction name | |
| 24 for rxn in reactions: | |
| 25 if 'biomass' in rxn.getName().lower(): | |
| 26 return rxn | |
| 27 # Search for 'biomass' keyword in products | |
| 28 # AND not in reactants | |
| 29 for rxn in reactions: | |
| 30 in_reactants = False | |
| 31 for reac in rxn.getListOfReactants(): | |
| 32 if 'biomass' in reac.getSpecies().lower(): | |
| 33 in_reactants = True | |
| 34 break | |
| 35 if not in_reactants: | |
| 36 for prod in rxn.getListOfProducts(): | |
| 37 if 'biomass' in prod.getSpecies().lower(): | |
| 38 return rxn | |
| 39 return None | |
| 40 | |
| 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(): | |
| 9 parser = ArgumentParser('Returns cell informations') | 88 parser = ArgumentParser('Returns cell informations') |
| 10 parser.add_argument( | 89 parser.add_argument( |
| 11 'infile', | 90 'infile', |
| 12 type=str, | 91 type=str, |
| 13 help='SBML input file (xml)' | 92 help='SBML input file (xml)' |
| 27 '--biomass', | 106 '--biomass', |
| 28 type=str, | 107 type=str, |
| 29 help='Path to store biomass reaction ID' | 108 help='Path to store biomass reaction ID' |
| 30 ) | 109 ) |
| 31 parser.add_argument( | 110 parser.add_argument( |
| 111 '--biomass-id', | |
| 112 type=str, | |
| 113 help='ID of biomass reaction' | |
| 114 ) | |
| 115 parser.add_argument( | |
| 32 '--hostid', | 116 '--hostid', |
| 33 type=str, | 117 type=str, |
| 34 help='Extended name of the host organism' | 118 help='Extended name of the host organism' |
| 35 ) | 119 ) |
| 36 parser.add_argument( | 120 parser.add_argument( |
| 37 '--taxid', | 121 '--taxid', |
| 38 type=str, | 122 type=str, |
| 39 help='Path to store host taxonomy ID' | 123 help='Path to store host taxonomy ID' |
| 40 ) | 124 ) |
| 41 params = parser.parse_args() | 125 params = parser.parse_args() |
| 126 return params | |
| 127 | |
| 128 | |
| 129 def entry_point(): | |
| 130 | |
| 131 params = args() | |
| 42 | 132 |
| 43 sbml_doc = readSBMLFromFile(params.infile) | 133 sbml_doc = readSBMLFromFile(params.infile) |
| 44 | 134 |
| 135 compartments = sbml_doc.getModel().getListOfCompartments() | |
| 136 comp_str = '' | |
| 137 for comp in compartments: | |
| 138 comp_str += f'{comp.getId()}\t{comp.getName()}\n' | |
| 45 if params.comp: | 139 if params.comp: |
| 46 compartments = sbml_doc.getModel().getListOfCompartments() | |
| 47 with open(params.comp, 'w') as f: | 140 with open(params.comp, 'w') as f: |
| 48 f.write('#ID\tNAME\n') | 141 f.write('#ID\tNAME\n') |
| 49 for comp in compartments: | 142 f.write(comp_str) |
| 50 f.write(f'{comp.getId()}\t{comp.getName()}\n') | 143 else: |
| 144 print('Compartments:') | |
| 145 for comp in compartments: | |
| 146 print(f'{comp.getId()}\t{comp.getName()}'.replace('\n', ' | ')) | |
| 51 | 147 |
| 148 if params.biomass_id: | |
| 149 biomass_rxn = sbml_doc.getModel().getReaction(params.biomass_id) | |
| 150 else: | |
| 151 biomass_rxn = get_biomass_rxn(sbml_doc) | |
| 152 if not biomass_rxn: | |
| 153 print('Warning: unable to retrieve biomass reaction') | |
| 154 biomass_id = '' | |
| 155 else: | |
| 156 biomass_id = biomass_rxn.getId() | |
| 52 if params.biomass: | 157 if params.biomass: |
| 53 reactions = sbml_doc.getModel().getListOfReactions() | |
| 54 with open(params.biomass, 'w') as f: | 158 with open(params.biomass, 'w') as f: |
| 55 f.write('#ID\n') | 159 f.write('#ID\n') |
| 56 for rxn in reactions: | 160 f.write(f'{biomass_id}\n') |
| 57 if 'biomass' in rxn.getId().lower(): | 161 else: |
| 58 f.write(f'{rxn.getId()}\n') | 162 print(f'Biomass reaction ID: {biomass_id}') |
| 163 | |
| 164 # Model from BiGG | |
| 165 if params.bigg: | |
| 166 taxid = get_taxon_id(params.hostid) | |
| 167 # Model from user | |
| 168 else: | |
| 169 taxid = params.hostid | |
| 59 | 170 |
| 60 if params.taxid: | 171 if params.taxid: |
| 61 hostname = '' | |
| 62 | |
| 63 # Model from BiGG | |
| 64 if params.bigg: | |
| 65 # Extended Name | |
| 66 server = 'http://bigg.ucsd.edu/api/v2/models/' | |
| 67 ext = params.hostid | |
| 68 r = r_get(server+ext, headers={ "Content-Type" : "application/json"}) | |
| 69 if not r.ok: | |
| 70 print(f"Warning: unable to retrieve host name for id {params.hostid}") | |
| 71 else: | |
| 72 try: | |
| 73 hostname = r.json()["organism"] | |
| 74 except KeyError: | |
| 75 print(f"Warning: unable to retrieve host name for id {params.hostid}") | |
| 76 if not hostname: | |
| 77 taxid = '' | |
| 78 else: | |
| 79 # TAXON ID | |
| 80 server = 'https://rest.ensembl.org' | |
| 81 ext = f'/taxonomy/id/{hostname}?' | |
| 82 r = r_get(server+ext, headers={ "Content-Type" : "application/json"}) | |
| 83 if not r.ok: | |
| 84 print(f"Warning: unable to retrieve taxonomy ID for host organism {hostname}") | |
| 85 else: | |
| 86 try: | |
| 87 taxid = r.json()["id"] | |
| 88 except KeyError: | |
| 89 print(f"Warning: unable to retrieve taxonomy ID for host organism {hostname}") | |
| 90 taxid = '' | |
| 91 | |
| 92 # Model from user | |
| 93 else: | |
| 94 taxid = params.hostid | |
| 95 | |
| 96 with open(params.taxid, 'w') as f: | 172 with open(params.taxid, 'w') as f: |
| 97 f.write('#ID\n') | 173 f.write('#ID\n') |
| 98 f.write(f'{taxid}\n') | 174 f.write(f'{taxid}\n') |
| 175 else: | |
| 176 print(f'Taxonomy ID: {taxid}') | |
| 99 | 177 |
| 100 | 178 |
| 101 if __name__ == "__main__": | 179 if __name__ == "__main__": |
| 102 entry_point() | 180 entry_point() |
