Class: Bioroebe::BlosumParser

Inherits:
CommandlineApplication show all
Defined in:
lib/bioroebe/parsers/blosum_parser.rb

Overview

Bioroebe::BlosumParser

Constant Summary collapse

VERTICAL_LINE =
#

VERTICAL_LINE

#
"|"

Constants inherited from CommandlineApplication

CommandlineApplication::OLD_VERBOSE_VALUE

Constants included from ColoursForBase

ColoursForBase::ARRAY_HTML_COLOURS_IN_USE

Constants inherited from Base

Bioroebe::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(i = '', run_already = true) ⇒ BlosumParser

#

initialize

The first argument should be the blosum file that we wish to use.

#


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bioroebe/parsers/blosum_parser.rb', line 33

def initialize(
    i           = '',
    run_already = true
  )
  reset
  set_blosum_file_to_use(i)
  if block_given?
    case yield
    # ===================================================================== #
    # === :be_quiet
    # ===================================================================== #
    when :be_quiet
      set_be_quiet
    end
  end
  run if run_already
end

Class Method Details

.return_as_2D_table(i = :blosum62) ⇒ Object

#

Bioroebe::BlosumParser.return_as_2D_table

#


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/parsers/blosum_parser.rb', line 140

def self.return_as_2D_table(i = :blosum62)
  @result = ''.dup
  if i.is_a? Array
    i = i.first # Only support the first element of an Array.
  end
  _ = Bioroebe::BlosumParser.new(i) { :be_quiet }
  hash = _.hash?
  @result << i.to_s.capitalize+N
  @result << N
  array = %w( A R N D C Q E G H I L K M F P S T W Y V B J Z X * )
  @result << '   '
  array.each {|entry| @result << "#{entry.to_s.rjust(3, ' ')}" }
  @result << N
  @result << '―'* 80
  @result << N
  array.each_with_index {|entry, index|
    @result << "#{entry} #{VERTICAL_LINE}"
    array.each_with_index {|inner_entry, inner_index|
      key_to_use = entry+inner_entry
      if hash.has_key? key_to_use
        match = hash[key_to_use]
        @result << "#{match.to_s.rjust(3, ' ')}"
      else
        @result << "No key found for #{key_to_use}."
      end
    }
    @result << N
  }
  @result << '―'* 80
  @result << N
  return @result
end

.show_as_2D_table(i = :blosum62) ⇒ Object

#

Bioroebe::BlosumParser.show_as_2D_table

#


133
134
135
# File 'lib/bioroebe/parsers/blosum_parser.rb', line 133

def self.show_as_2D_table(i = :blosum62)
  print Bioroebe::BlosumParser.return_as_2D_table(i)
end

Instance Method Details

#check_if_the_file_existsObject

#

check_if_the_file_exists

#


96
97
98
99
100
101
102
103
# File 'lib/bioroebe/parsers/blosum_parser.rb', line 96

def check_if_the_file_exists
  _ = @blosum_file_to_use.to_s
  if File.exist? _
    # Pass through - all is fine.
  else
    erev "The file at `#{sfile(_)}#{rev}` does not exist."
  end
end

#dataset?Boolean Also known as: data?, hash, hash?

#

dataset?

#

Returns:

  • (Boolean)


108
109
110
# File 'lib/bioroebe/parsers/blosum_parser.rb', line 108

def dataset?
  @hash
end

#resetObject

#

reset

#


54
55
56
57
58
59
60
61
# File 'lib/bioroebe/parsers/blosum_parser.rb', line 54

def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === @hash
  # ======================================================================= #
  @hash = {} # Initialize our main Hash here.
end

#runObject

#

run

#


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
# File 'lib/bioroebe/parsers/blosum_parser.rb', line 176

def run
  check_if_the_file_exists
  if File.exist? @blosum_file_to_use
    dataset = File.readlines(@blosum_file_to_use).map {|entry|
      entry.strip
    }
    # ======================================================================= #
    # === Eliminate comments from the file
    #
    # Next, we get rid of all rows that start with '#' character, as
    # we assume these to be comments.
    # ======================================================================= #
    dataset.reject! {|line| line.start_with?('#') or line.empty? }
    # ======================================================================= #
    # First, we obtain the headers in the proper order.
    # ======================================================================= #
    headers = dataset.shift.split(' ')
    # ======================================================================= #
    # Next we can iterate over the remaining dataset - the first entry will
    # be one part of the key in the hash.
    # ======================================================================= #
    dataset.each {|line|
      splitted = line.split(' ')
      which_aminoacid = splitted.shift # Get the first entry.
      splitted.each_with_index {|the_score, index|
        key = which_aminoacid + headers[index]
        @hash[key] = the_score.to_i
      }
    }
  end
end

#save_into_a_yaml_file(blosum_file_to_use = @blosum_file_to_use) ⇒ Object

#

save_into_a_yaml_file

This method can be used to store the main @hash into a yaml file.

#


119
120
121
122
123
124
125
126
127
128
# File 'lib/bioroebe/parsers/blosum_parser.rb', line 119

def save_into_a_yaml_file(
    blosum_file_to_use = @blosum_file_to_use
  )
  what = YAML.dump(@hash)
  into = log_dir?+File.basename(blosum_file_to_use)
  erev "Storing the (expanded) hash from the blosum file at"
  erev "#{sfile(blosum_file_to_use)}"
  erev "into the file #{sfile(into)}`."
  write_what_into(what, into)
end

#set_blosum_file_to_use(i) ⇒ Object

#

set_blosum_file_to_use

This method is used to determine the main BLOSUM-file to use, which is typically a .yaml file.

#


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/parsers/blosum_parser.rb', line 69

def set_blosum_file_to_use(i)
  i = i.first if i.is_a? Array
  if i.is_a? Symbol
    i = i.to_s
  end
  if i and i.include?('-')
    i.delete!('-')
  end
  if i.is_a?(String)
    i = i.dup
    # i.downcase! # This has been eliminated at 17.08.2021, because it
                  # may contain the full path. On Linux paths are
                  # case sensitive.
    unless i.end_with?('.yml')
      # =================================================================== #
      # In this case we have to modify the input a little bit.
      # =================================================================== #
      i = ::Bioroebe.blosum_directory?+
          i.to_s.dup+'.yml'
    end
  end
  @blosum_file_to_use = i
end