diff get_infos.py @ 8:768ed7cc0978 draft

planemo upload for repository https://github.com/brsynth/synbiocad-galaxy-wrappers commit aa5a9ac47997d9c1718b9c7eaff1b83c63ac1d58
author tduigou
date Fri, 29 Sep 2023 09:03:09 +0000
parents 8dc4d3964ab5
children 6a2871e89352
line wrap: on
line diff
--- a/get_infos.py	Mon Sep 04 14:19:43 2023 +0000
+++ b/get_infos.py	Fri Sep 29 09:03:09 2023 +0000
@@ -5,7 +5,86 @@
 from requests import get as r_get
 
 
-def entry_point():
+def get_biomass_rxn(sbml_doc):
+    '''
+    Returns the biomass reaction of the model
+    
+    Parameters
+    ----------
+    sbml_doc: libsbml.SBMLDocument
+        SBML model
+        
+    Returns
+    -------
+    biomass_rxn: libsbml.Reaction
+        Biomass reaction
+    '''
+    reactions = sbml_doc.getModel().getListOfReactions()
+    # Search for 'biomass' keyword in reaction name
+    for rxn in reactions:
+        if 'biomass' in rxn.getName().lower():
+            return rxn
+    # Search for 'biomass' keyword in products
+    # AND not in reactants
+    for rxn in reactions:
+        in_reactants = False
+        for reac in rxn.getListOfReactants():
+            if 'biomass' in reac.getSpecies().lower():
+                in_reactants = True
+                break
+        if not in_reactants:
+            for prod in rxn.getListOfProducts():
+                if 'biomass' in prod.getSpecies().lower():
+                    return rxn
+    return None
+
+
+def get_taxon_id(hostid):
+    '''
+    Returns the taxonomy ID of the host organism
+    
+    Parameters
+    ----------
+    hostid: str
+        Extended name of the host organism
+        
+    Returns
+    -------
+    taxid: str
+        Taxonomy ID of the host organism
+    '''
+    taxid = get_taxon_id(hostid)
+    hostname = ''
+    # Extended Name
+    server = 'http://bigg.ucsd.edu/api/v2/models/'
+    ext = hostid
+    r = r_get(server+ext, headers={ "Content-Type" : "application/json"})
+    if not r.ok:
+        print(f"Warning: unable to retrieve host name for id {hostid}")
+    else:
+        try:
+            hostname = r.json()["organism"]
+        except KeyError:
+            print(f"Warning: unable to retrieve host name for id {hostid}")
+    if not hostname:
+        taxid = ''
+    else:
+        # TAXON ID
+        server = 'https://rest.ensembl.org'
+        ext = f'/taxonomy/id/{hostname}?'
+        r = r_get(server+ext, headers={ "Content-Type" : "application/json"})
+        if not r.ok:
+            print(f"Warning: unable to retrieve taxonomy ID for host organism {hostname}")
+        else:
+            try:
+                taxid = r.json()["id"]
+            except KeyError:
+                print(f"Warning: unable to retrieve taxonomy ID for host organism {hostname}")
+                taxid = ''
+    return taxid
+
+
+def args():
     parser = ArgumentParser('Returns cell informations')
     parser.add_argument(
         'infile',
@@ -29,6 +108,11 @@
         help='Path to store biomass reaction ID'
     )
     parser.add_argument(
+        '--biomass-id',
+        type=str,
+        help='ID of biomass reaction'
+    )
+    parser.add_argument(
         '--hostid',
         type=str,
         help='Extended name of the host organism'
@@ -39,63 +123,57 @@
         help='Path to store host taxonomy ID'
     )
     params = parser.parse_args()
+    return params
+
+
+def entry_point():
+
+    params = args()
 
     sbml_doc = readSBMLFromFile(params.infile)
 
+    compartments = sbml_doc.getModel().getListOfCompartments()
+    comp_str = ''
+    for comp in compartments:
+        comp_str += f'{comp.getId()}\t{comp.getName()}\n'
     if params.comp:
-        compartments = sbml_doc.getModel().getListOfCompartments()
         with open(params.comp, 'w') as f:
             f.write('#ID\tNAME\n')
-            for comp in compartments:
-                f.write(f'{comp.getId()}\t{comp.getName()}\n')
+            f.write(comp_str)
+    else:
+        print('Compartments:')
+        for comp in compartments:
+            print(f'{comp.getId()}\t{comp.getName()}'.replace('\n', ' | '))
 
+    if params.biomass_id:
+        biomass_rxn = sbml_doc.getModel().getReaction(params.biomass_id)
+    else:
+        biomass_rxn = get_biomass_rxn(sbml_doc)
+    if not biomass_rxn:
+        print('Warning: unable to retrieve biomass reaction')
+        biomass_id = ''
+    else:
+        biomass_id = biomass_rxn.getId()
     if params.biomass:
-        reactions = sbml_doc.getModel().getListOfReactions()
         with open(params.biomass, 'w') as f:
             f.write('#ID\n')
-            for rxn in reactions:
-                if 'biomass' in rxn.getId().lower():
-                    f.write(f'{rxn.getId()}\n')
+            f.write(f'{biomass_id}\n')
+    else:
+        print(f'Biomass reaction ID: {biomass_id}')
+
+    # Model from BiGG
+    if params.bigg:
+        taxid = get_taxon_id(params.hostid)
+    # Model from user
+    else:
+        taxid = params.hostid
 
     if params.taxid:
-        hostname = ''
-
-        # Model from BiGG
-        if params.bigg:
-            # Extended Name
-            server = 'http://bigg.ucsd.edu/api/v2/models/'
-            ext = params.hostid
-            r = r_get(server+ext, headers={ "Content-Type" : "application/json"})
-            if not r.ok:
-                print(f"Warning: unable to retrieve host name for id {params.hostid}")
-            else:
-                try:
-                    hostname = r.json()["organism"]
-                except KeyError:
-                    print(f"Warning: unable to retrieve host name for id {params.hostid}")
-            if not hostname:
-                taxid = ''
-            else:
-                # TAXON ID
-                server = 'https://rest.ensembl.org'
-                ext = f'/taxonomy/id/{hostname}?'
-                r = r_get(server+ext, headers={ "Content-Type" : "application/json"})
-                if not r.ok:
-                    print(f"Warning: unable to retrieve taxonomy ID for host organism {hostname}")
-                else:
-                    try:
-                        taxid = r.json()["id"]
-                    except KeyError:
-                        print(f"Warning: unable to retrieve taxonomy ID for host organism {hostname}")
-                        taxid = ''
-
-        # Model from user
-        else:
-            taxid = params.hostid
-
         with open(params.taxid, 'w') as f:
             f.write('#ID\n')
             f.write(f'{taxid}\n')
+    else:
+        print(f'Taxonomy ID: {taxid}')
 
 
 if __name__ == "__main__":