001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.cipres.kepler;
031
032import java.io.File;
033
034import org.cipres.guigen.ServiceCommandPanel;
035import org.cipres.helpers.GUIRun;
036import org.cipres.kepler.registry.ActorInfo;
037import org.cipres.kepler.registry.ActorIterator;
038import org.cipres.kepler.registry.CipresKeplerRegistry;
039import org.cipres.kepler.registry.Globals;
040import org.cipres.util.file.cipresFile;
041
042import ptolemy.data.IntToken;
043import ptolemy.data.StringToken;
044import ptolemy.data.expr.Parameter;
045import ptolemy.kernel.CompositeEntity;
046import ptolemy.kernel.util.IllegalActionException;
047import ptolemy.kernel.util.NameDuplicationException;
048
049//////////////////////////////////////////////////////////////////////////
050////PAUPInfer
051/**
052 * This actor calls PAUP for tree inference using parsimony.
053 * 
054 * @author Zhijie Guan, Alex Borchers
055 * @version $Id: PAUPInfer.java 24234 2010-05-06 05:21:26Z welker $
056 */
057
058public class PAUPInfer extends GUIRunCIPRes {
059
060        /**
061         * Construct PAUPInfer source with the given container and name.
062         * 
063         * @param container
064         *            The container.
065         * @param name
066         *            The name of this actor.
067         * @exception IllegalActionException
068         *                If the entity cannot be contained by the proposed
069         *                container.
070         * @exception NameDuplicationException
071         *                If the container already has an actor with this name.
072         */
073
074        public PAUPInfer(CompositeEntity container, String name)
075                        throws NameDuplicationException, IllegalActionException {
076                super(container, name);
077
078                // get program information
079                CipresKeplerRegistry registry = Globals.getInstance().getRegistry();
080                _paupActor = registry.getActor("InferTreeByParsimony_Paup");
081
082                // set program information to parameters
083                command.setToken(new StringToken(_paupActor.getAppPathForOS()));
084                uiXMLFile.setToken(new StringToken(_paupActor.getGuiXmlFile()));
085                outputFile.setToken(new StringToken(registry.getDefaultStdOutDir()
086                                + "PAUPInferOut.log"));
087                errorFile.setToken(new StringToken(registry.getDefaultStdOutDir()
088                                + "PAUPInferError.log"));
089                workingDirectory.setToken(new StringToken(_paupActor
090                                .getWorkingDirecotry()));
091                parameterForOutput.setToken(new StringToken("outfile"));
092                paupCmdFile = new Parameter(this, "paupCmdFile", new StringToken(
093                                registry.getDefaultStdOutDir() + "paup_infer_cmds.nex"));
094                paupCmdFile.setDisplayName("PAUP Command File Name");
095
096                // iterations information
097                iterations = new Parameter(this, "iterations", new IntToken(1));
098                iterations.setDisplayName("Iteration Times");
099        }
100
101        // /////////////////////////////////////////////////////////////////
102        // // ports and parameters ////
103
104        /**
105         * The parameter for PAUP command file. This parameter will be set to
106         * String.
107         */
108        public Parameter paupCmdFile;
109
110        /**
111         * The iteration times of the program execution is set in this parameter.
112         */
113        public Parameter iterations;
114
115        // /////////////////////////////////////////////////////////////////
116        // // functional variables ////
117
118        // /////////////////////////////////////////////////////////////////
119        // // public methods ////
120
121        /**
122         * Run PAUP for tree inference.
123         * 
124         * @exception IllegalActionException
125         *                If it is thrown by the send() method sending out the
126         *                token.
127         */
128        public void fire() throws IllegalActionException {
129
130                ActorIterator actorIterator = null;
131                if (inputParameterValue.hasToken(0) && inputParameterName.hasToken(0)) {
132                        try {
133                                GUIRun grun = new GUIRun("PAUP Tree Inference");
134
135                                // get parameter values
136                                String commandFileName = ((StringToken) command.getToken())
137                                                .stringValue();
138                                if (commandFileName.length() != 0) {
139                                        grun.setCommand(commandFileName);
140                                } else {
141                                        System.out.println("Command is not defined!");
142                                        throw new IllegalActionException("Command is not defined");
143                                }
144
145                                String outFileName = ((StringToken) outputFile.getToken())
146                                                .stringValue();
147                                if (outFileName.length() != 0) {
148                                        grun.setOutputFileName(outFileName);
149                                }
150
151                                String errorFileName = ((StringToken) errorFile.getToken())
152                                                .stringValue();
153                                if (errorFileName.length() != 0) {
154                                        grun.setErrorFileName(errorFileName);
155                                }
156
157                                String workingDirName = ((StringToken) workingDirectory
158                                                .getToken()).stringValue();
159                                if (workingDirName.length() != 0) {
160                                        grun.setWorkingDirectory(workingDirName);
161                                }
162
163                                String uiXMLFileName = ((StringToken) uiXMLFile.getToken())
164                                                .stringValue();
165                                if (uiXMLFileName.length() != 0) {
166                                        actorIterator = new ActorIterator(new File(uiXMLFileName),
167                                                        ((IntToken) iterations.getToken()).intValue());
168                                }
169
170                                String outputParameterName = ((StringToken) parameterForOutput
171                                                .getToken()).stringValue();
172                                if (outputParameterName.length() == 0) {
173                                        outputParameterName = "outfile";
174                                }
175
176                                String paupCmdFileName = ((StringToken) paupCmdFile.getToken())
177                                                .stringValue();
178                                if (paupCmdFileName.length() == 0) {
179                                        paupCmdFileName = registry.getDefaultStdOutDir()
180                                                        + "paup_infer_cmds.nex";
181                                }
182
183                                // set the GUIGen interface for iterative execution
184                                actorIterator
185                                                .setParameterValueInAll(
186                                                                ((StringToken) inputParameterName.get(0))
187                                                                                .stringValue(),
188                                                                ((StringToken) inputParameterValue.get(0))
189                                                                                .stringValue());
190
191                                // collect the execution configuration for this program
192                                actorIterator.setOutfileParamInAll(_paupActor.actorName);
193                                actorIterator.show();
194                                ServiceCommandPanel[] pnls = actorIterator
195                                                .getServiceCommandPanels();
196
197                                if (pnls != null) { // null returned if user clicks 'Cancel'
198                                        // run paup_infer on each set of commands and send to
199                                        // forester to viz tree(s)
200                                        for (int i = 0; i < pnls.length; i++) {
201                                                // outfile is the file that user assigned for PAUP
202                                                // output
203                                                File outfile = new File(pnls[i]
204                                                                .getParameterValue(outputParameterName));
205                                                // paupApp indicates where PAUP is
206                                                File paupApp = new File(_paupActor.getAppPathForOS());
207                                                // paupOut is the real output that is generated by PAUP.
208                                                // It consists of the directory PAUP is in,
209                                                // plus the dir seperator, plus the outfile name
210                                                File paupOut = new File(paupApp.getParent()
211                                                                + Globals.getInstance().getDirSep()
212                                                                + outfile.getName());
213
214                                                String commands = pnls[i].getCmdBlock()
215                                                                + "\n EXPORT File=" + paupOut.getName()
216                                                                + " FORMAT=NEXUS REPLACE=YES;\n"
217                                                                + " SAVETREES File=" + paupOut.getName()
218                                                                + " FORMAT=NEXUS BRLENS=YES MAXDECIMALS=2"
219                                                                + " APPEND = YES;";
220
221                                                cipresFile cmdFile = new cipresFile(paupCmdFileName);
222                                                cmdFile.fillFromString("#NEXUS\nBEGIN PAUP;\n"
223                                                                + commands + " QUIT;\nEND;");
224
225                                                grun.setArguments(paupCmdFileName);
226
227                                                grun.setWaitForExecution(true);
228
229                                                grun.execute();
230
231                                                // now move outfile to its proper location
232                                                if (!outfile.getAbsolutePath().equalsIgnoreCase(
233                                                                paupOut.getAbsolutePath())) {
234                                                        paupOut.renameTo(outfile);
235                                                }
236
237                                                exitCode.send(0, new IntToken(grun.getExitCode()));
238
239                                                standardOutput.send(0, new StringToken(grun
240                                                                .getStandardOutput()));
241                                                standardError.send(0, new StringToken(grun
242                                                                .getStandardError()));
243
244                                                outputParameterValue.send(0, new StringToken(outfile
245                                                                .getAbsolutePath()));
246                                        }
247                                }
248
249                        } catch (Exception e) {
250                                e.printStackTrace();
251                        }
252                }
253        }
254
255        /**
256         * Post fire the actor. Return false to indicated that the process has
257         * finished. If it returns true, the process will continue indefinitely.
258         */
259        public boolean postfire() {
260                return false;
261        }
262
263        /**
264         * The instance of Cipres-Kepler registry
265         */
266        private CipresKeplerRegistry registry = Globals.getInstance().getRegistry();
267
268        /**
269         * private variables _paupActor records all the information for program
270         * PAUP.
271         */
272        private ActorInfo _paupActor;
273
274}