BaselineExpressionPerBiologicalReplicate implements Expression { @Override public JsonObject toJson() { if (data.size() == 0) { return new JsonObject(); } JsonObject result = new JsonObject(); double[] expressionValuesSorted = expressionValuesSorted(); double total = expressionValues().sum(); JsonObject stats = new JsonObject(); stats.addProperty("total", total); stats.addProperty("min", expressionValuesSorted[0]); stats.addProperty("lower_quartile", new Percentile(0.25).evaluate(expressionValuesSorted)); stats.addProperty("median", new Median().evaluate(expressionValuesSorted)); stats.addProperty("upper_quartile", new Percentile(0.75).evaluate(expressionValuesSorted)); stats.addProperty("max", expressionValuesSorted[expressionValuesSorted.length - 1]); result.add("stats", stats); JsonArray values = new JsonArray(); data.entrySet().stream().map(e -> { JsonObject o = new JsonObject(); o.addProperty("id", e.getKey().getId()); JsonArray assays = new JsonArray(); e.getKey().assaysAnalyzedForThisDataColumn().stream().sorted().forEach(assays::add); o.add("assays", assays); JsonObject value = new JsonObject(); double expressionLevel = e.getValue().getLevel(); value.addProperty("expression_absolute_units", expressionLevel); o.add("value", value); return o; }).forEach(values::add); result.add("values", values); return result; } BaselineExpressionPerBiologicalReplicate(Map<BiologicalReplicate, BaselineExpression> data); @Override double getLevel(); @Override JsonObject toJson(); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); final Map<BiologicalReplicate, BaselineExpression> data; }
@Test public void toJson() { assertThat( new BaselineExpressionPerBiologicalReplicate( ImmutableMap.of( new BiologicalReplicate("assay_1"), new BaselineExpression(1.0), new BiologicalReplicate("assay_2"), new BaselineExpression(2.0), new BiologicalReplicate( "t1", ImmutableSet.of("assay_3", "assay_4")), new BaselineExpression(0.5))) .toJson(), is(GSON.fromJson( "{\n" + " \"stats\": {\n" + " \"total\": 3.5,\n" + " \"min\": 0.5,\n" + " \"lower_quartile\": 0.5,\n" + " \"median\": 1.0,\n" + " \"upper_quartile\": 0.5,\n" + " \"max\": 2.0\n" + " },\n" + " \"values\": [{\n" + " \"id\": \"assay_1\",\n" + " \"assays\": [\"assay_1\"],\n" + " \"value\": {\n" + " \"expression_absolute_units\": 1.0\n" + " }\n" + " }, {\n" + " \"id\": \"assay_2\",\n" + " \"assays\": [\"assay_2\"],\n" + " \"value\": {\n" + " \"expression_absolute_units\": 2.0\n" + " }\n" + " }, {\n" + " \"id\": \"t1\",\n" + " \"assays\": [\"assay_3\", \"assay_4\"],\n" + " \"value\": {\n" + " \"expression_absolute_units\": 0.5\n" + " }\n" + " }]\n" + "}", JsonObject.class))); }