You need to enable JavaScript to run this app. 基于Biopython的多字典子串匹配FASTA序列筛选及重复输出问题修复
优惠活动
大模型
产品
解决方案
定价
更多

基于Biopython的多字典子串匹配FASTA序列筛选及重复输出问题修复

重复输出问题修复

原代码问题

原代码把打印逻辑放在4层遍历字典键的嵌套循环里,只要外层循环走到任意一组键的组合满足匹配条件,就会触发一次打印,和当前reads是否已经命中过无关,因此同一符合条件的reads会被重复输出。

优化后代码

from Bio import SeqIO

# 目标子串字典
fw1 = {'PLAU_fw1':'CCCFFF','EPCAM_fw1':'GGGTTT','MIF_fw1':'HHHFFF'}
fw1_rc = {'PLAU_fw1_rc':'cccfff','EPCAM_fw1_rc':'gggttt','MIF_fw1_rc':'hhhfff'}
fw2 = {'PLAU_fw2':'RRREEE','EPCAM_fw2':'OOOPPP','MIF_fw2':'KKKZZZ'}
fw2_rc = {'PLAU_fw2_rc':'rrreee','EPCAM_fw2_rc':'oooppp','MIF_fw2_rc':'kkkzzz'}

count = 0
with open('file.fasta','r') as f:
    for record in SeqIO.parse(f,'fasta'):
        seq = str(record.seq)
        is_matched = False
        output_content = None

        # 校验第一组规则:任意fw1子串 + 任意fw2_rc子串
        hit_fw1 = []
        for key, subseq in fw1.items():
            pos = seq.find(subseq)
            if pos != -1:
                hit_fw1.append((key, subseq, pos + 1))
        
        hit_fw2_rc = []
        for key, subseq in fw2_rc.items():
            pos = seq.find(subseq)
            if pos != -1:
                hit_fw2_rc.append((key, subseq, pos + 1))
        
        if hit_fw1 and hit_fw2_rc:
            is_matched = True
            k1, s1, p1 = hit_fw1[0]
            k2, s2, p2 = hit_fw2_rc[0]
            output_content = [
                "sample_2",
                record.id,
                seq,
                f"{k1} : {s1} - The position is {p1}",
                f"{k2} : {s2} - The position is {p2}"
            ]
        
        # 第一组规则未命中时,校验第二组规则:任意fw1_rc子串 + 任意fw2子串
        if not is_matched:
            hit_fw1_rc = []
            for key, subseq in fw1_rc.items():
                pos = seq.find(subseq)
                if pos != -1:
                    hit_fw1_rc.append((key, subseq, pos + 1))
            
            hit_fw2 = []
            for key, subseq in fw2.items():
                pos = seq.find(subseq)
                if pos != -1:
                    hit_fw2.append((key, subseq, pos + 1))
            
            if hit_fw1_rc and hit_fw2:
                is_matched = True
                k1, s1, p1 = hit_fw1_rc[0]
                k2, s2, p2 = hit_fw2[0]
                output_content = [
                    "sample_2",
                    record.id,
                    seq,
                    f"{k1} : {s1} - The position is {p1}",
                    f"{k2} : {s2} - The position is {p2}"
                ]
        
        # 单条reads仅输出一次,计数仅在确认命中后累加
        if is_matched:
            print('\n'.join(output_content))
            print('\n')
            count += 1
    
    print("The total number of reads that have both 21nt protein-specific sequences is " + str(count))

核心调整点

  • 移除冗余的4层嵌套循环,单条reads的匹配校验从最多81次遍历降至最多12次,运行效率提升6倍以上
  • 对每条reads先完成全量匹配判断,统一存储输出内容,只有校验完成且确认命中后才触发打印,彻底避免重复输出
  • 匹配逻辑无顺序限制,只要两组规则中的任意一组命中即筛选,完全符合需求
  • 计数逻辑与输出逻辑绑定,最终统计值准确

内容的提问来源于stack exchange,提问作者Apex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.15 16:15:06