Class: Wrnap::Global::Context

Inherits:
Rna
  • Object
show all
Defined in:
lib/wrnap/global/rna/context.rb

Instance Attribute Summary collapse

Attributes inherited from Rna

#comment, #second_structure, #structure

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rna

#empty_structure, init_from_array, init_from_context, init_from_fasta, init_from_hash, init_from_self, #inspect, #method_missing, #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, coord_options: {}) ⇒ Context

Returns a new instance of Context.



27
28
29
30
31
32
33
34
35
# File 'lib/wrnap/global/rna/context.rb', line 27

def initialize(sequence: nil, accession: nil, from: nil, to: nil, coord_options: {})
  @accession, @from, @to, @coord_options = accession, from, to, coord_options

  validate_coord_options

  if sequence
    @raw_sequence = (sequence.is_a?(String) ? Bio::Sequence::NA.new(sequence) : sequence).upcase
  end
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.



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

def accession
  @accession
end

#coord_optionsObject (readonly)

Returns the value of attribute coord_options.



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

def coord_options
  @coord_options
end

#fromObject (readonly)

Returns the value of attribute from.



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

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



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

def to
  @to
end

Class Method Details

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



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

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

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



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

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

Instance Method Details

#coord_windowObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/wrnap/global/rna/context.rb', line 101

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



57
58
59
# File 'lib/wrnap/global/rna/context.rb', line 57

def down_coord
  [from, to].max
end

#extend!(coord_options = {}) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/wrnap/global/rna/context.rb', line 88

def extend!(coord_options = {})
  tap do
    @coord_options = coord_options unless coord_options.empty?
    validate_coord_options
    @extended      = true
    remove_instance_variable(:@raw_sequence)
  end
end

#extended?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/wrnap/global/rna/context.rb', line 97

def extended?
  @extended
end

#minus_strand?Boolean

Returns:

  • (Boolean)


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

def minus_strand?
  !plus_strand?
end

#plus_strand?Boolean

Returns:

  • (Boolean)


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

def plus_strand?
  to > from
end

#seq_fromObject



61
62
63
# File 'lib/wrnap/global/rna/context.rb', line 61

def seq_from
  up_coord + coord_window.min
end

#seq_toObject



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

def seq_to
  up_coord + coord_window.max
end

#sequenceObject Also known as: seq



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

def sequence
  @raw_sequence ||= Entrez.rna_sequence_from_entrez(accession, up_coord, coord_window)
  @raw_sequence   = minus_strand? ? @raw_sequence.complement : @raw_sequence
end

#strandObject



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

def strand
  plus_strand? ? :plus : :minus
end

#up_coordObject



53
54
55
# File 'lib/wrnap/global/rna/context.rb', line 53

def up_coord
  [from, to].min
end

#validate_coord_optionsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/wrnap/global/rna/context.rb', line 37

def validate_coord_options
  unless coord_options.empty?
    unless 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