pyranges0 Documentation
repository·master·Indexed 19 days ago
https://github.com/pyranges/pyranges0A Python library optimized for fast and memory-efficient manipulation and querying of genomic intervals and annotations. It integrates with the Pandas ecosystem, using DataFrames for storage and providing a terse syntax that supports method chaining. Key features include intersection operations, interval merging, and support for exporting to CSV, GTF, GFF3, and BigWig formats. Note: pyranges0 is deprecated in favor of pyranges1.
What's inside pyranges0
- PyRanges is a Python library designed for efficient and intuitive manipulation of genomics data, specifically genomic intervals such as genes, genomic features, or reads. It is optimized for fast querying and manipulation of genomic annotations.
Overview of pyranges0 features
masterPyRanges is a Python library designed for efficient and intuitive manipulation of genomics data, specifically genomic intervals (e.g., genes, genomic features, or reads). It is optimized for fast querying and manipulation of genomic annotations.
Key features include:
- High speed and memory efficiency.
- Pythonic/Pandastic interface: It uses Pandas DataFrames, allowing it to integrate seamlessly with the broader Python data science stack.
- Terse syntax that supports method chaining.
How PyRanges handles documentation and testing
masterPyRanges relies on a continuous integration model with specific standards:
Documentation Standards
PyRanges uses the NumPy/SciPy-style for Python docstrings. This allows Sphinx to automatically generate API documentation. All new functions must have appropriate docstrings, and existing ones must be updated if the function logic changes.
Testing Layers
- Unit tests: Fast, mandatory tests for core functionality.
- Doctest: Mandatory tests that verify code snippets within the documentation (tutorials/how-tos) produce expected results.
- Property-based tests: Time-consuming tests that generate random data to validate PyRanges results against reference bioinformatics tools. These are run by the core team during backbone edits.
Perform group-by operations using apply
masterYou can perform 'group by then apply' operations (common in Pandas) by using
applyon a PyRanges object. Since PyRanges processes each Chromosome/Strand combination independently, you can use standard Pandasgroupbywithin theapplymethod to operate on specific groups (like an 'ID' column) within those chromosomes.To get the first (5'-most) exon of each CDS group, sort the intervals in 5' -> 3' order using
sort('5'), then useapplywith a Pandasgroupby().first()chain.( ann.subset(lambda x:x.Feature=='CDS') .drop(['Parent', 'Feature']) .sort('5') .apply(lambda x:x.groupby('ID', as_index=False).first()) )How subset, assign, and apply work with functions
masterWhen using
subset,assign, orapply, you provide a function that is applied to each DataFrame in the PyRanges collection (where each DataFrame represents a unique Chromosome/Strand combination).subset(func): The function must return a boolean Series with the same number of rows as the input PyRanges. It is used as a row selector.assign(name, func): The function must return a Series with the same number of rows as the input PyRanges. The returned Series is assigned to the new column name.apply(func): Use this when your function returns a DataFrame that can be converted back into a PyRanges object (i.e., it containsChromosome,Start,End, andStrandcolumns).
Calculate interval properties using pandas-style operations
masterPyRanges objects support element-wise operations similar to pandas Series. You can create new columns by performing arithmetic on existing columns (like
EndandStart) to calculate properties such as interval length.# Create a new 'Length' column by subtracting Start from End prom_in_cds.Length = prom_in_cds.End - prom_in_cds.StartWhat are PyRanges?
masterPyRanges are collections of genomic intervals that support comparison operations (such as overlap and intersection) and other methods useful for genomic analyses.
Key characteristics:
- Metadata Support: Intervals can have an arbitrary number of metadata fields (columns).
- Pandas Integration: Data is stored in a
pandas.DataFrame, making it compatible with the high-performance scientific computing ecosystem.
Understand Stranded vs Unstranded PyRanges objects
masterPyRanges objects are categorized as either Stranded or Unstranded:
- Stranded: An object where a
Strandcolumn is present and all values are either+or-. - Unstranded: An object where the
Strandcolumn is absent or contains invalid values (e.g.,.).
You can check the status of an object using the
.strandedproperty. Many PyRanges methods require a Stranded input. If your data contains invalid strand values, use.make_stranded()to transform them to+or remove them.# Check if the object is stranded is_stranded = cds.stranded # Transform invalid strand values to '+' or remove them cds = cds.make_stranded()- Stranded: An object where a
Understand PyRanges coordinate conventions
masterPyRanges objects represent sequence intervals (genomic regions, protein domains, etc.). It is critical to note that PyRanges follows standard Python conventions for coordinates:
- 0-based indexing.
- Start is included, end is excluded (half-open intervals).
While formats like GFF and GTF use 1-based, inclusive coordinates, PyRanges automatically handles the conversion between these conventions when loading and writing files in those formats.
Distinguish between PyRanges and pandas 'merge' and 'join'
masterBe careful when using the terms
mergeandjoin, as they have different meanings depending on the object type:- In pandas:
mergeandjoinrefer to database-style operations (joining tables based on common column values). - In PyRanges:
mergeandjoinrefer to genomic interval manipulation based on spatial overlap.
- In pandas:
How PyRanges handles sorting and internal data structure
masterA PyRanges object is a collection of DataFrames, where data is partitioned into separate tables for each chromosome/strand pair (e.g., one table for
chr1+strand, one forchr1-strand, etc.).Sorting Behavior
- PyRanges
.sort(): Sorts each internal chromosome/strand table independently. Because intervals on different chromosomes are never mixed in the same table, they have no relative order to each other. When printing, PyRanges displays the tables ordered by Chromosome and Strand. - Pandas
.sort_values(): Sorts the entire dataset globally, which can mix rows from different chromosomes and strands together.
Indexing
Unlike pandas, PyRanges objects do not have a user-facing index. While the internal tables have indices, they are hidden from the user and should not be queried or relied upon for data access.
- PyRanges
Access pyranges0 documentation
masterFull documentation forpyranges0, including installation instructions, API references, tutorials, and how-to guides, is hosted at: https://pyranges0.readthedocs.io/en/latest/