Class: Bio::GenomicInterval
- Inherits:
-
Object
- Object
- Bio::GenomicInterval
- 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
-
#adjacent ⇒ Object
Returns the value of attribute adjacent.
-
#chr_end ⇒ Object
Returns the value of attribute chr_end.
-
#chr_start ⇒ Object
Returns the value of attribute chr_start.
-
#chrom ⇒ Object
Returns the value of attribute chrom.
Class Method Summary collapse
-
.parse(interval) ⇒ Object
generate an interval object from a string expressing one-based full-closed interval such as “chr1:123-456”.
-
.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.
Instance Method Summary collapse
- #center ⇒ Object
-
#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.
- #expand(other) ⇒ Object
-
#initialize(chrom = "", chr_start = 1, chr_end = 1) ⇒ GenomicInterval
constructor
A new instance of GenomicInterval.
- #nearly_overlapped?(other) ⇒ Boolean
-
#overlap(other) ⇒ Object
-
When a overlap exist, return a positive integers (>1) for the overlap length.
-
- #overlapped?(other) ⇒ Boolean
- #size ⇒ Object (also: #length)
-
#to_s ⇒ Object
returns one-based full-closed interval such as “chr1:123-456”.
-
#zero_end ⇒ Object
returns zero-based half-closed end position.
-
#zero_start ⇒ Object
returns zero-based half-closed start position.
Constructor Details
#initialize(chrom = "", chr_start = 1, chr_end = 1) ⇒ GenomicInterval
Returns a new instance of GenomicInterval.
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
#adjacent ⇒ Object
Returns the value of attribute adjacent.
10 11 12 |
# File 'lib/bio-genomic-interval.rb', line 10 def adjacent @adjacent end |
#chr_end ⇒ Object
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_start ⇒ Object
Returns the value of attribute chr_start.
10 11 12 |
# File 'lib/bio-genomic-interval.rb', line 10 def chr_start @chr_start end |
#chrom ⇒ Object
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
#center ⇒ Object
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
109 110 111 112 113 114 |
# File 'lib/bio-genomic-interval.rb', line 109 def (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
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
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 |
#size ⇒ Object Also known as: length
116 117 118 |
# File 'lib/bio-genomic-interval.rb', line 116 def size chr_end - chr_start + 1 end |
#to_s ⇒ Object
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_end ⇒ Object
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_start ⇒ Object
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 |