Class: Bioroebe::ShowNucleotideSequence

Inherits:
Sequence show all
Includes:
ColoursForBase
Defined in:
lib/bioroebe/nucleotides/show_nucleotide_sequence.rb

Overview

Bioroebe::ShowNucleotideSequence

Constant Summary collapse

USE_THIS_COLOUR =
#

Bioroebe::ShowNucleotideSequence::USE_THIS_COLOUR

This colours is usually “steelblue”.

#
USE_THIS_COLOUR_FOR_DNA

Constants included from ColoursForBase

ColoursForBase::ARRAY_HTML_COLOURS_IN_USE

Constants inherited from Sequence

Bioroebe::Sequence::REMOVE_INVALID_CHARACTERS, Bioroebe::Sequence::SHALL_WE_UPCASE

Instance Method Summary collapse

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 Sequence

[], #automatic_support_for_nucleotides, #description?, #index, #infer_type, #is_DNA?, #is_RNA?, #is_a_protein?, #is_a_protein_now, #map, #n_uracil?, #randomize, #remove_invalid_entries_from_the_dna_sequence, #remove_invalid_entries_from_the_dna_sequence!, #return_string_nucleotides_or_aminoacids, #sanitize_dataset, #sanitize_rna, #save_sequence_to_this_file, sequence_from_file, #set_description, #set_dna, #set_protein, #set_rna, #set_save_file, #set_sequence, #set_type, #shall_we_upcase?, #size?, #to_genbank, #to_regexp, #type?

Methods inherited from RawSequence

#+, #<<, #[]=, #calculate_levensthein_distance, #chars?, #complement, #composition?, #count, #delete, #delete!, #downcase, #each_char, #empty?, #find_substring_indices, #first_position=, #freeze, #gsub, #gsub!, #include?, #insert_at_this_position, #prepend, #remove_n_characters_from_the_left_side, #reverse, #reverse!, #reverse_complement, #scan, #set_raw_sequence, #shuffle, #size?, #split, #start_with?, #strip, #subseq, #to_s, #to_str, #tr!, #upcase!

Constructor Details

#initialize(this_sequence = 'ATCG', run_already = true) ⇒ ShowNucleotideSequence

#

initialize

#

36
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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 36

def initialize(
    this_sequence = 'ATCG',
    run_already   = true
  )
  reset
  extend ::Bioroebe::CommandlineArguments # <- This comes here due to reset() otherwise be defind.
  if this_sequence.is_a? Array
    if this_sequence.any? {|entry| entry.start_with? '--' }
      set_commandline_arguments(
        select_commandline_arguments(this_sequence)
      )
      this_sequence.reject! {|inner_entry| inner_entry.start_with? '--' }
      menu(@commandline_arguments)
    end
  end
  set_this_sequence(
    this_sequence
  )
  case run_already
  # ======================================================================= #
  # === :do_not_run_yet
  # ======================================================================= #
  when :do_not_run_yet,
       :do_not_run
    run_already = false
  # ======================================================================= #
  # === :do_not_report_anything
  # ======================================================================= #
  when :do_not_report_anything
    do_not_report_anything
  end
  # ======================================================================= #
  # === Handle blocks next
  # ======================================================================= #
  if block_given?
    yielded = yield
    # ===================================================================== #
    # === Handle Symbols next
    # ===================================================================== #
    if yielded.is_a? Symbol
      case yielded
      # =================================================================== #
      # === :colourize_start_codon
      # =================================================================== #
      when :colourize_start_codon
        do_colourize_the_start_codon
      end
    # ===================================================================== #
    # === Handle Hashes next
    # ===================================================================== #
    elsif yielded.is_a? Hash
      # =================================================================== #
      # === :colourize_this_subsequence
      #
      # This entry point can be used to supply any other subsequence
      # that the user wants to colourize.
      #
      # Usage example:
      #
      #   {{ colourize_this_subsequence: 'ATG' }}
      #
      # =================================================================== #
      if yielded.has_key? :colourize_this_subsequence
        search_for_this_substring(
          yielded[:colourize_this_subsequence]
        )
      end
    end
  end
  run if run_already
end

Instance Method Details

#clear_paddingObject

#

clear_padding

#

166
167
168
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 166

def clear_padding
  @padding_to_use = ''.dup
end

#colourize_dna_sequence(i = '') ⇒ Object Also known as: colour_for_nucleotides, colourize_rna_sequence

#

colourize_dna_sequence

#

481
482
483
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 481

def colourize_dna_sequence(i = '')
  ::Colours.send(USE_THIS_COLOUR, i.dup)
end

#display_with_prior_formatting(i = @sequence_for_display, &block) ⇒ Object

#

display_with_prior_formatting

#

499
500
501
502
503
504
505
506
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 499

def display_with_prior_formatting(
    i = @sequence_for_display, &block
  )
  do_show_the_ruler
  clear_padding
  format_this_nucleotide_sequence(i, &block)
  report(i) # Need to invoke the original method. Before May 2020 this was display().
end

#do_colourize_the_start_codonObject

#

do_colourize_the_start_codon

#

528
529
530
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 528

def do_colourize_the_start_codon
  add_this_substring('ATG')
end

#do_not_report_anythingObject

#

do_not_report_anything

#

212
213
214
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 212

def do_not_report_anything
  @may_we_report = false
end

#do_show_the_rulerObject

#

do_show_the_ruler

#

394
395
396
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 394

def do_show_the_ruler
  @show_ruler = true
end

#format_this_nucleotide_sequence(i = main_sequence_as_string? ) ⇒ Object Also known as: format, format_this_string

#

format_this_nucleotide_sequence

#

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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 219

def format_this_nucleotide_sequence(
    i = main_sequence_as_string?
  )
  show_ruler               = @show_ruler
  padding_to_use           = @padding_to_use
  show_five_prime_leader   = @show_five_prime_leader
  show_three_prime_trailer = @show_three_prime_trailer
  # ======================================================================= #
  # The variable called result will contain our modified sequence that
  # is to be displayed on the commandline.
  # ======================================================================= #
  result                   = "#{padding_to_use}#{rev}".dup
  case i
  when nil
    i = string? if string?
  end
  i = i.dup if i.frozen?
  # ======================================================================= #
  # == Handle blocks given next
  # ======================================================================= #
  if block_given?
    yielded = yield
    case yielded
    # ===================================================================== #
    # === :colourize_start_codon
    # ===================================================================== #
    when :colourize_start_codon
      do_colourize_the_start_codon
    # ===================================================================== #
    # === :show_ruler
    # ===================================================================== #
    when :show_ruler
      show_ruler = true
    # ===================================================================== #
    # === :without_colours
    # ===================================================================== #
    when :without_colours
      show_ruler = yielded
      do_show_the_ruler
    # ===================================================================== #
    # === :complementary_strand
    #
    # When we use this entry point, specifically, we will also
    # revert leader and trailer.
    # ===================================================================== #
    when :complementary_strand
      result << return_three_prime_leader
      i = complementary_strand(
        i.chars
      )
      show_five_prime_leader = false
      show_three_prime_trailer = :use_five_prime_trailer
    # ===================================================================== #
    # === :to_rna
    # ===================================================================== #
    when :to_rna,
         :rna
      i = to_rna(i)
    else
      if yielded.is_a? Hash
        # ================================================================= #
        # === :use_this_as_padding
        # ================================================================= #
        if yielded.has_key? :use_this_as_padding
          padding_to_use = yielded[:use_this_as_padding]
        end
        # ================================================================= #
        # === :colourize_this_subsequence
        # ================================================================= #
        if yielded.has_key? :colourize_this_subsequence
          set_search_for(yielded[:colourize_this_subsequence])
        end
        # ================================================================= #
        # === :ruler
        # ================================================================= #
        if yielded.has_key? :ruler
          _ = yielded.delete(:ruler)
          case _
          when :without_colours
            show_ruler = _
          end
        end
      end
    end
  end
  # ======================================================================= #
  # Consider showing the ruler next:
  # ======================================================================= #
  if show_ruler
    result_as_string = ::Bioroebe::Ruler.new(i, :default) {{ ruler: show_ruler }}.result_as_string?
    erev padding_to_use+
         result_as_string
  end
  if show_five_prime_leader
    result << return_five_prime_leader
  end
  # ======================================================================= #
  # We will next colourize the nucleotides that we determined should
  # be colourized, in particular ATG start codons.
  # ======================================================================= #
  if @use_colours and !@array_colourize_these_substrings.empty?
    # ===================================================================== #
    # In this case we will try to colourize substrings.
    # ===================================================================== #
    @array_colourize_these_substrings.each {|this_substring|
      if i and i.include?(this_substring)
        i.prepend(return_colour_for_the_dna_sequence)
        i.gsub!(
          /(#{this_substring})/,
          crimson('\1')+
          remove_trailing_escape_part(colour_for_nucleotides)
        )
      end
    }
  end
  # ======================================================================= #
  # Support for dashed-form display comes next:
  # ======================================================================= #
  if @show_in_dashed_form
    how_many_times = 3
    use_this_token_for_rejoining = '-'
    n_times = '.' * how_many_times.to_i
    use_this_regex = /#{n_times}/
    i = i.scan(use_this_regex).join(use_this_token_for_rejoining)
  end
  result << colourize_dna_sequence(i)+
            rev
  if show_three_prime_trailer
    case show_three_prime_trailer
    when :use_five_prime_trailer
      result << " - 5'"
    else
      result << " - 3'"
    end
  end
  set_sequence_for_display(
    result
  )
end
#

menu (menu tag)

#

363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 363

def menu(
    i = @commandline_arguments
  )
  if i and i.is_a?(Array)
    i.each {|entry| menu(entry) }
  else
    case i
    # ===================================================================== #
    # === --ruler
    #
    # This can be invoked by doing something like:
    #
    #   show_nucleotide_sequence ATGATTGCACGACACACATAAA --ruler
    #
    # ===================================================================== #
    when /^-?-?ruler$/i
      @show_ruler = true
    end
  end
end

#report(i = @sequence_for_display, &block) ⇒ Object Also known as: display

#

report (report tag)

This method will only do the reporting by default - not a new format-operation, unless a block has been given to it.

#

452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 452

def report(
    i = @sequence_for_display,
    &block
  )
  if block_given?
    yielded = yield
    case yielded
    # ===================================================================== #
    # === :colourize_start_codon
    #
    # When a block is given then we actually have to re-format the main
    # string again.
    # ===================================================================== #
    when :colourize_start_codon
      do_colourize_the_start_codon
      format_this_string(i)
    # ===================================================================== #
    # === :complementary_strand
    # ===================================================================== #
    when :complementary_strand
      i = ::Bioroebe.complementary_strand(i)
    end
  end
  erev i
end

#report_this_sequence(i, &block) ⇒ Object

#

report_this_sequence

Usage example for this method:

@show_nucleotide_sequence.report_this_sequence(input) {{ padding_to_use: padding? }}
#

406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 406

def report_this_sequence(
    i, &block
  )
  if block_given?
    yielded = yield
    if yielded.is_a? Symbol
      case yielded
      # =================================================================== #
      # === :piped
      # =================================================================== #
      when :piped
        i.gsub!(/(...)/, "\\1|") # Add | at every third position.
      end
    elsif yielded.is_a? Hash
      # =================================================================== #
      # === :colourize_this_subsequence
      # =================================================================== #
      if yielded.has_key? :colourize_this_subsequence
        search_for_this_substring(
          yielded[:colourize_this_subsequence]
        )
      end
      # =================================================================== #
      # === :show_piped_output
      # =================================================================== #
      if yielded.has_key?(:show_piped_output) and (yielded[:show_piped_output] == true)
        i = i.gsub(/(...)/, "\\1|") # Add | at every third position.
      end
      # =================================================================== #
      # === :padding_to_use
      # =================================================================== #
      if yielded.has_key? :padding_to_use
        set_padding(yielded.delete(:padding_to_use))
      end
    end
  end
  format_this_nucleotide_sequence(i)
  report # Do not specify (i) here, as we will use the formatted-sequence for display instead.
end

#resetObject

#

reset

#

111
112
113
114
115
116
117
118
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 111

def reset
  super()
  # ======================================================================= #
  # === @commandline_arguments
  # ======================================================================= #
  @commandline_arguments = []
  # ======================================================================= #
  # === @sequence_for_display
  # ======================================================================= #
  @sequence_for_display     = nil
  # ======================================================================= #
  # === @may_we_report
  # ======================================================================= #
  @may_we_report            = true
  # ======================================================================= #
  # === @use_colours
  # ======================================================================= #
  @use_colours              = true
  # ======================================================================= #
  # === @show_five_prime_leader
  # ======================================================================= #
  @show_five_prime_leader   = true
  # ======================================================================= #
  # === @show_three_prime_trailer
  # ======================================================================= #
  @show_three_prime_trailer = true
  # ======================================================================= #
  # === @padding_to_use
  #
  # How much padding to use when displaying something.
  # ======================================================================= #
  @padding_to_use = '       '.dup
  # ======================================================================= #
  # === @show_in_dashed_form
  #
  # The dashed form is e. g. "ATG-GGC-CGC". By default this is not
  # enable.
  # ======================================================================= #
  @show_in_dashed_form = false
  # ======================================================================= #
  # === @show_ruler
  #
  # The next variable determines whether a ruler will be displayed
  # on top of the nucleotide-sequence.
  # ======================================================================= #
  @show_ruler          = false
  # ======================================================================= #
  # === @array_colourize_these_substrings
  # ======================================================================= #
  @array_colourize_these_substrings = []
end

#return_colour_for_the_dna_sequenceObject

#

return_colour_for_the_dna_sequence

#

489
490
491
492
493
494
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 489

def return_colour_for_the_dna_sequence
  result = Colours.remove_trailing_code(
    ::Colours.send(USE_THIS_COLOUR)
  )
  return result
end

#return_five_prime_leaderObject

#

return_five_prime_leader

#

187
188
189
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 187

def return_five_prime_leader
  "5' - "
end

#return_three_prime_leaderObject

#

return_three_prime_leader

#

180
181
182
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 180

def return_three_prime_leader
  "3' - "
end

#runObject

#

run

#

535
536
537
538
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 535

def run
  format_this_nucleotide_sequence
  report if @may_we_report
end

#search_for_this_substring(i) ⇒ Object Also known as: set_search_for, add_this_substring

#

search_for_this_substring

This method can be used to search for substrings, aka matches.

#

513
514
515
516
517
518
519
520
521
522
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 513

def search_for_this_substring(i)
  if i.is_a? Array
    i.each {|entry| search_for_this_substring(entry) }
  else
    unless @array_colourize_these_substrings.include? i
      @array_colourize_these_substrings << i
      @array_colourize_these_substrings.compact! # Added in 2021, to remove possible nil-values.
    end
  end
end

#sequence_for_display?Boolean Also known as: formatted_sequence?, sequence_for_display

#

sequence_for_display?

This method will return the sequence that will be displayed on the commandline, for instance.

#

Returns:

  • (Boolean)

204
205
206
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 204

def sequence_for_display?
  @sequence_for_display
end

#set_padding(i) ⇒ Object

#

set_padding

#

173
174
175
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 173

def set_padding(i)
  @padding_to_use = i
end

#set_sequence_for_display(i) ⇒ Object

#

set_sequence_for_display

#

194
195
196
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 194

def set_sequence_for_display(i)
  @sequence_for_display = i
end

#show_ruler?Boolean

#

show_ruler?

#

Returns:

  • (Boolean)

387
388
389
# File 'lib/bioroebe/nucleotides/show_nucleotide_sequence.rb', line 387

def show_ruler?
  @show_ruler
end