/* PAUPInfer actor calls PAUP for tree inference. * * Copyright (c) 2004 The Regents of the University of California. * All rights reserved. * * Permission is hereby granted, without written agreement and without * license or royalty fees, to use, copy, modify, and distribute this * software and its documentation for any purpose, provided that the * above copyright notice and the following two paragraphs appear in * all copies of this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN * IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY * OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. * * PT_COPYRIGHT_VERSION_2 * COPYRIGHTENDKEY */ package org.cipres.kepler; import java.io.File; import ptolemy.kernel.CompositeEntity; import ptolemy.kernel.util.*; import ptolemy.data.IntToken; import ptolemy.data.BooleanToken; import ptolemy.data.StringToken; import ptolemy.data.expr.Parameter; import org.cipres.guigen.ServiceCommandPanel; import org.cipres.helpers.GUIRun; import org.cipres.kepler.registry.ActorInfo; import org.cipres.kepler.registry.ActorIterator; import org.cipres.kepler.registry.CipresKeplerRegistry; import org.cipres.kepler.registry.Globals; import org.cipres.util.CipresFile; ////////////////////////////////////////////////////////////////////////// ////PAUPInfer /** * This actor calls PAUP for tree inference using parsimony. * * @author Zhijie Guan, Alex Borchers * @version $Id: $ */ public class PAUPInfer extends GUIRunCIPRes { // TODO: assign a proper hash value to this static final long serialVersionUID = 1L; /** Construct PAUPInfer source with the given container and name. * @param container The container. * @param name The name of this actor. * @exception IllegalActionException If the entity cannot be contained * by the proposed container. * @exception NameDuplicationException If the container already has an * actor with this name. */ public PAUPInfer(CompositeEntity container, String name) throws NameDuplicationException, IllegalActionException { super(container, name); // get program information CipresKeplerRegistry registry = Globals.getInstance().getRegistry(); _paupActor = registry.getActor("InferTreeByParsimony_Paup"); // set program information to parameters command.setToken( new StringToken(_paupActor.getAppPath()) ); uiXMLFile.setToken( new StringToken(_paupActor.getGuiXmlFile()) ); outputFile.setToken( new StringToken(registry.getDefaultStdOutDir() + "PAUPInferOut.log") ); errorFile.setToken( new StringToken(registry.getDefaultStdOutDir() + "PAUPInferError.log") ); workingDirectory.setToken( new StringToken(_paupActor.getWorkingDir()) ); parameterForOutput.setToken( new StringToken("outfile") ); paupCmdFile = new Parameter(this, "paupCmdFile", new StringToken("")); paupCmdFile.setToken(new StringToken(registry.getDefaultStdOutDir() + "paup_infer_cmds.nex")); paupCmdFile.setDisplayName("PAUP Command File Name"); paupCmdFile.setVisibility(Settable.EXPERT); // iterations information iterations = new Parameter(this, "iterations", new IntToken(1)); iterations.setDisplayName("Iteration Times"); // set ports "importance" inputParameterName.setRequired(true); inputParameterValue.setRequired(true); outputParameterValue.setUseful(true); // set inputParameterName, inputParameterName and outputParameterValue names display new Parameter(inputParameterName, "_showName", BooleanToken.TRUE); new Parameter(inputParameterValue, "_showName", BooleanToken.TRUE); new Parameter(outputParameterValue, "_showName", BooleanToken.TRUE); } /////////////////////////////////////////////////////////////////// //// ports and parameters //// /** The parameter for PAUP command file. This parameter will be set to String. */ public Parameter paupCmdFile; /** * The iteration times of the program execution is set in this parameter. */ public Parameter iterations; /////////////////////////////////////////////////////////////////// //// functional variables //// /////////////////////////////////////////////////////////////////// //// public methods //// /** Run PAUP for tree inference. * @exception IllegalActionException If it is thrown by the * send() method sending out the token. */ public void fire() throws IllegalActionException { CipresKeplerRegistry registry = Globals.getInstance().getRegistry(); ActorIterator actorIterator = null; if ( inputParameterValue.hasToken(0) && inputParameterName.hasToken(0) ) { try { GUIRun grun = new GUIRun("PAUP Tree Inference"); // get parameter values String commandFileName = ((StringToken)command.getToken()).stringValue(); if (commandFileName.length() != 0 ) { grun.setExecutable( commandFileName); } else { System.out.println("Command is not defined!"); throw new IllegalActionException("Command is not defined"); } String outFileName = ((StringToken)outputFile.getToken()).stringValue(); if (outFileName.length() != 0) { grun.setOutputFilename( outFileName ); } String errorFileName = ((StringToken)errorFile.getToken()).stringValue(); if (errorFileName.length() != 0) { grun.setErrorFilename( errorFileName ); } String workingDirName = ((StringToken)workingDirectory.getToken()).stringValue(); if (workingDirName.length() != 0 ) { grun.setWorkingDirPath( workingDirName ); } String uiXMLFileName = ((StringToken)uiXMLFile.getToken()).stringValue(); if (uiXMLFileName.length() != 0) { actorIterator = new ActorIterator(new File(uiXMLFileName),((IntToken)iterations.getToken()).intValue()); } String outputParameterName = ((StringToken)parameterForOutput.getToken()).stringValue(); if (outputParameterName.length() == 0) { outputParameterName = "outfile"; } String paupCmdFileName = ((StringToken)paupCmdFile.getToken()).stringValue(); if (paupCmdFileName.length() == 0) { paupCmdFileName = registry.getDefaultStdOutDir() + "paup_infer_cmds.nex"; } // set the GUIGen interface for iterative execution actorIterator.setParameterValueInAll(((StringToken)inputParameterName.get(0)).stringValue(), ((StringToken)inputParameterValue.get(0)).stringValue()); // collect the execution configuration for this program actorIterator.setOutfileParamInAll(_paupActor.getActorName()); actorIterator.setVisible(true); ServiceCommandPanel[] pnls = actorIterator.getServiceCommandPanels(); if(pnls!=null) { //null returned if user clicks 'Cancel' //run paup_infer on each set of commands and send to forester to viz tree(s) for (int i = 0; i < pnls.length; i++) { // outfile is the file that user assigned for PAUP output File outfile = new File(pnls[i].getParameterValue(outputParameterName)); String commands = pnls[i].getCmdBlock() + "\n EXPORT File=" + outfile.getAbsolutePath() + " FORMAT=NEXUS REPLACE=YES;\n" + " SAVETREES File=" + outfile.getAbsolutePath() + " FORMAT=NEXUS BRLENS=YES MAXDECIMALS=2" + " APPEND = YES;"; CipresFile cmdFile = new CipresFile(paupCmdFileName); cmdFile.fillFromString("#NEXUS\nBEGIN PAUP;\n" + commands + " QUIT;\nEND;"); String[] grunArgs = { paupCmdFileName }; grun.setArguments(grunArgs); grun.setWaitForExecution(true); grun.execute(); exitCode.send(0, new IntToken(grun.getExitCode())); standardOutput.send(0, new StringToken(grun.getStandardOutput())); standardError.send(0, new StringToken(grun.getStandardError())); outputParameterValue.send(0, new StringToken( outfile.getAbsolutePath() ) ); } } } catch (Exception e) { e.printStackTrace(); } } } /** Post fire the actor. Return false to indicated that the * process has finished. If it returns true, the process will * continue indefinitely. */ public boolean postfire() { return false; } /** * The instance of Cipres-Kepler registry */ // TODO: this is never read locally //private CipresKeplerRegistry registry = Globals.getInstance().getRegistry(); /** private variables * _paupActor records all the information for program PAUP. */ private ActorInfo _paupActor; }