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

public class FASTAReader
{
	private String fileName;
   	private String sequence;
 	private String header;
 	private String nextHeader;
	private	BufferedReader	inputFile;

    public FASTAReader(String f)
	{	fileName = f;
      		sequence = "";
     		nextHeader = "";
	}

	//does not check that all characters are ACGT
	public boolean isFASTA()
	{
		String firstLine = "";
		try
		{
			inputFile = new BufferedReader(new FileReader(fileName));

			firstLine = inputFile.readLine();
			if (firstLine == null)
			{	inputFile.close();
				return false;
			}

			//String secondLine = inputFile.readLine();
			//if (secondLine == null)
			//{	inputFile.close();
			//	return false;
			//}

            		if (firstLine.charAt(0) != '>')
			{	inputFile.close();
				return false;
			}

		} catch (Exception e) { e.printStackTrace(); }

		/* Ye */
		
		nextHeader = firstLine;
		sequence = "";
		//try
		//{
		//	nextHeader = inputFile.readLine();
		//} catch (Exception e) { e.printStackTrace(); }

		return true;
	}

  	public void readASeq()
	{
		header = "";
		sequence = "";
		if(nextHeader == null) return;
		if(nextHeader == "") return;

		try
		{
			header = nextHeader;

            		StringBuffer sb = new StringBuffer();

			String currLine = inputFile.readLine();
			while((currLine != null) && (currLine.length() > 0) && (currLine.charAt(0) != '>'))
			{	sb.append(currLine);
				currLine = inputFile.readLine();
			}
			while((currLine != null) && (currLine.length() == 0)) 
			{ //empty lines
				currLine = inputFile.readLine();
			}
			nextHeader = currLine;

      			sequence = sb.toString();
			if (currLine == null)
			{
				inputFile.close();
			}
		} catch (Exception e) 
		{ 
			System.out.println("reading file error, nextHeader " + nextHeader);
			e.printStackTrace(); 
		}
	}

	public String getSequence()
	{	return sequence;
	}

   	public String getHeader()
	{	
		return header.substring(1, header.length());
	}
}
