Ejemplo n.º 1
0
def make_sbgn(pysb_model, model_id):
    pa = PysbAssembler()
    pa.model = pysb_model
    for m in pysb_model.monomers:
        pysb_assembler.set_extended_initial_condition(pysb_model, m, 0)
    try:
        sbgn_str = pa.export_model('sbgn')
    except BngInterfaceError:
        logger.error('Reaction network could not be generated for SBGN.')
        return None
    return sbgn_str
Ejemplo n.º 2
0
def make_sbgn(pysb_model, model_id):
    pa = PysbAssembler()
    pa.model = pysb_model
    for m in pysb_model.monomers:
        pysb_assembler.set_extended_initial_condition(pysb_model, m, 0)
    try:
        sbgn_str = pa.export_model('sbgn')
    except BngInterfaceError:
        logger.error('Reaction network could not be generated for SBGN.')
        return None
    return sbgn_str
Ejemplo n.º 3
0
Archivo: api.py Proyecto: steppi/indra
    def post(self):
        """Assemble INDRA Statements and return PySB model string.

        Parameters
        ----------
        statements : list[indra.statements.Statement.to_json()]
            A list of INDRA Statements to assemble.

        export_format : str
            The format to export into, for instance "kappa", "bngl",
            "sbml", "matlab", "mathematica", "potterswheel". See
            http://pysb.readthedocs.io/en/latest/modules/export/index.html
            for a list of supported formats. In addition to the formats
            supported by PySB itself, this method also provides "sbgn"
            output.

        Returns
        -------
        image or model
            Assembled exported model. If export_format is kappa_im or kappa_cm,
            image is returned. Otherwise model string is returned.
        """
        args = request.json
        stmts_json = args.get('statements')
        export_format = args.get('export_format')
        stmts = stmts_from_json(stmts_json)
        pa = PysbAssembler()
        pa.add_statements(stmts)
        pa.make_model()
        try:
            for m in pa.model.monomers:
                pysb_assembler.set_extended_initial_condition(pa.model, m, 0)
        except Exception as e:
            logger.exception(e)

        if not export_format:
            model_str = pa.print_model()
        elif export_format in ('kappa_im', 'kappa_cm'):
            fname = 'model_%s.png' % export_format
            root = os.path.dirname(os.path.abspath(fname))
            graph = pa.export_model(format=export_format, file_name=fname)
            with open(fname, 'rb') as fh:
                data = 'data:image/png;base64,%s' % \
                    base64.b64encode(fh.read()).decode()
                return {'image': data}
        else:
            try:
                model_str = pa.export_model(format=export_format)
            except Exception as e:
                logger.exception(e)
                model_str = ''
        res = {'model': model_str}
        return res
Ejemplo n.º 4
0
def assemble_model(stmts):
    pa = PysbAssembler()
    pa.add_statements(stmts)
    model = pa.make_model(policies='one_step')
    pa.add_default_initial_conditions(100.0)

    try:
        targeted_agents = get_targeted_agents(stmts)
        no_upstream_active_agents = get_no_upstream_active_agents(stmts)
    except:
        targeted_agents = []
        no_upstream_active_agents = []
    try:
        chemical_agents = get_chemical_agents(stmts)
    except:
        chemical_agents = []

    for m in model.monomers:
        try:
            if m.name in targeted_agents or m.name in no_upstream_active_agents:
                pysb_assembler.set_base_initial_condition(model,
                    model.monomers[m.name], 50.0)
                pysb_assembler.set_extended_initial_condition(model, m, 50.0)
            elif m.name in chemical_agents:
                pysb_assembler.set_base_initial_condition(model,
                    model.monomers[m.name], 10000.0)
            else:
                pysb_assembler.set_extended_initial_condition(model, m, 0)
        except:
            pysb_assembler.set_extended_initial_condition(model, m, 0)
    # Tweak parameters
    for param in model.parameters:
        if 'kf' in param.name and 'bind' in param.name:
            param.value = param.value * 100
    return model
Ejemplo n.º 5
0
def assemble_model(stmts):
    pa = PysbAssembler()
    pa.add_statements(stmts)
    model = pa.make_model(policies='one_step')
    pa.add_default_initial_conditions(100.0)

    try:
        targeted_agents = get_targeted_agents(stmts)
        no_upstream_active_agents = get_no_upstream_active_agents(stmts)
    except:
        targeted_agents = []
        no_upstream_active_agents = []
    try:
        chemical_agents = get_chemical_agents(stmts)
    except:
        chemical_agents = []

    for m in model.monomers:
        try:
            if m.name in targeted_agents or m.name in no_upstream_active_agents:
                pysb_assembler.set_base_initial_condition(model,
                    model.monomers[m.name], 50.0)
                pysb_assembler.set_extended_initial_condition(model, m, 50.0)
            elif m.name in chemical_agents:
                pysb_assembler.set_base_initial_condition(model,
                    model.monomers[m.name], 10000.0)
            else:
                pysb_assembler.set_extended_initial_condition(model, m, 0)
        except:
            pysb_assembler.set_extended_initial_condition(model, m, 0)
    # Tweak parameters
    for param in model.parameters:
        if 'kf' in param.name and 'bind' in param.name:
            param.value = param.value * 100
    return model
Ejemplo n.º 6
0
def assemble_pysb():
    """Assemble INDRA Statements and return PySB model string."""
    if request.method == 'OPTIONS':
        return {}
    response = request.body.read().decode('utf-8')
    body = json.loads(response)
    stmts_json = body.get('statements')
    export_format = body.get('export_format')
    stmts = stmts_from_json(stmts_json)
    pa = PysbAssembler()
    pa.add_statements(stmts)
    pa.make_model()
    try:
        for m in pa.model.monomers:
            pysb_assembler.set_extended_initial_condition(pa.model, m, 0)
    except Exception as e:
        logger.exception(e)

    if not export_format:
        model_str = pa.print_model()
    elif export_format in ('kappa_im', 'kappa_cm'):
        fname = 'model_%s.png' % export_format
        root = os.path.dirname(os.path.abspath(fname))
        graph = pa.export_model(format=export_format, file_name=fname)
        with open(fname, 'rb') as fh:
            data = 'data:image/png;base64,%s' % \
                base64.b64encode(fh.read()).decode()
            return {'image': data}
    else:
        try:
            model_str = pa.export_model(format=export_format)
        except Exception as e:
            logger.exception(e)
            model_str = ''
    res = {'model': model_str}
    return res
Ejemplo n.º 7
0
def draw_reaction_network(pysb_model, model_id):
    """Generate a PySB/BNG reaction network as a PNG file."""
    try:
        for m in pysb_model.monomers:
            pysb_assembler.set_extended_initial_condition(pysb_model, m, 0)
        diagram_dot = render_reactions.run(pysb_model)
    # TODO: use specific PySB/BNG exceptions and handle them
    # here to show meaningful error messages
    except Exception as e:
        logger.error('Could not generate model diagram.')
        logger.error(e)
        return None
    try:
        fname_prefix = make_pic_name(model_id, 'rxn')
        with open(fname_prefix + '.dot', 'wt') as fh:
            fh.write(diagram_dot)
        subprocess.call(('dot -T png -o %s.png %s.dot' %
                         (fname_prefix, fname_prefix)).split(' '))
        fname = fname_prefix + '.png'
    except Exception as e:
        logger.error('Could not save model diagram.')
        logger.error(e)
        return None
    return fname
Ejemplo n.º 8
0
def assemble_pysb():
    """Assemble INDRA Statements and return PySB model string."""
    if request.method == 'OPTIONS':
        return {}
    response = request.body.read().decode('utf-8')
    body = json.loads(response)
    stmts_json = body.get('statements')
    export_format = body.get('export_format')
    stmts = stmts_from_json(stmts_json)
    pa = PysbAssembler()
    pa.add_statements(stmts)
    pa.make_model()
    try:
        for m in pa.model.monomers:
            pysb_assembler.set_extended_initial_condition(pa.model, m, 0)
    except Exception as e:
        logger.exception(e)

    if not export_format:
        model_str = pa.print_model()
    elif export_format in ('kappa_im', 'kappa_cm'):
        fname = 'model_%s.png' % export_format
        root = os.path.dirname(os.path.abspath(fname))
        graph = pa.export_model(format=export_format, file_name=fname)
        with open(fname, 'rb') as fh:
            data = 'data:image/png;base64,%s' % \
                base64.b64encode(fh.read()).decode() 
            return {'image': data}
    else:
        try:
            model_str = pa.export_model(format=export_format)
        except Exception as e:
            logger.exception(e)
            model_str = ''
    res = {'model': model_str}
    return res
Ejemplo n.º 9
0
def draw_reaction_network(pysb_model, model_id):
    """Generate a PySB/BNG reaction network as a PNG file."""
    try:
        for m in pysb_model.monomers:
            pysb_assembler.set_extended_initial_condition(pysb_model, m, 0)
        diagram_dot = render_reactions.run(pysb_model)
    # TODO: use specific PySB/BNG exceptions and handle them
    # here to show meaningful error messages
    except Exception as e:
        logger.error('Could not generate model diagram.')
        logger.error(e)
        return None
    try:
        fname_prefix = make_pic_name(model_id, 'rxn')
        with open(fname_prefix + '.dot', 'wt') as fh:
            fh.write(diagram_dot)
        subprocess.call(('dot -T png -o %s.png %s.dot' %
                         (fname_prefix, fname_prefix)).split(' '))
        fname = fname_prefix + '.png'
    except Exception as e:
        logger.error('Could not save model diagram.')
        logger.error(e)
        return None
    return fname