def test_thorough_json_file(self): """Test the round-trip through a node-link JSON file.""" sio = StringIO() to_nodelink_file(sialic_acid_graph, sio) sio.seek(0) graph = from_nodelink_file(sio) self._help_test_equal(graph)
def get_graph( self, use_cached: bool = True, use_tqdm: bool = False, tqdm_kwargs: Optional[Mapping[str, Any]] = None, ) -> BELGraph: """Get the BEL graph from all sheets in this repository. .. warning:: This BEL graph isn't pre-filled with namespace and annotation URLs. """ if use_cached and os.path.exists(self._cache_json_path): return pybel.from_nodelink_gz(self._cache_json_path) graph = BELGraph() if self.metadata is not None: self.metadata.update(graph) logger.info('streamlining parser') bel_parser = BELParser(graph) paths = list(self.iterate_sheets_paths()) if use_tqdm: _tqdm_kwargs = dict(desc=f'Sheets in {self.directory}') if tqdm_kwargs: _tqdm_kwargs.update(tqdm_kwargs) paths = tqdm(list(paths), **_tqdm_kwargs) for path in paths: graph.path = path try: df = pd.read_excel(path) except LookupError as exc: logger.warning(f'Error opening {path}: {exc}') continue # Check columns in DataFrame exist if not _check_curation_template_columns(df): logger.warning(f'^ above columns in {path} were missing') continue process_df(bel_parser=bel_parser, df=df, use_tqdm=use_tqdm, tqdm_kwargs=dict(desc=f'Reading {path}')) if self.prior is not None: # assign edges to sub-graphs prior = self.get_prior() assign_subgraphs(graph=graph, prior=prior) pybel.to_nodelink_file(graph, self._cache_json_path, indent=2, sort_keys=True) return graph
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)
def test_thorough_json_file(self): sio = StringIO() to_nodelink_file(self.thorough_graph, sio) sio.seek(0) graph = from_nodelink_file(sio) self.bel_thorough_reconstituted(graph)