Class: Rbfam::Sequence

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/rbfam/modules/sequence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(family, accession, from, to, options = {}) ⇒ Sequence

Returns a new instance of Sequence.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rbfam/modules/sequence.rb', line 5

def initialize(family, accession, from, to, options = {})
  @family        = family
  @accession     = accession
  @from          = from
  @to            = to
  @coord_options = options[:autoload].is_a?(Hash) ? options[:autoload] : {}
  
  if options[:sequence]
    @raw_sequence = (options[:sequence].is_a?(String) ? Bio::Sequence::NA.new(options[:sequence]) : options[:sequence]).upcase
  end
  
  if options[:autoload]
    sequence
  end
end

Instance Attribute Details

#accessionObject (readonly)

Returns the value of attribute accession.



3
4
5
# File 'lib/rbfam/modules/sequence.rb', line 3

def accession
  @accession
end

#coord_optionsObject (readonly)

Returns the value of attribute coord_options.



3
4
5
# File 'lib/rbfam/modules/sequence.rb', line 3

def coord_options
  @coord_options
end

#familyObject (readonly)

Returns the value of attribute family.



3
4
5
# File 'lib/rbfam/modules/sequence.rb', line 3

def family
  @family
end

#fromObject (readonly)

Returns the value of attribute from.



3
4
5
# File 'lib/rbfam/modules/sequence.rb', line 3

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



3
4
5
# File 'lib/rbfam/modules/sequence.rb', line 3

def to
  @to
end

Instance Method Details

#coord_windowObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rbfam/modules/sequence.rb', line 91

def coord_window
  # Options from coord_options ex: { length: 300, extend: 3 }, or { length: 250, extend: :both }
  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], [5, :minus] then Range.new(range.min, range.max + coord_options[:length])
      when [5, :plus], [3, :minus] then Range.new(range.min - coord_options[:length], range.max)
      else puts "WARNING: value for :direction key in sequence retrieval needs to be one of 5, 3, :both - found (%s)" % coord_options[:direction].inspect
      end
    end
  else
    range
  end
end

#descriptionObject



74
75
76
# File 'lib/rbfam/modules/sequence.rb', line 74

def description
  ("%s %s %s" % [accession, from, to]).gsub(/\W+/, "_")
end

#down_coordObject



45
46
47
# File 'lib/rbfam/modules/sequence.rb', line 45

def down_coord
  [from, to].max
end

#extend!(coord_options = {}) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/rbfam/modules/sequence.rb', line 78

def extend!(coord_options = {})
  tap do
    @extended      = true
    @coord_options = coord_options
    remove_instance_variable(:@raw_sequence)
    sequence
  end
end

#extended?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/rbfam/modules/sequence.rb', line 87

def extended?
  @extended
end

#inspectObject



110
111
112
# File 'lib/rbfam/modules/sequence.rb', line 110

def inspect
  "#<Rbfam::Sequence #{description} #{seq[0, 20] + ('...' if seq.length > 20)}>"
end

#minus_strand?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/rbfam/modules/sequence.rb', line 65

def minus_strand?
  !plus_strand?
end

#plus_strand?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/rbfam/modules/sequence.rb', line 61

def plus_strand?
  to > from
end

#save!(options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rbfam/modules/sequence.rb', line 21

def save!(options = {})
  unless extended?
    Rbfam.script("sequences_in_mysql")
  
    SequenceTable.create({ 
      family:          family.family_name, 
      accession:       accession, 
      sequence:        sequence, 
      sequence_length: sequence.length, 
      from:            from, 
      to:              to, 
      seq_from:        seq_from, 
      seq_to:          seq_to,
      seed:            options[:seed]
    })
  else
    tap { puts "ERROR: at this time you can not save #{self.class.name}#extend! objects to protect against DB redundancy (and I'm lazy)." }
  end
end

#seq_fromObject



49
50
51
# File 'lib/rbfam/modules/sequence.rb', line 49

def seq_from
  up_coord + coord_window.min
end

#seq_toObject



53
54
55
# File 'lib/rbfam/modules/sequence.rb', line 53

def seq_to
  up_coord + coord_window.max
end

#strandObject



57
58
59
# File 'lib/rbfam/modules/sequence.rb', line 57

def strand
  plus_strand? ? :plus : :minus
end

#up_coordObject



41
42
43
# File 'lib/rbfam/modules/sequence.rb', line 41

def up_coord
  [from, to].min
end