Пример #1
0
def test_creation():
    assert I.closed(0, 1) == I.AtomicInterval(I.CLOSED, 0, 1, I.CLOSED)
    assert I.open(0, 1) == I.AtomicInterval(I.OPEN, 0, 1, I.OPEN)
    assert I.openclosed(0, 1) == I.AtomicInterval(I.OPEN, 0, 1, I.CLOSED)
    assert I.closedopen(0, 1) == I.AtomicInterval(I.CLOSED, 0, 1, I.OPEN)
    assert I.closed(-I.inf, I.inf) == I.open(-I.inf, I.inf)

    with pytest.raises(ValueError):
        I.closed(1, -1)

    assert I.singleton(2) == I.closed(2, 2)
    assert I.Interval() == I.open(0, 0)
    assert I.empty() == I.Interval()
Пример #2
0
def test_emptiness():
    assert I.openclosed(1, 1).is_empty()
    assert I.closedopen(1, 1).is_empty()
    assert I.open(1, 1).is_empty()
    assert not I.closed(1, 1).is_empty()
    assert I.Interval().is_empty()
    assert I.empty().is_empty()
Пример #3
0
def array_from_intervals(intervals):
    '''Create m x 2 numpy array from the set of intervals in an interval.Interval object'''
    if isinstance(intervals, iv.AtomicInterval):
        intervals = iv.Interval(intervals)
    if not isinstance(intervals, iv.Interval):
        raise TypeError(
            "'intervals' parameter must be of type intervals.Interval")
    return np.array([[ai.lower, ai.upper] for ai in intervals])
Пример #4
0
def test_creation():
    assert I.closed(0, 1) == I.AtomicInterval(I.CLOSED, 0, 1, I.CLOSED)
    assert I.open(0, 1) == I.AtomicInterval(I.OPEN, 0, 1, I.OPEN)
    assert I.openclosed(0, 1) == I.AtomicInterval(I.OPEN, 0, 1, I.CLOSED)
    assert I.closedopen(0, 1) == I.AtomicInterval(I.CLOSED, 0, 1, I.OPEN)
    assert I.closed(-I.inf, I.inf) == I.open(-I.inf, I.inf)

    assert I.singleton(2) == I.closed(2, 2)
    assert I.Interval() == I.open(0, 0)
    assert I.empty() == I.Interval()
    assert I.closed(3, -3) == I.empty()
    assert I.openclosed(3, 3) == I.empty()

    # I.empty() is a singleton
    assert I.empty() is I.empty()

    assert I.Interval(I.closed(0, 1).to_atomic()) == I.closed(0, 1)
    assert I.Interval(I.closed(0, 1)) == I.closed(0, 1)
    assert I.Interval(I.closed(0, 1).to_atomic(),
                      I.closed(2, 3)) == I.closed(0, 1) | I.closed(2, 3)
    assert I.Interval(I.closed(0, 1)
                      | I.closed(2, 3)) == I.closed(0, 1) | I.closed(2, 3)

    with pytest.raises(TypeError):
        I.Interval(1)
Пример #5
0
def interval_list_from_IntervalSeries(intervalSeries):
    '''Create a list of interval.Interval objects, 1 for each interval in a pynwb.misc.IntervalSeries'''
    if not isinstance(intervalSeries, pynwb.misc.IntervalSeries):
        raise TypeError(
            "'intervalSeries' parameter must be of type pynwb.misc.IntervalSeries"
        )
    if isinstance(intervals, iv.AtomicInterval):
        intervals = iv.Interval(intervals)
    return [iv.closed(*i) for i in array_from_IntervalSeries(intervalSeries)]
Пример #6
0
 def add_note(self, note):
     """ Relates the note with the tonic and adds it to interval list
         of the chord. """
     try:
         assert(isinstance(note, n.Note))
         distance = n.distance(self.tonic, note)
         new = i.Interval(distance)
         self.add_interval(new)
     except AssertionError:
         print('Argument isn\'t a Note object.')        
Пример #7
0
def times_in_intervals(times, intervals, return_indices=False):
    '''Return list of times that are contained by a list of intervals (interval.Interval object)'''
    if isinstance(intervals, iv.AtomicInterval):
        intervals = iv.Interval(intervals)
    if not isinstance(intervals, iv.Interval):
        raise TypeError(
            "'intervals' parameter must be of type intervals.Interval")
    if not return_indices:
        return [t for t in times if t in intervals]
    else:
        return [i for i, t in enumerate(times) if t in intervals]
Пример #8
0
def test_interval_to_atomic():
    intervals = [I.closed(0, 1), I.open(0, 1), I.openclosed(0, 1), I.closedopen(0, 1)]
    for interval in intervals:
        assert interval == I.Interval(interval.to_atomic())
        assert interval == interval.to_atomic()

    assert I.closed(0, 1) | I.closed(2, 3) != I.closed(0, 3)
    assert (I.closed(0, 1) | I.closed(2, 3)).to_atomic() == I.closed(0, 3)

    assert I.closed(0, 1).to_atomic() == I.closed(0, 1).enclosure()
    assert (I.closed(0, 1) | I.closed(2, 3)).enclosure() == I.closed(0, 3)

    assert I.empty().to_atomic() == I.AtomicInterval(False, I.inf, -I.inf, False)
def allows_major(interval, soft=True):
    if isinstance(interval, I.Interval):
        try:
            interval = I.Interval(interval[-1]) if soft else interval
        except IndexError:  # empty interval
            return False
        return any(allows_major(i, soft) for i in interval)
    elif isinstance(interval, I.AtomicInterval):
        inc = float('inf') if soft else 1 + int(interval.left == I.OPEN)
        next_version = Version(interval.lower.major + inc, 0, 0)
        return next_version in interval
    else:
        raise TypeError(
            'Parameter must be an Interval or an AtomicInterval instance.')
Пример #10
0
def chord_id_to_intervals(chord_id):
    """ Recieves a chord id and a tonic note, returns a list of Interval instances;
        tonic could be either a Note instance or a string. """
    try:
        assert(chord_id in chord_id_to_name)
        result = []
        for j in chord_id:
            aux = i.Interval(j)
            i.add_interval(aux, result)
        return result
    except AssertionError:
        print('Wrong argument')
    
    return result
Пример #11
0
def test_to_interval_to_atomic():
    intervals = [
        I.closed(0, 1),
        I.open(0, 1),
        I.openclosed(0, 1),
        I.closedopen(0, 1)
    ]
    for interval in intervals:
        assert interval == I.Interval(interval.to_atomic())
        assert interval == interval.to_atomic()

    assert I.closed(0, 1) | I.closed(2, 3) != I.closed(0, 3)
    assert (I.closed(0, 1) | I.closed(2, 3)).to_atomic() == I.closed(0, 3)

    assert I.closed(0, 1).to_atomic() == I.closed(0, 1).enclosure()
    assert (I.closed(0, 1) | I.closed(2, 3)).enclosure() == I.closed(0, 3)
Пример #12
0
def find_intervals(genome1, genome2, all_match, strip):
    # NOTE: There might be trouble with RSIs that are the delimited at front
    #       and back by the same marker.

    # Only strip genomes (to find RSIs) if instructed in arguments
    stripped1 = genome1
    stripped2 = genome2
    if strip:
        stripped1 = strip_genome_unique(stripped1)
        stripped2 = strip_genome_unique(stripped2)

    # Index one genome, loop over the other.
    index = Genome_pairs_index()
    index.index_pairs(stripped1)

    ints = intervals.IntervalDict()
    for key, chrom in stripped2.chromosomes.iteritems():
        for i in xrange(len(chrom) - 1):
            pair2 = (chrom[i].id, chrom[i + 1].id)
            if markers.are_siblings(*pair2):
                continue
            full_ids2 = [ marker.id for marker in
                         genome2.chromosomes[ key ] \
                             [ chrom[i].index : chrom[i+1].index+1 ] ]
            pairs = index.query(*pair2)
            # Find all pairs in the index with a full match (or all match)
            matches = []
            match_ids = []
            for pair1 in pairs:
                # do we have a match between pair1 and pair2?

                full_ids1 = [ marker.id for marker in
                              genome1.chromosomes[ pair1[0].chromosome ] \
                                  [ pair1[1].index : pair1[2].index+1 ] ]

                cmp_result, _ = compare_marker_intervals(
                    full_ids1, full_ids2, all_match)
                if cmp_result:
                    matches.append(pair1)
                    match_ids = full_ids1

            if matches:
                # Add a new interval to ints if not already there, and add the
                # loci from matches to it. If the interval is already there, it
                # must already have the loci from matches.
                if not full_ids2 in ints:
                    ints.add(
                        intervals.Interval(
                            id=''.join(full_ids2),
                            marker_ids=full_ids2,
                            loci=[l for l, _, _ in matches],
                            order=intervals.Order(1),
                            weight=1,
                            comment='',
                        ))
                # Add the newly found locus in genome 2 to the interval.
                interval = ints[match_ids]
                locus2 = markers.Locus(
                    species=genome2.species,
                    chromosome=key,
                    start=chrom[i].locus.start,
                    end=chrom[i + 1].locus.end,
                    orientation=1,
                    comment='',
                )
                if interval.marker_ids == list(reversed(full_ids2)):
                    locus2.orientation *= -1
                interval.loci.append(locus2)

    # Filter the ints to distinguish repeat spanning intervals.
    adjs = intervals.IntervalDict()
    RSIs = intervals.IntervalDict()
    for interval in ints.itervalues():
        if len(interval.marker_ids) == 2:
            adjs.add(interval)
        else:
            RSIs.add(interval)

    return adjs, RSIs