9 scale = ARGV.shift.to_f
11 STDERR.puts "USAGE: newick2mafft.rb scale input_tree > output"
16 STDERR.puts "Inappropriate scale, #{scale.to_s}"
20 STDERR.puts "scale = " + scale.to_s
22 infp = File.open( ARGV.shift, "r" )
25 while line = infp.gets
32 #tree = tree.gsub( /_.*?:/, ":" ).gsub(/[0-9]\.[0-9]*e-[0-9][0-9]/, "0").gsub(/\[.*?\]/,"").gsub(/ /, "").gsub(/:\-[0-9\.]+/, ":0.0" )
33 tree = tree.gsub( /_.*?:/, ":" ).gsub(/[0-9]\.[0-9]*e-[0-9][0-9]/, "0").gsub(/\[.*?\]/,"").gsub(/ /, "")
36 STDERR.puts "Initial tree = " + tree
43 tree.sub!( /\,([0-9]+):(\-?[0-9\.]+)\,([0-9]+):(\-?[0-9\.]+)/, ",XXX" )
54 # puts "introduce XXX"
57 break unless tree.index(/XXX/)
59 poshit = tree.index(/XXX/)
60 # puts "poshit=" + poshit.to_s
65 break if height == 0 && tree[i..i] == '('
66 if tree[i..i] == ')' then
68 elsif tree[i..i] == '(' then
75 # puts "poskakko = " + poskakko.to_s
76 zenhan = tree[0..poskakko]
77 zenhan = "" if poskakko == -1
78 # puts "zenhan = " + zenhan
81 tree = zenhan + "(" + tree[poskakko+1..treelen]
84 tree.sub!( /XXX/, "#{hit1}:#{hit2}):0,#{hit3}:#{hit4}" )
97 while tree.index( /\(/ )
99 tree = resolve( tree )
101 tree.sub!( /\(([0-9]+):(\-?[0-9\.]+),([0-9]+):(\-?[0-9\.]+)\)/, "XXX" )
103 leni[0] = $2.to_f * scale
105 leni[1] = $4.to_f * scale
107 if leni[0] > 10 || leni[1] > 10 then
109 STDERR.puts "Please check the scale of branch length!"
110 STDERR.puts "The unit of branch lengths must be 'substitution/site'"
111 STDERR.puts "If the unit is 'substition' in your tree, please"
112 STDERR.puts "use the scale argument,"
113 STDERR.puts "% newick2mafft scale in > out"
114 STDERR.puts "where scale = 1/(alignment length)"
119 # STDERR.puts "subtree = " + $&
121 if memi[1] < memi[0] then
126 tree.sub!( /XXX/, memi[0].to_s )
128 STDERR.puts "Tree = " + tree
130 printf( "%5d %5d %10.5f %10.5f\n", memi[0], memi[1], leni[0], leni[1] )