Class Bio::Alignment::OriginalAlignment
In: lib/bio/alignment.rb  (CVS)
Parent: Object

Bio::Alignment::OriginalAlignment is the BioRuby original multiple sequence alignment container class. It includes HashExtension.

It is recommended only to use methods defined in EnumerableExtension (and the each_seq method). The method only defined in this class might be obsoleted in the future.

Methods

Included Modules

Enumerable HashExtension OriginalPrivate

External Aliases

alignment_slice -> slice
  The method name slice will be obsoleted. Please use alignment_slice instead.
alignment_subseq -> subseq
  The method name subseq will be obsoleted. Please use alignment_subseq instead.
consensus_string -> consensus
  The method name consensus will be obsoleted. Please use consensus_string instead.

Attributes

keys  [R]  identifiers (or definitions or names) of the sequences

Public Class methods

Creates a new alignment object. seqs may be one of follows: an array of sequences (or strings), an array of sequence database objects, an alignment object.

[Source]

# File lib/bio/alignment.rb, line 1553
      def initialize(seqs = [])
        @seqs = {}
        @keys = []
        self.add_sequences(seqs)
      end

Creates a new alignment object from given arguments.

It will be obsoleted.

[Source]

# File lib/bio/alignment.rb, line 1544
      def self.new2(*arg)
        self.new(arg)
      end

Read files and creates a new alignment object.

It will be obsoleted.

[Source]

# File lib/bio/alignment.rb, line 1530
      def self.readfiles(*files)
        require 'bio/io/flatfile'
        aln = self.new
        files.each do |fn|
          Bio::FlatFile.open(nil, fn) do |ff|
            aln.add_sequences(ff)
          end
        end
        aln
      end

Public Instance methods

Adds a sequence without key. The key is automatically determined.

[Source]

# File lib/bio/alignment.rb, line 1709
      def <<(seq)
        #(Array-like)
        self.store(nil, seq)
        self
      end

If x is the same value, returns true. Otherwise, returns false.

[Source]

# File lib/bio/alignment.rb, line 1561
      def ==(x)
        #(original)
        if x.is_a?(self.class)
          self.to_hash == x.to_hash
        else
          false
        end
      end

Gets a sequence. (Like Hash#[])

[Source]

# File lib/bio/alignment.rb, line 1716
      def [](*arg)
        #(Hash-like)
        @seqs[*arg]
      end

stores a sequences with the name

key:name of the sequence
seq:sequence

[Source]

# File lib/bio/alignment.rb, line 1614
      def __store__(key, seq)
        #(Hash-like)
        h = { key => seq }
        @keys << h.keys[0]
        @seqs.update(h)
        seq
      end

Adds a sequence to the alignment. Returns key if succeeded. Returns nil (and not added to the alignment) if key is already used.

It resembles BioPerl‘s AlignI::add_seq method.

[Source]

# File lib/bio/alignment.rb, line 1905
      def add_seq(seq, key = nil)
        #(BioPerl) AlignI::add_seq like method
        unless seq.is_a?(Bio::Sequence::NA) or seq.is_a?(Bio::Sequence::AA)
          s =   extract_seq(seq)
          key = extract_key(seq) unless key
          seq = s
        end
        self.store(key, seq)
      end

Adds sequences to the alignment. seqs may be one of follows: an array of sequences (or strings), an array of sequence database objects, an alignment object.

[Source]

# File lib/bio/alignment.rb, line 1581
      def add_sequences(seqs)
        if block_given? then
          seqs.each do |x|
            s, key = yield x
            self.store(key, s)
          end
        else
          if seqs.is_a?(self.class) then
            seqs.each_pair do |k, s|
              self.store(k, s)
            end
          elsif seqs.respond_to?(:each_pair)
            seqs.each_pair do |k, x|
              s = extract_seq(x)
              self.store(k, s)
            end
          else
            seqs.each do |x|
              s = extract_seq(x)
              k = extract_key(x)
              self.store(k, s)
            end
          end
        end
        self
      end

Iterates over each sequence and each results running block are collected and returns a new alignment.

The method name ‘collect_align’ will be obsoleted. Please use ‘alignment_collect’ instead.

[Source]

# File lib/bio/alignment.rb, line 1864
      def alignment_collect
        #(original)
        na = self.class.new
        na.set_all_property(get_all_property)
        self.each_pair do |k, s|
          na.store(k, yield(s))
        end
        na
      end

Iterates over each sequence, replacing the sequence with the value returned by the block.

[Source]

# File lib/bio/alignment.rb, line 1756
      def collect!
        #(Array-like)
        @keys.each do |k|
          @seqs[k] = yield @seqs[k]
        end
      end
collect_align()

Alias for alignment_collect

Removes empty sequences or nil and returns new alignment. (Like Array#compact)

[Source]

# File lib/bio/alignment.rb, line 1893
      def compact
        #(Array-like)
        na = self.dup
        na.compact!
        na
      end

Removes empty sequences or nil in the alignment. (Like Array#compact!)

[Source]

# File lib/bio/alignment.rb, line 1877
      def compact!
        #(Array-like)
        d = []
        self.each_pair do |k, s|
          if !s or s.empty?
            d << k
          end
        end
        d.each do |k|
          self.delete(k)
        end
        d.empty? ? nil : d
      end

Concatenates a string or an alignment. Returns self.

Note that the method will be obsoleted. Please use each_seq { |s| s << str } for concatenating a string and alignment_concat(aln) for concatenating an alignment.

[Source]

# File lib/bio/alignment.rb, line 2038
      def concat(aln)
        #(String-like)
        if aln.respond_to?(:to_str) then #aln.is_a?(String)
          self.each do |s|
            s << aln
          end
          self
        else
          alignment_concat(aln)
        end
      end

Removes the sequence whose key is key. Returns the removed sequence. If not found, returns nil.

[Source]

# File lib/bio/alignment.rb, line 1695
      def delete(key)
        #(Hash-like)
        @keys.delete(key)
        @seqs.delete(key)
      end

Performs multiple alignment by using external program.

[Source]

# File lib/bio/alignment.rb, line 2076
      def do_align(factory)
        a0 = self.class.new
        (0...self.size).each { |i| a0.store(i, self.order(i)) }
        r = factory.query(a0)
        a1 = r.alignment
        a0.keys.each do |k|
          unless a1[k.to_s] then
            raise 'alignment result is inconsistent with input data'
          end
        end
        a2 = self.new
        a0.keys.each do |k|
          a2.store(self.keys[k], a1[k.to_s])
        end
        a2
      end

Duplicates the alignment

[Source]

# File lib/bio/alignment.rb, line 1778
      def dup
        #(Hash-like)
        self.new(self)
      end

Iterates over each sequence. (Like Array#each)

[Source]

# File lib/bio/alignment.rb, line 1737
      def each
        #(Array-like)
        @keys.each do |k|
          yield @seqs[k]
        end
      end

Iterates over each key and sequence. (Like Hash#each_pair)

[Source]

# File lib/bio/alignment.rb, line 1747
      def each_pair
        #(Hash-like)
        @keys.each do |k|
          yield k, @seqs[k]
        end
      end
each_seq()

Alias for each

If the key exists, returns true. Otherwise, returns false. (Like Hash#has_key?)

[Source]

# File lib/bio/alignment.rb, line 1730
      def has_key?(key)
        #(Hash-like)
        @seqs.has_key?(key)
      end

Returns the key for a given sequence. If not found, returns nil.

[Source]

# File lib/bio/alignment.rb, line 1822
      def index(seq)
        #(Hash-like)
        last_key = nil
        self.each_pair do |k, s|
          last_key = k
          if s.class == seq.class then
            r = (s == seq)
          else
            r = (s.to_s == seq.to_s)
          end
          break if r
        end
        last_key
      end

Sequences in the alignment are duplicated. If keys are given to the argument, sequences of given keys are duplicated.

It will be obsoleted.

[Source]

# File lib/bio/alignment.rb, line 1842
      def isolate(*arg)
        #(original)
        if arg.size == 0 then
          self.collect! do |s|
            seqclass.new(s)
          end
        else
          arg.each do |k|
            if self.has_key?(k) then
              s = self.delete(key)
              self.store(k, seqclass.new(s))
            end
          end
        end
        self
      end

Not-destructive version of alignment_lstrip!. Returns a new alignment.

[Source]

# File lib/bio/alignment.rb, line 2000
      def lstrip
        #(String-like)
        na = self.dup
        na.isolate
        na.alignment_lstrip!
        na
      end

Merges given alignment and returns a new alignment.

[Source]

# File lib/bio/alignment.rb, line 1788
      def merge(*other)
        #(Hash-like)
        na = self.new(self)
        na.merge!(*other)
        na
      end

Merge given alignment. Note that it is destructive method.

[Source]

# File lib/bio/alignment.rb, line 1797
      def merge!(*other)
        #(Hash-like)
        if block_given? then
          other.each do |aln|
            aln.each_pair do |k, s|
              if self.has_key?(k) then
                s = yield k, self[k], s
                self.to_hash.store(k, s)
              else
                self.store(k, s)
              end
            end
          end
        else
          other.each do |aln|
            aln.each_pair do |k, s|
              self.delete(k) if self.has_key?(k)
              self.store(k, s)
            end
          end
        end
        self
      end

Not-destructive version of alignment_normalize!. Returns a new alignment.

[Source]

# File lib/bio/alignment.rb, line 1981
      def normalize
        #(original)
        na = self.dup
        na.alignment_normalize!
        na
      end
number_of_sequences()

Alias for size

Gets the n-th sequence. If not found, returns nil.

[Source]

# File lib/bio/alignment.rb, line 1687
      def order(n)
        #(original)
        @seqs[@keys[n]]
      end

Removes sequences from the alignment by given keys. Returns an alignment object consists of removed sequences.

It resembles BioPerl‘s AlignI::purge method.

[Source]

# File lib/bio/alignment.rb, line 1932
      def purge(*arg)
        #(BioPerl) AlignI::purge like method
        purged = self.new
        arg.each do |k|
          if self[k] then
            purged.store(k, self.delete(k))
          end
        end
        purged
      end

Reconstructs internal data structure. (Like Hash#rehash)

[Source]

# File lib/bio/alignment.rb, line 1651
      def rehash
        @seqs.rehash
        oldkeys = @keys
        tmpkeys = @seqs.keys
        @keys.collect! do |k|
          tmpkeys.delete(k)
        end
        @keys.compact!
        @keys.concat(tmpkeys)
        self
      end

Not-destructive version of remove_gaps!. Returns a new alignment.

The method name ‘remove_gap’ will be obsoleted. Please use ‘remove_all_gaps’ instead.

[Source]

# File lib/bio/alignment.rb, line 2023
      def remove_all_gaps
        #(original)
        na = self.dup
        na.isolate
        na.remove_all_gaps!
        na
      end

Removes given sequence from the alignment. Returns removed sequence. If nothing removed, returns nil.

It resembles BioPerl‘s AlignI::remove_seq.

[Source]

# File lib/bio/alignment.rb, line 1919
      def remove_seq(seq)
        #(BioPerl) AlignI::remove_seq like method
        if k = self.index(seq) then
          self.delete(k)
        else
          nil
        end
      end

Replace the specified region of the alignment to aln.

aln:String or Bio::Alignment object
arg:same format as String#slice

It will be obsoleted.

[Source]

# File lib/bio/alignment.rb, line 2055
      def replace_slice(aln, *arg)
        #(original)
        if aln.respond_to?(:to_str) then #aln.is_a?(String)
          self.each do |s|
            s[*arg] = aln
          end
        elsif aln.is_a?(self.class) then
          aln.each_pair do |k, s|
            self[k][*arg] = s
          end
        else
          i = 0
          aln.each do |s|
            self.order(i)[*arg] = s
            i += 1
          end
        end
        self
      end

Not-destructive version of alignment_rstrip!. Returns a new alignment.

[Source]

# File lib/bio/alignment.rb, line 1990
      def rstrip
        #(String-like)
        na = self.dup
        na.isolate
        na.alignment_rstrip!
        na
      end

If block is given, it acts like Array#select (Enumerable#select). Returns a new alignment containing all sequences of the alignment for which return value of given block is not false nor nil.

If no block is given, it acts like the BioPerl‘s AlignI::select. Returns a new alignment containing sequences of given keys.

The BioPerl‘s AlignI::select-like action will be obsoleted.

[Source]

# File lib/bio/alignment.rb, line 1951
      def select(*arg)
        #(original)
        na = self.new
        if block_given? then
          # 'arg' is ignored
          # nearly same action as Array#select (Enumerable#select)
          self.each_pair.each do |k, s|
            na.store(k, s) if yield(s)
          end
        else
          # BioPerl's AlignI::select like function
          arg.each do |k|
            if s = self[k] then
              na.store(k, s)
            end
          end
        end
        na
      end

Removes the first sequence in the alignment and returns [ key, seq ].

[Source]

# File lib/bio/alignment.rb, line 1675
      def shift
        k = @keys.shift
        if k then
          s = @seqs.delete(k)
          [ k, s ]
        else
          nil
        end
      end

Number of sequences in the alignment.

[Source]

# File lib/bio/alignment.rb, line 1722
      def size
        #(Hash&Array-like)
        @seqs.size
      end

stores a sequence with key (name or definition of the sequence). Unlike store method, the method doesn‘t allow same keys. If the key is already used, returns nil. When succeeded, returns key.

[Source]

# File lib/bio/alignment.rb, line 1628
      def store(key, seq)
        #(Hash-like) returns key instead of seq
        if @seqs.has_key?(key) then
          # don't allow same key
          # New key is discarded, while existing key is preserved.
          key = nil
        end
        unless key then
          unless defined?(@serial)
            @serial = 0
          end
          @serial = @seqs.size if @seqs.size > @serial
          while @seqs.has_key?(@serial)
            @serial += 1
          end
          key = @serial
        end
        self.__store__(key, seq)
        key
      end

Not-destructive version of alignment_strip!. Returns a new alignment.

[Source]

# File lib/bio/alignment.rb, line 2010
      def strip
        #(String-like)
        na = self.dup
        na.isolate
        na.alignment_strip!
        na
      end

Converts to fasta format and returns a string.

The specification of the argument will be changed.

Note: to_fasta is deprecated. Please use output_fasta instead.

[Source]

# File lib/bio/alignment.rb, line 2139
      def to_fasta(*arg)
        #(original)
        warn "to_fasta is deprecated. Please use output_fasta."
        self.to_fasta_array(*arg).join('')
      end

Convert to fasta format and returns an array of strings.

It will be obsoleted.

[Source]

# File lib/bio/alignment.rb, line 2096
      def to_fasta_array(*arg)
        #(original)
        width = nil
        if arg[0].is_a?(Integer) then
          width = arg.shift
        end
        options = (arg.shift or {})
        width = options[:width] unless width
        if options[:avoid_same_name] then
          na = __clustal_avoid_same_name(self.keys, 30)
        else
          na = self.keys.collect { |k| k.to_s.gsub(/[\r\n\x00]/, ' ') }
        end
        a = self.collect do |s|
          ">#{na.shift}\n" +
            if width then
              s.to_s.gsub(Regexp.new(".{1,#{width}}"), "\\0\n")
            else
              s.to_s + "\n"
            end
        end
        a
      end

Convets to fasta format and returns an array of FastaFormat objects.

It will be obsoleted.

[Source]

# File lib/bio/alignment.rb, line 2123
      def to_fastaformat_array(*arg)
        #(original)
        require 'bio/db/fasta'
        a = self.to_fasta_array(*arg)
        a.collect! do |x|
          Bio::FastaFormat.new(x)
        end
        a
      end

convert to hash

[Source]

# File lib/bio/alignment.rb, line 1571
      def to_hash
        #(Hash-like)
        @seqs
      end

Prepends seq (with key) to the front of the alignment. (Like Array#unshift)

[Source]

# File lib/bio/alignment.rb, line 1665
      def unshift(key, seq)
        #(Array-like)
        self.store(key, seq)
        k = @keys.pop
        @keys.unshift(k)
        k
      end

Returns sequences. (Like Hash#values)

[Source]

# File lib/bio/alignment.rb, line 1702
      def values
        #(Hash-like)
        @keys.collect { |k| @seqs[k] }
      end

Protected Instance methods

Creates new alignment. Internal use only.

[Source]

# File lib/bio/alignment.rb, line 1770
      def new(*arg)
        na = self.class.new(*arg)
        na.set_all_property(get_all_property)
        na
      end

[Validate]