Skip to main content

Algorithms and data structures for my Python projects

Project description

ddalg

Algorithms and data structures for my Python projects.

Interval tree

Interval tree is a data structure for holding intervals and to allow efficiently find intervals that overlap with given interval or point. Read more on Wikipedia.

Implementation note

This implementation uses half-open intervals, where begin coordinate is excluded. Half-open intervals are used in e.g. BED genomic format.

The current implementation needs to rebuild the tree after each insert, hence the tree is not efficient for using in read/write fashion.

Usage

  • implement your custom interval object while extending Interval. Two properties need to be overwritten:
    • begin - 0-based (excluded) begin coordinate of the interval
    • end - 0-based (included) end coordinate of the interval
      from ddalg.itree import Interval
      
      class YourInterval(Interval):
      
        def __init__(self, begin: int, end: int):
          self._begin = begin
          self._end = end
          # anything else
      
        @property
        def begin(self):
          return self._begin
      
        @property
        def end(self):
          return self._end
      
  • create a collection of your intervals and store them in the interval tree:
    from ddalg.itree import IntervalTree
     
    itree = IntervalTree([YourInterval(0, 3), YourInterval(1, 4)])
    # ... 
    
  • query itree:
    • using 1-based position:
      itree.search(1)
      

      returns (0,3)

    • using half-open interval coordinates:
      itree.get_overlaps(0, 1) 
      

      returns (0,3), effectively the same query as above

    • using fuzzy search with required coverage
      itree.fuzzy_query(0, 1, coverage=.90)
      

      require interval stored in a tree to have at least 90% overlap with query interval

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ddalg-0.0.2.post2.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

ddalg-0.0.2.post2-py3.6.egg (22.3 kB view details)

Uploaded Source

File details

Details for the file ddalg-0.0.2.post2.tar.gz.

File metadata

  • Download URL: ddalg-0.0.2.post2.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0.post20200309 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.3

File hashes

Hashes for ddalg-0.0.2.post2.tar.gz
Algorithm Hash digest
SHA256 486350139311115a7375c7b3e8a7f21533dfe56daaa776b4b3888908d315aa74
MD5 7daf6284964c79fdbe0d1642a06e3302
BLAKE2b-256 38a9bd8c147dd082cf7ebdd9f07fefa7be076cd5e072e3a768df4b3ca89f23c2

See more details on using hashes here.

File details

Details for the file ddalg-0.0.2.post2-py3.6.egg.

File metadata

  • Download URL: ddalg-0.0.2.post2-py3.6.egg
  • Upload date:
  • Size: 22.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0.post20200309 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.3

File hashes

Hashes for ddalg-0.0.2.post2-py3.6.egg
Algorithm Hash digest
SHA256 6de9a80b81dae2c6bac88b3b528a6801095077ad77a678385b934b9531864db0
MD5 37837596cbefb70e9882d7a0a7dc2239
BLAKE2b-256 921c6d718be33a80f76c857aa49c561e3a26cc8ba9f061c981b39799a03359db

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page