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.kepler.registry.ActorInfo;
035import org.cipres.kepler.registry.CipresKeplerRegistry;
036import org.cipres.kepler.registry.Globals;
037import org.cipres.util.file.cipresFile;
038
039import ptolemy.data.StringToken;
040import ptolemy.kernel.CompositeEntity;
041import ptolemy.kernel.util.IllegalActionException;
042import ptolemy.kernel.util.NameDuplicationException;
043
044//////////////////////////////////////////////////////////////////////////
045////ClustalWSeqAlign
046/**
047 * This actor calls ClustalW for Sequence Alignment. The Cipres-Kepler registry
048 * is used to provide the application information for ClustalW. After setting
049 * all the application related information, such as command name, GUIGen XML
050 * file, and standard output/error files, this actor invokes ClustalW and
051 * retrieves back the alignment result after the execution.
052 * 
053 * This actor inherits GUIRunCIPRes actor since basically it is a customized
054 * GUIRunCIPRes actor to call external Cipres programs.
055 * 
056 * @author Zhijie Guan, Alex Borchers
057 * @version $Id: ClustalWSeqAlign.java 24234 2010-05-06 05:21:26Z welker $
058 */
059
060public class ClustalWSeqAlign extends GUIRunCIPRes {
061
062        /**
063         * Construct ClustalWSeqAlign source with the given container and name.
064         * 
065         * @param container
066         *            The container.
067         * @param name
068         *            The name of this actor.
069         * @exception IllegalActionException
070         *                If the entity cannot be contained by the proposed
071         *                container.
072         * @exception NameDuplicationException
073         *                If the container already has an actor with this name.
074         */
075        public ClustalWSeqAlign(CompositeEntity container, String name)
076                        throws NameDuplicationException, IllegalActionException {
077                super(container, name);
078
079                // get the application information from the Cipers-Kepler registry
080                CipresKeplerRegistry registry = Globals.getInstance().getRegistry();
081                _clustalWActor = registry.getActor("SequenceAlign_Clustal");
082
083                // set the application information into the parameters
084                command.setToken(new StringToken(_clustalWActor.getAppPathForOS()));
085                uiXMLFile.setToken(new StringToken(_clustalWActor.getGuiXmlFile()));
086                outputFile.setToken(new StringToken(registry.getDefaultStdOutDir()
087                                + "ClustalWOut.log"));
088                errorFile.setToken(new StringToken(registry.getDefaultStdOutDir()
089                                + "ClustalWError.log"));
090                workingDirectory.setToken(new StringToken(_clustalWActor
091                                .getWorkingDirecotry()));
092                parameterForOutput.setToken(new StringToken("outfile"));
093        }
094
095        // /////////////////////////////////////////////////////////////////
096        // // ports and parameters ////
097
098        // /////////////////////////////////////////////////////////////////
099        // // functional variables ////
100
101        // /////////////////////////////////////////////////////////////////
102        // // public methods ////
103
104        /**
105         * Run ClustalW for Sequence Alignment.
106         * 
107         * @exception IllegalActionException
108         *                If it is thrown by the send() method sending out the
109         *                token.
110         */
111        public void fire() throws IllegalActionException {
112                super.fire();
113        }
114
115        /**
116         * After the sequence alignment with ClustalW, the output file usually does
117         * not comply with the standard Nexus file format. Thus, a post execution
118         * process is need to fix the file format of the ClustalW output. This
119         * function reads in the ClustalW output file, finds the symbol lists that
120         * does not comply with the Nexus format, and removes the symbol list from
121         * the output file.
122         */
123        public void postExecutionProcess(String outputFileName) {
124                // Fix the ClustalW output
125                File inFile = new File(outputFileName);
126                try {
127                        cipresFile cfIn = new cipresFile(inFile.getAbsolutePath());
128                        // put the fixed output in a temp file
129                        cipresFile cfOut = new cipresFile(Globals.getInstance()
130                                        .getTempDir()
131                                        + "ClustalWTempOutput.tmp");
132                        String content = cfIn.getContent();
133                        // find beginnning of symbols statement
134                        int pos1 = content.toLowerCase().indexOf("symbols");
135                        // find end of symbols statement (will be the 2nd quotation mark
136                        // after "symbols")
137                        int pos2 = content.indexOf("\"", pos1);
138                        pos2 = content.indexOf("\"", pos2 + 1);
139                        String symbolsString = content.substring(pos1, pos2);
140                        cfOut.fillFromString(content.substring(0, pos1)
141                                        + content.substring(pos1 + symbolsString.length() + 1,
142                                                        content.length()));
143                        // rename the fixed output file to the original ClustalW output file
144                        cfOut.renameTo(cfIn);
145                } catch (Exception ex) {
146                        System.out
147                                        .println("Exception on fixing ClustalW's output Nexus file: ");
148                        ex.printStackTrace();
149                }
150        }
151
152        /**
153         * Post fire the actor. Return false to indicated that the process has
154         * finished. If it returns true, the process will continue indefinitely.
155         */
156        public boolean postfire() {
157                return false;
158        }
159
160        /**
161         * private variables _clustalWActor is an application information record
162         * that stores all the ClustalW-related information.
163         */
164        private ActorInfo _clustalWActor;
165
166}