如何使用sys.args[]或getopts通过Python运行对齐命令

2024-06-08 15:31:22 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用sys.args[]或getopts通过python运行“MAFFT”对齐

我希望通过python自动化对齐,这样就不必对不同的文件多次使用独立的命令行MAFFT。这可能会非常耗时

理想情况下,我希望有一个函数可以做到这一点,这样就可以将不同的文件输入到这个函数中,以便进行MAFFT对齐

干杯


Tags: 文件函数命令行sys情况args理想耗时
1条回答
网友
1楼 · 发布于 2024-06-08 15:31:22

BioPython has a command line wrapper for the multiple alignment program MAFFT

>>> from Bio.Align.Applications import MafftCommandline
>>> mafft_exe = "/opt/local/mafft"
>>> in_file = "../Doc/examples/opuntia.fasta"
>>> mafft_cline = MafftCommandline(mafft_exe, input=in_file)
>>> print(mafft_cline)
/opt/local/mafft ../Doc/examples/opuntia.fasta

如果mafft二进制文件位于路径上(通常在Unix风格的操作系统上),则不需要提供可执行文件位置:

>>> from Bio.Align.Applications import MafftCommandline
>>> in_file = "../Doc/examples/opuntia.fasta"
>>> mafft_cline = MafftCommandline(input=in_file)
>>> print(mafft_cline)
mafft ../Doc/examples/opuntia.fasta

请注意,MAFFT会将对齐写入标准输出,您可能希望将其保存到文件中,然后进行解析,例如:

stdout, stderr = mafft_cline()
with open("aligned.fasta", "w") as handle:
    handle.write(stdout)
from Bio import AlignIO
align = AlignIO.read("aligned.fasta", "fasta")

或者,要直接使用AlignIO解析输出,可以使用StringIO将字符串转换为句柄:

stdout, stderr = mafft_cline()
from StringIO import StringIO
from Bio import AlignIO
align = AlignIO.read(StringIO(stdout), "fasta")

相关问题 更多 >