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

Key type mismatch: cannot index table with given expressions #3574

Open
iris-garden opened this issue May 6, 2024 · 0 comments
Open

Key type mismatch: cannot index table with given expressions #3574

iris-garden opened this issue May 6, 2024 · 0 comments
Labels
discourse migrated from discuss.hail.is

Comments

@iris-garden
Copy link
Owner

Note

The following post was exported from discuss.hail.is, a forum for asking questions about Hail which has since been deprecated.

(Dec 20, 2023 at 18:05) mgarcia said:

I am trying to annotate a MatrixTable with sample IDs written as integers, but I am encountering the key type mismatch error below. Is there a way to import the values into samples as strings to avoid this error? Should I use hl.import_lines? Thank you in advance.

    samples = hl.import_table(args.sub_annot, key='SAMPLE', impute=True)

    vcf = vcf.annotate_cols(
        sa=samples[vcf.s]
    )
Traceback (most recent call last):
File "/opt/notebooks/hail_variant_qc_V10.py, line 485, in <module> sa=samples [vcf.s]
File "/opt/conda/lib/python3.9/site-packages/hail/table-py, line 379, in getitem return self.index(*wrap_to_tuple(item))
File "/opt/conda/lib/python3.9/site-packages/hail/table-py", line 1840, in index
raise ExpressionException(f"Key type mismatch: cannot index table with given expressions: \n" hail.expr.expressions.base_expression. ExpressionException: Key type mismatch: cannot index table with given expressions:
Table key:
int32
Index Expressions: str

(Jan 02, 2024 at 14:41) danking said:

Hey mgarcia !

You probably don’t want hl.import_lines. hl.import_table is the recommended way to import delimited text files.

The issue is that Hail is guessing that your TSV/CSV file’s SAMPLE column is an integer (because they’re all parsable as integers), but your matrix table is probably coming from a VCF. hl.import_vcf unconditionally imports sample ids as strings. The fix is simple: hl.import_table accepts a types argument which overrides type imputation for the specified fields:

samples = hl.import_table(..., types={'SAMPLE': hl.tstr})
@iris-garden iris-garden added the discourse migrated from discuss.hail.is label May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discourse migrated from discuss.hail.is
Projects
None yet
Development

No branches or pull requests

1 participant