class Bio::Alignment::OriginalAlignment

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.

Attributes

keys[R]

identifiers (or definitions or names) of the sequences

Public Class Methods

new(seqs = []) click to toggle source

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.

     # File lib/bio/alignment.rb
1548 def initialize(seqs = [])
1549   @seqs = {}
1550   @keys = []
1551   self.add_sequences(seqs)
1552 end
new2(*arg) click to toggle source

Creates a new alignment object from given arguments.

It will be obsoleted.

     # File lib/bio/alignment.rb
1539 def self.new2(*arg)
1540   self.new(arg)
1541 end
readfiles(*files) click to toggle source

Read files and creates a new alignment object.

It will be obsoleted.

     # File lib/bio/alignment.rb
1525 def self.readfiles(*files)
1526   require 'bio/io/flatfile'
1527   aln = self.new
1528   files.each do |fn|
1529     Bio::FlatFile.open(nil, fn) do |ff|
1530       aln.add_sequences(ff)
1531     end
1532   end
1533   aln
1534 end

Public Instance Methods

<<(seq) click to toggle source

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

     # File lib/bio/alignment.rb
1703 def <<(seq)
1704   #(Array-like)
1705   self.store(nil, seq)
1706   self
1707 end
==(x) click to toggle source

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

     # File lib/bio/alignment.rb
1556 def ==(x)
1557   #(original)
1558   if x.is_a?(self.class)
1559     self.to_hash == x.to_hash
1560   else
1561     false
1562   end
1563 end
[](*arg) click to toggle source

Gets a sequence. (Like Hash#[])

     # File lib/bio/alignment.rb
1710 def [](*arg)
1711   #(Hash-like)
1712   @seqs[*arg]
1713 end
__store__(key, seq) click to toggle source

stores a sequences with the name

key

name of the sequence

seq

sequence

     # File lib/bio/alignment.rb
1609 def __store__(key, seq)
1610   #(Hash-like)
1611   h = { key => seq }
1612   @keys << h.keys[0]
1613   @seqs.update(h)
1614   seq
1615 end
add_seq(seq, key = nil) click to toggle source

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.

     # File lib/bio/alignment.rb
1899 def add_seq(seq, key = nil)
1900   #(BioPerl) AlignI::add_seq like method
1901   unless seq.is_a?(Bio::Sequence::NA) or seq.is_a?(Bio::Sequence::AA)
1902     s =   extract_seq(seq)
1903     key = extract_key(seq) unless key
1904     seq = s
1905   end
1906   self.store(key, seq)
1907 end
add_sequences(seqs) { |x| ... } click to toggle source

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.

     # File lib/bio/alignment.rb
1576 def add_sequences(seqs)
1577   if block_given? then
1578     seqs.each do |x|
1579       s, key = yield x
1580       self.store(key, s)
1581     end
1582   else
1583     if seqs.is_a?(self.class) then
1584       seqs.each_pair do |k, s|
1585         self.store(k, s)
1586       end
1587     elsif seqs.respond_to?(:each_pair)
1588       seqs.each_pair do |k, x|
1589         s = extract_seq(x)
1590         self.store(k, s)
1591       end
1592     else
1593       seqs.each do |x|
1594         s = extract_seq(x)
1595         k = extract_key(x)
1596         self.store(k, s)
1597       end
1598     end
1599   end
1600   self
1601 end
alignment_collect() { |s| ... } click to toggle source

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.

     # File lib/bio/alignment.rb
1858 def alignment_collect
1859   #(original)
1860   na = self.class.new
1861   na.set_all_property(get_all_property)
1862   self.each_pair do |k, s|
1863     na.store(k, yield(s))
1864   end
1865   na
1866 end
Also aliased as: collect_align
collect!() { |seqs| ... } click to toggle source

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

     # File lib/bio/alignment.rb
1750 def collect!
1751   #(Array-like)
1752   @keys.each do |k|
1753     @seqs[k] = yield @seqs[k]
1754   end
1755 end
collect_align()
Alias for: alignment_collect
compact() click to toggle source

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

     # File lib/bio/alignment.rb
1887 def compact
1888   #(Array-like)
1889   na = self.dup
1890   na.compact!
1891   na
1892 end
compact!() click to toggle source

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

     # File lib/bio/alignment.rb
1871 def compact!
1872   #(Array-like)
1873   d = []
1874   self.each_pair do |k, s|
1875     if !s or s.empty?
1876       d << k
1877     end
1878   end
1879   d.each do |k|
1880     self.delete(k)
1881   end
1882   d.empty? ? nil : d
1883 end
concat(aln) click to toggle source

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.

     # File lib/bio/alignment.rb
2032 def concat(aln)
2033   #(String-like)
2034   if aln.respond_to?(:to_str) then #aln.is_a?(String)
2035     self.each do |s|
2036       s << aln
2037     end
2038     self
2039   else
2040     alignment_concat(aln)
2041   end
2042 end
delete(key) click to toggle source

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

     # File lib/bio/alignment.rb
1689 def delete(key)
1690   #(Hash-like)
1691   @keys.delete(key)
1692   @seqs.delete(key)
1693 end
do_align(factory) click to toggle source

Performs multiple alignment by using external program.

     # File lib/bio/alignment.rb
2070 def do_align(factory)
2071   a0 = self.class.new
2072   (0...self.size).each { |i| a0.store(i, self.order(i)) }
2073   r = factory.query(a0)
2074   a1 = r.alignment
2075   a0.keys.each do |k|
2076     unless a1[k.to_s] then
2077       raise 'alignment result is inconsistent with input data'
2078     end
2079   end
2080   a2 = self.new
2081   a0.keys.each do |k|
2082     a2.store(self.keys[k], a1[k.to_s])
2083   end
2084   a2
2085 end
dup() click to toggle source

Duplicates the alignment

     # File lib/bio/alignment.rb
1772 def dup
1773   #(Hash-like)
1774   self.new(self)
1775 end
each() { |seqs| ... } click to toggle source

Iterates over each sequence. (Like Array#each)

     # File lib/bio/alignment.rb
1731 def each
1732   #(Array-like)
1733   @keys.each do |k|
1734     yield @seqs[k]
1735   end
1736 end
Also aliased as: each_seq
each_pair() { |k, seqs| ... } click to toggle source

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

     # File lib/bio/alignment.rb
1741 def each_pair
1742   #(Hash-like)
1743   @keys.each do |k|
1744     yield k, @seqs[k]
1745   end
1746 end
each_seq()
Alias for: each
has_key?(key) click to toggle source

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

     # File lib/bio/alignment.rb
1724 def has_key?(key)
1725   #(Hash-like)
1726   @seqs.has_key?(key)
1727 end
index(seq) click to toggle source

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

     # File lib/bio/alignment.rb
1816 def index(seq)
1817   #(Hash-like)
1818   last_key = nil
1819   self.each_pair do |k, s|
1820     last_key = k
1821     if s.class == seq.class then
1822       r = (s == seq)
1823     else
1824       r = (s.to_s == seq.to_s)
1825     end
1826     break if r
1827   end
1828   last_key
1829 end
isolate(*arg) click to toggle source

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

It will be obsoleted.

     # File lib/bio/alignment.rb
1836 def isolate(*arg)
1837   #(original)
1838   if arg.size == 0 then
1839     self.collect! do |s|
1840       seqclass.new(s)
1841     end
1842   else
1843     arg.each do |k|
1844       if self.has_key?(k) then
1845         s = self.delete(key)
1846         self.store(k, seqclass.new(s))
1847       end
1848     end
1849   end
1850   self
1851 end
lstrip() click to toggle source

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

     # File lib/bio/alignment.rb
1994 def lstrip
1995   #(String-like)
1996   na = self.dup
1997   na.isolate
1998   na.alignment_lstrip!
1999   na
2000 end
merge(*other) click to toggle source

Merges given alignment and returns a new alignment.

     # File lib/bio/alignment.rb
1782 def merge(*other)
1783   #(Hash-like)
1784   na = self.new(self)
1785   na.merge!(*other)
1786   na
1787 end
merge!(*other) { |k, self, s| ... } click to toggle source

Merge given alignment. Note that it is destructive method.

     # File lib/bio/alignment.rb
1791 def merge!(*other)
1792   #(Hash-like)
1793   if block_given? then
1794     other.each do |aln|
1795       aln.each_pair do |k, s|
1796         if self.has_key?(k) then
1797           s = yield k, self[k], s
1798           self.to_hash.store(k, s)
1799         else
1800           self.store(k, s)
1801         end
1802       end
1803     end
1804   else
1805     other.each do |aln|
1806       aln.each_pair do |k, s|
1807         self.delete(k) if self.has_key?(k)
1808         self.store(k, s)
1809       end
1810     end
1811   end
1812   self
1813 end
normalize() click to toggle source

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

     # File lib/bio/alignment.rb
1975 def normalize
1976   #(original)
1977   na = self.dup
1978   na.alignment_normalize!
1979   na
1980 end
number_of_sequences()
Alias for: size
order(n) click to toggle source

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

     # File lib/bio/alignment.rb
1681 def order(n)
1682   #(original)
1683   @seqs[@keys[n]]
1684 end
purge(*arg) click to toggle source

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

It resembles BioPerl’s AlignI::purge method.

     # File lib/bio/alignment.rb
1926 def purge(*arg)
1927   #(BioPerl) AlignI::purge like method
1928   purged = self.new
1929   arg.each do |k|
1930     if self[k] then
1931       purged.store(k, self.delete(k))
1932     end
1933   end
1934   purged
1935 end
rehash() click to toggle source

Reconstructs internal data structure. (Like Hash#rehash)

     # File lib/bio/alignment.rb
1646 def rehash
1647   @seqs.rehash
1648   tmpkeys = @seqs.keys
1649   @keys.collect! do |k|
1650     tmpkeys.delete(k)
1651   end
1652   @keys.compact!
1653   @keys.concat(tmpkeys)
1654   self
1655 end
remove_all_gaps() click to toggle source

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

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

     # File lib/bio/alignment.rb
2017 def remove_all_gaps
2018   #(original)
2019   na = self.dup
2020   na.isolate
2021   na.remove_all_gaps!
2022   na
2023 end
remove_seq(seq) click to toggle source

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

It resembles BioPerl’s AlignI::remove_seq.

     # File lib/bio/alignment.rb
1913 def remove_seq(seq)
1914   #(BioPerl) AlignI::remove_seq like method
1915   if k = self.index(seq) then
1916     self.delete(k)
1917   else
1918     nil
1919   end
1920 end
replace_slice(aln, *arg) click to toggle source

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.

     # File lib/bio/alignment.rb
2049 def replace_slice(aln, *arg)
2050   #(original)
2051   if aln.respond_to?(:to_str) then #aln.is_a?(String)
2052     self.each do |s|
2053       s[*arg] = aln
2054     end
2055   elsif aln.is_a?(self.class) then
2056     aln.each_pair do |k, s|
2057       self[k][*arg] = s
2058     end
2059   else
2060     i = 0
2061     aln.each do |s|
2062       self.order(i)[*arg] = s
2063       i += 1
2064     end
2065   end
2066   self
2067 end
rstrip() click to toggle source

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

     # File lib/bio/alignment.rb
1984 def rstrip
1985   #(String-like)
1986   na = self.dup
1987   na.isolate
1988   na.alignment_rstrip!
1989   na
1990 end
select(*arg) { |s| ... } click to toggle source

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.

     # File lib/bio/alignment.rb
1945 def select(*arg)
1946   #(original)
1947   na = self.new
1948   if block_given? then
1949     # 'arg' is ignored
1950     # nearly same action as Array#select (Enumerable#select)
1951     self.each_pair.each do |k, s|
1952       na.store(k, s) if yield(s)
1953     end
1954   else
1955     # BioPerl's AlignI::select like function
1956     arg.each do |k|
1957       if s = self[k] then
1958         na.store(k, s)
1959       end
1960     end
1961   end
1962   na
1963 end
shift() click to toggle source

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

     # File lib/bio/alignment.rb
1669 def shift
1670   k = @keys.shift
1671   if k then
1672     s = @seqs.delete(k)
1673     [ k, s ]
1674   else
1675     nil
1676   end
1677 end
size() click to toggle source

Number of sequences in the alignment.

     # File lib/bio/alignment.rb
1716 def size
1717   #(Hash&Array-like)
1718   @seqs.size
1719 end
Also aliased as: number_of_sequences
store(key, seq) click to toggle source

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.

     # File lib/bio/alignment.rb
1623 def store(key, seq)
1624   #(Hash-like) returns key instead of seq
1625   if @seqs.has_key?(key) then
1626     # don't allow same key
1627     # New key is discarded, while existing key is preserved.
1628     key = nil
1629   end
1630   unless key then
1631     unless defined?(@serial)
1632       @serial = 0
1633     end
1634     @serial = @seqs.size if @seqs.size > @serial
1635     while @seqs.has_key?(@serial)
1636       @serial += 1
1637     end
1638     key = @serial
1639   end
1640   self.__store__(key, seq)
1641   key
1642 end
strip() click to toggle source

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

     # File lib/bio/alignment.rb
2004 def strip
2005   #(String-like)
2006   na = self.dup
2007   na.isolate
2008   na.alignment_strip!
2009   na
2010 end
to_fasta(*arg) click to toggle source

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.

     # File lib/bio/alignment.rb
2133 def to_fasta(*arg)
2134   #(original)
2135   warn "to_fasta is deprecated. Please use output_fasta."
2136   self.to_fasta_array(*arg).join('')
2137 end
to_fasta_array(*arg) click to toggle source

Convert to fasta format and returns an array of strings.

It will be obsoleted.

     # File lib/bio/alignment.rb
2090 def to_fasta_array(*arg)
2091   #(original)
2092   width = nil
2093   if arg[0].is_a?(Integer) then
2094     width = arg.shift
2095   end
2096   options = (arg.shift or {})
2097   width = options[:width] unless width
2098   if options[:avoid_same_name] then
2099     na = __clustal_avoid_same_name(self.keys, 30)
2100   else
2101     na = self.keys.collect { |k| k.to_s.gsub(/[\r\n\x00]/, ' ') }
2102   end
2103   a = self.collect do |s|
2104     ">#{na.shift}\n" +
2105       if width then
2106         s.to_s.gsub(Regexp.new(".{1,#{width}}"), "\\0\n")
2107       else
2108         s.to_s + "\n"
2109       end
2110   end
2111   a
2112 end
to_fastaformat_array(*arg) click to toggle source

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

It will be obsoleted.

     # File lib/bio/alignment.rb
2117 def to_fastaformat_array(*arg)
2118   #(original)
2119   require 'bio/db/fasta'
2120   a = self.to_fasta_array(*arg)
2121   a.collect! do |x|
2122     Bio::FastaFormat.new(x)
2123   end
2124   a
2125 end
to_hash() click to toggle source

convert to hash

     # File lib/bio/alignment.rb
1566 def to_hash
1567   #(Hash-like)
1568   @seqs
1569 end
unshift(key, seq) click to toggle source

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

     # File lib/bio/alignment.rb
1659 def unshift(key, seq)
1660   #(Array-like)
1661   self.store(key, seq)
1662   k = @keys.pop
1663   @keys.unshift(k)
1664   k
1665 end
values() click to toggle source

Returns sequences. (Like Hash#values)

     # File lib/bio/alignment.rb
1696 def values
1697   #(Hash-like)
1698   @keys.collect { |k| @seqs[k] }
1699 end

Protected Instance Methods

new(*arg) click to toggle source

Creates new alignment. Internal use only.

     # File lib/bio/alignment.rb
1764 def new(*arg)
1765   na = self.class.new(*arg)
1766   na.set_all_property(get_all_property)
1767   na
1768 end