Skip to content

variant lookup table has sample_type #4289

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

Merged
merged 5 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified hail_search/fixtures/GRCh37/SNV_INDEL/lookup.ht/.README.txt.crc
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion hail_search/fixtures/GRCh37/SNV_INDEL/lookup.ht/README.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
This folder comprises a Hail (www.hail.is) native Table or MatrixTable.
Written with version 0.2.128-eead8100a1c1
Created at 2024/04/03 17:08:32
Created at 2024/08/12 16:34:26
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified hail_search/fixtures/GRCh37/SNV_INDEL/lookup.ht/metadata.json.gz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified hail_search/fixtures/GRCh38/MITO/lookup.ht/.README.txt.crc
Binary file not shown.
Binary file modified hail_search/fixtures/GRCh38/MITO/lookup.ht/.metadata.json.gz.crc
Binary file not shown.
2 changes: 1 addition & 1 deletion hail_search/fixtures/GRCh38/MITO/lookup.ht/README.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
This folder comprises a Hail (www.hail.is) native Table or MatrixTable.
Written with version 0.2.128-eead8100a1c1
Created at 2024/04/03 15:52:09
Created at 2024/08/12 16:46:33
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified hail_search/fixtures/GRCh38/MITO/lookup.ht/globals/parts/part-0
Binary file not shown.
Binary file modified hail_search/fixtures/GRCh38/MITO/lookup.ht/metadata.json.gz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified hail_search/fixtures/GRCh38/SNV_INDEL/lookup.ht/.README.txt.crc
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion hail_search/fixtures/GRCh38/SNV_INDEL/lookup.ht/README.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
This folder comprises a Hail (www.hail.is) native Table or MatrixTable.
Written with version 0.2.128-eead8100a1c1
Created at 2024/04/03 17:00:55
Created at 2024/08/12 17:01:09
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified hail_search/fixtures/GRCh38/SNV_INDEL/lookup.ht/metadata.json.gz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
25 changes: 11 additions & 14 deletions hail_search/queries/mito.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
from collections import defaultdict

from aiohttp.web import HTTPNotFound
import hail as hl
Expand Down Expand Up @@ -322,33 +322,30 @@ def _add_project_lookup_data(self, ht, annotation_fields, *args, **kwargs):
).filter(hl.is_defined),
)).filter(
lambda x: x[1].any(hl.is_defined)
).starmap(lambda project_guid, family_indices: (
project_guid,
hl.dict(family_indices.map(lambda j: (lookup_ht.project_families[project_guid][j], True))),
).starmap(lambda project_key, family_indices: (
project_key,
hl.dict(family_indices.map(lambda j: (lookup_ht.project_families[project_key][j], True))),
))), 1),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure on the syntax here but I think you can update this to return the structure you want by changing this to the following:

hl.enumerate(lookup_ht.project_stats).starmap(lambda i, ps: (
  lookup_ht.project_guids[i],
  hl.enumerate(ps).starmap(
    lambda j, s: hl.or_missing(self._stat_has_non_ref(s), j)
  ).filter(hl.is_defined),
)).filter(
  lambda x: x[1].any(hl.is_defined)
.starmap(lambda project_key, family_indices: (
  project_key,
  hl.dict(family_indices.map(lambda j: (lookup_ht.project_families[project_key][j], True))),
).group_by(
  lambda x: x[0][0]
).map_values(
  lambda project_data: hl.dict(project_data.starmap(
    lambda project_key, families: (project_key[1], families)
  ))  
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns something like {'R0001_1kg': {'WES': {'F000002_2': True}}, 'R0003_test': {'WES': {'F000011_11': True}, 'WGS': {'F000011_11': True}}} but the resulting structure is expected to be {project_guid: {family_guid: {sample_type: bool}}}. I've been trying to get the result that I want in this block of hail code but haven't figured it out yet.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I think the change would be

hl.enumerate(lookup_ht.project_stats).starmap(lambda i, ps: (
  lookup_ht.project_guids[i],
  hl.enumerate(ps).starmap(
    lambda j, s: hl.or_missing(self._stat_has_non_ref(s), j)
  ).filter(hl.is_defined),
)).filter(
  lambda x: x[1].any(hl.is_defined)
.flatmap(
  lambda x: x[1].map(lambda j: (x[0][0], x[0][1], lookup_ht.project_families[project_key][j]))
).group_by(
  lambda x: x[0]
).map_values(
  lambda project_data: project_data.group_by(
    lambda x: x[2]
  ).map_values(
    lambda x: {x[1]: True}
  )
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched the order of the project_samples dict and used your original suggestion here!

)[0]

for project_guid, families in variant_projects.items():
if os.path.exists(self._get_table_path(f'projects/WES/{project_guid}.ht')):
sample_type = 'WES'
else:
sample_type = 'WGS'
for family_guid in families:
families[family_guid] = {sample_type: True}

# Variant can be present in the lookup table with only ref calls, so is still not present in any projects
if not variant_projects:
raise HTTPNotFound()

new_variant_projects = defaultdict(lambda: defaultdict(dict))
for (project_guid, sample_type), families in variant_projects.items():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assuming you need to kepe this. post processing, new_variant_projects is a confusing variable name - maybe something like variant_project_sample_types

for family_guid, value in families.items():
new_variant_projects[project_guid][family_guid][sample_type] = value

annotation_fields.update({
'familyGenotypes': lambda r: hl.dict(r.family_entries.map(
lambda entries: (entries.first().familyGuid, entries.filter(hl.is_defined).map(self._get_sample_genotype))
)),
})

logger.info(f'Looking up {self.DATA_TYPE} variant in {len(variant_projects)} projects')
logger.info(f'Looking up {self.DATA_TYPE} variant in {len(new_variant_projects)} projects')

return super()._add_project_lookup_data(ht, annotation_fields, project_samples=variant_projects, **kwargs)
return super()._add_project_lookup_data(ht, annotation_fields, project_samples=new_variant_projects, **kwargs)

@staticmethod
def _stat_has_non_ref(s):
Expand Down
Loading