Julia可以用变量构造语句嘛

我希望通过字符串操作构造一个用于BioStructures中collectresidues函数的选择表达式:

这个函数是一个操作生物结构的选取器,其基本用法是:

collectresidues(element, res -> min <= resnumber(res) <= max, allselector)

我希望通过构造表达式,来取代中间res ->的部分以实现多重选择:

domain_str = collectresidues(chain, res -> ("$(domain_selection)"), allselector)

首先,我在终端中确认了构建出来的选择表达式确实可以用于collectresidues函数:

随后,我尝试在我的脚本中构建这一函数:

#         domain_vec是一个存储语句中各个点的向量
        for domain_range in split(domain_vec,",")
            frag_N = parse(Int64,split(domain_range,"-")[1])
            frag_C = parse(Int64,split(domain_range,"-")[2])
            ( length(domain_selection) == 0 ) && (domain_selection = "($frag_N <= resnumber(res) <= $frag_C)" )
            ( length(domain_selection) == 0 ) || (domain_selection = "$(domain_selection) || ( $frag_N <= resnumber(res) <= $frag_C)" )
        end
        println(domain_selection)
# 这一句输出的结果是:(13 <= resnumber(res) <= 436) || ( 13 <= resnumber(res) <= 436) || ( 504 <= resnumber(res) <= 534) || ( 655 <= resnumber(res) <= 682)
        domain_str = collectresidues(chain, res -> ("$(domain_selection)"), allselector)
        writepdb("$output_dir/$pdb_name-$model_num-$chain_id-$domain_id.pdb", domain_str)

然而,程序报错:

请问各位大佬,这是什么原因?如何在Julia中正确使用变量构造语句?

Meta.parse("res -> (\"$(domain_selection)\")") |> eval

BioStructures的开发者给出了另外一种解决方案:

Yes you can do it as you have by using || in the selector. You can merge selections by defining them as functions:

selector1(res) = 13  <= resnumber(res) <= 436
selector2(res) = 504 <= resnumber(res) <= 534
combinedselector(res) = selector1(res) || selector2(res)
domain = collectresidues(chain, combinedselector)

I deleted the allselector as it doesn’t do anything here.

感谢大佬的意见,最终我使用BioStructures开发者的建议解决了这一问题

for domain_ddd in readlines("$index_dir/$pdb_name-$model_num-$chain_id-domain.info")
    domain_id = split(domain_ddd,":")[1]
    domain_vec = split(domain_ddd,":")[2]
    com_id = 0
    domain_selection = []
    for domain_range in split(domain_vec,",")
        frag_N = parse(Int64,split(domain_range,"-")[1])
        frag_C = parse(Int64,split(domain_range,"-")[2])
        for resi in frag_N:frag_C
            push!(domain_selection, resi)
        end
    end
    domain_str = collectresidues(chain, res -> resnumber(res) in domain_selection , allselector)
    writepdb("$output_dir/$pdb_name-$model_num-$chain_id-$domain_id.pdb", domain_str)
end    

将残基编号存入向量domain_selection,随后在collectresidues中直接使用这一向量来指定残基,这样可以跳过复杂的选区合并操作!

备案号:京ICP备17009874号-2