Skip to content

Improve API for VCF read_variant_stats() and read_allele_count()#852

Merged
alancleary merged 18 commits into
mainfrom
alancleary/VCF-2/stats-improvements
Nov 5, 2025
Merged

Improve API for VCF read_variant_stats() and read_allele_count()#852
alancleary merged 18 commits into
mainfrom
alancleary/VCF-2/stats-improvements

Conversation

@alancleary

Copy link
Copy Markdown
Member

read_variant_stats():

  • Added support for a list of regions like read() and deprecated single region parameter.
  • Added option to scan_all_samples like read().
  • Added an option to drop "ref" alleles.
  • Pandas DataFrame is now returned by default; arrow Table can still be returned via new read_variant_stats_arrow() method .
  • Added contig column for joining.
  • Results are now ordered by contig, then pos.

read_allele_count():

  • Added support for a list of regions like read and deprecated single region parameter.
  • Pandas DataFrame is now returned by default; arrow Table can still be returned via new read_allele_count_arrow() method .
  • Added contig column for joining.
  • Results are now ordered by contig, then pos.

Miscellaneous:

  • Added a Region class in the Python API that is used to parse, sort, and consolidate regions; this is now applied to all regions input to the API.
  • Some cleanup of relevant AF stats code.
  • Fixed a minor TODO in the related allele count code path.

Previously the method returned a pyarrow.Table but the return type hint was a pandas.DataFrame, so the new behavior is correct. A pyarrow.Table can still be retrieved via a new Dataset.read_variant_stats_arrow() method.
…stats_arrow()

This allows multiple regions to be queried simultaneously. Consequently, a "contig" column was added to the returned DataFrame and Table, respectively, to clarify what contig positions occur on. The old "region" parameter is still present but has been deprecated in preference for the new "regions" parameter.
…nd consolidate regions

This ensures that stats have a consistent ordering and that there isn't redundant rows when regions overlap. Note that a Region class was introduced to facilitate this functionality.
…ariant_stats_arrow()

This allows variant stats to be computed relative to all samples in the dataset. To support this change, an AFComputer abstract class and two subclasses were introduced to encapsulate the AF stats code. Additionally, supporting types were introduced and variables that use these types were renamed to clarify how the code that uses these classes works.
…tats_arrow()

This causes the "ref" allele to be omitted from the results.
Previously the method returned a pyarrow.Table but the return type hint was a pandas.DataFrame, so the new behavior is correct. A pyarrow.Table can still be retrieved via a new Dataset.read_allele_count_arrow() method.
…unt_arrow()

This allows multiple regions to be queried simultaneously. Consequently, a "contig" column was added to the returned DataFrame and Table, respectively, to clarify what contig positions occur on. The old "region" parameter is still present but has been deprecated in preference for the new "regions" parameter. Also, the code for parsing and preparing input regions used by read_variant_stats() and read_variant_stats_arrow() was encapsulated in a _prepare_regions() method so that it may be used in this context as well.
This gives AlleleCountReader access to AlleleCount's private members, thus removing the need for redundant variables.
This means all regions passed to the API are now handled consistently, i.e. they are parsed, sorted, and consolidated the same way for every method.
@alancleary

Copy link
Copy Markdown
Member Author

@XanthosXanthopoulos This PR is ready for review.

Comment thread apis/python/src/tiledbvcf/dataset.py Outdated
"regions": regions,
"scan_all_samples": scan_all_samples,
}
return self.read_variant_stats_arrow(**kwargs).to_pandas()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Are any of the two flags described here helpful in this case?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. I think this could benefit all of the .to_pandas() calls in this file. I'll do some testing and report back.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@XanthosXanthopoulos I updated all of the Table.to_pandas() calls to use the technique described in the link you provided. I did some basic profiling of these calls with tracemalloc and didn't see a significant improvement. However, I see no harm in making this change and it may help when dealing with large datasets in production so I went ahead and pushed it.

Comment thread apis/python/src/tiledbvcf/dataset.py Outdated
if self.mode != "r":
raise Exception("Dataset not open in read mode")

return self.read_allele_count_arrow(regions=regions).to_pandas()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same comment

This was done using the split_blocks and self_destruct parameters in tandem.
@alancleary
alancleary merged commit 4c9a811 into main Nov 5, 2025
11 checks passed
@alancleary
alancleary deleted the alancleary/VCF-2/stats-improvements branch November 5, 2025 22:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants