001package com.hfg.bio.seq.format; 002 003import com.hfg.bio.seq.BioSequence; 004import com.hfg.bio.seq.BioSequenceFactory; 005import com.hfg.util.StringUtil; 006 007import java.io.BufferedReader; 008import java.io.File; 009import java.io.FileReader; 010import java.io.IOException; 011import java.io.StringReader; 012import java.util.List; 013 014 015//------------------------------------------------------------------------------ 016/** 017 Base class for readable sequence 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 ReadableSeqFormatBase<T extends BioSequence> implements ReadableSeqFormat<T> 044{ 045 private BioSequenceFactory<T> mSeqFactory; 046 047 //########################################################################### 048 // CONSTRUCTORS 049 //########################################################################### 050 051 //--------------------------------------------------------------------------- 052 public ReadableSeqFormatBase(BioSequenceFactory<T> inSeqFactory) 053 { 054 mSeqFactory = inSeqFactory; 055 } 056 057 //########################################################################### 058 // PUBLIC METHODS 059 //########################################################################### 060 061 //--------------------------------------------------------------------------- 062 public BioSequenceFactory<T> getBioSequenceFactory() 063 { 064 return mSeqFactory; 065 } 066 067 068 //--------------------------------------------------------------------------- 069 public List<T> read(File inFile) 070 throws SeqIOException 071 { 072 try 073 { 074 BufferedSeqReader reader = new BufferedSeqReader(new BufferedReader(new FileReader(inFile)), this); 075 076 return reader.readAll(); 077 } 078 catch (SeqIOException e) 079 { 080 throw e; 081 } 082 catch (Exception e) 083 { 084 throw new SeqIOException("Problem reading sequences from " + StringUtil.singleQuote(inFile.getPath()) + "!", e); 085 } 086 } 087 088 //--------------------------------------------------------------------------- 089 public List<T> read(BufferedReader inReader) 090 throws SeqIOException 091 { 092 try 093 { 094 BufferedSeqReader reader = new BufferedSeqReader(inReader, this); 095 096 return reader.readAll(); 097 } 098 catch (SeqIOException e) 099 { 100 throw e; 101 } 102 catch (Exception e) 103 { 104 throw new SeqIOException("Problem reading sequences!", e); 105 } 106 } 107 108 //--------------------------------------------------------------------------- 109 public T readRecord(CharSequence inString) 110 throws SeqIOException 111 { 112 T seq = null; 113 114 BufferedReader reader = null; 115 try 116 { 117 reader = new BufferedReader(new StringReader(inString.toString())); 118 seq = readRecord(reader); 119 } 120 finally 121 { 122 if (reader != null) 123 { 124 try 125 { 126 reader.close(); 127 } 128 catch (IOException e) 129 { 130 throw new SeqIOException(e); 131 } 132 } 133 } 134 135 return seq; 136 } 137 138}