JAL-1920 JAL-3017 use newline not <br/> in description and update test to cover multi...
[jalview.git] / src / jalview / ws / dbsources / Uniprot.java
index caed598..274ad32 100644 (file)
@@ -20,6 +20,7 @@
  */
 package jalview.ws.dbsources;
 
+import jalview.bin.Cache;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.DBRefEntry;
@@ -28,16 +29,20 @@ import jalview.datamodel.PDBEntry;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
-import jalview.datamodel.UniprotEntry;
-import jalview.datamodel.UniprotFile;
-import jalview.ws.ebi.EBIFetchClient;
+import jalview.datamodel.xdb.uniprot.UniprotEntry;
+import jalview.datamodel.xdb.uniprot.UniprotFeature;
+import jalview.datamodel.xdb.uniprot.UniprotFile;
+import jalview.schemes.ResidueProperties;
+import jalview.util.StringUtils;
 import jalview.ws.seqfetcher.DbSourceProxyImpl;
 
-import java.io.File;
-import java.io.FileReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.Reader;
 import java.net.URL;
+import java.net.URLConnection;
 import java.util.ArrayList;
+import java.util.List;
 import java.util.Vector;
 
 import org.exolab.castor.mapping.Mapping;
@@ -51,6 +56,8 @@ import com.stevesoft.pat.Regex;
  */
 public class Uniprot extends DbSourceProxyImpl
 {
+  private static final String DEFAULT_UNIPROT_DOMAIN = "https://www.uniprot.org";
+
   private static final String BAR_DELIMITER = "|";
 
   /*
@@ -66,6 +73,11 @@ public class Uniprot extends DbSourceProxyImpl
     super();
   }
 
+  private String getDomain()
+  {
+    return Cache.getDefault("UNIPROT_DOMAIN", DEFAULT_UNIPROT_DOMAIN);
+  }
+
   /*
    * (non-Javadoc)
    * 
@@ -161,16 +173,21 @@ public class Uniprot extends DbSourceProxyImpl
       queries = queries.toUpperCase().replaceAll(
               "(UNIPROT\\|?|UNIPROT_|UNIREF\\d+_|UNIREF\\d+\\|?)", "");
       AlignmentI al = null;
-      EBIFetchClient ebi = new EBIFetchClient();
-      // uniprotxml parameter required since december 2007
-      // uniprotkb dbname changed introduced december 2008
-      File file = ebi.fetchDataAsFile("uniprotkb:" + queries, "uniprotxml",
-              ".xml");
-      Vector<UniprotEntry> entries = getUniprotEntries(new FileReader(file));
+
+      String downloadstring = getDomain() + "/uniprot/" + queries
+              + ".xml";
+      URL url = null;
+      URLConnection urlconn = null;
+
+      url = new URL(downloadstring);
+      urlconn = url.openConnection();
+      InputStream istr = urlconn.getInputStream();
+      Vector<UniprotEntry> entries = getUniprotEntries(
+              new InputStreamReader(istr, "UTF-8"));
 
       if (entries != null)
       {
-        ArrayList<SequenceI> seqs = new ArrayList<SequenceI>();
+        ArrayList<SequenceI> seqs = new ArrayList<>();
         for (UniprotEntry entry : entries)
         {
           seqs.add(uniprotEntryToSequenceI(entry));
@@ -182,8 +199,10 @@ public class Uniprot extends DbSourceProxyImpl
       return al;
     } catch (Exception e)
     {
-      stopQuery();
       throw (e);
+    } finally
+    {
+      stopQuery();
     }
   }
 
@@ -193,14 +212,15 @@ public class Uniprot extends DbSourceProxyImpl
    *          UniprotEntry
    * @return SequenceI instance created from the UniprotEntry instance
    */
-  public SequenceI uniprotEntryToSequenceI(UniprotEntry entry){
+  public SequenceI uniprotEntryToSequenceI(UniprotEntry entry)
+  {
     String id = getUniprotEntryId(entry);
-    SequenceI sequence = new Sequence(id, entry.getUniprotSequence()
-            .getContent());
+    SequenceI sequence = new Sequence(id,
+            entry.getUniprotSequence().getContent());
     sequence.setDescription(getUniprotEntryDescription(entry));
 
     final String dbVersion = getDbVersion();
-    ArrayList<DBRefEntry> dbRefs = new ArrayList<DBRefEntry>();
+    ArrayList<DBRefEntry> dbRefs = new ArrayList<>();
     for (String accessionId : entry.getAccession())
     {
       DBRefEntry dbRef = new DBRefEntry(DBRefSource.UNIPROT, dbVersion,
@@ -210,7 +230,7 @@ public class Uniprot extends DbSourceProxyImpl
       dbRefs.add(dbRef);
     }
 
-    Vector<PDBEntry> onlyPdbEntries = new Vector<PDBEntry>();
+    Vector<PDBEntry> onlyPdbEntries = new Vector<>();
     for (PDBEntry pdb : entry.getDbReference())
     {
       DBRefEntry dbr = new DBRefEntry();
@@ -247,22 +267,23 @@ public class Uniprot extends DbSourceProxyImpl
         String cdsId = (String) pdb.getProperty("protein sequence ID");
         if (cdsId != null && cdsId.trim().length() > 0)
         {
-          dbr = new DBRefEntry(DBRefSource.ENSEMBL, DBRefSource.UNIPROT
-                  + ":" + dbVersion, cdsId.trim());
+          dbr = new DBRefEntry(DBRefSource.ENSEMBL,
+                  DBRefSource.UNIPROT + ":" + dbVersion, cdsId.trim());
           dbRefs.add(dbr);
 
         }
       }
-
     }
 
     sequence.setPDBId(onlyPdbEntries);
     if (entry.getFeature() != null)
     {
-      for (SequenceFeature sf : entry.getFeature())
+      for (UniprotFeature uf : entry.getFeature())
       {
-        sf.setFeatureGroup("Uniprot");
-        sequence.addSequenceFeature(sf);
+        SequenceFeature copy = new SequenceFeature(uf.getType(),
+                getDescription(uf), uf.getBegin(), uf.getEnd(), "Uniprot");
+        copy.setStatus(uf.getStatus());
+        sequence.addSequenceFeature(copy);
       }
     }
     for (DBRefEntry dbr : dbRefs)
@@ -273,6 +294,68 @@ public class Uniprot extends DbSourceProxyImpl
   }
 
   /**
+   * Constructs a feature description from the description and (optionally)
+   * original and variant fields of the Uniprot XML feature
+   * 
+   * @param uf
+   * @return
+   */
+  protected static String getDescription(UniprotFeature uf)
+  {
+    String orig = uf.getOriginal();
+    List<String> variants = uf.getVariation();
+    StringBuilder sb = new StringBuilder();
+
+    /*
+     * append variant in standard format if present
+     * e.g. p.Arg59Lys
+     */
+    if (orig != null && !orig.isEmpty() && variants != null
+            && !variants.isEmpty())
+    {
+      int p = 0;
+      for (String var : variants)
+      {
+        // TODO proper HGVC nomenclature for delins structural variations
+        sb.append("p.");
+        for (int c = 0, clen = orig.length(); c < clen; c++)
+        {
+          char origchar = orig.charAt(c);
+          String orig3 = ResidueProperties.aa2Triplet.get("" + origchar);
+          sb.append(orig3 == null ? origchar
+                  : StringUtils.toSentenceCase(orig3));
+        }
+
+        sb.append(Integer.toString(uf.getPosition()));
+
+        for (int c = 0, clen = var.length(); c < clen; c++)
+        {
+          char varchar = var.charAt(c);
+          String var3 = ResidueProperties.aa2Triplet.get("" + varchar);
+
+          sb.append(var3 != null ? StringUtils.toSentenceCase(var3)
+                  : "" + varchar);
+        }
+        if (++p != variants.size())
+        {
+          sb.append("\n");
+        }
+        else
+        {
+          sb.append(" ");
+        }
+      }
+    }
+    String description = uf.getDescription();
+    if (description != null)
+    {
+      sb.append(description);
+    }
+
+    return sb.toString();
+  }
+
+  /**
    * 
    * @param entry
    *          UniportEntry
@@ -300,23 +383,18 @@ public class Uniprot extends DbSourceProxyImpl
   /**
    *
    * @param entry
-   *          UniportEntry
+   *          UniprotEntry
    * @return The accession id(s) and name(s) delimited by '|'.
    */
   public static String getUniprotEntryId(UniprotEntry entry)
   {
     StringBuilder name = new StringBuilder(32);
-    // name.append("UniProt/Swiss-Prot");
-    // use 'canonicalised' name for optimal id matching
-    name.append(DBRefSource.UNIPROT);
-    for (String accessionId : entry.getAccession())
-    {
-      name.append(BAR_DELIMITER);
-      name.append(accessionId);
-    }
     for (String n : entry.getName())
     {
-      name.append(BAR_DELIMITER);
+      if (name.length() > 0)
+      {
+        name.append(BAR_DELIMITER);
+      }
       name.append(n);
     }
     return name.toString();