<commit_before># -*- coding: utf-8 -*-

"""Conversion functions for BEL graphs with GraphDati."""

import gzip
import json
from io import BytesIO
from typing import Any, Iterable, List, Mapping, Optional, TextIO, Union

import requests
from more_itertools import chunked
from networkx.utils import open_file
from tqdm import tqdm

from ..canonicalize import edge_to_tuple
from ..constants import CITATION, CITATION_DB, CITATION_IDENTIFIER, EVIDENCE
from ..struct import BELGraph

__all__ = [
    'to_graphdati',
    'to_graphdati_file',
    'to_graphdati_gz',
    'to_graphdati_jsons',
    'to_graphdati_jsonl',
    'to_graphdati_jsonl_gz',
    'BioDatiClient',
    'post_graphdati',
]

NanopubMapping = Mapping[str, Mapping[str, Any]]

SCHEMA_URI = 'https://github.com/belbio/schemas/blob/master/schemas/nanopub_bel-1.0.0.yaml'


@open_file(1, mode='w')
def to_graphdati_file(graph: BELGraph, path: Union[str, TextIO], use_identifiers: bool = True, **kwargs) -> None:
    """Write this graph as GraphDati JSON to a file.

    :param graph: A BEL graph
    :param path: A path or file-like
    """
    json.dump(to_graphdati(graph, use_identifiers=use_identifiers), path, ensure_ascii=False, **kwargs)


def to_graphdati_gz(graph: BELGraph, path: str, **kwargs) -> None:
    """Write a graph as GraphDati JSON to a gzip file."""
    with gzip.open(path, 'wt') as file:
        to_graphdati_file(graph, file, **kwargs)


def to_graphdati_jsons(graph: BELGraph, **kwargs) -> str:
    """Dump this graph as a GraphDati JSON object to a string.

    :param graph: A BEL graph
    """
    return json.dumps(to_graphdati(graph), ensure_ascii=False, **kwargs)


@open_file(1, mode='w')
def to_graphdati_jsonl(graph, file, use_identifiers: bool = True, use_tqdm: bool = True):
    """Write this graph as a GraphDati JSON lines file."""
    it = graph.edges(keys=True, data=True)
    if use_tqdm:
        it = tqdm(it, desc='Outputting GraphDati JSONL', total=graph.number_of_edges())
    for u, v, k, d in it:
        nanopub = _make_nanopub(graph, u, v, k, d, use_identifiers)
        print(json.dumps(nanopub), file=file)


def to_graphdati_jsonl_gz(graph: BELGraph, path: str, **kwargs) -> None:
    """Write a graph as GraphDati JSONL to a gzip file."""
    with gzip.open(path, 'wt') as file:
        to_graphdati_jsonl(graph, file, **kwargs)
<commit_msg>Reuse code in graphdati writer<commit_after># -*- coding: utf-8 -*-

"""Conversion functions for BEL graphs with GraphDati."""

import gzip
import json
from io import BytesIO
from typing import Any, Iterable, List, Mapping, Optional, TextIO, Union

import requests
from more_itertools import chunked
from networkx.utils import open_file
from tqdm import tqdm

from ..canonicalize import edge_to_tuple
from ..constants import CITATION, CITATION_DB, CITATION_IDENTIFIER, EVIDENCE
from ..struct import BELGraph

__all__ = [
    'to_graphdati',
    'to_graphdati_file',
    'to_graphdati_gz',
    'to_graphdati_jsons',
    'to_graphdati_jsonl',
    'to_graphdati_jsonl_gz',
    'BioDatiClient',
    'post_graphdati',
]

NanopubMapping = Mapping[str, Mapping[str, Any]]

SCHEMA_URI = 'https://github.com/belbio/schemas/blob/master/schemas/nanopub_bel-1.0.0.yaml'


@open_file(1, mode='w')
def to_graphdati_file(graph: BELGraph, path: Union[str, TextIO], use_identifiers: bool = True, **kwargs) -> None:
    """Write this graph as GraphDati JSON to a file.

    :param graph: A BEL graph
    :param path: A path or file-like
    """
    json.dump(to_graphdati(graph, use_identifiers=use_identifiers), path, ensure_ascii=False, **kwargs)


def to_graphdati_gz(graph: BELGraph, path: str, **kwargs) -> None:
    """Write a graph as GraphDati JSON to a gzip file."""
    with gzip.open(path, 'wt') as file:
        to_graphdati_file(graph, file, **kwargs)


def to_graphdati_jsons(graph: BELGraph, **kwargs) -> str:
    """Dump this graph as a GraphDati JSON object to a string.

    :param graph: A BEL graph
    """
    return json.dumps(to_graphdati(graph), ensure_ascii=False, **kwargs)


@open_file(1, mode='w')
def to_graphdati_jsonl(graph, file, use_identifiers: bool = True, use_tqdm: bool = True):
    """Write this graph as a GraphDati JSON lines file."""
    nanopubs = _iter_graphdati(graph, use_identifiers=use_identifiers)
    if use_tqdm:
        nanopubs = tqdm(nanopubs, desc='Outputting GraphDati JSONL', total=graph.number_of_edges())
    for nanopub in nanopubs:
        print(json.dumps(nanopub), file=file)


def to_graphdati_jsonl_gz(graph: BELGraph, path: str, **kwargs) -> None:
    """Write a graph as GraphDati JSONL to a gzip file."""
    with gzip.open(path, 'wt') as file:
        to_graphdati_jsonl(graph, file, **kwargs)

{'fraction_non_alphanumeric': 0.1198892674616695, 'fraction_numerical': 0.002555366269165247, 'mean_word_length': 4.271604938271605, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6507806', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1652, 'n_words': 458}