Python“Tuple”对象没有“has \u key”属性

2021-05-13 14:58:25 发布

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

我正在运行一段免费提供的python代码,用于检测单细胞测序数据中的cnv:

#!/usr/bin/env python

import sys

def main():

    infilename = sys.argv[1]
    outfilename = sys.argv[2]
    statfilename = sys.argv[3]

    chrominfo = ("/path/hg19.chrom.sizes.txt", 0)
    bins = ("/path/hg19.bin.boundaries.50k.bowtie.k50.sorted.txt", 0)
    INFILE = open(infilename, "r")
    OUTFILE = open(outfilename, "w")
    STATFILE = open(statfilename, "w")

    binCounts = []
    for i in range(len(bins)):
        binCounts.append(0)

    print len(binCounts)
    print len(bins)

    counter = 0
    totalReads = 0
    prevChrompos = ""
    for x in INFILE:
        arow = x.rstrip().split("\t")
        thisChrom = arow[2]
        thisChrompos = arow[3]
        if thisChrom.find("_") > -1:
            #print thisChrom
            continue
        if thisChrom == "chrM":
            #print thisChrom
            continue
        if thisChrom == "":
            continue
        if chrominfo.has_key(thisChrom):
            pass
        else:
            continue

        totalReads += 1

        thisChrominfo = chrominfo[thisChrom]
        thisAbspos = long(thisChrompos) + long(thisChrominfo[2])

        counter += 1

        indexUp = len(bins) - 1
        indexDown = 0
        indexMid = int((indexUp - indexDown) / 2.0)

        while True:
            if thisAbspos >= long(bins[indexMid][2]):
                indexDown = indexMid + 0
                indexMid = int((indexUp - indexDown) / 2.0) + indexMid
            else:
                indexUp = indexMid + 0
                indexMid = int((indexUp - indexDown) / 2.0) + indexDown

            if indexUp - indexDown < 2:
                break

        binCounts[indexDown] += 1
        prevChrompos = thisChrompos

    for i in range(len(binCounts)):
        thisRatio = float(binCounts[i]) / (float(counter) / float(len(bins)))
        OUTFILE.write("\t".join(bins[i][0:3]))
        OUTFILE.write("\t")
        OUTFILE.write(str(binCounts[i]))
        OUTFILE.write("\t")
        OUTFILE.write(str(thisRatio))
        OUTFILE.write("\n")

    binCounts.sort()

    STATFILE.write("TotalReads\tMedianBinCount\n")
    STATFILE.write(str(totalReads))
    STATFILE.write("\t")
    STATFILE.write(str(binCounts[len(bins)/2]))
    STATFILE.write("\n")

    INFILE.close()
    OUTFILE.close()
    STATFILE.close()


def fileToDictionary(inputFile, indexColumn):
    input = open(inputFile, "r")

    rd = dict()
#   input.readline()
    for x in input:
        arow = x.rstrip().split("\t")
        id = arow[indexColumn]
        if rd.has_key(id):
            #rd[id].append(arow)
            print "duplicate knowngene id = " + id
            print "arow =   " + str(arow)
            print "rd[id] = " + str(rd[id])
        else:
            rd[id] = arow

    input.close()
    return(rd)


def fileToArray(inputFile, skipFirst):
    input = open(inputFile, "r")

    ra = []

    for i in range(skipFirst):
        input.readline()

    for x in input:
        arow = x.rstrip().split("\t")
        ra.append(arow)

    input.close()
    return(ra)


if __name__ == "__main__":
    main()

我在第40行收到一个错误:

^{pr2}$

我不经常用Python工作,有人能给我个建议吗? 我从哪里开始?在

1条回答
网友
1楼 ·

你的代码需要一个字典和一个元组。我想你错过了一步:你需要改变

chrominfo = ("/path/hg19.chrom.sizes.txt", 0)

^{pr2}$

还要注意如果dict.has_键(key)已被弃用,取而代之的是数字键()

相关问题