From c4b49c5ce1ed3b46ae37e6ebd73c21d67d6d4810 Mon Sep 17 00:00:00 2001 From: martinahansen Date: Thu, 28 Feb 2013 14:34:26 +0000 Subject: [PATCH] finished refactoring s/has_key?/[]/ git-svn-id: http://biopieces.googlecode.com/svn/trunk@2109 74ccb610-7750-0410-82ae-013aeee3265d --- code_ruby/lib/maasha/align.rb | 12 ++++++------ code_ruby/lib/maasha/biopieces.rb | 30 +++++++++++++++--------------- code_ruby/lib/maasha/embl.rb | 2 +- code_ruby/lib/maasha/fastq.rb | 2 +- code_ruby/lib/maasha/genbank.rb | 2 +- code_ruby/lib/maasha/sam.rb | 18 +++++++++--------- code_ruby/lib/maasha/seq/digest.rb | 2 +- code_ruby/lib/maasha/usearch.rb | 4 ++-- 8 files changed, 36 insertions(+), 36 deletions(-) diff --git a/code_ruby/lib/maasha/align.rb b/code_ruby/lib/maasha/align.rb index ffa41c4..c139fe8 100755 --- a/code_ruby/lib/maasha/align.rb +++ b/code_ruby/lib/maasha/align.rb @@ -102,7 +102,7 @@ class Align Open3.popen3("muscle", "-quiet") do |stdin, stdout, stderr| entries.each do |entry| - raise AlignError, "Duplicate sequence name: #{entry.seq_name}" if index.has_key? entry.seq_name + raise AlignError, "Duplicate sequence name: #{entry.seq_name}" if index[entry.seq_name] index[entry.seq_name] = entry.dup @@ -270,14 +270,14 @@ class Align # NArrays. def consensus_calc if @has_qual - if @options.has_key? :quality_min + if @options[:quality_min] mask = mask_quality_min @na_seq *= mask @na_qual *= mask end - if @options.has_key? :quality_mean + if @options[:quality_mean] mask = mask_quality_mean @na_seq *= mask @@ -285,21 +285,21 @@ class Align end end - if @options.has_key? :sequence_min + if @options[:sequence_min] mask = mask_sequence_min @na_seq *= mask @na_qual *= mask if @has_qual end - if @options.has_key? :gap_max + if @options[:gap_max] mask = mask_gap_max @na_seq *= mask @na_qual *= mask if @has_qual end - if @options.has_key? :residue_min + if @options[:residue_min] mask = mask_residue_min @na_seq *= mask diff --git a/code_ruby/lib/maasha/biopieces.rb b/code_ruby/lib/maasha/biopieces.rb index bd85afb..1797d92 100644 --- a/code_ruby/lib/maasha/biopieces.rb +++ b/code_ruby/lib/maasha/biopieces.rb @@ -233,7 +233,7 @@ class Casts < Array def check_keys @cast_list.each do |cast| MANDATORY.each do |mandatory| - raise CastError, "Missing symbol in cast: '#{mandatory.to_sym}'" unless cast.has_key? mandatory.to_sym + raise CastError, "Missing symbol in cast: '#{mandatory}'" unless cast.has_key? mandatory.to_sym end end end @@ -272,7 +272,7 @@ class Casts < Array type_hash[type] = true end - unless type_hash.has_key? cast[:type] + unless type_hash[cast[:type]] raise CastError, "Illegal cast of type: '#{cast[:type]}'" end end @@ -312,8 +312,8 @@ class Casts < Array def check_duplicates check_hash = {} @cast_list.each do |cast| - raise CastError, "Duplicate argument: '--#{cast[:long]}'" if check_hash.has_key? cast[:long] - raise CastError, "Duplicate argument: '-#{cast[:short]}'" if check_hash.has_key? cast[:short] + raise CastError, "Duplicate argument: '--#{cast[:long]}'" if check_hash[cast[:long]] + raise CastError, "Duplicate argument: '-#{cast[:short]}'" if check_hash[cast[:short]] check_hash[cast[:long]] = true check_hash[cast[:short]] = true end @@ -446,7 +446,7 @@ class OptionHandler def options_default @casts.each do |cast| if cast[:default] - unless @options.has_key? cast[:long] + unless @options[cast[:long]] if cast[:type] == 'list' @options[cast[:long]] = cast[:default].split ',' else @@ -462,7 +462,7 @@ class OptionHandler def options_glob @casts.each do |cast| if cast[:type] == 'files' or cast[:type] == 'files!' - if @options.has_key? cast[:long] + if @options[cast[:long]] files = [] @options[cast[:long]].each do |path| @@ -498,13 +498,13 @@ class OptionHandler # Check if a mandatory option is set and raise if it isn't. def options_check_mandatory(cast) if cast[:mandatory] - raise ArgumentError, "Mandatory argument: --#{cast[:long]}" unless @options.has_key? cast[:long] + raise ArgumentError, "Mandatory argument: --#{cast[:long]}" unless @options[cast[:long]] end end # Check int type option and raise if not an integer. def options_check_int(cast) - if cast[:type] == 'int' and @options.has_key? cast[:long] + if cast[:type] == 'int' and @options[cast[:long]] unless @options[cast[:long]].is_a? Integer raise ArgumentError, "Argument to --#{cast[:long]} must be an integer, not '#{@options[cast[:long]]}'" end @@ -513,7 +513,7 @@ class OptionHandler # Check uint type option and raise if not an unsinged integer. def options_check_uint(cast) - if cast[:type] == 'uint' and @options.has_key? cast[:long] + if cast[:type] == 'uint' and @options[cast[:long]] unless @options[cast[:long]].is_a? Integer and @options[cast[:long]] >= 0 raise ArgumentError, "Argument to --#{cast[:long]} must be an unsigned integer, not '#{@options[cast[:long]]}'" end @@ -522,14 +522,14 @@ class OptionHandler # Check file! type argument and raise if file don't exists. def options_check_file(cast) - if cast[:type] == 'file!' and @options.has_key? cast[:long] + if cast[:type] == 'file!' and @options[cast[:long]] raise ArgumentError, "No such file: '#{@options[cast[:long]]}'" unless File.file? @options[cast[:long]] end end # Check files! type argument and raise if files don't exists. def options_check_files(cast) - if cast[:type] == 'files!' and @options.has_key? cast[:long] + if cast[:type] == 'files!' and @options[cast[:long]] @options[cast[:long]].each do |path| next if path == "-" raise ArgumentError, "File not readable: '#{path}'" unless File.readable? path @@ -539,24 +539,24 @@ class OptionHandler # Check dir! type argument and raise if directory don't exist. def options_check_dir(cast) - if cast[:type] == 'dir!' and @options.has_key? cast[:long] + if cast[:type] == 'dir!' and @options[cast[:long]] raise ArgumentError, "No such directory: '#{@options[cast[:long]]}'" unless File.directory? @options[cast[:long]] end end # Check options and raise unless allowed. def options_check_allowed(cast) - if cast[:allowed] and @options.has_key? cast[:long] + if cast[:allowed] and @options[cast[:long]] allowed_hash = {} cast[:allowed].split(',').each { |a| allowed_hash[a.to_s] = 1 } - raise ArgumentError, "Argument '#{@options[cast[:long]]}' to --#{cast[:long]} not allowed" unless allowed_hash.has_key? @options[cast[:long]].to_s + raise ArgumentError, "Argument '#{@options[cast[:long]]}' to --#{cast[:long]} not allowed" unless allowed_hash[@options[cast[:long]].to_s] end end # Check disallowed argument values and raise if disallowed. def options_check_disallowed(cast) - if cast[:disallowed] and @options.has_key? cast[:long] + if cast[:disallowed] and @options[cast[:long]] cast[:disallowed].split(',').each do |val| raise ArgumentError, "Argument '#{@options[cast[:long]]}' to --#{cast[:long]} is disallowed" if val.to_s == @options[cast[:long]].to_s end diff --git a/code_ruby/lib/maasha/embl.rb b/code_ruby/lib/maasha/embl.rb index d3532ae..d08dce5 100644 --- a/code_ruby/lib/maasha/embl.rb +++ b/code_ruby/lib/maasha/embl.rb @@ -114,7 +114,7 @@ class EMBL < Filesys end end - if keys.has_key? key.to_sym + if keys[key.to_sym] keys[key.to_sym] << " " + val else keys[key.to_sym] = val diff --git a/code_ruby/lib/maasha/fastq.rb b/code_ruby/lib/maasha/fastq.rb index c57f4eb..4ac44aa 100644 --- a/code_ruby/lib/maasha/fastq.rb +++ b/code_ruby/lib/maasha/fastq.rb @@ -83,7 +83,7 @@ class FastqIndex # Method to read from file a Fastq entry from an indexed position, # and return the entry as a Seq object. def get(seq_name) - raise FastqError, "Sequence name: #{seq_name} not found in index." unless @index.has_key? seq_name + raise FastqError, "Sequence name: #{seq_name} not found in index." unless @index[seq_name] elem = @index[seq_name] @ios.sysseek(elem.offset_seq) diff --git a/code_ruby/lib/maasha/genbank.rb b/code_ruby/lib/maasha/genbank.rb index fe08263..c3515ec 100644 --- a/code_ruby/lib/maasha/genbank.rb +++ b/code_ruby/lib/maasha/genbank.rb @@ -109,7 +109,7 @@ class Genbank < Filesys j += 1 end - if keys.has_key? key.to_sym + if keys[key.to_sym] keys[key.to_sym] << ";" + val else keys[key.to_sym] = val diff --git a/code_ruby/lib/maasha/sam.rb b/code_ruby/lib/maasha/sam.rb index 5ef7de5..074b999 100644 --- a/code_ruby/lib/maasha/sam.rb +++ b/code_ruby/lib/maasha/sam.rb @@ -79,7 +79,7 @@ class Sam < Filesys bp[:SCORES] = sam[:SEQ].convert_phred2illumina!.qual end - if sam.has_key? :NM and sam[:NM].to_i > 0 + if sam[:NM] and sam[:NM].to_i > 0 bp[:NM] = sam[:NM] bp[:MD] = sam[:MD] bp[:ALIGN] = self.align_descriptors(sam) @@ -264,7 +264,7 @@ class Sam < Filesys @header[:SQ][:SN] = Hash.new unless @header[:SQ][:SN].is_a? Hash - if @header[:SQ][:SN].has_key? seq_name + if @header[:SQ][:SN][seq_name] raise SamError, "Non-unique sequence name: #{seq_name}" else @header[:SQ][:SN][seq_name] = hash @@ -292,13 +292,13 @@ class Sam < Filesys end end - if hash.has_key? :FO + if hash[:FO] unless hash[:FO] =~ /^\*|[ACMGRSVTWYHKDBN]+$/ raise SamError, "Bad flow order: #{hash[:FO]}" end end - if hash.has_key? :PL + if hash[:PL] unless hash[:PL] =~ /^(CAPILLARY|LS454|ILLUMINA|SOLID|HELICOS|IONTORRENT|PACBIO)$/ raise SamError, "Bad platform: #{hash[:PL]}" end @@ -306,7 +306,7 @@ class Sam < Filesys @header[:RG][:ID] = Hash.new unless @header[:RG][:ID].is_a? Hash - if @header[:RG][:ID].has_key? id + if @header[:RG][:ID][id] raise SamError, "Non-unique read group identifier: #{id}" else @header[:RG][:ID][id] = hash @@ -336,7 +336,7 @@ class Sam < Filesys @header[:PG][:ID] = Hash.new unless @header[:PG][:ID].is_a? Hash - if @header[:PG][:ID].has_key? id + if @header[:PG][:ID][id] raise SamError, "Non-unique program record identifier: #{id}" else @header[:PG][:ID][id] = hash @@ -401,7 +401,7 @@ class Sam < Filesys fields[11 .. -1].each do |field| tag, type, val = field.split(':') - raise SamError, "Non-unique optional tag: #{tag}" if entry.has_key? tag.to_sym + raise SamError, "Non-unique optional tag: #{tag}" if entry[tag.to_sym] # A [!-~] Printable character @@ -439,7 +439,7 @@ class Sam < Filesys raise SamError, "Bad rname: #{rname}" unless rname =~ /^(\*|[!-()+-<>-~][!-~]*)$/ unless @header.empty? or rname == '*' - unless @header[:SQ][:SN].has_key? rname.to_sym + unless @header[:SQ][:SN][rname.to_sym] raise SamError, "rname not found in header hash: #{rname}" end end @@ -462,7 +462,7 @@ class Sam < Filesys raise SamError, "Bad rnext: #{rnext}" unless rnext =~ /^(\*|=|[!-()+-<>-~][!-~]*)$/ unless @header.empty? or rnext == '*' or rnext == '=' - unless @header[:SQ][:SN].has_key? rnext.to_sym + unless @header[:SQ][:SN][rnext.to_sym] raise SamError, "rnext not found in header hash: #{rnext}" end end diff --git a/code_ruby/lib/maasha/seq/digest.rb b/code_ruby/lib/maasha/seq/digest.rb index 835e8c0..a64aaa5 100644 --- a/code_ruby/lib/maasha/seq/digest.rb +++ b/code_ruby/lib/maasha/seq/digest.rb @@ -85,7 +85,7 @@ module Digest new_pattern = "" pattern.upcase.each_char do |char| - if ambiguity.has_key? char + if ambiguity[char] new_pattern << ambiguity[char] else raise DigestError, "Could not disambiguate residue: #{char}" diff --git a/code_ruby/lib/maasha/usearch.rb b/code_ruby/lib/maasha/usearch.rb index 2a3365b..8375850 100644 --- a/code_ruby/lib/maasha/usearch.rb +++ b/code_ruby/lib/maasha/usearch.rb @@ -78,8 +78,8 @@ class Usearch def usearch @command << "usearch --query #{@infile} --db #{@options[:database]} --userout #{@outfile}" @command << "--userfields target+tloz+thiz+query+bits+strand" - @command << "--id #{@options[:identity]}" if @options.has_key? :identity - @command << "--evalue #{@options[:e_val]}" if @options.has_key? :e_val + @command << "--id #{@options[:identity]}" if @options[:identity] + @command << "--evalue #{@options[:e_val]}" if @options[:e_val] @command << "--rev" execute -- 2.39.2