Class: Bioroebe::Compacter

Inherits:
CommandlineApplication show all
Defined in:
lib/bioroebe/utility_scripts/compacter/compacter.rb

Overview

Bioroebe::Compacter

Constant Summary

Constants inherited from CommandlineApplication

Bioroebe::CommandlineApplication::OLD_VERBOSE_VALUE

Constants included from ColoursForBase

Bioroebe::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 = true, &block) ⇒ Compacter

#

initialize

#


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bioroebe/utility_scripts/compacter/compacter.rb', line 29

def initialize(
    commandline_arguments = nil,
    run_already           = true,
    &block
  )
  register_sigint
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  # ======================================================================= #
  # === Handle blocks given to this class next
  # ======================================================================= #
  if block_given?
    yielded = yield
    case yielded
    # ===================================================================== #
    # === :do_not_ask_for_user_input
    # ===================================================================== #
    when :do_not_ask_for_user_input
      @internal_hash[:ask_for_user_input] = false
    end
  end
  run if run_already
end

Class Method Details

.[](i = ARGV) ⇒ Object

#

Bioroebe::Compacter[]

#


231
232
233
# File 'lib/bioroebe/utility_scripts/compacter/compacter.rb', line 231

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

Instance Method Details

#ask_for_user_input?Boolean

#

ask_for_user_input?

#

Returns:

  • (Boolean)


149
150
151
# File 'lib/bioroebe/utility_scripts/compacter/compacter.rb', line 149

def ask_for_user_input?
  @internal_hash[:ask_for_user_input]
end

#consider_working_through_the_input_filesObject

#

consider_working_through_the_input_files

#


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/bioroebe/utility_scripts/compacter/compacter.rb', line 156

def consider_working_through_the_input_files
  _ = @internal_hash[:work_on_these_files]
  if _.empty?
    opne 'Please provide at the least ony locally existing file as'
    opne 'input to this class.'
  else
    perform_these_actions_on_each_file = perform_these_actions_on_each_file?
    _.each {|this_file|
      if File.exist? this_file
        # ================================================================= #
        # Tell the user which file we will compact next.
        # ================================================================= #
        opne "#{rev}Working to compact the following file next: "\
             "`#{sfile(this_file)}#{rev}`." if be_verbose?
        dataset = default_readlines(this_file)
        # ================================================================= #
        # First, check for ORIGIN; or a FASTA header that begins with '>'.
        # ================================================================= #
        if dataset.first
          first = dataset.first
          if first.start_with? 'ORIGIN'
            dataset[0][0,'ORIGIN'.size] = ''
            dataset[0].lstrip!
          elsif first.start_with? '>'
            # =============================================================== #
            # In this case, chop off the first line completely.
            # =============================================================== #
            dataset[0][0, first.index("\n")] = ''
            dataset[0].lstrip!
          end
        end
        if perform_these_actions_on_each_file.include? :strip_newlines
          dataset.map! {|entry| entry.strip }
        end
        if perform_these_actions_on_each_file.include? :remove_numbers
          dataset.map! {|entry| entry.delete('[0-9]') }
        end
        if perform_these_actions_on_each_file.include? :upcase_the_sequence
          dataset.map! {|entry| entry.upcase }
        end
        if perform_these_actions_on_each_file.include? :eliminate_spaces
          dataset.map! {|entry| entry.delete(' ') }
        end
        # ================================================================= #
        # Next we tap into class Bioroebe::SanitizeNucleotideSequence
        # ================================================================= #
        dataset = Bioroebe::SanitizeNucleotideSequence[dataset] { :do_not_remove_newlines }
        if ask_for_user_input?
          opne 'We will now store into the same file, but you must'
          opne 'type "'+steelblue('y')+'" to confirm, else we cancel:'
          user_input = $stdin.gets.chomp
        else
          user_input = 'yes'
        end
        if user_input.start_with? 'y'
          opne "Now storing into the file `#{sfile(this_file)}`." if be_verbose?
          write_what_into(dataset, this_file)
        end
      end # no else clause is possible here, due to a prior if-check.
    }
  end
end

#determine_the_input_filesObject

#

determine_the_input_files

#


131
132
133
134
135
136
137
# File 'lib/bioroebe/utility_scripts/compacter/compacter.rb', line 131

def determine_the_input_files
  commandline_arguments?.each {|this_entry|
    if File.file?(this_entry)
      @internal_hash[:work_on_these_files] << this_entry
    end
  }
end
#

menu (menu tag)

#


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
# File 'lib/bioroebe/utility_scripts/compacter/compacter.rb', line 87

def menu(
    i = commandline_arguments?
  )
  if i.is_a? Array
    i.each {|entry| menu(entry) }
  else
    case i # case tag
    # ===================================================================== #
    # === --retain-newlines
    #
    # Usage example:
    #
    #   compacter SPRR4_protein.fasta --retain-newlines
    #   compacter *eukin*.fasta --retain-newlines
    #
    # ===================================================================== #
    when /-?-?retain(-|_)?newlines/
      @internal_hash[:perform_these_actions_on_each_file].reject! {|entry|
        entry == :strip_newlines
      }
    # ===================================================================== #
    # === --help
    # ===================================================================== #
    when /help/
      show_help
      exit
    end
  end
end

#perform_these_actions_on_each_file?Boolean

#

perform_these_actions_on_each_file?

#

Returns:

  • (Boolean)


142
143
144
# File 'lib/bioroebe/utility_scripts/compacter/compacter.rb', line 142

def perform_these_actions_on_each_file?
  @internal_hash[:perform_these_actions_on_each_file]
end

#resetObject

#

reset (reset tag)

#


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
# File 'lib/bioroebe/utility_scripts/compacter/compacter.rb', line 58

def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === :work_on_these_files
  # ======================================================================= #
  @internal_hash[:work_on_these_files] = []
  # ======================================================================= #
  # === :ask_for_user_input
  # ======================================================================= #
  @internal_hash[:ask_for_user_input] = true
  # ======================================================================= #
  # === :perform_these_actions_on_each_file
  #
  # The following Array specifies which actions are to be performed onto
  # the target file at hand. The user can modify this, to adjust the
  # behaviour of this class.
  # ======================================================================= #
  @internal_hash[:perform_these_actions_on_each_file] = [
    :strip_newlines,
    :remove_numbers,
    :upcase_the_sequence,
    :eliminate_spaces
  ]
end

#runObject

#

run (run tag)

#


222
223
224
225
226
# File 'lib/bioroebe/utility_scripts/compacter/compacter.rb', line 222

def run
  menu
  determine_the_input_files
  consider_working_through_the_input_files
end

#show_helpObject

#

show_help (help tag)

#


120
121
122
123
124
125
126
# File 'lib/bioroebe/utility_scripts/compacter/compacter.rb', line 120

def show_help
  opnn { :no_trailing }
  e
  e
  e '  --retain-newlines # retain newlines, aka do not dump into a single line'
  e
end