Class Bio::Location
In: lib/bio/location.rb  (CVS)
Parent: Object

Description

The Bio::Location class describes the position of a genomic locus. Typically, Bio::Location objects are created automatically when the user creates a Bio::Locations object, instead of initialized directly.

Usage

  location = Bio::Location.new('500..550')
  puts "start=" + location.from.to_s + ";end=" + location.to.to_s

  #, or better: through Bio::Locations
  locations = Bio::Locations.new('500..550')
  locations.each do |location|
    puts "start=" + location.from.to_s + ";end=" + location.to.to_s
  end

Methods

<=>   complement   new   range   replace  

Included Modules

Comparable

Attributes

from  [RW] 
gt  [RW] 
lt  [RW] 
sequence  [RW] 
strand  [RW] 
to  [RW] 
xref_id  [RW] 

Public Class methods

Parses a‘location’ segment, which can be ‘ID:’ + (‘n’ or ‘n..m’ or ‘n^m’ or "seq") with ’<’ or ’>’, and returns a Bio::Location object.

  location = Bio::Location.new('500..550')

Arguments:

Returns:the Bio::Location object

[Source]

# File lib/bio/location.rb, line 44
  def initialize(location = nil)

    if location
      if location =~ /:/                                # (G) ID:location
        xref_id, location = location.split(':')
      end
      if location =~ /</                                # (I) <,>
        lt = true
      end
      if location =~ />/
        gt = true
      end
    end

    # s : start base, e : end base => from, to
    case location
    when /^[<>]?(\d+)$/                                 # (A, I) n
      s = e = $1.to_i
    when /^[<>]?(\d+)\.\.[<>]?(\d+)$/                   # (B, I) n..m
      s = $1.to_i
      e = $2.to_i
      if e - s < 0
#       raise "Error: invalid range : #{location}"
        $stderr.puts "[Warning] invalid range : #{location}" if $DEBUG
      end
    when /^[<>]?(\d+)\^[<>]?(\d+)$/                     # (C, I) n^m
      s = $1.to_i
      e = $2.to_i
      if e - s != 1
#       raise "Error: invalid range : #{location}"
        $stderr.puts "[Warning] invalid range : #{location}" if $DEBUG
      end
    when /^"?([ATGCatgc]+)"?$/                  # (H) literal sequence
      sequence = $1.downcase
      s = e = nil
    when nil
      ;
    else
      raise "Error: unknown location format : #{location}"
    end

    @from       = s             # start position of the location
    @to         = e             # end position of the location
    @strand     = 1             # strand direction of the location
                                #   forward => 1 or complement => -1
    @sequence   = sequence      # literal sequence of the location
    @lt         = lt            # true if the position contains '<'
    @gt         = gt            # true if the position contains '>'
    @xref_id    = xref_id       # link to the external entry as GenBank ID
  end

Public Instance methods

Check where a Bio::Location object is located compared to another Bio::Location object (mainly to facilitate the use of Comparable). A location A is upstream of location B if the start position of location A is smaller than the start position of location B. If they‘re the same, the end positions are checked.


Arguments:

Returns::

  • 1 if self < other location
  • -1 if self > other location
  • 0 if both location are the same
  • nil if the argument is not a Bio::Location object

[Source]

# File lib/bio/location.rb, line 134
  def <=>(other)
    if ! other.kind_of?(Bio::Location)
      return nil
    end

    if @from.to_f < other.from.to_f
      return -1
    elsif @from.to_f > other.from.to_f
      return 1
    end

    if @to.to_f < other.to.to_f
      return -1
    elsif @to.to_f > other.to.to_f
      return 1
    end
    return 0
  end

Complements the sequence (i.e. alternates the strand).


Returns:the Bio::Location object

[Source]

# File lib/bio/location.rb, line 100
  def complement
    @strand *= -1
    self                                        # return Location object
  end

Returns the range (from..to) of the location as a Range object.

[Source]

# File lib/bio/location.rb, line 117
  def range
    @from..@to
  end

Replaces the sequence of the location.


Arguments:

  • (required) sequence: sequence to be used to replace the sequence at the location
Returns:the Bio::Location object

[Source]

# File lib/bio/location.rb, line 111
  def replace(sequence)
    @sequence = sequence.downcase
    self                                        # return Location object
  end

[Validate]