Exemplo n.º 1
0
def write_bel_export(df: pd.DataFrame):
    """Write BEL export."""
    graph = BELGraph(name='Chemical Roles')
    it = tqdm(df.dropna().values,
              total=len(df.index),
              desc='mapping to BEL',
              unit_scale=True)
    for source_db, source_id, source_name, modulation, target_type, target_db, target_id, target_name in it:
        if target_type == 'molecular function':
            continue
        source = pybel.dsl.Abundance(
            namespace=source_db,
            identifier=source_id,
            name=source_name,
        )
        target = _type_map[target_type](
            namespace=target_db,
            identifier=target_id,
            name=target_name,
        )
        adder = _adders[modulation]
        adder(graph, source, target, citation='', evidence='')

    pybel.dump(graph,
               os.path.join(EXPORT_DIRECTORY, 'export.bel.nodelink.json.gz'),
               indent=2)
Exemplo n.º 2
0
 def test_io(self):
     with tempfile.TemporaryDirectory() as directory:
         path = os.path.join(directory, 'ampk.bel.nodelink.json')
         pybel.dump(pybel.examples.ampk_graph, path)
         self.assertTrue(os.path.exists(path))
         new_graph = pybel.load(path)
         self.assertIsNotNone(new_graph)
Exemplo n.º 3
0
def main(force: bool, user: str, password: str):
    """Download and dump the INDRA 'rona graph."""
    if not os.path.exists(GROUNDED_PATH) and not force:
        if not os.path.exists(RAW_PATH) and not force:
            click.echo('Getting EMMAA graph')
            graph = pybel.from_emmaa('covid19')
            pybel.dump(graph, RAW_PATH)
        else:
            click.echo('Loading EMMAA graph from path')
            graph = pybel.load(RAW_PATH)

        graph = pybel.grounding.ground(graph)
        graph.summarize()

        pybel.dump(graph, GROUNDED_PATH)
    else:
        graph = pybel.load(GROUNDED_PATH)

    res = pybel.to_bel_commons(
        graph=graph,
        host='https://bel.labs.coronawhy.org',
        user=user,
        password=password,
    )
    click.secho(json.dumps(res.json(), indent=2))
Exemplo n.º 4
0
def get_graph(graph_name: str, force: bool = False) -> pybel.BELGraph:
    """Get the Selventa large corpus as a BEL Graph."""
    url = f'https://github.com/cthoyt/selventa-knowledge/raw/master/selventa_knowledge/{graph_name}.bel'
    cache_path = module.join(name=f'{graph_name}.bel.pickle')
    if cache_path.exists() and not force:
        return pybel.load(cache_path)
    path = module.ensure(url=url, force=force)
    graph = pybel.from_bel_script(path, citation_clearing=False)
    pybel.dump(graph, path.as_posix())
    return graph
Exemplo n.º 5
0
def main():
    """Convert the Selventa graph to Hipathia."""
    if not os.path.exists(PATH):
        urlretrieve(URL, PATH)
    if not os.path.exists(GROUNDED_PATH):
        graph = pybel.load(PATH)
        graph = pybel.grounding.ground(graph)
        pybel.dump(graph, GROUNDED_PATH)
    else:
        graph = pybel.load(GROUNDED_PATH)
    pybel.to_hipathia(graph, HERE)
Exemplo n.º 6
0
 def assemble_pybel(self, mode='local', bucket=EMMAA_BUCKET_NAME):
     """Assemble the model into PyBEL and return the assembled model."""
     if not self.assembled_stmts:
         self.run_assembly()
     pba = PybelAssembler(self.assembled_stmts)
     pybel_model = pba.make_model()
     if mode == 's3' and 'pybel' in self.export_formats:
         fname = f'pybel_{self.date_str}.bel.nodelink.json.gz'
         pybel.dump(pybel_model, fname)
         logger.info(f'Uploading {fname}')
         client = get_s3_client(unsigned=False)
         client.upload_file(fname, bucket, f'exports/{self.name}/{fname}')
     return pybel_model
Exemplo n.º 7
0
def main(force: bool):
    """Export CBN in several BEL formats."""
    if not os.path.exists(HUMAN_PATH):
        urlretrieve(HUMAN_URL, HUMAN_PATH)

    full = BELGraph(name='CausalBioNet Human', version='2.0')
    full_nodelink_path = os.path.join(BEL, 'human_2.0.bel.nodelink.json')
    full_bel_path = os.path.join(BEL, 'human_2.0.bel.gz')

    if os.path.exists(full_nodelink_path) and not force:
        click.echo('already done!')
        return sys.exit(0)

    with zipfile.ZipFile(HUMAN_PATH) as human_zipfile:
        for zf in human_zipfile.filelist:
            name = zf.filename[:-len('.jgf')]
            raw_path = os.path.join(HUMAN_PARTS,
                                    f'{name}-raw.bel.nodelink.json')
            grounded_path = os.path.join(HUMAN_PARTS,
                                         f'{name}-grounded.bel.nodelink.json')

            if os.path.exists(grounded_path) and not force:
                continue

            with human_zipfile.open(zf) as file:
                graph = pybel.from_cbn_jgif_file(file)

            print(zf)

            pybel.dump(graph, raw_path, indent=2)

            graph = pybel.grounding.ground(graph)
            pybel.dump(graph, grounded_path, indent=2)

            pybel.struct.add_annotation_value(graph,
                                              'cbn_pathway',
                                              name,
                                              strict=False)
            full += graph

    pybel.dump(full, full_nodelink_path, indent=2)
    pybel.dump(full, full_bel_path)
Exemplo n.º 8
0
 def test_invalid_io(self):
     with tempfile.TemporaryDirectory() as directory:
         path = os.path.join(directory, 'ampk.bel.invalid.json')
         with self.assertRaises(InvalidExtensionError):
             pybel.dump(pybel.examples.ampk_graph, path)
         self.assertFalse(os.path.exists(path))
Exemplo n.º 9
0
def bel(directory):
    """Write BEL export."""
    import pybel
    from .bel import get_bel
    graph = get_bel()
    pybel.dump(graph, os.path.join(directory, 'crog.bel.nodelink.json.gz'))
 def write(manager: BELManagerMixin, output: str):
     """Write as BEL Script."""
     graph = manager.to_bel()
     click.echo(graph.summary_str())
     pybel.dump(graph, output)