macro [1024 ;
macro* 15 (((ntax + 1) * 2) + 100 ) ;
macro = ;
macfloat 0 ;
lquote [ ;
travtree + ; 

/* --- From TNT To SVG --- 
   A simple macro to transform a TNT tree into a vectorial graphic (SVG)
   it can be read with any web browser (except... that one)
   (written by J. Salvador Arias, 2010)

   syntaxis
   toSVG T N H W F
   Parameters:
       T, the number of tree used for the output (tag-tree by default)
       N, the number of reference node used for the output (root by default)
       H, height of the output (autocalculated by default)
       W, width of the output (autocalculated by default)
       F, font size of the output (12 by default)
   Writes the result in tags.svg
*/

var:
 actTree
 actNode
 actDes
 trgtNode
 nextNode
 maxYSize
 nodeSize
 actY
 tmpY
 minY
 maxY
 yFactor
 maxXSize
 actX
 tmpX
 xFactor
 fontSize
 maxDist
 dist
 done 
 widthVal
 yPos [ ((ntax + 1) * 2) ]
;

goto = %0 ;
goto [%0 OnError ; 

if (ntax < 0)
  goto OnError ;
  p/ ;
end

if (argnumber > 0)
  set actTree %1 ;
else
    copytree + ; 
    set actTree ntrees ;
end
if ('actTree' > ntrees)
  quote ERROR: Invalid tree ;
  goto OnError ;
  p /;
end

if (argnumber > 1)
  set trgtNode %2 ;
else
  set trgtNode root ;
end
if ('trgtNode' > nnodes [ 'actTree' ])
  quote ERROR: Invalid node ;
  goto OnError ;
  p /;
end
if ('trgtNode' < ntax)
  quite Using root as base node ;
  set trgtNode root ;
end

set nodeSize numdes [ 'actTree' 'trgtNode' ] ;
if (argnumber > 2)
  set maxYSize %3 ;
  set yFactor ( 'maxYSize' / 'nodeSize' );
else
  set yFactor 30 ;					/* Cambiar esto */
  set maxYSize ('nodeSize' * 'yFactor') ;
end
set maxYSize += 'yFactor' ;

set maxDist 1 ;
loop =t 0 ntax
  if ( isanc ['actTree' 'trgtNode' #t ] )
    set dist distnode [ 'actTree' #t 'trgtNode' ] ;
    if ( 'dist' > 'maxDist' )
      set maxDist 'dist' ;
    end
  end
stop

if (argnumber > 3)
  set maxXSize %4 ;
  set xFactor ( 'maxXSize' / 'maxDist' ) ;
else
  set maxXSize ( 'maxDist' * 50 ) ;
  set xFactor 50 ;
end
set maxXSize += 250 ;

if (argnumber > 4)
  set fontSize %5 ;
else
  set fontSize 12 ;
end

set widthVal 1 ;

report- ;
sil = all ; 
log tags.svg ;
sil - file ; 
quote <?xml version=&34&49.0&34?> ;
quote <svg width="'maxXSize'" height="'maxYSize'" xmlns=&34http://www.w3.org/2000/svg&34> ;
quote <desc>Phylogenetic tree extracted from TNT</desc> ;
quote <g stroke-width="'widthVal'" stroke=&34black&34&32font-family=&34Verdana&34&32font-size="'fontSize'"> ;

set widthVal 0 ;

set done 0 ; 
set actNode 'trgtNode' ;
set actY 15 ; 
sil = con ; 

loop =nav 0 2

  set done ++ ;
  progress 'done' (root*2) Converting...; 
/*  quote <g id="'actNode'"> ; */
  set nextNode firstdes [ 'actTree' 'actNode' ] ;
  if ('nextNode' == -1)
    set yPos [ 'actNode' ] 'actY' ;
    set dist distnode [ 'actTree' 'actNode' 'trgtNode' ] + 1 ;
    set actX ( 5 + ( 'dist' * 'xFactor' )) ;
    set tmpX ( 'actX' - 'xFactor' ) ;
    quote <line x1="'tmpX'" y1="'actY'" x2="'actX'" y2="'actY'" /> ;
    set tmpX ( 'actX' + 5 );
    set actY += 5 ; 
    quote <text x="'tmpX'" y="'actY'" stroke-width="'widthVal'">$taxon 'actNode'</text> ;    
    set actY -= 5 ; 
/*    quote </g> ; */
    set actY += 'yFactor' ;
  else
    set actNode 'nextNode' ;
    setloop 0 ;
    continue ;
  end
  loop =gonx 0 2
    set nextNode sister [ 'actTree' 'actNode' ] ;
    if (( 'nextNode' == -1) || ( 'nextNode' == firstdes [ 'actTree' anc [ 'actTree' 'actNode' ]]))
      set actNode anc [ 'actTree' 'actNode' ] ;
      set actDes firstdes [ 'actTree' 'actNode' ] ;
      set minY 'maxYSize' ;
      set maxY 0 ;
      loop =ades 0 2
        set tmpY 'yPos [ 'actDes' ]' ;
        if ( 'tmpY' < 'minY' )
          set minY 'tmpY' ;
        end
        if ( 'tmpY' > 'maxY' )
          set maxY 'tmpY' ;
        end
        set actDes sister [ 'actTree' 'actDes' ] ;
        if (( 'actDes' == -1) || ( 'actDes' == firstdes [ 'actTree' 'actNode' ])) endloop ; end
        setloop 0;
      stop
      set dist distnode [ 'actTree' 'actNode' 'trgtNode' ] + 1 ;
      set actX ( 5 + ( 'dist' * 'xFactor' )) ;
      quote <line x1="'actX'" y1="'minY'" x2="'actX'" y2="'maxY'" /> ;
      set tmpX ( 'actX' - 'xFactor' ) ;
      set maxY -= (( 'maxY' - 'minY' ) / 2 ) ;
      set yPos [ 'actNode' ] 'maxY' ;
      quote <line x1="'tmpX'" y1="'maxY'" x2="'actX'" y2="'maxY'" /> ;
      set tmpX += 5 ;
      set maxY -= 5 ;
      quote <text x="'tmpX" y="'maxY'" stroke-width="'widthVal'">$ttag 'actNode'</text> ;    
/*      quote </g> ; */
      if ('actNode' == 'trgtNode') endloop ; end ;
      setloop 0 ;
      continue ;
    end
    set actNode 'nextNode' ;
    endloop ;
  stop ;
  if ('actNode' == 'trgtNode') endloop ; end ;
  setloop 0 ;
stop ;

progress/; 
quote </g> ;
quote </svg> ;
log /;
sil - all ;
report = ; 
p /;

label OnError;
quote *** --- From TNT To SVG --- *** 
 A simple macro to transform a TNT tree into a vectorial graphic (SVG) 
 it can be read with any web browser (except... that one) 
 Written by J. Salvador Arias (2010)  
 syntaxis 
 toSVG T N H W F 
 Parameters: 
 T, the number of tree used for the output (tag-tree by default) 
 N, the number of reference node used for the output (root by default) 
 H, height of the output (autocalculated by default) 
 W, width of the output (autocalculated by default) 
 F, font size of the output (12 by default) 
 Writes the result in tags.svg ;
p/ ;
