Class: Bio::Alignment::OriginalAlignment
- Includes:
- HashExtension, OriginalPrivate, Enumerable
- Defined in:
- lib/bio/alignment.rb
Overview
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.
Constant Summary
Constants included from PropertyMethods
PropertyMethods::GAP_CHAR, PropertyMethods::GAP_REGEXP, PropertyMethods::MISSING_CHAR
Instance Attribute Summary collapse
-
#keys ⇒ Object
readonly
identifiers (or definitions or names) of the sequences.
Attributes included from PropertyMethods
#gap_char, #gap_regexp, #missing_char, #seqclass
Class Method Summary collapse
-
.new2(*arg) ⇒ Object
Creates a new alignment object from given arguments.
-
.readfiles(*files) ⇒ Object
Read files and creates a new alignment object.
Instance Method Summary collapse
-
#<<(seq) ⇒ Object
Adds a sequence without key.
-
#==(x) ⇒ Object
If x is the same value, returns true.
-
#[](*arg) ⇒ Object
Gets a sequence.
-
#__store__(key, seq) ⇒ Object
- stores a sequences with the name key
- name of the sequence seq
-
sequence.
-
#add_seq(seq, key = nil) ⇒ Object
Adds a sequence to the alignment.
-
#add_sequences(seqs) ⇒ Object
Adds sequences to the alignment.
-
#alignment_collect ⇒ Object
(also: #collect_align)
Iterates over each sequence and each results running block are collected and returns a new alignment.
-
#collect! ⇒ Object
Iterates over each sequence, replacing the sequence with the value returned by the block.
-
#compact ⇒ Object
Removes empty sequences or nil and returns new alignment.
-
#compact! ⇒ Object
Removes empty sequences or nil in the alignment.
-
#concat(aln) ⇒ Object
Concatenates a string or an alignment.
-
#delete(key) ⇒ Object
Removes the sequence whose key is key.
-
#do_align(factory) ⇒ Object
Performs multiple alignment by using external program.
-
#dup ⇒ Object
Duplicates the alignment.
-
#each ⇒ Object
(also: #each_seq)
Iterates over each sequence.
-
#each_pair ⇒ Object
Iterates over each key and sequence.
-
#has_key?(key) ⇒ Boolean
If the key exists, returns true.
-
#index(seq) ⇒ Object
Returns the key for a given sequence.
-
#initialize(seqs = []) ⇒ OriginalAlignment
constructor
Creates a new alignment object.
-
#isolate(*arg) ⇒ Object
Sequences in the alignment are duplicated.
-
#lstrip ⇒ Object
Not-destructive version of alignment_lstrip!.
-
#merge(*other) ⇒ Object
Merges given alignment and returns a new alignment.
-
#merge!(*other) ⇒ Object
Merge given alignment.
-
#normalize ⇒ Object
Not-destructive version of alignment_normalize!.
-
#order(n) ⇒ Object
Gets the n-th sequence.
-
#purge(*arg) ⇒ Object
Removes sequences from the alignment by given keys.
-
#rehash ⇒ Object
Reconstructs internal data structure.
-
#remove_all_gaps ⇒ Object
Not-destructive version of remove_gaps!.
-
#remove_seq(seq) ⇒ Object
Removes given sequence from the alignment.
-
#replace_slice(aln, *arg) ⇒ Object
Replace the specified region of the alignment to aln.
-
#rstrip ⇒ Object
Not-destructive version of alignment_rstrip!.
-
#select(*arg) ⇒ Object
If block is given, it acts like Array#select (Enumerable#select).
-
#shift ⇒ Object
Removes the first sequence in the alignment and returns [ key, seq ].
-
#size ⇒ Object
(also: #number_of_sequences)
Number of sequences in the alignment.
-
#store(key, seq) ⇒ Object
stores a sequence with key (name or definition of the sequence).
-
#strip ⇒ Object
Not-destructive version of alignment_strip!.
-
#to_fasta(*arg) ⇒ Object
Converts to fasta format and returns a string.
-
#to_fasta_array(*arg) ⇒ Object
Convert to fasta format and returns an array of strings.
-
#to_fastaformat_array(*arg) ⇒ Object
Convets to fasta format and returns an array of FastaFormat objects.
-
#to_hash ⇒ Object
convert to hash.
-
#unshift(key, seq) ⇒ Object
Prepends seq (with key) to the front of the alignment.
-
#values ⇒ Object
Returns sequences.
Methods included from OriginalPrivate
Methods included from HashExtension
#alignment_concat, #sequence_names
Methods included from EnumerableExtension
#alignment_concat, #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, #sequence_names
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
Constructor Details
#initialize(seqs = []) ⇒ OriginalAlignment
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.
1548 1549 1550 1551 1552 |
# File 'lib/bio/alignment.rb', line 1548 def initialize(seqs = []) @seqs = {} @keys = [] self.add_sequences(seqs) end |
Instance Attribute Details
#keys ⇒ Object (readonly)
identifiers (or definitions or names) of the sequences
1604 1605 1606 |
# File 'lib/bio/alignment.rb', line 1604 def keys @keys end |
Class Method Details
.new2(*arg) ⇒ Object
Creates a new alignment object from given arguments.
It will be obsoleted.
1539 1540 1541 |
# File 'lib/bio/alignment.rb', line 1539 def self.new2(*arg) self.new(arg) end |
.readfiles(*files) ⇒ Object
Read files and creates a new alignment object.
It will be obsoleted.
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 |
# File 'lib/bio/alignment.rb', line 1525 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 |
Instance Method Details
#<<(seq) ⇒ Object
Adds a sequence without key. The key is automatically determined.
1703 1704 1705 1706 1707 |
# File 'lib/bio/alignment.rb', line 1703 def <<(seq) #(Array-like) self.store(nil, seq) self end |
#==(x) ⇒ Object
If x is the same value, returns true. Otherwise, returns false.
1556 1557 1558 1559 1560 1561 1562 1563 |
# File 'lib/bio/alignment.rb', line 1556 def ==(x) #(original) if x.is_a?(self.class) self.to_hash == x.to_hash else false end end |
#[](*arg) ⇒ Object
Gets a sequence. (Like Hash#[])
1710 1711 1712 1713 |
# File 'lib/bio/alignment.rb', line 1710 def [](*arg) #(Hash-like) @seqs[*arg] end |
#__store__(key, seq) ⇒ Object
stores a sequences with the name
- key
-
name of the sequence
- seq
-
sequence
1609 1610 1611 1612 1613 1614 1615 |
# File 'lib/bio/alignment.rb', line 1609 def __store__(key, seq) #(Hash-like) h = { key => seq } @keys << h.keys[0] @seqs.update(h) seq end |
#add_seq(seq, key = nil) ⇒ Object
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.
1899 1900 1901 1902 1903 1904 1905 1906 1907 |
# File 'lib/bio/alignment.rb', line 1899 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 |
#add_sequences(seqs) ⇒ Object
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.
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 |
# File 'lib/bio/alignment.rb', line 1576 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 |
#alignment_collect ⇒ Object Also known as: collect_align
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.
1858 1859 1860 1861 1862 1863 1864 1865 1866 |
# File 'lib/bio/alignment.rb', line 1858 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 |
#collect! ⇒ Object
Iterates over each sequence, replacing the sequence with the value returned by the block.
1750 1751 1752 1753 1754 1755 |
# File 'lib/bio/alignment.rb', line 1750 def collect! #(Array-like) @keys.each do |k| @seqs[k] = yield @seqs[k] end end |
#compact ⇒ Object
Removes empty sequences or nil and returns new alignment. (Like Array#compact)
1887 1888 1889 1890 1891 1892 |
# File 'lib/bio/alignment.rb', line 1887 def compact #(Array-like) na = self.dup na.compact! na end |
#compact! ⇒ Object
Removes empty sequences or nil in the alignment. (Like Array#compact!)
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 |
# File 'lib/bio/alignment.rb', line 1871 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 |
#concat(aln) ⇒ Object
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.
2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 |
# File 'lib/bio/alignment.rb', line 2032 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 |
#delete(key) ⇒ Object
Removes the sequence whose key is key. Returns the removed sequence. If not found, returns nil.
1689 1690 1691 1692 1693 |
# File 'lib/bio/alignment.rb', line 1689 def delete(key) #(Hash-like) @keys.delete(key) @seqs.delete(key) end |
#do_align(factory) ⇒ Object
Performs multiple alignment by using external program.
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 |
# File 'lib/bio/alignment.rb', line 2070 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 |
#dup ⇒ Object
Duplicates the alignment
1772 1773 1774 1775 |
# File 'lib/bio/alignment.rb', line 1772 def dup #(Hash-like) self.new(self) end |
#each ⇒ Object Also known as: each_seq
Iterates over each sequence. (Like Array#each)
1731 1732 1733 1734 1735 1736 |
# File 'lib/bio/alignment.rb', line 1731 def each #(Array-like) @keys.each do |k| yield @seqs[k] end end |
#each_pair ⇒ Object
Iterates over each key and sequence. (Like Hash#each_pair)
1741 1742 1743 1744 1745 1746 |
# File 'lib/bio/alignment.rb', line 1741 def each_pair #(Hash-like) @keys.each do |k| yield k, @seqs[k] end end |
#has_key?(key) ⇒ Boolean
If the key exists, returns true. Otherwise, returns false. (Like Hash#has_key?)
1724 1725 1726 1727 |
# File 'lib/bio/alignment.rb', line 1724 def has_key?(key) #(Hash-like) @seqs.has_key?(key) end |
#index(seq) ⇒ Object
Returns the key for a given sequence. If not found, returns nil.
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 |
# File 'lib/bio/alignment.rb', line 1816 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 |
#isolate(*arg) ⇒ Object
Sequences in the alignment are duplicated. If keys are given to the argument, sequences of given keys are duplicated.
It will be obsoleted.
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 |
# File 'lib/bio/alignment.rb', line 1836 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 |
#lstrip ⇒ Object
Not-destructive version of alignment_lstrip!. Returns a new alignment.
1994 1995 1996 1997 1998 1999 2000 |
# File 'lib/bio/alignment.rb', line 1994 def lstrip #(String-like) na = self.dup na.isolate na.alignment_lstrip! na end |
#merge(*other) ⇒ Object
Merges given alignment and returns a new alignment.
1782 1783 1784 1785 1786 1787 |
# File 'lib/bio/alignment.rb', line 1782 def merge(*other) #(Hash-like) na = self.new(self) na.merge!(*other) na end |
#merge!(*other) ⇒ Object
Merge given alignment. Note that it is destructive method.
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 |
# File 'lib/bio/alignment.rb', line 1791 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 |
#normalize ⇒ Object
Not-destructive version of alignment_normalize!. Returns a new alignment.
1975 1976 1977 1978 1979 1980 |
# File 'lib/bio/alignment.rb', line 1975 def normalize #(original) na = self.dup na.alignment_normalize! na end |
#order(n) ⇒ Object
Gets the n-th sequence. If not found, returns nil.
1681 1682 1683 1684 |
# File 'lib/bio/alignment.rb', line 1681 def order(n) #(original) @seqs[@keys[n]] end |
#purge(*arg) ⇒ Object
Removes sequences from the alignment by given keys. Returns an alignment object consists of removed sequences.
It resembles BioPerl’s AlignI::purge method.
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 |
# File 'lib/bio/alignment.rb', line 1926 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 |
#rehash ⇒ Object
Reconstructs internal data structure. (Like Hash#rehash)
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 |
# File 'lib/bio/alignment.rb', line 1646 def rehash @seqs.rehash tmpkeys = @seqs.keys @keys.collect! do |k| tmpkeys.delete(k) end @keys.compact! @keys.concat(tmpkeys) self end |
#remove_all_gaps ⇒ Object
Not-destructive version of remove_gaps!. Returns a new alignment.
The method name ‘remove_gap’ will be obsoleted. Please use ‘remove_all_gaps’ instead.
2017 2018 2019 2020 2021 2022 2023 |
# File 'lib/bio/alignment.rb', line 2017 def remove_all_gaps #(original) na = self.dup na.isolate na.remove_all_gaps! na end |
#remove_seq(seq) ⇒ Object
Removes given sequence from the alignment. Returns removed sequence. If nothing removed, returns nil.
It resembles BioPerl’s AlignI::remove_seq.
1913 1914 1915 1916 1917 1918 1919 1920 |
# File 'lib/bio/alignment.rb', line 1913 def remove_seq(seq) #(BioPerl) AlignI::remove_seq like method if k = self.index(seq) then self.delete(k) else nil end end |
#replace_slice(aln, *arg) ⇒ Object
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.
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 |
# File 'lib/bio/alignment.rb', line 2049 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 |
#rstrip ⇒ Object
Not-destructive version of alignment_rstrip!. Returns a new alignment.
1984 1985 1986 1987 1988 1989 1990 |
# File 'lib/bio/alignment.rb', line 1984 def rstrip #(String-like) na = self.dup na.isolate na.alignment_rstrip! na end |
#select(*arg) ⇒ Object
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.
1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 |
# File 'lib/bio/alignment.rb', line 1945 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 |
#shift ⇒ Object
Removes the first sequence in the alignment and returns [ key, seq ].
1669 1670 1671 1672 1673 1674 1675 1676 1677 |
# File 'lib/bio/alignment.rb', line 1669 def shift k = @keys.shift if k then s = @seqs.delete(k) [ k, s ] else nil end end |
#size ⇒ Object Also known as: number_of_sequences
Number of sequences in the alignment.
1716 1717 1718 1719 |
# File 'lib/bio/alignment.rb', line 1716 def size #(Hash&Array-like) @seqs.size end |
#store(key, seq) ⇒ Object
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.
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 |
# File 'lib/bio/alignment.rb', line 1623 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 |
#strip ⇒ Object
Not-destructive version of alignment_strip!. Returns a new alignment.
2004 2005 2006 2007 2008 2009 2010 |
# File 'lib/bio/alignment.rb', line 2004 def strip #(String-like) na = self.dup na.isolate na.alignment_strip! na end |
#to_fasta(*arg) ⇒ Object
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.
2133 2134 2135 2136 2137 |
# File 'lib/bio/alignment.rb', line 2133 def to_fasta(*arg) #(original) warn "to_fasta is deprecated. Please use output_fasta." self.to_fasta_array(*arg).join('') end |
#to_fasta_array(*arg) ⇒ Object
Convert to fasta format and returns an array of strings.
It will be obsoleted.
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 |
# File 'lib/bio/alignment.rb', line 2090 def to_fasta_array(*arg) #(original) width = nil if arg[0].is_a?(Integer) then width = arg.shift end = (arg.shift or {}) width = [:width] unless width if [: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 |
#to_fastaformat_array(*arg) ⇒ Object
Convets to fasta format and returns an array of FastaFormat objects.
It will be obsoleted.
2117 2118 2119 2120 2121 2122 2123 2124 2125 |
# File 'lib/bio/alignment.rb', line 2117 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 |
#to_hash ⇒ Object
convert to hash
1566 1567 1568 1569 |
# File 'lib/bio/alignment.rb', line 1566 def to_hash #(Hash-like) @seqs end |
#unshift(key, seq) ⇒ Object
Prepends seq (with key) to the front of the alignment. (Like Array#unshift)
1659 1660 1661 1662 1663 1664 1665 |
# File 'lib/bio/alignment.rb', line 1659 def unshift(key, seq) #(Array-like) self.store(key, seq) k = @keys.pop @keys.unshift(k) k end |
#values ⇒ Object
Returns sequences. (Like Hash#values)
1696 1697 1698 1699 |
# File 'lib/bio/alignment.rb', line 1696 def values #(Hash-like) @keys.collect { |k| @seqs[k] } end |