def upload_cbn_dir(dir_path, manager): """Uploads CBN data to edge store :param str dir_path: Directory full of CBN JGIF files :param pybel.Manager manager: """ t = time.time() for jfg_path in os.listdir(dir_path): if not jfg_path.endswith('.jgf'): continue path = os.path.join(dir_path, jfg_path) log.info('opening %s', path) with open(path) as f: cbn_jgif_dict = json.load(f) graph = pybel.from_cbn_jgif(cbn_jgif_dict) out_path = os.path.join(dir_path, jfg_path.replace('.jgf', '.bel')) with open(out_path, 'w') as o: pybel.to_bel_script(graph, o) strip_annotations(graph) enrich_pubmed_citations(manager=manager, graph=graph) pybel.to_database(graph, manager=manager) log.info('') log.info('done in %.2f', time.time() - t)
def main(directory: str): """Make hetionet exports.""" path = os.path.join(directory, 'hetionet.bel.nodelink.json.gz') if not os.path.exists(path): graph = get_hetionet() to_nodelink_gz(graph, path) else: click.echo('loading pickle from {}'.format(path)) graph = from_nodelink_gz(path) output_bel_gz_path = os.path.join(directory, 'hetionet.bel.gz') if not os.path.exists(output_bel_gz_path): click.echo('outputting whole hetionet as BEL GZ to {}'.format(output_bel_gz_path)) to_bel_script_gz(graph, output_bel_gz_path, use_identifiers=True) output_graphdati_jsonl_gz_path = os.path.join(directory, 'hetionet.bel.graphdati.jsonl.gz') if not os.path.exists(output_graphdati_jsonl_gz_path): click.echo('outputting whole hetionet as BEL GraphDati JSONL GZ to {}'.format(output_graphdati_jsonl_gz_path)) to_graphdati_jsonl_gz(graph, output_graphdati_jsonl_gz_path, use_identifiers=True) output_graphdati_gz_path = os.path.join(directory, 'hetionet.bel.graphdati.json.gz') if not os.path.exists(output_graphdati_gz_path): click.echo('outputting whole hetionet as BEL GraphDati JSON GZ to {}'.format(output_graphdati_gz_path)) to_graphdati_gz(graph, output_graphdati_gz_path, use_identifiers=True) summary_tsv_path = os.path.join(directory, 'hetionet_summary.tsv') if not os.path.exists(summary_tsv_path): click.echo('getting metaedges') rows = [] keep_keys = set() for value in get_metaedge_to_key(graph).values(): u, v, key = choice(list(value)) keep_keys.add(key) d = graph[u][v][key] bel = edge_to_bel(u, v, d, use_identifiers=True) rows.append((key[:8], bel)) df = pd.DataFrame(rows, columns=['key', 'bel']) df.to_csv(summary_tsv_path, sep='\t', index=False) non_sample_edges = [ (u, v, k, d) for u, v, k, d in tqdm(graph.edges(keys=True, data=True), desc='Getting non-sample edges to remove') if k not in keep_keys ] click.echo('Removing non-sample edges') graph.remove_edges_from(non_sample_edges) graph.remove_nodes_from(list(nx.isolates(graph))) sample_bel_path = os.path.join(directory, 'hetionet_sample.bel') click.echo('outputting sample hetionet in BEL to {}'.format(sample_bel_path)) to_bel_script(graph, sample_bel_path, use_identifiers=True) sample_graphdati_path = os.path.join(directory, 'hetionet_sample.bel.graphdati.json') click.echo('outputting sample hetionet in BEL to {}'.format(sample_bel_path)) to_graphdati_file(graph, sample_graphdati_path, use_identifiers=True, indent=2)
def write_neurommsig_bel( file: TextIO, df: pd.DataFrame, disease: str, nift_values: Mapping[str, str], ) -> None: """Write the NeuroMMSigDB excel sheet to BEL. :param file: a file or file-like that can be writen to :param df: :param disease: :param nift_values: a dictionary of lower-cased to normal names in NIFT """ graph = get_neurommsig_bel(df, disease, nift_values) pybel.to_bel_script(graph, file)
def save_model(self, path, output_format=None): """Save the :class:`pybel.BELGraph` using one of the outputs from :py:mod:`pybel` Parameters ---------- path : str The path to output to output_format : Optional[str] Output format as ``cx``, ``pickle``, ``json`` or defaults to ``bel`` """ if output_format == 'pickle': pybel.to_pickle(self.model, path) else: with open(path, 'w') as fh: if output_format == 'json': pybel.to_nodelink_file(self.model, fh) elif output_format == 'cx': pybel.to_cx_file(self.model, fh) else: # output_format == 'bel': pybel.to_bel_script(self.model, fh)