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

csv_string_to_codons_dict() raises ValueError when reading a custom codon table - proposed workaround #9

Open
lcareypando opened this issue Aug 10, 2024 · 2 comments

Comments

@lcareypando
Copy link

lcareypando commented Aug 10, 2024

I've used pandas df.to_csv() to write out a custom codon table
codon_table.csv

reading this table gives an error when reading the last line. adding the length check ( if len(line)>3:) solves this problem.

def csv_string_to_codons_dict(csv_string):
    """Transform a CSV string of a codon table to a dict."""
    result = {}
    for line in csv_string.split("\n")[1:]:
        if len(line)>3: # new check
            aa, codon, freq = line.split(",")
            if aa not in result:
                result[aa] = {}
            result[aa][codon] = float(freq)
    return result
@veghp
Copy link
Member

veghp commented Aug 12, 2024

Thank you. The issue seems to arise when the csv is terminated with an empty line, which is common and probably a preferable format as well. I'll address this later this month together with DNA Chisel; the function may be replaced with something from the csv package. But in any case perhaps it's better to remove all trailing newlines from the string.

@lcareypando
Copy link
Author

Hi Peter,
Thanks so much for addressing this!
best wishes, and thanks for the great packages.

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