class Bio::Phylip::PhylipFormat
This is phylip multiple alignment format parser. The two formats, interleaved and non-interleaved, are automatically determined.
Attributes
alignment_length[R]
alignment length
number_of_sequences[R]
number of sequences
Public Class Methods
new(str)
click to toggle source
create a new object from a string
# File lib/bio/appl/phylip/alignment.rb 26 def initialize(str) 27 @data = str.strip.split(/(?:\r\n|\r|\n)/) 28 @first_line = @data.shift 29 @number_of_sequences, @alignment_length = 30 @first_line.to_s.strip.split(/\s+/).collect { |x| x.to_i } 31 end
Public Instance Methods
alignment()
click to toggle source
Gets the alignment. Returns a Bio::Alignment
object.
# File lib/bio/appl/phylip/alignment.rb 54 def alignment 55 unless defined? @alignment then 56 do_parse 57 a = Bio::Alignment.new 58 (0...@number_of_sequences).each do |i| 59 a.add_seq(@sequences[i], @sequence_names[i]) 60 end 61 @alignment = a 62 end 63 @alignment 64 end
interleaved?()
click to toggle source
If the alignment format is “interleaved”, returns true. If not, returns false. It would mistake to determine if the alignment is very short.
# File lib/bio/appl/phylip/alignment.rb 42 def interleaved? 43 unless defined? @interleaved_flag then 44 if /\A +/ =~ @data[1].to_s then 45 @interleaved_flag = false 46 else 47 @interleaved_flag = true 48 end 49 end 50 @interleaved_flag 51 end
Private Instance Methods
do_parse()
click to toggle source
# File lib/bio/appl/phylip/alignment.rb 68 def do_parse 69 if interleaved? then 70 do_parse_interleaved 71 else 72 do_parse_noninterleaved 73 end 74 end
do_parse_interleaved()
click to toggle source
# File lib/bio/appl/phylip/alignment.rb 76 def do_parse_interleaved 77 first_block = @data[0, @number_of_sequences] 78 @data[0, @number_of_sequences] = '' 79 @sequence_names = Array.new(@number_of_sequences) { '' } 80 @sequences = Array.new(@number_of_sequences) do 81 ' ' * @alignment_length 82 end 83 first_block.each_with_index do |x, i| 84 n, s = x.split(/ +/, 2) 85 @sequence_names[i] = n 86 @sequences[i].replace(s.gsub(/\s+/, '')) 87 end 88 i = 0 89 @data.each do |x| 90 if x.strip.length <= 0 then 91 i = 0 92 else 93 @sequences[i] << x.gsub(/\s+/, '') 94 i = (i + 1) % @number_of_sequences 95 end 96 end 97 @data.clear 98 true 99 end
do_parse_noninterleaved()
click to toggle source
# File lib/bio/appl/phylip/alignment.rb 101 def do_parse_noninterleaved 102 @sequence_names = Array.new(@number_of_sequences) { '' } 103 @sequences = Array.new(@number_of_sequences) do 104 ' ' * @alignment_length 105 end 106 curseq = nil 107 i = 0 108 @data.each do |x| 109 next if x.strip.length <= 0 110 if !curseq or 111 curseq.length > @alignment_length or /^\s/ !~ x then 112 p i 113 n, s = x.strip.split(/ +/, 2) 114 @sequence_names[i] = n 115 curseq = @sequences[i] 116 curseq.replace(s.gsub(/\s+/, '')) 117 i += 1 118 else 119 curseq << x.gsub(/\s+/, '') 120 end 121 end 122 @data.clear 123 true 124 end