Class: Bioinform::MotifFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/bioinform/formatters/motif_formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MotifFormatter

Returns a new instance of MotifFormatter.

Raises:



5
6
7
8
9
10
11
12
13
# File 'lib/bioinform/formatters/motif_formatter.rb', line 5

def initialize(options = {})
  @with_name = options.fetch(:with_name, :auto)
  @nucleotides_in = options.fetch(:nucleotides_in, :columns).to_sym
  @precision = options.fetch(:precision, false)
  @with_nucleotide_header = options.fetch(:with_nucleotide_header, false)
  @with_position_header = options.fetch(:with_position_header, false)
  raise Error, "`with_name` can be either `true` or `false` or `:auto` but was `#{@with_name}`"  unless [true, false, :auto].include?(@with_name)
  raise Error, "`nucleotides_in` can be either `:rows` or `:columns` but was `#{@nucleotides_in}`"  unless [:rows, :columns].include?(@nucleotides_in)
end

Instance Attribute Details

#nucleotides_inObject (readonly)

Returns the value of attribute nucleotides_in.



3
4
5
# File 'lib/bioinform/formatters/motif_formatter.rb', line 3

def nucleotides_in
  @nucleotides_in
end

#precisionObject (readonly)

Returns the value of attribute precision.



3
4
5
# File 'lib/bioinform/formatters/motif_formatter.rb', line 3

def precision
  @precision
end

#with_nameObject (readonly)

Returns the value of attribute with_name.



3
4
5
# File 'lib/bioinform/formatters/motif_formatter.rb', line 3

def with_name
  @with_name
end

#with_nucleotide_headerObject (readonly)

Returns the value of attribute with_nucleotide_header.



3
4
5
# File 'lib/bioinform/formatters/motif_formatter.rb', line 3

def with_nucleotide_header
  @with_nucleotide_header
end

#with_position_headerObject (readonly)

Returns the value of attribute with_position_header.



3
4
5
# File 'lib/bioinform/formatters/motif_formatter.rb', line 3

def with_position_header
  @with_position_header
end

Instance Method Details

#format(motif) ⇒ Object



64
65
66
# File 'lib/bioinform/formatters/motif_formatter.rb', line 64

def format(motif)
  format_name(motif) + format_matrix(motif)
end

#format_matrix(motif) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bioinform/formatters/motif_formatter.rb', line 37

def format_matrix(motif)
  result = ""
  result << "\t"  if with_nucleotide_header && with_position_header

  case @nucleotides_in
  when :columns
    if with_nucleotide_header
      result << motif.alphabet.each_letter.to_a.join("\t") << "\n"
    end
    motif.each_position.with_index do |pos, pos_index|
      result << "\n"  if pos_index != 0
      result << "#{position_index_formatted(pos_index + 1)}\t"  if with_position_header
      result << pos.map{|el| element_rounded(el) }.join("\t")
    end
  when :rows
    if with_position_header
      result << (1..motif.length).map{|pos| position_index_formatted(pos) }.join("\t") << "\n"
    end
    motif.alphabet.each_letter.with_index do |letter, letter_index|
      result << "\n"  if letter_index != 0
      result << "#{letter}\t"  if with_nucleotide_header
      result << motif.matrix.transpose[letter_index].map{|el| element_rounded(el) }.join("\t")
    end
  end
  result
end

#format_name(motif) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bioinform/formatters/motif_formatter.rb', line 15

def format_name(motif)
  case @with_name
  when true
    raise Error, "Motif doesn't respond to #name"  unless motif.respond_to?(:name)
    ">#{motif.name}\n"
  when false
    ""
  when :auto
    (motif.respond_to?(:name) && motif.name && !motif.name.strip.empty?) ? ">#{motif.name}\n" : ""
  end
end