Class: KmerAbundancePattern

Inherits:
Array
  • Object
show all
Defined in:
lib/kmer_abundance_pattern.rb

Overview

A pattern of presence/absence/neither across a run of kmers

Instance Method Summary collapse

Instance Method Details

#binary_stringObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/kmer_abundance_pattern.rb', line 3

def binary_string
  to_return = ''
  each do |present|
    to_return += case present
    when true
      '1'
    when false
      '0'
    when '-'
      '-'
    else
      raise "Unexpected pattern atom found: #{present}"
    end
  end
  to_return
end

#consistent_with?(another_pattern) ⇒ Boolean

Return true if another_pattern shows presence in all places where this pattern is present, (but maybe more)

e.g. 101 is consisten with 101 and 111, but not 011

Behaviour not defined when the first (this) pattern includes no-man’s land components

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
# File 'lib/kmer_abundance_pattern.rb', line 57

def consistent_with?(another_pattern)
  unless length == another_pattern.length
    raise "Unexpected comparison of this pattern #{inspect} with another: #{another_pattern.inspect}"
  end
  each_with_index do |bool, i|
    raise unless [true, false].include?(bool)
    return false if bool and another_pattern[i] == false
  end
  return true
end

#parse_from_human(boolean_pattern) ⇒ Object

Parse a 100001011 type representation



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kmer_abundance_pattern.rb', line 21

def parse_from_human(boolean_pattern)
  self[0...length] = [] #remove the last pattern if it existed
  boolean_pattern.each_char do |char|
    if char == '1'
      push true
    elsif char == '0'
      push false
    elsif char == '-'
      push nil
    else
      raise "Unexpected pattern character: #{char}"
    end
  end
end

#parse_from_kmer_abundance(abundances, lower_limit, upper_limit) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kmer_abundance_pattern.rb', line 68

def parse_from_kmer_abundance(abundances, lower_limit, upper_limit)
  abundances.each do |a|
    if a>=upper_limit
      push true
    elsif a<=lower_limit
      push false
    else
      push '-'
    end
  end
end

#same_as?(another_pattern) ⇒ Boolean

Return true if this pattern is exactly the same as another pattern

e.g. 101 is same_as? 101 but not 111 or 110

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
# File 'lib/kmer_abundance_pattern.rb', line 40

def same_as?(another_pattern)
  unless length == another_pattern.length
    raise "Unexpected comparison of this pattern #{inspect} with another: #{another_pattern.inspect}"
  end
  each_with_index do |bool, i|
    return false if bool != another_pattern[i]
  end
  return true
end