001/*
002 * Copyright (c) 2009-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.camera.service;
031
032import java.io.BufferedReader;
033import java.io.FileReader;
034import java.io.IOException;
035import java.util.HashMap;
036import java.util.Map;
037
038import ptolemy.actor.TypedAtomicActor;
039import ptolemy.actor.TypedIOPort;
040import ptolemy.data.ObjectToken;
041import ptolemy.data.StringToken;
042import ptolemy.data.Token;
043import ptolemy.kernel.CompositeEntity;
044import ptolemy.kernel.util.IllegalActionException;
045import ptolemy.kernel.util.NameDuplicationException;
046//////////////////////////////////////////////////////////////////////////
047////IdSizeMapForSeqFile
048/**
049* It reads the sequence file in Fasta format for a 
050* meta genome sample and outputs the map with id
051* and sequence length.
052* 
053* @author Madhu, SDSC  madhu@sdsc.edu
054* @version $Id
055*/
056
057public class IdSizeMapForSeqFile extends TypedAtomicActor {
058        private static final long serialVersionUID = 1L;
059        
060        public TypedIOPort inputPort, outputPort;
061        public IdSizeMapForSeqFile(CompositeEntity container,  String name)
062        throws NameDuplicationException, IllegalActionException  {
063
064                super(container, name);
065                inputPort = new TypedIOPort(this, "inputPort", true, false);
066                outputPort = new TypedIOPort(this, "outputPort", false, true);
067
068        }
069        
070        /* (non-Javadoc)
071         * @see ptolemy.actor.AtomicActor#fire()
072         */
073        @Override
074        public void fire() throws IllegalActionException {
075            
076                super.fire();
077                
078                String  inputP  = null;
079            //In case no port is connected to the paramInputPort
080                Map<String, String> map = null;
081            if(inputPort.getWidth() > 0){
082                Token tkP = inputPort.get(0);
083                if(tkP != null){
084                        inputP=((StringToken)tkP).stringValue().trim();
085                }
086                
087                map = getInputGenomeFileMap(inputP);
088            }
089            
090            if(map != null && map.size() > 0){
091                outputPort.send(0, new ObjectToken(map));
092            }
093        }
094        
095        
096        /**
097         * @param fName is the meta genome sequence file in Fasta format
098         * @return map for seq id and its length for all the sequences
099         * in the meta genome sample file.
100         */
101        private Map<String, String> getInputGenomeFileMap(String fName){
102                
103                Map<String,String> map = new HashMap<String, String>();
104//              String genomeFileName = "C:"+ServiceUtils.FILESEP+"Camera"+ServiceUtils.FILESEP+"Ocean_Alaska.fa";
105                        
106                String valueRead = null;
107                String paramName = null;
108                String paramNameHolder = null;
109                StringBuilder paramValue = new StringBuilder();
110                boolean writeToMapCheck = false;
111                
112                try{
113                        //replaceFirst method for file Name is added because in case one is using FileParameter
114                        //then file:/ prefix may be added before the file name 
115                        //This comment added on 21 November, 2008
116                        
117//                      File afile = new File(System.getProperty("user.home") + ServiceUtils.FILESEP + "mapData");
118//                      if(!afile.exists()){
119//                              afile.createNewFile();
120//                      }
121//                  BufferedWriter out = new BufferedWriter(new FileWriter(afile));
122                                    
123                        BufferedReader br = null;
124                        if(ServiceUtils.OS.startsWith("Windows")){
125                                br = new BufferedReader(new FileReader(fName.replaceFirst("file:/", "")));
126                        }else{
127                                br = new BufferedReader(new FileReader(fName.replaceFirst("file:", "")));
128                        }
129                        while((valueRead = br.readLine())!= null){
130                                if(!ServiceUtils.checkEmptyString(valueRead)){
131                                        String valueReadTrim = valueRead.trim();
132                                        if(valueReadTrim.startsWith(ServiceUtils.GT)){
133                                                paramNameHolder = paramName;
134                                                writeToMapCheck = true; //first time it is true, but paramValue is empty
135                                                String[] fragements = valueReadTrim.split("/");
136                                                paramName = fragements[0].trim().replace(ServiceUtils.GT, ServiceUtils.NOSPACE);
137                                                
138                                        }else{
139                                                paramValue.append(valueReadTrim);
140                                        }
141                                        if(writeToMapCheck == true){
142                                                writeToMapCheck = false;                                                
143                                                if(!ServiceUtils.checkEmptyString(paramNameHolder)&& !ServiceUtils.checkEmptyString(paramValue.toString())){
144                                                        map.put(paramNameHolder, String.valueOf(paramValue.length()));
145//                                                      out.write("PARAM NAME: " + paramNameHolder + " VALUE: " + paramValue);
146//                                                      out.write(ServiceUtils.LINESEP);
147                                                        paramNameHolder = null;
148                                                        paramValue = new StringBuilder();
149                                                }
150                                        }
151                                        
152                                }
153                                                
154                                
155                        }
156                        map.put(paramName, String.valueOf(paramValue.length())); //Last value has to written outside the loop
157//                      Set<String> keys = map.keySet();
158//                      for(String myKey: keys){
159//                              out.write("KEY: " + myKey + " VALUE: " + map.get(myKey));
160//                              out.write(ServiceUtils.LINESEP);
161//                      }
162//                      out.close();
163                }catch (IOException ioe) {
164                        ioe.printStackTrace();
165                }catch(Exception e){                                            
166                        e.printStackTrace();
167                }
168//              System.out.println("Inside getInputGenomeFileMap");
169//              System.out.println("Genome fragments for input file: " + map.size());           
170                
171                return map;
172        }
173
174}