Class: Scaffolder::Region

Inherits:
Object
  • Object
show all
Extended by:
Errors
Includes:
Errors
Defined in:
lib/scaffolder/region.rb

Direct Known Subclasses

Insert, Sequence, Unresolved

Defined Under Namespace

Classes: Insert, Sequence, Unresolved

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](type) ⇒ Scaffolder::Region

Returns subclassed instances of Scaffolder::Region by name

Returns:



11
12
13
# File 'lib/scaffolder/region.rb', line 11

def [](type)
  self.const_get(type.capitalize)
end

.attribute(name, options = {}) ⇒ Object

Links the specification of values in the scaffold file to the assignment of instance variables.

Examples:

Simple specification

class MyRegion < Scaffolder::Region
  attribute :value # "value" usable as a keyword in the scaffold file
end

Specification with a default value

attribute :value, :default => 1

Specification with where proc is evaluated for the default

attribute :value, :default => lamdba{ Time.now.to_s }

Specification with proc where the region instance is avaiable

attribute :value, :default => lamdba{|s| s.other_variable + 1 }

Parameters:

  • Define (Symbol)

    attributes for this type of scaffold region. Attributes are read from the scaffold file and stored as instance variables.

  • options (Hash) (defaults to: {})

    Attribute options.

Options Hash (options):

  • Default (Object, Proc)

    Specify a default value for this attribute if a value is not defined in the scaffold file.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/scaffolder/region.rb', line 34

def attribute(name,options = {})
  define_method(name) do |*arg|
    var = "@#{name}"
    default = options[:default]
    unless arg.first # Is an argument is passed to the method?
      value = instance_variable_get(var)
      return value if value
      return default.respond_to?(:call) ? default.call(self) : default
    end
    instance_variable_set(var,arg.first)
  end
end

.generate(region_data) ⇒ Scaffolder::Region

Parse each key-value pair in the scaffold hash calling the corresponding attribute method for the key and passing the value as an argument.

Parameters:

  • region_data (Hash)

    Key-Value pairs of the data required to define this scaffolder region.

Returns:

  • (Scaffolder::Region)

    Returns an region object where the instance variables have been assigned according to the region data hash.

Raises:

  • (UnknownAttributeError)

    If a keyword in the scaffold file does not have a corresponding attribute in the class.

See Also:



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/scaffolder/region.rb', line 58

def generate(region_data)
  region = self.new
  region_data.each_pair do |attribute,value|
    begin
      region.send(attribute.to_sym,value)
    rescue NoMethodError => e
      raise UnknownAttributeError.new(e)
    end
  end
  region
end

Instance Method Details

#entry_typeSymbol

The name of the class. Useful for selecting specific region types.

Returns:

  • (Symbol)


110
111
112
# File 'lib/scaffolder/region.rb', line 110

def entry_type
  self.class.name.split('::').last.downcase.to_sym
end

#raw_sequenceString

The raw sequence for this region.

Parameters:

  • (String)

Returns:

  • (String)


76
# File 'lib/scaffolder/region.rb', line 76

attribute :raw_sequence

#reverseBoolean

Should the sequence be reverse complemented. Reverse complementation is performed after the start and end of the sequence has been trimmed.

Parameters:

  • (Boolean)

Returns:

  • (Boolean)


96
# File 'lib/scaffolder/region.rb', line 96

attribute :reverse

#sequenceString

Returns the value of the Scaffolder::Region#raw_sequence after subsequencing and reverse complementation (if specified in the scaffold file).

Returns:

  • (String)

    Sequence after all modifications

Raises:

  • (CoordinateError)

    if the start position is less than 1.

  • (CoordinateError)

    if the stop position is greater than the sequence length.

  • (CoordinateError)

    if the start position is greater than the stop position.



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/scaffolder/region.rb', line 124

def sequence
  seq = sequence_hook

  raise CoordinateError.new if start < 1
  raise CoordinateError.new if stop  > seq.length
  raise CoordinateError.new if start > stop

  seq = seq[(start-1)..(stop-1)]
  seq = Bio::Sequence::NA.new(seq).reverse_complement if reverse
  seq.to_s.upcase
end

#sequence_hookString

Override this to manipulate the sequence before it’s subsequenced, reverse complemented etc. by Scaffolder::Region#sequence.

Returns:

  • (String)

    The value of the raw_sequence attribute

See Also:



103
104
105
# File 'lib/scaffolder/region.rb', line 103

def sequence_hook
  raw_sequence
end

#startInterger

Trim the start of sequence to this position. Default is 1.

Parameters:

  • (Interger)

Returns:

  • (Interger)


82
# File 'lib/scaffolder/region.rb', line 82

attribute :start, :default => 1

#stopInterger

Trim the end of sequence to this position. Default is the sequence length..

Parameters:

  • (Interger)

Returns:

  • (Interger)


88
# File 'lib/scaffolder/region.rb', line 88

attribute :stop, :default => lambda{|s| s.sequence_hook.length}