Merge branch 'develop' into merged_2_11_2_0_to_2_12
[jalview.git] / src / jalview / ws / dbsources / Uniprot.java
index b9fe52f..567626d 100644 (file)
@@ -21,7 +21,6 @@
 package jalview.ws.dbsources;
 
 import java.util.Locale;
-
 import jalview.bin.Cache;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
@@ -33,6 +32,7 @@ import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
 import jalview.schemes.ResidueProperties;
 import jalview.util.StringUtils;
+import jalview.util.Platform;
 import jalview.ws.seqfetcher.DbSourceProxyImpl;
 import jalview.xml.binding.uniprot.DbReferenceType;
 import jalview.xml.binding.uniprot.Entry;
@@ -71,6 +71,7 @@ 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;
 
   /**
    * Constructor
@@ -104,7 +105,12 @@ public class Uniprot extends DbSourceProxyImpl
   @Override
   public Regex getAccessionValidator()
   {
-    return new Regex("([A-Z]+[0-9]+[A-Z0-9]+|[A-Z0-9]+_[A-Z0-9]+)");
+    if (ACCESSION_REGEX == null)
+    {
+      ACCESSION_REGEX = Platform
+              .newRegex("([A-Z]+[0-9]+[A-Z0-9]+|[A-Z0-9]+_[A-Z0-9]+)");
+    }
+    return ACCESSION_REGEX;
   }
 
   /*
@@ -144,31 +150,31 @@ public class Uniprot extends DbSourceProxyImpl
               "(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);
-      HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
+      HttpURLConnection urlconn = (HttpURLConnection) url.openConnection();
       // anything other than 200 means we don't have data
-      // TODO: JAL-3882 reuse the EnsemblRestClient's fair 
+      // 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)
       {
         InputStream istr = urlconn.getInputStream();
         List<Entry> entries = getUniprotEntries(istr);
         if (entries != null)
-        {
+       {
           List<SequenceI> seqs = new ArrayList<>();
-          for (Entry entry : entries)
+          for (Entry entry: entries)
           {
             seqs.add(uniprotEntryToSequence(entry));
           }
           al = new Alignment(seqs.toArray(new SequenceI[seqs.size()]));
-        }
+       }
       }
+
       stopQuery();
       return al;
-      
+
     } catch (Exception e)
     {
       throw (e);
@@ -193,8 +199,7 @@ public class Uniprot extends DbSourceProxyImpl
     String seqString = entry.getSequence().getValue().replaceAll("\\s*",
             "");
 
-    SequenceI sequence = new Sequence(id,
-            seqString);
+    SequenceI sequence = new Sequence(id, seqString);
     sequence.setDescription(getUniprotEntryDescription(entry));
 
     /*
@@ -202,12 +207,12 @@ public class Uniprot extends DbSourceProxyImpl
      */
     final String dbVersion = getDbVersion();
     List<DBRefEntry> dbRefs = new ArrayList<>();
-    boolean canonical=true;
+    boolean canonical = true;
     for (String accessionId : entry.getAccession())
     {
       DBRefEntry dbRef = new DBRefEntry(DBRefSource.UNIPROT, dbVersion,
-              accessionId,null,canonical);
-      canonical=false;
+              accessionId, null, canonical);
+      canonical = false;
       dbRefs.add(dbRef);
     }
 
@@ -512,10 +517,12 @@ public class Uniprot extends DbSourceProxyImpl
       XMLStreamReader streamReader = XMLInputFactory.newInstance()
               .createXMLStreamReader(is);
       javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
-      JAXBElement<jalview.xml.binding.uniprot.Uniprot> uniprotElement = 
-                 um.unmarshal(streamReader, jalview.xml.binding.uniprot.Uniprot.class);
-      jalview.xml.binding.uniprot.Uniprot uniprot = uniprotElement.getValue();
-      
+      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();
@@ -523,7 +530,10 @@ 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]"))
+      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;