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

default populator of z-index implemented

parent 40db452e
No related branches found
No related tags found
1 merge request!768Resolve "Custom images as overlay levels or background"
package lcsb.mapviewer.converter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.apache.log4j.Logger;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.common.comparator.DoubleComparator;
import lcsb.mapviewer.common.comparator.StringComparator;
import lcsb.mapviewer.model.map.BioEntity;
import lcsb.mapviewer.model.map.compartment.Compartment;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Complex;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.Species;
/**
* This util class populate with z-index data if necessary.
*
* @author Piotr Gawron
*
*/
public class ZIndexPopulator {
@SuppressWarnings("unused")
private static Logger logger = Logger.getLogger(ZIndexPopulator.class);
private DoubleComparator doubleComparator = new DoubleComparator(Configuration.EPSILON);
private StringComparator stringComparator = new StringComparator();
/**
* Populate all elements that don't have z-index with valid values. Elements
* without z-index would have bigger z-values than elements with existing
* z-values.
*
* @param model
*/
public void populateZIndex(Model model) {
int maxZIndex = 0;
List<BioEntity> bioEntities = new ArrayList<>();
bioEntities.addAll(model.getBioEntities());
bioEntities.sort(new Comparator<BioEntity>() {
@Override
public int compare(BioEntity o1, BioEntity o2) {
if (o1 instanceof Reaction) {
if (o2 instanceof Reaction) {
return stringComparator.compare(o1.getElementId(), o2.getElementId());
}
return -1;
}
if (o2 instanceof Reaction) {
return 1;
}
Element e1 = (Element) o1;
Element e2 = (Element) o2;
if (isChildren(e1, e2)) {
return -1;
}
if (isChildren(e2, e1)) {
return 1;
}
int result = -doubleComparator.compare(e1.getSize(), e2.getSize());
if (result == 0) {
result = stringComparator.compare(e1.getElementId(), e2.getElementId());
}
return result;
}
private boolean isChildren(Element e1, Element e2) {
if (e1 instanceof Complex && e2 instanceof Species) {
Complex parent = (Complex) e1;
Species child = (Species) e2;
return parent.getAllSimpleChildren().contains(child);
}
if (e1 instanceof Compartment) {
Compartment parent = (Compartment) e1;
return parent.getAllSubElements().contains(e2);
}
return false;
}
});
for (BioEntity bioEntity : bioEntities) {
if (bioEntity.getZ() != null) {
maxZIndex = Math.max(maxZIndex, bioEntity.getZ());
}
}
for (BioEntity bioEntity : bioEntities) {
if (bioEntity.getZ() == null) {
bioEntity.setZ(++maxZIndex);
}
}
}
}
......@@ -12,6 +12,7 @@ import lcsb.mapviewer.converter.annotation.XmlAnnotationParserTest;
OverviewParserTest.class,
ProjectFactoryTest.class,
XmlAnnotationParserTest.class,
ZIndexPopulatorTest.class
})
public class AllTests {
......
package lcsb.mapviewer.converter;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.apache.log4j.Logger;
import org.junit.Test;
import lcsb.mapviewer.model.map.compartment.Compartment;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelFullIndexed;
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.Species;
public class ZIndexPopulatorTest {
Logger logger = Logger.getLogger(ZIndexPopulatorTest.class);
int identifierCounter = 0;
@Test
public void testMixZIndexValuesInSpecies() {
Model model = new ModelFullIndexed(null);
Species speciesWithoutZIndex = createProtein();
model.addElement(speciesWithoutZIndex);
Species speciesWithZIndex = createProtein();
speciesWithZIndex.setZ(100);
model.addElement(speciesWithZIndex);
ZIndexPopulator populator = new ZIndexPopulator();
populator.populateZIndex(model);
assertNotNull(speciesWithoutZIndex.getZ());
assertTrue(speciesWithoutZIndex.getZ() > speciesWithZIndex.getZ());
}
@Test
public void testZIndexBySpeciesSize() {
Model model = new ModelFullIndexed(null);
Species microSpecies = createProtein();
microSpecies.setWidth(1);
microSpecies.setHeight(1);
model.addElement(microSpecies);
Species biggerSpecies = createProtein();
biggerSpecies.setWidth(100);
biggerSpecies.setHeight(100);
model.addElement(biggerSpecies);
Species smallerSpecies = createProtein();
smallerSpecies.setWidth(10);
smallerSpecies.setHeight(10);
model.addElement(smallerSpecies);
ZIndexPopulator populator = new ZIndexPopulator();
populator.populateZIndex(model);
assertTrue(biggerSpecies.getZ() < smallerSpecies.getZ());
assertTrue(smallerSpecies.getZ() < microSpecies.getZ());
}
@Test
public void testZIndexReactionShouldBeBeforeElement() {
Model model = new ModelFullIndexed(null);
Species species = createProtein();
species.setWidth(1);
species.setHeight(1);
model.addElement(species);
Reaction reaction = createReaction();
model.addReaction(reaction);
ZIndexPopulator populator = new ZIndexPopulator();
populator.populateZIndex(model);
assertTrue(reaction.getZ() < species.getZ());
}
@Test
public void testZIndexEqualSizeByElementId() {
Model model = new ModelFullIndexed(null);
Species firstSpecies = createProtein();
firstSpecies.setWidth(1);
firstSpecies.setHeight(1);
model.addElement(firstSpecies);
Species secondSpecies = createProtein();
secondSpecies.setWidth(1);
secondSpecies.setHeight(1);
model.addElement(secondSpecies);
Species thirdSpecies = createProtein();
thirdSpecies.setWidth(1);
thirdSpecies.setHeight(1);
model.addElement(thirdSpecies);
ZIndexPopulator populator = new ZIndexPopulator();
populator.populateZIndex(model);
assertTrue(firstSpecies.getZ() < secondSpecies.getZ());
assertTrue(secondSpecies.getZ() < thirdSpecies.getZ());
}
@Test
public void testZIndexComplexChildren() {
Model model = new ModelFullIndexed(null);
Complex complex = createComplex();
complex.setWidth(1);
complex.setHeight(1);
model.addElement(complex);
Species child = createProtein();
child.setWidth(10);
child.setHeight(10);
model.addElement(child);
complex.addSpecies(child);
ZIndexPopulator populator = new ZIndexPopulator();
populator.populateZIndex(model);
assertTrue(complex.getZ() < child.getZ());
}
@Test
public void testZIndexCompartmentChildren() {
Model model = new ModelFullIndexed(null);
Compartment compartment = createCompartment();
compartment.setWidth(1);
compartment.setHeight(1);
model.addElement(compartment);
Compartment childCompartment = createCompartment();
childCompartment.setWidth(2);
childCompartment.setHeight(2);
model.addElement(childCompartment);
compartment.addElement(childCompartment);
Species child = createProtein();
child.setWidth(10);
child.setHeight(10);
model.addElement(child);
childCompartment.addElement(child);
ZIndexPopulator populator = new ZIndexPopulator();
populator.populateZIndex(model);
assertTrue(compartment.getZ() < childCompartment.getZ());
assertTrue(childCompartment.getZ() < child.getZ());
}
private Species createProtein() {
GenericProtein result = new GenericProtein("s" + identifierCounter++);
return result;
}
private Complex createComplex() {
Complex result = new Complex("s" + identifierCounter++);
return result;
}
private Compartment createCompartment() {
Compartment result = new Compartment("s" + identifierCounter++);
return result;
}
private Reaction createReaction () {
Reaction result = new TransportReaction("s" + identifierCounter++);
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