Ejemplo n.º 1
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'])
Ejemplo n.º 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
Ejemplo n.º 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'])
Ejemplo n.º 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
Ejemplo n.º 5
0
def readModelFromFile(filePath):
    reader = SBMLReader()
    document = reader.readSBML(filePath)

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

    model = document.getModel()
    return (model)
Ejemplo n.º 6
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()))))
Ejemplo n.º 7
0
 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
Ejemplo n.º 8
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)
Ejemplo n.º 9
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()
Ejemplo n.º 10
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
Ejemplo n.º 11
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)
Ejemplo n.º 12
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))
Ejemplo n.º 13
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
Ejemplo n.º 14
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
Ejemplo n.º 15
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)
Ejemplo n.º 16
0
 def __get_sbml_model (self):
   sbml = SBMLReader().readSBML("test/gene-filter-example.xml")
   self.assertTrue (sbml.getNumErrors() == 0)
   return sbml.getModel()
Ejemplo n.º 17
0
def parse(fp):
    from libsbml import SBMLReader
    document = SBMLReader().readSBML(fp)
    print(document.getNumErrors())
Ejemplo n.º 18
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] = {
Ejemplo n.º 19
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)
Ejemplo n.º 20
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
Ejemplo n.º 21
0
 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"