python - str.maketrans 在交互式 python 中可用,但在 python 脚本中不可用?

标签 python python-3.x bioinformatics python-3.3 biopython

我正在编写的代码应该找到 DNA 正向和反向互补链上基因序列的所有开放阅读框 (orfs)。为了制作 DNA 的反向链,我打算使用 str.maketrans() 将互补碱基映射到彼此。

#!/usr/bin/env python3.3

import re
import sys
from argparse import ArgumentParser

pattern = re.compile(r'(?=(ATG(?:...)*?)(?=TAG|TGA|TAA))')

dna_seq = 'ATGACGGCTTGTTTCTTTTCTGTGGCTGCGTGA'

    def find_orfs(dna_seq):
        """
        finds all possible open reading frames (orfs)

        :param dna_seq: str, dna sequence
        :return: list, possible open reading frames
        """

        r_comp = dna_seq[::-1].translate(str.maketrans("ATGC","TACG"))
        return list(pattern.findall(dna) + pattern.findall(r_comp))

当我在解释器中运行它时,它起作用了!它返回正确的答案:

['ATGACGGCTTGTTTCTTTTCTGTGGCTGCG']

当我将其作为脚本(版本 3.3)运行时,我收到 AttributeError!

AttributeError: type object 'str' has no attribute 'maketrans'

但是当我在解释器(版本 3.3)中 dir(str) 时,我看到了 maketrans!什么给了!?

在阅读了对 bytes.maketrans() 的更改后,我尝试了此操作,但没有成功。我该怎么做才能在 python3.3 中获得与 maketrans() 相同的功能?

最佳答案

您的 shebang 行似乎返回了 2.7.x 版本的 python 解释器。如果您不担心,可以使用 #!/usr/local/bin/python3.3 指定直接路径(更改路径以适合解释器的位置)以使其正常工作可移植性(允许其他用户使用您的文件)。有关 #!/usr/bin/env python#!/usr/local/bin/python 之间差异的文章,您可以 look here 。基本上,前者将使用首先出现在环境的 $PATH 上的解释器,在您的情况下是 python2.7

编辑

OP 使用 shell 使用以下命令运行脚本:python myscript.py。这使用默认解释器(在他们的例子中是 2.7),它不识别 maketrans 方法。使用 python3.3 myscript.py 运行脚本解决了问题。

关于python - str.maketrans 在交互式 python 中可用,但在 python 脚本中不可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30065446/

相关文章:

Python-正则表达式 : extract a list of tuples after a keyword from a text file

python - write() 写入的文件无法在 Windows 中打开

python - 如何在 Python 中打开文本文件?

database - 是否有用于创建具有网站前端的通用 DNA 序列数据库的现有解决方案?

r - 如何在 R 包中包含 sqlite 数据库文件。以便我可以与数据库文件一起共享/发布它?

python - 告诉基于 contains() 的 XPath 查询在到达字母时停止?

python - 并排显示两张 Folium map ?

python - 如何使用字符串获取 python 的平均值?

python-3.x - 我想从 Python 中的点云生成网格

python - 尝试使用 gzip 解压缩文件但错误 : "name ' d' is not defined"