Class: Bioroebe::DnaToAminoacidSequence

Inherits:
CommandlineApplication show all
Defined in:
lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb

Overview

Bioroebe::DnaToAminoacidSequence

Constant Summary collapse

DEFAULT_RUN_MODE =
#

DEFAULT_RUN_MODE

#
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, #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(commandline_arguments = nil, run_already = DEFAULT_RUN_MODE) ⇒ DnaToAminoacidSequence

#

initialize

#


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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 53

def initialize(
    commandline_arguments = nil,
    run_already           = DEFAULT_RUN_MODE
  )
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  # ======================================================================= #
  # === Handle blocks given next:
  # ======================================================================= #
  if block_given?
    yielded = yield
    case yielded
    # ===================================================================== #
    # === :be_verbose
    # ===================================================================== #
    when :be_verbose
      set_be_verbose
    # ===================================================================== #
    # === :be_quiet
    # ===================================================================== #
    when :be_quiet
      be_quiet
    end
  end
  case run_already # case tag
  # ======================================================================= #
  # === :frame_two
  # ======================================================================= #
  when :frame_two,
       :two
    set_use_frame_number(:two)
    run_already = DEFAULT_RUN_MODE
  # ======================================================================= #
  # === :frame_three
  # ======================================================================= #
  when :frame_three,
       :three
    set_use_frame_number(:three)
    run_already = DEFAULT_RUN_MODE
  # ======================================================================= #
  # === :be_quiet_and_no_colours_and_no_output,
  # ======================================================================= #
  when :be_quiet_and_no_colours_and_no_output,
       :return_silently
    be_quiet
    disable_colours
    no_output
    run_already = DEFAULT_RUN_MODE
  # ======================================================================= #
  # === :be_quiet_and_no_colours
  # ======================================================================= #
  when :be_quiet_and_no_colours
    be_quiet
    disable_colours
    run_already = DEFAULT_RUN_MODE
  # ======================================================================= #
  # === :be_quiet
  # ======================================================================= #
  when :be_quiet
    be_quiet
    run_already = DEFAULT_RUN_MODE
  when :do_not_run_yet
    run_already = false
  end
  run if run_already
end

Class Method Details

.[](i = ARGV) ⇒ Object

#

Bioroebe::DnaToAminoacidSequence[]

#


596
597
598
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 596

def self.[](i = ARGV)
  new(i) { :be_quiet }.result
end

Instance Method Details

#be_quietObject

#

be_quiet

#


204
205
206
207
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 204

def be_quiet
  set_be_quiet
  @internal_hash[:report_result] = false
end

#be_quiet_and_no_coloursObject

#

be_quiet_and_no_colours

#


212
213
214
215
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 212

def be_quiet_and_no_colours
  be_quiet
  disable_colours
end

#colourize_stop_codons?Boolean

#

colourize_stop_codons?

#

Returns:

  • (Boolean)


176
177
178
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 176

def colourize_stop_codons?
  @internal_hash[:colourize_stop_codons]
end

#do_report_the_reading_frame(i = @result) ⇒ Object

#

do_report_the_reading_frame

This method will, with colours, output the full reading frame.

#


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
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 321

def do_report_the_reading_frame(
    i = @result
  )
  # ======================================================================= #
  # Always assign to @result next.
  # ======================================================================= #
  if show_as_three_letter_aminoacid_sequence?
    i = one_to_three(i)
  end
  result = "#{lightgreen(i)}#{rev}"
  if colourize_stop_codons? and result.include?('*')
    result = result.dup if result.frozen?
    # ===================================================================== #
    # Colourize stop codons in orchid. 
    # ===================================================================== #
    result.gsub!(
      /\*/,
      orchid('*')+
      remove_trailing_ansii_escape_code(lightgreen(''))
    )
  end
  set_result(result)
  if report_result? and be_verbose?
    if show_ruler? # Show a pretty ruler here.
      e "  #{Bioroebe.return_ruler(i)}"
    end
    e "  #{@result}"
    e N
  end
end

#do_show_as_the_three_letter_aminoacid_sequenceObject

#

do_show_as_the_three_letter_aminoacid_sequence

#


183
184
185
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 183

def do_show_as_the_three_letter_aminoacid_sequence
  @internal_hash[:show_as_three_letter_aminoacid_sequence] = true
end

#do_show_rulerObject

#

do_show_ruler

#


573
574
575
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 573

def do_show_ruler
  @internal_hash[:show_ruler] = true
end

#do_the_conversion_stepObject

#

do_the_conversion_step

This method will do the actual DNA-to-aminoacid conversion step.

Make sure that the correct reading frame has been “selected” before calling this method.

#


493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 493

def do_the_conversion_step
  # ======================================================================= #
  # Use the proper, designated codon-table.
  # ======================================================================= #
  ::Bioroebe.load_the_codon_table_dataset(@use_the_codon_table_for)
  _ = ensure_DNA_sequence(
    commandline_arguments?.join(' ').strip.dup.
    delete(' ').
    delete('|'). # We don't want any '|' here.
    delete('-')  # And we don't want any "-" there either.
  )
  if _ and _.include?('.') and File.file?(_)
    _ = File.read(_).strip # Add support for reading local files here.
  end
  if _ =~ /\d+/ # Handle input such as "5'-ATGAATAGGAAGACCTAA-3'".
    _.delete!('[0-9]')
  end
  # ======================================================================= #
  # Here we have to honour the correct reading frame:
  # ======================================================================= #
  case @use_frame_number
  when :frame_two
    _[0,1] = ''
  when :frame_three
    _[0,2] = ''
  end
  unless _
    erev 'Please supply a sequence to this class.'
    exit
  end
  splitted = _.scan(/.../)
  @result = ''.dup
  splitted.each {|this_codon|
    # ===================================================================== #
    # === Do convert the DNA sequence into the corresponding aminoacid
    #     sequence next.
    #
    # Find the corresponding aminoacid next, by looking at the
    # codon_table_dataset. We will replace all 'U' with 'T' to
    # support RNA as input as well, just in case.
    #
    # The result is stored in the variable called @result.
    # ===================================================================== #
    translated_codon = translate_dna_into_aminoacid(this_codon).to_s
    @result << translated_codon
    if @stop_at_the_first_stop_codon and (translated_codon == '*')
      break
    end
  }
end

#ensure_DNA_sequence(i) ⇒ Object

#

ensure_DNA_sequence

#


547
548
549
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 547

def ensure_DNA_sequence(i)
  return i.tr('U','T')
end
#

menu (menu tag)

These are the registered commandline arguments for this class.

We will act only on arguments given to this method, staring with two hyphens (aka –).

#


386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
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
445
446
447
448
449
450
451
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
477
478
479
480
481
482
483
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 386

def menu(
    i = return_commandline_arguments_starting_with_two_hyphens
  )
  if i.is_a? Array
    i.each {|entry| menu(entry) }
  else
    case i
    # ===================================================================== #
    # === Show a helpful ruler
    #
    # Usage example:
    #
    #   dnatoaminoacidsequence AUGUUU --ruler
    #
    # ===================================================================== #
    when /^-?-?ruler$/i
      do_show_ruler
    # ===================================================================== #
    # === Show the aminoacids as three-letter code
    #
    # Usage example:
    #
    #   dnatoaminoacidsequence AUGUUU --three-letter
    #
    # ===================================================================== #
    when /^-?-?three(-|_| )?letter$/i
      do_show_as_the_three_letter_aminoacid_sequence
    # ===================================================================== #
    # === Show the help section
    #
    # Usage example:
    #
    #   dnatoaminoacidsequence --help
    #
    # ===================================================================== #
    when /^-?-?help$/i
      show_help
      exit
    # ===================================================================== #
    # === Start the GUI component
    #
    # Usage example:
    #
    #   dnatoaminoacidsequence --gui
    #
    # ===================================================================== #
    when /^-?-?gui$/i
      require 'bioroebe/gui/gtk3/dna_to_aminoacid_widget/dna_to_aminoacid_widget.rb'
      ::Bioroebe::GUI::Gtk::DnaToAminoacidWidget.run
      exit
    # ===================================================================== #
    # === Stop at the first stop codon
    #
    # This entry point allows us to stop at the first found stop codon.
    #
    # Normally the whole aminoacid sequence would be shown, but via
    # the --stop-at-the-first-stop-codon flag we can stop right at
    # the first found '*' token.
    #
    # Usage example:
    #
    #   dna_to_aminoacid_sequence ATGATGCCTCCAGGG --stop-at-the-first-stop-codon
    #
    # ===================================================================== #
    when /^-?-?stop(-|_| )?at(-|_| )?the(-|_| )?first(-|_| )?stop(-|_| )?codon$/i
      @stop_at_the_first_stop_codon = true
    # ===================================================================== #
    # === Use a specific frame
    #
    # This entry point can be used to designate a specific frame,
    # such as frame2 or frame3. (Specifying frame1 won't make
    # a whole lot of sense, though, but it exists primarily for
    # debugging purposes).
    #
    # Usage example:
    #
    #   dna_to_aminoacid_sequence ATGATGCCTCCAGGG --frame1
    #   dna_to_aminoacid_sequence ATGATGCCTCCAGGG --frame2
    #   dna_to_aminoacid_sequence ATGATGCCTCCAGGG --frame3
    #
    # ===================================================================== # 
    when /^-?-?frame(\d{1})$/i
      set_use_this_reading_frame($1.to_s.dup)
    # ===================================================================== #
    # === dna_to_aminoacid_sequence ATGATGCCTCCA
    #
    # Usage example:
    #
    #   dna_to_aminoacid_sequence ATGATGCCTCCAGGG --use-this-codon-table=bacteria
    #   dna_to_aminoacid_sequence ATGATGCCTCCAGGG --use-this-codon-table=humans
    #
    # ===================================================================== #
    when /^-?-?use(-|_| )?this(-|_| )?codon(-|_| )?table=(.+)$/i # === $4
      _ = $4.to_s.dup.to_sym
      set_use_the_codon_table_for(_)
    end
  end
end

#no_outputObject

#

no_output

#


286
287
288
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 286

def no_output
  @internal_hash[:report_result] = false
end

#one_two_or_three(i) ⇒ Object

#

one_two_or_three

#


220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 220

def one_two_or_three(i)
  case i
  # ======================================================================= #
  # === :frame_one
  # ======================================================================= #
  when :frame_one
    '1'
  # ======================================================================= #
  # === :frame_two
  # ======================================================================= #
  when :frame_two
    '2'
  # ======================================================================= #
  # === :frame_three
  # ======================================================================= #
  when :frame_three
    '3'
  end
end

#reportObject

#

report

#


304
305
306
307
308
309
310
311
312
313
314
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 304

def report
  if be_verbose?
    e
    coloured_frame = palegreen(
      'frame-'+one_two_or_three(@use_frame_number)
    )+rev
    e "#{rev}The #{coloured_frame}#{rev} sequence is: (#{sequence?.size} aminoacids)"
    e
  end
  do_report_the_reading_frame
end

#report_result?Boolean

#

report_result?

#

Returns:

  • (Boolean)


279
280
281
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 279

def report_result?
  @internal_hash[:report_result]
end

#resetObject

#

reset (reset tag)

#


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
162
163
164
165
166
167
168
169
170
171
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 125

def reset
  super()
  set_use_frame_number(:one) # Default is reading frame number one.
  # ======================================================================= #
  # === @codon_table_dataset
  #
  # Obtain the codon table dataset next.
  # ======================================================================= #
  @codon_table_dataset = ::Bioroebe.codon_table_dataset?
  # ======================================================================= #
  # === @stop_at_the_first_stop_codon
  # ======================================================================= #
  @stop_at_the_first_stop_codon = false
  # ======================================================================= #
  # === @result
  # ======================================================================= #
  @result = nil
  # ======================================================================= #
  # === @use_the_codon_table_for
  #
  # Specifiy which codon table is to be used. By default we will use
  # the codon table for humans.
  # ======================================================================= #
  @use_the_codon_table_for = :humans
  # ======================================================================= #
  # === :report_result
  # ======================================================================= #
  @internal_hash[:report_result] = true
  # ======================================================================= #
  # === :show_ruler
  # ======================================================================= #
  @internal_hash[:show_ruler] = false
  # ======================================================================= #
  # === :show_as_three_letter_aminoacid_sequence
  #
  # If this is true then this class will show "Phe" rather than "F" for
  # that amino acid. The same is true for all the other aminoacids as well.
  # ======================================================================= #
  @internal_hash[:show_as_three_letter_aminoacid_sequence] = false
  # ======================================================================= #
  # === :colourize_stop_codons
  #
  # If the following variable is set to true then any stop codon, aka
  # '*', will appear in the colour orchid.
  # ======================================================================= #
  @internal_hash[:colourize_stop_codons] = true
end

#result?Boolean Also known as: result, input_sequence?, seq?, sequence?, sequence

#

result?

This variable will hold the translated sequence ready, e. g. the aminoacid sequence.

#

Returns:

  • (Boolean)


358
359
360
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 358

def result?
  @result
end

#runObject

#

run (run tag)

#


587
588
589
590
591
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 587

def run
  menu
  do_the_conversion_step
  report
end

#set_result(i) ⇒ Object Also known as: use_this_sequence, use_this_sequence=, set_sequence

#

set_result

#


293
294
295
296
297
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 293

def set_result(i)
  i = i.join if i.is_a? Array
  i.delete!('|') if i.include? '|'
  @result = i
end

#set_use_frame_number(i = :default) ⇒ Object Also known as: use_this_reading_frame=, set_use_this_reading_frame

#

set_use_frame_number

Designate which frame is to be used through this method. By default it will be frame number 1.

#


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
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 246

def set_use_frame_number(
    i = :default
  )
  case i
  # ======================================================================= #
  # === :one
  #
  # First comes the default.
  # ======================================================================= #
  when :one,
       :default,
       '1'
    i = :frame_one
  # ======================================================================= #
  # === :two
  # ======================================================================= #
  when :two,
       '2'
    i = :frame_two
  # ======================================================================= #
  # === :three
  # ======================================================================= #
  when :three,
       '3'
    i = :frame_three
  end
  @use_frame_number = i
end

#set_use_the_codon_table_for(i) ⇒ Object

#

set_use_the_codon_table_for

#


197
198
199
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 197

def set_use_the_codon_table_for(i)
  @use_the_codon_table_for = i
end

#show_as_three_letter_aminoacid_sequence?Boolean

#

show_as_three_letter_aminoacid_sequence?

#

Returns:

  • (Boolean)


190
191
192
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 190

def show_as_three_letter_aminoacid_sequence?
  @internal_hash[:show_as_three_letter_aminoacid_sequence]
end

#show_helpObject

#

show_help (help tag)

#


554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 554

def show_help
  e 'Available options:'
  e
  e '  --gui                           # start the GUI'
  e '  --stop-at-the-first-stop-codon  # stop at the first stop codon encountered'
  e '  --frame1                        # show the first frame translation only'
  e '  --frame2                        # show the second frame translation only'
  e '  --frame3                        # show the third frame translation only'
  e '  --use-this-codon-table=bacteria # specify a specific codon table '\
    '(in this case bacteria)'
  e '  --three-letter                  # show the three letter amino acid code'
  e '  --ruler                         # show a helpful ruler on top '\
    'of the sequence'
  e
end

#show_ruler?Boolean

#

show_ruler?

#

Returns:

  • (Boolean)


580
581
582
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 580

def show_ruler?
  @internal_hash[:show_ruler]
end

#translate_dna_into_aminoacid(i) ⇒ Object

#

translate_dna_into_aminoacid

This method will perform the lookup onto the hash (codon table dataset), in order to find the corresponding aminoacid.

#


372
373
374
375
376
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 372

def translate_dna_into_aminoacid(i)
  @codon_table_dataset[
    i.tr('U','T')
  ]
end