/* BioJavaEntrySource.java
 *
 * created: Tue Apr 10 2001
 *
 * This file is part of Artemis
 *
 * Copyright (C) 2001  Genome Research Limited
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header: /nfs/pathsoft/repository/powmap/diana/components/BioJavaEntrySource.java,v 1.2 2002/08/14 12:04:10 kmr Exp $
 */

package diana.components;

import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.RuntimeException;

import org.biojava.bio.program.das.*;
import org.biojava.bio.BioException;

import uk.ac.sanger.pathogens.*;
import uk.ac.sanger.pathogens.embl.EntryInformation;
import uk.ac.sanger.pathogens.embl.SimpleEntryInformation;

import diana.*;
import diana.sequence.*;

/**
 *  This is an EntrySource that reads Entry objects from a BioJava Sequence
 *  object.
 *
 *  @author Kim Rutherford <kmr@sanger.ac.uk>
 *  @version $Id: BioJavaEntrySource.java,v 1.2 2002/08/14 12:04:10 kmr Exp $
 **/

public class BioJavaEntrySource implements EntrySource {
  /**
   *  Create a new BioJavaEntrySource.
   **/
  public BioJavaEntrySource () {

  }

  /**
   *  Get an Entry object from this source (by reading from a file, reading
   *  from a CORBA server, or whatever).
   *  @param bases The Bases object to pass to the Entry constructor.
   *  @param show_progress If true a InputStreamProgressDialog will be shown
   *    while loading.  (Not implemented)
   *  @exception OutOfRangeException Thrown if one of the features in
   *    the Entry is out of range of the Bases object.
   *  @return null if and only if the read is cancelled by the user or if the
   *    read fails.
   **/
    public Entry getEntry (final Bases bases, final boolean show_progress)
	throws OutOfRangeException, IOException
    {
	final org.biojava.bio.seq.Sequence bioJavaSeq = mangleSequence();
	final EntryInformation entryInformation = 
	    new SimpleEntryInformation(Options.getArtemisEntryInformation());

	final uk.ac.sanger.pathogens.embl.BioJavaEntry emblEntry =
	    new uk.ac.sanger.pathogens.embl.BioJavaEntry (entryInformation,
							  bioJavaSeq);
 
	return new Entry (bases, emblEntry);
    }

  /**
   *  Get an Entry object from this source (by reading from a file, reading
   *  from a CORBA server, or whatever).  A Bases object will be created for
   *  the sequence of the new Entry.
   *  @param show_progress If true a InputStreamProgressDialog will be shown
   *    while loading.  (Not implemented)
   *  @exception OutOfRangeException Thrown if one of the features in
   *    the Entry is out of range of the Bases object.
   *  @exception NoSequenceException Thrown if the entry that we read has no
   *    sequence.
   *  @return null if and only if the user cancels the read or if the read
   *    fails.
   **/
    public Entry getEntry (final boolean show_progress)
	throws OutOfRangeException, NoSequenceException, IOException
    {
	final org.biojava.bio.seq.Sequence bioJavaSeq = mangleSequence();
	final EntryInformation entryInformation = 
	    new SimpleEntryInformation(Options.getArtemisEntryInformation());

	final uk.ac.sanger.pathogens.embl.BioJavaEntry emblEntry =
	    new uk.ac.sanger.pathogens.embl.BioJavaEntry (entryInformation,
							  bioJavaSeq);

	return new Entry (emblEntry);
    }

  /**
   *  Returns true if and only if this EntrySource always returns "full"
   *  entries.  ie. entries that contain features and sequence.
   **/
  public boolean isFullEntrySource () {
    return true;
  }

  /**
   *  Return the name of this source (for display to the user in menus).
   **/
  public String getSourceName () {
    return "BioJava";
  }

//      private org.biojava.bio.seq.Sequence mangleSequence()
//      {
//  	DASSequence dasSeq = null;

//  	try
//  	{
//  	    URL dbURL = new URL("http://servlet.sanger.ac.uk:8080/das/ensembl080/");
//  	    DASSequenceDB dasDB = new DASSequenceDB(dbURL);

//  	    Set ids = dasDB.ids();
//  	    for (Iterator i = ids.iterator(); i.hasNext(); ) {
//  		System.out.println(i.next().toString());
//  	    }

//  	    // dasSeq = (DASSequence) dasDB.getSequence((String) ids.iterator().next());

//  	    SequenceIterator si = dasDB.sequenceIterator();

//  	    if (si.hasNext())
//  	    {
//  		dasSeq = (DASSequence) si.nextSequence();

//  		System.err.println("Got: " + dasSeq);
//  	    }
//  	    else
//  		System.err.println("No more sequences...");

//  	}
//  	catch (MalformedURLException mue)
//  	{
//  	    mue.printStackTrace();
//  	}
//  	catch (BioException be)
//  	{
//  	    be.printStackTrace();
//  	}

//  	return dasSeq;
//      }


    private org.biojava.bio.seq.Sequence mangleSequence()
	throws IOException
    {
	String fileName = "/nfs/maul/kdj/devel/java/powmap/etc/c1215.embl";
	File   emblFile = new File(fileName);

	BufferedReader reader =
	    new BufferedReader(new InputStreamReader(new FileInputStream(emblFile)));

	org.biojava.bio.seq.SequenceIterator seqIterator =
	    org.biojava.bio.seq.io.SeqIOTools.readEmbl(reader);

	try
	{
	    if (seqIterator.hasNext())
	    {
		return seqIterator.nextSequence();
	    }
	}
	catch (org.biojava.bio.BioException be)
	{
	    throw new IOException("Error reading BioJava sequence");
	}

	return null;
    }
}
