<commit_before>from models import Protein
from models import Mutation
from database import bdb, bdb_refseq
from database import make_snv_key
from database import decode_csv
from database import get_or_create
from helpers.bioinf import decode_raw_mutation
from operator import attrgetter


def get_genomic_muts(chrom, dna_pos, dna_ref, dna_alt):
    snv = make_snv_key(chrom, dna_pos, dna_ref, dna_alt)

    items = [
        decode_csv(item)
        for item in bdb[snv]
    ]

    # this could be speed up by: itemgetters, accumulative queries and so on

    for item in items:

        protein = Protein.query.get(item['protein_id'])
        item['protein'] = protein

        mutation, created = get_or_create(
            Mutation,
            protein_id=protein.id,
            position=item['pos'],
            alt=item['alt']
        )
        item['mutation'] = mutation
        item['type'] = 'genomic'
    return items


def get_affected_isoforms(gene_name, ref, pos, alt):
    """Returns all isoforms where specified mutation might happen.

    Explanation: shouldn't we look for refseq with gene name only?

    Well, we have to look for all isoforms of given gene which
    cover given mutation - so those with length Y: X <= Y, where
    X is the position (pos) of analysed mutation.

    There are many such isoforms and simple lookup:
        gene_name => preferred_isoform, or
        gene_name => all_isoforms
    is not enough to satisfy all conditions.

    So what do we have here is (rougly) an equivalent to:

        from models import Gene

        # the function below should check if we don't have symbols
        # that are not representing any of known amino acids.

        is_mut_allowed(alt)

        gene = Gene.query.filter_by(name=gene_name).one()
        return [
            isoform
            for isoform in gene.isoforms
            if (isoform.length >= pos and
                isoform.sequence[pos - 1] == ref)
        ]<commit_msg>Explain amino acid change constraint in docstring<commit_after>from models import Protein
from models import Mutation
from database import bdb, bdb_refseq
from database import make_snv_key
from database import decode_csv
from database import get_or_create
from helpers.bioinf import decode_raw_mutation
from operator import attrgetter


def get_genomic_muts(chrom, dna_pos, dna_ref, dna_alt):
    snv = make_snv_key(chrom, dna_pos, dna_ref, dna_alt)

    items = [
        decode_csv(item)
        for item in bdb[snv]
    ]

    # this could be speed up by: itemgetters, accumulative queries and so on

    for item in items:

        protein = Protein.query.get(item['protein_id'])
        item['protein'] = protein

        mutation, created = get_or_create(
            Mutation,
            protein_id=protein.id,
            position=item['pos'],
            alt=item['alt']
        )
        item['mutation'] = mutation
        item['type'] = 'genomic'
    return items


def get_affected_isoforms(gene_name, ref, pos, alt):
    """Returns all isoforms where specified mutation might happen.

    Explanation: shouldn't we look for refseq with gene name only?

    Well, we have to look for all isoforms of given gene which
    cover given mutation - so those with length Y: X <= Y, where
    X is the position (pos) of analysed mutation.

    There is on more constraint: some proteomic mutations cannot
    be caused by a single genomic mutations: F => S cannot be a
    result of a single SNV/SNP because neither UUU nor UUC could be
    changed to AGU or AGC in a single step.

    There are many such isoforms and simple lookup:
        gene_name => preferred_isoform, or
        gene_name => all_isoforms
    is not enough to satisfy all conditions.

    So what do we have here is (rougly) an equivalent to:

        from models import Gene

        # the function below should check if we don't have symbols
        # that are not representing any of known amino acids.

        is_mut_allowed(alt)

        # function above and below were not implemented but lets
        # assume that they throw a flow-changing exception

        can_be_result_of_single_snv(ref, alt)

        gene = Gene.query.filter_by(name=gene_name).one()
        return [
            isoform
            for isoform in gene.isoforms
            if (isoform.length >= pos and
                isoform.sequence[pos - 1] == ref)
        ]
{'fraction_non_alphanumeric': 0.06366530241018645, 'fraction_numerical': 0.00045475216007276033, 'mean_word_length': 2.599836333878887, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, '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': '4594390', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1204, 'n_words': 480}