Class: NewickFile

Inherits:
Object
  • Object
show all
Defined in:
lib/rnewick.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ NewickFile

Returns a new instance of NewickFile.



59
60
61
62
# File 'lib/rnewick.rb', line 59

def initialize(filename)
  @newickStrings = File.open(filename).readlines.map{|line| NewickString.new(line)}
  @filename = filename 
end

Instance Attribute Details

#newickStringsObject (readonly)

Returns the value of attribute newickStrings.



58
59
60
# File 'lib/rnewick.rb', line 58

def newickStrings
  @newickStrings
end

Instance Method Details

#remove_taxa(taxa, pruned_tree, verbose = false) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rnewick.rb', line 73

def remove_taxa(taxa, pruned_tree, verbose = false)
  # assume nw_prune and nw_reroot are in the PATH
  nw_pruner = "nw_prune"
  nw_unrooter = "nw_reroot"
  #required_utils = [nw_pruner, nw_unrooter] 
  required_utils = [nw_pruner] 
  required_utils.each do |nw_util|
    raise "#{nw_util} from newick utilities not available" unless binary_available(nw_util)
  end
  # prune a list of taxa and unroot resulting tree with these nice and fast newick utilities
  # on the first tree of the file or all of them?
  #system "#{nw_pruner} #{@filename} #{taxa.join ' '} | #{nw_unrooter} -d - > #{pruned_tree}"
  system "#{nw_pruner} #{@filename} #{taxa.join ' '} > #{pruned_tree}"
  puts "removed #{taxa.join ','} and saved in #{pruned_tree}" if verbose
end

#save_each_newick_as(newfile_basename, ending) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/rnewick.rb', line 66

def save_each_newick_as(newfile_basename, ending)
  @newickStrings.each_with_index do |newick, i|
    File.open(newfile_basename + "_#{i}.#{ending}" , "w") do |f|
      f.puts newick.str
    end
  end
end

#sizeObject



63
64
65
# File 'lib/rnewick.rb', line 63

def size
  @newickStrings.size
end