Commit 4b63adc8 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

allow user to upload overlay and filter by model_name

parent 28e88325
Pipeline #3081 passed with stage
in 48 seconds
......@@ -792,6 +792,8 @@ public class LayoutService implements ILayoutService {
sb.append("\t");
} else if (column.equals(ColorSchemaColumn.NAME)) {
sb.append(schema.getName() + "\t");
} else if (column.equals(ColorSchemaColumn.MODEL_NAME)) {
sb.append(schema.getModelName() + "\t");
} else if (column.equals(ColorSchemaColumn.VALUE)) {
sb.append(schema.getValue() + "\t");
} else if (column.equals(ColorSchemaColumn.COMPARTMENT)) {
......@@ -847,6 +849,8 @@ public class LayoutService implements ILayoutService {
sb.append("\t");
} else if (column.equals(ColorSchemaColumn.NAME)) {
sb.append(schema.getName() + "\t");
} else if (column.equals(ColorSchemaColumn.MODEL_NAME)) {
sb.append(schema.getModelName() + "\t");
} else if (column.equals(ColorSchemaColumn.VALUE)) {
sb.append(schema.getValue() + "\t");
} else if (column.equals(ColorSchemaColumn.COMPARTMENT)) {
......
......@@ -157,6 +157,7 @@ public class ColorSchemaReader {
Integer colorColumn = schemaColumns.get(ColorSchemaColumn.COLOR);
Integer contigColumn = schemaColumns.get(ColorSchemaColumn.CONTIG);
Integer nameColumn = schemaColumns.get(ColorSchemaColumn.NAME);
Integer modelNameColumn = schemaColumns.get(ColorSchemaColumn.MODEL_NAME);
Integer identifierColumn = schemaColumns.get(ColorSchemaColumn.IDENTIFIER);
Integer variantIdentifierColumn = schemaColumns.get(ColorSchemaColumn.VARIANT_IDENTIFIER);
Integer allelFrequencyColumn = schemaColumns.get(ColorSchemaColumn.ALLEL_FREQUENCY);
......@@ -202,6 +203,9 @@ public class ColorSchemaReader {
if (nameColumn != null) {
processNameColumn(schema, values[nameColumn]);
}
if (modelNameColumn != null) {
processModelNameColumn(schema, values[modelNameColumn]);
}
if (compartmentColumn != null) {
processCompartmentColumn(schema, values[compartmentColumn]);
}
......@@ -328,6 +332,12 @@ public class ColorSchemaReader {
schema.setName(content);
}
}
private void processModelNameColumn(ColorSchema schema, String content) {
if (!content.isEmpty()) {
schema.setModelName(content);
}
}
/**
* Sets proper compartment names to {@link ColorSchema} from cell content.
......@@ -463,6 +473,7 @@ public class ColorSchemaReader {
Integer valueColumn = schemaColumns.get(ColorSchemaColumn.VALUE);
Integer colorColumn = schemaColumns.get(ColorSchemaColumn.COLOR);
Integer nameColumn = schemaColumns.get(ColorSchemaColumn.NAME);
Integer modelNameColumn = schemaColumns.get(ColorSchemaColumn.MODEL_NAME);
Integer identifierColumn = schemaColumns.get(ColorSchemaColumn.IDENTIFIER);
Integer reactionIdentifierColumn = schemaColumns.get(ColorSchemaColumn.REACTION_IDENTIFIER);
Integer compartmentColumn = schemaColumns.get(ColorSchemaColumn.COMPARTMENT);
......@@ -499,6 +510,9 @@ public class ColorSchemaReader {
if (nameColumn != null) {
processNameColumn(schema, values[nameColumn]);
}
if (modelNameColumn != null) {
processModelNameColumn(schema, values[modelNameColumn]);
}
if (valueColumn != null) {
schema.setValue(parseValueColumn(values[valueColumn], errorPrefix));
}
......@@ -729,6 +743,9 @@ public class ColorSchemaReader {
if (schema.getName() != null) {
result.add(ColorSchemaColumn.NAME);
}
if (schema.getModelName() != null) {
result.add(ColorSchemaColumn.MODEL_NAME);
}
if (schema.getReactionIdentifier() != null) {
result.add(ColorSchemaColumn.REACTION_IDENTIFIER);
}
......
......@@ -41,285 +41,302 @@ import lcsb.mapviewer.services.utils.data.ColorSchemaColumn;
*
*/
public class ColorSchemaXlsxReader {
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private Logger logger = Logger.getLogger(ColorSchemaXlsxReader.class);
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private Logger logger = Logger.getLogger(ColorSchemaXlsxReader.class);
/**
* @param fileName
* file name
* @param sheetName
* sheet name.
* @return list of ColorSchema.
* @throws IOException
* exception related to opening the input stream.
* @throws InvalidColorSchemaException
* invalid color schema exception.
*/
public Collection<ColorSchema> readColorSchema(String fileName, String sheetName) throws IOException, InvalidColorSchemaException {
ColorParser colorParser = new ColorParser();
List<ColorSchema> result = new ArrayList<>();
FileInputStream file = null;
Workbook workbook = null;
/**
* @param fileName
* file name
* @param sheetName
* sheet name.
* @return list of ColorSchema.
* @throws IOException
* exception related to opening the input stream.
* @throws InvalidColorSchemaException
* invalid color schema exception.
*/
public Collection<ColorSchema> readColorSchema(String fileName, String sheetName)
throws IOException, InvalidColorSchemaException {
ColorParser colorParser = new ColorParser();
List<ColorSchema> result = new ArrayList<>();
FileInputStream file = null;
Workbook workbook = null;
try {
file = new FileInputStream(fileName);
// Using XSSF for xlsx format, for xls use HSSF
// <-Interface, accepts both HSSF and XSSF.
if (FilenameUtils.getExtension(fileName).equalsIgnoreCase("xls")) {
workbook = new HSSFWorkbook(file);
} else if (FilenameUtils.getExtension(fileName).equalsIgnoreCase("xlsx")) {
workbook = new XSSFWorkbook(file);
} else {
throw new IllegalArgumentException("Received file does not have a standard excel extension.");
}
Sheet sheet = null;
if (sheetName == null) {
sheet = workbook.getSheetAt(0);
} else {
sheet = workbook.getSheet(sheetName);
}
DataFormat fmt = workbook.createDataFormat();
CellStyle textStyle = workbook.createCellStyle();
textStyle.setDataFormat(fmt.getFormat("@"));
try {
file = new FileInputStream(fileName);
// Using XSSF for xlsx format, for xls use HSSF
// <-Interface, accepts both HSSF and XSSF.
if (FilenameUtils.getExtension(fileName).equalsIgnoreCase("xls")) {
workbook = new HSSFWorkbook(file);
} else if (FilenameUtils.getExtension(fileName).equalsIgnoreCase("xlsx")) {
workbook = new XSSFWorkbook(file);
} else {
throw new IllegalArgumentException("Received file does not have a standard excel extension.");
}
Sheet sheet = null;
if (sheetName == null) {
sheet = workbook.getSheetAt(0);
} else {
sheet = workbook.getSheet(sheetName);
}
DataFormat fmt = workbook.createDataFormat();
CellStyle textStyle = workbook.createCellStyle();
textStyle.setDataFormat(fmt.getFormat("@"));
Iterator<Row> rowIterator = sheet.iterator();
Integer valueColumn = null;
Integer colorColumn = null;
Integer nameColumn = null;
Integer identifierColumn = null;
Integer reactionIdentifierColumn = null;
Integer compartmentColumn = null;
Integer typeColumn = null;
Integer lineWidthColumn = null;
Integer reverseReactionColumn = null;
List<Pair<MiriamType, Integer>> foundCustomIdentifiers = new ArrayList<Pair<MiriamType, Integer>>();
int lineIndex = 0;
Map<ColorSchemaColumn, Integer> foundSchemaColumns = new HashMap<ColorSchemaColumn, Integer>();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Cell cell = row.getCell(0, Row.RETURN_BLANK_AS_NULL);
if (cell == null) {
continue;
} else {
cell.setCellType(Cell.CELL_TYPE_STRING);
if (cell.getStringCellValue().startsWith("#")) {
continue;
}
}
lineIndex++;
if (lineIndex == 1) {
Iterator<Row> rowIterator = sheet.iterator();
Integer valueColumn = null;
Integer colorColumn = null;
Integer nameColumn = null;
Integer modelNameColumn = null;
Integer identifierColumn = null;
Integer reactionIdentifierColumn = null;
Integer compartmentColumn = null;
Integer typeColumn = null;
Integer lineWidthColumn = null;
Integer reverseReactionColumn = null;
List<Pair<MiriamType, Integer>> foundCustomIdentifiers = new ArrayList<Pair<MiriamType, Integer>>();
int lineIndex = 0;
Map<ColorSchemaColumn, Integer> foundSchemaColumns = new HashMap<ColorSchemaColumn, Integer>();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Cell cell = row.getCell(0, Row.RETURN_BLANK_AS_NULL);
if (cell == null) {
continue;
} else {
cell.setCellType(Cell.CELL_TYPE_STRING);
if (cell.getStringCellValue().startsWith("#")) {
continue;
}
}
lineIndex++;
if (lineIndex == 1) {
Map<String, MiriamType> acceptableIdentifiers = new HashMap<String, MiriamType>();
for (MiriamType type : MiriamType.values()) {
acceptableIdentifiers.put(type.getCommonName().toLowerCase(), type);
}
Map<String, MiriamType> acceptableIdentifiers = new HashMap<String, MiriamType>();
for (MiriamType type : MiriamType.values()) {
acceptableIdentifiers.put(type.getCommonName().toLowerCase(), type);
}
Iterator<Cell> cellIterator = row.cellIterator();
int columnIndex = 0;
while (cellIterator.hasNext()) {
columnIndex++;
sheet.setDefaultColumnStyle(columnIndex, textStyle);
cell = cellIterator.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
String value = cell.getStringCellValue();
boolean found = false;
for (ColorSchemaColumn schemaColumn : ColorSchemaColumn.values()) {
if (value.trim().equalsIgnoreCase(schemaColumn.getTitle())) {
foundSchemaColumns.put(schemaColumn, columnIndex - 1);
found = true;
break;
}
}
if (!found) {
if (acceptableIdentifiers.keySet().contains(value.toLowerCase())) {
foundCustomIdentifiers.add(new Pair<MiriamType, Integer>(acceptableIdentifiers.get(value.toLowerCase()), columnIndex - 1));
} else {
String columnNames = "";
for (ColorSchemaColumn schemaColumn : ColorSchemaColumn.values()) {
columnNames += schemaColumn.getTitle() + ", ";
}
for (String string : acceptableIdentifiers.keySet()) {
columnNames += ", " + string;
}
throw new InvalidColorSchemaException("Unknown column type: " + value + ". Acceptable column name: " + columnNames);
}
}
}
valueColumn = foundSchemaColumns.get(ColorSchemaColumn.VALUE);
colorColumn = foundSchemaColumns.get(ColorSchemaColumn.COLOR);
nameColumn = foundSchemaColumns.get(ColorSchemaColumn.NAME);
identifierColumn = foundSchemaColumns.get(ColorSchemaColumn.IDENTIFIER);
reactionIdentifierColumn = foundSchemaColumns.get(ColorSchemaColumn.REACTION_IDENTIFIER);
compartmentColumn = foundSchemaColumns.get(ColorSchemaColumn.COMPARTMENT);
typeColumn = foundSchemaColumns.get(ColorSchemaColumn.TYPE);
lineWidthColumn = foundSchemaColumns.get(ColorSchemaColumn.LINE_WIDTH);
reverseReactionColumn = foundSchemaColumns.get(ColorSchemaColumn.REVERSE_REACTION);
Iterator<Cell> cellIterator = row.cellIterator();
int columnIndex = 0;
while (cellIterator.hasNext()) {
columnIndex++;
sheet.setDefaultColumnStyle(columnIndex, textStyle);
cell = cellIterator.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
String value = cell.getStringCellValue();
boolean found = false;
for (ColorSchemaColumn schemaColumn : ColorSchemaColumn.values()) {
if (value.trim().equalsIgnoreCase(schemaColumn.getTitle())) {
foundSchemaColumns.put(schemaColumn, columnIndex - 1);
found = true;
break;
}
}
if (!found) {
if (acceptableIdentifiers.keySet().contains(value.toLowerCase())) {
foundCustomIdentifiers.add(
new Pair<MiriamType, Integer>(acceptableIdentifiers.get(value.toLowerCase()), columnIndex - 1));
} else {
String columnNames = "";
for (ColorSchemaColumn schemaColumn : ColorSchemaColumn.values()) {
columnNames += schemaColumn.getTitle() + ", ";
}
for (String string : acceptableIdentifiers.keySet()) {
columnNames += ", " + string;
}
throw new InvalidColorSchemaException(
"Unknown column type: " + value + ". Acceptable column name: " + columnNames);
}
}
}
valueColumn = foundSchemaColumns.get(ColorSchemaColumn.VALUE);
colorColumn = foundSchemaColumns.get(ColorSchemaColumn.COLOR);
nameColumn = foundSchemaColumns.get(ColorSchemaColumn.NAME);
modelNameColumn = foundSchemaColumns.get(ColorSchemaColumn.MODEL_NAME);
identifierColumn = foundSchemaColumns.get(ColorSchemaColumn.IDENTIFIER);
reactionIdentifierColumn = foundSchemaColumns.get(ColorSchemaColumn.REACTION_IDENTIFIER);
compartmentColumn = foundSchemaColumns.get(ColorSchemaColumn.COMPARTMENT);
typeColumn = foundSchemaColumns.get(ColorSchemaColumn.TYPE);
lineWidthColumn = foundSchemaColumns.get(ColorSchemaColumn.LINE_WIDTH);
reverseReactionColumn = foundSchemaColumns.get(ColorSchemaColumn.REVERSE_REACTION);
if (valueColumn != null && colorColumn != null) {
throw new InvalidColorSchemaException("Schema can contain only one of these two columns: ");
}
if (valueColumn != null && colorColumn != null) {
throw new InvalidColorSchemaException("Schema can contain only one of these two columns: ");
}
if (nameColumn == null && identifierColumn == null && foundCustomIdentifiers.size() == 0 && reactionIdentifierColumn == null) {
throw new InvalidColorSchemaException("One of these columns is obligatory: name, identifier, reactionIdentifier");
}
if (nameColumn == null && identifierColumn == null && foundCustomIdentifiers.size() == 0
&& reactionIdentifierColumn == null) {
throw new InvalidColorSchemaException(
"One of these columns is obligatory: name, identifier, reactionIdentifier");
}
if (valueColumn == null && colorColumn == null) {
throw new InvalidColorSchemaException("Schema must contain one of these two columns: value, name");
}
if (valueColumn == null && colorColumn == null) {
throw new InvalidColorSchemaException("Schema must contain one of these two columns: value, name");
}
} else {
ColorSchema schema = new GenericColorSchema();
if (nameColumn != null) {
schema.setName(row.getCell(nameColumn).getStringCellValue());
}
if (valueColumn != null) {
try {
cell = row.getCell(valueColumn);
cell.setCellType(Cell.CELL_TYPE_STRING);
schema.setValue(Double.parseDouble(cell.getStringCellValue().replace(",", ".")));
} catch (Exception e) {
throw new InvalidColorSchemaException("[Line " + lineIndex + "] Problem with parsing value for value column. Cell value" + cell);
}
if (schema.getValue() > 1 + Configuration.EPSILON || schema.getValue() < -1 - Configuration.EPSILON) {
throw new InvalidColorSchemaException(
"[Line " + lineIndex + "] Value " + schema.getValue() + " out of range. Only values between -1 and 1 are allowed.");
}
}
if (compartmentColumn != null) {
String value = row.getCell(compartmentColumn).getStringCellValue();
if (value != null) {
String[] compartments = value.split(",");
schema.addCompartments(compartments);
}
}
if (typeColumn != null) {
String value = row.getCell(typeColumn).getStringCellValue();
if (value != null) {
String[] types = value.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 + ".");
}
}
}
}
if (colorColumn != null) {
schema.setColor(colorParser.parse(row.getCell(colorColumn).getStringCellValue()));
}
if (reactionIdentifierColumn != null) {
schema.setReactionIdentifier(row.getCell(reactionIdentifierColumn).getStringCellValue());
}
if (lineWidthColumn != null) {
cell = row.getCell(lineWidthColumn);
cell.setCellType(Cell.CELL_TYPE_STRING);
String value = cell.getStringCellValue();
if (value != null && !value.trim().isEmpty()) {
try {
schema.setLineWidth(Double.parseDouble(value.replace(",", ".")));
} catch (NumberFormatException e) {
throw new InvalidColorSchemaException("[Line " + lineIndex + "] Problem with parsing value: \"" + value + "\"");
}
}
}
if (reverseReactionColumn != null) {
cell = row.getCell(reverseReactionColumn);
if (cell != null) {
cell.setCellType(Cell.CELL_TYPE_STRING);
schema.setReverseReaction("true".equalsIgnoreCase(cell.getStringCellValue()));
}
}
if (identifierColumn != null) {
cell = row.getCell(identifierColumn);
if (cell != null && !cell.getStringCellValue().trim().isEmpty()) {
MiriamConnector miriamConnector = new MiriamConnector();
String value = cell.getStringCellValue().trim();
if (miriamConnector.isValidIdentifier(value)) {
schema.setGeneralIdentifier(value);
} else {
throw new InvalidColorSchemaException("[Line " + lineIndex + "]" + " Invalid identifier: " + value);
}
}
}
for (Pair<MiriamType, Integer> pair : foundCustomIdentifiers) {
cell = row.getCell(pair.getRight());
if (cell != null) {
schema.addIdentifierColumn(new Pair<MiriamType, String>(pair.getLeft(), cell.getStringCellValue()));
}
}
} else {
ColorSchema schema = new GenericColorSchema();
if (nameColumn != null) {
schema.setName(row.getCell(nameColumn).getStringCellValue());
}
if (modelNameColumn != null) {
schema.setModelName(row.getCell(modelNameColumn).getStringCellValue());
}
if (valueColumn != null) {
try {
cell = row.getCell(valueColumn);
cell.setCellType(Cell.CELL_TYPE_STRING);
schema.setValue(Double.parseDouble(cell.getStringCellValue().replace(",", ".")));
} catch (Exception e) {
throw new InvalidColorSchemaException(
"[Line " + lineIndex + "] Problem with parsing value for value column. Cell value" + cell);
}
if (schema.getValue() > 1 + Configuration.EPSILON || schema.getValue() < -1 - Configuration.EPSILON) {
throw new InvalidColorSchemaException("[Line " + lineIndex + "] Value " + schema.getValue()
+ " out of range. Only values between -1 and 1 are allowed.");
}
}
if (compartmentColumn != null) {
String value = row.getCell(compartmentColumn).getStringCellValue();
if (value != null) {
String[] compartments = value.split(",");
schema.addCompartments(compartments);
}
}
if (typeColumn != null) {
String value = row.getCell(typeColumn).getStringCellValue();
if (value != null) {
String[] types = value.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 + ".");
}
}
}
}
if (colorColumn != null) {
schema.setColor(colorParser.parse(row.getCell(colorColumn).getStringCellValue()));
}
if (reactionIdentifierColumn != null) {
schema.setReactionIdentifier(row.getCell(reactionIdentifierColumn).getStringCellValue());
}
if (lineWidthColumn != null) {
cell = row.getCell(lineWidthColumn);
cell.setCellType(Cell.CELL_TYPE_STRING);
String value = cell.getStringCellValue();
if (value != null && !value.trim().isEmpty()) {
try {
schema.setLineWidth(Double.parseDouble(value.replace(",", ".")));
} catch (NumberFormatException e) {
throw new InvalidColorSchemaException(
"[Line " + lineIndex + "] Problem with parsing value: \"" + value + "\"");
}
}
}
if (reverseReactionColumn != null) {
cell = row.getCell(reverseReactionColumn);
if (cell != null) {
cell.setCellType(Cell.CELL_TYPE_STRING);
schema.setReverseReaction("true".equalsIgnoreCase(cell.getStringCellValue()));
}
}
if (identifierColumn != null) {
cell = row.getCell(identifierColumn);
if (cell != null && !cell.getStringCellValue().trim().isEmpty()) {
MiriamConnector miriamConnector = new MiriamConnector();
String value = cell.getStringCellValue().trim();
if (miriamConnector.isValidIdentifier(value)) {
schema.setGeneralIdentifier(value);
} else {
throw new InvalidColorSchemaException("[Line " + lineIndex + "]" + " Invalid identifier: " + value);
}
}
}
for (Pair<MiriamType, Integer> pair : foundCustomIdentifiers) {
cell = row.getCell(pair.getRight());
if (cell != null) {
schema.addIdentifierColumn(new Pair<MiriamType, String>(pair.getLeft(), cell.getStringCellValue()));
}
}
if ((schema.getValue() != null && schema.getColor() != null) || (schema.getValue() == null && schema.getColor() == null)) {
throw new InvalidColorSchemaException("Value or Color is needed not both");
}
if ((schema.getValue() != null && schema.getColor() != null)
|| (schema.getValue() == null && schema.getColor() == null)) {
throw new InvalidColorSchemaException("Value or Color is needed not both");
}
if (schema.getName() == null && schema.getGeneralIdentifier() == null && foundCustomIdentifiers.size() == 0
&& schema.getReactionIdentifier() == null) {
throw new InvalidColorSchemaException("One of these columns values is obligatory: name, identifier, reactionIdentifier");
}
result.add(schema);
}
}
} finally {
try {
if (file != null) {
file.close();
}
if (workbook != null) {
workbook.close();
}
} catch (Exception e) {
if (schema.getName() == null && schema.getGeneralIdentifier() == null && foundCustomIdentifiers.size() == 0
&& schema.getReactionIdentifier() == null) {
throw new InvalidColorSchemaException(
"One of these columns values is obligatory: name, identifier, reactionIdentifier");
}
result.add(schema);
}
}
} finally {
try {
if (file != null) {
file.close();
}
if (workbook != null) {
workbook.close();
}
} catch (Exception e) {
}
}
return result;
}
}
return result;