+ SequenceI seq = parseFeaturesJson(queries);
+ if (seq == null)
+ return null;
+ return new Alignment(new SequenceI[] { seq });
+
+ }
+
+ /**
+ * Parses the JSON response into Jalview sequence features and attaches them
+ * to a dummy sequence
+ *
+ * @param br
+ * @return
+ */
+ @SuppressWarnings("unchecked")
+ private SequenceI parseFeaturesJson(List<String> queries)
+ {
+ SequenceI seq = new Sequence("Dummy", "");
+ try
+ {
+ Iterator<Object> rvals = (Iterator<Object>) getJSON(null, queries, -1, MODE_ITERATOR, null);
+ if (rvals == null)
+ {
+ return null;
+ }
+ while (rvals.hasNext())
+ {
+ try
+ {
+ Map<String, Object> obj = (Map<String, Object>) rvals.next();
+ String type = obj.get("feature_type").toString();
+ int start = Integer.parseInt(obj.get("start").toString());
+ 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"));
+
+ /*
+ * convert 'variation' to 'sequence_variant', and 'cds' to 'CDS'
+ * so as to have a valid SO term for the feature type
+ * ('gene', 'exon', 'transcript' don't need any conversion)
+ */
+ if ("variation".equals(type))
+ {
+ type = SequenceOntologyI.SEQUENCE_VARIANT;
+ }
+ else if (SequenceOntologyI.CDS.equalsIgnoreCase((type)))
+ {
+ 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");
+ sf.setValue("alleles", alleles);
+ sf.setValue("clinical_significance", clinSig);
+
+ seq.addSequenceFeature(sf);
+
+ } catch (Throwable t)
+ {
+ // ignore - keep trying other features
+ }
+ }
+ } catch (ParseException | IOException e)
+ {
+ e.printStackTrace();
+ // ignore
+ }
+
+ return seq;
+ }
+
+ /**
+ * 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)
+ {
+ for (String key : keys)
+ {
+ Object val = obj.get(key);
+ if (val != null)
+ {
+ String s = val instanceof List<?>
+ ? JSONUtils.arrayToStringList((List<Object>) val)
+ : val.toString();
+ if (!s.isEmpty())
+ {
+ return s;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * A helper method that reads the 'key' entry in the JSON object, and if not
+ * null, sets its string value as an attribute on the sequence feature
+ *
+ * @param sf
+ * @param obj
+ * @param key
+ */
+ protected void setFeatureAttribute(SequenceFeature sf, Map<String, Object> obj,
+ String key)
+ {
+ Object object = obj.get(key);
+ if (object != null)
+ {
+ sf.setValue(key, object.toString());
+ }