Merge commit 'alpha/update_2_12_for_2_11_2_series_merge^2' into HEAD
[jalview.git] / src / jalview / ws / dbsources / Uniprot.java
index fa6c3c7..0775870 100644 (file)
  */
 package jalview.ws.dbsources;
 
-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.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 java.util.Locale;
 import jalview.bin.Cache;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
@@ -47,7 +31,6 @@ import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
 import jalview.schemes.ResidueProperties;
-import jalview.util.Platform;
 import jalview.util.StringUtils;
 import jalview.ws.seqfetcher.DbSourceProxyImpl;
 import jalview.xml.binding.uniprot.DbReferenceType;
@@ -57,6 +40,23 @@ 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.HttpURLConnection;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+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;
+
 /**
  * This class queries the Uniprot database for sequence data, unmarshals the
  * returned XML, and converts it to Jalview Sequence records (including attached
@@ -70,7 +70,6 @@ public class Uniprot extends DbSourceProxyImpl
   private static final String DEFAULT_UNIPROT_DOMAIN = "https://www.uniprot.org";
 
   private static final String BAR_DELIMITER = "|";
-
   private static Regex ACCESSION_REGEX;
 
   /**
@@ -146,7 +145,7 @@ 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;
 
@@ -154,10 +153,11 @@ public class Uniprot extends DbSourceProxyImpl
               + ".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)
@@ -169,6 +169,7 @@ public class Uniprot extends DbSourceProxyImpl
 
       stopQuery();
       return al;
+      
     } catch (Exception e)
     {
       throw (e);
@@ -202,10 +203,12 @@ public class Uniprot extends DbSourceProxyImpl
      */
     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);
+              accessionId,null,canonical);
+      canonical=false;
       dbRefs.add(dbRef);
     }
 
@@ -521,6 +524,11 @@ 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;