コード例 #1
0
ファイル: sbml.py プロジェクト: willigott/framed
def load_sbml_model(filename, kind=STOICHIOMETRIC):
    """ Loads a metabolic model from a file.
    
    Arguments:
        filename : String -- SBML file path
        kind : {STOICHIOMETRIC (default), CONSTRAINT_BASED, GPR_CONSTRAINED} -- define kind of model to load (optional)
    
    Returns:
        StoichiometricModel -- Stoichiometric model or respective subclass
    """
    reader = SBMLReader()
    document = reader.readSBML(filename)
    sbml_model = document.getModel()

    if sbml_model is None:
        raise IOError('Failed to load model.')

    if kind == STOICHIOMETRIC:
        model = _load_stoichiometric_model(sbml_model)
    elif kind == CONSTRAINT_BASED:
        model = _load_constraintbased_model(sbml_model)
    elif kind == GPR_CONSTRAINED:
        model = _load_gprconstrained_model(sbml_model)
    else:
        model = None

    return model
コード例 #2
0
    def guessXML(self, xml_content):

        root = etree.fromstring(xml_content)
        tag = root.tag.split("}")[1]

        if isinstance(xml_content, bytes):
            xml_content = str("%s" % xml_content.decode('ascii', 'ignore'))

        if tag == "sbml":
            sbmlReader = SBMLReader()
            if sbmlReader is not None:
                sbmlDoc = sbmlReader.readSBMLFromString(xml_content)
                return self.SBML + ".level-%d.version-%d" % (
                    sbmlDoc.getLevel(), sbmlDoc.getVersion())
            else:
                return self.SBML

        elif tag == "sedML":
            sedmlDoc = readSedMLFromString(xml_content)
            return self.SEDML + ".level-%d.version-%d" % (
                sedmlDoc.getLevel(), sedmlDoc.getVersion())

        elif tag == "numl":
            numlDoc = readNUMLFromString(xml_content)
            return self.NUML + ".level-%d.version-%d" % (numlDoc.getLevel(),
                                                         numlDoc.getVersion())

        elif tag == "omexManifest":
            return self.MANIFEST

        else:
            return self.XML
コード例 #3
0
def test_m2m_recon_call():
    """
    Test m2m recon when called in terminal.
    """
    subprocess.call(['mpwt', '--delete', 'fatty_acid_beta_oxydation_icyc'])
    subprocess.call([
        'm2m', 'recon', '-g', 'recon_data', '-o', 'recon_data_output', '-c',
        '1', '-p'
    ])

    reader = SBMLReader()
    document = reader.readSBML(
        'recon_data_output/sbml/fatty_acid_beta_oxydation_I.sbml')
    expected_fabo_reactions = [
        convert_from_coded_id(reaction.getId())[0]
        for reaction in document.getModel().getListOfReactions()
    ]
    assert set(fabo_reactions()).issubset(set(expected_fabo_reactions))

    padmet = PadmetSpec(
        'recon_data_output/padmet/fatty_acid_beta_oxydation_I.padmet')
    fabo_rxns = [
        node.id for node in padmet.dicOfNode.values()
        if node.type == "reaction"
    ]
    assert set(fabo_reactions()).issubset(set(fabo_rxns))

    shutil.rmtree('recon_data_output')
    subprocess.call(['mpwt', '--delete', 'fatty_acid_beta_oxydation_icyc'])
コード例 #4
0
def load_sbml_model(filename,
                    kind=None,
                    flavor=None,
                    exchange_detection_mode=None,
                    load_gprs=True,
                    load_metadata=True):
    """ Loads a metabolic model from a file.
    
    Arguments:
        filename (str): SBML file path
        kind (str): define kind of model to load ('cb' or 'ode', optional)
        flavor (str): adapt to different modeling conventions (optional, see Notes)
        exchange_detection_mode (str): detect exchange reactions (optional, see Notes)
    
    Returns:
        Model: Simple model or respective subclass

    Notes:
        Currently supported flavors:
            * 'cobra': UCSD models in the old cobra toolbox format
            * 'cobra:other': other models using the old cobra toolbox format
            * 'seed': modelSEED format
            * 'bigg': BiGG database format (uses sbml-fbc2)
            * 'fbc2': other models using sbml-fbc2

        Supported exchange detection modes:
            * 'unbalanced': Exchange reactions is the one that have either only reactants or products
            * 'boundary': Exchange reaction is the one that have single boundary metabolite on one side
            * <regular expression pattern>: Regular expression which is executed against reaction ID

        Note that some flavors (cobra, bigg) have their own exchange detection mode.

    """
    if not os.path.exists(filename):
        raise IOError("Model file was not found")

    reader = SBMLReader()
    document = reader.readSBML(filename)
    sbml_model = document.getModel()

    if sbml_model is None:
        document.printErrors()
        raise IOError('Failed to load model {}.'.format(filename))

    if kind and kind.lower() == CB_MODEL:
        model = _load_cbmodel(sbml_model,
                              flavor,
                              exchange_detection_mode=exchange_detection_mode,
                              load_gprs=load_gprs,
                              load_metadata=load_metadata)
    elif kind and kind.lower() == ODE_MODEL:
        model = _load_odemodel(sbml_model)
    else:
        model = _load_stoichiometric_model(sbml_model)

    if load_metadata:
        _load_metadata(sbml_model, model)

    return model
コード例 #5
0
ファイル: sbml_wrapper.py プロジェクト: twomagpi/pygom
def readModelFromFile(filePath):
    reader = SBMLReader()
    document = reader.readSBML(filePath)

    assert document.getNumErrors() == 0, "Error detected in sbml file"

    model = document.getModel()
    return (model)
コード例 #6
0
def readModelFromFile(filePath):
    reader = SBMLReader()
    document = reader.readSBML(filePath)

    assert document.getNumErrors() == 0, "Error detected in sbml file"

    model = document.getModel()
    return model
コード例 #7
0
ファイル: tests.py プロジェクト: mahjalili/GEMtractor
 def _valid_sbml (self, xml):
   if not self._valid_xml (xml):
     return False, None
   
   sbml = SBMLReader().readSBMLFromString(xml.decode("utf-8"))
   if sbml.getNumErrors() > 0:
     for i in range (0, sbml.getNumErrors()):
       logging.getLogger(__name__).info("SBML BAD: " + sbml.getError(i).getMessage())
     return False, None
   return True, sbml
コード例 #8
0
 def parse(self, handle):
     if hasattr(self, "notify_progress"):
         self.notify_progress(current = 0, total = 1, message = "Parsing SBML...")
         
     self.reader = SBMLReader()
     self.document = self.reader.readSBMLFromString("".join(line for line in handle))
     self.model = self.document.getModel()
     self.compartments = list(map(lambda i: self.model.getCompartment(i), range(len(self.model.getListOfCompartments()))))
     self.sbml_species = list(map(lambda i: self.model.getSpecies(i), range(len(self.model.getListOfSpecies()))))
     self.reactions = list(map(lambda i: self.model.getReaction(i), range(len(self.model.getListOfReactions()))))
コード例 #9
0
def test_m2m_metacom_call():
    """
    Test m2m metacom when called in terminal.
    """
    # RUN THE COMMAND
    inppath = 'metabolic_data/'
    respath = 'metacom_output/'
    if not os.path.exists(respath):
        os.makedirs(respath)
    with tarfile.open(inppath + 'toy_bact.tar.gz') as tar:
        tar.extractall(path=respath)
    subprocess.call([
        'm2m', 'metacom', '-n', respath + '/toy_bact', '-o',
        respath, '-s', inppath + '/seeds_toy.sbml', '-q'
    ])
    target_file = respath + 'community_analysis/targets.sbml'
    iscope_file = respath + 'indiv_scopes/indiv_scopes.json'
    cscope_file = respath + 'community_analysis/comm_scopes.json'
    resfile = respath + 'community_analysis/mincom.json'
    # ISCOPE ANALYSIS
    # ensure there is the right number of computed indiv scopes
    with open(iscope_file, 'r') as json_idata:
        d_iscope = json.load(json_idata)
    assert len(d_iscope) == NUMBER_BACT
    # ensure the union and intersection are ok
    d_iscope_set = {}
    for elem in d_iscope:
        d_iscope_set[elem] = set(d_iscope[elem])
    union_scope = set.union(*list(d_iscope_set.values()))
    assert len(union_scope) == SIZE_UNION
    intersection_scope = set.intersection(*list(d_iscope_set.values()))
    assert len(intersection_scope) == SIZE_INTERSECTION
    # CSCOPE ANALYSIS
    with open(cscope_file, 'r') as json_cdata:
        d_cscope = json.load(json_cdata)
    assert len(d_cscope['com_scope']) == SIZE_CSCOPE
    # ADDEDVALUE ANALYSIS
    reader = SBMLReader()
    document = reader.readSBML(target_file)
    new_targets = set([specie.getId() for specie in document.getModel().getListOfSpecies()])
    assert new_targets == EXPECTED_TARGETS
    # MINCOM ANALYSIS
    with open(resfile, 'r') as json_data:
        d_mincom = json.load(json_data)
    # ensure the minimal number of bacteria in a minimal community is ok
    assert len(d_mincom['bacteria']) == MIN_SIZE_COM
    # ensure the bacteria in union are ok
    assert set(d_mincom['union_bacteria']) == UNION
    # ensure the bacteria in intersection are ok
    assert set(d_mincom['inter_bacteria']) == INTERSECTION
    # ensure the newly producible targets are ok
    assert set(d_mincom['newly_prod']) == NEWLYPROD_TARGETS
    # clean
    shutil.rmtree(respath)
コード例 #10
0
def get_species_id_list_libsbml(model_file = '/Users/wbryant/work/BTH/data/iAH991/BTH_with_gprs.xml'):
    """ Return a list of species IDs from the input model."""
    
    reader = SBMLReader()
    document = reader.readSBMLFromFile(model_file)
    model = document.getModel()
    
    species_id_list = []
    for metabolite in model.getListOfSpecies():
        species_id_list.append((metabolite.getId(), metabolite.getName()))
    
    return species_id_list
コード例 #11
0
def get_sbml_level(sbml_file):
    """Get SBML Level of a file

    Args:
        sbml_file (str): SBML file

    Returns:
        int: SBML Level
    """
    reader = SBMLReader()
    document = reader.readSBML(sbml_file)
    return document.getLevel()
コード例 #12
0
def load_sbml_model(filename, kind=None, flavor=None, exchange_detection_mode=None,
                    load_gprs=True, load_metadata=True):
    """ Loads a metabolic model from a file.
    
    Arguments:
        filename (str): SBML file path
        kind (str): define kind of model to load ('cb' or 'ode', optional)
        flavor (str): adapt to different modeling conventions (optional, see Notes)
        exchange_detection_mode (str): detect exchange reactions (optional, see Notes)
    
    Returns:
        Model: Simple model or respective subclass

    Notes:
        Currently supported flavors:
            * 'cobra': UCSD models in the old cobra toolbox format
            * 'cobra:other': other models using the old cobra toolbox format
            * 'seed': modelSEED format
            * 'bigg': BiGG database format (uses sbml-fbc2)
            * 'fbc2': other models using sbml-fbc2

        Supported exchange detection modes:
            * 'unbalanced': Exchange reactions is the one that have either only reactants or products
            * 'boundary': Exchange reaction is the one that have single boundary metabolite on one side
            * <regular expression pattern>: Regular expression which is executed against reaction ID

        Note that some flavors (cobra, bigg) have their own exchange detection mode.

    """
    if not os.path.exists(filename):
        raise IOError("Model file was not found")

    reader = SBMLReader()
    document = reader.readSBML(str(filename))
    sbml_model = document.getModel()

    if sbml_model is None:
        document.printErrors()
        raise IOError('Failed to load model {}.'.format(filename))

    if kind and kind.lower() == CB_MODEL:
        model = _load_cbmodel(sbml_model, flavor, exchange_detection_mode=exchange_detection_mode,
                              load_gprs=load_gprs, load_metadata=load_metadata)
    elif kind and kind.lower() == ODE_MODEL:
        model = _load_odemodel(sbml_model)
    else:
        model = _load_stoichiometric_model(sbml_model)

    if load_metadata:
        _load_metadata(sbml_model, model)

    return model
コード例 #13
0
def get_compounds(sbml_file):
    """Get target from sbml

    Args:
        sbml_file (str): SBML file

    Returns:
        list: target
    """
    reader = SBMLReader()
    document = reader.readSBML(sbml_file)
    model = document.getModel()
    targets = [target.id for target in model.getListOfSpecies()]
    return targets
コード例 #14
0
def test_m2m_addedvalue_call():
    """
    Test m2m addedvalue when called in terminal.
    """
    # RUN THE COMMAND
    inppath = 'metabolic_data'
    respath = 'addedvalue_output'
    toy_tgz_bact = os.path.join(inppath, 'toy_bact.tar.gz')
    toy_bact = os.path.join(respath, 'toy_bact')
    seeds_path = os.path.join(inppath, 'seeds_toy.sbml')

    if not os.path.exists(respath):
        os.makedirs(respath)
    with tarfile.open(toy_tgz_bact) as tar:
        tar.extractall(path=respath)
    subprocess.call([
        'm2m', 'addedvalue', '-n', toy_bact, '-o', respath, '-s', seeds_path,
        '-q'
    ])
    target_file = os.path.join(
        *[respath, 'community_analysis', 'targets.sbml'])
    iscope_file = os.path.join(*[respath, 'indiv_scopes', 'indiv_scopes.json'])
    cscope_file = os.path.join(
        *[respath, 'community_analysis', 'comm_scopes.json'])
    # ISCOPE ANALYSIS
    # ensure there is the right number of computed indiv scopes
    with open(iscope_file, 'r') as json_idata:
        d_iscope = json.load(json_idata)
    assert len(d_iscope) == NUMBER_BACT
    # ensure the union and intersection are ok
    d_iscope_set = {}
    for elem in d_iscope:
        d_iscope_set[elem] = set(d_iscope[elem])
    union_scope = set.union(*list(d_iscope_set.values()))
    assert len(union_scope) == SIZE_UNION
    intersection_scope = set.intersection(*list(d_iscope_set.values()))
    assert len(intersection_scope) == SIZE_INTERSECTION
    # CSCOPE ANALYSIS
    with open(cscope_file, 'r') as json_cdata:
        d_cscope = json.load(json_cdata)
    assert len(d_cscope['com_scope']) == SIZE_CSCOPE
    # ADDEDVALUE ANALYSIS
    reader = SBMLReader()
    document = reader.readSBML(target_file)
    new_targets = set(
        [specie.getId() for specie in document.getModel().getListOfSpecies()])
    assert new_targets == EXPECTED_TARGETS
    # clean
    shutil.rmtree(respath)
コード例 #15
0
 def __init__(self, sbml_file):
   self.__GENE_ASSOCIATION_PATTERN = re.compile(r".*GENE_ASSOCIATION:([^<]+) *<.*", re.DOTALL)
   self.__GENE_LIST_PATTERN = re.compile(r".*GENE_LIST: *([^ <][^<]*)<.*", re.DOTALL)
   self.__EXPRESSION_PARSER = self.__get_expression_parser ()
   self.__logger = logging.getLogger(__name__)
   self.__reaction_gene_map = {}
   self.__sbml_file = sbml_file
   self.__fbc_plugin = None
   self.__logger.debug("reading sbml file " + self.__sbml_file)
   self.sbml = SBMLReader().readSBML(self.__sbml_file)
   if self.sbml.getNumErrors() > 0:
     e = []
     for i in range (0, self.sbml.getNumErrors()):
       e.append (self.sbml.getError(i).getMessage())
     raise IOError ("model seems to be invalid: " + str (e))
コード例 #16
0
def get_species_id_dict(model_file = '/Users/wbryant/work/BTH/data/iAH991/BTH_with_gprs.xml'):
    """ Return a dictionary of species IDs from IDs and names from the input model."""
    
    reader = SBMLReader()
    document = reader.readSBMLFromFile(model_file)
    model = document.getModel()
    
    species_id_dict = {}
    ambiguous_synonyms = []
    covered_metabolites = []
    for metabolite in model.getListOfSpecies():
        
        metabolite_id = metabolite.getId()
        
        #print metabolite_id
        
        
        if metabolite_id[0:2] == "M_":
            met_id = metabolite_id[2:].lower().strip()
            met_id_orig = metabolite_id[2:]
        else:
            met_id = metabolite_id.lower().strip()
            met_id_orig = metabolite_id
        met_name = metabolite.getName().lower().strip()
        
        if met_id in species_id_dict:
            print("Ambiguous ID: {} - {} ({})".format(met_id, met_name, species_id_dict[met_id]))
            ambiguous_synonyms.append(met_id)
        else:
            species_id_dict[met_id] = met_id_orig
            
        if met_name in species_id_dict:
            if met_name != met_id:
                print("Ambiguous name: {} - {} ({})".format(met_name, met_id, species_id_dict[met_id]))
                ambiguous_synonyms.append(met_name)
        else:
            species_id_dict[met_name + metabolite_id[-2:]] = met_id_orig
        
    for ambiguous_synonym in ambiguous_synonyms:
        species_id_dict.pop(ambiguous_synonym, None)
        
        
    return species_id_dict
コード例 #17
0
def get_compounds(sbml_file):
    """Get compound from sbml

    Args:
        sbml_file (str): SBML file

    Returns:
        list: compound
    """
    reader = SBMLReader()
    document = reader.readSBML(sbml_file)
    model = document.getModel()
    if model is None:
        logger.critical(
            'SBML file "' + sbml_file +
            '" not well formatted. Is this file a SBML? Does it contains <model></model> tags?'
        )
        sys.exit(1)
    compounds = [compound.id for compound in model.getListOfSpecies()]
    return compounds
コード例 #18
0
ファイル: sbml.py プロジェクト: cdanielmachado/arfba
def load_allosteric_model(filename):

    reader = SBMLReader()
    document = reader.readSBML(filename)
    sbml_model = document.getModel()

    if sbml_model is None:
        raise IOError('Failed to load model.')

    model = AllostericModel(sbml_model.getId())
    model.add_compartments(_load_compartments(sbml_model))
    model.add_metabolites(_load_metabolites(sbml_model))
    model.add_reactions(_load_reactions(sbml_model))
    model.add_stoichiometry(_load_stoichiometry(sbml_model))
    model.add_regulators(_load_regulators(sbml_model))
    bounds, coefficients = _load_cb_parameters(sbml_model)
    model.set_bounds(bounds)
    model.set_objective_coefficients(coefficients)
    genes, rules = _load_gpr(sbml_model)
    model.add_genes(genes)
    model.set_rules(rules)

    return model
コード例 #19
0
def load_allosteric_model(filename):

    reader = SBMLReader()
    document = reader.readSBML(filename)
    sbml_model = document.getModel()

    if sbml_model is None:
        raise IOError('Failed to load model.')

    model = AllostericModel(sbml_model.getId())
    model.add_compartments(_load_compartments(sbml_model))
    model.add_metabolites(_load_metabolites(sbml_model))
    model.add_reactions(_load_reactions(sbml_model))
    model.add_stoichiometry(_load_stoichiometry(sbml_model))
    model.add_regulators(_load_regulators(sbml_model))
    bounds, coefficients = _load_cb_parameters(sbml_model)
    model.set_bounds(bounds)
    model.set_objective_coefficients(coefficients)
    genes, rules = _load_gpr(sbml_model)
    model.add_genes(genes)
    model.set_rules(rules)

    return model
コード例 #20
0
def extend_model_with_DB_SBML(model, reactionsDB):
    """ Extends the given model with the reactions in reactionsDB.
        This is done by loading the model in sbml format reactionsDB
        and adding only the reactions that are not part of "model" to it.

        Arguments:
            model : the constraint based model to be extended
            reactionsDB (str): SBML file path for the reactionsDB model

        Returns:
            model: the given model extended with the database reactions/metabolites
            db_reactions: the reactions that were added from the database to
                          the given model
    """
    reader = SBMLReader()
    document = reader.readSBML(reactionsDB)
    sbml_model = document.getModel()

    if sbml_model is None:
        raise IOError('Failed to load model.')

    (model, db_reactions) = _load_constraintbased_model(sbml_model, model)

    return (model, db_reactions)
コード例 #21
0
def extend_model_with_DB_SBML(model, reactionsDB):
    """ Extends the given model with the reactions in reactionsDB.
        This is done by loading the model in sbml format reactionsDB
        and adding only the reactions that are not part of "model" to it.

        Arguments:
            model : the constraint based model to be extended
            reactionsDB (str): SBML file path for the reactionsDB model

        Returns:
            model: the given model extended with the database reactions/metabolites
            db_reactions: the reactions that were added from the database to
                          the given model
    """
    reader = SBMLReader()
    document = reader.readSBML(reactionsDB)
    sbml_model = document.getModel()

    if sbml_model is None:
        raise IOError('Failed to load model.')

    (model, db_reactions) = _load_constraintbased_model(sbml_model, model)

    return (model, db_reactions)
コード例 #22
0
def test_m2m_recon_call():
    """
    Test m2m recon when called in terminal.
    """
    sbml_file_path = os.path.join(
        *['recon_data_output', 'sbml', 'fatty_acid_beta_oxydation_I.sbml'])
    padmet_path = os.path.join(
        *['recon_data_output', 'padmet', 'fatty_acid_beta_oxydation_I.padmet'])

    subprocess.call(['mpwt', '--delete', 'fatty_acid_beta_oxydation_icyc'])
    subprocess.call([
        'm2m', 'recon', '-g', 'recon_data', '-o', 'recon_data_output', '-c',
        '1', '-p'
    ])

    reader = SBMLReader()
    document = reader.readSBML(sbml_file_path)
    expected_fabo_reactions = [
        convert_from_coded_id(reaction.getId())[0]
        for reaction in document.getModel().getListOfReactions()
    ]
    assert set(get_fabo_reactions()).issubset(set(expected_fabo_reactions))

    padmet = PadmetSpec(padmet_path)
    fabo_rxns = [
        node.id for node in padmet.dicOfNode.values()
        if node.type == "reaction"
    ]
    assert set(get_fabo_reactions()).issubset(set(fabo_rxns))

    shutil.rmtree('recon_data_output')

    subprocess.call([
        'm2m', 'recon', '-g', 'recon_data', '-o', 'recon_data_output', '-c',
        '1', '--pwt-xml'
    ])
    reader = SBMLReader()
    document = reader.readSBML(sbml_file_path)
    # Extract reaction ID from annotaiton.
    fabo_reactions = [
        reaction.name for reaction in document.getModel().getListOfReactions()
    ]
    known_fabo_reactions = get_fabo_reactions()
    results = {}
    for known_fabo_reaction in known_fabo_reactions:
        presence_reaction = sum([
            1 if known_fabo_reaction in fabo_reaction else 0
            for fabo_reaction in fabo_reactions
        ])
        if presence_reaction > 0:
            results[known_fabo_reaction] = True

    assert all(results.values())

    shutil.rmtree('recon_data_output')
    subprocess.call(['mpwt', '--delete', 'fatty_acid_beta_oxydation_icyc'])
コード例 #23
0
def get_species_id_list(model_file = '/Users/wbryant/work/BTH/data/iAH991/BTH_with_gprs.xml'):
    """ Return a list of species IDs from an SBML model.  Use COBRA.
    
    Also, return whether each metabolite is an orphan
    """
    
    cobra_model = create_cobra_model_from_sbml_file(model_file)
    
    reader = SBMLReader()
    document = reader.readSBMLFromFile(model_file)
    model = document.getModel()
    
    species_id_list = []
    print("Model orphans:\n")
    for metabolite in cobra_model.metabolites:
        ## Is metabolite an orphan (and internal)?
         
        reactions_list = metabolite.get_reaction()
         
        
        if len(reactions_list) == 1:
            orphan = True
            print metabolite.id
        else:
            orphan = False
         
        met_id = re.sub("__","-",metabolite.id)
        met_name = re.sub("__","-",metabolite.name)
             
        species_id_list.append((met_id, met_name, orphan))
         
#     species_id_list = []
#     for metabolite in model.getListOfSpecies():
#         species_id_list.append((metabolite.getId(), metabolite.getName(), False))
         
    return species_id_list
コード例 #24
0
import os
from os import listdir
import sys
from sys import exit

dirs = [
    '00001', '00002', '00050', '00100', '00150', '00200', '00250', '00300',
    '00350', '00400', '00450', '00500'
]

for d in dirs:
    modelfile = filter(lambda x: '.xml' in x, listdir(d))[0]
    print(modelfile)

    reader = SBMLReader()

    document = reader.readSBML(os.path.join(d, modelfile))

    errors = document.getNumErrors()

    if (errors > 0):
        print("Encountered the following SBML errors:" + "\n")
        document.printErrors()
        print("Conversion skipped.  Please correct the problems above first." +
              "\n")
        break

    #
    # If the given model is not already L2v4, assume that the user wants to
    # convert it to the latest release of SBML (which is L2v4 currently).
コード例 #25
0
class SBML(BioParser):
    def parse(self, handle):
        if hasattr(self, "notify_progress"):
            self.notify_progress(current = 0, total = 1, message = "Parsing SBML...")
            
        self.reader = SBMLReader()
        self.document = self.reader.readSBMLFromString("".join(line for line in handle))
        self.model = self.document.getModel()
        self.compartments = list(map(lambda i: self.model.getCompartment(i), range(len(self.model.getListOfCompartments()))))
        self.sbml_species = list(map(lambda i: self.model.getSpecies(i), range(len(self.model.getListOfSpecies()))))
        self.reactions = list(map(lambda i: self.model.getReaction(i), range(len(self.model.getListOfReactions()))))

    @atomic
    def apply(self):
        self.detail.save()
        
        total = len(self.compartments) + len(self.sbml_species) + len(self.reactions)

        # Compartment importer
        for i, compartment in enumerate(self.compartments):
            wid = slugify(compartment.getId())
            
            if compartment.getName():
                name = compartment.getName()
            else:
                name = wid

            if hasattr(self, "notify_progress"):
                out_str = "Importing Compartment %s (%d/%d)" % (wid, i + 1, total)
                self.notify_progress(current = i+1, total = total, message = out_str)
            
            # TODO: compartment.getOutside() not implemented
            cobj = cmodels.Compartment.objects.for_species(self.species).for_wid(wid, create = True)
            
            cobj.name = name
            cobj.species = self.species
            cobj.save(self.detail)

        # Species (= Metabolites) importer
        for i, specie in enumerate(self.sbml_species):
            if not self.model.getCompartment(specie.getCompartment()):
                ##self.stderr.write("WARN: Species {} has invalid compartment {}".format(specie.id, specie.getCompartment()))
                continue
                
            wid = slugify(specie.getId())
            if specie.getName():
                name = specie.getName()
            else:
                name = wid
            
            if hasattr(self, "notify_progress"):
                current = len(self.compartments) + i + 1
                out_str = "Importing Metabolite %s (%d/%d)" % (wid, current, total)
                self.notify_progress(current = current, total = total, message = out_str)
            
            # TODO: specie.getBoundaryCondition() not implemented
            sobj = cmodels.Metabolite.objects.for_species(self.species).for_wid(wid, create = True)
            
            sobj.name = name
            sobj.charge = 0 # TODO
            sobj.is_hydrophobic = False # TODO
            sobj.species = self.species
            sobj.save(self.detail)

        for i, reaction in enumerate(self.reactions):
            wid = slugify(reaction.getId())
            if reaction.getName():
                name = reaction.getName()
            else:
                name = wid
                
            valid = False
            
            if hasattr(self, "notify_progress"):
                current = len(self.compartments) + len(self.sbml_species) + i + 1
                out_str = "Importing Reaction %s (%d/%d)" % (wid, current, total)
                self.notify_progress(current = current, total = total, message = out_str)
            
            # Validation of reactants
            reactants = map(lambda i: reaction.getReactant(i), range(len(reaction.getListOfReactants())))
            products = map(lambda i: reaction.getProduct(i), range(len(reaction.getListOfProducts())))
            
            for reactant in reactants:
                if not self.model.getSpecies(reactant.getSpecies()):
                    ##self.stderr.write("WARN: Reactant {} has invalid species {}".format(reactant.id, reactant.species))
                    break
            else:
                # Validation of products
                for product in products:
                    if not self.model.getSpecies(product.getSpecies()):
                        ##self.stderr.write("WARN: Product {} has invalid species {}".format(product.id, product.species))
                        break
                else:
                    # Validation passed
                    valid = True
            
            if valid:
                reaction_obj = cmodels.Reaction.objects.for_species(self.species).for_wid(wid, create = True)
                
                reaction_obj.name = name
                reaction_obj.direction = 'r' if reaction.getReversible() else 'f'
                reaction_obj.is_spontaneous = False  # TODO
                reaction_obj.species = self.species
                reaction_obj.save(self.detail)

                for reactant in reactants:
                    #try:
                    #    participant_obj = cmodels.ReactionStoichiometryParticipant.objects.get(wid = wid)
                    #except ObjectDoesNotExist:
                    #    participant_obj = cmodels.ReactionStoichiometryParticipant(wid = wid)

                    participant_obj = cmodels.ReactionStoichiometryParticipant()
                    participant_obj.molecule = cmodels.Metabolite.objects.for_species(self.species).for_wid(slugify(reactant.getSpecies()))
                    participant_obj.coefficient = -reactant.getStoichiometry()
                    participant_obj.compartment = cmodels.Compartment.objects.for_species(self.species).for_wid(slugify(self.model.getSpecies(reactant.getSpecies()).getCompartment()))
                    participant_obj.save(self.detail)

                    reaction_obj.stoichiometry.add(participant_obj)

                for product in products:
                    #try:
                    #    participant_obj = cmodels.ReactionStoichiometryParticipant.objects.get(wid = wid)
                    #except ObjectDoesNotExist:
                    #    participant_obj = cmodels.ReactionStoichiometryParticipant(wid = wid)

                    participant_obj = cmodels.ReactionStoichiometryParticipant()
                    participant_obj.molecule = cmodels.Metabolite.objects.for_species(self.species).for_wid(slugify(product.getSpecies()))
                    participant_obj.coefficient = product.getStoichiometry()
                    participant_obj.compartment = cmodels.Compartment.objects.for_species(self.species).for_wid(slugify(self.model.getSpecies(product.getSpecies()).getCompartment()))
                    participant_obj.detail = self.detail
                    participant_obj.save(self.detail)

                    reaction_obj.stoichiometry.add(participant_obj)
コード例 #26
0
                        "--output",
                        help="output filename for families statistics",
                        required=True)

    args = parser.parse_args()

    mn_dir = args.dir
    json_family = args.json
    outfile = args.output

    species_by_mn = {}

    for mn in os.listdir(mn_dir):
        print(mn)
        mn_name = mn.rstrip(".sbml")
        reader = SBMLReader()
        model = reader.readSBML(mn_dir + '/' + mn).getModel()
        species = [
            convert_from_coded_id(i.getId())[0]
            for i in model.getListOfSpecies()
        ]
        species_by_mn[mn_name] = species

    with open(json_family, "r") as f:
        family_dict = json.load(f)

    # count_dict = {i:0 for i in list(set(sum(family_dict.values(), [])))}
    families_by_mn = {}

    for mn in species_by_mn:
        families_by_mn[mn] = {
コード例 #27
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from pathlib import Path
import pkg_resources

from libsbml import SBMLReader 


model_file_name = "example-network.xml"

model_file = Path(pkg_resources.resource_filename("libsbml_draw", "model/libs/" + model_file_name))

print("model file: ", model_file)
    
reader = SBMLReader()    

doc = reader.readSBMLFromFile(str(model_file))    

model = doc.getModel()

speciesList = model.getListOfSpecies()

print("num species: ", model.getNumSpecies())
print("num species: ", len(speciesList))


def getBoundarySpeciesIds(doc):

        model = doc.getModel()
        speciesList = model.getListOfSpecies()    
コード例 #28
0
    def readFromFile(self, path, omex=False):
        '''
        Reads EnzymeML document to an object layer EnzymeMLDocument class.
        
        Args:
            String path: Path to .omex container or folder destination for plain .xml
            Boolean omex: Determines whether reader handles an .omex file or not
        '''

        self.omex = omex
        self.__path = path

        if self.omex:
            self.archive = CombineArchive()
            self.archive.initializeFromArchive(self.__path)

            sbmlfile = self.archive.getEntry(0)
            content = self.archive.extractEntryToString(sbmlfile.getLocation())

        reader = SBMLReader()

        if self.omex:
            document = reader.readSBMLFromString(content)

        else:
            document = reader.readSBMLFromFile(self.__path + '/experiment.xml')

        document.getErrorLog().printErrors()

        model = document.getModel()

        enzmldoc = EnzymeMLDocument(model.getName(), model.getLevel(),
                                    model.getVersion())

        # Fetch references
        self.__getRefs(model, enzmldoc)

        # Fetch meta data
        try:
            creators = self.__getCreators(model)
            enzmldoc.setCreator(creators)
        except AttributeError:
            enzmldoc.setCreator(Creator("UNKNOWN", "UNKNOWN", "UNKNOWN"))

        try:
            model_hist = model.getModelHistory()
            enzmldoc.setCreated(model_hist.getCreatedDate().getDateAsString())
            enzmldoc.setModified(
                model_hist.getModifiedDate().getDateAsString())
        except AttributeError:
            enzmldoc.setCreated("2020")
            enzmldoc.setModified("2020")

        # Fetch units
        unitDict = self.__getUnits(model)
        enzmldoc.setUnitDict(unitDict)

        # Fetch Vessel
        vessel = self.__getVessel(model)
        enzmldoc.setVessel(vessel, use_parser=False)

        # Fetch Species
        proteinDict, reactantDict = self.__getSpecies(model)
        enzmldoc.setReactantDict(reactantDict)
        enzmldoc.setProteinDict(proteinDict)

        # fetch reaction
        reactionDict = self.__getReactions(model, enzmldoc)
        enzmldoc.setReactionDict(reactionDict)

        del self.__path

        return enzmldoc
コード例 #29
0
ファイル: export_model.py プロジェクト: wbryant/incognito
    def handle(self, *args, **options):
        
        """ Export a model to SBML for a given source ID for exchange.
            
        """ 
        
        
        ## Cycle through command line arguments
        num_args = 0
        for arg in args:
            num_args += 1
        
        model_source_id = args[0]
        try:
            source = Source.objects.get(name=model_source_id)
        except:
            print("There is no such source as '%s', please check ID ..." % model_source_id)
            sys.exit(1) 
        
        
        
        ## Create model container
#         document = SBMLDocument()
#         document.setLevelAndVersion(2,4)
        reader = SBMLReader()
        document = reader.readSBMLFromFile('/Users/wbryant/work/BTH/level2v4_template.xml')
        model = document.getModel()
        model.setName(model_source_id)
        model.setId(model_source_id)
        
        
        ## Get reactions from Metabolic_model
        model_reactions = Metabolic_model.objects.filter()
        
        
        ## Create compartments
        compartments = Compartment.objects.filter(stoichiometry__source=source).distinct()
        
        for compartment in compartments:
            c_model = model.createCompartment()
            c_model.setName(str(compartment.name))
            c_model.setId(str(compartment.id))
            
        
        ## Create Species
        stoichiometries = Stoichiometry.objects.filter(source=source)\
            .values_list(
                "metabolite__id",
                "metabolite__name",
                "compartment__id",
            )\
            .distinct()
        print("There are %d metabolites (with compartments) ..." % len(stoichiometries))
        
        print("Populating model with metabolites ...")
        counter = loop_counter(len(stoichiometries))
        
        
        for met in stoichiometries:
            counter.step()
            species = model.createSpecies()
            species.setName(met[1].encode("utf-8"))
            species.setId(str(met[0] + "__in__" + met[2]))
            species.setCompartment(str(met[2]))
            
        counter.stop()
        
        
        ## Create Reactions (get list according to 'Metabolic_model')
        model_reactions = Metabolic_model.objects.filter(source=source).prefetch_related()
        num_reactions = len(model_reactions)
        
        print("There are %d reactions ..." % num_reactions)
        
        print("Populating model with reactions ...")
        counter = loop_counter(num_reactions)
        
        for mod in model_reactions:
            counter.step()
            reaction = model.createReaction()
            reaction.setName(mod.reaction.name.encode("utf-8"))
            reaction.setId(mod.reaction.name.encode("utf-8"))
            
            if mod.direction.direction == "both":
                reversible = True
            else:
                reversible = False
            reaction.setReversible(reversible)
        
            for sto in mod.reaction.stoichiometry_set.all():
                if sto.stoichiometry > 0:
                    met = reaction.createReactant()
                else:
                    met = reaction.createProduct()
                
                species_id = sto.metabolite.id + "__in__"\
                                + sto.compartment.id
                
                met.setSpecies(str(species_id))
                met.setStoichiometry(abs(sto.stoichiometry))
        
        counter.stop()
            
        ## Set and save the model
        
        print("Writing model to SBML ...")
        document.setModel(model)
#         document.setLevelAndVersion(2,4)
        writeSBMLToFile(document, "/Users/wbryant/work/cogzymes/incognito/test_libsbml.xml")
        
            
        
            
        
        
        
        
コード例 #30
0
ファイル: tests_utils.py プロジェクト: sbi-rostock/GEMtractor
 def __get_sbml_model (self):
   sbml = SBMLReader().readSBML("test/gene-filter-example.xml")
   self.assertTrue (sbml.getNumErrors() == 0)
   return sbml.getModel()
コード例 #31
0
ファイル: model_data__.py プロジェクト: mmoskon/Grohar
 def load_model_sbml(self, path):
     reader = SBMLReader()
     self.document = reader.readSBML(path)
     self.model = self.document.getModel()
     self.fill_data_sbml()
     self.type = "sbml"
コード例 #32
0
 '00050',
 '00100',
 '00150',
 '00200',
 '00250',
 '00300',
 '00350',
 '00400',
 '00450',
 '00500']
 
for d in dirs:
  modelfile = filter(lambda x: '.xml' in x, listdir(d))[0]
  print(modelfile)
  
  reader = SBMLReader()
  
  document = reader.readSBML(os.path.join(d,modelfile))
  
  errors = document.getNumErrors();
  
  if (errors > 0):
    print("Encountered the following SBML errors:" + "\n");
    document.printErrors();
    print("Conversion skipped.  Please correct the problems above first."
    + "\n");
    break
  
  # 
  # If the given model is not already L2v4, assume that the user wants to
  # convert it to the latest release of SBML (which is L2v4 currently).
コード例 #33
0
  ('00050',  "./00050/00050-sbml-l2v4.xml"),
  ('00100', "./00100/00100-sbml-l2v4.xml"),
  ('00150', "./00150/00150-sbml-l2v4.xml"),
  ('00200', "./00200/00200-sbml-l2v4.xml"),
  ('00250', "./00250/00250-sbml-l2v4.xml"),
  ('00300', "./00300/00300-sbml-l2v4.xml"),
  ('00350', "./00350/00350-sbml-l2v4.xml"),
  ('00400', "./00400/00400-sbml-l2v4.xml"),
  ('00450', "./00450/00450-sbml-l2v4.xml"),
  ('00500', "./00500/00500-sbml-l2v4.xml")]

for t in tests:
  name = t[0]
  path = t[1]
  
  reader = SBMLReader()

  document = reader.readSBML(path)

  errors = document.getNumErrors();

  if (errors > 0):
    print("Encountered the following SBML errors:" + "\n", file=stderr);
    document.printErrors();
    print("Conversion skipped.  Please correct the problems above first."
      + "\n", file=stderr);
    continue
  
  model = document.getModel()
  
  print('{}: {} species and {} reactions'.format(name, model.getNumSpecies(), model.getNumReactions()))
コード例 #34
0
def parse(fp):
    from libsbml import SBMLReader
    document = SBMLReader().readSBML(fp)
    print(document.getNumErrors())
コード例 #35
0
ファイル: ug_scripts_dev.py プロジェクト: wbryant/incognito
def import_SBML(SBML_filename, rxn_notes_fields = [], met_notes_fields = []):
    """Import file C{SBML_filename} and convert it into a bipartite metabolic network."""
    
    print '\n\nImporting SBML model ...'
    
    # Import SBML model
    reader = SBMLReader()
    document = reader.readSBMLFromFile(SBML_filename)
    model = document.getModel()
    print 'Model being imported: %s ...' % (model.getId())
    
    # Initialize NetworkX model and populate with metabolite and reaction nodes.
    # At the same time, create an id / node_idx dictionary for use when creating
    # edges.
    G = nx.DiGraph()
    G.model_id = model.getId()
    node_idx = 0
    node_id_dictionary = {}
   
    for metabolite in model.getListOfSpecies():
        node_idx += 1
        G.add_node(node_idx)
        G.node[node_idx]['name'] = metabolite.getName().strip()
        G.node[node_idx]['id'] = metabolite.getId()
        G.node[node_idx]['type'] = 'metabolite'
        node_id_dictionary[metabolite.getId()] = node_idx

    for reaction in model.getListOfReactions():
        node_idx += 1
        G.add_node(node_idx)
        G.node[node_idx]['name'] = reaction.getName().strip()
        G.node[node_idx]['id'] = reaction.getId()
        G.node[node_idx]['type'] = 'reaction'
        node_id_dictionary[reaction.getId()] = node_idx
        
        #print node_idx
        #print G.node[node_idx]['name']
        
        notes = reaction.getNotesString()
        
        genelist = []
        genes = re.search('GENE[_ ]ASSOCIATION\:([^<]+)<',notes)
        if genes is not None:
            for gene in re.finditer('([^\s\&\|\(\)]+)', genes.group(1)):
                if not gene.group(1) == 'and' and not gene.group(1) == 'or' and not gene.group(1) == 'none':
                    genelist.append(gene.group(1))
            G.node[node_idx]['gpr'] = genes.group(1)
        else:
            G.node[node_idx]['gpr'] = 'None'
        G.node[node_idx]['genelist'] = list(set(genelist))
        
        # Cycle through all reactants and products and add edges
        #print 'REACTANTS:'
        for reactant in reaction.getListOfReactants():
            #print reactant.getSpecies()
            reactant_idx = node_id_dictionary[reactant.getSpecies()]
            reactant_stoichiometry = -1*reactant.getStoichiometry()
            #print reactant_idx
            G.add_edge(reactant_idx,node_idx,coeff=reactant_stoichiometry)
            #print G.edges(reactant_idx)
        #print 'PRODUCTS:'
        for product in reaction.getListOfProducts():
            #print product.getSpecies()
            product_idx = node_id_dictionary[product.getSpecies()]
            product_stoichiometry = product.getStoichiometry()
            
            G.add_edge(node_idx,product_idx,coeff=product_stoichiometry)
            
            #print G.edges(node_idx)
        #print '\n'
    # Add degree of each metabolite as 'weight' attribute
    for node in G.nodes():
        if G.node[node]['type'] == 'metabolite':
            G.node[node]['weight'] = float(G.degree(node))
            G.node[node]['score'] = -1*float(G.degree(node))
    print 'Finished model import.'
    
    return G
コード例 #36
0
def parse(fp):
    from libsbml import SBMLReader
    document = SBMLReader().readSBML(fp)
    print(document.getNumErrors())
コード例 #37
0
 def handle(self, *args, **options):
     
     """ COMMAND DESCRIPTION
         
     """ 
         
     
     SBML_filename = '/Users/wbryant/work/BTH/data/iAH991/BTH_with_gprs.xml'
     
     
     ## Clear any previous import from this file and create Source record 
     source_name = SBML_filename.split("/")[-1]
     if Source.objects.filter(name=source_name).count() > 0:
         Source.objects.filter(name=source_name).delete()
         
     source = Source(name=source_name)
     source.save()
     
     """Import Joern's model: assume model has correct stoichiometry, so override DB.
     
     For each non-MetaNetX reaction, try to find MNX metabolites 
     to do best integration possible, then add reaction and 
     non-MNX metabolites."""
     
     ### Use SBML to pull out reaction details (met details are implied)
     
     ## Read in SBML file
     reader = SBMLReader()
     document = reader.readSBMLFromFile(SBML_filename)
     model = document.getModel()
     
     idx = 0
     counter = loop_counter(len(model.getListOfReactions()))
     
     for rxn in model.getListOfReactions():
         
         idx += 1
         counter.step()
         
         
         ## Get reaction reversibility (and direction)
         reversible = rxn.getReversible()
         if reversible == True:
             direction_id = "both"
         else:
             direction_id = "ltr"
         direction = Direction.objects.get(direction=direction_id)
         
         
         ## Get "SOURCE: " and "REACTION: " from notes and find best ID
         note_source_id = get_note(rxn, "SOURCE")
         note_reaction_name = get_note(rxn, "REACTION")
         if note_reaction_name is not None:
             ## Has MNX ID
             reaction_name = note_reaction_name
         elif note_source_id is not None:
             ## Has alternative ID
             reaction_name = note_source_id
         else:
             reaction_name = rxn.getId()
         reaction_name = reaction_name.split(";")[0]
         reaction_name = re.sub("^R\_","",reaction_name)
         
         
         ## Create new reaction
         new_reaction = Reaction(
             name=reaction_name,
             source=source,
         )
         new_reaction.save()
         
         ## Get all old synonyms for this reaction ID and point them towards the model reactions
         for synonym in Reaction_synonym.objects.filter(reaction__name=reaction_name):
             synonym.reaction = new_reaction
             synonym.save()
         
         try:
             synonym = Reaction_synonym.objects.get(synonym=reaction_name)
             synonym.reaction = new_reaction
             synonym.save()
         except:    
             new_reaction_synonym = Reaction_synonym(
                 synonym = reaction_name,
                 reaction = new_reaction,
                 source = source
             )
             new_reaction_synonym.save()
             
         
         ## Create list of all participants in reaction
         metabolites_list = []
         for reactant in rxn.getListOfReactants():
             metabolites_list.append(("reactant",reactant))                     
         for product in rxn.getListOfProducts():
             metabolites_list.append(("product",product))
               
          
         ### For each reactant and product, create stoichiometry
         for metabolite_tuple in metabolites_list:
               
             met = metabolite_tuple[1]
             met_type = metabolite_tuple[0] 
             species_id = met.getSpecies()
               
             ## Establish species ID and compartment
             try:
                 u = species_id.split("__64__")
                 metabolite_id = u[0]
                 compartment_id = u[1]
             except:
                 metabolite_id = species_id
                 print(" - '%s' ID without compartment?" % species_id)
               
             ## Get or create metabolite
             if Metabolite.objects.filter(id=metabolite_id).count() == 1:
                 metabolite = Metabolite.objects.get(id=metabolite_id)
             elif Metabolite.objects.filter(metabolite_synonym__synonym=metabolite_id).distinct().count() == 1:
                 metabolite = Metabolite.objects.filter(metabolite_synonym__synonym=metabolite_id).distinct()[0]
             else:
                 ## There may be multiple entries with the same synonym, so eliminate the synonyms
                 Metabolite_synonym.objects.filter(synonym=metabolite_id).delete()
                 
                 ## Create new metabolite with ID metabolite_id
                 metabolite = Metabolite(
                     id=metabolite_id,
                     name=metabolite_id,
                     source=source
                 )
                 metabolite.save()
                 metabolite_synonym = Metabolite_synonym(
                     synonym = metabolite_id,
                     metabolite = metabolite,
                     source=source
                 )
                 metabolite_synonym.save()                    
              
              
             ## Get compartment 
             try:
                 compartment = Compartment.objects.get(id=compartment_id)
             except:
                 compartment = Compartment(
                     id = compartment_id,
                     name = compartment_id,
                     source = source
                 )
                 try:
                     compartment.save()
                 except:
                     print compartment.id
                     print compartment.name
                     print compartment.source
                     sys.exit(1)
                  
                  
             ## Get stoichiometric coefficient     
             coeff = met.getStoichiometry()
             if met_type == "reactant":
                 coeff = -1*coeff
              
             ## Create stoichiometry for reaction
             stoichiometry = Stoichiometry(
                 reaction = new_reaction,
                 metabolite = metabolite,
                 source = source,
                 compartment = compartment,
                 stoichiometry = coeff
             )
             stoichiometry.save()
         
         ## Now that reaction is imported, add to Metabolic_model with directionality
         model_reaction = Metabolic_model(
             source = source,
             reaction = new_reaction,
             direction = direction
         )
         model_reaction.save()
     
     counter.stop()    
     
     
     
     
     
     
     
     
     
コード例 #38
0
def test_m2m_metacom_targets_import():
    """
    Test m2m metacom when called in terminal.
    """
    # RUN THE COMMAND
    inppath = 'metabolic_data/'
    respath = 'metacom_output/'
    toy_bact_tgz_path = os.path.join(inppath, 'toy_bact.tar.gz')
    toy_bact_path = os.path.join(respath, 'toy_bact')
    seeds_path = os.path.join(inppath, 'seeds_toy.sbml')
    targets_path = os.path.join(inppath, 'targets_toy.sbml')

    if not os.path.exists(respath):
        os.makedirs(respath)
    with tarfile.open(toy_bact_tgz_path) as tar:
        tar.extractall(path=respath)
    metage2metabo.m2m_workflow.metacom_analysis(sbml_dir=toy_bact_path,
                                                out_dir=respath,
                                                seeds=seeds_path,
                                                host_mn=None,
                                                targets_file=targets_path)

    iscope_file = os.path.join(*[respath, 'indiv_scopes', 'indiv_scopes.json'])
    cscope_file = os.path.join(
        *[respath, 'community_analysis', 'comm_scopes.json'])
    resfile = os.path.join(*[respath, 'community_analysis', 'mincom.json'])
    targetfile = os.path.join(*[respath, 'producibility_targets.json'])

    # ISCOPE ANALYSIS
    # ensure there is the right number of computed indiv scopes
    with open(iscope_file, 'r') as json_idata:
        d_iscope = json.load(json_idata)
    assert len(d_iscope) == NUMBER_BACT
    # ensure the union and intersection are ok
    d_iscope_set = {}
    for elem in d_iscope:
        d_iscope_set[elem] = set(d_iscope[elem])
    union_scope = set.union(*list(d_iscope_set.values()))
    assert len(union_scope) == SIZE_UNION
    intersection_scope = set.intersection(*list(d_iscope_set.values()))
    assert len(intersection_scope) == SIZE_INTERSECTION
    # CSCOPE ANALYSIS
    with open(cscope_file, 'r') as json_cdata:
        d_cscope = json.load(json_cdata)
    assert len(d_cscope['com_scope']) == SIZE_CSCOPE
    # MINCOM ANALYSIS
    with open(resfile, 'r') as json_data:
        d_mincom = json.load(json_data)
    # Targets results
    with open(targetfile, 'r') as json_data:
        d_target = json.load(json_data)
    # ensure the minimal number of bacteria in a minimal community is ok
    assert len(d_mincom['bacteria']) == MIN_SIZE_COM
    # ensure the bacteria in union are ok
    assert set(d_mincom['union_bacteria']) == UNION
    # ensure the bacteria in intersection are ok
    assert set(d_mincom['inter_bacteria']) == INTERSECTION
    # ensure the newly producible targets are ok
    assert set(d_mincom['producible']) == PROD_TARGETS
    # ensure the bacteria in union are ok
    assert set(d_target['keystone_species']) == UNION
    # ensure the newly producible targets are ok
    assert set(d_target['mincom_producible']) == PROD_TARGETS

    # Ensure the final producers in com_only_producers contains reactions producing the targets
    sbml_products = {}
    for sbml_file in os.listdir(toy_bact_path):
        reader = SBMLReader()
        sbml_path = os.path.join(toy_bact_path, sbml_file)
        document = reader.readSBML(sbml_path)
        model = document.getModel()
        sbml_name, _ = os.path.splitext(sbml_file)
        sbml_products[sbml_name] = [
            product.getSpecies()
            for sbml_reaction in model.getListOfReactions()
            for product in sbml_reaction.getListOfProducts()
        ]

    for target in d_target['com_only_producers']:
        for species in d_target['com_only_producers'][target]:
            assert target in sbml_products[species]
    # clean
    shutil.rmtree(respath)
コード例 #39
0
# Code for simple gene expression without delay

# Import relevant types
from bioscrape.types import Model
from bioscrape.simulator import DeterministicSimulator, SSASimulator
from bioscrape.simulator import ModelCSimInterface

import numpy as np
from libsbml import SBMLReader

# Load the model by creating a model with the file name containing the model
reader = SBMLReader()
# m = reader.readSBMLFromFile('splitEnzymeKinietics1.xml')
# m = m.getModel();
# print str(m.getNumSpecies()) + " species"
m = Model('splitEnzymeKinietics1.xml')
# Expose the model's core characteristics for simulation. (i.e. stoichiometry,
# delays, and propensities)
s = ModelCSimInterface(m)

# Set the initial simulation time
s.py_set_initial_time(0)

# This function uses sparsity to further optimize the speed of deterministic
# simulations. You must call it before doing deterministic simulations.
s.py_prep_deterministic_simulation()

# Set up our desired timepoints for which to simulate.
# Must match with initial time.
timepoints = np.linspace(0, 1000, 1000)
コード例 #40
0
ファイル: io.py プロジェクト: nickfyson/pyabolism
def load_sbml(filename):
    """docstring for load_model"""

    notes_pattern = re.compile('\<\w*:?\w*\>([^<>]*)\<\/\w*:?\w*\>')

    sbml_document = SBMLReader().readSBML(filename)
    sbml_model    = sbml_document.getModel()

    if sbml_model is None:
        raise IOError('Failed to load model.')

    # initialise model

    model = MetaModel(id=sbml_model.getId())
    model.name = sbml_model.getName()

    # add all compartments
    for sbml_compartment in sbml_model.getListOfCompartments():
        compartment = Compartment(sbml_compartment.getId(), name=sbml_compartment.getName(),
                                  outside=sbml_compartment.getOutside())
        model.compartment.add(compartment)

    # add all the unit definitions
    for sbml_unitDefinition in sbml_model.getListOfUnitDefinitions():
        unit_definition = UnitDefinition(sbml_unitDefinition.getId())
        for sbml_unit in sbml_unitDefinition.getListOfUnits():
            unit = Unit(UnitKind_toString(sbml_unit.getKind()))
            unit.multiplier = sbml_unit.getMultiplier()
            unit.scale      = sbml_unit.getScale()
            unit.exponent   = sbml_unit.getExponent()
            unit.offset     = sbml_unit.getOffset()
            unit_definition.units.append(unit)
        model.unit_definition[unit_definition.id] = unit_definition

    # add all species
    for sbml_species in sbml_model.getListOfSpecies():

        metabolite = Metabolite(sbml_species.getId(),
                                name=sbml_species.getName(),
                                compartment=sbml_species.getCompartment(),
                                charge=sbml_species.getCharge(),
                                boundaryCondition=sbml_species.getBoundaryCondition())

        metabolite.raw_notes = sbml_species.getNotesString()
        model.metabolite.add(metabolite)

    # add all reactions
    for sbml_reaction in sbml_model.getListOfReactions():

        reaction = Reaction(sbml_reaction.getId(), name=sbml_reaction.getName(),
                            reversible=sbml_reaction.getReversible())

        for string in notes_pattern.findall(sbml_reaction.getNotesString()):
            reaction.notes[string.split(':')[0].strip()] = string.split(':')[-1].strip()

        reaction.lower_bound = \
            sbml_reaction.getKineticLaw().getParameter('LOWER_BOUND').getValue() \
            if sbml_reaction.getKineticLaw().getParameter('LOWER_BOUND') else None
        reaction.upper_bound = \
            sbml_reaction.getKineticLaw().getParameter('UPPER_BOUND').getValue() \
            if sbml_reaction.getKineticLaw().getParameter('UPPER_BOUND') else None

        reaction.default_bounds = (reaction.lower_bound, reaction.upper_bound)

        reaction.objective_coefficient = \
            sbml_reaction.getKineticLaw().getParameter('OBJECTIVE_COEFFICIENT').getValue() \
            if sbml_reaction.getKineticLaw().getParameter('OBJECTIVE_COEFFICIENT') else 0.0

        for sbml_reactant in sbml_reaction.getListOfReactants():

            try:
                metabolite      = model.metabolite[sbml_reactant.getSpecies()]
            except KeyError:
                m_id = sbml_reactant.getSpecies()
                metabolite = Metabolite(m_id,
                                        compartment=m_id.split('_')[-1],
                                        boundaryCondition=False)
                model.metabolite.add(metabolite)
            stoichiometry   = -1.0 * sbml_reactant.getStoichiometry()

            reaction.add_participant(metabolite, stoichiometry)

        for sbml_reactant in sbml_reaction.getListOfProducts():

            try:
                metabolite      = model.metabolite[sbml_reactant.getSpecies()]
            except KeyError:
                m_id = sbml_reactant.getSpecies()
                metabolite = Metabolite(m_id,
                                        compartment=m_id.split('_')[-1],
                                        boundaryCondition=False)
                model.metabolite.add(metabolite)
            stoichiometry   = 1.0 * sbml_reactant.getStoichiometry()

            reaction.add_participant(metabolite, stoichiometry)

        model.reaction.add(reaction)

    return model
コード例 #41
0
import re




if __name__ == '__main__':
    
    gpr_file = '/Users/wbryant/Dropbox/UG Project - COGzymes/model data/iAH991_reactions_revised.csv'
    f_in = open(gpr_file,'r')
    gpr_dict = {}
    for line in f_in:
        cols = line.split("\t")
        gpr_dict[cols[1]] = cols[4]
    
    infile = '/Users/wbryant/Dropbox/UG Project - COGzymes/model data/BTH_iah991.xml'
    reader = SBMLReader()
    document = reader.readSBMLFromFile(infile)
    model = document.getModel()
    
    notes_string = '  <body xmlns="http://www.w3.org/1999/xhtml">\n    <p>GENE_ASSOCIATION: %s</p>\n  </body>'
    
    for reaction in model.getListOfReactions():
        
        id_rxn = reaction.getId()
        
        id_rxn = re.sub("^R_","",id_rxn)
        id_rxn = re.sub("_LPAREN_","(",id_rxn)
        id_rxn = re.sub("_RPAREN_",")",id_rxn)
        id_rxn = re.sub("_DASH_","-",id_rxn)        
        
        if id_rxn in gpr_dict:
コード例 #42
0
class GEMtractor:
  """Main class for the GEMtractor
  
  It reads an SBML file, trims entities, and extracts the encoded network.
  
  :param sbml_file: path to the SBML file
  :type sbml_file: str
  """
  
  def __init__(self, sbml_file):
    self.__GENE_ASSOCIATION_PATTERN = re.compile(r".*GENE_ASSOCIATION:([^<]+) *<.*", re.DOTALL)
    self.__GENE_LIST_PATTERN = re.compile(r".*GENE_LIST: *([^ <][^<]*)<.*", re.DOTALL)
    self.__EXPRESSION_PARSER = self.__get_expression_parser ()
    self.__logger = logging.getLogger(__name__)
    self.__reaction_gene_map = {}
    self.__sbml_file = sbml_file
    self.__fbc_plugin = None
    self.__logger.debug("reading sbml file " + self.__sbml_file)
    self.sbml = SBMLReader().readSBML(self.__sbml_file)
    if self.sbml.getNumErrors() > 0:
      e = []
      for i in range (0, self.sbml.getNumErrors()):
        e.append (self.sbml.getError(i).getMessage())
      raise IOError ("model seems to be invalid: " + str (e))
  
  def __get_expression_parser (self):
    """
    get a parser to parse gene-association strings of reactions
    
    :return: the expression parser
    :rtype: `pyparsing:ParserElement <https://pyparsing-docs.readthedocs.io/en/latest/pyparsing.html#pyparsing.ParserElement>`_
    """
    variables = pp.Word(pp.alphanums + "_-.") 
    condition = pp.Group(variables)
    return pp.infixNotation(condition,[("and", 2, pp.opAssoc.LEFT, ),("or", 2, pp.opAssoc.LEFT, ),])


  def _parse_expression (self, expression):
    """
    parse a gene-association expression
    
    uses the expression parser from :func:`_GEMtractor__get_expression_parser`
    
    :param expression: the gene-association expression
    :type expression: str
    
    :return: the parse result
    :rtype: `pyparsing:ParseResults <https://pyparsing-docs.readthedocs.io/en/latest/pyparsing.html#pyparsing.ParseResults>`_
    """
    try:
        return self.__EXPRESSION_PARSER.parseString(expression.lower (), True)
    except pp.ParseException as e:
        raise InvalidGeneExpression ("cannot parse expression: >>" + expression + "<< -- " + getattr(e, 'message', repr(e)))

  def _unfold_complex_expression (self, parseresult):
    """
    unfold a gene-association parse result
    
    takes a parse result and unfolds it to a list of alternative gene complexes, which can catalyze a certain reaction
    
    :param parseresult: the result of the expression parser
    :type parseresult: `pyparsing:ParseResults <https://pyparsing-docs.readthedocs.io/en/latest/pyparsing.html#pyparsing.ParseResults>`_
    
    :return: the list of gene-complexes catalyzing
    :rtype: list of :class:`.network.genecomplex.GeneComplex`
    """
    if isinstance(parseresult, pp.ParseResults):
        
        if len(parseresult) == 1:
            return self._unfold_complex_expression (parseresult[0])
        
        if len(parseresult) > 1 and len(parseresult) % 2 == 1:
            # this is a chain of ANDs/ORs
            # make sure it's only ANDs or only ORs
            for i in range (0, math.floor (len(parseresult) / 2) - 1):
              if parseresult[2*i+1] != parseresult[2*i+3]:
                # if it's mixed we'll raise an error
                self.__logger.critical('and/or mixed in expression: ' + str(parseresult[1]))
                raise NotImplementedError ('and/or mixed in expression: ' + str(parseresult[1]))
            
            logic = None
            if parseresult[1] == "and":
                logic = True
            elif parseresult[1] == "or":
                logic = False
            else:
                self.__logger.critical('neither and nor or -> do not understand: ' + str(parseresult[1]))
                raise NotImplementedError ('neither and nor or -> do not understand: ' + str(parseresult[1]))
            
            # parse the first subterm
            term_combination = self._unfold_complex_expression (parseresult[0])
            
            # from the second subterm, it depends on the logic-connection on how to combine subterms
            for i in range(2, len(parseresult), 2):
                if logic:
                    # it's ANDed -> so every previous item needs to be connected to every next item
                    newterms = self._unfold_complex_expression (parseresult[i])
                    # ~ tmp = []
                    for a in term_combination:
                        for b in newterms:
                          a.add_genes (b)
                            # ~ tmp.append (str(a)+" and "+str (b))
                    # ~ term_combination = tmp
                else:
                    # just add them to the list as alternatives
                    term_combination += self._unfold_complex_expression (parseresult[i])
            
            return term_combination
        
        else:
            self.__logger.critical('unexpected expression: ' + str(parseresult))
            raise NotImplementedError ('unexpected expression: ' + str(parseresult))
            
    else:
        return [GeneComplex (Gene (parseresult))]


  def _extract_genes_from_sbml_notes (self, annotation, default):
    """
    extract the genes from a free-text sbml note
    
    expects the gene associations as
    
    .. code-block:: xml
    
       <p>GENE_ASSOCIATION: a and (b or c)</p>
    
    
    :param annotation: the annotation string
    :param default: the default to return if no gene-associations were found
    
    :type annotation: str
    :type default: str
    
    :return: the gene-associations
    :rtype: string
    """
    m = re.match (self.__GENE_ASSOCIATION_PATTERN, annotation)
    if m:
        g = m.group (1).strip()
        if len (g) > 0:
          return g
    return default
  
  def _overwrite_genes_in_sbml_notes (self, new_genes, reaction):
    """
    set the gene-associations for a note in an sbml reaction
    
    overwrites it, if it was set already
    
    :param new_genes: the new gene-associations
    :param reaction: the sbml reaction
    :type new_genes: str
    :type reaction: `libsbml:Reaction <http://sbml.org/Special/Software/libSBML/docs/python-api/classlibsbml_1_1_reaction.html>`_
    """
    m = re.match (self.__GENE_ASSOCIATION_PATTERN, reaction.getNotesString())
    if m:
      reaction.setNotes (reaction.getNotesString().replace (m.group (1), new_genes))
    else:
      self.__logger.debug('no gene notes to update: ' + reaction.getId ())
    
    # TODO
    # also update the GENE_LIST using __GENE_LIST_PATTERN
    # but for this we need the list of genes here (not only the logic expression)
      
  
  def get_gene_product_annotations (self, gene):
    """
    get the annotations of a gene product
    
    if the document is annotated using the `FBC package <http://sbml.org/Documents/Specifications/SBML_Level_3/Packages/fbc>`_ there is good chance we'll find more information about a gene in the gene-product's annotations
    
    :param gene: the label gene product of interest
    :type gene: str
    
    :return: the annotations of the gene-product labeled `gene` or None if there are no such annotations
    :rtype: xml str
    
    """
    if self.__fbc_plugin is not None:
      gp = self.__fbc_plugin.getGeneProductByLabel (gene)
      if gp is not None:
        return self.__fbc_plugin.getGeneProductByLabel (gene).getAnnotationString()
    return None
    
    
  def get_reaction_annotations (self, reactionid):
    """
    get the annotations of an sbml reaction
    
    :param reactionid: the identifier of the reaction in the sbml document
    :type reactionid: str
    
    :return: the annotations of that reaction
    :rtype: xml str
    """
    return self.sbml.getModel().getReaction (reactionid).getAnnotationString ()
    
   
  
  def get_sbml (self, filter_species = [], filter_reactions = [], filter_genes = [], filter_gene_complexes = [], remove_reaction_enzymes_removed = True, remove_ghost_species = False, discard_fake_enzymes = False, remove_reaction_missing_species = False, removing_enzyme_removes_complex = True):
    """ Get a filtered SBML document from the model file
    
    this parses the SBML file, applies the trimming according to the arguments, and returns the trimmed model
    
    :param filter_species: species identifiers to get rid of
    :param filter_reactions: reaction identifiers to get rid of
    :param filter_genes: enzyme identifiers to get rid of
    :param filter_gene_complexes: enzyme-complex identifiers to get rid of, every list-item should be of format: 'A + B + gene42'
    :param remove_reaction_enzymes_removed: should we remove a reaction if all it's genes were removed?
    :param remove_ghost_species: should species be removed, that do not participate in any reaction anymore - even though they might be required in other entities?
    :param discard_fake_enzymes: should fake enzymes (implicitly assumes enzymes, if no enzymes are annotated to a reaction) be removed?
    :param remove_reaction_missing_species: remove a reaction if one of the participating genes was removed?
    :param removing_enzyme_removes_complex: if an enzyme is removed, should also all enzyme complexes be removed in which it participates?
    
    :type filter_species: list of str
    :type filter_reactions: list of str
    :type filter_genes: list of str
    :type filter_gene_complexes: list of str
    :type remove_reaction_enzymes_removed: bool
    :type remove_ghost_species: bool
    :type discard_fake_enzymes: bool
    :type remove_reaction_missing_species: bool
    :type removing_enzyme_removes_complex: bool
    
    :return: the SBML document
    :rtype: `libsbml:SBMLDocument <http://sbml.org/Special/Software/libSBML/docs/python-api/classlibsbml_1_1_s_b_m_l_document.html>`_
    
    """
    self.__logger.debug("processing sbml model from " + self.__sbml_file)
    model = self.sbml.getModel()
    name = model.getName ()
    if name is None or len (name) < 1:
        name = model.getId()
    model.setId (model.getId() + "_gemtracted_ReactionNetwork")
    model.setName ("GEMtracted ReactionNetwork of " + name)
    self.__logger.info("got proper sbml model")
    
    self.__fbc_plugin = model.getPlugin ("fbc")
    
    self.__logger.debug("append a note")
    Utils.add_model_note (model, filter_species, filter_reactions, filter_genes, filter_gene_complexes, remove_reaction_enzymes_removed, remove_ghost_species, discard_fake_enzymes, remove_reaction_missing_species, removing_enzyme_removes_complex)
    
    if filter_species is None:
      filter_species = []
    if filter_reactions is None:
      filter_reactions = []
    if filter_genes is None:
      filter_genes = []
    if filter_gene_complexes is None:
      filter_gene_complexes = []
      
    
    if len(filter_species) > 0 or len(filter_reactions) > 0 or len(filter_genes) > 0 or len(filter_gene_complexes) > 0 or discard_fake_enzymes:
      try:
        #TODO dc modified?
        self.__logger.debug("filtering things")
        for n in range (model.getNumReactions () - 1, -1, -1):
          reaction = model.getReaction (n)
          if filter_reactions is not None and reaction.getId () in filter_reactions:
            model.removeReaction (n)
            continue
          
          if filter_species is not None:
            for sn in range (reaction.getNumReactants() -1, -1, -1):
                s = reaction.getReactant(sn).getSpecies()
                if s in filter_species:
                  if remove_reaction_missing_species:
                    model.removeReaction (n)
                    raise BreakLoops
                  reaction.removeReactant (sn)
                
            for sn in range (reaction.getNumProducts() -1, -1, -1):
                s = reaction.getProduct(sn).getSpecies()
                if s in filter_species:
                  if remove_reaction_missing_species:
                    model.removeReaction (n)
                    raise BreakLoops
                  reaction.removeProduct (sn)
                
            for sn in range (reaction.getNumModifiers() -1, -1, -1):
                s = reaction.getModifier(sn).getSpecies()
                if s in filter_species:
                  if remove_reaction_missing_species:
                    model.removeReaction (n)
                    raise BreakLoops
                  reaction.removeModifier (sn)
          
          if filter_genes is not None:
            current_genes = self._get_genes (reaction)
            self.__logger.debug("current genes: " + self._implode_genes (current_genes) + " - reaction: " + reaction.getId ())
            
            if len(current_genes) < 1:
              self.__logger.info("did not find genes in reaction " + reaction.getId ())
              raise NotImplementedError ("did not find genes in reaction " + reaction.getId ())
            
            self.__logger.info(discard_fake_enzymes)
            if discard_fake_enzymes and len(current_genes) == 1 and "reaction_" in current_genes[0].get_id ():
              model.removeReaction (n)
              continue
            # if len(current_genes) == 1 and current_genes[0] == reaction.getId ():
            
            final_genes = []
            for g in current_genes:
              # ~ print (g.get_id())
              # ~ print (g.genes)
              if g.get_id () not in filter_genes and g.get_id () not in filter_gene_complexes and not (removing_enzyme_removes_complex and g.contains_one_of (filter_genes)):
                # ~ print (g.get_id() + " will be in model")
                final_genes.append (g)
            
            if len (final_genes) < 1:
              if remove_reaction_enzymes_removed:
                model.removeReaction (n)
                continue
              else:
                final_genes = [Gene (reaction.getId ())]
            
            # should we update the genes in the model?
            if (len (final_genes) != len (current_genes)):
              self._set_genes_in_sbml (final_genes, reaction)
            
            self.__reaction_gene_map[reaction.getId ()] = final_genes
          
          if reaction.getNumReactants() + reaction.getNumModifiers() + reaction.getNumProducts() == 0:
            model.removeReaction (n)
        
        if len(filter_species) > 0 and remove_ghost_species:
          for n in range (model.getNumSpecies () - 1, -1, -1):
            species = model.getSpecies (n)
            if species.getId () in filter_species:
              model.removeSpecies (n)
      except BreakLoops:
        pass
    
    return self.sbml
  
  def _set_genes_in_sbml (self, genes, reaction):
    """
    set the genes of a reaction in an sbml model
    
    tries to set the genes using the `FBC package <http://sbml.org/Documents/Specifications/SBML_Level_3/Packages/fbc>`_, if supported, otherwise sets the genes in the reaction's notes.
    if genes already exist the will be overwritten...
    
    :param genes: the new list of gene associations
    :param reaction: the reaction to annotate
    :type genes: list of :class:`.network.genecomplex.GeneComplex`
    :type reaction: `libsbml:Reaction <http://sbml.org/Special/Software/libSBML/docs/python-api/classlibsbml_1_1_reaction.html>`_
    """
    
    rfbc = reaction.getPlugin ("fbc")
    if rfbc is not None:
      gpa = rfbc.getGeneProductAssociation()
      if gpa is not None:
        g = self._implode_genes (genes)
        gpa.setAssociation(FbcAssociation_parseFbcInfixAssociation (g, self.__fbc_plugin))
      else:
        self.__logger.debug('no fbc to update: ' + reaction.getId ())
    
    self._overwrite_genes_in_sbml_notes (self._implode_genes (genes), reaction)
    
  
  def _implode_genes (self, genes):
    """
    implode a list of genes into a proper logical expression

    basically joins the list with `or`, making sure every item is enclosed in brackets
    
    :param genes: the list of optional genes
    :type genes: list of :class:`.network.genecomplex.GeneComplex`
    
    :return: the logical expression (genes joined using 'or')
    :rtype: str
    """
    r = "("
    for g in genes:
      r += str (g.to_sbml_string ()) + " or "
    
    return r[:-4] + ")"
  
  
  def _get_genes (self, reaction):
    """
    get the genes associated to a reaction
    
    Will cache the gene associations. If there is nothing in cache, it will run :func:`_GEMtractor__find_genes`, :func:`_parse_expression`, and  :func:`_unfold_complex_expression` to find them.
    
    :param reaction: the SBML S-Base reaction
    :type reaction: `libsbml:Reaction <http://sbml.org/Special/Software/libSBML/docs/python-api/classlibsbml_1_1_reaction.html>`_
    
    :return: list of GeneComplexes catalyzing the reaction
    :rtype: list of :class:`.network.genecomplex.GeneComplex`
    
    """
    if reaction.getId () not in self.__reaction_gene_map:
      self.__reaction_gene_map[reaction.getId ()] = self._unfold_complex_expression(self._parse_expression(self.__find_genes (reaction)))
    
    return self.__reaction_gene_map[reaction.getId ()]
    
  
  def __find_genes (self, reaction):
    """
    Look for genes associated to a reaction.
    
    Will check if there are annotations using the
    `FBC package <http://sbml.org/Documents/Specifications/SBML_Level_3/Packages/fbc>`_,
    otherwise it will evaluate the reactions' notes.
    If there are still no valid gene associations, the function assumes there is an undocumented catalyst and falls back to a gene with the reaction's identifier (prefixed with 'reaction\_').
    
    :param reaction: the SBML S-Base reaction
    :type reaction: `libsbml:Reaction <http://sbml.org/Special/Software/libSBML/docs/python-api/classlibsbml_1_1_reaction.html>`_
    
    :return: the gene associations
    :rtype: str
    """
    rfbc = reaction.getPlugin ("fbc")
    if rfbc is not None:
        gpa = rfbc.getGeneProductAssociation()
        if gpa is not None:
            return gpa.getAssociation().toInfix()
        else:
            self.__logger.debug('no association: ' + reaction.getId ())
    else:
        self.__logger.debug('no fbc: ' + reaction.getId ())
    
    return self._extract_genes_from_sbml_notes (reaction.getNotesString(), "reaction_" + reaction.getId ())
      
      
  
  def extract_network_from_sbml (self):
    """
    Extract the Network from the SBML model
    
    Will go through the SBML file and convert the (remaining) entities into our own network structure.
    
    :return: the network (after optional trimming)
    :rtype: :class:`.network.network.Network`
    
    """
    if self.sbml.getNumErrors() > 0:
      raise IOError ("model seems to be invalid")
    model = self.sbml.getModel()
    self.__logger.info ("extracting network from " + model.getId ())
    
    network = Network ()
    species = {}
    
    for n in range (0, model.getNumSpecies()):
      s = model.getSpecies (n)
      species[s.getId ()] = network.add_species (s.getId (), s.getName ())
    
    for n in range (0, model.getNumReactions()):
      if n % 100 == 0:
        self.__logger.info ("processing reaction " + str (n))
      reaction = model.getReaction(n)
      # TODO: reversible?
      #r = Reaction (reaction.getId (), reaction.getName ())
      r = network.add_reaction (reaction.getId (), reaction.getName ())
      if reaction.isSetReversible ():
        r.reversible = reaction.getReversible ()
      
      current_genes = self._get_genes (reaction)
      self.__logger.debug("current genes: " + self._implode_genes (current_genes) + " - reaction: " + reaction.getId ())
    
      if len(current_genes) < 1:
        self.__logger.debug("did not find genes in reaction " + reaction.getId ())
        raise NotImplementedError ("did not find genes in reaction " + reaction.getId ())
      
      network.add_genes (r, current_genes)
      
      for sn in range (0, reaction.getNumReactants()):
        s = reaction.getReactant(sn).getSpecies()
        r.add_input (species[s])
          
      for sn in range (0, reaction.getNumProducts()):
        s = reaction.getProduct(sn).getSpecies()
        r.add_output (species[s])
    
      
    self.__logger.info ("extracted network")
    return network