<commit_before>/*
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 */

package	org.biojava.bio.seq.io;

import java.io.*;
import java.util.*;

import org.biojava.utils.*;
import org.biojava.bio.*;
import org.biojava.bio.symbol.*;
import org.biojava.bio.seq.*;

/**
 * Format reader for GenBank files. Converted from the old style io to the
 * new by working from <code>EmblLikeFormat</code>
 *
 * @author <NAME>
 * @author <NAME>
 * Added GenBank header	info to	the sequence annotation. The ACCESSION header
 * tag is not included.	Stored in sequence.getName().
 * @author <NAME>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */

public class GenbankFormat
        implements SequenceFormat,
                   Serializable,
                   org.biojava.utils.ParseErrorListener,
                   org.biojava.utils.ParseErrorSource
{
    static
    {
	Set validFormats = new HashSet();
	validFormats.add("Genbank");

	SequenceFormat.FORMATS.put(GenbankFormat.class.getName(),
				   validFormats);
    }

    protected static final String END_SEQUENCE_TAG = "//";
    protected static final String FEATURE_TAG = "FEATURES";
    protected static final String START_SEQUENCE_TAG = "ORIGIN";
    protected static final String FEATURE_LINE_PREFIX = "     ";
    protected static final String FEATURE_FLAG = "FT";
    protected static final String ACCESSION_TAG = "ACCESSION";
    protected static final String LOCUS_TAG = "LOCUS";
    protected static final String SIZE_TAG = "SIZE";
    protected static final String TYPE_TAG = "TYPE";
    protected static final String STRAND_NUMBER_TAG = "STRANDS";
    protected static final String CIRCULAR_TAG = "CIRCULAR";
    protected static final String DIVISION_TAG = "DIVISION";
    protected static final String DATE_TAG = "MDAT";
    protected static final String VERSION_TAG = "VERSION";
    protected static final String GI_TAG = "GI";

    private Vector mListeners = new Vector();

    /**
     * Reads a sequence from the specified reader using the Symbol
     * parser and Sequence Factory provided. The sequence read in must
     * be in Genbank format.
     *
     * @return boolean True if there is another sequence in the file; false
     * otherwise
     */
    public boolean readSequence(BufferedReader reader,
				SymbolParser symParser,
				SeqIOListener listener)
	throws IllegalSymbolException, IOException, ParseException
    {
	GenbankContext ctx = new GenbankContext(symParser, listener);
    ctx.addParseErrorListener(this);
	String line;
	boolean hasAnotherSequence    = true;
	boolean hasInternalWhitespace = false;

	listener.startSequence();

	while ((line = reader.readLine()) != null)
	{
  	    if (line.startsWith(END_SEQUENCE_TAG))
  	    {
		// To close the StreamParser encapsulated in the
		// GenbankContext object
		ctx.processLine(line);

		// Allows us to tolerate trailing whitespace without
		// thinking that there is another Sequence to follow
		char [] cbuf = new char [1];

		while (true)
		{
		    reader.mark(1);

		    if (reader.read() == -1)
		    {
			hasAnotherSequence = false;
			break;
		    }

		    reader.read(cbuf, 0, 1);

		    if (Character.isWhitespace(cbuf[0]))
		    {
			hasInternalWhitespace = true;
			continue;
		    }
		    else
		    {
			if (hasInternalWhitespace)
			    System.err.println("Warning: whitespace found between sequence entries");
			reader.reset();
			break;
		    }
		}

		listener.endSequence();
		return hasAnotherSequence;
	    }
	    ctx.processLine(line);
	}

	throw new IOException("Premature end of stream for GENBANK");
    }

    public void	writeSequence(Sequence seq, PrintStream os)
	throws IOException
    {
	String defaultFormat = getDefaultFormat();

	try
	{
	    SeqFileFormer former = SeqFileFormerFactory.makeFormer(defaultFormat);
	    former.setPrintStream(os);<commit_msg>Added <NAME>'s reader patch<commit_after>/*
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 */

package	org.biojava.bio.seq.io;

import java.io.*;
import java.util.*;

import org.biojava.utils.*;
import org.biojava.bio.*;
import org.biojava.bio.symbol.*;
import org.biojava.bio.seq.*;

/**
 * Format reader for GenBank files. Converted from the old style io to the
 * new by working from <code>EmblLikeFormat</code>
 *
 * @author <NAME>
 * @author <NAME>
 * Added GenBank header	info to	the sequence annotation. The ACCESSION header
 * tag is not included.	Stored in sequence.getName().
 * @author <NAME>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */

public class GenbankFormat
        implements SequenceFormat,
                   Serializable,
                   org.biojava.utils.ParseErrorListener,
                   org.biojava.utils.ParseErrorSource
{
    static
    {
	Set validFormats = new HashSet();
	validFormats.add("Genbank");

	SequenceFormat.FORMATS.put(GenbankFormat.class.getName(),
				   validFormats);
    }

    protected static final String END_SEQUENCE_TAG = "//";
    protected static final String FEATURE_TAG = "FEATURES";
    protected static final String START_SEQUENCE_TAG = "ORIGIN";
    protected static final String FEATURE_LINE_PREFIX = "     ";
    protected static final String FEATURE_FLAG = "FT";
    protected static final String ACCESSION_TAG = "ACCESSION";
    protected static final String LOCUS_TAG = "LOCUS";
    protected static final String SIZE_TAG = "SIZE";
    protected static final String TYPE_TAG = "TYPE";
    protected static final String STRAND_NUMBER_TAG = "STRANDS";
    protected static final String CIRCULAR_TAG = "CIRCULAR";
    protected static final String DIVISION_TAG = "DIVISION";
    protected static final String DATE_TAG = "MDAT";
    protected static final String VERSION_TAG = "VERSION";
    protected static final String GI_TAG = "GI";

    private Vector mListeners = new Vector();

    /**
     * Reads a sequence from the specified reader using the Symbol
     * parser and Sequence Factory provided. The sequence read in must
     * be in Genbank format.
     *
     * @return boolean True if there is another sequence in the file; false
     * otherwise
     */
    public boolean readSequence(BufferedReader reader,
				SymbolParser symParser,
				SeqIOListener listener)
	throws IllegalSymbolException, IOException, ParseException
    {
        GenbankContext ctx = new GenbankContext(symParser, listener);
        ctx.addParseErrorListener(this);
        String line;
        boolean hasAnotherSequence    = true;
        boolean hasInternalWhitespace = false;

        listener.startSequence();

        while ((line = reader.readLine()) != null)
        {
            if (line.startsWith(END_SEQUENCE_TAG))
            {
                // To close the StreamParser encapsulated in the
                // GenbankContext object
                ctx.processLine(line);

                // Allows us to tolerate trailing whitespace without
                // thinking that there is another Sequence to follow
                while (true)
                {
                    reader.mark(1);
                    int c = reader.read();

                    if (c == -1)
                    {
                        hasAnotherSequence = false;
                        break;
                    }

                    if (Character.isWhitespace((char) c))
                    {
                        hasInternalWhitespace = true;
                        continue;
                    }

                    if (hasInternalWhitespace)
                        System.err.println("Warning: whitespace found between sequence entries");

                    reader.reset();
                    break;
                }

                listener.endSequence();
                return hasAnotherSequence;
            }
            ctx.processLine(line);
        }

        throw new IOException("Premature end of stream for GENBANK");
    }

    public void	writeSequence(Sequence seq, PrintStream os)
	throws IOException
    {
	String defaultFormat = getDefaultFormat();

	try
	{
	    SeqFileFormer former = SeqFileFormerFactory.makeFormer(defaultFormat);
	    former.setPrintStream(os);
{'fraction_non_alphanumeric': 0.08572984749455338, 'fraction_numerical': 0.0008714596949891067, 'mean_word_length': 2.5393215111796454, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4883333', 'n_tokens_mistral': 2618, 'n_tokens_neox': 2425, 'n_words': 805}