#  Copyright (c) 1997-2009
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
#  http://www.polymake.de
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-------------------------------------------------------------------------------
#  $Project: polymake $$Id: splitstree.rules 9535 2010-03-05 17:13:17Z benmuell $

###########################################################################################
#
#  Object type to visualize with SplitsTree
#

package Visual::FiniteMetricSpace;

use Polymake::Struct (
   [ '@ISA' => 'Object' ],
   '$metric',
   [ '$taxa' => 'unify_labels(#%)', default => 'undef' ],
);

object TightSpan {

# @topic credits/splitstree
# SplitsTree4 is the leading application for computing evolutionary networks from molecular sequence data.
# Copyright by Daniel Huson and David Bryant.
# http://www.splitstree.org

# @category Visualization
# Visualize the splits of a finite metric space (that is, a planar image of a tight span).  Calls SplitsTree.
# @return Visual::FiniteMetricSpace

user_method VISUAL_SPLITS () {
   my $this=shift;
   my $M= new Visual::FiniteMetricSpace(Name => $this->name,
					metric => (convert_to<Float>($this->METRIC)),
					taxa => $this->TAXA);
   visualize($M);
}

}

package SplitsTree::Viewer;

# path to SplitsTree
custom $splitstree;

CONFIGURE {
   find_via_path($splitstree, "SplitsTree");
}

###########################################################################################
#
#  Static (file-based) implementation of the Viewer interface
#

use SplitsTree;
use BackgroundViewer;

use Polymake::Struct [ '@ISA' => 'SimpleViewer' ];

sub file_suffix { ".nex" }

sub command {
   my ($self, $filename)=@_;
   "$splitstree -i $filename";
}

# @category Visualization
# Use [[wiki:external_software#SplitsTree]] to show planar images of tight spans.
label splitstree

global_method splitstree.tight_span: draw(Visual::FiniteMetricSpace) {
   my ($self, $M)=@_;
   $self->graphics->metric=$M->metric;
   $self->graphics->taxa=$M->taxa;
}


package SplitsTree::File::Writer;
import Visual::FileWriter;

package application;

# @Category Visualization
# Call [[wiki:external_software#SplitsTree]] with the given visual objects.
#
# @param Visual::Object vis_obj one or more objects to display
#
# @option String File "filename" or "AUTO"
#  Only create a NEXUS format file, don't start the GUI.
#
#  The ''.nex'' suffix is automatically added to the file name.
#
#  Specify //AUTO// if you want the filename be automatically derived from the drawing title.
#
#  You can also use any expression allowed for the ''open'' function,
#  including "-" for terminal output, "&HANDLE" for an already opened file handle,
#  or "| program" for a pipe.

user_function splitstree(Visual::Object+, { File => undef }) {
   visualize_explicit(@_, "SplitsTree");
}


# Local Variables:
# c-basic-offset:3
# mode: perl
# End:
