Class: Bio::Location

Inherits:
Object show all
Includes:
Comparable
Defined in:
lib/bio/location.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location = nil) ⇒ Location

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:

  • (required) str: GenBank style position string (see Bio::Locations documentation)

Returns

the Bio::Location object

[View source]

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
95
96
97
# File 'lib/bio/location.rb', line 45

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
    carat = true
    if e - s != 1 or e != 1 # assert n^n+1 or n^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
  @carat      = carat         # true if the location indicates the site
                              # between two adjoining nucleotides
end

Instance Attribute Details

#caratObject

(true, false or nil) true if the location indicates the site between two adjoining nucleotides


122
123
124
# File 'lib/bio/location.rb', line 122

def carat
  @carat
end

#fromObject

(Integer) start position of the location


100
101
102
# File 'lib/bio/location.rb', line 100

def from
  @from
end

#gtObject

(true, false or nil) true if the position contains ‘>’


115
116
117
# File 'lib/bio/location.rb', line 115

def gt
  @gt
end

#ltObject

(true, false or nil) true if the position contains ‘<’


112
113
114
# File 'lib/bio/location.rb', line 112

def lt
  @lt
end

#sequenceObject

(String) literal sequence of the location


109
110
111
# File 'lib/bio/location.rb', line 109

def sequence
  @sequence
end

#strandObject

(Integer) strand direction of the location (forward => 1 or complement => -1)


106
107
108
# File 'lib/bio/location.rb', line 106

def strand
  @strand
end

#toObject

(Integer) end position of the location


102
103
104
# File 'lib/bio/location.rb', line 102

def to
  @to
end

#xref_idObject

(String) link to the external entry as GenBank ID


118
119
120
# File 'lib/bio/location.rb', line 118

def xref_id
  @xref_id
end

Instance Method Details

#<=>(other) ⇒ Object

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:

  • (required) _other location_: a Bio::Location object

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

[View source]

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/bio/location.rb', line 163

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

#complementObject

Complements the sequence location (i.e. alternates the strand). Note that it is destructive method (i.e. modifies itself), but it does not modify the “sequence” attribute.


Returns

the Bio::Location object

[View source]

129
130
131
132
# File 'lib/bio/location.rb', line 129

def complement
  @strand *= -1
  self					# return Location object
end

#rangeObject

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

[View source]

146
147
148
# File 'lib/bio/location.rb', line 146

def range
  @from..@to
end

#replace(sequence) ⇒ Object

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

[View source]

140
141
142
143
# File 'lib/bio/location.rb', line 140

def replace(sequence)
  @sequence = sequence.downcase
  self					# return Location object
end