From 5a9b7c80e4e4428e404575ed3c0290cde15a7402 Mon Sep 17 00:00:00 2001 From: martinahansen Date: Thu, 28 Feb 2013 14:17:03 +0000 Subject: [PATCH] refactoring of ruby code s/has_key?/[]/ git-svn-id: http://biopieces.googlecode.com/svn/trunk@2108 74ccb610-7750-0410-82ae-013aeee3265d --- bp_bin/analyze_assembly | 2 +- bp_bin/analyze_seq | 2 +- bp_bin/bin_vals | 2 +- bp_bin/blast_seq_pair | 2 +- bp_bin/bwa_seq | 2 +- bp_bin/calc_N50 | 2 +- bp_bin/cat_seq | 2 +- bp_bin/clip_seq | 4 ++-- bp_bin/denoise_seq | 2 +- bp_bin/digest_seq | 2 +- bp_bin/duplicate_record | 2 +- bp_bin/find_barcodes | 4 ++-- bp_bin/find_homopolymers | 2 +- bp_bin/join_seq | 2 +- bp_bin/length_seq | 2 +- bp_bin/mask_seq | 2 +- bp_bin/patscan_seq | 6 +++--- bp_bin/pcr_seq | 2 +- bp_bin/plot_distribution | 2 +- bp_bin/progress_meter | 2 +- bp_bin/read_embl | 4 ++-- bp_bin/read_fasta | 4 ++-- bp_bin/read_genbank | 4 ++-- bp_bin/read_sff | 4 ++-- bp_bin/remove_indel_columns | 4 ++-- bp_bin/remove_primers | 2 +- bp_bin/replace_vals | 6 +++--- bp_bin/scores_to_dec | 2 +- bp_bin/shred_seq | 2 +- bp_bin/swapcase_seq | 2 +- bp_bin/trim_seq | 2 +- bp_bin/uclust_seq | 6 +++--- bp_bin/usearch_seq | 2 +- bp_bin/write_fasta_files | 2 +- bp_bin/write_fastq | 2 +- bp_bin/write_fastq_files | 2 +- 36 files changed, 49 insertions(+), 49 deletions(-) diff --git a/bp_bin/analyze_assembly b/bp_bin/analyze_assembly index 3b9748d..a9a6d12 100755 --- a/bp_bin/analyze_assembly +++ b/bp_bin/analyze_assembly @@ -53,7 +53,7 @@ outfile = File.join(tmpdir, "out.prodigal") Fasta.open(infile, mode="w") do |fasta_output| Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| - if record.has_key? :SEQ + if record[:SEQ] seq = Seq.new_bp(record) total += record[:SEQ].length diff --git a/bp_bin/analyze_seq b/bp_bin/analyze_seq index f54b905..c2b96a5 100755 --- a/bp_bin/analyze_seq +++ b/bp_bin/analyze_seq @@ -38,7 +38,7 @@ options = Biopieces.options_parse(ARGV, casts) Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| - if record.has_key? :SEQ + if record[:SEQ] seq = Seq.new(record[:SEQ_NAME], record[:SEQ], record[:SEQ_TYPE], record[:SCORE]) comp = seq.composition diff --git a/bp_bin/bin_vals b/bp_bin/bin_vals index e5e73c7..a0bad39 100755 --- a/bp_bin/bin_vals +++ b/bp_bin/bin_vals @@ -39,7 +39,7 @@ options = Biopieces.options_parse(ARGV, casts) Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| - if record.has_key? options[:key].to_sym + if record[options[:key].to_sym] record[(options[:key] + "_BIN").to_sym] = (record[options[:key].to_sym].to_i / options[:bin_size]) * options[:bin_size] end diff --git a/bp_bin/blast_seq_pair b/bp_bin/blast_seq_pair index d0488c2..1963eaf 100755 --- a/bp_bin/blast_seq_pair +++ b/bp_bin/blast_seq_pair @@ -164,7 +164,7 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| output.puts record - if record.has_key? :SEQ_NAME and record.has_key? :SEQ + if record[:SEQ_NAME] and record[:SEQ] seq = Seq.new_bp(record) unless got1 diff --git a/bp_bin/bwa_seq b/bp_bin/bwa_seq index 4fd89a2..fd80782 100755 --- a/bp_bin/bwa_seq +++ b/bp_bin/bwa_seq @@ -102,7 +102,7 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| output.puts record - if record.has_key? :SEQ_NAME and record.has_key? :SEQ and record.has_key? :SCORES + if record[:SEQ_NAME] and record[:SEQ] and record[:SCORES] entry = Seq.new_bp(record) io_fq.puts entry.to_fastq diff --git a/bp_bin/calc_N50 b/bp_bin/calc_N50 index 3e56af9..13c8240 100755 --- a/bp_bin/calc_N50 +++ b/bp_bin/calc_N50 @@ -44,7 +44,7 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| output.puts record unless options[:no_stream] - if record.has_key? :SEQ + if record[:SEQ] total += record[:SEQ].length lengths << record[:SEQ].length end diff --git a/bp_bin/cat_seq b/bp_bin/cat_seq index 88e9d24..67cf397 100755 --- a/bp_bin/cat_seq +++ b/bp_bin/cat_seq @@ -41,7 +41,7 @@ seq = "" Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| - seq << record[:SEQ] if record.has_key? :SEQ + seq << record[:SEQ] if record[:SEQ] output.puts record unless options[:no_stream] end diff --git a/bp_bin/clip_seq b/bp_bin/clip_seq index 45a96c7..44d1e50 100755 --- a/bp_bin/clip_seq +++ b/bp_bin/clip_seq @@ -37,7 +37,7 @@ options = Biopieces.options_parse(ARGV, casts) Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| - if record.has_key? :SEQ + if record[:SEQ] trim_beg = 0 trim_end = record[:SEQ].length @@ -53,7 +53,7 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| record[:SEQ] = record[:SEQ][trim_beg ... trim_end] record[:SEQ_LEN] = record[:SEQ].length - record[:SCORES] = record[:SCORES][trim_beg ... trim_end] if record.has_key? :SCORES + record[:SCORES] = record[:SCORES][trim_beg ... trim_end] if record[:SCORES] end output.puts record diff --git a/bp_bin/denoise_seq b/bp_bin/denoise_seq index 0129c47..baa62ad 100755 --- a/bp_bin/denoise_seq +++ b/bp_bin/denoise_seq @@ -72,7 +72,7 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| Fasta.open(fasta_file, "w") do |fasta_io| Fastq.open(fastq_file, "w") do |fastq_io| input.each_record do |record| - if record.has_key? :SEQ and record.has_key? :SCORES + if record[:SEQ] and record[:SCORES] entry = Seq.new_bp(record) entry.seq_name = seq_count.to_s diff --git a/bp_bin/digest_seq b/bp_bin/digest_seq index b445134..2aa9ff9 100755 --- a/bp_bin/digest_seq +++ b/bp_bin/digest_seq @@ -39,7 +39,7 @@ options = Biopieces.options_parse(ARGV, casts) Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| - if record.has_key? :SEQ_NAME and record.has_key? :SEQ + if record[:SEQ_NAME] and record[:SEQ] seq = Seq.new_bp(record) seq.each_digest(options[:pattern].to_s, options[:cut_pos]) do |digest| diff --git a/bp_bin/duplicate_record b/bp_bin/duplicate_record index af3d112..9caa864 100755 --- a/bp_bin/duplicate_record +++ b/bp_bin/duplicate_record @@ -39,7 +39,7 @@ key = options[:key].to_sym Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| - if record.has_key? key + if record[key] (0 ... record[key].to_i).each { output.puts record } else output.puts record diff --git a/bp_bin/find_barcodes b/bp_bin/find_barcodes index b994c0d..3cad732 100755 --- a/bp_bin/find_barcodes +++ b/bp_bin/find_barcodes @@ -287,7 +287,7 @@ class BarCodeFinder hamming_dist = 0 barcode = seq[@pos ... @pos + size].upcase.to_sym - if @barcode_hash.has_key? barcode + if @barcode_hash[barcode] return BarCode.new(barcode, @barcode_hash[barcode], @pos, size, hamming_dist) elsif @max_mismatches > 0 @barcode_hash.each_key do |key| @@ -352,7 +352,7 @@ bc_finder.load_barcodes(RLMID_HASH) if options[:rlmids] Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| - if record.has_key? :SEQ + if record[:SEQ] if barcode = bc_finder.find_barcode(record[:SEQ]) record.merge!(barcode.to_hash) diff --git a/bp_bin/find_homopolymers b/bp_bin/find_homopolymers index 3cad86f..89b8e8c 100755 --- a/bp_bin/find_homopolymers +++ b/bp_bin/find_homopolymers @@ -39,7 +39,7 @@ options = Biopieces.options_parse(ARGV, casts) Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| - if record.has_key? :SEQ + if record[:SEQ] seq = Seq.new(nil, record[:SEQ]) record[:HOMOPOL_MAX] = seq.homopol_max(options[:min]) diff --git a/bp_bin/join_seq b/bp_bin/join_seq index ce36e59..b2749e9 100755 --- a/bp_bin/join_seq +++ b/bp_bin/join_seq @@ -42,7 +42,7 @@ seq = Seq.new(nil, "") Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| - if record.has_key? :SEQ + if record[:SEQ] unless seq.seq_name seq.seq_name = record[:SEQ_NAME] end diff --git a/bp_bin/length_seq b/bp_bin/length_seq index e9f8726..04384db 100755 --- a/bp_bin/length_seq +++ b/bp_bin/length_seq @@ -37,7 +37,7 @@ options = Biopieces.options_parse(ARGV, casts) Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| - record[:SEQ_LEN] = record[:SEQ].length if record.has_key? :SEQ + record[:SEQ_LEN] = record[:SEQ].length if record[:SEQ] output.puts record end end diff --git a/bp_bin/mask_seq b/bp_bin/mask_seq index 3cd162a..d9dd01a 100755 --- a/bp_bin/mask_seq +++ b/bp_bin/mask_seq @@ -39,7 +39,7 @@ options = Biopieces.options_parse(ARGV, casts) Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| - if record.has_key? :SEQ + if record[:SEQ] entry = Seq.new_bp(record) options[:hardmask] ? entry.mask_seq_hard!(options[:cutoff]) : entry.mask_seq_soft!(options[:cutoff]) diff --git a/bp_bin/patscan_seq b/bp_bin/patscan_seq index 1b533a9..c8ff362 100755 --- a/bp_bin/patscan_seq +++ b/bp_bin/patscan_seq @@ -141,7 +141,7 @@ seq_type = nil Biopieces.open(options[:stream_in], tmp_file) do |input, output| Fasta.open(in_file, mode="w") do |fasta_io| input.each_record do |record| - if record.has_key? :SEQ_NAME + if record[:SEQ_NAME] seq_name_hash[seq_name_count] = record[:SEQ_NAME] record[:SEQ_NAME] = seq_name_count seq_name_count += 1 @@ -166,12 +166,12 @@ results = patscan.results_parse Biopieces.open(tmp_file, options[:stream_out]) do |input, output| input.each_record do |record| - if record.has_key? :SEQ_NAME + if record[:SEQ_NAME] key = record[:SEQ_NAME].to_i record[:SEQ_NAME] = seq_name_hash[key] if options[:inline] - if results.has_key? key + if results[key] results[key].each do |result| record[:PATTERN] = options[:pattern] record[:MATCH] = result.match diff --git a/bp_bin/pcr_seq b/bp_bin/pcr_seq index 57eb447..f6c874e 100755 --- a/bp_bin/pcr_seq +++ b/bp_bin/pcr_seq @@ -177,7 +177,7 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| output.puts record - if record.has_key? :SEQ + if record[:SEQ] entry = Seq.new_bp(record) ios.puts entry.to_fasta end diff --git a/bp_bin/plot_distribution b/bp_bin/plot_distribution index 02f802e..175ca59 100755 --- a/bp_bin/plot_distribution +++ b/bp_bin/plot_distribution @@ -54,7 +54,7 @@ count_hash = Hash.new(0) Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| - if record.has_key? key + if record[key] count_hash[record[key].to_i] += 1 end diff --git a/bp_bin/progress_meter b/bp_bin/progress_meter index 49003a2..43cdd77 100755 --- a/bp_bin/progress_meter +++ b/bp_bin/progress_meter @@ -39,7 +39,7 @@ options = Biopieces.options_parse(ARGV, casts) Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_with_index do |record, count| - output.puts record unless options.has_key? :no_stream + output.puts record unless options[:no_stream] $stderr.printf "\n% 9d ", count if (count % (options[:count] * 100)) == 0 $stderr.print "." if (count % options[:count]) == 0 end diff --git a/bp_bin/read_embl b/bp_bin/read_embl index 91f6728..0a3558c 100755 --- a/bp_bin/read_embl +++ b/bp_bin/read_embl @@ -52,7 +52,7 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| num = 0 last = false - if options.has_key? :data_in + if options[:data_in] options[:data_in].each do |file| EMBL.open(file, mode='r') do |embl_io| embl_io.each(hash_keys, hash_feats, hash_quals) do |entry| @@ -60,7 +60,7 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| num += 1 - if options.has_key? :num and options[:num] == num + if options[:num] and options[:num] == num last = true break end diff --git a/bp_bin/read_fasta b/bp_bin/read_fasta index 0508ceb..8bc5874 100755 --- a/bp_bin/read_fasta +++ b/bp_bin/read_fasta @@ -47,14 +47,14 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| num = 0 last = false - if options.has_key? :data_in + if options[:data_in] options[:data_in].each do |file| Fasta.open(file, mode='r') do |fasta| fasta.each do |entry| output.puts entry.to_bp num += 1 - if options.has_key? :num and options[:num] == num + if options[:num] and options[:num] == num last = true break end diff --git a/bp_bin/read_genbank b/bp_bin/read_genbank index d40f1aa..7cabf2b 100755 --- a/bp_bin/read_genbank +++ b/bp_bin/read_genbank @@ -53,7 +53,7 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| num = 0 last = false - if options.has_key? :data_in + if options[:data_in] options[:data_in].each do |file| Genbank.open(file, mode='r') do |gb| gb.each(hash_keys, hash_feats, hash_quals) do |entry| @@ -61,7 +61,7 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| num += 1 - if options.has_key? :num and options[:num] == num + if options[:num] and options[:num] == num last = true break end diff --git a/bp_bin/read_sff b/bp_bin/read_sff index 3de8b2e..cd9d58a 100755 --- a/bp_bin/read_sff +++ b/bp_bin/read_sff @@ -47,7 +47,7 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| num = 0 last = false - if options.has_key? :data_in + if options[:data_in] options[:data_in].each do |file| SFF.open(file, mode='r') do |sff| sff.each do |entry| @@ -56,7 +56,7 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| output.puts entry.to_bp num += 1 - if options.has_key? :num and options[:num] == num + if options[:num] and options[:num] == num last = true break end diff --git a/bp_bin/remove_indel_columns b/bp_bin/remove_indel_columns index 5de6c0b..193c786 100755 --- a/bp_bin/remove_indel_columns +++ b/bp_bin/remove_indel_columns @@ -40,7 +40,7 @@ count = 0 Biopieces.open(options[:stream_in], file_records) do |input, output| input.each do |record| - if record.has_key? :SEQ + if record[:SEQ] na_mask = NArray.int(record[:SEQ].length) unless na_mask na_seq = NArray.to_na(record[:SEQ], "byte") na_mask += na_seq.eq('-'.ord) @@ -61,7 +61,7 @@ sum = na_mask.sum Biopieces.open(file_records, options[:stream_out]) do |input, output| input.each do |record| - if sum > 0 and record.has_key? :SEQ + if sum > 0 and record[:SEQ] na_seq = NArray.to_na(record[:SEQ], "byte") record[:SEQ] = na_seq[na_mask].to_s record[:SEQ_LEN] = record[:SEQ].length diff --git a/bp_bin/remove_primers b/bp_bin/remove_primers index eef8248..1f7b7b2 100755 --- a/bp_bin/remove_primers +++ b/bp_bin/remove_primers @@ -47,7 +47,7 @@ options = Biopieces.options_parse(ARGV, casts) Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each do |record| - if record.has_key? :SEQ + if record[:SEQ] forward = false reverse = false seq = Seq.new_bp(record) diff --git a/bp_bin/replace_vals b/bp_bin/replace_vals index a810559..860b913 100755 --- a/bp_bin/replace_vals +++ b/bp_bin/replace_vals @@ -60,7 +60,7 @@ if options[:file] k = fields[options[:search_col] - 1] v = fields[options[:replace_col] - 1] - raise RuntimeError, "duplicate key: #{k} found in file" if replace_hash.has_key? k + raise RuntimeError, "duplicate key: #{k} found in file" if replace_hash[k] replace_hash[k] = v end @@ -70,8 +70,8 @@ end Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| - if record.has_key? key - if replace_hash.has_key? record[key] + if record[key] + if replace_hash[record[key]] record[key] = replace_hash[record[key]] end end diff --git a/bp_bin/scores_to_dec b/bp_bin/scores_to_dec index 2d91c2b..b93587f 100755 --- a/bp_bin/scores_to_dec +++ b/bp_bin/scores_to_dec @@ -36,7 +36,7 @@ class Hash # Method to convert scores from ASCII encode string to # a semi-colon seperated string of decimal values. def scores2dec! - if self.has_key? :SCORES + if self[:SCORES] self[:SCORES].gsub! /./ do |score| score = (score.ord - Seq::SCORE_BASE).to_s + ";" end diff --git a/bp_bin/shred_seq b/bp_bin/shred_seq index 465f088..ffd79cb 100755 --- a/bp_bin/shred_seq +++ b/bp_bin/shred_seq @@ -83,7 +83,7 @@ options = Biopieces.options_parse(ARGV, casts) Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| - if record.has_key? :SEQ and record[:SEQ].length >= options[:size] + if record[:SEQ] and record[:SEQ].length >= options[:size] entry = Seq.new(record[:SEQ_NAME], record[:SEQ], record[:SCORES]) entry.type = 'dna' diff --git a/bp_bin/swapcase_seq b/bp_bin/swapcase_seq index bc1cded..b1fafa2 100755 --- a/bp_bin/swapcase_seq +++ b/bp_bin/swapcase_seq @@ -35,7 +35,7 @@ options = Biopieces.options_parse(ARGV) Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| - record[:SEQ].swapcase! if record.has_key? :SEQ + record[:SEQ].swapcase! if record[:SEQ] output.puts record end end diff --git a/bp_bin/trim_seq b/bp_bin/trim_seq index d6c1a09..7afec9c 100755 --- a/bp_bin/trim_seq +++ b/bp_bin/trim_seq @@ -41,7 +41,7 @@ options = Biopieces.options_parse(ARGV, casts) Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| - if record.has_key? :SEQ and record.has_key? :SCORES + if record[:SEQ] and record[:SCORES] entry = Seq.new_bp(record) case options[:trim] diff --git a/bp_bin/uclust_seq b/bp_bin/uclust_seq index 695d3cf..936d642 100755 --- a/bp_bin/uclust_seq +++ b/bp_bin/uclust_seq @@ -49,7 +49,7 @@ Biopieces.open(options[:stream_in], file_records) do |input, output| input.each_record do |record| output.puts record - if record.has_key? :SEQ_NAME and record.has_key? :SEQ + if record[:SEQ_NAME] and record[:SEQ] fasta_io.puts Seq.new_bp(record).to_fasta end end @@ -68,8 +68,8 @@ end Biopieces.open(file_records, options[:stream_out]) do |input, output| input.each_record do |record| - if record.has_key? :SEQ_NAME and record.has_key? :SEQ - if hash.has_key? record[:SEQ_NAME].to_sym + if record[:SEQ_NAME] and record[:SEQ] + if hash[record[:SEQ_NAME].to_sym] us = hash[record[:SEQ_NAME].to_sym] record[:CLUSTER] = us[:CLUSTER].to_i record[:IDENT] = us[:IDENT].to_i diff --git a/bp_bin/usearch_seq b/bp_bin/usearch_seq index 2075285..c390708 100755 --- a/bp_bin/usearch_seq +++ b/bp_bin/usearch_seq @@ -50,7 +50,7 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| input.each_record do |record| output.puts record - if record.has_key? :SEQ_NAME and record.has_key? :SEQ + if record[:SEQ_NAME] and record[:SEQ] fasta_io.puts Seq.new_bp(record).to_fasta end end diff --git a/bp_bin/write_fasta_files b/bp_bin/write_fasta_files index debea31..36011ed 100755 --- a/bp_bin/write_fasta_files +++ b/bp_bin/write_fasta_files @@ -48,7 +48,7 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| if record[:SEQ_NAME] and record[:SEQ] and record[key] seq = Seq.new_bp(record) - if fh_hash.has_key? record[key].to_sym + if fh_hash[record[key].to_sym] fasta_io = fh_hash[record[key].to_sym] else fasta_file = File.join(options[:dir], record[key] + ".fasta") diff --git a/bp_bin/write_fastq b/bp_bin/write_fastq index f6050f3..796e187 100755 --- a/bp_bin/write_fastq +++ b/bp_bin/write_fastq @@ -62,7 +62,7 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| io_out.puts entry.to_fastq end - output.puts record unless options.has_key? :no_stream + output.puts record unless options[:no_stream] end io_out.close diff --git a/bp_bin/write_fastq_files b/bp_bin/write_fastq_files index eab5a87..a116040 100755 --- a/bp_bin/write_fastq_files +++ b/bp_bin/write_fastq_files @@ -48,7 +48,7 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| if record[:SEQ_NAME] and record[:SEQ] and record[:SCORES] and record[key] seq = Seq.new_bp(record) - if fh_hash.has_key? record[key].to_sym + if fh_hash[record[key].to_sym] fastq_io = fh_hash[record[key].to_sym] else fastq_file = File.join(options[:dir], record[key] + ".fastq") -- 2.39.2