Commit e65950e7 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

state variables that cannot be translated to residue are transformed to structural state

parent dfdbb89b
......@@ -10,8 +10,8 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.sbgn.*;
import org.sbgn.bindings.*;
import org.sbgn.bindings.Map;
import org.sbgn.bindings.Arc.*;
import org.sbgn.bindings.Map;
import lcsb.mapviewer.common.comparator.DoubleComparator;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
......@@ -202,7 +202,13 @@ public class SbgnmlXmlExporter {
glyph.setClazz(GlyphClazz.STATE_VARIABLE.getClazz());
Glyph.State state = new Glyph.State();
state.setValue(structuralState);
if (structuralState.indexOf("@") >= 0) {
state.setValue(structuralState.substring(0, structuralState.indexOf("@")));
state.setVariable(structuralState.substring(structuralState.indexOf("@") + 1));
} else {
state.setValue(structuralState);
}
glyph.setState(state);
Bbox bbox = new Bbox();
......
package lcsb.mapviewer.converter.model.sbgnml;
import java.awt.*;
import java.awt.Color;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.io.File;
......@@ -584,13 +584,30 @@ public class SbgnmlXmlParser {
for (Glyph child : children) {
if (GlyphClazz.fromClazz(child.getClazz()).equals(GlyphClazz.STATE_VARIABLE)) {
if (child.getState() == null || child.getState().getVariable() != null) {
try {
parseStateVariable(child, newSpecies);
} catch (Exception ex) {
logger.warn(ex.getMessage());
Residue residue = stateVariableToResidue(child);
if (residue != null) {
if (newSpecies instanceof Protein) {
((Protein) newSpecies).addResidue(residue);
} else if (newSpecies instanceof Complex) {
String state = createStructuralState(child);
Complex complex = (Complex) newSpecies;
complex.setStructuralState(state);
} else {
logger.warn("Only macromolecule elements can have state variables.");
}
} else {
String state = createStructuralState(child);
if (newSpecies instanceof Protein) {
((Protein) newSpecies).setStructuralState(state);
} else if (newSpecies instanceof Complex) {
Complex complex = (Complex) newSpecies;
complex.setStructuralState(state);
} else {
logger.warn("Only macromolecule elements can have state.");
}
}
} else {
String structuralState = child.getState().getValue();
String structuralState = createStructuralState(child);
if (newSpecies instanceof Protein) {
Protein protein = (Protein) newSpecies;
protein.setStructuralState(structuralState);
......@@ -608,6 +625,64 @@ public class SbgnmlXmlParser {
}
/**
* Method used to parse state variable.
*
* @param glyph
* unit of information glyph from sbgn-ml file
* @throws Exception
* Exception is thrown if state variable is parsed for species other
* than Protein
*/
private Residue stateVariableToResidue(Glyph glyph) {
if (glyph.getState() != null && (glyph.getState().getVariable() == null
|| glyph.getState().getVariable().trim().isEmpty())) {
return null;
}
Residue mr = new Residue();
mr.setIdModificationResidue(glyph.getId());
if (glyph.getState() != null) {
// If State variable consists of value and variable
mr.setName(glyph.getState().getVariable());
for (ModificationState ms : ModificationState.values()) {
if (ms.getAbbreviation().equals(glyph.getState().getValue())) {
mr.setState(ms);
}
}
}
if (mr.getState() == null) {
return null;
}
// Compute the angle from coordinates and dimensions
double x = glyph.getBbox().getX() + glyph.getBbox().getW() / 2;
double y = glyph.getBbox().getY() + glyph.getBbox().getH() / 2;
mr.setPosition(new Point2D.Double(x, y));
return mr;
}
private String createStructuralState(Glyph glyph) {
if (glyph.getState() == null) {
logger.warn("Invalid structural state");
return null;
}
String result = "";
if (glyph.getState().getValue() != null) {
result += glyph.getState().getValue();
}
if (glyph.getState().getVariable() != null && !glyph.getState().getVariable().isEmpty()) {
result += "@";
result += glyph.getState().getVariable();
}
return result;
}
/**
* {@link ModificationResidue} in element might have slightly off coordinates
* (due to different symbol shapes). For that we need to align them to match our
......
......@@ -105,4 +105,18 @@ public class SbgnmlXmlParserTest2 extends SbgnmlTestFunctions {
assertEquals(p2.getLine().getBeginPoint(), outputOperator.getLine().getBeginPoint());
}
@Test
public void testImportExportStateWithVariable() throws Exception {
Converter converter = new SbgnmlXmlConverter();
Model model = converter.createModel(
new ConverterParams().filename("testFiles/sbgnmlParserTestFiles/sbgnmlFiles/state_with_variable.sbgn"));
String sbgn = converter.model2String(model);
logger.debug(sbgn);
assertTrue("State variable is missing", sbgn.contains("state variable"));
assertTrue("State variable doesn't contain variable", sbgn.contains("variable=\"g\""));
}
}
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<sbgn xmlns="http://sbgn.org/libsbgn/0.2">
<map language="process description">
<glyph id="nwtN_a529363c-d32c-4b03-bce7-1b79e6066b2d" class="complex">
<label text="complex"/>
<bbox x="1001.671875" y="148.375" w="63.25" h="33.25"/>
<glyph id="nwtSV_439e2238-3d15-46da-aa67-77a1119a3552" class="state variable">
<state value="xxx" variable="g"/>
<bbox x="1049.1945435615057" y="142.375" w="22.39697265625" h="12"/>
</glyph>
<glyph id="nwtN_d5109825-20cd-40f4-a9ba-1fd707c6b2a1" class="macromolecule">
<label text="macromolecule"/>
<bbox x="1018.296875" y="165" w="60" h="30"/>
</glyph>
</glyph>
</map>
</sbgn>
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment