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.CipresIDL.api1.DataMatrix;
035import org.cipres.datatypes.PhyloDataset;
036
037import ptolemy.actor.TypedAtomicActor;
038import ptolemy.actor.TypedIOPort;
039import ptolemy.data.StringToken;
040import ptolemy.data.expr.Parameter;
041import ptolemy.data.type.BaseType;
042import ptolemy.kernel.CompositeEntity;
043import ptolemy.kernel.util.IllegalActionException;
044import ptolemy.kernel.util.NameDuplicationException;
045
046//////////////////////////////////////////////////////////////////////////
047////SubsetChooserActor
048/**
049 * This actor reads the input file, generates the entities list, facilitates the
050 * user to choose a subset of entities, and stores the selected entities into an
051 * output file.
052 * 
053 * @author Alex Borchers, Zhijie Guan
054 * @version $Id: SubsetChooserActor.java 24234 2010-05-06 05:21:26Z welker $
055 */
056
057public class SubsetChooserActor extends TypedAtomicActor {
058
059        /**
060         * Construct SubsetChooserActor source with the given container and name.
061         * 
062         * @param container
063         *            The container.
064         * @param name
065         *            The name of this actor.
066         * @exception IllegalActionException
067         *                If the entity cannot be contained by the proposed
068         *                container.
069         * @exception NameDuplicationException
070         *                If the container already has an actor with this name.
071         */
072
073        public SubsetChooserActor(CompositeEntity container, String name)
074                        throws NameDuplicationException, IllegalActionException {
075                super(container, name);
076
077                inputFileName = new TypedIOPort(this, "Subset Chooser Input File",
078                                true, false);
079                inputFileName.setTypeEquals(BaseType.STRING);
080
081                outputFileName = new TypedIOPort(this, "Selected entities file", false,
082                                true);
083                // Set the type constraint.
084                outputFileName.setTypeEquals(BaseType.STRING);
085
086                outputFileDefaultName = new Parameter(this,
087                                "Output File Path and Name", new StringToken(""));
088
089                _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" "
090                                + "width=\"60\" height=\"20\" " + "style=\"fill:white\"/>\n"
091                                + "</svg>\n");
092
093        }
094
095        // /////////////////////////////////////////////////////////////////
096        // // ports and parameters ////
097
098        /**
099         * The input file name is received from this port.
100         */
101        public TypedIOPort inputFileName = null;
102
103        /**
104         * The output file name is sent out through this port.
105         */
106        public TypedIOPort outputFileName = null;
107
108        /**
109         * The file name parameter, which defines the default output file name.
110         */
111        public Parameter outputFileDefaultName;
112
113        // /////////////////////////////////////////////////////////////////
114        // // functional variables ////
115
116        // /////////////////////////////////////////////////////////////////
117        // // public methods ////
118
119        /**
120         * Choose a subset from the displayed objects.
121         * 
122         * @exception IllegalActionException
123         *                If it is thrown by the send() method sending out the
124         *                token.
125         */
126        public void fire() throws IllegalActionException {
127                super.fire();
128
129                if (inputFileName.hasToken(0)) {
130                        try {
131
132                                PhyloDataset pdIn = new PhyloDataset();
133                                File inputNexusFile = new File(((StringToken) inputFileName
134                                                .get(0)).stringValue());
135                                pdIn.initialize(inputNexusFile);
136
137                                if (pdIn.getTaxaInfo() == null) {
138                                        throw new IllegalActionException(this,
139                                                        "There are no taxa to subset");
140                                }
141
142                                DisplayObject[] displayObjects = new DisplayObject[pdIn
143                                                .getTaxaInfo().length];
144                                for (int i = 0; i < pdIn.getTaxaInfo().length; i++) {
145                                        displayObjects[i] = new DisplayObject(new Integer(i), pdIn
146                                                        .getTaxaInfo()[i]);
147                                }
148
149                                SubsetChooser sc = new SubsetChooser(displayObjects);
150                                DisplayObject[] displayObjectsSubset = sc
151                                                .showSubsetChooserAsDialog();
152
153                                // create new PhyloDataset comprised of the subset and write it
154                                // to nexus file
155                                PhyloDataset pdOut;
156                                DataMatrix dmIn = pdIn.getDataMatrix();
157                                DataMatrix dmOut = new DataMatrix();
158                                String[] taxa = new String[displayObjectsSubset.length];
159                                short[][] matrix = new short[displayObjectsSubset.length][pdIn
160                                                .getDataMatrix().m_numCharacters];
161                                Integer jInt;
162                                int j = 0;
163                                for (int i = 0; i < displayObjectsSubset.length; i++) {
164                                        jInt = (Integer) displayObjectsSubset[i].getObject();
165                                        j = jInt.intValue();
166                                        taxa[i] = pdIn.getTaxaInfo()[j];
167                                        for (int k = 0; k < dmIn.m_matrix[0].length; k++) {
168                                                matrix[i][k] = dmIn.m_matrix[j][k];
169                                        }
170                                }
171
172                                dmOut.m_matrix = matrix;
173                                dmOut.m_symbols = dmIn.m_symbols;
174                                dmOut.m_charStateLookup = dmIn.m_charStateLookup;
175                                dmOut.m_datatype = dmIn.m_datatype;
176                                dmOut.m_numCharacters = dmIn.m_numCharacters;
177                                dmOut.m_numStates = dmIn.m_numStates;
178                                dmOut.m_charStateLookup = dmIn.m_charStateLookup;
179
180                                pdOut = new PhyloDataset(dmOut, pdIn.getTrees(), taxa);
181
182                                String outFileName = ((StringToken) outputFileDefaultName
183                                                .getToken()).stringValue();
184                                File outputNexusFile = new File(outFileName);
185
186                                pdOut.writeToNexus(outputNexusFile);
187                                outputFileName.send(0, new StringToken(outFileName));
188                        } catch (Exception e) {
189                                System.out.println("Exception on Subset Chooser: ");
190                                e.printStackTrace();
191                        }
192                }
193        }
194
195}