JAL-3878 Create SlivkaAnnotationWSClient
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Mon, 11 Apr 2022 16:12:42 +0000 (18:12 +0200)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Mon, 11 Apr 2022 16:12:42 +0000 (18:12 +0200)
src/jalview/ws2/client/slivka/SlivkaWSClient.java

index 16c9d3a..b32c188 100644 (file)
@@ -6,14 +6,23 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.EnumMap;
 import java.util.List;
+import java.util.Map;
 import java.util.regex.Pattern;
 
+import jalview.api.FeatureColourI;
 import jalview.bin.Cache;
+import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.SequenceI;
+import jalview.datamodel.features.FeatureMatcherSetI;
+import jalview.io.AnnotationFile;
 import jalview.io.DataSourceType;
+import jalview.io.FeaturesFile;
 import jalview.io.FileFormat;
 import jalview.io.FormatAdapter;
 import jalview.ws.params.ArgumentI;
@@ -21,12 +30,15 @@ import jalview.ws2.api.Credentials;
 import jalview.ws2.api.JobStatus;
 import jalview.ws2.api.WebServiceJobHandle;
 import jalview.ws2.client.api.AlignmentWebServiceClientI;
+import jalview.ws2.client.api.AnnotationWebServiceClientI;
 import jalview.ws2.client.api.WebServiceClientI;
 import uk.ac.dundee.compbio.slivkaclient.Job;
 import uk.ac.dundee.compbio.slivkaclient.Parameter;
 import uk.ac.dundee.compbio.slivkaclient.SlivkaClient;
 import uk.ac.dundee.compbio.slivkaclient.SlivkaService;
 
+import static java.lang.String.format;
+
 public class SlivkaWSClient implements WebServiceClientI
 {
   final SlivkaService service;
@@ -52,7 +64,8 @@ public class SlivkaWSClient implements WebServiceClientI
   }
 
   // pattern for matching media types
-  static final Pattern mediaTypePattern = Pattern.compile("(?:text|application)\\/(?:x-)?(\\w+)");
+  static final Pattern mediaTypePattern = Pattern.compile(
+      "(?:text|application)\\/(?:x-)?([\\w-]+)");
 
   @Override
   public WebServiceJobHandle submit(List<SequenceI> sequences,
@@ -226,4 +239,53 @@ class SlivkaAlignmentWSClient extends SlivkaWSClient
     throw new IOException("no alignment found");
   }
 
-}
\ No newline at end of file
+}
+
+class SlivkaAnnotationWSClient extends SlivkaWSClient
+    implements AnnotationWebServiceClientI
+{
+  SlivkaAnnotationWSClient(SlivkaService service)
+  {
+    super(service);
+  }
+
+  @Override
+  public List<AlignmentAnnotation> attachAnnotations(WebServiceJobHandle job,
+      List<SequenceI> sequences, Map<String, FeatureColourI> colours,
+      Map<String, FeatureMatcherSetI> filters) throws IOException
+  {
+    var slivkaJob = client.getJob(job.getJobId());
+    var aln = new Alignment(sequences.toArray(new SequenceI[sequences.size()]));
+    boolean featPresent = false, annotPresent = false;
+    for (var f : slivkaJob.getResults())
+    {
+      // TODO: restrict file label to "annotations" or "features"
+      var match = mediaTypePattern.matcher(f.getMediaType());
+      if (!match.find())
+        continue;
+      String fmt = match.group(1);
+      if (fmt.equalsIgnoreCase("jalview-annotations"))
+      {
+        annotPresent = new AnnotationFile().readAnnotationFileWithCalcId(
+            aln, service.getId(), f.getContentUrl().toString(),
+            DataSourceType.URL);
+        if (annotPresent)
+          Cache.log.debug(format("loaded annotations for %s", service.getId()));
+      }
+      else if (fmt.equalsIgnoreCase("jalview-features"))
+      {
+        FeaturesFile ff = new FeaturesFile(f.getContentUrl().toString(),
+            DataSourceType.URL);
+        featPresent = ff.parse(aln, colours, true);
+        if (featPresent)
+          Cache.log.debug(format("loaded features for %s", service.getId()));
+      }
+    }
+    if (!annotPresent)
+      Cache.log.debug(format("no annotations found for %s", service.getId()));
+    if (!featPresent)
+      Cache.log.debug(format("no features found for %s", service.getId()));
+    return aln.getAlignmentAnnotation() != null ? Arrays.asList(aln.getAlignmentAnnotation())
+        : Collections.emptyList();
+  }
+}