过滤器对象

时间:2016-06-20 23:03:06

标签: python dictionary

我的词典理解在这里发生了什么?

我正在解析BLAST文件,并希望为文件中的每一行创建对象。理想情况下,每个对象都将存储在字典中,以便稍后在程序中使用。 解析工作正常,但我最终得到一个空白的transSwiss字典。

以下是一些输出行:

c0_g1_i1|m.1    gi|74665200|sp|Q9HGP0.1|PVG4_SCHPO  100.00  372 0   0   1   372 1   372 0.0   754
c1000_g1_i1|m.799   gi|48474761|sp|O94288.1|NOC3_SCHPO  100.00  747 0   0   5   751 1   747 0.0  1506
c1001_g1_i1|m.800   gi|259016383|sp|O42919.3|RT26A_SCHPO    100.00  268 0   0   1   268 1   268 0.0   557
c1002_g1_i1|m.801   gi|1723464|sp|Q10302.1|YD49_SCHPO   100.00  646 0   0   1   646 1   646 0.0  1310

我正在尝试将每个BLAST行设为parse_blast对象。

class parse_blast(object):

    def __init__(self, line):

        #Strip end-of-line and split on tabs
        self.fields = line.strip("\n").split("\t")
        self.transcriptId, self.isoform = self.fields[0].split("|")
        self.swissStuff = self.fields[1].split("|")
        self.swissProtId = self.swissStuff[3]
        self.percentId = self.fields[2]

    def filterblast(self):
        return float(self.percentId) > 95



blastmap = map(parse_blast, blast_output.readlines())

filtered = filter(parse_blast.filterblast, blastmap)

transSwiss = {blastmap.transcriptId:blastmap for blastmap.transcriptId in filtered}

1 个答案:

答案 0 :(得分:2)

执行此操作时:

for blastmap.transcriptId in filtered

您正尝试按顺序将filtered的每个元素分配给blastmap.transcriptIdblastmapmap类型的列表或实例,具体取决于您的Python版本,因此它没有transcriptId属性,并且您的代码失败并显示AttributeError

使用变量。一个 new 变量:

transSwiss = {pb.transcriptId: pb for pb in filtered}