Sha256: 607ba82da95147d76155c8b62ccebdcc94e6b52203b9a13fa162fa659948fe31

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

require 'scaffolder'

module Scaffolder::Test
  class Sequence

    def initialize(options = {})
      @options = options
    end

    def initialize_copy(source)
      super
      @options = @options.dup
    end

    [:name,:sequence,:reverse,:start,:stop].each do |attribute|
      define_method(attribute) do |*arg|
        unless arg.first
          return @options[attribute]
        end
        @options[attribute] = arg.first
        return self
      end
    end

    def inserts(arg = nil)
      return @options[:inserts] if arg.nil?
      @options[:inserts] ||= Array.new
      if arg.instance_of?(Array)
       arg.each {|a| @options[:inserts] << a}
      else
        @options[:inserts] << arg
      end
      return self
    end

    def to_hash
      hash = {'source' => name}.merge stringify_keys(@options)
      if @options[:inserts]
        hash['inserts'] = Array.new
        @options[:inserts].each_with_index do |insert,i|
          hash['inserts'] << {'source' => "insert#{i+1}"}.merge(stringify_keys(insert))
        end
      end
      {'sequence' => hash}
    end

    def to_fasta
      fasta = Bio::Sequence.new(sequence).output(:fasta,:header => name)
      inserts.each_with_index do |insert,i|
        fasta << Bio::Sequence.new(insert[:sequence]).output(:fasta,:header => "insert#{i+1}")
      end if inserts
      fasta.strip
    end

    private

    def stringify_keys(hash)
      [:start,:stop,:reverse,:open,:close].inject(Hash.new) do |stringified,key|
        stringified[key.to_s] = hash[key] if hash[key]
        stringified
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
scaffolder-test-helpers-0.4.1 lib/scaffolder/test/sequence.rb
scaffolder-test-helpers-0.4.0 lib/scaffolder/test/sequence.rb
scaffolder-test-helpers-0.3.0 lib/scaffolder/test/sequence.rb