<commit_before>from app.dataformats import peptable as peptabledata
from app.dataformats import prottable as prottabledata
from app.readers import tsv as reader
from app.actions.peptable.base import evaluate_peptide
from math import log


def generate_proteins(pepfn, proteins, pepheader, scorecol, minlog,
                      higherbetter=True, protcol=False):
    """Best peptide for each protein in a table"""
    protein_peptides = {}
    if minlog:
        higherbetter = False
    if not protcol:
        protcol = peptabledata.HEADER_MASTERPROTEINS
    for psm in reader.generate_tsv_psms(pepfn, pepheader):
        p_acc = psm[protcol]
        if ';' in p_acc:
            continue
        protein_peptides = evaluate_peptide(protein_peptides, psm, p_acc,
                                            higherbetter, scorecol,
                                            fncol=False)
    if minlog:
        nextbestscore = min([pep['score'] for pep in protein_peptides.values()
                             if pep['score'] > 0])
        nextbestscore = -log(nextbestscore, 10)
    for protein in proteins:
        peptide = protein_peptides[protein[prottabledata.HEADER_PROTEIN]]
        if minlog:
            peptide['score'] = log_score(peptide['score'], nextbestscore)
        protein[prottabledata.HEADER_QSCORE] = str(
            peptide['score'])<commit_msg>Added error handling in case wrong peptide table is used when running bestpeptide on prottable<commit_after>from app.dataformats import peptable as peptabledata
from app.dataformats import prottable as prottabledata
from app.readers import tsv as reader
from app.actions.peptable.base import evaluate_peptide
from math import log


def generate_proteins(pepfn, proteins, pepheader, scorecol, minlog,
                      higherbetter=True, protcol=False):
    """Best peptide for each protein in a table"""
    protein_peptides = {}
    if minlog:
        higherbetter = False
    if not protcol:
        protcol = peptabledata.HEADER_MASTERPROTEINS
    for psm in reader.generate_tsv_psms(pepfn, pepheader):
        p_acc = psm[protcol]
        if ';' in p_acc:
            continue
        protein_peptides = evaluate_peptide(protein_peptides, psm, p_acc,
                                            higherbetter, scorecol,
                                            fncol=False)
    if minlog:
        nextbestscore = min([pep['score'] for pep in protein_peptides.values()
                             if pep['score'] > 0])
        nextbestscore = -log(nextbestscore, 10)
    for protein in proteins:
        try:
            peptide = protein_peptides[protein[prottabledata.HEADER_PROTEIN]]
        except KeyError:
            print('WARNING - protein {} not found in peptide '
                  'table'.format(protein[prottabledata.HEADER_PROTEIN]))
        if minlog:
            peptide['score'] = log_score(peptide['score'], nextbestscore)
        protein[prottabledata.HEADER_QSCORE] = str(
            peptide['score'])
{'fraction_non_alphanumeric': 0.07753743760399334, 'fraction_numerical': 0.0019966722129783694, 'mean_word_length': 2.358659217877095, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5432625', 'n_tokens_mistral': 893, 'n_tokens_neox': 820, 'n_words': 219}