<commit_before>from decimal import Decimal, getcontext

from app.lookups.sqlite import quant as sqlite
from app import formatting
from app.readers import openms as openmsreader
from app.readers import spectra as specreader


def create_quant_lookup(fn_spectra, workdir, consensus_els):
    """Creates an sqlite lookup table of scannrs with quant data.

    spectra - an iterable of tupled (filename, spectra)
    consensus_els - a iterable with consensusElements"""
    quantdb = sqlite.QuantDB()
    quantdb.create_quantdb(workdir)
    quants = {}
    count = 0
    for consensus_el in consensus_els:
        count += 1
        rt = openmsreader.get_consxml_rt(consensus_el)
        fn, spec_scan_nr = get_spec_scan_nr(fn_spectra, rt)
        qdata = get_quant_data(consensus_el)
        try:
            quants[fn][spec_scan_nr] = qdata
        except KeyError:
            quants[fn] = {}
            quants[fn][spec_scan_nr] = qdata
        if count == 5000:
            store_quants(quants, quantdb)
            count = 0
            quants = {}
        formatting.clear_el(consensus_el)
    store_quants(quants, quantdb)
    quantdb.index_quants()
    return quantdb.fn


def get_spec_scan_nr(fn_spectra, cons_rt):
    """Returns mzML filename and scan nr of a spectrum that belongs to the
    retention time in cons_rt, when given a generator of multiple
    spectra files in fn_spectra.
    The generator is a tuple of fn, spectrum, namespace."""
    getcontext().prec = 8  # sets decimal point precision
    cons_rt_dec = Decimal(cons_rt)
    for fn, spectrum, ns in fn_spectra:<commit_msg>Larger precision in retention time comparisons<commit_after>from decimal import Decimal, getcontext

from app.lookups.sqlite import quant as sqlite
from app import formatting
from app.readers import openms as openmsreader
from app.readers import spectra as specreader


def create_quant_lookup(fn_spectra, workdir, consensus_els):
    """Creates an sqlite lookup table of scannrs with quant data.

    spectra - an iterable of tupled (filename, spectra)
    consensus_els - a iterable with consensusElements"""
    quantdb = sqlite.QuantDB()
    quantdb.create_quantdb(workdir)
    quants = {}
    count = 0
    for consensus_el in consensus_els:
        count += 1
        rt = openmsreader.get_consxml_rt(consensus_el)
        fn, spec_scan_nr = get_spec_scan_nr(fn_spectra, rt)
        qdata = get_quant_data(consensus_el)
        try:
            quants[fn][spec_scan_nr] = qdata
        except KeyError:
            quants[fn] = {}
            quants[fn][spec_scan_nr] = qdata
        if count == 5000:
            store_quants(quants, quantdb)
            count = 0
            quants = {}
        formatting.clear_el(consensus_el)
    store_quants(quants, quantdb)
    quantdb.index_quants()
    return quantdb.fn


def get_spec_scan_nr(fn_spectra, cons_rt):
    """Returns mzML filename and scan nr of a spectrum that belongs to the
    retention time in cons_rt, when given a generator of multiple
    spectra files in fn_spectra.
    The generator is a tuple of fn, spectrum, namespace."""
    getcontext().prec = 14 # sets decimal point precision
    cons_rt_dec = Decimal(cons_rt)
    for fn, spectrum, ns in fn_spectra:
{'fraction_non_alphanumeric': 0.07056638811513463, 'fraction_numerical': 0.005261528938409161, 'mean_word_length': 3.024906600249066, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3668207', 'n_tokens_mistral': 1006, 'n_tokens_neox': 979, 'n_words': 300}