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, #sequence, #structures

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Wrnap::Rna

#==, #copy_name_from, #eql?, #formatted_string, init_from_array, init_from_context, init_from_hash, parse_rna_attributes, #pp, #run, #temp_fa_file!, #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
# 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,
    structures: options[:rna][:structures] || options[:rna][:structure] || options[:rna][:strs] || options[:rna][:str],
    comment:    options[:rna][:comment] || options[:rna][:name] || identifier,
    &block
  )

  remove_instance_variable(:@sequence)
end

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



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

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

#coord_windowObject



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

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



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

def down_coord
  [from, to].max
end

#extend!(coord_options = {}) ⇒ Object



107
108
109
# File 'lib/wrnap/rna/context.rb', line 107

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

#identifierObject



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

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

#inspectObject



139
140
141
142
143
144
145
# File 'lib/wrnap/rna/context.rb', line 139

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

#minus_strand?Boolean

Returns:

  • (Boolean)


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

def minus_strand?
  !plus_strand?
end

#plus_strand?Boolean

Returns:

  • (Boolean)


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

def plus_strand?
  to > from
end

#seq_fromObject



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

def seq_from
  up_coord + coord_window.min
end

#seq_toObject



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

def seq_to
  up_coord + coord_window.max
end

#strandObject



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

def strand
  plus_strand? ? :plus : :minus
end

#up_coordObject



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

def up_coord
  [from, to].min
end

#validate_coord_optionsObject



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

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