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.GenbankFormat;
034import org.biojava.bio.seq.io.SeqIOTools;
035import org.biojava.bio.seq.io.SequenceFormat;
036import org.biojava.bio.symbol.Alphabet;
037
038/**
039 * @author Lei Lai
040 * @author Matthew Pocock
041 * @author Shuvankar Mukherjee
042 * @George Waldon
043 */
044public class GenpeptSequenceDB {
045  private static SequenceFormat format = new GenbankFormat();
046  private static String DBName = "Genpept";
047  private boolean IOExceptionFound = false;
048  private boolean ExceptionFound = false;
049
050  protected SequenceFormat getSequenceFormat() {
051    return format;
052  }
053
054  protected Alphabet getAlphabet() {
055    return ProteinTools.getTAlphabet();
056  }
057
058  protected URL getAddress(String id) throws MalformedURLException {
059      String defaultReturnFormat = "text";
060      return getAddress(id, defaultReturnFormat);
061  }
062
063    //ask user to input id and return format
064   protected URL getAddress(String id, String format) throws
065          MalformedURLException {
066      FetchURL seqURL = new FetchURL(DBName, format);
067      String baseurl = seqURL.getbaseURL();
068      String db = seqURL.getDB();
069      String type = seqURL.getRetrievalType();
070      String mode = seqURL.getRetrievalMode();
071      String url = baseurl + db + "&id=" + id + "&rettype=" + type + "&retmode=" + mode;
072      return new URL(url);
073  }
074
075  public String getName() {
076    return DBName;
077  }
078
079  public Sequence getSequence(String id) throws BioException {
080    try {
081      IOExceptionFound = false;
082      ExceptionFound = false;
083      URL queryURL = getAddress(id); //achieve URL based on ID
084
085
086      //System.err.println("got data from " + queryURL);
087      DataInputStream in = new DataInputStream(queryURL.openStream());
088      BufferedReader reader = new BufferedReader(new InputStreamReader(in));
089      SequenceIterator seqI = SeqIOTools.readGenpept(reader);
090
091      return seqI.nextSequence();
092
093    } catch (Exception e) {
094      System.out.println(e.toString());
095      IOExceptionFound = true;
096      ExceptionFound = true;
097      return null;
098    }
099  }
100
101  public boolean checkIOException() {
102    return IOExceptionFound;
103  }
104
105  public boolean checkException() {
106    return ExceptionFound;
107  }
108}