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 */
021package org.biojava.bio.seq.db;
022
023import java.io.BufferedReader;
024import java.io.DataInputStream;
025import java.io.InputStreamReader;
026import java.net.MalformedURLException;
027import java.net.URL;
028
029import org.biojava.bio.BioException;
030import org.biojava.bio.seq.ProteinTools;
031import org.biojava.bio.seq.Sequence;
032import org.biojava.bio.seq.SequenceIterator;
033import org.biojava.bio.seq.io.EmblLikeFormat;
034import org.biojava.bio.seq.io.SeqIOTools;
035import org.biojava.bio.seq.io.SequenceFormat;
036import org.biojava.bio.symbol.Alphabet;
037
038/**
039 * This class contains functions accessing sequences in swiss-prot.
040 *
041 * @author Lei Lai
042 * @author Matthew Pocock
043 */
044public class SwissprotSequenceDB
045{
046  private static SequenceFormat format = new EmblLikeFormat();
047  private static String DBName="swiss-prot";
048  private boolean IOExceptionFound=false;
049  
050  
051  protected SequenceFormat getSequenceFormat() 
052  {
053    return format;
054  }
055  
056  protected Alphabet getAlphabet() 
057  {
058    return ProteinTools.getTAlphabet();
059  }
060
061  protected URL getAddress (String id) throws MalformedURLException
062  {
063        String defaultReturnFormat="";
064        FetchURL seqURL = new FetchURL(DBName, defaultReturnFormat);
065        String baseurl = seqURL.getbaseURL();
066        
067        String url = baseurl+id;
068        
069    return new URL (url);
070  }
071 
072  public String getName() 
073  {
074    return DBName;
075  }
076  
077  public Sequence getSequence(String id) throws BioException 
078  {
079    try 
080        {
081          IOExceptionFound=false;
082      URL queryURL = getAddress(id);//achieve URL based on ID  
083          DataInputStream in=new DataInputStream(queryURL.openStream());
084          BufferedReader reader = new BufferedReader (new InputStreamReader (in));
085          SequenceIterator seqI= SeqIOTools.readSwissprot(reader);
086      return seqI.nextSequence();
087    } 
088        catch ( Exception e )
089        {
090          System.out.println (e.toString());
091          IOExceptionFound=true;
092          return null;
093    } 
094  }
095  
096  public boolean checkIOException()
097  {
098        return IOExceptionFound;
099  }
100}