Module: IntervalList::Interval

Included in:
GenomicLocus
Defined in:
lib/intervals.rb

Instance Method Summary collapse

Instance Method Details

#above?(interval) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/intervals.rb', line 30

def above? interval
  start > interval.stop
end

#below?(interval) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/intervals.rb', line 26

def below? interval
  stop < interval.start
end

#centerObject



84
85
86
# File 'lib/intervals.rb', line 84

def center
  (stop + start)/2.0
end

#clone {|c| ... } ⇒ Object

this interface needs to implement :seqname, :start, :stop, and :copy

Yields:

  • (c)


20
21
22
23
24
# File 'lib/intervals.rb', line 20

def clone
  c = copy
  yield c if block_given?
  return c
end

#contains?(interval) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/intervals.rb', line 38

def contains? interval
  if interval.is_a? Numeric
    start <= interval && stop >= interval
  else
    seqname == interval.seqname && start <= interval.start && stop >= interval.stop
  end
end

#diff(interval) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/intervals.rb', line 56

def diff interval
  ol = overlap interval
  if !ol
    return yield([ self ])
  end
    
  ints = []
  if ol.start > start
    ints.push(clone { |c| c.start = start; c.stop = ol.start-1 })
  end
  if ol.stop < stop
    ints.push(clone { |c| c.start = ol.stop+1; c.stop = stop })
  end
  return yield(ints)
end

#dist(interval) ⇒ Object



88
89
90
# File 'lib/intervals.rb', line 88

def dist interval
  (center-interval.center).abs
end

#intersect(interval) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/intervals.rb', line 46

def intersect interval
  return nil if !overlaps? interval

  clone do |c|
    c.seqname = seqname,
    c.start = [ interval.start, start ].max
    c.stop = [ interval.stop, stop ].min
  end
end

#overlaps?(interval) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/intervals.rb', line 34

def overlaps? interval
  seqname == interval.seqname && !below?(interval) && !above?(interval)
end

#sizeObject



80
81
82
# File 'lib/intervals.rb', line 80

def size
  stop - start + 1
end

#union(interval) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/intervals.rb', line 72

def union interval
  return nil unless interval && overlaps?(interval)
  clone do |c|
    c.start = [ interval.start, start ].min
    c.stop = [ interval.stop, stop ].max
  end
end