001/*
002 * Copyright (c) 2007-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.sdm.spa;
031
032import java.io.BufferedReader;
033import java.io.File;
034import java.io.IOException;
035
036import ptolemy.actor.TypedAtomicActor;
037import ptolemy.actor.TypedIOPort;
038import ptolemy.actor.parameters.FilePortParameter;
039import ptolemy.data.IntToken;
040import ptolemy.data.type.BaseType;
041import ptolemy.kernel.CompositeEntity;
042import ptolemy.kernel.util.IllegalActionException;
043import ptolemy.kernel.util.NameDuplicationException;
044
045//////////////////////////////////////////////////////////////////////////
046//// FASTACounter
047
048/**
049 * On each firing, output the number of FASTA entries in a file.
050 * 
051 * @author Daniel Crawl
052 * @version $Id: FASTACounter.java 24234 2010-05-06 05:21:26Z welker $
053 */
054
055public class FASTACounter extends TypedAtomicActor {
056
057        /**
058         * Construct a FASTACounter source with the given container and name.
059         * 
060         * @param name
061         *            The name of this actor.
062         * @exception IllegalActionException
063         *                If the entity cannot be contained by the proposed
064         *                container.
065         * @exception NameDuplicationException
066         *                If the container already has an actor with this name.
067         */
068        public FASTACounter(CompositeEntity container, String name)
069                        throws NameDuplicationException, IllegalActionException {
070                super(container, name);
071
072                filename = new FilePortParameter(this, "filename");
073                filename.setTypeEquals(BaseType.STRING);
074
075                count = new TypedIOPort(this, "count", false, true);
076                count.setTypeEquals(BaseType.INT);
077
078                _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" "
079                                + "width=\"60\" height=\"20\" " + "style=\"fill:white\"/>\n"
080                                + "</svg>\n");
081        }
082
083        // /////////////////////////////////////////////////////////////////
084        // // ports and parameters ////
085
086        /** The FASTA file name. */
087        public FilePortParameter filename = null;
088
089        /** The number of FASTA entries. */
090        public TypedIOPort count = null;
091
092        // /////////////////////////////////////////////////////////////////
093        // // public methods ////
094
095        /** Count the number of FASTA entries. */
096        public void fire() throws IllegalActionException {
097                super.fire();
098
099                filename.update();
100
101                File file = filename.asFile();
102                if (!file.isFile()) {
103                        throw new IllegalActionException(this, filename.stringValue()
104                                        + " is not a file.");
105                }
106
107                BufferedReader br = filename.openForReading();
108                int num = 0;
109                String line;
110
111                try {
112                        while ((line = br.readLine()) != null) {
113                                // a FASTA entry must begin with ">".
114                                // http://www.ncbi.nlm.nih.gov/BLAST/fasta.shtml
115                                if (line.length() > 0 && line.charAt(0) == '>') {
116                                        num++;
117                                }
118                        }
119                } catch (IOException e) {
120                        throw new IllegalActionException(this, "IOException: "
121                                        + e.getMessage());
122                }
123
124                count.broadcast(new IntToken(num));
125        }
126}