Module: Bio::Alignment::HashExtension

Includes:
EnumerableExtension
Included in:
OriginalAlignment, SequenceHash
Defined in:
lib/bio/alignment.rb

Overview

Bio::Alignment::HashExtension is a set of useful methods for multiple sequence alignment. It is designed to be extended to hash objects or included in your own classes which inherit Hash. (It can also be included in Hash, though not recommended.)

It possesses all methods defined in EnumerableExtension. For usage of methods, please refer to EnumerableExtension.

Because SequenceHash#alignment_collect is redefined, some methods’ return value’s class are changed to SequenceHash instead of SequenceArray.

Because the order of the objects in a hash is inconstant, some methods strictly affected with the order of objects might not work correctly, e.g. EnumerableExtension#convert_match and #convert_unmatch.

Constant Summary

Constants included from PropertyMethods

PropertyMethods::GAP_CHAR, PropertyMethods::GAP_REGEXP, PropertyMethods::MISSING_CHAR

Instance Attribute Summary

Attributes included from PropertyMethods

#gap_char, #gap_regexp, #missing_char, #seqclass

Instance Method Summary collapse

Methods included from EnumerableExtension

#alignment_length, #alignment_lstrip!, #alignment_normalize!, #alignment_rstrip!, #alignment_site, #alignment_slice, #alignment_strip!, #alignment_subseq, #alignment_window, #collect_each_site, #consensus_each_site, #consensus_iupac, #consensus_string, #convert_match, #convert_unmatch, #each_site, #each_site_step, #each_window, #match_line, #match_line_amino, #match_line_nuc, #remove_all_gaps!, #seqclass

Methods included from Output

#__output_phylip_common, #output, #output_clustal, #output_fasta, #output_molphy, #output_msf, #output_phylip, #output_phylipnon, #to_clustal

Methods included from PropertyMethods

#get_all_property, #is_gap?, #set_all_property

Instance Method Details

#alignment_collectObject

Iterates over each sequence and each results running block are collected and returns a new alignment as a Bio::Alignment::SequenceHash object.

Note that it would be redefined if you want to change return value’s class.



1390
1391
1392
1393
1394
1395
1396
1397
# File 'lib/bio/alignment.rb', line 1390

def alignment_collect
  a = SequenceHash.new
  a.set_all_property(get_all_property)
  each_pair do |key, str|
    a.store(key, yield(str))
  end
  a
end

#alignment_concat(align) ⇒ Object

Concatenates the given alignment. If align is a Hash (or SequenceHash), sequences of same keys are concatenated. Otherwise, align must have each_seq or each method and works same as EnumerableExtension#alignment_concat.

Returns self.

Note that it is a destructive method.



1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
# File 'lib/bio/alignment.rb', line 1410

def alignment_concat(align)
  flag = nil
  begin
    align.each_pair do |key, seq|
      flag = true
      if origseq = self[key]
        origseq.concat(seq)
      end
    end
    return self
  rescue NoMethodError, ArgumentError =>evar
    raise evar if flag
  end
  a = values
  i = 0
  begin
    align.each_seq do |seq|
      flag = true
      a[i].concat(seq) if a[i] and seq
      i += 1
    end
    return self
  rescue NoMethodError, ArgumentError => evar
    raise evar if flag
  end
  align.each do |seq|
    a[i].concat(seq) if a[i] and seq
    i += 1
  end
  self
end

#each_seqObject

Iterates over each sequences. Yields a sequence.

It works the same as Hash#each_value.



1378
1379
1380
1381
# File 'lib/bio/alignment.rb', line 1378

def each_seq #:yields: seq
  #each_value(&block)
  each_key { |k| yield self[k] }
end

#number_of_sequencesObject

Returns number of sequences in this alignment.



1443
1444
1445
# File 'lib/bio/alignment.rb', line 1443

def number_of_sequences
  self.size
end

#sequence_namesObject

Returns an array of sequence names. The order of the names must be the same as the order of each_seq.



1450
1451
1452
# File 'lib/bio/alignment.rb', line 1450

def sequence_names
  self.keys
end