Merge branch 'develop' into update_212_Dec_merge_with_21125_chamges
[jalview.git] / src / jalview / ext / ensembl / EnsemblFeatures.java
index 6a0d67c..bc94619 100644 (file)
  */
 package jalview.ext.ensembl;
 
-import jalview.datamodel.Alignment;
-import jalview.datamodel.AlignmentI;
-import jalview.datamodel.Sequence;
-import jalview.datamodel.SequenceFeature;
-import jalview.datamodel.SequenceI;
-import jalview.io.gff.SequenceOntologyI;
-import jalview.util.JSONUtils;
-
-import java.io.BufferedReader;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -39,6 +30,14 @@ import java.util.Map;
 
 import org.json.simple.parser.ParseException;
 
+import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.Sequence;
+import jalview.datamodel.SequenceFeature;
+import jalview.datamodel.SequenceI;
+import jalview.io.gff.SequenceOntologyI;
+import jalview.util.JSONUtils;
+
 /**
  * A client for fetching and processing Ensembl feature data in GFF format by
  * calling the overlap REST service
@@ -90,12 +89,14 @@ class EnsemblFeatures extends EnsemblRestClient
   public AlignmentI getSequenceRecords(String query) throws IOException
   {
     // TODO: use a vararg String... for getSequenceRecords instead?
+
     List<String> queries = new ArrayList<>();
     queries.add(query);
     SequenceI seq = parseFeaturesJson(queries);
     if (seq == null)
-       return null;
+      return null;
     return new Alignment(new SequenceI[] { seq });
+
   }
 
   /**
@@ -106,18 +107,17 @@ class EnsemblFeatures extends EnsemblRestClient
    * @return
    */
   @SuppressWarnings("unchecked")
-private SequenceI parseFeaturesJson(List<String> queries)
+  private SequenceI parseFeaturesJson(List<String> queries)
   {
-         
-         
     SequenceI seq = new Sequence("Dummy", "");
-
     try
     {
-       
-      Iterator<Object> rvals = (Iterator<Object>) getJSON(null, queries, -1, MODE_ITERATOR, null);
+      Iterator<Object> rvals = (Iterator<Object>) getJSON(null, queries, -1,
+              MODE_ITERATOR, null);
       if (rvals == null)
-         return null;
+      {
+        return null;
+      }
       while (rvals.hasNext())
       {
         try
@@ -128,11 +128,11 @@ private SequenceI parseFeaturesJson(List<String> queries)
           int end = Integer.parseInt(obj.get("end").toString());
           String source = obj.get("source").toString();
           String strand = obj.get("strand").toString();
+          Object phase = obj.get("phase");
           String alleles = JSONUtils
                   .arrayToStringList((List<Object>) obj.get("alleles"));
-          String clinSig = JSONUtils
-                  .arrayToStringList(
-                          (List<Object>) obj.get("clinical_significance"));
+          String clinSig = JSONUtils.arrayToStringList(
+                  (List<Object>) obj.get("clinical_significance"));
 
           /*
            * convert 'variation' to 'sequence_variant', and 'cds' to 'CDS'
@@ -147,12 +147,16 @@ private SequenceI parseFeaturesJson(List<String> queries)
           {
             type = SequenceOntologyI.CDS;
           }
-          
+
           String desc = getFirstNotNull(obj, "alleles", "external_name",
                   JSON_ID);
           SequenceFeature sf = new SequenceFeature(type, desc, start, end,
                   source);
           sf.setStrand("1".equals(strand) ? "+" : "-");
+          if (phase != null)
+          {
+            sf.setPhase(phase.toString());
+          }
           setFeatureAttribute(sf, obj, "id");
           setFeatureAttribute(sf, obj, "Parent");
           setFeatureAttribute(sf, obj, "consequence_type");
@@ -160,6 +164,7 @@ private SequenceI parseFeaturesJson(List<String> queries)
           sf.setValue("clinical_significance", clinSig);
 
           seq.addSequenceFeature(sf);
+
         } catch (Throwable t)
         {
           // ignore - keep trying other features
@@ -167,38 +172,40 @@ private SequenceI parseFeaturesJson(List<String> queries)
       }
     } catch (ParseException | IOException e)
     {
-       e.printStackTrace();
+      e.printStackTrace();
       // ignore
     }
 
     return seq;
   }
 
-  
-/**
-   * Returns the first non-null attribute found (if any) as a string
+  /**
+   * Returns the first non-null attribute found (if any) as a string, formatted
+   * suitably for display as feature description or tooltip. Answers null if
+   * none of the attribute keys is present.
    * 
    * @param obj
    * @param keys
    * @return
    */
+  @SuppressWarnings("unchecked")
   protected String getFirstNotNull(Map<String, Object> obj, String... keys)
   {
-    String desc = null;
-
     for (String key : keys)
     {
       Object val = obj.get(key);
       if (val != null)
       {
-        String s = val.toString();
+        String s = val instanceof List<?>
+                ? JSONUtils.arrayToStringList((List<Object>) val)
+                : val.toString();
         if (!s.isEmpty())
         {
           return s;
         }
       }
     }
-    return desc;
+    return null;
   }
 
   /**
@@ -209,8 +216,8 @@ private SequenceI parseFeaturesJson(List<String> queries)
    * @param obj
    * @param key
    */
-  protected void setFeatureAttribute(SequenceFeature sf, Map<String, Object> obj,
-          String key)
+  protected void setFeatureAttribute(SequenceFeature sf,
+          Map<String, Object> obj, String key)
   {
     Object object = obj.get(key);
     if (object != null)