built in function for computing overlap in Python

后端 未结 4 1318
囚心锁ツ
囚心锁ツ 2020-12-09 08:33

is there a built in function to compute the overlap between two discrete intervals, e.g. the overlap between [10, 15] and [20, 38]? In that case the overlap is 0. If it\'s

4条回答
  •  有刺的猬
    2020-12-09 08:56

    Here is a good function from Aaron Quinlan's chrom_sweep, modified for your interval representation. It returns the number of bp of overlap if they do overlap, otherwise it returns the distance as a negative int.

    def overlaps(a, b):
        """
        Return the amount of overlap, in bp
        between a and b.
        If >0, the number of bp of overlap
        If 0,  they are book-ended.
        If <0, the distance in bp between them
        """
    
        return min(a[1], b[1]) - max(a[0], b[0])
    

提交回复
热议问题