Class: Wrnap::Rna::Context

Inherits:
Wrnap::Rna show all
Extended by:
Forwardable
Defined in:
lib/wrnap/rna/context.rb

Constant Summary

Constants inherited from Wrnap::Rna

CANONICAL_BASES

Instance Attribute Summary collapse

Attributes inherited from Wrnap::Rna

#comment, #metadata, #second_structure, #structure

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Wrnap::Rna

#==, #copy_name_from, #empty_structure, #eql?, #formatted_string, init_from_array, init_from_context, init_from_fasta, init_from_hash, init_from_self, #method_missing, #no_structure, #one_structure, #pp, #run, #temp_fa_file!, #two_structures, #write_fa!

Methods included from Constraints

included

Methods included from TreeFunctions

#trunk, #with_tree

Methods included from Metadata

included

Methods included from Wrnapper

#wrnap

Methods included from Extensions

included

Methods included from Global::Yaml

#deserialize, #serialize

Constructor Details

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

Returns a new instance of Context.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/wrnap/rna/context.rb', line 31

def initialize(sequence: nil, accession: nil, from: nil, to: nil, options: {}, &block)
  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,
    &block
  )

  remove_instance_variable(:@sequence)
end

Dynamic Method Handling

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

Instance Attribute Details

#accessionObject (readonly)

Returns the value of attribute accession.



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

def accession
  @accession
end

#coord_optionsObject (readonly)

Returns the value of attribute coord_options.



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

def coord_options
  @coord_options
end

#fromObject (readonly)

Returns the value of attribute from.



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

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



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

def to
  @to
end

Class Method Details

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



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

def init_from_entrez(accession, from, to, options = {}, &block)
  new(
    accession: accession,
    from:      from.to_i,
    to:        to.to_i,
    options:   options,
    &block
  )
end

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



19
20
21
22
23
24
25
26
27
28
# File 'lib/wrnap/rna/context.rb', line 19

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

Instance Method Details

#_idObject



138
139
140
# File 'lib/wrnap/rna/context.rb', line 138

def _id
  identifier.gsub(/[^A-Z0-9]/, ?_).gsub(/__+/, ?_)
end

#coord_windowObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/wrnap/rna/context.rb', line 114

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



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

def down_coord
  [from, to].max
end

#extend!(coord_options = {}) ⇒ Object



110
111
112
# File 'lib/wrnap/rna/context.rb', line 110

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

#identifierObject



134
135
136
# File 'lib/wrnap/rna/context.rb', line 134

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

#inspectObject



142
143
144
145
146
147
148
# File 'lib/wrnap/rna/context.rb', line 142

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

#minus_strand?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/wrnap/rna/context.rb', line 93

def minus_strand?
  !plus_strand?
end

#plus_strand?Boolean

Returns:

  • (Boolean)


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

def plus_strand?
  to > from
end

#seq_fromObject



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

def seq_from
  up_coord + coord_window.min
end

#seq_toObject



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

def seq_to
  up_coord + coord_window.max
end

#sequenceObject Also known as: seq



97
98
99
100
101
102
103
104
# File 'lib/wrnap/rna/context.rb', line 97

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

#strandObject



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

def strand
  plus_strand? ? :plus : :minus
end

#up_coordObject



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

def up_coord
  [from, to].min
end

#validate_coord_optionsObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/wrnap/rna/context.rb', line 53

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