Class: Bio::BioAlignment::Alignment

Inherits:
Object
  • Object
show all
Includes:
Columns, Pal2Nal, Rows, Enumerable
Defined in:
lib/bio-alignment/bioruby.rb,
lib/bio-alignment/alignment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Columns

#clone_columns!, #columns, #columns_to_s, #columns_where, #num_columns

Methods included from Rows

#rows_where

Methods included from Pal2Nal

#pal2nal

Constructor Details

#initialize(seqs = nil, ids = nil) ⇒ Alignment

Create alignment. seqs can be a list of sequences. If these are String types, they get converted to the library Sequence container



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bio-alignment/alignment.rb', line 23

def initialize seqs = nil, ids = nil
  @sequences = []
  if seqs 
    num = 0
    seqs.each_with_index do | seq, i |
      next if seq == nil or seq.to_s.strip == ""
      id = num
      id = ids[i] if ids and ids[i]
      @sequences << 
        if seq.kind_of?(String)
          seq1 = Sequence.new(id,seq.strip)
          seq1
        else
          seq
        end
      num += 1
    end
  end
end

Instance Attribute Details

#sequencesObject Also known as: rows

Returns the value of attribute sequences.



17
18
19
# File 'lib/bio-alignment/alignment.rb', line 17

def sequences
  @sequences
end

#treeObject (readonly)

Returns the value of attribute tree.



18
19
20
# File 'lib/bio-alignment/alignment.rb', line 18

def tree
  @tree
end

Instance Method Details

#[](index) ⇒ Object

Return a sequence by index



55
56
57
# File 'lib/bio-alignment/alignment.rb', line 55

def [] index
  rows[index]
end

#attach_tree(tree) ⇒ Object

extend BioAlignment with Tree functionality - this method adds a tree and pulls in the functionality of the Tree module. Returns the tree traverser



107
108
109
110
111
# File 'lib/bio-alignment/alignment.rb', line 107

def attach_tree tree
  extend Tree
  @tree = Tree::init(tree)
  @tree
end

#cloneObject

Return a deep cloned alignment. This method clones sequences, and the state objects



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/bio-alignment/alignment.rb', line 91

def clone
  aln = super
  # clone the sequences
  aln.sequences = []
  each do | seq |
    aln.sequences << seq.clone
  end
  aln.clone_columns! if @columns
  # clone the tree
  @tree = @tree.clone if @tree
  aln
end

#eachObject



59
60
61
62
# File 'lib/bio-alignment/alignment.rb', line 59

def each
  rows.each { | seq | yield seq }
  self
end

#each_elementObject



64
65
66
67
# File 'lib/bio-alignment/alignment.rb', line 64

def each_element
  each { |seq| seq.each { |e| yield e }}
  self
end

#find(name) ⇒ Object



69
70
71
72
73
74
# File 'lib/bio-alignment/alignment.rb', line 69

def find name
  each do | seq |
    return seq if seq.id == name
  end
  raise "ERROR: Sequence not found by its name, looking for <#{name}>"
end

#idsObject

return an array of sequence ids



46
47
48
# File 'lib/bio-alignment/alignment.rb', line 46

def ids
  rows.map { |r| r.id }
end

#sizeObject



50
51
52
# File 'lib/bio-alignment/alignment.rb', line 50

def size
  rows.size
end

#to_bioruby_alignmentObject



25
26
27
# File 'lib/bio-alignment/bioruby.rb', line 25

def to_bioruby_alignment
  Bio::Alignment.new(self)
end

#to_sObject



82
83
84
85
86
87
# File 'lib/bio-alignment/alignment.rb', line 82

def to_s
  res = ""
  res += "\t" + columns_to_s + "\n" if @columns
  res += map{ |seq| seq.id.to_s + "\t" + seq.to_s }.join("\n")
  res
end

#tree_reduce(new_tree) ⇒ Object

Reduce an alignment, based on the new tree



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/bio-alignment/alignment.rb', line 114

def tree_reduce new_tree
  names = new_tree.map { | node | node.name }.compact
  # p names
  nrows = []
  names.each do | name |
    nrows << find(name).clone
  end
  new_aln = Alignment.new(nrows)
  new_aln.attach_tree(new_tree.clone)
  new_aln
end

#update_each_elementObject

clopy alignment and allow updating elements



77
78
79
80
# File 'lib/bio-alignment/alignment.rb', line 77

def update_each_element
  aln = self.clone
  aln.each { |seq| seq.each_with_index { |e,i| seq.seq[i] = yield e }}
end