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

"""
Copyright [2009-2018] 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 operator as op
import itertools as it

import attr
from attr.validators import optional
from attr.validators import instance_of as is_a

from .description import description_of

from .rna_type import rna_type_of


def database_names(accessions):
    names = {acc.pretty_database for acc in accessions}
    return ','.join(sorted(names))


def partioned_accessions(all_accessions):
    accessions = []
    inactive_accessions = []
    for accession in all_accessions:
        if accession.pop('is_active'):
            accessions.append(Accession.build(accession))
        else:
            inactive_accessions.append(Accession.build(accession))
    return accessions, inactive_accessions


@attr.s()
class Accession(object):
    """
    This represents the minimal amount of information we need to produce a good
    name from the accession level data for a sequence.
    """

    gene = attr.ib(validator=optional(is_a(basestring)))
    optional_id = attr.ib(validator=optional(is_a(basestring)))
    pretty_database = attr.ib(validator=is_a(basestring))
    feature_name = attr.ib(validator=is_a(basestring))
    ncrna_class = attr.ib(validator=optional(is_a(basestring)))
    species = attr.ib(validator=optional(is_a(basestring)))
    common_name = attr.ib(validator=optional(is_a(basestring)))
    description = attr.ib(validator=is_a(basestring))
    locus_tag = attr.ib(validator=optional(is_a(basestring)))

    @classmethod
    def build(cls, data):
        """
        Create a new Accession from the given dict. This assumes the dict has
        keys with the same names as accession fields.
        """
        return cls(**data)  # pylint: disable=star-args

    @property
    def database(self):
        """
        The normalized (lowercase) database name.
        """
        return self.pretty_database.lower()  # pylint: disable=no-member

    @property
    def rna_type(self):
        """
        Get a single INSDC RNA type for this accession.
        """

        if self.feature_name == 'ncRNA':
            return self.ncrna_class
        return self.feature_name


@attr.s()
class Sequence(object):
    upi = attr.ib(validator=is_a(basestring))
    taxid = attr.ib(validator=optional(is_a(int)))
    length = attr.ib(validator=is_a(int))
    accessions = attr.ib(validator=is_a(list))
    inactive_accessions = attr.ib(validator=is_a(list))
    is_active = attr.ib(validator=is_a(bool))
    xref_has_coordinates = attr.ib(validator=is_a(bool))
    rna_was_mapped = attr.ib(validator=is_a(bool))
    previous_data = attr.ib(validator=is_a(dict))

    def is_species_specific(self):
        return self.taxid is not None


@attr.s()
class SpeciesSequence(Sequence):
    """
    This represents what data is needed in order to precompute the data for a
    species specific sequence.
    """

    @classmethod
    def build(cls, data):
        """
        Given a dictonary of result from the precompute query this will build a
        SpeciesSequence.
        """

        active, inactive = partioned_accessions(data['accessions'])
        return cls(
            upi=data['upi'],
            taxid=data['taxid'],
            length=data['length'],
            accessions=active,
            inactive_accessions=inactive,
            is_active=any(not d for d in data['deleted']),
            xref_has_coordinates=any(data['xref_has_coordinates']),
            rna_was_mapped=data['rna_was_mapped'],
            previous_data=data['previous'][0],
        )

@attr.s()
class GenericSequence(Sequence):

    @classmethod
    def build(cls, sequences):
        """
        Build a Seqeunce object which represents the generic sequence (no
        assigned taxid) given a list of species specific sequences (have an
        assigend taxid).
        """

        inactive = []
        accessions = []
        for seq in sequences:
            accessions.extend(seq.accessions)
            inactive.extend(seq.inactive_accessions)

        has_coordinates = any(s['xref_has_coordinates'] for s in sequences)
        return cls(
            upi=sequences[0].upi,
            taxid=None,<commit_msg>Correct generation of GenericSequences<commit_after># -*- coding: utf-8 -*-

"""
Copyright [2009-2018] 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 operator as op
import itertools as it

import attr
from attr.validators import optional
from attr.validators import instance_of as is_a

from .description import description_of

from .rna_type import rna_type_of


def database_names(accessions):
    names = {acc.pretty_database for acc in accessions}
    return ','.join(sorted(names))


def partioned_accessions(all_accessions):
    accessions = []
    inactive_accessions = []
    for accession in all_accessions:
        if accession.pop('is_active'):
            accessions.append(Accession.build(accession))
        else:
            inactive_accessions.append(Accession.build(accession))
    return accessions, inactive_accessions


@attr.s()
class Accession(object):
    """
    This represents the minimal amount of information we need to produce a good
    name from the accession level data for a sequence.
    """

    gene = attr.ib(validator=optional(is_a(basestring)))
    optional_id = attr.ib(validator=optional(is_a(basestring)))
    pretty_database = attr.ib(validator=is_a(basestring))
    feature_name = attr.ib(validator=is_a(basestring))
    ncrna_class = attr.ib(validator=optional(is_a(basestring)))
    species = attr.ib(validator=optional(is_a(basestring)))
    common_name = attr.ib(validator=optional(is_a(basestring)))
    description = attr.ib(validator=is_a(basestring))
    locus_tag = attr.ib(validator=optional(is_a(basestring)))

    @classmethod
    def build(cls, data):
        """
        Create a new Accession from the given dict. This assumes the dict has
        keys with the same names as accession fields.
        """
        return cls(**data)  # pylint: disable=star-args

    @property
    def database(self):
        """
        The normalized (lowercase) database name.
        """
        return self.pretty_database.lower()  # pylint: disable=no-member

    @property
    def rna_type(self):
        """
        Get a single INSDC RNA type for this accession.
        """

        if self.feature_name == 'ncRNA':
            return self.ncrna_class
        return self.feature_name


@attr.s()
class Sequence(object):
    upi = attr.ib(validator=is_a(basestring))
    taxid = attr.ib(validator=optional(is_a(int)))
    length = attr.ib(validator=is_a(int))
    accessions = attr.ib(validator=is_a(list))
    inactive_accessions = attr.ib(validator=is_a(list))
    is_active = attr.ib(validator=is_a(bool))
    xref_has_coordinates = attr.ib(validator=is_a(bool))
    rna_was_mapped = attr.ib(validator=is_a(bool))
    previous_data = attr.ib(validator=is_a(dict))

    def is_species_specific(self):
        return self.taxid is not None


@attr.s()
class SpeciesSequence(Sequence):
    """
    This represents what data is needed in order to precompute the data for a
    species specific sequence.
    """

    @classmethod
    def build(cls, data):
        """
        Given a dictonary of result from the precompute query this will build a
        SpeciesSequence.
        """

        active, inactive = partioned_accessions(data['accessions'])
        return cls(
            upi=data['upi'],
            taxid=data['taxid'],
            length=data['length'],
            accessions=active,
            inactive_accessions=inactive,
            is_active=any(not d for d in data['deleted']),
            xref_has_coordinates=any(data['xref_has_coordinates']),
            rna_was_mapped=data['rna_was_mapped'],
            previous_data=data['previous'][0],
        )

@attr.s()
class GenericSequence(Sequence):

    @classmethod
    def build(cls, sequences):
        """
        Build a Seqeunce object which represents the generic sequence (no
        assigned taxid) given a list of species specific sequences (have an
        assigend taxid).
        """

        inactive = []
        accessions = []
        for seq in sequences:
            accessions.extend(seq.accessions)
            inactive.extend(seq.inactive_accessions)

        has_coordinates = any(s.xref_has_coordinates for s in sequences)
        return cls(
            upi=sequences[0].upi,
            taxid=None,
{'fraction_non_alphanumeric': 0.0967436974789916, 'fraction_numerical': 0.0031512605042016808, 'mean_word_length': 3.081011573081869, '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': '6031157', 'n_tokens_mistral': 2846, 'n_tokens_neox': 2796, 'n_words': 754}