3 # Copyright (C) 2007-2012 Martin A. Hansen.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23 # This program is part of the Biopieces framework (www.biopieces.org).
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27 # Classify records with taxonomy information.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32 require 'maasha/biopieces'
34 # Class containing methods to construct a taxonomic tree recursively
35 # for the classification of organisms. Currently only works with GreenGenes type of entries
37 attr_accessor :level, :name, :count, :score, :children
39 # Method to initalize a TaxNode object.
40 def initialize(level, name, count, score)
41 @level = level # Taxonomic level e.g. phylum, class, etc
42 @name = name # Name of organism
43 @count = count # Number of times this organism was encountered
44 @score = score # Similarity score
48 # Method to add to the taxonomic tree a GreenGenes entry.
49 def add_gg(s_id, score, size)
52 s_id.scan(/([\w])__([^;]+)/) do
53 level = expand_level($1)
56 if node.children[name].nil?
57 node.children[name] = TaxNode.new(level, name, size * 1, size * score)
59 node.children[name].count += size * 1
60 node.children[name].score += size * score
63 node = node.children[name]
67 # Method to merge two TaxNodes.
68 def merge(node_new, node_old = self)
69 node_new.children.each do |name, child|
70 if node_old.children[name]
71 node_old.children[name].count += child.count
72 node_old.children[name].score += child.score
74 merge(child, node_old.children[name])
76 node_old.children[name] = child
81 # Method to flatten a taxonomic tree turning this into a list by recursive depth first traversal.
82 def flatten(node = self, list = [])
83 list << TaxNode.new(node.level, node.name, node.count, node.score)
85 node.children.each_value do |child|
92 # Method to recursively remove branches in taxonomic tree where the child count is less than or
93 # equal to a given minimum.
94 def debranch(min_count = nil)
97 node.children.each do |name, child|
98 node.children.delete(name) if child.count <= min_count
101 node.children.each_value do |child|
102 child.debranch(min_count)
106 # Method to recursively trim a taxonomic tree so that it only contains an unbranched tree,
107 # which gives the lowest common ancestor.
108 def lowest_common_ancestor
111 node.children = {} if node.children.size > 1
113 node.children.each_value do |child|
114 child.lowest_common_ancestor
118 # Method for iterating over a taxonomic tree.
120 self.flatten.each do |node|
127 # Method to convert a TaxNode to a Biopiece record.
130 record[:REC_TYPE] = "Classification"
131 record[:LEVEL] = @level
132 record[:NAME] = @name
133 record[:COUNT] = @count
134 record[:SCORE] = @count == 0 ? 0.0 : (@score / @count).round(2)
141 # Method containing a helper hash to expand the phylogenetic level name.
142 def expand_level(level)
154 raise "unknown level: #{level}" unless hash[level]
161 casts << {:long=>'LCA', :short=>'l', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
162 casts << {:long=>'size', :short=>'s', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
163 casts << {:long=>'min_count', :short=>'m', :type=>'uint', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
165 options = Biopieces.options_parse(ARGV, casts)
167 tax_tree = TaxNode.new("root", "Root", 0, 0.0)
170 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
171 input.each_record do |record|
172 if record[:Q_ID] and record[:S_ID] and record[:SCORE]
176 if record[:Q_ID].match(/_(\d+)$/)
179 # raise BiopiecesError, "Could not extract size from Q_ID: #{record[:Q_ID]}"
184 tax_hash[record[:Q_ID]] = TaxNode.new("root", "Root", 0, 0.0) unless tax_hash[record[:Q_ID]]
185 tax_hash[record[:Q_ID]].add_gg(record[:S_ID], record[:SCORE].to_f, size)
187 tax_tree.add_gg(record[:S_ID], record[:SCORE].to_f, size)
195 tax_hash.each_value do |tree|
196 tree.debranch(options[:min_count]) if options[:min_count]
197 tree.lowest_common_ancestor
200 tax_hash.each_value do |tree|
205 tax_tree.each do |node|
206 output.puts node.to_bp unless node.level == 'root'
211 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<