001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021
022package org.biojava.bio.seq.io;
023
024import java.io.PrintStream;
025import java.util.ArrayList;
026import java.util.List;
027
028import org.biojava.bio.BioError;
029import org.biojava.bio.BioException;
030import org.biojava.bio.seq.Feature;
031import org.biojava.bio.symbol.Alphabet;
032import org.biojava.bio.symbol.IllegalAlphabetException;
033import org.biojava.bio.symbol.IllegalSymbolException;
034import org.biojava.bio.symbol.Symbol;
035
036/**
037 * This class performs the detailed formatting of refseq protein entries.
038 * Functionality is essentially identical to GenbankFileFormer except that
039 * SimpleFeatures are created intead of StrandedFeatures
040 *
041 * @author Greg Cox
042 * @deprecated Use org.biojavax.bio.seq.io framework instead
043 */
044public class ProteinRefSeqFileFormer extends GenbankFileFormer
045{
046    // Constructors and initialization
047
048    protected ProteinRefSeqFileFormer()
049    {
050        super(System.out);
051    }
052
053    protected ProteinRefSeqFileFormer(PrintStream theStream)
054    {
055        super(theStream);
056    }
057
058    // Interface implementations
059    public void addSymbols(Alphabet  theAlphabet,
060                           Symbol [] theSymbols,
061                           int       theStart,
062                           int       theLength)
063        throws IllegalAlphabetException
064    {
065        // Get newline character
066        String newLine = System.getProperty("line.separator");
067        this.getPrintStream().print("ORIGIN" + newLine);
068        List brokenLines = this.breakSymbolArray(theAlphabet, theSymbols,
069                                                 theStart, theLength);
070
071        java.util.Iterator iterator = brokenLines.iterator();
072        String leader = "     ";
073        while(iterator.hasNext())
074        {
075            this.getPrintStream().print(leader + iterator.next() + newLine);
076        }
077        this.getPrintStream().println("//" + newLine);
078    }
079
080    public void startFeature(Feature.Template templ)
081        throws ParseException
082    {
083        // CAUTION: This hasn't been tested.  Use at your own risk.
084
085        // There are 21 spaces in the leader
086        String leader = "                     ";
087        int    strand = 0;
088
089        StringBuffer theBuffer = new StringBuffer();
090        formatLocationBlock(theBuffer, templ.location, strand, leader, 80);
091        theBuffer.replace(5, 5 + templ.type.length(), templ.type);
092        this.getPrintStream().println(theBuffer);
093    }
094
095    // Public methods
096
097    // Protected methods
098
099    /**
100     * Converts the symbol list passed in into an array of strings.  The
101     * strings will be blocks of ten, with six blocks on a line.
102     *
103     * @param theAlphabet The alphabet of the symbol data
104     * @param theSymbols An array containing symbols
105     * @param theStart The start offset of valid data within the array
106     * @param theLength The number of valid symbols in the array
107     * @return The symbol list passed in broken into blocks of ten
108     * characters, six to a string.
109     *
110     * @throws IllegalAlphabetException if we can't cope with this
111     *                                  alphabet.
112     */
113    protected List breakSymbolArray(Alphabet theAlphabet,
114                                    Symbol[] theSymbols,
115                                    int theStart,
116                                    int theLength)
117        throws IllegalAlphabetException
118    {
119        List returnList = new ArrayList(theLength / 60 + 1);
120        int blockCount = 0;
121        int blockIndex = 0;
122        StringBuffer tempString = new StringBuffer();
123        SymbolTokenization tokenization;
124        try {
125            tokenization = theAlphabet.getTokenization("token");
126        } catch (BioException ex) {
127            throw new BioError("Expected tokenization",ex);
128        }
129        for(int i = theStart; i < theStart + theLength; i++)
130        {
131            try
132            {
133                theAlphabet.validate(theSymbols[i]);
134            }
135            catch (IllegalSymbolException e)
136            {
137                throw new IllegalAlphabetException(e);
138            }
139
140            // Every six completed blocks, put on the stack to return
141            if(blockIndex == 10)
142            {
143                tempString.append(' ');
144                blockIndex = 0;
145                blockCount++;
146            }
147
148            if(blockCount == 6)
149            {
150                returnList.add(tempString.substring(0));
151                tempString.setLength(0);
152                blockCount = 0;
153                blockIndex = 0;
154            }
155
156            try
157            {
158                tempString.append(tokenization.tokenizeSymbol(theSymbols[i]));
159            }
160            catch (IllegalSymbolException e)
161            {
162                throw new IllegalAlphabetException(e);
163            }
164            blockIndex++;
165        }
166
167        // Add the last line on
168        if(tempString.length() != 0)
169        {
170            returnList.add(tempString.substring(0));
171        }
172        return returnList;
173    }
174}