Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

00_rmChr.py "dupread" error #12

Open
dustin-mullaney opened this issue Feb 10, 2023 · 0 comments
Open

00_rmChr.py "dupread" error #12

dustin-mullaney opened this issue Feb 10, 2023 · 0 comments

Comments

@dustin-mullaney
Copy link

The script 00_rmChr.py produces the following error:

Traceback (most recent call last):
  File "/Users/usr/programs/ATACgraph/script/ATACgraph", line 38, in <module>
    module.main()
  File "/Users/usr/programs/ATACgraph/script/00_rmChr.py", line 80, in main
    rmChr(infile,outfile,targetChr)
  File "/Users/usr/programs/ATACgraph/script/00_rmChr.py", line 49, in rmChr
    dupread += 1
UnboundLocalError: local variable 'dupread' referenced before assignment

I resolved this issue by editing the script:

def rmChr (infile,outfile,targetChr):
    samfile = openBamFile(infile)
    samout = pysam.AlignmentFile(outfile, "wb", template=samfile)
    chrs = samfile.references
    chrs_keep,chrs_rm = processInput (targetChr,chrs)
    keepread = 0
    dupread = 0 #added to fix error
    for chrom in chrs_keep:
        fetchread = samfile.fetch(chrom)
        for read in fetchread:
            samout.write(read)
            keepread += 1
            if read.is_duplicate:
                dupread += 1

    totalread = sum([x.total for x in samfile.get_index_statistics()])
    removeread = totalread - keepread
    removeratio = float(removeread) / totalread

    for chrom in samfile.get_index_statistics():
        if chrom.contig in chrs_rm:
            rmcount = chrom.total
            print "Remove %s %d reads" % (chrom.contig,rmcount)

    print "Remove total %d out of %d (%.3f)" % (removeread,totalread,removeratio) 

    samout.close()
    samfile.close()

Not 100% sure this was an appropriate solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant