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

"""Utility for assigning pathways."""

import itertools as itt
import json
import os
import random
from collections import defaultdict
from typing import List, Optional

import bio2bel_hgnc
import bio2bel_mgi
import bio2bel_rgd

from pybel import BELGraph
from pybel.dsl import BaseAbundance, ListAbundance

__all__ = [
    'PathwayAssigner',
]


class PathwayAssigner:
    """A tool for assigning pathways to unannotated BEL edges."""

    def __init__(
        self,
        *,
        graph: BELGraph,
        managers: List,
        mgi_cache_path: Optional[str] = None,
        rgd_cache_path: Optional[str] = None,
    ):
        """Initialize the pathway assigner with several lookup dictionaries.

        :param managers: A ComPath manager or iterable of ComPath managers
        """
        self.graph = graph

        self.pathway_to_symbols = defaultdict(set)
        self.symbol_to_pathways = defaultdict(set)

        if not isinstance(managers, list):
            managers = []

        for manager in managers:
            self._add_manager(manager)

        # These won't be loaded more so convert to normal dicts
        self.pathway_to_symbols = dict(self.pathway_to_symbols)
        self.symbol_to_pathways = dict(self.symbol_to_pathways)

        # Prepare RGD mappings
        if rgd_cache_path is not None and os.path.exists(rgd_cache_path):
            with open(rgd_cache_path) as file:
                self.rgd_symbol_to_hgnc_symbol = json.load(file)
        else:
            self.rgd_symbol_to_hgnc_symbol = {}
            rgd_symbol_to_id = {}
            rgd_id_to_symbol = {}

            rgd_manager = bio2bel_rgd.Manager()
            rat_genes = rgd_manager.list_genes()
            for rat_gene in rat_genes:
                rgd_symbol_to_id[rat_gene.symbol] = rat_gene.rgd_id
                rgd_id_to_symbol[rat_gene.rgd_id] = rat_gene.symbol

            hgnc_manager = bio2bel_hgnc.Manager()
            genes = hgnc_manager.list_human_genes()
            for gene in genes:
                for rgd_id in gene.rgds:
                    rgd_symbol = rgd_id_to_symbol.get(rgd_id)
                    if rgd_symbol is None:
                        print(f'could not find rgd:{rgd_id}')
                        continue
                    self.rgd_symbol_to_hgnc_symbol[rgd_symbol] = gene.symbol

            if rgd_cache_path is not None:
                with open(rgd_cache_path, 'w') as file:
                    json.dump(self.rgd_symbol_to_hgnc_symbol, file, indent=2)

        # Prepare MGI mappings
        if mgi_cache_path is not None and os.path.exists(mgi_cache_path):
            with open(mgi_cache_path) as file:
                self.mgi_symbol_to_hgnc_symbol = json.load(file)
        else:
            self.mgi_symbol_to_hgnc_symbol = {}
            mgi_symbol_to_id = {}
            mgi_id_to_symbol = {}
            mgi_to_hgnc = {}

            mgi_manager = bio2bel_mgi.Manager()
            mouse_genes = mgi_manager.get_genes()
            for mouse_gene in mouse_genes:<commit_msg>Update pathway_assigner.py<commit_after># -*- coding: utf-8 -*-

"""Utility for assigning pathways."""

import itertools as itt
import json
import os
import random
from collections import defaultdict
from typing import List, Optional

import bio2bel_hgnc
import bio2bel_mgi
import bio2bel_rgd

from pybel import BELGraph
from pybel.dsl import BaseAbundance, ListAbundance

__all__ = [
    'PathwayAssigner',
]


class PathwayAssigner:
    """A tool for assigning pathways to unannotated BEL edges."""

    def __init__(
        self,
        *,
        graph: BELGraph,
        managers: List,
        mgi_cache_path: Optional[str] = None,
        rgd_cache_path: Optional[str] = None,
    ):
        """Initialize the pathway assigner with several lookup dictionaries.

        :param managers: A ComPath manager or iterable of ComPath managers
        """
        self.graph = graph

        self.pathway_to_symbols = defaultdict(set)
        self.symbol_to_pathways = defaultdict(set)

        if not isinstance(managers, list):
            managers = []

        for manager in managers:
            self._add_manager(manager)

        # These won't be loaded more so convert to normal dicts
        self.pathway_to_symbols = dict(self.pathway_to_symbols)
        self.symbol_to_pathways = dict(self.symbol_to_pathways)

        # Prepare RGD mappings
        if rgd_cache_path is not None and os.path.exists(rgd_cache_path):
            with open(rgd_cache_path) as file:
                self.rgd_symbol_to_hgnc_symbol = json.load(file)
        else:
            self.rgd_symbol_to_hgnc_symbol = {}
            rgd_symbol_to_id = {}
            rgd_id_to_symbol = {}

            rgd_manager = bio2bel_rgd.Manager()
            rat_genes = rgd_manager.list_genes()
            for rat_gene in rat_genes:
                rgd_symbol_to_id[rat_gene.symbol] = rat_gene.rgd_id
                rgd_id_to_symbol[rat_gene.rgd_id] = rat_gene.symbol

            hgnc_manager = bio2bel_hgnc.Manager()
            genes = hgnc_manager.list_human_genes()
            for gene in genes:
                for rgd_id in gene.rgds:
                    rgd_id = str(rgd_id)
                    rgd_symbol = rgd_id_to_symbol.get(rgd_id)
                    if rgd_symbol is None:
                        print(f'could not find rgd:{rgd_id}')
                        continue
                    self.rgd_symbol_to_hgnc_symbol[rgd_symbol] = gene.symbol

            if rgd_cache_path is not None:
                with open(rgd_cache_path, 'w') as file:
                    json.dump(self.rgd_symbol_to_hgnc_symbol, file, indent=2)

        # Prepare MGI mappings
        if mgi_cache_path is not None and os.path.exists(mgi_cache_path):
            with open(mgi_cache_path) as file:
                self.mgi_symbol_to_hgnc_symbol = json.load(file)
        else:
            self.mgi_symbol_to_hgnc_symbol = {}
            mgi_symbol_to_id = {}
            mgi_id_to_symbol = {}
            mgi_to_hgnc = {}

            mgi_manager = bio2bel_mgi.Manager()
            mouse_genes = mgi_manager.get_genes()
            for mouse_gene in mouse_genes:
{'fraction_non_alphanumeric': 0.07177341486964918, 'fraction_numerical': 0.002574831026713872, 'mean_word_length': 2.0767326732673266, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '1540394', 'n_tokens_mistral': 2023, 'n_tokens_neox': 1930, 'n_words': 430}