Class: Bioroebe::SimpleStringComparer

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

Overview

Bioroebe::SimpleStringComparer

Constant Summary collapse

STRING1 =
#

STRING1

#
'gelfand'
STRING2 =
#

STRING2

#
'gandalf'
VERTICAL_BAR =
#

VERTICAL_BAR

#
'|'
DEFAULT_MATCH_TOKEN =
#

DEFAULT_MATCH_TOKEN

This token is used to indicate a matching position. By default it will be the ‘|’ character.

#
VERTICAL_BAR

Constants inherited from CommandlineApplication

CommandlineApplication::OLD_VERBOSE_VALUE

Constants included from ColoursForBase

ColoursForBase::ARRAY_HTML_COLOURS_IN_USE

Constants inherited from Base

Base::NAMESPACE

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, #opnerev, #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, #runmode?, #set_be_verbose, #set_runmode, #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 BaseModule

#absolute_path, #default_file_read, #file_readlines

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_will_we_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?, #is_on_roebe?, #is_palindrome?, #main_encoding?, #mkdir, #move_file, #mv, #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

Methods included from InternalHashModule

#internal_hash?, #reset_the_internal_hash

Methods included from InferTheNamespaceModule

#infer_the_namespace, #namespace?

Constructor Details

#initialize(i = nil, run_already = true, &block) ⇒ SimpleStringComparer

#

initialize

#


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/bioroebe/string_matching/simple_string_comparer.rb', line 60

def initialize(
    i           = nil,
    run_already = true,
    &block
  )
  reset
  set_commandline_arguments(i)
  case i
  when nil # Pass through in this case.
  when :dont_run_yet
    i = nil
    run_already = false
  else
    guess_from_this_input(i)
  end
  # ======================================================================= #
  # === Handle blocks given to this method
  # ======================================================================= #
  if block_given?
    yielded = yield
    case yielded
    when :disable_colours
      disable_colours
    # ===================================================================== #
    # === :use_vertical_bar
    # ===================================================================== #
    when :use_vertical_bar
      set_match_token :vertical_bar
    end
  end
  run if run_already
end

Instance Method Details

#consider_notifying_the_user_if_the_sequences_are_not_equal_in_lengthObject

#

consider_notifying_the_user_if_the_sequences_are_not_equal_in_length

#


292
293
294
295
296
297
298
299
300
301
302
# File 'lib/bioroebe/string_matching/simple_string_comparer.rb', line 292

def consider_notifying_the_user_if_the_sequences_are_not_equal_in_length
  if @string1.size == @string2.size
    # In this case, all is fine.
  else
    # else notify the user
    e
    e '(Note that the two sequences are not equal in length. This class'
    e 'will only try to align from the starting position of both'
    e 'sequences as-is.)'
  end
end

#do_compareObject Also known as: compare

#

do_compare

#


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
# File 'lib/bioroebe/string_matching/simple_string_comparer.rb', line 243

def do_compare
  e rev # A newline for easier reading.
  chars1 = string1?.chars
  array1 = chars1.each_slice(80).to_a.map {|entry|
    entry.join
  }
  chars2 = string2?.chars
  array2 = chars2.each_slice(80).to_a.map {|entry|
    entry.join
  }
  # ======================================================================= #
  # Combine the two Arrays in order to find the "third" array.
  # ======================================================================= #
  array3 = chars1.zip(chars2).map {|entry|
    match_or_no_match_for(entry[0], entry[1])
  }.each_slice(80).to_a.map {|entry|
    entry.join
  }.map {|line|
    line.gsub(
      /\|/,
      send(@colour_for_a_match.to_sym, match_token?)+
      rev
    ) # Colourize the match-token in the colour seagreen.
  }
  0.upto(array1.size-1) {|index|
    e array1[index]
    e array3[index] # Middle position.
    e array2[index]
  }
  consider_notifying_the_user_if_the_sequences_are_not_equal_in_length
  e
end

#guess_from_this_input(i) ⇒ Object

#

guess_from_this_input

#


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/bioroebe/string_matching/simple_string_comparer.rb', line 119

def guess_from_this_input(i)
  # ======================================================================= #
  # === Handle the case when the user did input two Arrays
  # ======================================================================= #
  i.flatten! if i.is_a? Array
  if i.is_a? Array and i.empty?
    # ===================================================================== #
    # If our Array is empty, assign a default here.
    # ===================================================================== #
    splitted = [STRING1, STRING2]
  else
    splitted = i
  end
  if splitted and splitted.first.include?('|')
    splitted = splitted.first
  end
  splitted = splitted.split(N) unless splitted.is_a? Array
  splitted.map!(&:strip) # Get rid of trailing and leading newspaces.
  if splitted.is_a? Array
    case splitted.size
    when 1
      if splitted.first.include? '|'
        splitted = splitted.first.split('|')
      end
    end
  end
  set_string_one(splitted.first)
  set_string_last(splitted.last)
end

#match_or_no_match_for(char1, char2) ⇒ Object

#

match_or_no_match_for

#


205
206
207
208
209
210
211
212
# File 'lib/bioroebe/string_matching/simple_string_comparer.rb', line 205

def match_or_no_match_for(char1, char2)
  if char1 == char2
    @n_matches += 1
    return_match_token
  else
    return_non_match_token
  end
end

#match_token?Boolean

#

match_token?

#

Returns:

  • (Boolean)


236
237
238
# File 'lib/bioroebe/string_matching/simple_string_comparer.rb', line 236

def match_token?
  @match_token
end
#

menu (menu tag)

#


307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/bioroebe/string_matching/simple_string_comparer.rb', line 307

def menu(
    i = commandline_arguments_with_leading_hyphens?
  )
  if i.is_a? Array
    i.each {|entry| menu(entry) }
  else
    case i # case tag
    # ===================================================================== #
    # === --colour-for-a-match=lightblue
    # ===================================================================== #
    when /^-?-?colour(-|_| )?for(-|_| )?a(-|_| )?match=(.+)$/ # === $4
      set_colour_for_a_match($4.to_s.dup)
    end
  end
end

#n_matches?Boolean

#

n_matches?

#

Returns:

  • (Boolean)


198
199
200
# File 'lib/bioroebe/string_matching/simple_string_comparer.rb', line 198

def n_matches?
  @n_matches
end

#resetObject

#

reset (reset tag)

Set default values for the two Strings.

#


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bioroebe/string_matching/simple_string_comparer.rb', line 98

def reset
  super()
  # ======================================================================= #
  # Set up default strings first.
  # ======================================================================= #
  set_string1 STRING1
  set_string2 STRING2
  # ======================================================================= #
  # === @n_matches
  #
  # This counter will keep track how many matches we found.
  # ======================================================================= #
  @n_matches = 0
  set_match_token
  @non_match_token = '.' # This is the token that will be displayed for non-matches.
  set_colour_for_a_match
end

#return_match_tokenObject

#

return_match_token

#


191
192
193
# File 'lib/bioroebe/string_matching/simple_string_comparer.rb', line 191

def return_match_token
  @match_token
end

#return_non_match_tokenObject

#

return_non_match_token

#


183
184
185
186
# File 'lib/bioroebe/string_matching/simple_string_comparer.rb', line 183

def return_non_match_token
  return swarn(@non_match_token)+rev if use_colours? # Colourize this in red if we use colours.
  @non_match_token # Else just return it.
end

#runObject

#

run (run tag)

#


326
327
328
329
# File 'lib/bioroebe/string_matching/simple_string_comparer.rb', line 326

def run
  menu(commandline_arguments_with_leading_hyphens?)
  do_compare
end

#set_colour_for_a_match(i = :default) ⇒ Object

#

set_colour_for_a_match

#


279
280
281
282
283
284
285
286
287
# File 'lib/bioroebe/string_matching/simple_string_comparer.rb', line 279

def set_colour_for_a_match(
    i = :default
  )
  case i
  when :default
    i = :seagreen
  end
  @colour_for_a_match = i
end

#set_match_token(i = DEFAULT_MATCH_TOKEN) ⇒ Object Also known as: set_main_alignment_token_to

#

set_match_token

You can define another match token here. The default is ‘+’, another popular variant would be ‘|’.

#


220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/bioroebe/string_matching/simple_string_comparer.rb', line 220

def set_match_token(
    i = DEFAULT_MATCH_TOKEN
  )
  case i
  # ======================================================================= #
  # === :vertical_bar
  # ======================================================================= #
  when :vertical_bar
    i = VERTICAL_BAR
  end
  @match_token = i
end

#set_string1(i) ⇒ Object Also known as: string1=, set_string_one

#

set_string1

#


152
153
154
# File 'lib/bioroebe/string_matching/simple_string_comparer.rb', line 152

def set_string1(i)
  @string1 = i
end

#set_string2(i) ⇒ Object Also known as: string2=, set_string_two, set_string_last

#

set_string2

#


160
161
162
# File 'lib/bioroebe/string_matching/simple_string_comparer.rb', line 160

def set_string2(i)
  @string2 = i
end

#string1?Boolean Also known as: input?

#

string1?

#

Returns:

  • (Boolean)


169
170
171
# File 'lib/bioroebe/string_matching/simple_string_comparer.rb', line 169

def string1?
  @string1
end

#string2?Boolean Also known as: string2

#

string2

#

Returns:

  • (Boolean)


176
177
178
# File 'lib/bioroebe/string_matching/simple_string_comparer.rb', line 176

def string2?
  @string2
end