class BioDSL::WriteTree
Write aligned sequences from stream as a tree.¶ ↑
Description
write_tree
takes aligned sequences from the stream and uses FastTree to to create a distance tree between the sequences. The tree is in Newick format. FastTree must be installed.
For more about the FastTree here:
www.microbesonline.org/fasttree/
Usage¶ ↑
write_tree([, output: <file>[, force: <bool>[, type: <string>]]])
Options¶ ↑
-
output <file> - Output file.
-
force <bool> - Force overwrite existing output file.
-
type <string> - Sequence type :dna|:rna|:protein (default=:dna).
Examples¶ ↑
To create a tree from aligned FASTA sequences in the file ‘align.fna` do:
BD.new. read_fasta(input: "align.fna"). write_tree(output: "align.tree"). run
Constants
- STATS
Public Class Methods
Constructor for WriteTree
.
@param options [Hash] Options hash. @option options [String] :output @option options [Boolean] :force @option options [Symbol] :type
@return [WriteTree] Class instance.
# File lib/BioDSL/commands/write_tree.rb, line 73 def initialize(options) @options = options aux_exist('FastTree') check_options @cmd = compile_command end
Public Instance Methods
Return command lambda for write_tree.
@return [Proc] Command
lambda.
# File lib/BioDSL/commands/write_tree.rb, line 87 def lmb lambda do |input, output, status| status_init(status, STATS) Open3.popen3(@cmd) do |stdin, stdout, stderr, wait_thr| input.each_with_index do |record, i| @status[:records_in] += 1 write_seq(stdin, record, i) if record[:SEQ] output << record && @status[:records_out] += 1 if output end stdin.close tree_data = stdout.read.chomp stdout.close exit_status = wait_thr.value fail stderr.read unless exit_status.success? write_tree(tree_data) end end end
Private Instance Methods
Check options.
# File lib/BioDSL/commands/write_tree.rb, line 121 def check_options options_allowed(@options, :force, :output, :type) options_allowed_values(@options, type: [:dna, :rna, :protein]) options_files_exist_force(@options, :output) end
Compile command for running FastTree.
@return [String] FastTree command.
# File lib/BioDSL/commands/write_tree.rb, line 130 def compile_command cmd = [] cmd << 'FastTree' cmd << '-nt' unless @options[:type] == :protein cmd << '-quiet' unless BioDSL.verbose cmd.join(' ') end
Write a record with sequence to stdin.
@param stdin [IO] Open3 IO. @param record [Hash] BioDSL
record. @param i [Integer] Record index.
# File lib/BioDSL/commands/write_tree.rb, line 143 def write_seq(stdin, record, i) entry = BioDSL::Seq.new_bp(record) entry.seq_name ||= i @status[:sequences_in] += 1 @status[:residues_in] += entry.length stdin.puts entry.to_fasta end
Write tree data to file or stdout.
@param tree_data [String] Tree data in Newick format.
# File lib/BioDSL/commands/write_tree.rb, line 156 def write_tree(tree_data) if @options[:output] File.open(@options[:output], 'w') do |ios| ios.puts tree_data end else puts tree_data end end