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;
int end = Integer.parseInt(obj.get("end").toString());
String source = obj.get("source").toString();
String strand = obj.get("strand").toString();
- Object value = obj.get("consequence_type");
- value = obj.get("alleles");
- JSONArray allelesArray = (JSONArray) value;
- String alleles = allelesArray == null ? null
- : allelesArray.toString(); // todo need as a List?
- value = obj.get("clinical_significance");
- JSONArray clinSigArray = (JSONArray) value;
- String clinSig = clinSigArray == null ? null
- : clinSigArray.toString();
+ String alleles = JSONUtils
+ .arrayToList((JSONArray) obj.get("alleles"));
+ String clinSig = JSONUtils
+ .arrayToList(
+ (JSONArray) obj.get("clinical_significance"));
/*
* convert 'variation' to 'sequence_variant', and 'cds' to 'CDS'
/**
* Answers true if the feature type is either 'NMD_transcript_variant' or
- * 'transcript' or one of its sub-types in the Sequence Ontology. This is
- * needed because NMD_transcript_variant behaves like 'transcript' in Ensembl
+ * 'transcript' (or one of its sub-types in the Sequence Ontology). This is
+ * because NMD_transcript_variant behaves like 'transcript' in Ensembl
* although strictly speaking it is not (it is a sub-type of
* sequence_variant).
+ * <p>
+ * (This test was needed when fetching transcript features as GFF. As we are
+ * now fetching as JSON, all features have type 'transcript' so the check for
+ * NMD_transcript_variant is redundant. Left in for any future case arising.)
*
* @param featureType
* @return
--- /dev/null
+package jalview.util;
+
+import org.json.simple.JSONArray;
+
+public class JSONUtils
+{
+
+ /**
+ * Converts a JSONArray of values to a string as a comma-separated list.
+ * Answers null if the array is null or empty.
+ *
+ * @param jsonArray
+ * @return
+ */
+ public static String arrayToList(JSONArray jsonArray)
+ {
+ if (jsonArray == null)
+ {
+ return null;
+ }
+
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < jsonArray.size(); i++)
+ {
+ if (i > 0)
+ {
+ sb.append(",");
+ }
+ sb.append(jsonArray.get(i).toString());
+ }
+ return sb.length() == 0 ? null : sb.toString();
+ }
+
+}
--- /dev/null
+package jalview.util;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNull;
+
+import org.json.JSONException;
+import org.json.simple.JSONArray;
+import org.testng.annotations.Test;
+
+public class JSONUtilsTest
+{
+ @Test(groups = "Functional")
+ public void testArrayToList() throws JSONException
+ {
+ assertNull(JSONUtils.arrayToList(null));
+
+ JSONArray ja = new JSONArray();
+ assertNull(JSONUtils.arrayToList(null));
+
+ ja.add("hello");
+ assertEquals(JSONUtils.arrayToList(ja), "hello");
+
+ ja.add("world");
+ assertEquals(JSONUtils.arrayToList(ja), "hello,world");
+ }
+}