Skip to content

Commit

Permalink
rdoc comments preprared
Browse files Browse the repository at this point in the history
  • Loading branch information
misshie committed Apr 7, 2011
1 parent 2882c8e commit 9c7c290
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
16 changes: 16 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,27 @@ Last one is generated from a "Zero-based half-closed[start, end)" interval
ref.adjacent = 1
ref.compare(cmp) # => :right_off

== Overlap metrics
* 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.

ref = Bio::GenomicInterval.parse("chr1:10-20")
cmp = Bio::GenomicInterval.parse("chr1:15-25")
ref.overlap(cmp) # => 6
cmp2 = Bio::GenomicInterval.parse("chr1:25-35")
ref.overlap(cmp) # => -4

== Expansion (or integration)
ref = Bio::GenomicInterval.parse("chr1:400-600")
other = Bio::GenomicInterval.parse("chr1:650-800")
ref.expand(other).to_s # => "chr1:400-800"

== Center
obj1 = Bio::GenomicInterval.parse("chr1:1-3")
obj1.center # => "chr1:2-2"
obj2 = Bio::GenomicInterval.parse("chr2:1-4")
obj2.center # => "chr1:2-2"

== and others
See RDoc and Rspec specification.

Expand Down
36 changes: 32 additions & 4 deletions lib/bio-genomic-interval.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Bio

#
# =a class for manupirate genomic intervals such as "chr1:123-456"
#
class GenomicInterval
# a default value to determine the adjacent/very near distance in bp
DEFAULT_ADJACENT = 20

def initialize(chrom = "", chr_start = 1, chr_end = 1)
Expand All @@ -15,6 +19,10 @@ def initialize(chrom = "", chr_start = 1, chr_end = 1)

attr_accessor :chrom, :chr_start, :chr_end, :adjacent

#
# generate an interval object from a string expressing
# one-based full-closed interval such as "chr1:123-456"
#
def self.parse(interval)
chrom, start_end = interval.split(":")
str_start, str_end = start_end.split("-")[0..1]
Expand All @@ -26,24 +34,44 @@ def self.parse(interval)
end
self.new(chrom, chr_start, chr_end)
end


#
# generate an interval object from three atguments expressing
# zero-based half-closed interval such as "chr1", 122, 456
#
def self.zero_based(chrom = "", z_start = 0, z_end = 1)
z_start += 1
self.new(chrom, z_start, z_end)
end

#
# returns one-based full-closed interval such as "chr1:123-456"
#
def to_s
"#{@chrom}:#{@chr_start}-#{@chr_end}"
end

#
# returns zero-based half-closed start position
#
def zero_start
@chr_start - 1
end

#
# returns zero-based half-closed end position
#
def zero_end
@chr_end
end

#
# returns one of the followings:
# :different_chrom, :left_adjacent, :right_adjacent
# :left_off, :right_off, :equal
# :contained, :containing, :left_overlapped, :right_overlapped
# Imagine that the receiver object is fixed on a number line
#
def compare(other)
case
when self.chrom != other.chrom
Expand Down Expand Up @@ -107,9 +135,9 @@ def center
Bio::GenomicInterval.new(self.chrom, center, center)
end

# 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
# * 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.
#
def overlap(other)
case self.compare(other)
when :different_chrom
Expand Down

0 comments on commit 9c7c290

Please sign in to comment.