Skip to content
Snippets Groups Projects
Commit 65335d19 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

glyphs are assigned to elements having glyph property

parent 6ca2f662
No related branches found
No related tags found
1 merge request!768Resolve "Custom images as overlay levels or background"
......@@ -17,7 +17,10 @@ import lcsb.mapviewer.converter.zip.ImageZipEntryFile;
import lcsb.mapviewer.converter.zip.LayoutZipEntryFile;
import lcsb.mapviewer.converter.zip.ZipEntryFile;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.layout.graphics.Glyph;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelData;
import lcsb.mapviewer.model.map.species.Element;
public class ProjectFactory {
......@@ -75,6 +78,9 @@ public class ProjectFactory {
project
.addOverviewImages(parser.parseOverviewLinks(models, imageEntries, params.getVisualizationDir(), zipFile));
}
if (project.getGlyphs().size() > 0) {
assignGlyphsToElements(project);
}
return project;
} catch (IOException e) {
throw new InvalidArgumentException(e);
......@@ -82,4 +88,44 @@ public class ProjectFactory {
throw new InvalidInputDataExecption("Invalid coordinates file. " + e.getMessage(), e);
}
}
private void assignGlyphsToElements(Project project) throws InvalidGlyphFile {
for (ModelData model : project.getModels()) {
for (Element element : model.getElements()) {
Glyph glyph = extractGlyph(project, element.getNotes());
if (glyph != null) {
element.setNotes(removeGlyph(element.getNotes()));
element.setGlyph(glyph);
}
}
}
}
String removeGlyph(String notes) {
String lines[] = notes.split("[\n\r]+");
StringBuilder result = new StringBuilder("");
for (String line : lines) {
if (!line.startsWith("Glyph:")) {
result.append(line + "\n");
}
}
return result.toString();
}
Glyph extractGlyph(Project project, String notes) throws InvalidGlyphFile {
String lines[] = notes.split("[\n\r]+");
for (String line : lines) {
if (line.startsWith("Glyph:")) {
String glyphString = line.replace("Glyph:", "").trim().toLowerCase();
for (Glyph glyph : project.getGlyphs()) {
if (glyph.getFile().getOriginalFileName().toLowerCase().equalsIgnoreCase(glyphString)) {
return glyph;
}
}
throw new InvalidGlyphFile("Glyph file doesn't exist: " + glyphString);
}
}
return null;
}
}
......@@ -8,8 +8,6 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
......@@ -19,133 +17,21 @@ import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import lcsb.mapviewer.common.MimeType;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.common.exception.InvalidClassException;
import lcsb.mapviewer.converter.zip.LayoutZipEntryFile;
import lcsb.mapviewer.converter.zip.ModelZipEntryFile;
import lcsb.mapviewer.model.map.InconsistentModelException;
import lcsb.mapviewer.model.map.layout.Layout;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelFullIndexed;
import lcsb.mapviewer.model.map.model.ModelSubmodelConnection;
import lcsb.mapviewer.model.map.model.SubmodelType;
import lcsb.mapviewer.model.map.reaction.Product;
import lcsb.mapviewer.model.map.reaction.Reactant;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.reaction.type.TransportReaction;
import lcsb.mapviewer.model.map.species.Complex;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.GenericProtein;
import lcsb.mapviewer.model.map.species.Phenotype;
import lcsb.mapviewer.model.map.species.Species;
public class ComplexZipConverterTest {
@SuppressWarnings("unused")
private static Logger logger = Logger.getLogger(ComplexZipConverterTest.class);
public static class MockConverter extends Converter {
@Override
public Model createModel(ConverterParams params) {
Model result = new ModelFullIndexed(null);
Species sa1 = new GenericProtein("sa1");
result.addElement(sa1);
Species sa2 = new GenericProtein("sa2");
result.addElement(sa2);
Species sa3 = new GenericProtein("sa3");
result.addElement(sa3);
Species sa4 = new Phenotype("sa4");
result.addElement(sa4);
Complex ca1 = new Complex("ca1");
ca1.setName("main");
result.addElement(ca1);
Species sa5 = new GenericProtein("sa5");
sa5.setName("sa1");
result.addElement(sa5);
ca1.addSpecies(sa5);
Species sa6 = new Phenotype("sa6");
sa6.setName("sa4");
result.addElement(sa6);
ca1.addSpecies(sa6);
Complex ca2 = new Complex("ca2");
ca2.setName("s1");
result.addElement(ca2);
Species sa7 = new GenericProtein("sa7");
sa7.setName("sa1");
result.addElement(sa7);
ca2.addSpecies(sa7);
Complex ca3 = new Complex("cs3");
ca3.setName("s2");
result.addElement(ca3);
Complex ca4 = new Complex("cs4");
ca4.setName("s3");
result.addElement(ca4);
Reaction r1 = new TransportReaction("re1");
r1.addReactant(new Reactant(sa5));
r1.addProduct(new Product(sa7));
result.addReaction(r1);
Reaction r2 = new TransportReaction("re2");
r2.addReactant(new Reactant(sa6));
r2.addProduct(new Product(ca3));
result.addReaction(r2);
Reaction r3 = new TransportReaction("re3");
r3.addReactant(new Reactant(sa7));
r3.addProduct(new Product(ca4));
result.addReaction(r3);
return result;
}
@Override
public String model2String(Model model) throws InconsistentModelException, ConverterException {
// TODO Auto-generated method stub
return null;
}
@Override
public InputStream model2InputStream(Model model) throws InconsistentModelException, ConverterException {
// TODO Auto-generated method stub
return null;
}
@Override
public File model2File(Model model, String filePath) throws InconsistentModelException {
// TODO Auto-generated method stub
return null;
}
@Override
public String getCommonName() {
// TODO Auto-generated method stub
return null;
}
@Override
public MimeType getMimeType() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getFileExtension() {
// TODO Auto-generated method stub
return null;
}
}
@Before
public void setUp() throws Exception {
}
......
package lcsb.mapviewer.converter;
import java.io.File;
import java.io.InputStream;
import lcsb.mapviewer.common.MimeType;
import lcsb.mapviewer.model.map.InconsistentModelException;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelFullIndexed;
import lcsb.mapviewer.model.map.reaction.Product;
import lcsb.mapviewer.model.map.reaction.Reactant;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.reaction.type.TransportReaction;
import lcsb.mapviewer.model.map.species.Complex;
import lcsb.mapviewer.model.map.species.GenericProtein;
import lcsb.mapviewer.model.map.species.Phenotype;
import lcsb.mapviewer.model.map.species.Species;
public class MockConverter extends Converter {
public static Model modelToBeReturned = null;
@Override
public Model createModel(ConverterParams params) {
if (modelToBeReturned != null) {
return modelToBeReturned;
}
Model result = new ModelFullIndexed(null);
Species sa1 = new GenericProtein("sa1");
result.addElement(sa1);
Species sa2 = new GenericProtein("sa2");
result.addElement(sa2);
Species sa3 = new GenericProtein("sa3");
result.addElement(sa3);
Species sa4 = new Phenotype("sa4");
result.addElement(sa4);
Complex ca1 = new Complex("ca1");
ca1.setName("main");
result.addElement(ca1);
Species sa5 = new GenericProtein("sa5");
sa5.setName("sa1");
result.addElement(sa5);
ca1.addSpecies(sa5);
Species sa6 = new Phenotype("sa6");
sa6.setName("sa4");
result.addElement(sa6);
ca1.addSpecies(sa6);
Complex ca2 = new Complex("ca2");
ca2.setName("s1");
result.addElement(ca2);
Species sa7 = new GenericProtein("sa7");
sa7.setName("sa1");
result.addElement(sa7);
ca2.addSpecies(sa7);
Complex ca3 = new Complex("cs3");
ca3.setName("s2");
result.addElement(ca3);
Complex ca4 = new Complex("cs4");
ca4.setName("s3");
result.addElement(ca4);
Reaction r1 = new TransportReaction("re1");
r1.addReactant(new Reactant(sa5));
r1.addProduct(new Product(sa7));
result.addReaction(r1);
Reaction r2 = new TransportReaction("re2");
r2.addReactant(new Reactant(sa6));
r2.addProduct(new Product(ca3));
result.addReaction(r2);
Reaction r3 = new TransportReaction("re3");
r3.addReactant(new Reactant(sa7));
r3.addProduct(new Product(ca4));
result.addReaction(r3);
return result;
}
@Override
public String model2String(Model model) throws InconsistentModelException, ConverterException {
return null;
}
@Override
public InputStream model2InputStream(Model model) throws InconsistentModelException, ConverterException {
return null;
}
@Override
public File model2File(Model model, String filePath) throws InconsistentModelException {
return null;
}
@Override
public String getCommonName() {
return null;
}
@Override
public MimeType getMimeType() {
return null;
}
@Override
public String getFileExtension() {
return null;
}
}
\ No newline at end of file
package lcsb.mapviewer.converter;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
......@@ -16,18 +17,22 @@ import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import lcsb.mapviewer.converter.ComplexZipConverterTest.MockConverter;
import lcsb.mapviewer.converter.zip.ModelZipEntryFile;
import lcsb.mapviewer.converter.zip.ZipEntryFileFactory;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.OverviewImage;
import lcsb.mapviewer.model.map.OverviewLink;
import lcsb.mapviewer.model.map.OverviewModelLink;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelData;
import lcsb.mapviewer.model.map.model.ModelFullIndexed;
import lcsb.mapviewer.model.map.species.GenericProtein;
public class ProjectFactoryTest {
public class ProjectFactoryTest extends ConverterTestFunctions {
Logger logger = Logger.getLogger(ProjectFactoryTest.class);
int elementCounter = 0;
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
......@@ -38,6 +43,7 @@ public class ProjectFactoryTest {
@After
public void tearDown() throws Exception {
MockConverter.modelToBeReturned = null;
}
@Test
......@@ -153,7 +159,51 @@ public class ProjectFactoryTest {
Project project = projectFactory.create(params);
assertNotNull(project);
assertEquals(1, project.getGlyphs().size());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testParseGlyphsAndPutThemAsElementGlyphs() throws Exception {
try {
Model model = new ModelFullIndexed(null);
GenericProtein protein = createProtein();
protein.setNotes("Glyph: glyphs/g1.png");
model.addElement(protein);
MockConverter.modelToBeReturned = model;
ComplexZipConverter converter = new ComplexZipConverter(MockConverter.class);
ProjectFactory projectFactory = new ProjectFactory(converter);
ComplexZipConverterParams params = new ComplexZipConverterParams();
params.zipFile(new ZipFile("testFiles/complex_with_glyphs.zip"));
params.entry(new ModelZipEntryFile("main.xml", "main", true, false, null));
ZipFile zipFile = new ZipFile("testFiles/complex_with_glyphs.zip");
ZipEntryFileFactory factory = new ZipEntryFileFactory();
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (!entry.isDirectory()) {
params.entry(factory.createZipEntryFile(entry, zipFile));
}
}
Project project = projectFactory.create(params);
assertNotNull(project);
model = project.getModels().iterator().next().getModel();
GenericProtein fetchedProtein = (GenericProtein) model.getElements().iterator().next();
assertFalse("Glyph field should be removed from notes", fetchedProtein.getNotes().contains("Glyph"));
assertNotNull("Glyph in the protein is not defined but should", fetchedProtein.getGlyph());
} catch (Exception e) {
e.printStackTrace();
throw e;
......@@ -161,4 +211,9 @@ public class ProjectFactoryTest {
}
private GenericProtein createProtein() {
GenericProtein result = new GenericProtein("s" + elementCounter++);
return result;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment