Skip to content

Commit

Permalink
refactor: fix circular import
Browse files Browse the repository at this point in the history
  • Loading branch information
msto committed Mar 12, 2024
1 parent 3e50e79 commit 95f4c1d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
31 changes: 31 additions & 0 deletions pybedlite/bed_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@
pertaining to a BED record.
"""

from __future__ import annotations

import attr
import enum
from typing import Optional
from typing import Tuple
from typing import List
from typing import ClassVar
from typing import Type
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from pybedlite.overlap_detector import Interval

Check warning on line 27 in pybedlite/bed_record.py

View check run for this annotation

Codecov / codecov/patch

pybedlite/bed_record.py#L27

Added line #L27 was not covered by tests


"""Maximum BED fields that can be present in a well formed BED file written to specification"""
Expand Down Expand Up @@ -189,3 +196,27 @@ def as_bed_line(self, number_of_output_fields: Optional[int] = None) -> str:
)
fields = self.bed_fields[:number_of_output_fields]
return "\t".join(fields)

@classmethod
def from_interval(cls: Type["BedRecord"], interval: Interval) -> "BedRecord":
"""
Construct a `BedRecord` from a `Interval` instance.
**Note that `Interval` cannot represent a `BedRecord` with a missing strand.**
Converting a record with no strand to `Interval` and then back to `BedRecord` will result in
a record with **positive strand**.
Args:
interval: The `Interval` instance to convert.
Returns:
A `BedRecord` corresponding to the same region specified in the interval.
"""

return BedRecord(
chrom=interval.refname,
start=interval.start,
end=interval.end,
strand=BedStrand.Negative if interval.negative else BedStrand.Positive,
name=interval.name,
)
20 changes: 0 additions & 20 deletions pybedlite/overlap_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,6 @@ def length(self) -> int:
"""Returns the length of the interval."""
return self.end - self.start

def to_bedrecord(self) -> BedRecord:
"""
Convert an `Interval` to a `BedRecord` instance.
**Note that `Interval` cannot represent a `BedRecord` with a missing strand.**
Converting a record with no strand to `Interval` and then back to `BedRecord` will result in
a record with **positive strand**.
Returns:
An `Interval` corresponding to the same region specified in the record.
"""

return BedRecord(
chrom=self.refname,
start=self.start,
end=self.end,
strand=BedStrand.Negative if self.negative else BedStrand.Positive,
name=self.name,
)

@classmethod
def from_bedrecord(cls: Type["Interval"], record: BedRecord) -> "Interval":
"""
Expand Down
2 changes: 1 addition & 1 deletion pybedlite/tests/test_overlap_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def test_construction_from_interval(bed_records: List[BedRecord]) -> None:
# I don't think pytest.mark.parametrize can accept a fixture and expand over its values.
# For loop it is.
for record in bed_records:
new_record = Interval.from_bedrecord(record).to_bedrecord()
new_record = BedRecord.from_interval(Interval.from_bedrecord(record))

assert new_record.chrom == record.chrom
assert new_record.start == record.start
Expand Down

0 comments on commit 95f4c1d

Please sign in to comment.