#!/usr/bin/perl ####!/opt/perl-5.16.2/bin/perl # Program name: formatFigtree2.pl # Version 1.0 (june 2015) # Author: Celine Petitjean # modified and translated to English by Lukasz Sobala in July 2023 #Format a newick tree to a figtree format, coloring the leaves depending on the taxa (or anything given in the list with color) # Infiles: List of trees in newick format, in one line, and one tree per file. #Document with in column one the list of the pattern we will use to define the color of the leave (could be a taxon, number anything but without space) and separated by a space a second column with the color tag from figtree we want to add for the pattern #outfile: trees in figtree format, named tree.fgcol #also change: the order of the nodes (decreasing), the values at nodes (label) and the font for nodes and leaves (Liberation Sans). ################################################################################### $USAGE = "\n formatFigtree2.pl CP version 1.0 (june 2015), modified July 2023\n\n [USAGE] : formatFigtree.pl 0ListTrees -C TableTaxa/color -f font(optional - default Liberation Sans) -sb sizebranch(optional - default 8) -sl sizeleaves(optional - default 12) \n\n Open for file info\n\n" ; # Display information if no argument is entered unless( @ARGV ) { print $USAGE ; exit ; } # Variable initialization $font = "Liberation Sans"; $sizebranch = "10"; $sizeleaves = "12"; $ListTrees=$ARGV[0]; $TableTaxaColor=$ARGV[1]; # Initialization of optional variables for $i (1..10){ if ($ARGV[$i] eq "-C"){ $TableTaxaColor = $ARGV[$i+1];} if ($ARGV[$i] eq "-f"){ $font = $ARGV[$i+1];} if ($ARGV[$i] eq "-sb"){ $sizebranch = $ARGV[$i+1];} if ($ARGV[$i] eq "-sl"){ $sizeleaves = $ARGV[$i+1];} } # Read the taxa/pattern table and store it in a hash. open(tableTC,"<$TableTaxaColor")|| die ("Error opening input file ".$ARGV[1]) ; %hashcolor = (); while(){ @split_line = split(/\t/,$_); $taxa = $split_line[0]; $color = $split_line[1]; $hashcolor{$taxa} = $color; } open(listtrees,"<$ListTrees")|| die ("Error opening input file ".$ARGV[0]) ; # Read the list of files to process. while (){ $tree = $_; chomp($tree); $treeout = $tree.".fgcol"; open(tree, "<$tree")||die("Error opening input file ".$tree); open (out, ">$treeout"); # open an outfile for this particular tree print "tree: ".$tree." tree out: ".$treeout."\n"; @intree = (); $compt_sps = 0; @listspsC = (); while (){ $treenexus = $_; chomp($treenexus); @intree = split(/,/, $treenexus); # split the tree by line (split de la ligne d'arbre) $spsF = 0; $sps = 0; $spsC = 0; foreach $element(@intree){ # Search for leaf names by successive splits $element =~ s/\(//g; @split_element = split(/\:/,$element); $sps = $split_element[0]; chomp($sps); #($species) = split(/\:/,$element); $spsF = "\t".$sps."\n";; foreach $taxon (keys %hashcolor){ # Search for the taxa or speciess pattern, and add color if necessary. #print "taxa test: ".$taxon."\n"; if ($sps =~ m/$taxon/){ $color = $hashcolor{$taxon}; chomp($color); #$spsC = "\t".$sps.$color."\n"; #print species without single quotes $spsC = "\t\'".$sps."\'".$color."\n"; #print species in single quotes $spsF = $spsC; #print "line sps: ".$spsC." for the taxon: ".$taxon."\n"; } } push(@listspsC, $spsF); # Fill the table for writing the figtree file $compt_sps++; } # Write the figtree file, with header and footer. print out "#NEXUS\nbegin taxa;\n\tdimensions ntax=".$compt_sps.";\n\ttaxlabels\n"; foreach $spsC (@listspsC){ print out $spsC; } print out ";\nend;\n\nbegin trees;\n\ttree tree_1 = [&R] ".$treenexus."\nend;\n\nbegin figtree;\n"; print out "\tset appearance.backgroundColorAttribute=\"Default\";\n"; print out "\tset appearance.backgroundColour=#ffffff;\n"; print out "\tset appearance.branchColorAttribute=\"User selection\";\n"; print out "\tset appearance.branchColorGradient=false;\n"; print out "\tset appearance.branchLineWidth=1.0;\n"; print out "\tset appearance.branchMinLineWidth=0.0;\n"; print out "\tset appearance.branchWidthAttribute=\"Fixed\";\n"; print out "\tset appearance.foregroundColour=#000000;\n"; print out "\tset appearance.hilightingGradient=false;\n"; print out "\tset appearance.selectionColour=#2d3680;\n"; print out "\tset branchLabels.colorAttribute=\"User selection\";\n"; print out "\tset branchLabels.displayAttribute=\"label\";\n"; print out "\tset branchLabels.fontName=\"$font\";\n"; print out "\tset branchLabels.fontSize=$sizebranch;\n"; print out "\tset branchLabels.fontStyle=0;\n"; print out "\tset branchLabels.isShown=true;\n"; print out "\tset branchLabels.significantDigits=4;\n"; print out "\tset layout.expansion=0;\n"; print out "\tset layout.layoutType=\"RECTILINEAR\";\n"; print out "\tset layout.zoom=0;\n"; print out "\tset legend.attribute=\"label\";\n"; print out "\tset legend.fontSize=10.0;\n"; print out "\tset legend.isShown=false;\n"; print out "\tset legend.significantDigits=4;\n"; print out "\tset nodeBars.barWidth=4.0;\n"; print out "\tset nodeBars.displayAttribute=null;\n"; print out "\tset nodeBars.isShown=false;\n"; print out "\tset nodeLabels.colorAttribute=\"User selection\";\n"; print out "\tset nodeLabels.displayAttribute=\"label\";\n"; print out "\tset nodeLabels.fontName=\"$font\";\n"; print out "\tset nodeLabels.fontSize=$sizebranch;\n"; print out "\tset nodeLabels.fontStyle=0;\n"; print out "\tset nodeLabels.isShown=false;\n"; print out "\tset nodeLabels.significantDigits=4;\n"; print out "\tset nodeShape.colourAttribute=\"User selection\";\n"; print out "\tset nodeShape.isShown=false;\n"; print out "\tset nodeShape.minSize=10.0;\n"; print out "\tset nodeShape.scaleType=Width;\n"; print out "\tset nodeShape.shapeType=Circle;\n"; print out "\tset nodeShape.size=4.0;\n"; print out "\tset nodeShape.sizeAttribute=\"Fixed\";\n"; print out "\tset polarLayout.alignTipLabels=false;\n"; print out "\tset polarLayout.angularRange=0;\n"; print out "\tset polarLayout.rootAngle=0;\n"; print out "\tset polarLayout.rootLength=100;\n"; print out "\tset polarLayout.showRoot=true;\n"; print out "\tset radialLayout.spread=0.0;\n"; print out "\tset rectilinearLayout.alignTipLabels=false;\n"; print out "\tset rectilinearLayout.curvature=0;\n"; print out "\tset rectilinearLayout.rootLength=100;\n"; print out "\tset scale.offsetAge=0.0;\n"; print out "\tset scale.rootAge=1.0;\n"; print out "\tset scale.scaleFactor=1.0;\n"; print out "\tset scale.scaleRoot=false;\n"; print out "\tset scaleAxis.automaticScale=true;\n"; print out "\tset scaleAxis.fontSize=8.0;\n"; print out "\tset scaleAxis.isShown=false;\n"; print out "\tset scaleAxis.lineWidth=1.0;\n"; print out "\tset scaleAxis.majorTicks=1.0;\n"; print out "\tset scaleAxis.origin=0.0;\n"; print out "\tset scaleAxis.reverseAxis=false;\n"; print out "\tset scaleAxis.showGrid=true;\n"; print out "\tset scaleBar.automaticScale=true;\n"; print out "\tset scaleBar.fontSize=10.0;\n"; print out "\tset scaleBar.isShown=false;\n"; print out "\tset scaleBar.lineWidth=1.0;\n"; print out "\tset scaleBar.scaleRange=0.0;\n"; print out "\tset tipLabels.colorAttribute=\"User selection\";\n"; print out "\tset tipLabels.displayAttribute=\"Names\";\n"; print out "\tset tipLabels.fontName=\"$font\";\n"; print out "\tset tipLabels.fontSize=$sizeleaves;\n"; print out "\tset tipLabels.fontStyle=0;\n"; print out "\tset tipLabels.isShown=true;\n"; print out "\tset tipLabels.significantDigits=4;\n"; print out "\tset trees.order=true;\n"; print out "\tset trees.orderType=\"decreasing\";\n"; print out "\tset trees.rooting=false;\n"; print out "\tset trees.rootingType=\"User Selection\";\n"; print out "\tset trees.transform=false;\n"; print out "\tset trees.transformType=\"cladogram\";\n"; print out "end;\n" } close(tree); close (out); }