def edge_distances(peak,feature):
    """
    Return distances between peak and feature edges

    Arguments:
      peak (Peak): peak instance
      feature (Feature): feature instance

    Returns:
      tuple: a pair of distances, the first being the
        smallest distance between an edge of the peak
        region and an edge of the feature region and
        the second being the distance from the same peak
        edge to the opposite feature edge.
        Both distances are zero if the peak is entirely
        contained within the feature (or vice versa).
        If there is partial overlap then the smallest
        distance is returned as zero, the second is
        the amount of the peak that lies outside the
        feature.

    """
    if ((peak.start >= feature.start and
         peak.end <= feature.end) or
        (feature.start >= peak.start and
         feature.end <= peak.end)):
        # Peak entirely contained in feature (or vice versa)
        return (0,0)
    # All possible distances
    ds_fs = abs(peak.start - feature.start)
    ds_fe = abs(peak.start - feature.end)
    de_fs = abs(peak.end - feature.start)
    de_fe = abs(peak.end - feature.end)
    if peak.start >= feature.start and peak.start <= feature.end:
        # Peak start inside feature:
        #
        # |              PPPPPPP     |
        # |--------FFFFFFFFF---------|
        #
        # Return distances from peak end to feature end
        return (0,de_fe)
    if peak.end >= feature.start and peak.end <= feature.end:
        # Peak end inside feature
        #
        # |    PPPPPPP               |
        # |--------FFFFFFFFF---------|
        #
        # Return distances from peak start to feature start
        return (0,ds_fs)
    if ds_fe < de_fs:
        # No overlap: peak start to feature end is smallest
        #
        # |                PPPPPPP    |
        # |----FFFFFFFFF--------------|
        #
        return (ds_fe,ds_fs)
    else:
        # No overlap: peak end to feature start is smallest
        #
        # |    PPPPPPP                |
        # |-------------FFFFFFFFF-----|
        #
        return (de_fs,de_fe)