Class: Bio::BioAlignment::Alignment

Inherits:
Object
  • Object
show all
Includes:
Coerce, 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 Coerce

fetch_id, fetch_seq, fetch_seq_string, to_elements

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



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

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



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

def sequences
  @sequences
end

#treeObject (readonly)

Returns the value of attribute tree



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

def tree
  @tree
end

Instance Method Details

#<<(seq) ⇒ Object



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

def << seq
  @sequences << seq
end

#[](index) ⇒ Object

Return a sequence by index



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

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



114
115
116
117
118
# File 'lib/bio-alignment/alignment.rb', line 114

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

#cloneObject

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



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/bio-alignment/alignment.rb', line 98

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



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

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

#each_elementObject



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

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

#find(name) ⇒ Object



74
75
76
77
78
79
# File 'lib/bio-alignment/alignment.rb', line 74

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

#idsObject

return an array of sequence ids



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

def ids
  rows.map { |r| Coerce::fetch_id(r) }
end

#sizeObject



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

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



88
89
90
91
92
93
94
# File 'lib/bio-alignment/alignment.rb', line 88

def to_s
  res = ""
  res += "\t" + columns_to_s + "\n" if @columns
  # fetch each sequence in turn
  res += map{ |seq| Coerce::fetch_id(seq).to_s + "\t" + Coerce::fetch_seq_string(seq) }.join("\n")
  res
end

#tree_reduce(new_tree) ⇒ Object

Reduce an alignment, based on the new tree



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/bio-alignment/alignment.rb', line 121

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

copy alignment and allow updating elements. Returns alignment.



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

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