diff --git a/service/src/main/java/lcsb/mapviewer/services/utils/ColorSchemaReader.java b/service/src/main/java/lcsb/mapviewer/services/utils/ColorSchemaReader.java index 574e2bcb654526728edcdde5ed5e37d26a085eae..eec055b8dd5dc73f0e90675dd5cc4fb925a7d784 100644 --- a/service/src/main/java/lcsb/mapviewer/services/utils/ColorSchemaReader.java +++ b/service/src/main/java/lcsb/mapviewer/services/utils/ColorSchemaReader.java @@ -35,6 +35,7 @@ import lcsb.mapviewer.model.map.layout.GeneVariationColorSchema; import lcsb.mapviewer.model.map.layout.GenericColorSchema; import lcsb.mapviewer.model.map.layout.InvalidColorSchemaException; import lcsb.mapviewer.model.map.layout.ReferenceGenomeType; +import lcsb.mapviewer.model.map.species.Element; import lcsb.mapviewer.services.utils.data.ColorSchemaColumn; import lcsb.mapviewer.services.utils.data.ColorSchemaType; @@ -477,14 +478,14 @@ public class ColorSchemaReader { } if (valueColumn == null && colorColumn == null) { - throw new InvalidColorSchemaException("Schema must contain one of these two columns: value, name"); + throw new InvalidColorSchemaException("Schema must contain one of these two columns: value, color"); } lineIndex++; line = br.readLine(); while (line != null) { - lineIndex++; String errorPrefix = "[Line " + lineIndex + "]\t"; + lineIndex++; if (line.trim().equals("")) { line = br.readLine(); @@ -492,7 +493,7 @@ public class ColorSchemaReader { } String[] values = line.split("\t", -1); if (values.length != columns.length) { - throw new InvalidColorSchemaException("[Line " + lineIndex + "] Wrong number of cells"); + throw new InvalidColorSchemaException(errorPrefix + "Wrong number of cells"); } ColorSchema schema = new GenericColorSchema(); if (nameColumn != null) { @@ -511,16 +512,7 @@ public class ColorSchemaReader { processCompartmentColumn(schema, values[compartmentColumn]); } if (typeColumn != null) { - String[] types = values[typeColumn].split(","); - for (String string : types) { - SpeciesMapping mapping = SpeciesMapping.getMappingByString(string); - if (mapping != null) { - schema.addType(mapping.getModelClazz()); - } else { - throw new InvalidColorSchemaException("Unknown class type: " + string + "."); - } - } - + schema.setTypes(parseSpeciesTypes(values[typeColumn], errorPrefix)); } if (descriptionColumn != null) { schema.setDescription(values[descriptionColumn]); @@ -545,8 +537,7 @@ public class ColorSchemaReader { if (mc.isValidIdentifier(values[identifierColumn])) { schema.setGeneralIdentifier(values[identifierColumn]); } else { - throw new InvalidColorSchemaException( - "[Line " + lineIndex + "]" + " Invalid identifier: " + values[identifierColumn]); + throw new InvalidColorSchemaException(errorPrefix + "Invalid identifier: " + values[identifierColumn]); } } for (Pair<MiriamType, Integer> pair : customIdentifiers) { @@ -561,6 +552,26 @@ public class ColorSchemaReader { return result; } + private List<Class<? extends Element>> parseSpeciesTypes(String typesString, String errorPrefix) + throws InvalidColorSchemaException { + List<Class<? extends Element>> result = new ArrayList<>(); + String[] types = typesString.split(","); + for (String string : types) { + SpeciesMapping mapping = SpeciesMapping.getMappingByString(string); + if (mapping != null) { + result.add(mapping.getModelClazz()); + } else { + String validStrings = ""; + for (SpeciesMapping speciesMapping : SpeciesMapping.values()) { + validStrings += speciesMapping.getCellDesignerString() + ", "; + } + throw new InvalidColorSchemaException( + errorPrefix + "Unknown class type: " + string + ". Valid values are: " + validStrings); + } + } + return null; + } + private Double parseValueColumn(String string, String errorPrefix) throws InvalidColorSchemaException { Double result = null; if (!string.isEmpty()) { diff --git a/service/src/main/java/lcsb/mapviewer/services/utils/data/ColorSchemaColumn.java b/service/src/main/java/lcsb/mapviewer/services/utils/data/ColorSchemaColumn.java index a7f6ed47414cfab0c7d833e2575cee3013659371..6792e10ef5b0ebff44da960be6ad99a953e9ab1f 100644 --- a/service/src/main/java/lcsb/mapviewer/services/utils/data/ColorSchemaColumn.java +++ b/service/src/main/java/lcsb/mapviewer/services/utils/data/ColorSchemaColumn.java @@ -59,7 +59,7 @@ public enum ColorSchemaColumn { LINE_WIDTH("lineWidth", new ColorSchemaType[] { ColorSchemaType.GENERIC }), // /** - * Postion where gene variants starts. + * Position where gene variants starts. */ POSITION("position", new ColorSchemaType[] { ColorSchemaType.GENETIC_VARIANT }), // diff --git a/service/src/test/java/lcsb/mapviewer/services/utils/ColorSchemaReaderTest.java b/service/src/test/java/lcsb/mapviewer/services/utils/ColorSchemaReaderTest.java index c8919bf6a14544aaaa82af410d133fdbbb289e0a..6b904433256b636dd313f90cc6bcdd53f34aab85 100644 --- a/service/src/test/java/lcsb/mapviewer/services/utils/ColorSchemaReaderTest.java +++ b/service/src/test/java/lcsb/mapviewer/services/utils/ColorSchemaReaderTest.java @@ -265,13 +265,31 @@ public class ColorSchemaReaderTest extends ServiceTestFunctions { } } + @Test + public void testElementsByType() throws Exception { + try { + ColorSchemaReader reader = new ColorSchemaReader(); + Map<String, String> params = new HashMap<>(); + params.put(TextFileUtils.COLUMN_COUNT_PARAM, "3"); + + String input = "type\tname\tvalue\n" + // + "protein\t\t1.0\n"; + Collection<ColorSchema> schemas = reader + .readColorSchema(new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)), params); + assertEquals(1, schemas.size()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + @Test public void testColoringWithInvalidValueAndColor() throws Exception { try { ColorSchemaReader reader = new ColorSchemaReader(); Map<String, String> params = new HashMap<>(); params.put(TextFileUtils.COLUMN_COUNT_PARAM, "3"); - + String input = "name\tcolor\tvalue\ns1\t#ff0000\t1.0"; reader.readColorSchema(new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)), params); fail("Exception expected"); @@ -288,7 +306,7 @@ public class ColorSchemaReaderTest extends ServiceTestFunctions { ColorSchemaReader reader = new ColorSchemaReader(); Map<String, String> params = new HashMap<>(); params.put(TextFileUtils.COLUMN_COUNT_PARAM, "3"); - + String input = "name\tcolor\tvalue\ns1\t\t"; reader.readColorSchema(new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)), params); } catch (Exception e) {