Class: Bioroebe::HammingDistance

Inherits:
CommandlineApplication show all
Defined in:
lib/bioroebe/string_matching/hamming_distance.rb

Constant Summary collapse

SHOW_SPACER =
#

SHOW_SPACER

#
true

Constants inherited from CommandlineApplication

CommandlineApplication::OLD_VERBOSE_VALUE

Constants included from ColoursForBase

ColoursForBase::ARRAY_HTML_COLOURS_IN_USE

Constants inherited from Base

Base::NAMESPACE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CommandlineApplication

#all_aminoacids?, #append_what_into, #at_home?, #be_silent, #be_verbose?, #cat, #ccliner, #change_directory, #cliner, #codon_table_dataset?, #codon_to_aminoacid, #codons_for?, #colourize_this_dna_sequence, #complement, #cp, #disable_warnings, #download_dir?, #editor?, #enable_warnings, #ensure_that_the_base_directories_exist, #esystem, #extract, #is_this_a_start_codon?, #is_this_a_stop_codon?, #leading_five_prime, #load_bioroebe_yaml_file, #log_directory?, #one_letter_to_long_name, #one_to_three, #only_numbers?, #open_in_browser, #opne, #opnn, #pad_with_double_quotes, #pad_with_single_quotes, #partner_nucleotide, #remove_numbers, #remove_trailing_ansii_escape_code, #return_all_possible_start_codons, #return_array_of_one_letter_aminoacids, #return_cheerful_person, #return_chunked_display, #return_ubiquitin_sequence, #set_be_verbose, #start_codon?, #stop_codons?, #strict_filter_away_invalid_aminoacids, #taxonomy_download_directory?, #three_to_one, #to_rna, #trailing_three_prime, #use_opn?, #verbose_truth, #was_or_were, #without_extname, #write_what_into

Methods included from CommandlineArguments

#commandline_arguments?, #commandline_arguments_that_are_files?, #e, #first?, #first_non_hyphen_argument?, #remove_hyphens_from_the_commandline_arguments, #return_commandline_arguments_as_string, #return_commandline_arguments_that_are_not_files, #return_entries_without_two_leading_hyphens, #select_commandline_arguments, #select_entries_starting_with_two_hyphens, #set_commandline_arguments

Methods included from ColoursForBase

#colourize_this_aminoacid_sequence_for_the_commandline, #colourize_this_nucleotide_sequence, #disable_colours, #ecomment, #efancy, #egold, #enable_colours, #eorange, #eparse, #erev, #red, #remove_trailing_escape_part, #return_colour_for_nucleotides, #rev, #sdir, #set_use_colours, #sfancy, #sfile, #simp, #swarn, #use_colours?, #use_colours_within_the_bioroebe_namespace?

Methods inherited from Base

#append_what_into, #can_base_pair?, #convert_global_env, #delete_file, #directory_to_the_codon_tables?, #file_readlines, #infer_the_namespace, #is_on_roebe?, #is_palindrome?, #main_encoding?, #mkdir, #move_file, #mv, #namespace?, #no_file_exists_at, #no_newlines, #project_yaml_directory?, #rds, #register_sigint, #return_pwd, #return_the_first_line_of_this_file, #word_wrap, #write_what_into

Constructor Details

#initialize(i = ARGV, run_already = true) ⇒ HammingDistance

#

initialize

#

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bioroebe/string_matching/hamming_distance.rb', line 31

def initialize(
    i           = ARGV,
    run_already = true
  )
  super()
  reset
  set_input(i)
  # ======================================================================= #
  # === Handle blocks next
  # ======================================================================= #
  if block_given?
    case yield
    when :be_quiet
      @be_verbose = false
    end
  end
  run if run_already
end

Class Method Details

.[](i) ⇒ Object

#

Bioroebe::HammingDistance[]

#

295
296
297
# File 'lib/bioroebe/string_matching/hamming_distance.rb', line 295

def self.[](i)
  new(i)
end

Instance Method Details

#colourize_in_green(i) ⇒ Object

#

colourize_in_green

#

131
132
133
# File 'lib/bioroebe/string_matching/hamming_distance.rb', line 131

def colourize_in_green(i)
  rev+i+rev
end

#colourize_in_red(i) ⇒ Object

#

colourize_in_red

#

124
125
126
# File 'lib/bioroebe/string_matching/hamming_distance.rb', line 124

def colourize_in_red(i)
  swarn(i)
end

#consider_notifying_the_user_if_no_input_was_givenObject

#

consider_notifying_the_user_if_no_input_was_given

#

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/bioroebe/string_matching/hamming_distance.rb', line 138

def consider_notifying_the_user_if_no_input_was_given
  notify_the_user = false # By default, we will notify the user.
  notify_the_user = true unless input?
  if input?
    notify_the_user = true if input?.empty?
  end
  if notify_the_user
    opnn; e 'Please provide input.'
    opnn; e
    opnn; e 'The two strings should be separated by a " ". Example:'
    opnn; e
    opnn; e simp('  AGUUCGAUGGAAAAT AGUCCGGUCGAAAAA')
    opnn; e
    return
  end
end

#input?Boolean

#

input?

#

Returns:

  • (Boolean)

110
111
112
# File 'lib/bioroebe/string_matching/hamming_distance.rb', line 110

def input?
  @input
end

#n_differences?Boolean Also known as: n_differences

#

n_differences?

This method has to return an Integer. The integer tells us how many differences exist between the two given input strings at hand.

#

Returns:

  • (Boolean)

170
171
172
# File 'lib/bioroebe/string_matching/hamming_distance.rb', line 170

def n_differences?
  @n_differences
end

#report_n_differencesObject

#

report_n_differences

#

158
159
160
161
162
# File 'lib/bioroebe/string_matching/hamming_distance.rb', line 158

def report_n_differences
  opnn
  e "#{rev}We have discovered #{simp(@n_differences)}#{rev}# differences "\
    "between these two strings."
end

#report_resultObject

#

report_result

#

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/bioroebe/string_matching/hamming_distance.rb', line 210

def report_result
  if @be_verbose and !(@be_verbose == :short)
    _ = rev.dup
    if @n_differences > 1
      _ << 'These '+colourize_in_red(@n_differences)+rev+
           ' differences'
    else
      _ << 'This '+colourize_in_red(@n_differences)+rev+
           ' difference'
    end
    _ << ', highlighted in '+colourize_in_red('red')+rev
    if @n_differences > 1
      _ << ', were found.'
    else
      _ << ', was found.'
    end
    e _
    e
  end
  counter = 0
  # ======================================================================= #
  # @zipped may look like this:
  #
  #   [["AAACCCGGGTTT", "CGACGATATGTC"]]
  #
  # ======================================================================= #
  @zipped.each {|inner_array1, inner_array2|
    arr1 = inner_array1.chars
    arr2 = inner_array2.chars
    inner_array1 = []
    inner_array2 = []
    arr1.each_with_index {|entry, index|
      if entry == arr2[index]
        inner_array1 << entry
        inner_array2 << arr2[index]
      else
        inner_array1 << colourize_in_red(entry)+rev
        inner_array2 << colourize_in_red(arr2[index])+rev
      end
    }
    submatch = @array_with_matches[counter..(counter+inner_array2.size-1)]
    if submatch.include? '|'
      # =================================================================== #
      # In this case, we have to colourize too.
      # =================================================================== #
      _ = submatch.each_with_index.map {|entry, index|
        entry = index if entry == '|'
        entry
      }.reject {|entry| entry.to_s.strip.empty? }
    end
    # ===================================================================== #
    # Finally, output the two different Arrays here. But only do so if
    # the verbosity level is not set to short.
    # ===================================================================== #
    case @be_verbose
    when true, :default
      e
      e rev+inner_array1.join
      e submatch.map {|entry|
        entry.gsub(/\|/,
          colourize_in_green('|')+rev)
      }.join
      e rev+inner_array2.join
      e
    # ===================================================================== #
    # === :short_style
    # ===================================================================== #
    when :short_style,
         :short
      e n_differences?
    end
    counter += inner_array2.size
  }
end

#resetObject

#

reset

#

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bioroebe/string_matching/hamming_distance.rb', line 53

def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === @input
  # ======================================================================= #
  @input = nil
  reset_spacer
  # ======================================================================= #
  # === @be_verbose
  #
  # The @be_verbose variable can also be set to :short, in which case
  # this class will only output a number.
  # ======================================================================= #
  @be_verbose = true # If this is true then we will also show the amount of differences.
end

#reset_spacerObject

#

reset_spacer

#

117
118
119
# File 'lib/bioroebe/string_matching/hamming_distance.rb', line 117

def reset_spacer
  @spacer = ''.dup
end

#runObject

#

run (run tag)

#

288
289
290
# File 'lib/bioroebe/string_matching/hamming_distance.rb', line 288

def run
  run_everything
end

#run_everythingObject

#

run_everything

#

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/bioroebe/string_matching/hamming_distance.rb', line 177

def run_everything
  consider_notifying_the_user_if_no_input_was_given
  return unless input?
  reset_spacer
  first_array  = input?.first
  second_array = input?.last
  return unless first_array
  chunk1 = first_array.chars.each_slice(80).to_a.map {|entry| entry.join }
  chunk2 = second_array.chars.each_slice(80).to_a.map {|entry| entry.join }
  # ===================================================================== #
  # We built our two Arrays. Next, we must build another array that
  # keeps a | or an empty ' ' slot.
  # ===================================================================== #
  @array_with_matches = []
  @zipped = chunk1.zip(chunk2)
  @zipped.each {|array1, array2|
    array1.chars.each_with_index {|entry, index|
      if entry == array2[index]
        @array_with_matches << '|'
      else
        @array_with_matches << ' '
      end
    }
  }
  @n_differences = (
    @array_with_matches.flatten.size - @array_with_matches.flatten.count('|')
  )
  report_result
end

#set_input(i = nil) ⇒ Object

#

set_input

The input should always be kept as an Array.

#

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bioroebe/string_matching/hamming_distance.rb', line 75

def set_input(i = nil)
  if i.nil?
    e 'Please provide two strings, such as:'
    e
    e '  hamming AGUUCGAUGG AGUCCGGUCG'
    e
    e 'Note that if you use the Shell component of the Bioroebe project (Bioroebe::Shell),'
    e 'then you can omit the first sequence, if you already have assigned one.'
  end
  case i.first
  when 'TEST'
    i = 'AGUUCGAUGGAAAATAT AGUCCGGUCGAAAAAAA'
  else
    if i and File.file?(i.first)
      i = File.readlines(i.first).map(&:strip)
    end
  end if i.is_a? Array
  if i.is_a? String
    if i.include? ' '
      i = i.split(' ')
    elsif i.include? '|' # Split on '|' in this case
      i = i.split('|')
    end
  end
  i = [i] unless i.is_a? Array
  if i.any? {|line| line.include? '|' }
    i.map! {|line| line.split('|') }
  end
  i.flatten!
  @input = i
end