Method: Bio::Location#initialize

Defined in:
lib/bio/location.rb

#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



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