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(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 upload_jgf_directory(directory: str, manager: Manager): """Upload CBN data to edge store.""" if not (os.path.exists(directory) and os.path.isdir(directory)): logger.warning('directory does not exist: %s', directory) return t = time.time() for path in iter_jgf(directory): gpickle_path = get_jgf_corresponding_gpickle_path(path) if os.path.exists(gpickle_path): graph = from_pickle(gpickle_path) strip_annotations(graph) else: with open(path) as f: cbn_jgif_dict = json.load(f) graph = pybel.from_cbn_jgif(cbn_jgif_dict) strip_annotations(graph) to_pickle(graph, gpickle_path) try: insert_graph(manager, graph, public=True, use_tqdm=True) except OperationalError: manager.session.rollback() logger.info('could not insert %s', graph) logger.info('done in %.2f seconds', time.time() - t)
def test_jgif_interchange(self): """Tests data from CBN""" with open(test_jgif_path) as f: graph_jgif_dict = json.load(f) graph = from_cbn_jgif(graph_jgif_dict) self.assertEqual(jgif_expected_nodes, set(graph)) for u, v, d in jgif_expected_edges: self.assert_has_edge(graph, u, v, **d) # TODO test more thoroughly? export_jgif = to_jgif(graph) self.assertIsInstance(export_jgif, dict)
def process_cbn_jgif_file(file_name): """Return a PybelProcessor by processing a CBN JGIF JSON file. Parameters ---------- file_name : str The path to a CBN JGIF JSON file. Returns ------- bp : PybelProcessor A PybelProcessor object which contains INDRA Statements in bp.statements. """ with open(file_name, 'r') as jgf: return process_pybel_graph(pybel.from_cbn_jgif(json.load(jgf)))
def main(): """Convert all CBN graphs to Hipathia.""" if not os.path.exists(PATH): urlretrieve(URL, PATH) if not os.path.exists(SOURCE): with zipfile.ZipFile(PATH) as file: file.extractall(SOURCE) for filename in tqdm(os.listdir(SOURCE)): path = os.path.join(SOURCE, filename) with open(path) as f: cbn_jgif_dict = json.load(f) graph = pybel.from_cbn_jgif(cbn_jgif_dict) graph = pybel.grounding.ground(graph) pybel.to_hipathia(graph, OUTPUT)