<jupyter_start><jupyter_text>读取文件的类型 ：读取名字和对应的序列>hsa-let-7a-5p MIMAT0000062 Homo sapiens let-7a-5pUGAGGUAGUAGGUUGUAUAGUU>hsa-let-7a-3p MIMAT0004481 Homo sapiens let-7a-3pCUAUACAAUCUACUGUCUUUC<jupyter_code>def load_sequence(file):
    with open(file, 'r') as f:
        lines = f.readlines()
    data = [line for line in lines if line != '\n']
    seq_line = data[1::2]#
    name_lines = data[0::2]
    idx = [i for i in range(len(seq_lines)) if len(seq_lines[i]) < 25]
    seq = [seq_lines[i].strip() for i in idx] # .strip() : remove spaces
    name = [name_lines[i].strip().split()[-1] for i in idx]
    return seq, name<jupyter_output><empty_output><jupyter_text>输入：文件路径""  输出：序列seq[""],名称name[""]  - 把 sequence和名称一行一行的读取出来 - 添加index（序列最长定为25） - 文件中会有空行，去除空行   读取文件的类型 ： 读取名字和对应结合度，选取最高的结合度excel 表格 sheet “result”miR-4755-3p.pdb	3	-7.6miR-8055.pdb	3	-7.6<jupyter_code>def load_affinity(file):
    # 读取文件
    sheet = pds.read_excel(file, sheet_name='result')
    # 选取结合度所在的行
    affinity_t = sheet.value[:, 2]#不懂
    affinity_t = [a for a in affinity_t]
    # 读取名字所在的行
    name_t = sheet.values[:, 0]#不懂
    name_t = [n.split('.')[0] for n in name_t]#分割开“.”之前的元素并读取
    affi = []#最后返回的结合度
    name = []#最后返回的名字
    idx_same = []
    for i,n in enumerate(name_t):
        #如果该名字从未出现过，将其加入到返回的名字中，并找到最大的结合度
        if i not in idx_same:
            affi_t = [affinity_t[i]]
            
            # 遍历之后的每个名字，将所有的结合度比较，并将重复名字的下标添加到idx_same中，之后便不再遍历
            for j in range(i + 1, len(name_t)):
                if name_t[j] == n :
                    affi_t.append(affinity_t[j])
                    idx_sa,e.append(j)
            affi.append(sorted(affi_t)[0])
            name.append(n)
        else:
            continue
    return affi,name<jupyter_output><empty_output><jupyter_text>输入：文件路径"" 输出：结合度affi[float],名称name[""]，根据下标一一对应- 分别读取结合度所在的行和名字所在的行 返回到列表- 选取最高的结合度 enumerate  与上一函数一样，结合度取平均值<jupyter_code>def load_affinity_mean(file):
    sheet = pds.read_excel(file, sheet_name = 'result')
    affinity_t = sheet.values[:, 2]
    affinity_t = [a for a in affinity_t]
    name_t = sheet.values[:, 0]
    name_t = [n.split('.')[0] for n in name_t]
    num = sheet.values[:, 1]
    num = [n for n in num]
    affi = []
    name = []
    idx_same = []
    for i, n in enumerate(name_t):
        if i not in idx_same:
            affi_t = [affinity_t[i]]
            num_t = num[i]
            for j in range(i + 1, len(name_t)):
                if name_t[j] == n:
                    affi_t.append(affinity_t[j])
                    idx_same.append(j)
                    num_t += num[j]
            affi.append(sum(affi_t)/num_t)
            name.append(n)
        else:
            continue
    return affi, name<jupyter_output><empty_output><jupyter_text>读取结合度<jupyter_code>def load_affinity_rich(file):
    affinity_t = sheet.values[:, 2]
    affinity_t = [a for a in affinity_t]
    name_t = sheet.values[:, 0]
    name_t = [n.split('.')[0] for n in name_t]
    max_affi = max(affinity_t)
    min_affi = min(affinity_t)
    affi = []
    name = []
    idx_same = []
    for i, n in enumerate(name_t):
        if i not in idx_name:
            affi_t = [affinity_t[i]]
            for j in range(i + 1, len(name_t)):
                if name_t[j] == n:
                    affi_t.append(affinity_t[j])
                    idx_same.append(j)
            affi_sorted = sorted(affi_t)
            if (affi_sorted[0]-min_affi)/(max_affi-min_affi) > 0.75:
                for _ in range(6):
                    for af in affi_sorted:
                        affi.append(af)
                        name.append(n)
            elif (affi_sorted[0] - min_affi) / (max_affi - min_affi) < 0.25:
                for _ in range(10):
                    for af in affi_sorted:
                        affi.append(af)
                        name.append(n)
            elif (affi_sorted[0]-min_affi)/(max_affi-min_affi) > 0.6:
                for _ in range(2):
                    for af in affi_sorted:
                        affi.append(af)
                        name.append(n)
            elif (affi_sorted[0] - min_affi) / (max_affi - min_affi) < 0.4:
                for _ in range(2):
                    for af in affi_sorted:
                        affi.append(af)
                        name.append(n)
            else:
                for af in affi_sorted:
                    affi.append(af)
                    name.append(n)
        else:
            continue
    return affi, name<jupyter_output><empty_output><jupyter_text>因为结合度呈正态分布，中间结合度的样本会比两边的概率大很多    导致训练神经网络时，会根据概率更趋于平均，所以要将结合度按正态分布饿概率进行扩充    使得其概率分布趋于均匀分布    记得在记录结合度时要同时记录名字，这样两个列表长度才一样能够一一对应<jupyter_code>def load_affinity_smooth(file):
    """
        读取结合度，但是因为结合度呈正态分布，中间结合度的样本会比两边的概率大很多
        导致训练神经网络时，会根据概率更趋于平均，所以要将结合度按正态分布饿概率进行扩充
        使得其概率分布趋于均匀分布
        记得在记录结合度时要同时记录名字，这样两个列表长度才一样能够一一对应
        enrich的方式不一样
        """
    sheet = pds.read_excel(file, sheet_name='result')
    affinity_t = sheet.values[:, 2]
    affinity_t = [a for a in affinity_t]
    name_t = sheet.values[:, 0]
    name_t = [n.split('.')[0] for n in name_t]
    max_affi = max(affinity_t)
    min_affi = min(affinity_t)
    affi = []
    name = []
    idx_same = []
    for i, n in enumerate(name_t):
        if i not in idx_same:
            affi_t = [affinity_t[i]]
            for j in range(i + 1, len(name_t)):
                if name_t[j] == n:
                    affi_t.append(affinity_t[j])
                    idx_same.append(j)
            affi_sorted = sorted(affi_t)
            if ((affi_sorted[0]-min_affi)/(max_affi-min_affi) > 0.7) | \
                    ((affi_sorted[0]-min_affi)/(max_affi-min_affi) < 0.35):
                for af in affi_sorted:
                    affi.append(af)
                    name.append(n)
            elif ((affi_sorted[0]-min_affi)/(max_affi-min_affi) > 0.55) | \
                    ((affi_sorted[0]-min_affi)/(max_affi-min_affi) < 0.45):
                for af in affi_sorted[:len(affi_sorted) // 2]:
                    affi.append(af)
                    name.append(n)
            else:
                affi.append(sorted(affi_t)[0])
                name.append(n)
        else:
            continue
    return affi, name
def cross_search_smooth(seq, name_seq, affi, name_affi):
    """
    和上一函数一样，只是根据enrich过的结果生成对应关系
    """
    seq_res = []
    affi_res = []
    name_res = []
    name_t = [n for n in name_seq if n in name_affi]
    for name in name_t:
        idx = [i for i, v in enumerate(name_affi) if v == name]
        for i in idx:
            name_res.append(name)
            seq_res.append(seq[name_seq.index(name)])
            affi_res.append(affi[i])

    return seq_res, affi_res, name_res
def load_mi(seq_file, affi_file):
    affi, name_affi = load_affinity(affi_file)
    seq, name_seq = load_sequence(seq_file)
    seq_res, affi_res, name_res = cross_search(seq, name_seq, affi, name_affi)
    return seq_res, affi_res, name_res


def load_mi_mean(seq_file, affi_file):
    affi, name_affi = load_affinity_mean(affi_file)
    seq, name_seq = load_sequence(seq_file)
    seq_res, affi_res, name_res = cross_search(seq, name_seq, affi, name_affi)
    return seq_res, affi_res, name_res


def load_mi_smooth(seq_file, affi_file):
    affi, name_affi = load_affinity_smooth(affi_file)
    seq, name_seq = load_sequence(seq_file)
    seq_res, affi_res, name_res = cross_search_smooth(seq, name_seq, affi, name_affi)
    return seq_res, affi_res, name_res


def load_mi_rich(seq_file, affi_file):
    affi, name_affi = load_affinity_rich(affi_file)
    seq, name_seq = load_sequence(seq_file)
    seq_res, affi_res, name_res = cross_search_smooth(seq, name_seq, affi, name_affi)
    return seq_res, affi_res, name_res


def load_pi(addr):
    with open(addr, 'r') as f:
        lines = f.readlines()
    data = [line for line in lines if line != '\n']
    seq = data[1::2]
    seq = [s.strip() for s in seq]
    name = data[0::2]
    name = [s.strip().split('|')[0] for s in name]
    name_pi = []
    for n in name:
        if n not in name_pi:
            name_pi.append(n)
    seq_pi = [seq[name.index(n)] for n in name_pi]
    return seq_pi, name_pi


def check(seq, affi, name):
    with open('homo-miRNA.txt', 'r') as f:
        lines = f.readlines()
    data = [line for line in lines if line != '\n']
    seq_lines = data[1::2]
    name_lines = data[0::2]
    sheet = pds.read_excel('original.xls', sheet_name='result')
    affinity_t = sheet.values[:, 2]
    affinity_t = [a for a in affinity_t]
    name_t = sheet.values[:, 0]
    name_t = [n.split('.')[0] for n in name_t]
    if (affi == affinity_t[name_t.index(name+'.pdb')]) & (seq == seq_lines[name_lines.index(name)]):
        return True
    else:
        return False
grades = [70, 100, 97, 70, 85, 100, 400, 200, 32]
grades.index(100)
grades.index(100,3,8)#搜素索引3到索引8
seq<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12108629783413759, 'fraction_numerical': 0.016159802491302885, 'mean_word_length': 2.182857142857143, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 27, '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}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '429340', 'license': 'MIT', 'n_tokens_mistral': 3845, 'n_tokens_neox': 3788, 'n_words': 663, 'path': 'DataPreprocess.ipynb', 'repo_name': 'Margery0011/The-research-on-the-Triptolide-nucleic-acid-sequence-target-prediction-model-', 'size': '8911'}