From 4cfcb454419bf016fc833f104a796c788f2f0c22 Mon Sep 17 00:00:00 2001
From: tjc <tjc@ee4ac58c-ac51-4696-9907-e4b3aa274f04>
Date: Fri, 21 Apr 2006 09:26:22 +0000
Subject: [PATCH] synonym retrieval from chado

git-svn-id: svn+ssh://svn.internal.sanger.ac.uk/repos/svn/pathsoft/artemis/trunk@4286 ee4ac58c-ac51-4696-9907-e4b3aa274f04
---
 uk/ac/sanger/artemis/chado/Alias.java         | 78 +++++++++++++++++++
 uk/ac/sanger/artemis/chado/ChadoDAO.java      | 17 ++--
 uk/ac/sanger/artemis/chado/Dbxref.java        |  3 +-
 uk/ac/sanger/artemis/chado/IBatisDAO.java     | 36 +++++++++
 uk/ac/sanger/artemis/chado/JdbcDAO.java       | 40 ++++++++++
 uk/ac/sanger/artemis/io/GFFStreamFeature.java |  6 +-
 .../sanger/artemis/util/DatabaseDocument.java | 16 ++++
 7 files changed, 186 insertions(+), 10 deletions(-)
 create mode 100644 uk/ac/sanger/artemis/chado/Alias.java

diff --git a/uk/ac/sanger/artemis/chado/Alias.java b/uk/ac/sanger/artemis/chado/Alias.java
new file mode 100644
index 000000000..1058556d9
--- /dev/null
+++ b/uk/ac/sanger/artemis/chado/Alias.java
@@ -0,0 +1,78 @@
+/* 
+ *
+ * created: 2006
+ *
+ * This file is part of Artemis
+ *
+ * Copyright (C) 2006  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.
+ *
+ */
+
+package uk.ac.sanger.artemis.chado;
+
+/**
+ * Alias GFF tags - values are stored in the synonym table and
+ * linked to feature via feature_synonym table.
+ */
+public class Alias
+{
+  private Integer feature_id;
+  private String name;
+  private String schema;
+  private String uniquename;
+  
+  public String getSchema()
+  {
+    return schema;
+  }
+
+  public void setSchema(String schema)
+  {
+    this.schema = schema;
+  }
+
+  public String getUniquename()
+  {
+    return uniquename;
+  }
+
+  public void setUniquename(String uniquename)
+  {
+    this.uniquename = uniquename;
+  }
+
+  public Integer getFeature_id()
+  {
+    return feature_id;
+  }
+  
+  public void setFeature_id(Integer feature_id)
+  {
+    this.feature_id = feature_id;
+  }
+  
+  public String getName()
+  {
+    return name;
+  }
+  
+  public void setName(String name)
+  {
+    this.name = name;
+  }
+
+}
diff --git a/uk/ac/sanger/artemis/chado/ChadoDAO.java b/uk/ac/sanger/artemis/chado/ChadoDAO.java
index 90642635b..9fead21aa 100644
--- a/uk/ac/sanger/artemis/chado/ChadoDAO.java
+++ b/uk/ac/sanger/artemis/chado/ChadoDAO.java
@@ -145,7 +145,6 @@ public interface ChadoDAO
   
 
   /**
-   * 
    * Get dbxref for a feature.
    * @param schema      the postgres schema name
    * @param uniquename  the unique name for the feature. If set to NULL
@@ -156,11 +155,21 @@ public interface ChadoDAO
   public Hashtable getDbxref(final String schema, final String uniquename)
               throws SQLException;
   
+  /**
+   * Get dbxref for a feature.
+   * @param schema      the postgres schema name
+   * @param uniquename  the unique name for the feature. If set to NULL
+   *                    all <code>Dbxref</code> are returned.
+   * @return a <code>Hashtable</code> of dbxrefs.
+   * @throws SQLException
+   */
+  public Hashtable getAlias(final String schema, final String uniquename)
+              throws SQLException;
+  
 //
 // WRITE BACK
 //
   /**
-   *
    * Update attributes defined by the <code>ChadoTransaction</code>.
    * @param schema	schema/organism name or null
    * @param tsn		the <code>ChadoTransaction</code>
@@ -172,7 +181,6 @@ public interface ChadoDAO
                      throws SQLException;
 
   /**
-   *
    * Insert attributes defined by the <code>ChadoTransaction</code>.
    * @param schema      schema/organism name or null
    * @param tsn         the <code>ChadoTransaction</code>
@@ -183,7 +191,6 @@ public interface ChadoDAO
                      throws SQLException;
 
   /**
-   *
    * Delete attributes defined by the <code>ChadoTransaction</code>.
    * @param schema      schema/organism name or null
    * @param tsn         the <code>ChadoTransaction</code>
@@ -194,7 +201,6 @@ public interface ChadoDAO
                      throws SQLException;
 
   /**
-   *
    * Insert a feature into the database defined by the <code>ChadoTransaction</code>.
    * @param schema       	schema/organism name or null
    * @param tsn         	the <code>ChadoTransaction</code>
@@ -207,7 +213,6 @@ public interface ChadoDAO
                      throws SQLException;
 
   /**
-   *
    * Delete a feature from the database defined by the <code>ChadoTransaction</code>.
    * @param schema 	schema/organism name or null
    * @param tsn         the <code>ChadoTransaction</code>
diff --git a/uk/ac/sanger/artemis/chado/Dbxref.java b/uk/ac/sanger/artemis/chado/Dbxref.java
index 6924e9f90..60ac5c987 100644
--- a/uk/ac/sanger/artemis/chado/Dbxref.java
+++ b/uk/ac/sanger/artemis/chado/Dbxref.java
@@ -26,7 +26,8 @@ package uk.ac.sanger.artemis.chado;
 
 /**
  *
- * Java Object used by ORM to represent the chado feature_dbxref table.
+ * Java Object used by ORM to represent the chado feature_dbxref,
+ * dbxref and db tables.
  *
  */
 public class Dbxref
diff --git a/uk/ac/sanger/artemis/chado/IBatisDAO.java b/uk/ac/sanger/artemis/chado/IBatisDAO.java
index 8664cea1b..42eab274a 100644
--- a/uk/ac/sanger/artemis/chado/IBatisDAO.java
+++ b/uk/ac/sanger/artemis/chado/IBatisDAO.java
@@ -268,6 +268,42 @@ public class IBatisDAO implements ChadoDAO
     return JdbcDAO.mergeDbxref(list);
   }
   
+  /**
+   * Get dbxref for a feature.
+   * @param schema      the postgres schema name
+   * @param uniquename  the unique name for the feature. If set to NULL
+   *                    all <code>Dbxref</code> are returned.
+   * @return a <code>Hashtable</code> of dbxrefs.
+   * @throws SQLException
+   */
+  public Hashtable getAlias(final String schema, final String uniquename)
+              throws SQLException
+  {
+    Alias alias = new Alias();
+    alias.setSchema(schema);
+    alias.setUniquename(uniquename);
+    SqlMapClient sqlMap = DbSqlConfig.getSqlMapInstance();
+    List list = sqlMap.queryForList("getAlias", alias);  
+    
+    Hashtable synonym = new Hashtable();
+    Integer feature_id;
+    Vector value;
+    for(int i=0; i<list.size(); i++)
+    {
+      alias = (Alias)list.get(i);
+      feature_id = alias.getFeature_id();
+      if(synonym.containsKey(feature_id))
+        value = (Vector)synonym.get(feature_id);
+      else
+        value = new Vector();
+      
+      value.add(alias.getName());
+      synonym.put(feature_id, value);
+    }
+    
+    return synonym;
+  }
+  
   /**
    *
    * @param name cvterm name
diff --git a/uk/ac/sanger/artemis/chado/JdbcDAO.java b/uk/ac/sanger/artemis/chado/JdbcDAO.java
index f61a973b9..cbed0a166 100644
--- a/uk/ac/sanger/artemis/chado/JdbcDAO.java
+++ b/uk/ac/sanger/artemis/chado/JdbcDAO.java
@@ -457,6 +457,46 @@ public class JdbcDAO
     return mergeDbxref(dbxrefs);
   }
   
+  /**
+   * Get dbxref for a feature.
+   * @param schema      the postgres schema name
+   * @param uniquename  the unique name for the feature. If set to NULL
+   *                    all synonyms are returned.
+   * @return a <code>Hashtable</code> of synonym values with the 
+   *         feature_id as the key.
+   * @throws SQLException
+   */
+  public Hashtable getAlias(final String schema, final String uniquename)
+              throws SQLException
+  {
+    String sql = "SELECT s.name, f.feature_id FROM "+schema+".feature_synonym fs "+
+                 "LEFT JOIN "+schema+".feature f ON f.feature_id=fs.feature_id "+
+                 "LEFT JOIN "+schema+".synonym s ON fs.synonym_id=s.synonym_id ";
+    
+    if(uniquename != null)
+      sql = sql + "WHERE uniquename='"+uniquename+"'";
+    
+    appendToLogFile(sql, sqlLog);
+    Statement st = conn.createStatement();
+    ResultSet rs = st.executeQuery(sql);
+    Hashtable synonym = new Hashtable();
+    Integer feature_id;
+    Vector value;
+    while(rs.next())
+    {
+      feature_id = new Integer(rs.getInt("feature_id"));
+      if(synonym.containsKey(feature_id))
+        value = (Vector)synonym.get(feature_id);
+      else
+        value = new Vector();
+      
+      value.add(rs.getString("name"));
+      synonym.put(feature_id, value);
+    }
+    
+    return synonym;
+  }
+  
   /**
    * Get the time a feature was last modified.
    * @param schema      schema/organism name or null
diff --git a/uk/ac/sanger/artemis/io/GFFStreamFeature.java b/uk/ac/sanger/artemis/io/GFFStreamFeature.java
index 0049e7d4f..4f9de2ee8 100644
--- a/uk/ac/sanger/artemis/io/GFFStreamFeature.java
+++ b/uk/ac/sanger/artemis/io/GFFStreamFeature.java
@@ -20,7 +20,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  *
- * $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/io/GFFStreamFeature.java,v 1.32 2006-04-12 12:22:13 tjc Exp $
+ * $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/io/GFFStreamFeature.java,v 1.33 2006-04-21 09:26:22 tjc Exp $
  */
 
 package uk.ac.sanger.artemis.io;
@@ -39,7 +39,7 @@ import java.text.SimpleDateFormat;
  *  A StreamFeature that thinks it is a GFF feature.
  *
  *  @author Kim Rutherford
- *  @version $Id: GFFStreamFeature.java,v 1.32 2006-04-12 12:22:13 tjc Exp $
+ *  @version $Id: GFFStreamFeature.java,v 1.33 2006-04-21 09:26:22 tjc Exp $
  **/
 
 public class GFFStreamFeature extends SimpleDocumentFeature
@@ -760,7 +760,7 @@ public class GFFStreamFeature extends SimpleDocumentFeature
           att_values.add(rest_of_token);
       }
 
-      if(att_name.equals("Dbxref")) // convert to multi-line
+      if(att_name.equals("Dbxref") || att_name.equals("Alias")) // convert to multi-line
       {
         StringTokenizer stok = 
             new StringTokenizer((String)att_values.get(0), ",");
diff --git a/uk/ac/sanger/artemis/util/DatabaseDocument.java b/uk/ac/sanger/artemis/util/DatabaseDocument.java
index 1e16a921d..5440b4087 100644
--- a/uk/ac/sanger/artemis/util/DatabaseDocument.java
+++ b/uk/ac/sanger/artemis/util/DatabaseDocument.java
@@ -371,6 +371,9 @@ public class DatabaseDocument extends Document
 
     // get all dbrefs
     Hashtable dbxrefs = dao.getDbxref(schema, null);
+    
+    // get all synonyms
+    Hashtable synonym = dao.getAlias(schema, null);
 
     for(int i = 0; i < feature_size; i++)
     {
@@ -463,6 +466,19 @@ public class DatabaseDocument extends Document
         }
       }
       
+      if(synonym != null &&
+         synonym.containsKey(new Integer(feature_id)))
+      {
+        this_buff.append(";Alias=");
+        Vector v_synonyms = (Vector)synonym.get(new Integer(feature_id));
+        for(int j=0; j<v_synonyms.size(); j++)
+        {
+          this_buff.append((String)v_synonyms.get(j));
+          if(j<v_synonyms.size()-1)
+            this_buff.append(",");
+        }
+      }
+      
       this_buff.append("\n");
 
       progress_listener.progressMade("Read from database: " + name);
-- 
GitLab