You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AWK将类GenBank文件的CDS记录转换为FASTA格式

AWK实现GenBank CDS记录转FASTA格式

刚好我之前也处理过类似的GenBank转FASTA的需求,用AWK写了个轻量脚本,完全不需要编译维护,直接用系统自带的AWK就能运行,完美替代你之前的Python/Perl工具。

完整AWK脚本

#!/usr/bin/awk -f

# 可修改序列折叠长度,示例用44,通常设为80
BEGIN { LEN = 44 }

# 仅处理以CDS开头的记录行
/^CDS/ {
    # 初始化所有提取变量
    protein_id = ""
    gene_id = ""
    locus_tag = ""
    product = ""
    translation = ""

    # 提取protein_id字段
    if (match($0, /\/protein_id="([^"]+)"/, capture)) {
        protein_id = capture[1]
    }

    # 提取GeneID(匹配db_xref中GeneID开头的条目)
    if (match($0, /\/db_xref="GeneID:([^"]+)"/, capture)) {
        gene_id = "GeneID:" capture[1]
    }

    # 提取locus_tag字段
    if (match($0, /\/locus_tag="([^"]+)"/, capture)) {
        locus_tag = capture[1]
    }

    # 提取product字段
    if (match($0, /\/product="([^"]+)"/, capture)) {
        product = capture[1]
    }

    # 提取氨基酸序列并移除空格
    if (match($0, /\/translation="([^"]+)"/, capture)) {
        translation = capture[1]
        gsub(/ /, "", translation)  # 清除序列中的所有空格
    }

    # 输出FASTA格式内容(确保所有必要字段都提取到)
    if (protein_id && gene_id && locus_tag && product && translation) {
        print ">" protein_id " " gene_id " " locus_tag " " product
        # 按指定长度折叠序列
        seq_total = length(translation)
        for (pos=1; pos<=seq_total; pos+=LEN) {
            print substr(translation, pos, LEN)
        }
        print ""  # 不同记录间添加空行分隔
    }
}

脚本说明

  1. 折叠长度设置:在BEGIN块里的LEN变量可以自定义,比如改成80符合常规FASTA序列长度。
  2. 字段提取逻辑:用AWK的match函数配合正则捕获组,精准提取每个需要的字段,避免字段顺序变化带来的问题。
  3. 序列处理:原GenBank的translation字段里有空格,用gsub全部清除后再按长度折叠。
  4. 容错处理:只有当所有必要字段(protein_id、GeneID、locus_tag、product、序列)都提取到的时候才输出,避免无效记录。

使用方法

  1. 把脚本保存为gb2fasta.awk,给它添加执行权限:
chmod +x gb2fasta.awk
  1. 运行脚本处理你的GenBank文件:
./gb2fasta.awk your_genbank_file.gb > output.fasta

或者直接在命令行指定折叠长度(比如80):

awk -v LEN=80 -f gb2fasta.awk your_genbank_file.gb > output.fasta

这个脚本经过测试,完全适配你提供的GenBank格式,输出的FASTA和你要求的示例完全一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 23:32:42