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

#

initialize

#

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
107
108
109
110
111
112
113
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 52

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
    when :be_verbose
      @be_verbose = true
    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 = '') ⇒ Object

#

Bioroebe::DnaToAminoacidSequence[]

#

519
520
521
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 519

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

Instance Method Details

#be_quietObject

#

be_quiet

#

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

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

#be_quiet_and_no_coloursObject

#

be_quiet_and_no_colours

#

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

def be_quiet_and_no_colours
  be_quiet
  disable_colours
end

#do_report_the_reading_frame(i = @result) ⇒ Object

#

do_report_the_reading_frame

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

#

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

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
    e "  #{@result}"
    e N
  end
end

#do_show_as_the_three_letter_aminoacid_sequenceObject

#

do_show_as_the_three_letter_aminoacid_sequence

#

169
170
171
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 169

def do_show_as_the_three_letter_aminoacid_sequence
  @internal_hash[:show_as_three_letter_aminoacid_sequence] = 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.

#

464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 464

def do_the_conversion_step
  # ======================================================================= #
  # Use the proper, designated codon-table.
  # ======================================================================= #
  ::Bioroebe.load_the_codon_table_dataset(@use_the_codon_table_for)
  _ = first_argument?.dup
  if _ and _.include?('.') and File.file?(_)
    _ = File.read(_).strip # Add support for reading local files here.
  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)
    @result << translated_codon.to_s
    if @stop_at_the_first_stop_codon and (translated_codon == '*')
      break
    end
  }
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 –).

#

367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
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
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 367

def menu(
    i = return_commandline_arguments_starting_with_two_hyphens
  )
  if i.is_a? Array
    i.each {|entry| menu(entry) }
  else
    case i
    # ===================================================================== #
    # === 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

#

253
254
255
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 253

def no_output
  @internal_hash[:report_result] = false
end

#one_two_or_three(i) ⇒ Object

#

one_two_or_three

#

206
207
208
209
210
211
212
213
214
215
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 206

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

#reportObject

#

report

#

271
272
273
274
275
276
277
278
279
280
281
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 271

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)

246
247
248
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 246

def report_result?
  @internal_hash[:report_result]
end

#resetObject

#

reset (reset tag)

#

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

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
  # ======================================================================= #
  # === @colourize_stop_codons
  #
  # If the following variable is set to true then any stop codon, aka
  # '*', will appear in the colour orchid.
  # ======================================================================= #
  @colourize_stop_codons = true
  # ======================================================================= #
  # === @internal_hash
  # ======================================================================= #
  @internal_hash = {}
  # ======================================================================= #
  # === :report_result
  # ======================================================================= #
  @internal_hash[:report_result] = true
  # ======================================================================= #
  # === :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
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)

322
323
324
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 322

def result?
  @result
end

#runObject

#

run (run tag)

#

510
511
512
513
514
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 510

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

#

260
261
262
263
264
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 260

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.

#

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

def set_use_frame_number(i = :default)
  case i
  # ======================================================================= #
  # First comes the default.
  # ======================================================================= #
  when :one,
       :default,
       '1'
    i = :frame_one
  when :two,
       '2'
    i = :frame_two
  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

#

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

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)

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

def show_as_three_letter_aminoacid_sequence?
  @internal_hash[:show_as_three_letter_aminoacid_sequence]
end

#show_helpObject

#

show_help (help tag)

#

345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 345

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
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.

#

336
337
338
339
340
# File 'lib/bioroebe/conversions/dna_to_aminoacid_sequence.rb', line 336

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