Class: Bio::MAF::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/bio/maf/writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fspec) ⇒ Writer

Returns a new instance of Writer.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/bio/maf/writer.rb', line 6

def initialize(fspec)
  if fspec.respond_to? :write
    @f = fspec
    if fspec.respond_to? :path
      @path = fspec.path
    end
  else
    @path = fspec
    @f = File.open(fspec, 'w')
  end
end

Instance Attribute Details

#fObject (readonly)



4
5
6
# File 'lib/bio/maf/writer.rb', line 4

def f
  @f
end

#pathObject (readonly)



4
5
6
# File 'lib/bio/maf/writer.rb', line 4

def path
  @path
end

Instance Method Details

#flatten_vars(vars) ⇒ Object



18
19
20
# File 'lib/bio/maf/writer.rb', line 18

def flatten_vars(vars)
  vars.to_a.collect {|k, v| "#{k}=#{v}"}.join(" ")
end

#write_block(block) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/bio/maf/writer.rb', line 34

def write_block(block)
  lines = ["a #{flatten_vars(block.vars)}"]
  block.sequences.each do |seq| 
    write_seq(seq, lines)
  end
  lines << "\n"
  f.write(lines.join("\n"))
end

#write_blocks(blocks) ⇒ Object



27
28
29
30
31
32
# File 'lib/bio/maf/writer.rb', line 27

def write_blocks(blocks)
  blocks.each do |block|
    write_block(block)
  end
  f.flush
end

#write_header(header) ⇒ Object



22
23
24
25
# File 'lib/bio/maf/writer.rb', line 22

def write_header(header)
  f.write "##maf #{flatten_vars(header.vars)}\n"
  f.write "##{header.alignment_params}\n" if header.alignment_params
end

#write_seq(s, lines) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bio/maf/writer.rb', line 43

def write_seq(s, lines)
  lines << sprintf("%s %-20s %12d %2d %s %9d %s",
                   s.empty? ? "e" : "s",
                   s.source,
                   s.start,
                   s.size,
                   s.strand,
                   s.src_size,
                   s.empty? ? s.status : s.text)
  if s.quality
    lines << sprintf("q %-20s                           %s",
                     s.source, s.quality)
  end
  if s.i_data
    lines << sprintf("i %-20s %s %s %s %s",
                     s.source, *s.i_data)
  end
end