JAL-2758 flag and tests for omitting uniprot accession prefix from sequence names
[jalview.git] / src / jalview / ws / dbsources / Uniprot.java
index c868576..2452aed 100644 (file)
@@ -31,13 +31,13 @@ import jalview.datamodel.SequenceI;
 import jalview.datamodel.xdb.uniprot.UniprotEntry;
 import jalview.datamodel.xdb.uniprot.UniprotFeature;
 import jalview.datamodel.xdb.uniprot.UniprotFile;
-import jalview.ws.ebi.EBIFetchClient;
 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.Vector;
 
@@ -60,6 +60,11 @@ public class Uniprot extends DbSourceProxyImpl
   private static Mapping map;
 
   /**
+   * configurable parameter controlling prefixing of entry names with accessions
+   */
+  private boolean includeAllIds = false;
+
+  /**
    * Constructor
    */
   public Uniprot()
@@ -162,17 +167,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");
+
+      String downloadstring = "http://www.uniprot.org/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 FileReader(file));
+              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));
@@ -184,8 +193,10 @@ public class Uniprot extends DbSourceProxyImpl
       return al;
     } catch (Exception e)
     {
-      stopQuery();
       throw (e);
+    } finally
+    {
+      stopQuery();
     }
   }
 
@@ -197,13 +208,13 @@ public class Uniprot extends DbSourceProxyImpl
    */
   public SequenceI uniprotEntryToSequenceI(UniprotEntry entry)
   {
-    String id = getUniprotEntryId(entry);
+    String id = getUniprotEntryId(entry, includeAllIds);
     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,
@@ -213,7 +224,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();
@@ -307,20 +318,27 @@ public class Uniprot extends DbSourceProxyImpl
    *          UniportEntry
    * @return The accession id(s) and name(s) delimited by '|'.
    */
-  public static String getUniprotEntryId(UniprotEntry entry)
+  public static String getUniprotEntryId(UniprotEntry entry,
+          boolean includeAllIds)
   {
     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())
+    if (includeAllIds)
     {
-      name.append(BAR_DELIMITER);
-      name.append(accessionId);
+      // // 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();