| Class | Bio::Alignment::OriginalAlignment |
| In: |
lib/bio/alignment.rb
|
| 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.
| 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. | ||
| keys | [R] | identifiers (or definitions or names) of the sequences |
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, line 1553
1553: def initialize(seqs = [])
1554: @seqs = {}
1555: @keys = []
1556: self.add_sequences(seqs)
1557: end
Read files and creates a new alignment object.
It will be obsoleted.
# File lib/bio/alignment.rb, line 1530
1530: def self.readfiles(*files)
1531: require 'bio/io/flatfile'
1532: aln = self.new
1533: files.each do |fn|
1534: Bio::FlatFile.open(nil, fn) do |ff|
1535: aln.add_sequences(ff)
1536: end
1537: end
1538: aln
1539: end
Adds a sequence without key. The key is automatically determined.
# File lib/bio/alignment.rb, line 1709
1709: def <<(seq)
1710: #(Array-like)
1711: self.store(nil, seq)
1712: self
1713: end
If x is the same value, returns true. Otherwise, returns false.
# File lib/bio/alignment.rb, line 1561
1561: def ==(x)
1562: #(original)
1563: if x.is_a?(self.class)
1564: self.to_hash == x.to_hash
1565: else
1566: false
1567: end
1568: end
stores a sequences with the name
| key: | name of the sequence |
| seq: | sequence |
# File lib/bio/alignment.rb, line 1614
1614: def __store__(key, seq)
1615: #(Hash-like)
1616: h = { key => seq }
1617: @keys << h.keys[0]
1618: @seqs.update(h)
1619: seq
1620: 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.
# File lib/bio/alignment.rb, line 1905
1905: def add_seq(seq, key = nil)
1906: #(BioPerl) AlignI::add_seq like method
1907: unless seq.is_a?(Bio::Sequence::NA) or seq.is_a?(Bio::Sequence::AA)
1908: s = extract_seq(seq)
1909: key = extract_key(seq) unless key
1910: seq = s
1911: end
1912: self.store(key, seq)
1913: 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.
# File lib/bio/alignment.rb, line 1581
1581: def add_sequences(seqs)
1582: if block_given? then
1583: seqs.each do |x|
1584: s, key = yield x
1585: self.store(key, s)
1586: end
1587: else
1588: if seqs.is_a?(self.class) then
1589: seqs.each_pair do |k, s|
1590: self.store(k, s)
1591: end
1592: elsif seqs.respond_to?(:each_pair)
1593: seqs.each_pair do |k, x|
1594: s = extract_seq(x)
1595: self.store(k, s)
1596: end
1597: else
1598: seqs.each do |x|
1599: s = extract_seq(x)
1600: k = extract_key(x)
1601: self.store(k, s)
1602: end
1603: end
1604: end
1605: self
1606: 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.
# File lib/bio/alignment.rb, line 1864
1864: def alignment_collect
1865: #(original)
1866: na = self.class.new
1867: na.set_all_property(get_all_property)
1868: self.each_pair do |k, s|
1869: na.store(k, yield(s))
1870: end
1871: na
1872: end
Removes empty sequences or nil in the alignment. (Like Array#compact!)
# File lib/bio/alignment.rb, line 1877
1877: def compact!
1878: #(Array-like)
1879: d = []
1880: self.each_pair do |k, s|
1881: if !s or s.empty?
1882: d << k
1883: end
1884: end
1885: d.each do |k|
1886: self.delete(k)
1887: end
1888: d.empty? ? nil : d
1889: 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.
# File lib/bio/alignment.rb, line 2038
2038: def concat(aln)
2039: #(String-like)
2040: if aln.respond_to?(:to_str) then #aln.is_a?(String)
2041: self.each do |s|
2042: s << aln
2043: end
2044: self
2045: else
2046: alignment_concat(aln)
2047: end
2048: end
Removes the sequence whose key is key. Returns the removed sequence. If not found, returns nil.
# File lib/bio/alignment.rb, line 1695
1695: def delete(key)
1696: #(Hash-like)
1697: @keys.delete(key)
1698: @seqs.delete(key)
1699: end
Performs multiple alignment by using external program.
# File lib/bio/alignment.rb, line 2076
2076: def do_align(factory)
2077: a0 = self.class.new
2078: (0...self.size).each { |i| a0.store(i, self.order(i)) }
2079: r = factory.query(a0)
2080: a1 = r.alignment
2081: a0.keys.each do |k|
2082: unless a1[k.to_s] then
2083: raise 'alignment result is inconsistent with input data'
2084: end
2085: end
2086: a2 = self.new
2087: a0.keys.each do |k|
2088: a2.store(self.keys[k], a1[k.to_s])
2089: end
2090: a2
2091: end
Duplicates the alignment
# File lib/bio/alignment.rb, line 1778
1778: def dup
1779: #(Hash-like)
1780: self.new(self)
1781: end
If the key exists, returns true. Otherwise, returns false. (Like Hash#has_key?)
# File lib/bio/alignment.rb, line 1730
1730: def has_key?(key)
1731: #(Hash-like)
1732: @seqs.has_key?(key)
1733: end
Returns the key for a given sequence. If not found, returns nil.
# File lib/bio/alignment.rb, line 1822
1822: def index(seq)
1823: #(Hash-like)
1824: last_key = nil
1825: self.each_pair do |k, s|
1826: last_key = k
1827: if s.class == seq.class then
1828: r = (s == seq)
1829: else
1830: r = (s.to_s == seq.to_s)
1831: end
1832: break if r
1833: end
1834: last_key
1835: end
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, line 1842
1842: def isolate(*arg)
1843: #(original)
1844: if arg.size == 0 then
1845: self.collect! do |s|
1846: seqclass.new(s)
1847: end
1848: else
1849: arg.each do |k|
1850: if self.has_key?(k) then
1851: s = self.delete(key)
1852: self.store(k, seqclass.new(s))
1853: end
1854: end
1855: end
1856: self
1857: end
Merge given alignment. Note that it is destructive method.
# File lib/bio/alignment.rb, line 1797
1797: def merge!(*other)
1798: #(Hash-like)
1799: if block_given? then
1800: other.each do |aln|
1801: aln.each_pair do |k, s|
1802: if self.has_key?(k) then
1803: s = yield k, self[k], s
1804: self.to_hash.store(k, s)
1805: else
1806: self.store(k, s)
1807: end
1808: end
1809: end
1810: else
1811: other.each do |aln|
1812: aln.each_pair do |k, s|
1813: self.delete(k) if self.has_key?(k)
1814: self.store(k, s)
1815: end
1816: end
1817: end
1818: self
1819: end
Gets the n-th sequence. If not found, returns nil.
# File lib/bio/alignment.rb, line 1687
1687: def order(n)
1688: #(original)
1689: @seqs[@keys[n]]
1690: end
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, line 1932
1932: def purge(*arg)
1933: #(BioPerl) AlignI::purge like method
1934: purged = self.new
1935: arg.each do |k|
1936: if self[k] then
1937: purged.store(k, self.delete(k))
1938: end
1939: end
1940: purged
1941: end
Reconstructs internal data structure. (Like Hash#rehash)
# File lib/bio/alignment.rb, line 1651
1651: def rehash
1652: @seqs.rehash
1653: oldkeys = @keys
1654: tmpkeys = @seqs.keys
1655: @keys.collect! do |k|
1656: tmpkeys.delete(k)
1657: end
1658: @keys.compact!
1659: @keys.concat(tmpkeys)
1660: self
1661: 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.
# File lib/bio/alignment.rb, line 2023
2023: def remove_all_gaps
2024: #(original)
2025: na = self.dup
2026: na.isolate
2027: na.remove_all_gaps!
2028: na
2029: end
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, line 1919
1919: def remove_seq(seq)
1920: #(BioPerl) AlignI::remove_seq like method
1921: if k = self.index(seq) then
1922: self.delete(k)
1923: else
1924: nil
1925: end
1926: 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.
# File lib/bio/alignment.rb, line 2055
2055: def replace_slice(aln, *arg)
2056: #(original)
2057: if aln.respond_to?(:to_str) then #aln.is_a?(String)
2058: self.each do |s|
2059: s[*arg] = aln
2060: end
2061: elsif aln.is_a?(self.class) then
2062: aln.each_pair do |k, s|
2063: self[k][*arg] = s
2064: end
2065: else
2066: i = 0
2067: aln.each do |s|
2068: self.order(i)[*arg] = s
2069: i += 1
2070: end
2071: end
2072: self
2073: 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.
# File lib/bio/alignment.rb, line 1951
1951: def select(*arg)
1952: #(original)
1953: na = self.new
1954: if block_given? then
1955: # 'arg' is ignored
1956: # nearly same action as Array#select (Enumerable#select)
1957: self.each_pair.each do |k, s|
1958: na.store(k, s) if yield(s)
1959: end
1960: else
1961: # BioPerl's AlignI::select like function
1962: arg.each do |k|
1963: if s = self[k] then
1964: na.store(k, s)
1965: end
1966: end
1967: end
1968: na
1969: end
Removes the first sequence in the alignment and returns [ key, seq ].
# File lib/bio/alignment.rb, line 1675
1675: def shift
1676: k = @keys.shift
1677: if k then
1678: s = @seqs.delete(k)
1679: [ k, s ]
1680: else
1681: nil
1682: end
1683: end
Number of sequences in the alignment.
# File lib/bio/alignment.rb, line 1722
1722: def size
1723: #(Hash&Array-like)
1724: @seqs.size
1725: 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.
# File lib/bio/alignment.rb, line 1628
1628: def store(key, seq)
1629: #(Hash-like) returns key instead of seq
1630: if @seqs.has_key?(key) then
1631: # don't allow same key
1632: # New key is discarded, while existing key is preserved.
1633: key = nil
1634: end
1635: unless key then
1636: unless defined?(@serial)
1637: @serial = 0
1638: end
1639: @serial = @seqs.size if @seqs.size > @serial
1640: while @seqs.has_key?(@serial)
1641: @serial += 1
1642: end
1643: key = @serial
1644: end
1645: self.__store__(key, seq)
1646: key
1647: 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.
# File lib/bio/alignment.rb, line 2139
2139: def to_fasta(*arg)
2140: #(original)
2141: warn "to_fasta is deprecated. Please use output_fasta."
2142: self.to_fasta_array(*arg).join('')
2143: end
Convert to fasta format and returns an array of strings.
It will be obsoleted.
# File lib/bio/alignment.rb, line 2096
2096: def to_fasta_array(*arg)
2097: #(original)
2098: width = nil
2099: if arg[0].is_a?(Integer) then
2100: width = arg.shift
2101: end
2102: options = (arg.shift or {})
2103: width = options[:width] unless width
2104: if options[:avoid_same_name] then
2105: na = __clustal_avoid_same_name(self.keys, 30)
2106: else
2107: na = self.keys.collect { |k| k.to_s.gsub(/[\r\n\x00]/, ' ') }
2108: end
2109: a = self.collect do |s|
2110: ">#{na.shift}\n" +
2111: if width then
2112: s.to_s.gsub(Regexp.new(".{1,#{width}}"), "\\0\n")
2113: else
2114: s.to_s + "\n"
2115: end
2116: end
2117: a
2118: end
Convets to fasta format and returns an array of FastaFormat objects.
It will be obsoleted.
# File lib/bio/alignment.rb, line 2123
2123: def to_fastaformat_array(*arg)
2124: #(original)
2125: require 'bio/db/fasta'
2126: a = self.to_fasta_array(*arg)
2127: a.collect! do |x|
2128: Bio::FastaFormat.new(x)
2129: end
2130: a
2131: end
convert to hash
# File lib/bio/alignment.rb, line 1571
1571: def to_hash
1572: #(Hash-like)
1573: @seqs
1574: end
Prepends seq (with key) to the front of the alignment. (Like Array#unshift)
# File lib/bio/alignment.rb, line 1665
1665: def unshift(key, seq)
1666: #(Array-like)
1667: self.store(key, seq)
1668: k = @keys.pop
1669: @keys.unshift(k)
1670: k
1671: end
Returns sequences. (Like Hash#values)
# File lib/bio/alignment.rb, line 1702
1702: def values
1703: #(Hash-like)
1704: @keys.collect { |k| @seqs[k] }
1705: end