1

I'm running a piece of freely available python code used to detect CNVs in single cell sequencing data:

#!/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()

I'm getting an error on line 40:

Traceback (most recent call last):
  File "/path/varbin.50k.sam.py", line 129, in <module>
    main()
  File "/path/varbin.50k.sam.py", line 40, in main
    **if chrominfo.has_key(thisChrom):
AttributeError: 'tuple' object has no attribute 'has_key'**

I don't work regularly in Python, can someone offer a suggestion? Where do I begin?

4
  • What do you want to do with this line ? chrominfo is defined at the beginning as a tuple. You can access its elements with chrominfo[0] and chrominfo[1] Commented Jul 9, 2015 at 18:07
  • I'm not sure why it's hanging in error - what is missing from the 'has_key' call that isn't represented in that first line where it's defined? Commented Jul 9, 2015 at 18:13
  • should this part : chrominfo = ("/path/hg19.chrom.sizes.txt", 0) be changed to chrominfo = ("/path/hg19.chrom.sizes.txt", thisChrom)? Commented Jul 9, 2015 at 18:20
  • chrominfo is a tuple. Tuple do not have has_key method. This method is defined for dictionnaries. So my question is : what do you want to achieve with this line of code ? thisChrom is not defined here, so you cannot use it in the definition of chrominfo Commented Jul 9, 2015 at 18:22

1 Answer 1

1

Your code is expecting a dictionary and getting a tuple. I think you've missed a step: You need to change

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

To

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

Note also that if dict.has_key(key) has been deprecated in favour of if key in dict.keys()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.