File: location.py

package info (click to toggle)
python-cogent 1.4.1-1.2
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 13,260 kB
  • ctags: 20,087
  • sloc: python: 116,163; ansic: 732; makefile: 74; sh: 9
file content (866 lines) | stat: -rw-r--r-- 30,279 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
#!/usr/bin/env python
"""Alignments and Sequences are _Annotatables
_Annotatables hold a list of Maps.
Maps can be Features, Variables or AlignedSequences.
Maps have a list of Spans.

Also provides Range and Point classes for dealing with parts of sequences.

Span is a region with a start, an end, and a direction. Range is an ordered
collection of Spans (note: Range does _not_ support the list interface, but
you can always access Range.Spans directly). Map is like a Range but is
immutable and is able to be nested, i.e. Maps can be defined relative to
other Maps.

Implementation Notes

Span and Range behave much like Python's slices: a Span contains the element
after its Start but does not contain the element after its End. It may help to
think of the Span indices occurring _between_ the list elements:
    
    a b c d e
   | | | | | |
   0 1 2 3 4 5

...so that a Span whose Start is its End contains no elements (e.g. 2:2), and
a Span whose End is 2 more than its start contains 2 elements (e.g. 2:4 has c
and d), etc. Similarly, Span(0,2) does _not_ overlap Span(2,3), since the
former contains a and b while the latter contains c.

A Point is a Span whose Start and End refer to the same object, i.e. the same
position in the sequence. A Point occurs between elements in the sequence,
and so does not contain any elements itself.

WARNING: this differs from the way e.g. NCBI handles sequence indices, where
the sequence is 1-based, a single index is treated as containing one element,
the point 3 contains exactly one element, 3, rather than no elements, and a
range from 2:4 contains 2, 3 and 4, _not_ just 2 and 3.

"""
from cogent.util.misc import FunctionWrapper, ClassChecker, ConstrainedList, \
    iterable
from itertools import chain
from string import strip

from bisect import bisect_right, bisect_left
import copy

__author__ = "Rob Knight"
__copyright__ = "Copyright 2007-2009, The Cogent Project"
__credits__ = ["Rob Knight", "Peter Maxwell", "Matthew Wakefield",
                    "Gavin Huttley"]
__license__ = "GPL"
__version__ = "1.4.1"
__maintainer__ = "Rob Knight"
__email__ = "rob@spot.colorado.edu"
__status__ = "Prototype"

def _norm_index(i, length, default):
    """For converting s[:3] to s[0:3], s[-1] to s[len(s)-1] and s[0:lots] to s[0:len(s)]"""
    if i is None:
        i = default
    elif i < 0:
        i += length
    return min(max(i,0),length)

def _norm_slice(index, length):
    """_norm_slice(slice(1, -2, 3), 10) -> (1,8,3)"""
    if isinstance(index, slice):
        start = _norm_index(index.start, length, 0)
        end = _norm_index(index.stop, length, length)
        return (start, end, index.step)
    else:
        start = index
        if start < 0: start += length
        if start >= length: raise IndexError(index)
        return (start, start+1, 1)

def as_map(slice, length):
    """Take anything that might be used as a subscript: Integer, Slice,
    or Map, and return a Map."""
    
    if isinstance(slice, (list, tuple)):
        spans = []
        for i in slice:
            spans.extend(as_map(i, length).spans)
        map = Map(spans=spans, parent_length=length)
    elif isinstance(slice, Map):
        map = slice
        # reasons for failure when the following is not commented out
        # should be checked further
        #assert map.parent_length == length, (map, length)
    else:
        (lo, hi, step) = _norm_slice(slice, length)
        assert (step or 1) == 1
        map = Map([(lo, hi)], parent_length=length)
    return map

class SpanI(object):
    """Abstract interface for Span and Range objects.
    
    Required properties: Start, End (must both be numbers)
    """
    __slots__ = []      #override in subclass
    
    def __contains__(self, other):
        """Returns True if other entirely contained in self."""
        raise NotImplementedError
    
    def overlaps(self, other):
        """Returns True if any positions in self are also in other."""
        raise NotImplementedError
    
    def reverse(self):
        """Reverses self."""
        raise NotImplementedError
    
    def __iter__(self):
        """Iterates over indices contained in self."""
        raise NotImplementedError
    
    def __str__(self):
        """Returns string representation of self."""
        return '(%s,%s)' % (self.Start, self.End)
    
    def __len__(self):
        """Returns length of self."""
        raise NotImplementedError
    
    def __cmp__(self):
        """Compares indices of self with indices of other."""
        raise NotImplementedError
    
    def startsBefore(self, other):
        """Returns True if self starts before other or other.Start."""
        try:
            return self.Start < other.Start
        except AttributeError:
            return self.Start < other
    
    def startsAfter(self, other):
        """Returns True if self starts after other or after other.Start."""
        try:
            return self.Start > other.Start
        except AttributeError:
            return self.Start > other
    
    def startsAt(self, other):
        """Returns True if self starts at the same place as other."""
        try:
            return self.Start == other.Start
        except AttributeError:
            return self.Start == other
    
    def startsInside(self, other):
        """Returns True if self's start in other or equal to other."""
        try:
            return self.Start in other
        except (AttributeError, TypeError):  #count other as empty span
            return False
    
    def endsBefore(self, other):
        """Returns True if self ends before other or other.End."""
        try:
            return self.End < other.End
        except AttributeError:
            return self.End < other
    
    def endsAfter(self, other):
        """Returns True if self ends after other or after other.End."""
        try:
            return self.End > other.End
        except AttributeError:
            return self.End > other
    
    def endsAt(self, other):
        """Returns True if self ends at the same place as other."""
        try:
            return self.End == other.End
        except AttributeError:
            return self.End == other
    
    def endsInside(self, other):
        """Returns True if self's end in other or equal to other."""
        try:
            return self.End in other
        except (AttributeError, TypeError):  #count other as empty span
            return False
    

class Span(SpanI):
    """A contiguous location, not much more than (start, end)
    
    Spans don't even know what map they are on.  The only smarts the class
    has is the ability to slice correctly.  Spans do not expect to be
    reverse-sliced (sl[5,3]) and treat positions as relative to themselves,
    not an underlying sequence (eg sl[:n] == sl[0:n]), so this slicing is
    very different to feature slicing.
    
    Spans may optionaly have a value, which gets preserved when they are remapped etc."""
    
    lost = False
    
    __slots__ = ( 'tidy_start', 'tidy_end', 'length', 'value',
            'Start', 'End', 'Reverse')
    
    def __init__(self, Start, End=None, tidy_start=False, tidy_end=False,
            value=None, Reverse=False):
        self._new_init(Start, End, Reverse)
        self.tidy_start = tidy_start
        self.tidy_end = tidy_end
        self.value = value
        self.length = self.End - self.Start
        assert self.length >= 0
    
    def _new_init(self, Start, End=None, Reverse=False):
        """Returns a new Span object, with Start, End, and Reverse properties.
        
        If End is not supplied, it is set to Start + 1 (providing a 1-element
        range).
        Reverse defaults to False.
        
        This should replace the current __init__ method when deprecated vars
        are removed.
        """
        #special handling in case we were passed another Span
        if isinstance(Start, Span):
            assert End is None
            self.Start, self.End, self.Reverse = Start.Start, Start.End, \
                Start.Reverse
        else:
            #reverse start and end so that start is always first
            if End is None:
                End = Start + 1
            elif Start > End:
                Start, End = End, Start
            
            self.Start = Start
            self.End = End
            self.Reverse = Reverse
    
    def __setstate__(self, args):
         self.__init__(*args)
    
    def __getstate__(self):
        return (self.Start, self.End, self.tidy_start, self.tidy_end, \
        self.value, self.Reverse)
    
    def __repr__(self):
        (start, end) = (self.Start, self.End)
        if self.Reverse:
            (end, start) = (start, end)
        return '%s:%s' % (start, end)
    
    def reversed(self):
        return self.__class__(self.Start, self.End, self.tidy_end, self.tidy_start, self.value, Reverse=not self.Reverse)
    
    def __getitem__(self, slice):
        start,end,step = _norm_slice(slice, self.length)
        assert (step or 1) == 1, slice
        assert start <= end, slice
        tidy_start = self.tidy_start and start==0
        tidy_end = self.tidy_end and end == self.length
        if self.Reverse:
            (Start, End, Reverse) = (self.End-end, self.End-start, True)
        else:
            (Start, End, Reverse) = (self.Start+start, self.Start+end, False)
        return type(self)(Start, End, tidy_start, tidy_end, self.value, Reverse)
    
    def __mul__(self, scale):
        return Span(self.Start * scale, self.End * scale,
                self.tidy_start, self.tidy_end, self.value, self.Reverse)
    
    def __div__(self, scale):
        assert not self.Start % scale or self.End % scale
        return Span(self.Start // scale, self.End // scale,
                self.tidy_start, self.tidy_end, self.value, self.Reverse)
    
    def remapWith(self, map):
        """The list of spans corresponding to this span on its grandparent, ie:
        C is a span of a feature on B which itself is a feature on A, so to
        place C on A return that part of B (map) covered by C (self)"""
        
        (offsets, spans) = (map.offsets, map.spans)
        map_length = offsets[-1] + spans[-1].length
        
        # don't try to remap any non-corresponding end region(s)
        # this won't matter if all spans lie properly within their
        # parent maps, but that might not be true of Display slices.
        (zlo, zhi) = (max(0, self.Start), min(map_length, self.End))
        
        # Find the right span(s) of the map
        first = bisect_right(offsets, zlo) - 1
        last = bisect_left(offsets, zhi, first) -1
        result= spans[first:last+1]
        
        # Cut off something at either end to get
        # the same position and length as 'self'
        if result:
            end_of_last = offsets[last]+spans[last].length
            if end_of_last > zhi:
                result[-1] = result[-1][:result[-1].length-(end_of_last-zhi)]
            if zlo > offsets[first]:
                result[0] = result[0][zlo-offsets[first]:]
        
        # May need to add a bit at either end if the span didn't lie entirely
        # within its parent map (eg: Display slice, inverse of feature map).
        if self.Start < 0:
            result.insert(0, LostSpan(-self.Start))
        if self.End > map_length:
            result.append(LostSpan(self.End-map_length))
        
        # If the ends of self are meaningful then so are the new ends,
        # but not any new internal breaks.
        if result:
            if self.tidy_start:
                result[0].tidy_start = True
            if self.tidy_end:
                result[-1].tidy_end = True
        
        # Deal with case where self is a reverse slice.
        if self.Reverse:
            result = [part.reversed() for part in result]
            result.reverse()
        
        if self.value is not None:
            result = [copy.copy(s) for s in result]
            for s in result: s.value = self.value
        
        return result
    
    def __contains__(self, other):
        """Returns True if other completely contained in self.
        
        other must either be a number or have Start and End properties.
        """
        try:
            return other.Start >= self.Start and other.End <= self.End
        except AttributeError:
            #other is scalar: must be _less_ than self.End,
            #for the same reason that 3 is not in range(3).
            return other >= self.Start and other < self.End
    
    def overlaps(self, other):
        """Returns True if any positions in self are also in other."""
        #remember to subtract 1 from the Ends, since self.End isn't really
        #in self...
        try:
            return (self.Start in other) or (other.Start in self)
        except AttributeError:  #other was probably a number?
            return other in self
    
    def reverse(self):
        """Reverses self."""
        self.Reverse = not self.Reverse
    
    def reversedRelativeTo(self, length):
        """Returns a new span with positions adjusted relative to length. For
        use in reverse complementing of nucleic acids"""
        
        # if reverse complementing, the start becomes the length minus the end
        # position
        start = length - self.End
        assert start >= 0
        end = start + self.length
        return self.__class__(start, end, value = self.value,
                Reverse = not self.Reverse)
    
    def __iter__(self):
        """Iterates over indices contained in self.
        
        NOTE: to make sure that the same items are contained whether going
        through the range in forward or reverse, need to adjust the indices
        by 1 if going backwards.
        """
        if self.Reverse:
            return iter(xrange(self.End-1, self.Start-1, -1))
        else:
            return iter(xrange(self.Start, self.End, 1))
    
    def __str__(self):
        """Returns string representation of self."""
        return '(%s,%s,%s)' % (self.Start, self.End, bool(self.Reverse))
    
    def __len__(self):
        """Returns length of self."""
        return self.End - self.Start
    
    def __cmp__(self, other):
        """Compares indices of self with indices of other."""
        if hasattr(other, 'Start') and hasattr(other, 'End'):
            return cmp(self.Start, other.Start) or cmp(self.End, other.End) \
                or cmp(self.Reverse, other.Reverse)
        else:
            return cmp(type(self), type(other))
    

class _LostSpan(object):
    """A placeholder span which doesn't exist in the underlying sequence"""
    
    __slots__ = ['length', 'value']
    lost = True
    terminal = False
    
    def __init__(self, length, value=None):
        self.length = length
        self.value = value
    
    def __len__(self):
        return self.length
    
    def __setstate__(self, args):
         self.__init__(*args)
    
    def __getstate__(self):
        return (self.length, self.value)
    
    def __repr__(self):
        return '-%s-' % (self.length)
    
    def where(self, index):
        return None
    
    def reversed(self):
        return self
    
    def __getitem__(self, slice):
        (start,end,step) = _norm_slice(slice, self.length)
        assert (step or 1) == 1, slice
        return self.__class__(abs(end-start), self.value)
    
    def __mul__(self, scale):
        return LostSpan(self.length * scale, self.value)
    
    def __div__(self, scale):
        assert not self.length % 3
        return LostSpan(self.length // scale, self.value)
    
    def remapWith(self, map):
        return [self]
    
    def reversedRelativeTo(self, length):
        return self
    

# Save memory by only making one of each small gap
_lost_span_cache = {}
def LostSpan(length, value=None):
    global _lost_span_cache
    if value is None and length < 1000:
        if length not in _lost_span_cache:
            _lost_span_cache[length] = _LostSpan(length, value)
        return _lost_span_cache[length]
    else:
        return _LostSpan(length, value)

class TerminalPadding(_LostSpan):
    terminal = True
    def __repr__(self):
        return '?%s?' % (self.length)
    

class Map(object):
    """A map holds a list of spans.  """
    
    def __init__(self, locations=None, spans=None, tidy=False,
            parent_length=None, termini_unknown=False):
        assert parent_length is not None
        if spans is None:
            spans = []
            for (start, end) in locations:
                diff = 0
                reverse = start > end
                if max(start, end) < 0 or min(start, end) > parent_length:
                    raise RuntimeError("located outside sequence: %s" % \
                                    str((start, end, parent_length)))
                elif max(start, end) < 0:
                    diff = min(start, end)
                    start = [start, 0][start < 0]
                    end = [end, 0][end < 0]
                elif min(start, end) > parent_length:
                    diff = max(start, end) - parent_length
                    start = [start, parent_length][start > parent_length]
                    end = [end, parent_length][end > parent_length]
                
                span = Span(start, end, tidy, tidy, Reverse=reverse)
                if diff < 0:
                    spans += [LostSpan(-diff), span]
                elif diff > 0:
                    spans += [span, LostSpan(diff)]
                else:
                    spans += [span]
        
        self.offsets = []
        self.useful = False
        self.complete = True
        self.Reverse = None
        posn = 0
        for span in spans:
            self.offsets.append(posn)
            posn += span.length
            if span.lost:
                self.complete = False
            elif not self.useful:
                self.useful = True
                (self.Start, self.End) = (span.Start, span.End)
                self.Reverse = span.Reverse
            else:
                self.Start = min(self.Start, span.Start)
                self.End = max(self.End, span.End)
                if self.Reverse is not None and (span.Reverse != self.Reverse):
                    self.Reverse = None
        
        if termini_unknown:
            if spans[0].lost:
                spans[0] = TerminalPadding(spans[0].length)
            if spans[-1].lost:
                spans[-1] = TerminalPadding(spans[-1].length)
        
        self.spans = spans
        self.length = posn
        self.parent_length = parent_length
        self.__inverse = None
    
    def __len__(self):
        return self.length
    
    def __repr__(self):
        return repr(self.spans) + '/%s' % self.parent_length
    
    def __getitem__(self, slice):
        # A possible shorter map at the same level
        slice = as_map(slice, len(self))
        new_parts = []
        for span in slice.spans:
            new_parts.extend(span.remapWith(self))
        return Map(spans=new_parts, parent_length=self.parent_length)
    
    def __mul__(self, scale):
        # For Protein -> DNA
        new_parts = []
        for span in self.spans:
            new_parts.append(span * scale)
        return Map(spans=new_parts, parent_length=self.parent_length*scale)
    
    def __div__(self, scale):
        # For DNA -> Protein
        new_parts = []
        for span in self.spans:
            new_parts.append(span / scale)
        return Map(spans=new_parts, parent_length=self.parent_length // scale)
    
    def withTerminiUnknown(self):
        return Map(self, spans=self.spans[:],
                parent_length=self.parent_length,
                termini_unknown = True)
    
    def getCoveringSpan(self):
        if self.Reverse == -1:
            span = (self.End, self.Start)
        else:
            span = (self.Start, self.End)
        return Map([span], parent_length=self.parent_length)
    
    def covered(self):
        """>>> Map([(10,20), (15, 25), (80, 90)]).covered().spans
        [Span(10,25), Span(80, 90)]"""
        
        delta = {}
        for span in self.spans:
            if span.lost:
                continue
            delta[span.Start] = delta.get(span.Start, 0) + 1
            delta[span.End] = delta.get(span.End, 0) - 1
        positions = delta.keys()
        positions.sort()
        last_y = y = 0
        last_x = start = None
        result = []
        for x in positions:
            y += delta[x]
            if x == last_x:
                continue
            if y and not last_y:
                assert start is None
                start = x
            elif last_y and not y:
                result.append((start, x))
                start = None
            last_x = x
            last_y = y
        assert y == 0
        return Map(result, parent_length=self.parent_length)
    
    def reversed(self):
        spans = [s.reversed() for s in self.spans]
        spans.reverse()
        return Map(spans=spans, parent_length=self.parent_length)
    
    def nucleicReversed(self):
        """reverses spans according to concept of nucleic acid sequence
        reverse complement"""
        spans = [s.reversedRelativeTo(self.parent_length) for s in self.spans]
        return Map(spans=spans, parent_length=self.parent_length)
    
    def gaps(self):
        """The gaps (lost spans) in this map"""
        locations = []
        offset = 0
        for s in self.spans:
            if s.lost:
                locations.append((offset, offset+s.length))
            offset += s.length
        return Map(locations, parent_length=len(self))
    
    def shadow(self):
        """The 'negative' map of the spans not included in this map"""
        return self.inverse().gaps()
    
    def nongap(self):
        locations = []
        offset = 0
        for s in self.spans:
            if not s.lost:
                locations.append((offset, offset+s.length))
            offset += s.length
        return Map(locations, parent_length=len(self))
    
    def withoutGaps(self):
        return Map(
                spans = [s for s in self.spans if not s.lost],
                parent_length = self.parent_length)
    
    def inverse(self):
        if self.__inverse is None:
            self.__inverse = self._inverse()
        return self.__inverse
    
    def _inverse(self):
        # can't work if there are overlaps in the map
        # tidy ends don't survive inversion
        if self.parent_length is None:
            raise ValueError("Uninvertable. Parent length not known")
        posn = 0
        temp = []
        for span in self.spans:
            if not span.lost:
                if span.Reverse:
                    temp.append((span.Start, span.End, posn+span.length, posn))
                else:
                    temp.append((span.Start, span.End, posn, posn+span.length))
            posn += span.length
        
        temp.sort()
        new_spans = []
        last_hi = 0
        for (lo, hi, start, end) in temp:
            if lo > last_hi:
                new_spans.append(LostSpan(lo-last_hi))
            elif lo < last_hi:
                raise ValueError, "Uninvertable. Overlap: %s < %s" % (lo, last_hi)
            new_spans.append(Span(start, end, Reverse=start>end))
            last_hi = hi
        if self.parent_length > last_hi:
            new_spans.append(LostSpan(self.parent_length-last_hi))
        return Map(spans=new_spans, parent_length=len(self))
    

class SpansOnly(ConstrainedList):
    """List that converts elements to Spans on addition."""
    Mask = FunctionWrapper(Span)
    _constraint = ClassChecker(Span)

class Range(SpanI):
    """Complex object consisting of many spans."""
    
    def __init__(self, Spans=[]):
        """Returns a new Range object with data in Spans.
        """
        result = SpansOnly()
        #need to check if we got a single Span, since they define __iter__.
        if isinstance(Spans, Span):
            result.append(Spans)
        elif hasattr(Spans, 'Spans'):   #probably a single range object?
            result.extend(Spans.Spans)
        else:
            for s in iterable(Spans):
                if hasattr(s, 'Spans'):
                  result.extend(s.Spans)
                else:
                    result.append(s)
        self.Spans = result
    
    def __str__(self):
        """Returns string representation of self."""
        return '(%s)' % ','.join(map(str, self.Spans))
    
    def __len__(self):
        """Returns sum of span lengths.
        
        NOTE: if spans overlap, will count multiple times. Use reduce() to
        get rid of overlaps.
        """
        return sum(map(len, self.Spans))
    
    def __cmp__(self, other):
        """Compares spans of self with indices of other."""
        if hasattr(other, 'Spans'):
            return cmp(self.Spans, other.Spans)
        elif len(self.Spans) == 1 and hasattr(other, 'Start') and \
            hasattr(other, 'End'):
            return cmp(self.Spans[0].Start, other.Start) or \
                cmp(self.Spans[0].End, other.End)
        else:
            return object.__cmp__(self, other)
    
    def _get_start(self):
        """Finds earliest start of items in self.Spans."""
        return min([i.Start for i in self.Spans])
    Start = property(_get_start)
    
    def _get_end(self):
        """Finds latest end of items in self.Spans."""
        return max([i.End for i in self.Spans])
    End = property(_get_end)
    
    def _get_reverse(self):
        """Reverse is True if any piece is reversed."""
        for i in self.Spans:
            if i.Reverse:
                return True
        return False
    Reverse = property(_get_reverse)
    
    def reverse(self):
        """Reverses all spans in self."""
        for i in self.Spans:
            i.reverse()
    
    def __contains__(self, other):
        """Returns True if other completely contained in self.
        
        other must either be a number or have Start and End properties.
        """
        if hasattr(other, 'Spans'):
            for curr in other.Spans:
                found = False
                for i in self.Spans:
                    if curr in i:
                        found = True
                        break
                if not found:
                    return False
            return True
        else:
            for i in self.Spans:
                if other in i:
                    return True
            return False
    
    def overlaps(self, other):
        """Returns True if any positions in self are also in other."""
        if hasattr(other, 'Spans'):
            for i in self.Spans:
                for j in other.Spans:
                    if i.overlaps(j):
                        return True
        else:
            for i in self.Spans:
                if i.overlaps(other):
                    return True
        return False
    
    def overlapsExtent(self, other):
        """Returns True if any positions in self's extent also in other's."""
        if hasattr(other, 'Extent'):
            return self.Extent.overlaps(other.Extent)
        else:
            return self.Extent.overlaps(other)
    
    def sort(self):
        """Sorts the spans in self."""
        self.Spans.sort()
    
    def __iter__(self):
        """Iterates over indices contained in self."""
        return chain(*[iter(i) for i in self.Spans])
    
    def _get_extent(self):
        """Returns Span object representing the extent of self."""
        return Span(self.Start, self.End)
    Extent = property(_get_extent)
    
    def simplify(self):
        """Reduces the spans in self in-place to get fewest spans.
        
        Will not condense spans with opposite directions.
        
        Will condense adjacent but nonoverlapping spans (e.g. (1,3) and (4,5)).
        """
        forward = []
        reverse = []
        spans = self.Spans[:]
        spans.sort()
        for span in spans:
            if span.Reverse:
                direction = reverse
            else:
                direction = forward
            
            found_overlap = False
            for other in direction:
                if span.overlaps(other) or (span.Start == other.End) or \
                    (other.Start == span.End):  #handle adjacent spans also
                    other.Start = min(span.Start, other.Start)
                    other.End = max(span.End, other.End)
                    found_overlap = True
                    break
            if not found_overlap:
                direction.append(span)
        self.Spans[:] = forward + reverse
    

class Point(Span):
    """Point is a special case of Span, where Start always equals End.
    
    Note that, as per Python standard, a point is _between_ two elements
    in a sequence. In other words, a point does not contain any elements.
    If you want a single element, use a Span where End = Start + 1.
    
    A Point does have a direction (i.e. a Reverse property) to indicate
    where successive items would go if it were expanded.
    """
    
    def __init__(self, Start, Reverse=False):
        """Returns new Point object."""
        self.Reverse = Reverse
        self._start = Start
    
    def _get_start(self):
        """Returns self.Start."""
        return self._start
    
    def _set_start(self, Start):
        """Sets self.Start and self.End."""
        self._start = Start
    
    Start = property(_get_start, _set_start)
    End = Start     #start and end are synonyms for the same property

def RangeFromString(string, delimiter=','):
    """Returns Range object from string of the form 1-5,11,20,30-50.
    
    Ignores whitespace; expects values to be comma-delimited and positive.
    """
    result = Range()
    pairs = map(strip, string.split(delimiter))
    for p in pairs:
        if not p:   #adjacent delimiters?
            continue
        if '-' in p:    #treat as pair
            first, second = p.split('-')
            result.Spans.append(Span(int(first), int(second)))
        else:
            result.Spans.append(Span(int(p)))
    return result