Commit 9ad4bfce authored by Piotr Gawron's avatar Piotr Gawron
Browse files

convertModel method moved

parent 75ecf0a9
package lcsb.mapviewer.api.convert;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.sbml.jsbml.SBMLException;
......@@ -13,35 +15,67 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.converter.ConverterException;
import lcsb.mapviewer.converter.InvalidInputDataExecption;
import lcsb.mapviewer.commands.CreateHierarchyCommand;
import lcsb.mapviewer.common.MinervaLoggerAppender;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.converter.*;
import lcsb.mapviewer.converter.graphics.DrawingException;
import lcsb.mapviewer.model.map.InconsistentModelException;
import lcsb.mapviewer.model.map.layout.graphics.Layer;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.modelutils.map.LogFormatter;
import lcsb.mapviewer.services.QueryException;
@RestController
@RequestMapping(value = "/api/convert", produces = MediaType.APPLICATION_JSON_VALUE)
public class ConvertController extends BaseController {
@SuppressWarnings("unused")
private Logger logger = LogManager.getLogger();
private ConvertRestImpl convertController;
private List<Converter> modelConverters;
@Autowired
public ConvertController(ConvertRestImpl convertController) {
public ConvertController(ConvertRestImpl convertController, List<Converter> modelConverters) {
this.convertController = convertController;
this.modelConverters = modelConverters;
}
@PostMapping(value = "/{fromFormat}:{toFormat}", produces = MediaType.APPLICATION_XML_VALUE)
@PostMapping(value = "/{fromFormat}:{toFormat:.+}", produces = MediaType.APPLICATION_XML_VALUE)
public String convertInput(@PathVariable(value = "fromFormat") String fromFormat,
@PathVariable(value = "toFormat") String toFormat,
@RequestBody byte[] body)
throws IOException, QueryException, SBMLException,
InvalidInputDataExecption, InconsistentModelException, ConverterException {
return convertController.convert(fromFormat, toFormat, body);
ConverterParams params = createConvertParams(body);
MinervaLoggerAppender appender = MinervaLoggerAppender.createAppender();
Model original;
try {
original = getModelParserByNameOrClass(fromFormat).createModel(params);
StringBuilder notes = new StringBuilder(original.getNotes());
if (!appender.getWarnings().isEmpty()) {
for (String entry : new LogFormatter().createFormattedWarnings(appender.getWarnings())) {
notes.append("\n" + entry);
}
}
original.setNotes(notes.toString());
} finally {
MinervaLoggerAppender.unregisterLogEventStorage(appender);
}
try {
new CreateHierarchyCommand(original, 10, 1024).execute();
for (Layer layer : original.getLayers()) {
layer.setVisible(false);
}
} catch (Exception e) {
logger.error("Problem with creating pathways", e);
}
Converter exporter = getModelParserByNameOrClass(toFormat);
return IOUtils.toString(exporter.model2InputStream(original), StandardCharsets.UTF_8);
}
@PostMapping(value = "/image/{fromFormat}:{toFormat}")
@PostMapping(value = "/image/{fromFormat}:{toFormat:.+}")
public @ResponseBody ResponseEntity<byte[]> convertInputToImage(
@PathVariable(value = "fromFormat") String fromFormat,
@PathVariable(value = "toFormat") String toFormat,
......@@ -78,4 +112,48 @@ public class ConvertController extends BaseController {
return convertController.getInformationImage();
}
private ConverterParams createConvertParams(byte[] input) {
InputStream is = new ByteArrayInputStream(input);
return new ConverterParams().inputStream(is);
}
private Converter getModelParserByNameOrClass(String id) throws QueryException {
try {
return getModelParserByName(id);
} catch (QueryException e) {
return getModelParser(id);
}
}
private Converter getModelParserByName(String name) throws QueryException {
for (Converter converter : modelConverters) {
if (removeWhiteSpaces(converter.getCommonName()).equals(name)) {
try {
return converter.getClass().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new InvalidStateException(e);
}
}
}
throw new QueryException("Unknown parser name: " + name);
}
private String removeWhiteSpaces(String str) {
return str.replace(' ', '_');
}
protected Converter getModelParser(String handlerClass) throws QueryException {
for (Converter converter : modelConverters) {
if (converter.getClass().getCanonicalName().equals(handlerClass)) {
try {
return (Converter) Class.forName(handlerClass).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new QueryException("Problem with handler:" + handlerClass);
}
}
}
throw new QueryException("Unknown handlerClass: " + handlerClass);
}
}
\ No newline at end of file
......@@ -3,7 +3,6 @@ package lcsb.mapviewer.api.convert;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
......@@ -17,7 +16,6 @@ import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.commands.*;
import lcsb.mapviewer.common.MinervaLoggerAppender;
import lcsb.mapviewer.common.Pair;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.converter.*;
......@@ -26,7 +24,6 @@ import lcsb.mapviewer.converter.graphics.*;
import lcsb.mapviewer.model.map.InconsistentModelException;
import lcsb.mapviewer.model.map.layout.graphics.Layer;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.modelutils.map.LogFormatter;
import lcsb.mapviewer.services.QueryException;
@Transactional
......@@ -35,37 +32,6 @@ public class ConvertRestImpl extends BaseRestImpl {
private Logger logger = LogManager.getLogger();
public String convert(String fromFormat, String toFormat, byte[] input)
throws InvalidInputDataExecption, SBMLException, InconsistentModelException, IOException, ConverterException,
QueryException {
ConverterParams params = createConvertParams(input);
MinervaLoggerAppender appender = MinervaLoggerAppender.createAppender();
Model original;
try {
original = getModelParserByNameOrClass(fromFormat).createModel(params);
StringBuilder notes = new StringBuilder(original.getNotes());
if (!appender.getWarnings().isEmpty()) {
for (String entry : new LogFormatter().createFormattedWarnings(appender.getWarnings())) {
notes.append("\n" + entry);
}
}
original.setNotes(notes.toString());
} finally {
MinervaLoggerAppender.unregisterLogEventStorage(appender);
}
try {
new CreateHierarchyCommand(original, 10, 1024).execute();
for (Layer layer : original.getLayers()) {
layer.setVisible(false);
}
} catch (Exception e) {
logger.error("Problem with creating pathways", e);
}
Converter exporter = getModelParserByNameOrClass(toFormat);
return IOUtils.toString(exporter.model2InputStream(original), StandardCharsets.UTF_8);
}
public ByteArrayOutputStream converToImage(String fromFormat, String toFormat, byte[] input,
Double targetWidth, Double targetHeight)
throws InvalidInputDataExecption, SBMLException, IOException, ConverterException, DrawingException,
......
......@@ -71,16 +71,6 @@ public class ConvertRestImplTest extends RestTestFunctions {
assertTrue(outputs.size() > 0);
}
@Test
public void testCelDesigner2Sbml1() throws Exception {
byte[] content = readFile("testFiles/convert/sample-cd.xml").getBytes();
String result = convertRestImpl.convert(
"lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser",
"lcsb.mapviewer.converter.model.sbml.SbmlParser", content);
assertTrue(result.length() > 0);
assertTrue(result.contains("</layout:speciesReferenceGlyph>"));
}
@Test
public void testCelDesigner2Svg() throws Exception {
String content = readFile("testFiles/convert/sample-cd.xml");
......@@ -89,27 +79,6 @@ public class ConvertRestImplTest extends RestTestFunctions {
assertTrue(result.contains("<rect"));
}
@Test
public void testCellDesigner2Sbml2Svg() throws Exception {
byte[] content = readFile("testFiles/convert/neuron_cd.xml").getBytes();
String resultSbml = convertRestImpl.convert("CellDesigner_SBML", "SBML", content).toString();
String resultSvg = convertRestImpl.converToImage("SBML", "svg", resultSbml.getBytes()).toString();
assertTrue(resultSvg.contains("<rect"));
}
@Test
public void testCellDesigner2Sbml2CellDesignerSvg() throws Exception {
String content = readFile("testFiles/convert/neuron_cd.xml");
String resultSbml = convertRestImpl.convert("CellDesigner_SBML", "SBML", content.getBytes()).toString();
String resultCellDesignerSbml = convertRestImpl.convert("SBML", "CellDesigner_SBML", resultSbml.getBytes())
.toString();
String resultSvg = convertRestImpl.converToImage("CellDesigner_SBML", "svg", resultCellDesignerSbml.getBytes())
.toString();
assertTrue(resultSvg.contains("<rect"));
}
@Test
public void testScaling() throws Exception {
String content = readFile("testFiles/convert/sample-cd.xml");
......@@ -127,94 +96,4 @@ public class ConvertRestImplTest extends RestTestFunctions {
assertFalse(matcher1.group(1).toString().equals(matcher2.group(1).toString()));
}
@Test
public void test1() throws Exception {
String content = readFile("testFiles/convert/glycolysis2.xml");
String result = convertRestImpl.convert("CellDesigner_SBML", "SBML", content.getBytes());
ByteArrayOutputStream result1 = convertRestImpl.converToImage("SBML", "svg", result.getBytes());
File output = new File("testFiles/convert/glycolysis2.svg");
FileOutputStream outputStream = new FileOutputStream(output);
outputStream.write(result1.toByteArray());
outputStream.close();
assertTrue(output.exists());
output.delete();
}
@Test
public void test2() throws Exception {
String content = readFile("testFiles/convert/glycolysis2.xml");
String result = convertRestImpl.convert("CellDesigner_SBML", "SBML", content.getBytes());
String result1 = convertRestImpl.convert("SBML", "CellDesigner_SBML", result.getBytes());
assertNotNull(result);
assertNotNull(result1);
}
@Test
public void testCelDesigner2Sbgn() throws Exception {
String content = readFile("testFiles/convert/sample-cd.xml");
String result = convertRestImpl.convert("CellDesigner_SBML", "SBGN-ML", content.getBytes()).toString();
assertTrue(result.contains("glyph class=\"complex\""));
}
@SuppressWarnings("unchecked")
private List<String> extractIdsFromOutputs(Map<String, ?> infoObject) {
List<String> ids = new ArrayList<>();
for (Map<String, List<String>> o : (List<Map<String, List<String>>>) infoObject.get("outputs")) {
ids.add(o.get("available_names").get(0));
}
return ids;
}
private void test2All(String content, String converter)
throws SBMLException, SecurityException, InvalidInputDataExecption, InconsistentModelException,
IOException, ConverterException, XMLStreamException, DrawingException, QueryException {
Map<String, ?> info = convertRestImpl.getInformation();
Map<String, ?> infoImage = convertRestImpl.getInformationImage();
List<String> targets = new ArrayList<>();
targets.addAll(extractIdsFromOutputs(info));
for (String target : targets) {
String result = convertRestImpl.convert(converter, target, content.getBytes()).toString();
assertTrue(result.length() > 0);
}
targets.clear();
targets.addAll(extractIdsFromOutputs(infoImage));
for (String target : targets) {
String result = convertRestImpl.converToImage(converter, target, content.getBytes()).toString();
assertTrue(result.length() > 0);
}
}
@Test
public void testCelDesigner2All() throws Exception {
String content = readFile("testFiles/convert/sample-cd.xml");
test2All(content, "CellDesigner_SBML");
}
@Test
public void testSbml2All() throws Exception {
String content = readFile("testFiles/convert/sample-sbml.xml");
test2All(content, "SBML");
}
@Test
public void testSbgn2All() throws Exception {
String content = readFile("testFiles/convert/sample.sbgn");
test2All(content, "SBGN-ML");
}
@Test
public void testNewtSbgn2All() throws Exception {
String content = readFile("testFiles/convert/newt.sbgn");
test2All(content, "SBGN-ML");
}
}
......@@ -6,8 +6,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.awt.geom.Point2D;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
......@@ -680,4 +679,22 @@ abstract public class ControllerIntegrationTest {
.optional());
}
protected String readFile(String file) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(file));
try {
String line = null;
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
} finally {
reader.close();
}
return stringBuilder.toString();
}
}
package lcsb.mapviewer.web;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
......@@ -26,6 +27,9 @@ import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.RequestBuilder;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lcsb.mapviewer.api.convert.ConvertRestImpl;
import lcsb.mapviewer.converter.ConverterParams;
import lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser;
......@@ -38,6 +42,9 @@ public class ConvertControllerIntegrationTest extends ControllerIntegrationTest
@Autowired
private ConvertRestImpl converterRestImpl;
@Autowired
private ObjectMapper objectMapper;
Logger logger = LogManager.getLogger();
@Test
......@@ -218,4 +225,164 @@ public class ConvertControllerIntegrationTest extends ControllerIntegrationTest
return StringUtils.join(converters, ", ");
}
@Test
public void testCelDesigner2Sbml1() throws Exception {
String content = readFile("src/test/resources/convert/sample-cd.xml");
String result = convertToModel(content, "lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser",
"lcsb.mapviewer.converter.model.sbml.SbmlParser");
assertTrue(result.length() > 0);
assertTrue(result.contains("</layout:speciesReferenceGlyph>"));
}
@Test
public void testCellDesignerToSbmlToSvg() throws Exception {
String content = readFile("src/test/resources/convert/neuron_cd.xml");
String resultSbml = convertToModel(content, "CellDesigner_SBML", "SBML");
String resultSvg = convertToImage(resultSbml, "SBML", "svg");
assertTrue(resultSvg.contains("<rect"));
}
@Test
public void testCellDesignerToSbmlToSvg2() throws Exception {
String content = readFile("src/test/resources/convert/glycolysis2.xml");
String resultSbml = convertToModel(content, "CellDesigner_SBML", "SBML");
String resultSvg = convertToImage(resultSbml, "SBML", "svg");
assertTrue(resultSvg.contains("<rect"));
}
@Test
public void testCellDesignerToSbmlToCellDesigner() throws Exception {
String content = readFile("src/test/resources/convert/glycolysis2.xml");
String resultSbml = convertToModel(content, "CellDesigner_SBML", "SBML");
String resultCellDesigner = convertToModel(resultSbml, "SBML", "CellDesigner_SBML");
assertTrue(resultCellDesigner.contains("rect"));
}
@Test
public void testCellDesigner2Sbml2CellDesignerSvg() throws Exception {
String content = readFile("src/test/resources/convert/neuron_cd.xml");
String resultSbml = convertToModel(content, "CellDesigner_SBML", "SBML");
String resultCellDesignerSbml = convertToModel(resultSbml, "SBML", "CellDesigner_SBML");
String resultSvg = convertToImage(resultCellDesignerSbml, "CellDesigner_SBML", "svg");
assertTrue(resultSvg.contains("<rect"));
}
@Test
public void testCelDesigner2Sbgn() throws Exception {
String content = readFile("src/test/resources/convert/sample-cd.xml");
String result = convertToModel(content, "CellDesigner_SBML", "SBGN-ML");
assertTrue(result.contains("glyph class=\"complex\""));
}
private void test2All(String content, String converter)
throws Exception {
List<String> modelConverters = getModelOutputConverters();
for (String target : modelConverters) {
String result = convertToModel(content, converter, target);
assertTrue(result.length() > 0);
}
List<String> imageConverters = getImageOutputConverters();
for (String target : imageConverters) {
String result = convertToImage(content, converter, target);
assertTrue(result.length() > 0);
}
}
private String convertToImage(String content, String converter, String target)
throws UnsupportedEncodingException, Exception {
RequestBuilder request = post("/api/convert/image/{inputFormat}:{outputFormat}", converter, target)
.header("post-filename", "input_file.xml")
.content(content.getBytes("UTF-8"))
.characterEncoding("UTF-8");
String result = mockMvc.perform(request)
.andExpect(status().is2xxSuccessful())
.andReturn().getResponse().getContentAsString();
return result;
}
private String convertToModel(String content, String converter, String target)
throws UnsupportedEncodingException, Exception {
RequestBuilder request = post("/api/convert/{inputFormat}:{outputFormat}", converter, target)
.header("post-filename", "input_file.xml")
.content(content.getBytes("UTF-8"))
.characterEncoding("UTF-8");
String result = mockMvc.perform(request)
.andExpect(status().is2xxSuccessful())
.andReturn().getResponse().getContentAsString();
return result;
}
private List<String> getModelOutputConverters() throws Exception {
List<String> result = new ArrayList<>();
RequestBuilder request = get("/api/convert/");
String response = mockMvc.perform(request)
.andExpect(status().is2xxSuccessful())
.andReturn().getResponse().getContentAsString();
Map<String, List<Map<String, List<String>>>> data = objectMapper.readValue(response,
new TypeReference<Map<String, List<Map<String, List<String>>>>>() {
});
for (Map<String, List<String>> entry : data.get("outputs")) {
result.add(entry.get("available_names").get(0));
}
return result;
}
private List<String> getImageOutputConverters() throws Exception {
List<String> result = new ArrayList<>();
RequestBuilder request = get("/api/convert/image/");
String response = mockMvc.perform(request)
.andExpect(status().is2xxSuccessful())
.andReturn().getResponse().getContentAsString();
Map<String, List<Map<String, List<String>>>> data = objectMapper.readValue(response,
new TypeReference<Map<String, List<Map<String, List<String>>>>>() {
});
for (Map<String, List<String>> entry : data.get("outputs")) {
result.add(entry.get("available_names").get(0));
}
return result;
}
@Test
public void testCelDesigner2All() throws Exception {
String content = readFile("src/test/resources/convert/sample-cd.xml");
test2All(content, "CellDesigner_SBML");
}
@Test
public void testSbml2All() throws Exception {
String content = readFile("src/test/resources/convert/sample-sbml.xml");
test2All(content, "SBML");
}
@Test
public void testSbgn2All() throws Exception {
String content = readFile("src/test/resources/convert/sample.sbgn");
test2All(content, "SBGN-ML");
}
@Test
public void testNewtSbgn2All() throws Exception {
String content = readFile("src/test/resources/convert/newt.sbgn");
test2All(content, "SBGN-ML");
}
}
This diff is collapsed.
This diff is collapsed.
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<sbgn xmlns="http://sbgn.org/libsbgn/0.2">
<map language="process description">
<glyph id="nwtN_f034af08-f2b3-4912-aaa8-58b4f55f43b9" class="macromolecule">
<label text="dffgdf"/>