Skip to content
Snippets Groups Projects
DatabaseDocument.java 27 KiB
Newer Older
  • Learn to ignore specific revisions
  • tjc's avatar
    tjc committed
    /* DatabaseDocument.java
     *
    
    tjc's avatar
    tjc committed
     * created: 2005
    
    tjc's avatar
    tjc committed
     *
     * This file is part of Artemis
     * 
    
    tjc's avatar
    tjc committed
     * Copyright (C) 2005  Genome Research Limited
    
    tjc's avatar
    tjc committed
     * 
     * 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.
     *
     */
    
    package uk.ac.sanger.artemis.util;
    
    
    tjc's avatar
    tjc committed
    import com.ibatis.sqlmap.client.SqlMapClient;
    import uk.ac.sanger.ibatis.*;
    
    tjc's avatar
    tjc committed
    import uk.ac.sanger.artemis.io.GFFStreamFeature;
    
    tjc's avatar
    tjc committed
    import uk.ac.sanger.artemis.chado.ChadoTransaction;
    
    
    tjc's avatar
    tjc committed
    import java.sql.*;
    import java.io.*;
    import java.util.Hashtable;
    
    import java.util.Vector;
    
    tjc's avatar
    tjc committed
    import java.util.Enumeration;
    
    tjc's avatar
    tjc committed
    import java.util.Date;
    
    tjc's avatar
    tjc committed
    import java.util.List;
    import java.util.Iterator;
    
    tjc's avatar
    tjc committed
    import javax.swing.JOptionPane;
    
    tjc's avatar
    tjc committed
    import javax.swing.JPasswordField;
    
    tjc's avatar
    tjc committed
    
    /**
    
    tjc's avatar
    tjc committed
     * Objects of this class are Documents created from a relational database.
     * 
     */
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
    public class DatabaseDocument extends Document
    
    tjc's avatar
    tjc committed
    {
      private String name = null;
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
      private String feature_id = "1";
    
    tjc's avatar
    tjc committed
    
      /** database schema */
      private String schema = "public";
    
    
    tjc's avatar
    tjc committed
      private static Hashtable cvterm;
    
    tjc's avatar
    tjc committed
    
    
      private InputStreamProgressListener progress_listener;
    
    tjc's avatar
    tjc committed
    
    
      private Hashtable db;
    
    tjc's avatar
    tjc committed
    
    
      private Vector organism;
    
    tjc's avatar
    tjc committed
    
      private Hashtable org2schema;
    
      private String sqlLog = System.getProperty("user.home") +
                              System.getProperty("file.separator") + 
                              "art_sql_debug.log";
    
    
    tjc's avatar
    tjc committed
      private ByteBuffer[] gff_buffer;
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
      private ByteBuffer gff_buff;
    
    tjc's avatar
    tjc committed
    
      /** entries to split into */
    
    tjc's avatar
    tjc committed
      private String[] types = { "exon", "gene", "CDS", "transcript" };
    
    tjc's avatar
    tjc committed
    
      /** true if splitting the GFF into entries */
    
    tjc's avatar
    tjc committed
      private boolean splitGFFEntry;
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
      private boolean iBatis = false;
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
      private JPasswordField pfield;
    
    tjc's avatar
    tjc committed
    
      /**
    
    tjc's avatar
    tjc committed
       * 
       * Create a new Document from a database.
       * 
       * @param location
       *          This should be a URL string giving:
       *          jdbc:postgresql://host:port/datbase_name?user=username
       * 
       */
    
    tjc's avatar
    tjc committed
      public DatabaseDocument(String location, JPasswordField pfield)
    
    tjc's avatar
    tjc committed
      {
        super(location);
    
    tjc's avatar
    tjc committed
        this.pfield = pfield;
    
    
    tjc's avatar
    tjc committed
        if(System.getProperty("ibatis") != null)
    
    tjc's avatar
    tjc committed
        {
    
    tjc's avatar
    tjc committed
          iBatis = true;
    
    tjc's avatar
    tjc committed
          System.setProperty("chado", location);
    
    tjc's avatar
    tjc committed
        }
    
    tjc's avatar
    tjc committed
      }
    
      /**
    
    tjc's avatar
    tjc committed
       * 
       * Create a new Document from a database.
       * 
       * @param location
       *          This should be a URL string giving:
       *          jdbc:postgresql://host:port/datbase_name?user=username
       * @param feature_id
       *          ID of a feature to be extracted.
       * 
       */
    
    tjc's avatar
    tjc committed
      public DatabaseDocument(String location, JPasswordField pfield,
    
    tjc's avatar
    tjc committed
                              String feature_id, String schema)
    
    tjc's avatar
    tjc committed
      {
        super(location);
    
    tjc's avatar
    tjc committed
        this.pfield = pfield;
    
    
    tjc's avatar
    tjc committed
        this.feature_id = feature_id;
    
    tjc's avatar
    tjc committed
        this.schema = schema;
    
    
    tjc's avatar
    tjc committed
        if(System.getProperty("ibatis") != null)
    
    tjc's avatar
    tjc committed
        {
    
    tjc's avatar
    tjc committed
          iBatis = true;
    
    tjc's avatar
    tjc committed
          System.setProperty("chado", location);
    
    tjc's avatar
    tjc committed
        }
    
    tjc's avatar
    tjc committed
      }
    
    
    tjc's avatar
    tjc committed
      /**
    
    tjc's avatar
    tjc committed
       * 
       * Create a new Document from a database.
       * 
       * @param location
       *          This should be a URL string giving:
       *          jdbc:postgresql://host:port/datbase_name?user=username
       * @param feature_id
       *          ID of a feature to be extracted.
       * @param splitGFFEntry
       *          split into separate entries based on feature types.
       * @param progress_listener
       *          input stream progress listener
       * 
       */
    
    tjc's avatar
    tjc committed
      public DatabaseDocument(String location, JPasswordField pfield,
    
    tjc's avatar
    tjc committed
                              String feature_id, String schema, boolean splitGFFEntry,
    
                              InputStreamProgressListener progress_listener)
      {
        super(location);
    
    tjc's avatar
    tjc committed
        this.pfield = pfield;
    
        this.feature_id = feature_id;
    
    tjc's avatar
    tjc committed
        this.schema = schema;
    
    tjc's avatar
    tjc committed
        this.splitGFFEntry = splitGFFEntry;
    
        this.progress_listener = progress_listener;
    
    tjc's avatar
    tjc committed
        if(System.getProperty("ibatis") != null)
    
    tjc's avatar
    tjc committed
        {
    
    tjc's avatar
    tjc committed
          iBatis = true;
    
    tjc's avatar
    tjc committed
          System.setProperty("chado", location);
    
    tjc's avatar
    tjc committed
        }
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
      public DatabaseDocument(String location, JPasswordField pfield,
    
    tjc's avatar
    tjc committed
                              String feature_id, String schema,
    
    tjc's avatar
    tjc committed
                              ByteBuffer gff_buff, String name)
    
    tjc's avatar
    tjc committed
      {
        super(location);
    
    tjc's avatar
    tjc committed
        this.pfield = pfield;
    
    tjc's avatar
    tjc committed
        this.feature_id = feature_id;
    
    tjc's avatar
    tjc committed
        this.schema = schema;
        this.gff_buff = gff_buff;
    
    tjc's avatar
    tjc committed
        this.name = name;
    
    tjc's avatar
    tjc committed
        if(System.getProperty("ibatis") != null)
    
    tjc's avatar
    tjc committed
        {
    
    tjc's avatar
    tjc committed
          iBatis = true;
    
    tjc's avatar
    tjc committed
          System.setProperty("chado", location);
    
    tjc's avatar
    tjc committed
        }
    
    tjc's avatar
    tjc committed
      /**
    
    tjc's avatar
    tjc committed
       * 
       * Append a String to the Document location.
       * 
       * @param name
       *          The name to append.
       * 
       */
      public Document append(String name) throws IOException
    
    tjc's avatar
    tjc committed
      {
    
    tjc's avatar
    tjc committed
        return new DatabaseDocument( ((String)getLocation()) + name, pfield);
    
    tjc's avatar
    tjc committed
      }
    
      /**
    
    tjc's avatar
    tjc committed
       * 
       * Return the name of this Document (the last element of the Document
       * location).
       * 
       */
      public String getName()
    
    tjc's avatar
    tjc committed
      {
        if(name == null)
        {
    
    tjc's avatar
    tjc committed
          int ind     = ((String) getLocation()).indexOf("?");
          String name = ((String) getLocation()).substring(0, ind);
    
    tjc's avatar
    tjc committed
          ind = name.lastIndexOf("/");
    
    tjc's avatar
    tjc committed
          return name.substring(ind + 1);
    
    tjc's avatar
    tjc committed
        }
        return name;
      }
    
    
    tjc's avatar
    tjc committed
       * 
       * Return a Document with the last element stripped off.
       * 
       */
    
      public Document getParent()
    
    tjc's avatar
    tjc committed
      {
        return null;
      }
    
    tjc's avatar
    tjc committed
      /**
    
    tjc's avatar
    tjc committed
       * 
       * Return true if and only if the Document refered to by this object exists
       * and is readable. Always returns true.
       * 
       */
      public boolean readable()
    
    tjc's avatar
    tjc committed
      {
        return true;
      }
    
      /**
    
    tjc's avatar
    tjc committed
       * 
       * Return true if and only if the Document refered to by this object exists
       * and can be written to. Always returns false.
       * 
       */
      public boolean writable()
    
    tjc's avatar
    tjc committed
      {
        return true;
      }
    
      /**
    
    tjc's avatar
    tjc committed
       * Create a new InputStream object from this Document. The contents of the
       * Document can be read from the InputStream.
       * 
       * @exception IOException
       *              Thrown if the Document can't be read from (for example if it
       *              doesn't exist).
       */
      public InputStream getInputStream() throws IOException
    
    tjc's avatar
    tjc committed
      {
    
    tjc's avatar
    tjc committed
        ByteArrayInputStream instream;
    
        if(gff_buff != null)
        {
    
    tjc's avatar
    tjc committed
          instream = new ByteArrayInputStream(gff_buff.getBytes());
    
    tjc's avatar
    tjc committed
          return instream;
        }
    
    
    tjc's avatar
    tjc committed
        try
        {
    
    tjc's avatar
    tjc committed
          Connection conn = null;
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          if(!iBatis)
            conn = getConnection();
    
          if(iBatis)
            gff_buffer = getGFFiBatis(feature_id);
          else
    
    tjc's avatar
    tjc committed
            gff_buffer = getGFFJdbc(conn, feature_id, schema);
    
    tjc's avatar
    tjc committed
          ByteBuffer entry = new ByteBuffer();
    
    tjc's avatar
    tjc committed
          if(splitGFFEntry)
    
    tjc's avatar
    tjc committed
          {
            if(gff_buffer[0].size() > 0)
              entry.append(gff_buffer[0]);
    
    tjc's avatar
    tjc committed
    
            if(iBatis)
              getSequenceIbatis(entry);
            else
    
    tjc's avatar
    tjc committed
              getSequence(conn, entry, schema);
    
    tjc's avatar
    tjc committed
          }
    
    tjc's avatar
    tjc committed
          else
          {
    
    tjc's avatar
    tjc committed
            for(int i = 0; i < gff_buffer.length; i++)
    
    tjc's avatar
    tjc committed
            {
              if(gff_buffer[i].size() > 0)
                entry.append(gff_buffer[i]);
            }
    
    tjc's avatar
    tjc committed
    
            if(iBatis)
              getSequenceIbatis(entry);
            else
    
    tjc's avatar
    tjc committed
              getSequence(conn, entry, schema);
    
    tjc's avatar
    tjc committed
          if(System.getProperty("debug") != null)
    
    tjc's avatar
    tjc committed
            appendToLogFile(new String(entry.getBytes()), sqlLog);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          instream = new ByteArrayInputStream(entry.getBytes());
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          if(conn != null)
            conn.close();
    
    tjc's avatar
    tjc committed
          return instream;
        }
        catch(java.sql.SQLException sqlExp)
        {
          System.out.println("Problems connecting...");
          sqlExp.printStackTrace();
        }
    
        return null;
      }
    
    
    tjc's avatar
    tjc committed
      /**
    
    tjc's avatar
    tjc committed
       * 
       * Called (by DatabaseEntrySource) to retrieve all the documents for each
       * entry created.
       * 
       */
      public DatabaseDocument[] getGffDocuments(String location, String id,
                                                String schema)
    
    tjc's avatar
    tjc committed
        int nentries = 0;
    
    tjc's avatar
    tjc committed
        for(int i = 1; i < gff_buffer.length; i++)
    
    tjc's avatar
    tjc committed
        {
          if(gff_buffer[i].size() > 0)
            nentries++;
        }
    
        DatabaseDocument[] new_docs = new DatabaseDocument[nentries];
        nentries = 0;
    
    tjc's avatar
    tjc committed
        for(int i = 1; i < gff_buffer.length; i++)
    
    tjc's avatar
    tjc committed
          if(gff_buffer[i].size() == 0)
            continue;
    
    
    tjc's avatar
    tjc committed
          String name;
          if(i >= types.length)
            name = "other";
          else
            name = types[i];
    
    
    tjc's avatar
    tjc committed
          new_docs[nentries] = new DatabaseDocument(location, pfield, id, schema,
                                                    gff_buffer[i], name);
    
    tjc's avatar
    tjc committed
          nentries++;
    
    tjc's avatar
    tjc committed
        }
    
        return new_docs;
      }
    
    
    tjc's avatar
    tjc committed
      /**
    
    tjc's avatar
    tjc committed
       * 
       * Return a feature name given the feature_id.
       * 
       */
    
    tjc's avatar
    tjc committed
      private String getFeatureNameIbatis(String feature_id)
    
    tjc's avatar
    tjc committed
          throws java.sql.SQLException
    
    tjc's avatar
    tjc committed
      {
        SqlMapClient sqlMap = DbSqlConfig.getSqlMapInstance();
    
    tjc's avatar
    tjc committed
        return (String) sqlMap.queryForObject("getFeatureName", feature_id);
    
    tjc's avatar
    tjc committed
      }
    
      /**
    
    tjc's avatar
    tjc committed
       * 
       * Return a feature name given the feature_id.
       * 
       */
      private String getFeatureNameJdbc(String feature_id, Connection conn,
          String schema) throws java.sql.SQLException
    
    tjc's avatar
    tjc committed
      {
        Statement st = conn.createStatement();
    
    
    tjc's avatar
    tjc committed
        String sql = "SELECT name FROM " + schema + ".feature WHERE feature_id= " +
                      feature_id;
        appendToLogFile(sql, sqlLog);
    
    tjc's avatar
    tjc committed
        ResultSet rs = st.executeQuery(sql);
        rs.next();
        return rs.getString("name");
      }
    
      private ByteBuffer[] getGFFiBatis(String parentFeatureID)
    
    tjc's avatar
    tjc committed
          throws java.sql.SQLException
    
    tjc's avatar
    tjc committed
      {
    
        SqlMapClient sqlMap = DbSqlConfig.getSqlMapInstance();
    
    tjc's avatar
    tjc committed
        List featList = sqlMap.queryForList("getGffLine", new Integer(feature_id));
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        ByteBuffer[] buffers = new ByteBuffer[types.length + 1];
        for(int i = 0; i < buffers.length; i++)
    
    tjc's avatar
    tjc committed
          buffers[i] = new ByteBuffer();
    
        String parentFeature = getFeatureNameIbatis(parentFeatureID);
        Hashtable hstore = new Hashtable();
        ByteBuffer this_buff;
    
        int feature_size = featList.size();
    
    
    tjc's avatar
    tjc committed
        for(int i = 0; i < feature_size; i++)
    
    tjc's avatar
    tjc committed
        {
          Feature feat = (Feature)featList.get(i);
    
    tjc's avatar
    tjc committed
          int fmin                = feat.getFmin() + 1;
          int fmax                = feat.getFmax();
          long type_id            = feat.getType_id();
          long prop_type_id       = feat.getProp_type_id();
          int strand              = feat.getStrand();
          String name             = feat.getUniquename();
          String typeName         = getCvtermName(null, type_id);
          String propTypeName     = getCvtermName(null, prop_type_id);
    
    tjc's avatar
    tjc committed
          String timelastmodified = feat.getTimelastmodified().toString();
    
    tjc's avatar
    tjc committed
          String feature_id       = Integer.toString(feat.getId());
    
    tjc's avatar
    tjc committed
          hstore.put(feature_id, name);
    
    
    tjc's avatar
    tjc committed
          String parent_id = feat.getObject_id();
    
    tjc's avatar
    tjc committed
          if(parent_id != null && hstore.containsKey(parent_id))
            parent_id = (String)hstore.get(parent_id);
    
    
    tjc's avatar
    tjc committed
          // make gff format
    
    tjc's avatar
    tjc committed
    
          // select buffer
          this_buff = buffers[types.length];
    
    tjc's avatar
    tjc committed
          for(int j = 0; j < types.length; j++)
    
    tjc's avatar
    tjc committed
          {
            if(types[j].equals(typeName))
              this_buff = buffers[j];
          }
    
    
    tjc's avatar
    tjc committed
          this_buff.append(parentFeature + "\t"); // seqid
          this_buff.append("chado\t");            // source
          this_buff.append(typeName + "\t");      // type
          this_buff.append(fmin + "\t");          // start
          this_buff.append(fmax + "\t");          // end
          this_buff.append(".\t");                // score
          if(strand == -1)                       // strand
    
    tjc's avatar
    tjc committed
            this_buff.append("-\t");
          else if(strand == 1)
            this_buff.append("+\t");
          else
            this_buff.append(".\t");
    
    
    tjc's avatar
    tjc committed
          this_buff.append(".\t");               // phase
          this_buff.append("ID=" + name + ";");
    
    tjc's avatar
    tjc committed
    
          if(parent_id != null)
    
    tjc's avatar
    tjc committed
            this_buff.append("Parent=" + parent_id + ";");
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          this_buff.append("timelastmodified=" + timelastmodified + ";");
    
    tjc's avatar
    tjc committed
    
          String value = "";
          if(feat.getValue() != null)
            value = GFFStreamFeature.encode(feat.getValue());
    
    
    tjc's avatar
    tjc committed
          this_buff.append(propTypeName + "=" + value); // attributes
    
    tjc's avatar
    tjc committed
    
          // is the next line part of the same feature, if so merge
          boolean rewind = false;
          Feature featNext = null;
    
    
    tjc's avatar
    tjc committed
          if(i < feature_size - 1)
            featNext = (Feature)featList.get(i + 1);
    
    
    tjc's avatar
    tjc committed
          // merge next line if part of the same feature
    
    tjc's avatar
    tjc committed
          while(featNext != null && featNext.getUniquename().equals(name))
    
    tjc's avatar
    tjc committed
          {
    
    tjc's avatar
    tjc committed
            prop_type_id = featNext.getProp_type_id();
    
    tjc's avatar
    tjc committed
            propTypeName = getCvtermName(null, prop_type_id);
    
    tjc's avatar
    tjc committed
            value = GFFStreamFeature.encode(featNext.getValue());
    
    tjc's avatar
    tjc committed
            this_buff.append(";" + propTypeName + "=" + value);
    
    tjc's avatar
    tjc committed
            i++;
    
    tjc's avatar
    tjc committed
            if(i < feature_size - 1)
              featNext = (Feature) featList.get(i + 1);
            else
    
    tjc's avatar
    tjc committed
              break;
          }
    
          this_buff.append("\n");
    
    
    tjc's avatar
    tjc committed
          progress_listener.progressMade("Read from database: " + name);
    
    tjc's avatar
    tjc committed
        }
    
        return buffers;
      }
    
    
    tjc's avatar
    tjc committed
       * 
       * Given a parent (chromosome, contig, supercontig) retrieve the features in
       * the form of a GFF stream.
       * 
       */
      private ByteBuffer[] getGFFJdbc(Connection conn, String parentFeatureID,
                                      String schema) 
    
    tjc's avatar
    tjc committed
              throws java.sql.SQLException
      {
    
    tjc's avatar
    tjc committed
        Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_UPDATABLE);
    
        String sql = "SELECT timelastmodified, feature.feature_id, object_id, strand, fmin, fmax, uniquename, "
            + schema + ".feature.type_id, "
            + schema + ".featureprop.type_id AS prop_type_id, featureprop.value"
            + " FROM  "
            + schema + ".featureloc, "
            + schema + ".feature"
            + " LEFT JOIN "
            + schema + ".feature_relationship ON "
            + schema + ".feature_relationship.subject_id="
            + schema + ".feature.feature_id"
            + " LEFT JOIN "
            + schema + ".featureprop ON "
            + schema + ".featureprop.feature_id="
            + schema + ".feature.feature_id"
            + " WHERE srcfeature_id = "
            + parentFeatureID + " AND "
            + schema + ".featureloc.feature_id="
            + schema + ".feature.feature_id"
            + " AND ("
            + schema + ".featureloc.rank="
            + schema + ".feature_relationship.rank OR "
            + schema + ".feature_relationship.rank IS NULL)"
            + " ORDER BY "
            + schema + ".feature.type_id,  uniquename";
    
        appendToLogFile(sql, sqlLog);
    
    tjc's avatar
    tjc committed
        ResultSet rs = st.executeQuery(sql);
    
    tjc's avatar
    tjc committed
        ByteBuffer[] buffers = new ByteBuffer[types.length + 1];
        for(int i = 0; i < buffers.length; i++)
    
    tjc's avatar
    tjc committed
          buffers[i] = new ByteBuffer();
    
    tjc's avatar
    tjc committed
        String parentFeature = getFeatureNameJdbc(parentFeatureID, conn, schema);
    
        Hashtable hstore = new Hashtable();
    
    tjc's avatar
    tjc committed
        ByteBuffer this_buff;
    
    tjc's avatar
    tjc committed
        while(rs.next())
        {
    
    tjc's avatar
    tjc committed
          int fmin                = rs.getInt("fmin") + 1;
          int fmax                = rs.getInt("fmax");
          long type_id            = rs.getLong("type_id");
          long prop_type_id       = rs.getLong("prop_type_id");
          int strand              = rs.getInt("strand");
          String name             = rs.getString("uniquename");
          String typeName         = getCvtermName(conn, type_id);
          String propTypeName     = getCvtermName(conn, prop_type_id);
    
    tjc's avatar
    tjc committed
          String timelastmodified = rs.getString("timelastmodified");
    
    tjc's avatar
    tjc committed
          String feature_id       = rs.getString("feature_id");
    
          hstore.put(feature_id, name);
    
    
    tjc's avatar
    tjc committed
          String parent_id = rs.getString("object_id");
    
          if(parent_id != null && hstore.containsKey(parent_id))
            parent_id = (String)hstore.get(parent_id);
    
    tjc's avatar
    tjc committed
    
          // make gff format
    
    tjc's avatar
    tjc committed
    
          // select buffer
          this_buff = buffers[types.length];
    
    tjc's avatar
    tjc committed
          for(int i = 0; i < types.length; i++)
    
    tjc's avatar
    tjc committed
          {
            if(types[i].equals(typeName))
              this_buff = buffers[i];
          }
    
    
    tjc's avatar
    tjc committed
          this_buff.append(parentFeature + "\t"); // seqid
          this_buff.append("chado\t");            // source
          this_buff.append(typeName + "\t");      // type
          this_buff.append(fmin + "\t");          // start
          this_buff.append(fmax + "\t");          // end
          this_buff.append(".\t");                // score
          if (strand == -1)                       // strand
    
    tjc's avatar
    tjc committed
            this_buff.append("-\t");
    
    tjc's avatar
    tjc committed
          else if (strand == 1)
    
    tjc's avatar
    tjc committed
            this_buff.append("+\t");
    
    tjc's avatar
    tjc committed
          else
    
    tjc's avatar
    tjc committed
            this_buff.append(".\t");
    
    tjc's avatar
    tjc committed
          this_buff.append(".\t");               // phase
          this_buff.append("ID=" + name + ";");
    
          if(parent_id != null)
    
    tjc's avatar
    tjc committed
            this_buff.append("Parent=" + parent_id + ";");
    
          this_buff.append("timelastmodified=" + timelastmodified + ";");
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          String value = "";
          if(rs.getString("value") != null)
            value = GFFStreamFeature.encode(rs.getString("value"));
    
    
    tjc's avatar
    tjc committed
          this_buff.append(propTypeName + "=" + value); // attributes
    
    tjc's avatar
    tjc committed
          // is the next line part of the same feature, if so merge
    
    tjc's avatar
    tjc committed
          boolean rewind = false;
    
    tjc's avatar
    tjc committed
          while((rewind = rs.next()) && rs.getString("uniquename").equals(name))
    
    tjc's avatar
    tjc committed
          {
            prop_type_id = rs.getLong("prop_type_id");
    
    tjc's avatar
    tjc committed
            propTypeName = getCvtermName(conn, prop_type_id);
    
    tjc's avatar
    tjc committed
            value = GFFStreamFeature.encode(rs.getString("value"));
    
    tjc's avatar
    tjc committed
            this_buff.append(";" + propTypeName + "=" + value);
    
    tjc's avatar
    tjc committed
          if(rewind)
    
    tjc's avatar
    tjc committed
            rs.previous();
    
    
    tjc's avatar
    tjc committed
          this_buff.append("\n");
    
    tjc's avatar
    tjc committed
          progress_listener.progressMade("Read from database: " + name);
    
    tjc's avatar
    tjc committed
        }
    
    
    tjc's avatar
    tjc committed
        return buffers;
    
    tjc's avatar
    tjc committed
      }
    
    
    tjc's avatar
    tjc committed
      public static Long getCvtermID(String name)
    
    tjc's avatar
    tjc committed
      {
        Enumeration enum_cvterm = cvterm.keys();
        while(enum_cvterm.hasMoreElements())
        {
          Long key = (Long)enum_cvterm.nextElement();
          if(name.equals(cvterm.get(key)))
    
    tjc's avatar
    tjc committed
            return key;
    
    tjc's avatar
    tjc committed
        }
    
    tjc's avatar
    tjc committed
        return null;
    
    tjc's avatar
    tjc committed
        // return new Long("-1.");
    
    tjc's avatar
    tjc committed
      }
    
    
    tjc's avatar
    tjc committed
      private String getCvtermName(Connection conn, long id)
      {
        if(cvterm == null)
    
    tjc's avatar
    tjc committed
        {
          if(iBatis)
            getCvtermIbatis(null);
          else
    
    tjc's avatar
    tjc committed
            getCvterm(conn, null);
    
    tjc's avatar
    tjc committed
        }
    
    
    tjc's avatar
    tjc committed
        return (String)cvterm.get(new Long(id));
      }
    
    
    tjc's avatar
    tjc committed
      /**
    
    tjc's avatar
    tjc committed
       * 
       * Look up cvterms names and id and return in a hashtable.
       * 
       */
    
    tjc's avatar
    tjc committed
      private Hashtable getCvtermIbatis(String cv_name)
      {
        cvterm = new Hashtable();
    
        try
        {
          SqlMapClient sqlMap = DbSqlConfig.getSqlMapInstance();
    
          List cvtem_list = sqlMap.queryForList("getCvterm", null);
          Iterator it = cvtem_list.iterator();
    
          while(it.hasNext())
          {
            Cvterm cv = (Cvterm)it.next();
            cvterm.put(new Long(cv.getId()), cv.getName());
          }
        }
    
    tjc's avatar
    tjc committed
        catch(SQLException sqle)
    
    tjc's avatar
    tjc committed
        {
          System.err.println(this.getClass() + ": SQLException retrieving CvTerms");
          System.err.println(sqle);
        }
    
        return cvterm;
      }
    
    
    tjc's avatar
    tjc committed
      /**
    
    tjc's avatar
    tjc committed
       * 
       * Look up cvterms names and id and return in a hashtable.
       * 
       */
    
    tjc's avatar
    tjc committed
      private Hashtable getCvterm(Connection conn, String cv_name)
      {
        String sql = "SELECT cvterm.cvterm_id, cvterm.name " +
                     "FROM cvterm, cv WHERE cv.cv_id = cvterm.cv_id";
    
        if(cv_name != null)
    
    tjc's avatar
    tjc committed
          sql = sql + " AND cv.name='" + cv_name + "'";
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        appendToLogFile(sql, sqlLog);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        cvterm = new Hashtable();
    
    
    tjc's avatar
    tjc committed
        try
    
    tjc's avatar
    tjc committed
        {
          Statement s = conn.createStatement();
          ResultSet rs = s.executeQuery(sql);
    
    
    tjc's avatar
    tjc committed
          while(rs.next())
    
    tjc's avatar
    tjc committed
          {
            long id = rs.getLong("cvterm_id");
            String name = rs.getString("name");
    
    
    tjc's avatar
    tjc committed
            if(cvterm.get(name) != null)
              System.err.println(this.getClass()
                  + ": WARNING - read multiple CvTerms with name = '" + name + "'");
    
            cvterm.put(new Long(id), name);
    
    tjc's avatar
    tjc committed
          }
    
    tjc's avatar
    tjc committed
        }
        catch(SQLException sqle)
    
    tjc's avatar
    tjc committed
        {
          System.err.println(this.getClass() + ": SQLException retrieving CvTerms");
          System.err.println(sqle);
        }
    
        return cvterm;
      }
    
    
    tjc's avatar
    tjc committed
      public ByteBuffer getSequenceIbatis(ByteBuffer buff)
          throws java.sql.SQLException
    
    tjc's avatar
    tjc committed
      {
        SqlMapClient sqlMap = DbSqlConfig.getSqlMapInstance();
    
    tjc's avatar
    tjc committed
        Feature feat = (Feature)sqlMap.queryForObject("getSequence", new Integer(
            feature_id));
    
    tjc's avatar
    tjc committed
        buff.append("##FASTA\n>");
        buff.append(feat.getName());
        buff.append("\n");
        buff.append(feat.getResidues());
        return buff;
      }
    
    
    tjc's avatar
    tjc committed
      public ByteBuffer getSequence(Connection conn, ByteBuffer buff, String schema)
          throws java.sql.SQLException
    
    tjc's avatar
    tjc committed
      {
        Statement st = conn.createStatement();
    
    tjc's avatar
    tjc committed
        String sql = "SELECT name, residues from " + schema +
                     ".feature where feature_id = '" + feature_id + "'";
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        appendToLogFile(sql, sqlLog);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        ResultSet rs = st.executeQuery(sql);
        rs.next();
    
    tjc's avatar
    tjc committed
    
        buff.append("##FASTA\n>");
        buff.append(rs.getBytes("name"));
        buff.append("\n");
        buff.append(rs.getBytes("residues"));
        return buff;
    
    
    tjc's avatar
    tjc committed
        // return "##FASTA\n>" + name + "\n" + rs.getString("residues");
    
    tjc's avatar
    tjc committed
      }
    
    
    tjc's avatar
    tjc committed
      public Hashtable getDatabaseEntries()
      {
        if(iBatis)
          return getDatabaseEntriesIbatis();
        else
          return getDatabaseEntriesJdbc();
      }
    
    
    tjc's avatar
    tjc committed
      public Hashtable getSchemaEntries()
      {
        return org2schema;
      }
    
    
    tjc's avatar
    tjc committed
      /**
       * 
    
    tjc's avatar
    tjc committed
       * Create a hashtable of the available entries.
       * 
       */
    
    tjc's avatar
    tjc committed
      private Hashtable getDatabaseEntriesJdbc()
    
    tjc's avatar
    tjc committed
      {
    
        db = new Hashtable();
        organism = new Vector();
    
    tjc's avatar
    tjc committed
        org2schema = new Hashtable();
    
    tjc's avatar
    tjc committed
        try
        {
          Connection conn = getConnection();
    
    tjc's avatar
    tjc committed
          System.out.println("JDBC Connection");
    
    tjc's avatar
    tjc committed
    
          Statement st = conn.createStatement();
    
    
    tjc's avatar
    tjc committed
          String query = "SELECT schema_name FROM information_schema.schemata WHERE schema_name=schema_owner";
          appendToLogFile(query, sqlLog);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          ResultSet rs = st.executeQuery(query);
          Vector schemas = new Vector();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          while(rs.next())
            schemas.add(rs.getString("schema_name"));
    
    tjc's avatar
    tjc committed
          for(int i = 0; i < schemas.size(); i++)
    
    tjc's avatar
    tjc committed
            String schema = (String)schemas.get(i);
            appendToLogFile(schema, sqlLog);
    
    tjc's avatar
    tjc committed
            String sql = "SELECT DISTINCT type_id FROM " + schema +
                         ".feature WHERE residues notnull";
            appendToLogFile(sql, sqlLog);
    
    tjc's avatar
    tjc committed
            Vector cvterm_id = new Vector();
            rs = st.executeQuery(sql);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
            while(rs.next())
              cvterm_id.add(rs.getString("type_id"));
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
            sql = new String(
                "SELECT abbreviation, name, feature_id, type_id FROM organism, "+
                schema + ".feature WHERE (");
    
            for(int j = 0; j < cvterm_id.size(); j++)
            {
              sql = sql + " type_id = " + (String)cvterm_id.get(j);
              if(j < cvterm_id.size() - 1)
                sql = sql + " OR ";
            }
    
            sql = sql + ") and organism.organism_id=" + schema
                + ".feature.organism_id " + "and residues notnull "
                + "ORDER BY abbreviation";
    
            appendToLogFile(sql, sqlLog);
    
            rs = st.executeQuery(sql);
            while(rs.next())
            {
              String org      = rs.getString("abbreviation");
              String typeName = getCvtermName(conn, rs.getLong("type_id"));
              db.put(org + " - " + typeName + " - " + rs.getString("name"), 
                     rs.getString("feature_id"));
              if(!organism.contains(org))
                organism.add(org);
              if(!org2schema.containsKey(org))
              {
                org2schema.put(org, schema);
              }
            }
          }
    
    tjc's avatar
    tjc committed
          conn.close();
    
    tjc's avatar
    tjc committed
        }
        catch(java.sql.SQLException sqlExp)
        {
    
    tjc's avatar
    tjc committed
          JOptionPane.showMessageDialog(null, "SQL Problems...", "SQL Error",
                                        JOptionPane.ERROR_MESSAGE);
    
    tjc's avatar
    tjc committed
          sqlExp.printStackTrace();
        }
    
    tjc's avatar
    tjc committed
        catch (java.net.ConnectException conn)
    
    tjc's avatar
    tjc committed
        {
          JOptionPane.showMessageDialog(null, "Problems connecting...",
    
    tjc's avatar
    tjc committed
                                        "Database Connection Error - Check Server",
                                        JOptionPane.ERROR_MESSAGE);
          conn.printStackTrace();
    
    tjc's avatar
    tjc committed
        }
    
    tjc's avatar
    tjc committed
    
        return db;
      }
    
    
    tjc's avatar
    tjc committed
      /**
       * 
       * Create a hashtable of the available entries.
       * 
       */
    
    tjc's avatar
    tjc committed
      private Hashtable getDatabaseEntriesIbatis()
      {
        db = new Hashtable();
        organism = new Vector();
    
        try
        {
    
    tjc's avatar
    tjc committed
          DbSqlConfig.init(pfield);
    
    tjc's avatar
    tjc committed
          SqlMapClient sqlMap = DbSqlConfig.getSqlMapInstance();
          List list = sqlMap.queryForList("getResidueType", null);
    
    tjc's avatar
    tjc committed
          List list_residue_features = sqlMap.queryForList("getResidueFeatures",
                                                           list);
    
    tjc's avatar
    tjc committed
          Iterator it = list_residue_features.iterator();
    
          while(it.hasNext())
          {
            Feature feat = (Feature)it.next();
    
    tjc's avatar
    tjc committed
            String org = feat.getAbbreviation();
            String typeName = getCvtermName(null, feat.getType_id());
            db.put(org + " - " + typeName + " - " + feat.getName(), 
    
    tjc's avatar
    tjc committed
                   Integer.toString(feat.getId()));
            if(!organism.contains(org))
              organism.add(org);
          }
        }
        catch(java.sql.SQLException sqlExp)
        {
    
    tjc's avatar
    tjc committed
          JOptionPane.showMessageDialog(null, "SQL Problems...", "SQL Error",
                                        JOptionPane.ERROR_MESSAGE);
    
    tjc's avatar
    tjc committed
          sqlExp.printStackTrace();
        }
        return db;
      }
    
    
      public Vector getOrganism()
      {
        return organism;
      }
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
      /**
    
    tjc's avatar
    tjc committed
       * 
       * Make a connetion with the jdbc
       * jdbc:postgresql://localhost:13001/chadoCVS?user=es2
       * 
       */
    
    tjc's avatar
    tjc committed
      public Connection getConnection() throws java.sql.SQLException,
    
    tjc's avatar
    tjc committed
          java.net.ConnectException
    
    tjc's avatar
    tjc committed
      {
    
    tjc's avatar
    tjc committed
        String location = (String)getLocation();
    
    tjc's avatar
    tjc committed
        if(pfield == null || pfield.getPassword().length == 0)
    
    tjc's avatar
    tjc committed
          return DriverManager.getConnection(location);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        // assume we have a password
        final int index = location.indexOf("?user=");
        return DriverManager.getConnection(location.substring(0, index), 
                                           location.substring(index + 6),
                                           new String(pfield.getPassword()));
      }
    
    tjc's avatar
    tjc committed
    
      /**
    
    tjc's avatar
    tjc committed
       * Create a new OutputStream object from this Document. The contents of the
       * Document can be written from the stream.
       * 
       * @exception IOException
       *              Thrown if the Document can't be written.
       */
    
    tjc's avatar
    tjc committed
      public OutputStream getOutputStream() throws IOException
      {
        System.out.println("DatabaseDocument - ReadOnlyException");
    
    tjc's avatar
    tjc committed
        throw new ReadOnlyException("this Database Document can not be written to");
    
    tjc's avatar
    tjc committed
      }
    
    
    tjc's avatar
    tjc committed
      /**
    
    tjc's avatar
    tjc committed
       * 
       * Appends a log entry to the log file
       * 
       * @param logEntry
       *          entry to add to log file
       * @param logFileName
       *          log file name
       * 
       */
    
    tjc's avatar
    tjc committed
      private void appendToLogFile(String logEntry, String logFileName)
      {
    
    tjc's avatar
    tjc committed
        if(System.getProperty("debug") == null)
          return;
    
    
    tjc's avatar
    tjc committed
        BufferedWriter bw = null;
        try
        {
          String dat = new java.util.Date().toString();
    
          bw = new BufferedWriter(new FileWriter(logFileName, true));
    
    tjc's avatar
    tjc committed
          bw.write(dat + ":: " + logEntry);
    
    tjc's avatar
    tjc committed
          bw.newLine();
          bw.flush();
        }
    
    tjc's avatar
    tjc committed
        catch(Exception ioe)
    
    tjc's avatar
    tjc committed
        {
    
    tjc's avatar
    tjc committed
          System.out.println("Error writing to log file " + logFileName);
    
    tjc's avatar
    tjc committed
          ioe.printStackTrace();
        }
    
    tjc's avatar
    tjc committed
        finally
        // always close the file
    
    tjc's avatar
    tjc committed
        {
          if(bw != null)
    
    tjc's avatar
    tjc committed
            try
            {
              bw.close();
            }
            catch(IOException ioe2)
            {
            }
    
    tjc's avatar
    tjc committed
        }
      }
    
    
    tjc's avatar
    tjc committed
      public void commit(Vector sql)
      {
        try
        {
          Connection conn = getConnection();
          int row = 0;
    
    
    tjc's avatar
    tjc committed
          for(int i = 0; i < sql.size(); i++)
    
    tjc's avatar
    tjc committed
          {
    
    tjc's avatar
    tjc committed
            ChadoTransaction tsn = (ChadoTransaction) sql.get(i);
    
            String[] sql_array = tsn.getSqlQuery();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
            for(int j = 0; j < sql_array.length; j++)
    
            {
              System.out.println(sql_array[j]);
    
              Statement st = conn.createStatement();
              row += st.executeUpdate(sql_array[j]);
            }
    
    tjc's avatar
    tjc committed
          }
    
          conn.close();
        }
    
    tjc's avatar
    tjc committed
        catch (java.sql.SQLException sqlExp)
    
    tjc's avatar
    tjc committed
        {
          sqlExp.printStackTrace();
        }
    
    tjc's avatar
    tjc committed
        catch (java.net.ConnectException conn)
    
    tjc's avatar
    tjc committed
        {