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

"""
Copyright [2009-2017] EMBL-European Bioinformatics Institute
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import json

from ..data import Entry

from . import helpers


def as_entry(data, mapping):
    """
    Turn an entry from the JSON file into a Entry object for writing.
    """

    location_range = helpers.feature_location_endpoints(data)
    return Entry(
        primary_id=helpers.primary_id(data),
        accession=helpers.accession(data),
        ncbi_tax_id=helpers.taxid(data),
        database='RFAM',
        sequence=helpers.sequence(data),
        exons=[],
        rna_type=helpers.rna_type(data, mapping),
        url=helpers.url(data),
        note_data=helpers.note(data),
        species=helpers.species(data),
        common_name=helpers.common_name(data),
        lineage=helpers.lineage(data),
        optional_id=helpers.optional_id(data),
        product=helpers.product(data),
        parent_accession=helpers.parent_accession(data),
        project='RFAM',
        experiment=helpers.experiment(data),
        description=helpers.description(data),
        mol_type=helpers.mol_type(data),
        seq_version=helpers.seq_version(data),
        is_composite='N',
        location_start=location_range[0],
        location_end=location_range[1],
        references=helpers.references(),
    )


def parse(filename, mapping):
    """
    Parse the JSON file of Rfam data and produce a generator of all Entry
    objects in the file.
    """

    with open(filename, 'rb') as raw:
        data = json.load(raw)
        for entry in data:
            yield as_entry(entry, mapping)<commit_msg>Add standard parser interface to Rfam<commit_after># -*- coding: utf-8 -*-

"""
Copyright [2009-2017] EMBL-European Bioinformatics Institute
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import json
import itertools as it

from rnacentral_pipeline.writers import build_entry_writer

from ..data import Entry

from . import helpers


def as_entry(data, mapping):
    """
    Turn an entry from the JSON file into a Entry object for writing.
    """

    location_range = helpers.feature_location_endpoints(data)
    return Entry(
        primary_id=helpers.primary_id(data),
        accession=helpers.accession(data),
        ncbi_tax_id=helpers.taxid(data),
        database='RFAM',
        sequence=helpers.sequence(data),
        exons=[],
        rna_type=helpers.rna_type(data, mapping),
        url=helpers.url(data),
        note_data=helpers.note(data),
        species=helpers.species(data),
        common_name=helpers.common_name(data),
        lineage=helpers.lineage(data),
        optional_id=helpers.optional_id(data),
        product=helpers.product(data),
        parent_accession=helpers.parent_accession(data),
        project='RFAM',
        experiment=helpers.experiment(data),
        description=helpers.description(data),
        mol_type=helpers.mol_type(data),
        seq_version=helpers.seq_version(data),
        is_composite='N',
        location_start=location_range[0],
        location_end=location_range[1],
        references=helpers.references(),
    )


def parse(handle, mapping):
    """
    Parse the JSON file of Rfam data and produce a generator of all Entry
    objects in the file.
    """

    data = json.load(handle)
    return it.imap(lambda e: as_entry(e, mapping), data)


def from_file(handle, mapping, output):
    writer = build_entry_writer(parse)
    writer(output, handle, mapping)
{'fraction_non_alphanumeric': 0.09492818671454219, 'fraction_numerical': 0.006732495511669659, 'mean_word_length': 3.3911330049261084, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2225472', 'n_tokens_mistral': 1282, 'n_tokens_neox': 1274, 'n_words': 365}