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

Bash for循环处理多PDB文件时为各文件匹配对应动态变量的方法

Bash流水线定制修改方案

你可以根据使用场景从以下几种实现方式里选,都能实现不同PDB文件自动匹配对应flex_residues参数的需求:

  • 方案1:关联数组映射(推荐,Bash 4.0+版本支持)
    直接在脚本头部用关联数组存储文件名和残基参数的对应关系,维护起来最直观,增删映射只需要改数组键值对即可。
home="$PWD"
receptors="${home}"/receptors

# 在此处维护所有受体文件和对应柔性残基的映射
declare -A flex_residues_map
flex_residues_map["receptor1.pdb"]='MET44_ASN142_CYS145_HIE163_GLN189'
flex_residues_map["receptor2.pdb"]='TRP12_ASN142_GLN188_GLU166_GLN189'
flex_residues_map["receptor3.pdb"]='ALA49_ASN142_MET111_HIS164_GLN189'

for prot in "${receptors}"/*.pdb; do
    filename=$(basename "$prot")
    # 校验当前文件是否有对应配置,避免传空参数运行
    if [[ -z ${flex_residues_map[$filename]} ]]; then
        echo "警告:未找到${filename}对应的flex_residues配置,跳过该文件"
        continue
    fi
    prepare_flexreceptor.py -r "$prot" -s "${flex_residues_map[$filename]}"
done
  • 方案2:case分支匹配(兼容低版本Bash)
    如果运行环境的Bash版本低于4.0不支持关联数组,直接在循环内用case语句匹配文件名赋值对应参数即可,不需要额外语法支持。
home="$PWD"
receptors="${home}"/receptors

for prot in "${receptors}"/*.pdb; do
    filename=$(basename "$prot")
    receptor_name=$(basename "$prot" .pdb)

    case "$filename" in
        "receptor1.pdb")
            flex_residues='MET44_ASN142_CYS145_HIE163_GLN189'
            ;;
        "receptor2.pdb")
            flex_residues='TRP12_ASN142_GLN188_GLU166_GLN189'
            ;;
        "receptor3.pdb")
            flex_residues='ALA49_ASN142_MET111_HIS164_GLN189'
            ;;
        *)
            echo "警告:未找到${filename}对应的flex_residues配置,跳过该文件"
            continue
            ;;
    esac

    prepare_flexreceptor.py -r "${receptors}/${receptor_name}.pdb" -s "$flex_residues"
done
  • 方案3:外置配置文件映射(适合大量映射/频繁改配置的场景)
    如果需要配置的受体文件很多,或者经常要调整残基参数,可以把映射关系单独存在配置文件里,不用修改主流水线脚本就能更新配置。
    首先在脚本同级目录新建flex_residues.conf配置文件,每行写一组映射,文件名和残基值用空格分隔,支持#开头的注释行和空行:
# 格式:pdb文件名 对应柔性残基值
receptor1.pdb MET44_ASN142_CYS145_HIE163_GLN189
receptor2.pdb TRP12_ASN142_GLN188_GLU166_GLN189
receptor3.pdb ALA49_ASN142_MET111_HIS164_GLN189

主流水线脚本修改为读取配置文件加载映射:

home="$PWD"
receptors="${home}"/receptors
config_path="${home}/flex_residues.conf"

declare -A flex_residues_map
# 逐行读取配置加载映射
while read -r pdb_file residues; do
    [[ -z $pdb_file || $pdb_file == \#* ]] && continue
    flex_residues_map["$pdb_file"]="$residues"
done < "$config_path"

for prot in "${receptors}"/*.pdb; do
    filename=$(basename "$prot")
    if [[ -z ${flex_residues_map[$filename]} ]]; then
        echo "警告:未找到${filename}对应的flex_residues配置,跳过该文件"
        continue
    fi
    prepare_flexreceptor.py -r "$prot" -s "${flex_residues_map[$filename]}"
done

补充:如果需要给未单独配置的PDB文件用统一默认残基值,只需要把跳过逻辑里的continue替换为给flex_residues赋默认值即可,不用额外加判断。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 18:54:27