<commit_before>from Bio import SeqIO
from app import sqlite


def create_searchspace(dbfns):
    """Given FASTA databases, proteins are trypsinized and resulting peptides
    stored in a database or dict for lookups"""
    lookup = sqlite.SearchSpaceDB()
    lookup.create_searchspacedb()

    for dbfn in dbfns:
        allpeps = []
        protindex = SeqIO.index(dbfn, 'fasta')
        for acc in protindex:
            pepseqs = trypsinize(protindex[acc].seq)
            # Exchange all leucines to isoleucines because MS can't differ
            pepseqs = [(str(pep).replace('L', 'I'),) for pep in pepseqs]
            allpeps.extend(pepseqs)
            if len(allpeps) > 1000000:  # more than x peps, write to SQLite
                lookup.write_peps(allpeps)
                allpeps = []
        # write remaining peps to sqlite
        lookup.write_peps(allpeps)
    lookup.index_peps()
    lookup.close_connection()
    return lookup.fn


def trypsinize(proseq):
    """Trypsinize a sequence. From Yafeng Zhu."""
    indice = [0]
    peptides = []
    for i in range(0, len(proseq) - 1):
        if proseq[i] == 'K' and proseq[i + 1] != 'P':
            indice.append(i + 1)
        elif proseq[i] == 'R' and proseq[i + 1] != 'P':
            indice.append(i + 1)

    indice.append(-1)
    for j in range(0, len(indice) - 1):
        if indice[j] + 1 == indice[j + 1]:
            peptides.append(proseq[indice[j]:indice[j + 2]])
            peptides.append(proseq[indice[j - 1]:indice[j + 1]])
        else:
            peptides.append(proseq[indice[j]:indice[j + 1]])
    return peptides<commit_msg>Trypsinaztion bug did not include last amino acid of proteins<commit_after>from Bio import SeqIO
from app import sqlite


def create_searchspace(dbfns):
    """Given FASTA databases, proteins are trypsinized and resulting peptides
    stored in a database or dict for lookups"""
    lookup = sqlite.SearchSpaceDB()
    lookup.create_searchspacedb()

    for dbfn in dbfns:
        allpeps = []
        protindex = SeqIO.index(dbfn, 'fasta')
        for acc in protindex:
            pepseqs = trypsinize(protindex[acc].seq)
            # Exchange all leucines to isoleucines because MS can't differ
            pepseqs = [(str(pep).replace('L', 'I'),) for pep in pepseqs]
            allpeps.extend(pepseqs)
            if len(allpeps) > 1000000:  # more than x peps, write to SQLite
                lookup.write_peps(allpeps)
                allpeps = []
        # write remaining peps to sqlite
        lookup.write_peps(allpeps)
    lookup.index_peps()
    lookup.close_connection()
    return lookup.fn


def trypsinize(proseq):
    """Trypsinize a sequence. From <NAME>. Returns fully
    tryptic, and overlapping peptides if there are a sequence of
    tryptic residues"""
    indices = [0]
    peptides = []
    for aa, i in enumerate(range(0, len(proseq) - 1)):
        nextaa = proseq[i + 1]
        if aa in ['K', 'P'] and nextaa != 'P':
            indices.append(i + 1)

    indices.append(-1)
    for j in range(0, len(indices) - 1):
        if indices[j] + 1 == indices[j + 1]:
            peptides.append(proseq[indices[j]:indices[j + 2]])
            peptides.append(proseq[indices[j - 1]:indices[j + 1]])
        elif indices[j + 1] == -1:
            peptides.append(proseq[indices[j]:])
        else:
            peptides.append(proseq[indices[j]:indices[j + 1]])
    return peptides
{'fraction_non_alphanumeric': 0.11687170474516696, 'fraction_numerical': 0.013473930872876391, 'mean_word_length': 2.3845391476709614, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, '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': '6477046', 'n_tokens_mistral': 1206, 'n_tokens_neox': 1103, 'n_words': 302}