<commit_before>from .moments import moments
from .fractions import print_fraction_of_abundances


def read_loom_layers(file_name, backup_url=None):
    """Read loom file and return AnnData object

    Arguments
    ---------
    loom_filepath: str
        directory of loom file

    backup_url: str

    all_loom_layers: bool, default=False
        whether to load loom layers 'loom_obs' and 'loom_var' into AnnData object

    Returns
    -------
    Updates the attributes .X, .var, .obsm['Xs'], .obsm['Xs']
    """
    import os
    import loompy
    from scanpy.api import AnnData
    from collections import OrderedDict

    if not os.path.exists(file_name):
        try:
            from urllib.request import urlretrieve
            urlretrieve(backup_url, file_name)
        except OSError:
            print("OS error: {0}".format(OSError))

    with loompy.connect(file_name, 'r') as lc:
        X = lc.layer['spliced'][:, :].T

        layers = OrderedDict()
        layers['spliced'] = lc.layer["spliced"][:, :].T
        layers['unspliced'] = lc.layer["unspliced"][:, :].T

        obs = dict(lc.col_attrs)
        obs['obs_names'] = obs.pop('CellID')

        var = dict(lc.row_attrs)
        var['var_names'] = var.pop('Gene')

        adata = AnnData(X, obs=obs, var=var, layers=layers)
        lc.close()

    return adata<commit_msg>fix loompy.connect.close()<commit_after>from .moments import moments
from .fractions import print_fraction_of_abundances


def read_loom_layers(file_name, backup_url=None):
    """Read loom file and return AnnData object

    Arguments
    ---------
    loom_filepath: str
        directory of loom file

    backup_url: str

    all_loom_layers: bool, default=False
        whether to load loom layers 'loom_obs' and 'loom_var' into AnnData object

    Returns
    -------
    Updates the attributes .X, .var, .obsm['Xs'], .obsm['Xs']
    """
    import os
    import loompy
    from scanpy.api import AnnData
    from collections import OrderedDict

    if not os.path.exists(file_name):
        try:
            from urllib.request import urlretrieve
            urlretrieve(backup_url, file_name)
        except OSError:
            print("OS error: {0}".format(OSError))

    with loompy.connect(file_name, 'r') as lc:
        X = lc.layer['spliced'][:, :].T

        layers = OrderedDict()
        layers['spliced'] = lc.layer["spliced"][:, :].T
        layers['unspliced'] = lc.layer["unspliced"][:, :].T

        obs = dict(lc.col_attrs)
        obs['obs_names'] = obs.pop('CellID')

        var = dict(lc.row_attrs)
        var['var_names'] = var.pop('Gene')

        adata = AnnData(X, obs=obs, var=var, layers=layers)

    return adata
{'fraction_non_alphanumeric': 0.1278724981467754, 'fraction_numerical': 0.0007412898443291327, 'mean_word_length': 2.5143229166666665, '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': '6866001', 'n_tokens_mistral': 916, 'n_tokens_neox': 882, 'n_words': 215}