Class: Bio::GenomicInterval

Inherits:
Object
  • Object
show all
Defined in:
lib/bio-genomic-interval.rb

Overview

a class for manupirate genomic intervals such as “chr1:123-456”

Constant Summary collapse

DEFAULT_ADJACENT =

a default value to determine the adjacent/very near distance in bp

20

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chrom = "", chr_start = 1, chr_end = 1) ⇒ GenomicInterval

Returns a new instance of GenomicInterval.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
# File 'lib/bio-genomic-interval.rb', line 12

def initialize(chrom = "", chr_start = 1, chr_end = 1)
  raise ArgumentError unless chr_start >= 1
  raise ArgumentError unless chr_end >= 1
  raise ArgumentError unless chr_start <= chr_end
  @chrom = chrom
  @chr_start = chr_start
  @chr_end = chr_end
  @adjacent = DEFAULT_ADJACENT
end

Instance Attribute Details

#adjacentObject

Returns the value of attribute adjacent.



10
11
12
# File 'lib/bio-genomic-interval.rb', line 10

def adjacent
  @adjacent
end

#chr_endObject

Returns the value of attribute chr_end.



10
11
12
# File 'lib/bio-genomic-interval.rb', line 10

def chr_end
  @chr_end
end

#chr_startObject

Returns the value of attribute chr_start.



10
11
12
# File 'lib/bio-genomic-interval.rb', line 10

def chr_start
  @chr_start
end

#chromObject

Returns the value of attribute chrom.



10
11
12
# File 'lib/bio-genomic-interval.rb', line 10

def chrom
  @chrom
end

Class Method Details

.parse(interval) ⇒ Object

generate an interval object from a string expressing one-based full-closed interval such as “chr1:123-456”



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bio-genomic-interval.rb', line 24

def self.parse(interval)
  chrom, start_end = interval.split(":")
  str_start, str_end = start_end.split("-")[0..1]
  str_end = str_start if str_end.nil?
  chr_start = Integer(str_start.gsub(",",""))
  chr_end   = Integer(str_end.gsub(",",""))
  if chr_start > chr_end
    chr_end, chr_start = chr_start, chr_end
  end
  self.new(chrom, chr_start, chr_end)
end

.zero_based(chrom = "", z_start = 0, z_end = 1) ⇒ Object

generate an interval object from three atguments expressing zero-based half-closed interval such as “chr1”, 122, 456



38
39
40
41
42
# File 'lib/bio-genomic-interval.rb', line 38

def self.zero_based(chrom = "", z_start = 0, z_end = 1)
  z_end += 1 if (z_start == z_end)
  z_start += 1
  self.new(chrom, z_start, z_end)
end

Instance Method Details

#centerObject



122
123
124
125
# File 'lib/bio-genomic-interval.rb', line 122

def center
  center = (chr_start + chr_end) / 2
  Bio::GenomicInterval.new(self.chrom, center, center)
end

#compare(other) ⇒ Object

returns one of the followings:

:different_chrom, :left_adjacent, :right_adjacent
:left_off, :right_off, :equal
:contained_by, :contains, :left_overlapped, :right_overlapped

Imagine that the receiver object is fixed on a number line



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
# File 'lib/bio-genomic-interval.rb', line 64

def compare(other)
  case
  when self.chrom != other.chrom
    :different_chrom
  when other.chr_end.between?(self.chr_start - @adjacent, self.chr_start - 1)
    :left_adjacent
  when other.chr_start.between?(self.chr_end + 1, self.chr_end + @adjacent)
    :right_adjacent
  when other.chr_end < self.chr_start
    :left_off
  when self.chr_end < other.chr_start
    :right_off
  when (self.chr_start == other.chr_start) &&
      (self.chr_end == other.chr_end)
    :equal
  when (other.chr_start.between?(self.chr_start, self.chr_end)) &&
      (other.chr_end.between?(self.chr_start, self.chr_end))
    :contained_by
  when (self.chr_start.between?(other.chr_start, other.chr_end)) &&
      (self.chr_end.between?(other.chr_start, other.chr_end))
    :contains
  when (other.chr_start < self.chr_start) &&
      (other.chr_end.between?(self.chr_start, self.chr_end))
    :left_overlapped
  when (other.chr_start.between?(self.chr_start, self.chr_end)) &&
      (self.chr_end < other.chr_end)
    :right_overlapped
  else
    raise Exception, "must not happen"
  end
end

#expand(other) ⇒ Object

Raises:

  • (ArgumentError)


109
110
111
112
113
114
# File 'lib/bio-genomic-interval.rb', line 109

def expand(other)
  raise ArgumentError unless self.chrom == other.chrom
  new_start = [self.chr_start, other.chr_start].min
  new_end = [self.chr_end, other.chr_end].max
  Bio::GenomicInterval.new(@chrom, new_start, new_end)
end

#nearly_overlapped?(other) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
# File 'lib/bio-genomic-interval.rb', line 96

def nearly_overlapped?(other)
  result = compare(other)
  [ :left_adjacent, :right_adjacent,
    :equal, :contained_by, :contains,
    :left_overlapped, :right_overlapped].any?{|x| x == result} 
end

#overlap(other) ⇒ Object

  • When a overlap exist, return a positive integers (>1) for the overlap length.

  • When a overlap does not exist, return a zero or a negative (<= 0) for the space size between the intervals.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/bio-genomic-interval.rb', line 129

def overlap(other)
  case self.compare(other)
  when :different_chrom
    0
  when :left_off, :left_adjacent, :left_overlapped
    other.chr_end - self.chr_start + 1
  when :contained_by, :equal
    other.size
  when :contains
    self.size
  when :right_off, :right_adjacent, :right_overlapped
    self.chr_end - other.chr_start + 1
  else
    raise Exception, "must not happen"
  end
end

#overlapped?(other) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
107
# File 'lib/bio-genomic-interval.rb', line 103

def overlapped?(other)
  result = compare(other)
  [ :equal, :contained_by, :contains,
    :left_overlapped, :right_overlapped].any?{|x| x == result} 
end

#sizeObject Also known as: length



116
117
118
# File 'lib/bio-genomic-interval.rb', line 116

def size
  chr_end - chr_start + 1
end

#to_sObject

returns one-based full-closed interval such as “chr1:123-456”



45
46
47
# File 'lib/bio-genomic-interval.rb', line 45

def to_s
  "#{@chrom}:#{@chr_start}-#{@chr_end}"
end

#zero_endObject

returns zero-based half-closed end position



55
56
57
# File 'lib/bio-genomic-interval.rb', line 55

def zero_end
  @chr_end
end

#zero_startObject

returns zero-based half-closed start position



50
51
52
# File 'lib/bio-genomic-interval.rb', line 50

def zero_start
  @chr_start - 1
end