Class: Wrnap::Global::Rna::Context

Inherits:
Wrnap::Global::Rna show all
Defined in:
lib/wrnap/global/rna/context.rb

Instance Attribute Summary collapse

Attributes inherited from Wrnap::Global::Rna

#comment, #second_structure, #structure

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Wrnap::Global::Rna

#==, #empty_structure, init_from_array, init_from_context, init_from_fasta, init_from_hash, init_from_self, #method_missing, #no_structure, #one_structure, #run, #temp_fa_file!, #two_structures, #write_fa!

Methods included from Extensions

included

Constructor Details

#initialize(sequence: nil, accession: nil, from: nil, to: nil, options: {}) ⇒ Context

Returns a new instance of Context.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/wrnap/global/rna/context.rb', line 28

def initialize(sequence: nil, accession: nil, from: nil, to: nil, options: {})
  options = { coords: {}, rna: {} }.merge(options)

  @accession, @from, @to, @coord_options = accession, from, to, options[:coords]

  validate_coord_options

  if sequence
    @raw_sequence = (sequence.is_a?(String) ? Bio::Sequence::NA.new(sequence) : sequence).upcase
  end

  super({
    sequence:         self.sequence,
    structure:        options[:rna][:structure]        || options[:rna][:str_1] || options[:rna][:str],
    second_structure: options[:rna][:second_structure] || options[:rna][:str_2],
    comment:          options[:rna][:comment]          || options[:rna][:name] || identifier
  })

  remove_instance_variable(:@sequence)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Wrnap::Global::Rna

Instance Attribute Details

#accessionObject (readonly)

Returns the value of attribute accession.



5
6
7
# File 'lib/wrnap/global/rna/context.rb', line 5

def accession
  @accession
end

#coord_optionsObject (readonly)

Returns the value of attribute coord_options.



5
6
7
# File 'lib/wrnap/global/rna/context.rb', line 5

def coord_options
  @coord_options
end

#fromObject (readonly)

Returns the value of attribute from.



5
6
7
# File 'lib/wrnap/global/rna/context.rb', line 5

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



5
6
7
# File 'lib/wrnap/global/rna/context.rb', line 5

def to
  @to
end

Class Method Details

.init_from_entrez(accession, from, to, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/wrnap/global/rna/context.rb', line 8

def init_from_entrez(accession, from, to, options = {})
  new(
    accession: accession,
    from:      from,
    to:        to,
    options:   options
  )
end

.init_from_string(sequence, accession, from, to, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/wrnap/global/rna/context.rb', line 17

def init_from_string(sequence, accession, from, to, options = {})
  new(
    sequence:  sequence,
    accession: accession,
    from:      from,
    to:        to,
    options:   options
  )
end

Instance Method Details

#coord_windowObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/wrnap/global/rna/context.rb', line 108

def coord_window
  # This does not support extending the range in both directions, though it should be easy to do.
  # Options from coord_options ex: { length: 300, direction: 3 }, { length: 250, direction: :both }, { length: 200, direction: :down }
  range = 0..(down_coord - up_coord)

  if coord_options[:length] && coord_options[:direction]
    if coord_options[:direction] == :both
      Range.new(range.min - coord_options[:length], range.max + coord_options[:length])
    else
      case [coord_options[:direction], strand]
      when [3, :plus], [:down, :plus], [5, :minus], [:up, :minus] then Range.new(range.min, range.max + coord_options[:length])
      when [5, :plus], [:up, :plus], [3, :minus], [:down, :minus] then Range.new(range.min - coord_options[:length], range.max)
      else Wrnap.debugger { "WARNING: value for :direction key in sequence retreival needs to be one of 5, 3, :both - found (%s)" % coord_options[:direction].inspect }
      end
    end
  else
    range
  end
end

#down_coordObject



69
70
71
# File 'lib/wrnap/global/rna/context.rb', line 69

def down_coord
  [from, to].max
end

#extend!(coord_options = {}) ⇒ Object



104
105
106
# File 'lib/wrnap/global/rna/context.rb', line 104

def extend!(coord_options = {})
  self.class.init_from_entrez(accession, from, to, coords: coord_options)
end

#identifierObject



128
129
130
# File 'lib/wrnap/global/rna/context.rb', line 128

def identifier
  "%s %d %s %d" % [accession, from, plus_strand? ? ?+ : ?-, to]
end

#inspectObject



132
133
134
# File 'lib/wrnap/global/rna/context.rb', line 132

def inspect
  super.gsub(/((\w(::)?)+)>$/) { |_| "%s %s>" % [identifier, $1] }
end

#minus_strand?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/wrnap/global/rna/context.rb', line 89

def minus_strand?
  !plus_strand?
end

#plus_strand?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/wrnap/global/rna/context.rb', line 85

def plus_strand?
  to > from
end

#seq_fromObject



73
74
75
# File 'lib/wrnap/global/rna/context.rb', line 73

def seq_from
  up_coord + coord_window.min
end

#seq_toObject



77
78
79
# File 'lib/wrnap/global/rna/context.rb', line 77

def seq_to
  up_coord + coord_window.max
end

#sequenceObject Also known as: seq



93
94
95
96
97
98
99
100
# File 'lib/wrnap/global/rna/context.rb', line 93

def sequence
  if @raw_sequence
    @raw_sequence
  else
    entrez_sequence = Entrez.rna_sequence_from_entrez(accession, up_coord, coord_window)
    @raw_sequence   = (minus_strand? ? entrez_sequence.complement : entrez_sequence).upcase
  end
end

#strandObject



81
82
83
# File 'lib/wrnap/global/rna/context.rb', line 81

def strand
  plus_strand? ? :plus : :minus
end

#up_coordObject



65
66
67
# File 'lib/wrnap/global/rna/context.rb', line 65

def up_coord
  [from, to].min
end

#validate_coord_optionsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/wrnap/global/rna/context.rb', line 49

def validate_coord_options
  unless coord_options.empty?
    unless Set.new(coord_options.keys) == Set.new(i|direction length|)
      raise ArgumentError.new("coord_options keys must contain only [:direction, :length], found: %s" % coord_options.keys)
    end

    unless (length = coord_options[:length]).is_a?(Integer) && length > 0
      raise ArgumentError.new("coord_options length must be greater than 0, found: %d" % length)
    end

    unless [:up, :down, :both, 5, 3].include?(direction = coord_options[:direction])
      raise ArgumentError.new("coord_options directions is not a valid key, found: %s" % direction)
    end
  end
end