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

CodonAdaptationIndex division by zero error #962

Closed
cswarth opened this issue Oct 10, 2016 · 3 comments
Closed

CodonAdaptationIndex division by zero error #962

cswarth opened this issue Oct 10, 2016 · 3 comments

Comments

@cswarth
Copy link

cswarth commented Oct 10, 2016

CodonUsage.CodonAdaptationIndex is broken in a couple ways. First, the generate_index() method will crash with a float division by zero error unless the fasta file uses every possible codon at least once.

from Bio.SeqUtils import CodonUsage
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord

with open("minimal.fa", "w") as fh:
    seq = Seq("AGTACACTGCGT")
    rec = SeqRecord(seq, id="AC12345")
    SeqIO.write(rec, fh, "fasta")

ci = CodonUsage.CodonAdaptationIndex()
ci.generate_index("minimal.fa")

produces,

$  python bin/bug.py 
Traceback (most recent call last):
  File "bin/bug.py", line 13, in <module>
    ci.generate_index("minimal.fa")
  File "/home/cwarth/.conda/envs/biopython/lib/python2.7/site-packages/Bio/SeqUtils/CodonUsage.py", line 105, in generate_index
    rcsu.append(self.codon_count[codon] / denominator)
ZeroDivisionError: float division by zero

Second, the signature of generate_index() should take a file handle to read, NOT the string name of a fasta file. Mocking a file handle for testing is a lot easier than writing a mock file to the filesystem.

@peterjc
Copy link
Member

peterjc commented Oct 11, 2016

Taking a handle OR a filename (to preserve backward compatibility, but also matching the typical usecase) would be a sensible improvement. If you would like to submit this as a pull request, please use:

from Bio.File import as_handle

for consistency.

@peterjc
Copy link
Member

peterjc commented Oct 11, 2016

See also #243.

@peterjc
Copy link
Member

peterjc commented Jul 5, 2018

This is a duplicate of #243.

@peterjc peterjc closed this as completed Jul 5, 2018
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

2 participants