JAL-2353 primary ref candidates that should be promoted should preserve original...
[jalview.git] / src / jalview / ws / dbsources / Uniprot.java
index 74a9ea4..c9db7f2 100644 (file)
  */
 package jalview.ws.dbsources;
 
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+import java.util.Vector;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.JAXBException;
+import javax.xml.stream.FactoryConfigurationError;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import com.stevesoft.pat.Regex;
+
 import jalview.bin.Cache;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
@@ -39,22 +57,6 @@ import jalview.xml.binding.uniprot.LocationType;
 import jalview.xml.binding.uniprot.PositionType;
 import jalview.xml.binding.uniprot.PropertyType;
 
-import java.io.InputStream;
-import java.net.URL;
-import java.net.URLConnection;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Vector;
-
-import javax.xml.bind.JAXBContext;
-import javax.xml.bind.JAXBException;
-import javax.xml.stream.FactoryConfigurationError;
-import javax.xml.stream.XMLInputFactory;
-import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamReader;
-
-import com.stevesoft.pat.Regex;
-
 /**
  * This class queries the Uniprot database for sequence data, unmarshals the
  * returned XML, and converts it to Jalview Sequence records (including attached
@@ -137,29 +139,34 @@ public class Uniprot extends DbSourceProxyImpl
     startQuery();
     try
     {
-      queries = queries.toUpperCase().replaceAll(
+      queries = queries.toUpperCase(Locale.ROOT).replaceAll(
               "(UNIPROT\\|?|UNIPROT_|UNIREF\\d+_|UNIREF\\d+\\|?)", "");
       AlignmentI al = null;
 
-      String downloadstring = getDomain() + "/uniprot/" + queries
-              + ".xml";
+      String downloadstring = getDomain() + "/uniprot/" + queries + ".xml";
 
       URL url = new URL(downloadstring);
-      URLConnection urlconn = url.openConnection();
-      InputStream istr = urlconn.getInputStream();
-      List<Entry> entries = getUniprotEntries(istr);
-      if (entries != null)
+      HttpURLConnection urlconn = (HttpURLConnection) url.openConnection();
+      // anything other than 200 means we don't have data
+      // TODO: JAL-3882 reuse the EnsemblRestClient's fair
+      // use/backoff logic to retry when the server tells us to go away
+      if (urlconn.getResponseCode() == 200)
       {
-        List<SequenceI> seqs = new ArrayList<>();
-        for (Entry entry : entries)
+        InputStream istr = urlconn.getInputStream();
+        List<Entry> entries = getUniprotEntries(istr);
+        if (entries != null)
         {
-          seqs.add(uniprotEntryToSequence(entry));
+          List<SequenceI> seqs = new ArrayList<>();
+          for (Entry entry : entries)
+          {
+            seqs.add(uniprotEntryToSequence(entry));
+          }
+          al = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
         }
-        al = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
       }
-
       stopQuery();
       return al;
+
     } catch (Exception e)
     {
       throw (e);
@@ -178,29 +185,26 @@ public class Uniprot extends DbSourceProxyImpl
   SequenceI uniprotEntryToSequence(Entry entry)
   {
     String id = getUniprotEntryId(entry);
-    String seqString = entry.getSequence().getValue();
-
     /*
-     * for backwards compatibility with Castor processing,
-     * remove any internal spaces
+     * Sequence should not include any whitespace, but JAXB leaves these in
      */
-    if (seqString.indexOf(' ') > -1)
-    {
-      seqString = seqString.replaceAll(" ", "");
-    }
-    SequenceI sequence = new Sequence(id,
-            seqString);
-    sequence.setDescription(getUniprotEntryDescription(entry));
+    String seqString = entry.getSequence().getValue().replaceAll("\\s*",
+            "");
 
+    SequenceI sequence = new Sequence(id, seqString);
+    sequence.setDescription(getUniprotEntryDescription(entry));
+    final String uniprotRecordVersion = "" + entry.getVersion();
     /*
      * add a 'self' DBRefEntry for each accession
      */
     final String dbVersion = getDbVersion();
     List<DBRefEntry> dbRefs = new ArrayList<>();
+    boolean canonical = true;
     for (String accessionId : entry.getAccession())
     {
-      DBRefEntry dbRef = new DBRefEntry(DBRefSource.UNIPROT, dbVersion,
-              accessionId);
+      DBRefEntry dbRef = new DBRefEntry(DBRefSource.UNIPROT,
+              uniprotRecordVersion, accessionId, null, canonical);
+      canonical = false;
       dbRefs.add(dbRef);
     }
 
@@ -237,13 +241,23 @@ public class Uniprot extends DbSourceProxyImpl
           // remove version
           String[] vrs = cdsId.split("\\.");
           String version = vrs.length > 1 ? vrs[1]
-                  : DBRefSource.UNIPROT + ":" + dbVersion;
+                  : DBRefSource.UNIPROT + ":" + uniprotRecordVersion;
           dbr = new DBRefEntry(DBRefSource.EMBLCDS, version, vrs[0]);
+          // TODO: process VARIANT features to allow EMBLCDS record's product to
+          // match Uniprot
+          dbr.setCanonical(true);
           dbRefs.add(dbr);
         }
       }
-      if ("Ensembl".equals(type))
+      if (type != null
+              && type.toLowerCase(Locale.ROOT).startsWith("ensembl"))
       {
+        // remove version
+        String[] vrs = dbref.getId().split("\\.");
+        String version = vrs.length > 1 ? vrs[1]
+                : DBRefSource.UNIPROT + ":" + uniprotRecordVersion;
+        dbr.setAccessionId(vrs[0]);
+        dbr.setVersion(version);
         /*
          * e.g. Uniprot accession Q9BXM7 has
          * <dbReference type="Ensembl" id="ENST00000321556">
@@ -256,8 +270,12 @@ public class Uniprot extends DbSourceProxyImpl
                 "protein sequence ID");
         if (cdsId != null && cdsId.trim().length() > 0)
         {
+          // remove version
+          String[] cdsVrs = cdsId.split("\\.");
+          String cdsVersion = cdsVrs.length > 1 ? cdsVrs[1]
+                  : DBRefSource.UNIPROT + ":" + uniprotRecordVersion;
           dbr = new DBRefEntry(DBRefSource.ENSEMBL,
-                  DBRefSource.UNIPROT + ":" + dbVersion, cdsId.trim());
+                  DBRefSource.UNIPROT + ":" + cdsVersion, cdsVrs[0]);
           dbRefs.add(dbr);
         }
       }
@@ -505,7 +523,12 @@ public class Uniprot extends DbSourceProxyImpl
       XMLStreamReader streamReader = XMLInputFactory.newInstance()
               .createXMLStreamReader(is);
       javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
-      jalview.xml.binding.uniprot.Uniprot uniprot = (jalview.xml.binding.uniprot.Uniprot) um.unmarshal(streamReader);
+      JAXBElement<jalview.xml.binding.uniprot.Uniprot> uniprotElement = um
+              .unmarshal(streamReader,
+                      jalview.xml.binding.uniprot.Uniprot.class);
+      jalview.xml.binding.uniprot.Uniprot uniprot = uniprotElement
+              .getValue();
+
       if (uniprot != null && !uniprot.getEntry().isEmpty())
       {
         entries = uniprot.getEntry();
@@ -513,6 +536,14 @@ public class Uniprot extends DbSourceProxyImpl
     } catch (JAXBException | XMLStreamException
             | FactoryConfigurationError e)
     {
+      if (e instanceof javax.xml.bind.UnmarshalException
+              && e.getCause() != null
+              && e.getCause() instanceof XMLStreamException
+              && e.getCause().getMessage().contains("[row,col]:[1,1]"))
+      {
+        // trying to parse an empty stream
+        return null;
+      }
       e.printStackTrace();
     }
     return entries;