python - 属性错误 : 'str' object has no attribute 'transID'

标签 python class error-handling bioinformatics

这是我在python3中定义的一个类。

class BlastHit():
    '''
    instance attributes:
        entry: one line from blast output file
        transID: transcript ID
        SPID: SwissProt ID (without the version number)
        ID: identity
        MM: mismatch

    methods:
        constructor
        __repr__ 
        goodMatchCheck: 
            method that returns whether the hit is a really good match (e.g. >95 identity)
    '''
    def __init__(self, entry):
        self.entry = entry
        splited_line = entry.rstrip().split("\t")

        # Find transcript ID (transID)
        qseqid = splited_line[0]
        self.transID = qseqid.split("|")[0]

        # Find SwissProt ID (SPID)
        sseqid = splited_line[1]
        sp = sseqid.split("|")[3]
        self.SPID = sp.split(".")[0]

        # Find identity (ID)
        self.ID = splited_line[2]

        # Find mismatch (MM)
        self.MM = splited_line[4]

    def __repr__(self):
        return f"BlastHit('{self.entry}')"

    def goodMatchCheck(self):
        return float(self.MM) >= 95.0

    def list_results(self):
        results = list()
        results += self.transID
        return results

我尝试运行以下命令:
if __name__ == "__main__":
    BlastHit.list_results("c0_g1_i1|m.1 gi|74665200|sp|Q9HGP0.1|PVG4_SCHPO  100.00  372 0   0   1   372 1   372 0.0   754")

但是,系统一直给我同样的错误信息:

AttributeError: 'str' object has no attribute 'transID'



谁有想法,请与我分享。谢谢!

最佳答案

BlastHit.list_results("c0_g1_i1|m.1 gi|74665200|sp|Q9HGP0.1|PVG4_SCHPO  100.00  372 0   0   1   372 1   372 0.0   754")

您调用list_result好像它是一个静态方法。所以你传递的字符串被绑定(bind)到 self .

您需要创建一个实例并使用它来代替:
bh = BlastHit("c0_g1_i1|m.1 gi|74665200|sp|Q9HGP0.1|PVG4_SCHPO  100.00  372 0   0   1   372 1   372 0.0   754")
bh.list_results()

关于python - 属性错误 : 'str' object has no attribute 'transID' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61277551/

相关文章:

python - 奇怪的 numpy fft 性能

python - 口是心非在 macOS 上不起作用,得到 : env: python2: No such file or directory

c++ - 在类中实例化类

java - 当从另一个类调用方法时 - 返回一个难以理解的值 JAVA

PHP,用于处理 MySql 和 mysql_close 问题的简单类

php - $_FILES 为空时究竟是什么意思?

mysql - 存储过程,如何处理错误代码: 1062

python - 使用 Matplotlib 后端删除 Holoviews 图形上的字母标签

python - 使用 tor 和 python 抓取谷歌学术搜索

sql-server-2008 - 在将字符串转换为uniqueidentifier时抑制错误