001/*
002 * Copyright (c) 2005-2012 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: jianwu $'
006 * '$Date: 2013-12-04 00:30:59 +0000 (Wed, 04 Dec 2013) $' 
007 * '$Revision: 32555 $'
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 */
029package org.kepler.bio.actor;
030
031import java.util.ArrayList;
032import java.util.Collections;
033import java.util.Iterator;
034import java.util.List;
035
036import org.kepler.bio.util.BlastTabularResult;
037
038import ptolemy.actor.TypedAtomicActor;
039import ptolemy.actor.TypedIOPort;
040import ptolemy.actor.parameters.PortParameter;
041import ptolemy.data.IntToken;
042import ptolemy.data.StringToken;
043import ptolemy.data.type.BaseType;
044import ptolemy.kernel.CompositeEntity;
045import ptolemy.kernel.util.IllegalActionException;
046import ptolemy.kernel.util.NameDuplicationException;
047
048//////////////////////////////////////////////////////////////////////////
049////BlastTabularResultMerge
050
051/**
052* This actor will merge blast tabular result.
053* 
054* @author Jianwu Wang
055* @version $Id: BlastTabularResultMerge.java 32555 2013-12-04 00:30:59Z jianwu $
056*/
057public class BlastTabularResultMerge extends TypedAtomicActor {
058        
059        /**
060         * Construct a BlastTabularResultMerge 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        public BlastTabularResultMerge(CompositeEntity container, String name)
073                        throws NameDuplicationException, IllegalActionException {
074                super(container, name);
075
076                input = new TypedIOPort(this, "input", true, false);
077                input.setTypeEquals(BaseType.STRING);
078
079                output = new TypedIOPort(this, "output", false, true);
080                output.setTypeEquals(BaseType.STRING);
081
082                alignLimit = new PortParameter(this, "alignments limit for (B)");
083                alignLimit.setToken(new IntToken(250));
084                alignLimit.getPort().setTypeEquals(BaseType.INT);
085
086                _attachText("_iconDescription", "<svg>\n"
087                                + "<rect x=\"-25\" y=\"-20\" " + "width=\"50\" height=\"40\" "
088                                + "style=\"fill:white\"/>\n"
089                                + "<polygon points=\"-15,-10 -12,-10 -8,-14 -1,-14 3,-10"
090                                + " 15,-10 15,10, -15,10\" " + "style=\"fill:red\"/>\n"
091                                + "</svg>\n");
092        }
093
094        // /////////////////////////////////////////////////////////////////
095        // // ports and parameters ////
096
097        /**
098         * The input port, which includes blast tabular results. Each result is a separate line.
099         */
100        public TypedIOPort input = null;
101        /**
102         * The output port, which contains the new directory path.
103         */
104        public TypedIOPort output = null;
105        /**
106         * The parameter, which is an integer for alignments limits of one query sequence, which is the value of "-b" option for blast command.
107         */
108        public PortParameter alignLimit = null;
109
110        // /////////////////////////////////////////////////////////////////
111        // // public methods ////
112
113        /**
114         * Sort the blast tabular results from input port,
115         *  and truncate them if needed (defined by alignLimit parameter).
116         *  Send the results to output port.
117         * 
118         * @exception IllegalActionException
119         *                
120         */
121        public void fire() throws IllegalActionException {
122                super.fire();
123                
124                _input = ((StringToken)input.get(0)).stringValue();
125//              System.out.println("_input:" + _input);
126                
127                alignLimit.update();
128                _alignLimit = ((IntToken)alignLimit.getToken()).intValue();
129                
130                BlastTabularResult blastResult = null;
131                List<BlastTabularResult> blastResList = new ArrayList<BlastTabularResult>();
132                String[] lines = _input.split("\n");
133                for (String line : lines){
134//                      System.out.println("one line:" + line);
135                        if (!line.equals("")){
136                                blastResult = new BlastTabularResult(line);
137                                blastResList.add(blastResult);
138                        }
139                }
140                Collections.sort(blastResList);
141                Iterator blastIt = blastResList.iterator();
142                int index = 0;
143                _output = new StringBuffer();
144                //if the number exceed _alignLimit, it will not be sent out.
145                while (blastIt.hasNext() && index < _alignLimit){
146                        _output.append(blastIt.next());
147                        _output.append("\n");
148                        index++;
149                }
150                if (_output.length() == 0)
151                        output.send(0, new StringToken(""));
152                else{
153                        //the last "\n" need to be deleted since the delimiter will be added in FileDataSink actor.
154                        output.send(0, new StringToken(_output.substring(0, _output.length()-1)));
155                }
156        }
157
158        // /////////////////////////////////////////////////////////////////
159        // // private members ////
160
161        private int _alignLimit;
162        private String _input;
163        private StringBuffer _output;
164
165}