001package com.hfg.chem.format;
002
003
004import java.io.BufferedReader;
005import java.io.File;
006import java.io.FileReader;
007import java.io.IOException;
008import java.io.StringReader;
009import java.util.List;
010
011import com.hfg.chem.Molecule;
012import com.hfg.util.StringUtil;
013
014
015//------------------------------------------------------------------------------
016/**
017 Base class for readable chemistry formats.
018 <div>
019 @author J. Alex Taylor, hairyfatguy.com
020 </div>
021 */
022//------------------------------------------------------------------------------
023// com.hfg Library
024//
025// This library is free software; you can redistribute it and/or
026// modify it under the terms of the GNU Lesser General Public
027// License as published by the Free Software Foundation; either
028// version 2.1 of the License, or (at your option) any later version.
029//
030// This library is distributed in the hope that it will be useful,
031// but WITHOUT ANY WARRANTY; without even the implied warranty of
032// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
033// Lesser General Public License for more details.
034//
035// You should have received a copy of the GNU Lesser General Public
036// License along with this library; if not, write to the Free Software
037// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
038//
039// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
040// jataylor@hairyfatguy.com
041//------------------------------------------------------------------------------
042
043public abstract class ReadableChemFormatBase<T extends Molecule> implements ReadableChemFormat<T>
044{
045   private MoleculeFactory<T> mMolFactory;
046
047   //###########################################################################
048   // CONSTRUCTORS
049   //###########################################################################
050
051   //---------------------------------------------------------------------------
052   public ReadableChemFormatBase(MoleculeFactory<T> inMoleculeFactory)
053   {
054      mMolFactory = inMoleculeFactory;
055   }
056
057   //###########################################################################
058   // PUBLIC METHODS
059   //###########################################################################
060
061   //---------------------------------------------------------------------------
062   public void setBioSequenceFactory(MoleculeFactory<T> inValue)
063   {
064      mMolFactory = inValue;
065   }
066
067   //---------------------------------------------------------------------------
068   public MoleculeFactory<T> getMoleculeFactory()
069   {
070      return mMolFactory;
071   }
072
073
074   //---------------------------------------------------------------------------
075   public List<T> read(File inFile)
076         throws ChemIOException
077   {
078      try
079      {
080         BufferedMoleculeReader reader = new BufferedMoleculeReader(new BufferedReader(new FileReader(inFile)), this);
081
082         return reader.readAll();
083      }
084      catch (ChemIOException e)
085      {
086         throw e;
087      }
088      catch (Exception e)
089      {
090         throw new ChemIOException("Problem reading sequences from " + StringUtil.singleQuote(inFile.getPath()) + "!", e);
091      }
092   }
093
094   //---------------------------------------------------------------------------
095   public List<T> read(BufferedReader inReader)
096         throws ChemIOException
097   {
098      try
099      {
100         BufferedMoleculeReader reader = new BufferedMoleculeReader(inReader, this);
101
102         return reader.readAll();
103      }
104      catch (ChemIOException e)
105      {
106         throw e;
107      }
108      catch (Exception e)
109      {
110         throw new ChemIOException("Problem reading sequences!", e);
111      }
112   }
113
114   //---------------------------------------------------------------------------
115   public T readRecord(CharSequence inString)
116         throws ChemIOException
117   {
118      T seq = null;
119
120      BufferedReader reader = null;
121      try
122      {
123         reader = new BufferedReader(new StringReader(inString.toString()));
124         seq = readRecord(reader);
125      }
126      finally
127      {
128         if (reader != null)
129         {
130            try
131            {
132               reader.close();
133            }
134            catch (IOException e)
135            {
136               throw new ChemIOException(e);
137            }
138         }
139      }
140
141      return seq;
142   }
143
144}