diff --git a/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/bioEntities/elements/ElementsRestImpl.java b/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/bioEntities/elements/ElementsRestImpl.java index 087f49f4b5dc44ead6eebba48b8767b657f6d372..996005a94edd4b316c0a0302dc97f7665ed93d69 100644 --- a/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/bioEntities/elements/ElementsRestImpl.java +++ b/rest-api/src/main/java/lcsb/mapviewer/api/projects/models/bioEntities/elements/ElementsRestImpl.java @@ -13,12 +13,16 @@ import org.springframework.transaction.annotation.Transactional; import lcsb.mapviewer.api.BaseRestImpl; import lcsb.mapviewer.model.map.model.Model; +import lcsb.mapviewer.model.map.species.AntisenseRna; import lcsb.mapviewer.model.map.species.Element; +import lcsb.mapviewer.model.map.species.Protein; +import lcsb.mapviewer.model.map.species.Rna; import lcsb.mapviewer.model.map.species.Species; +import lcsb.mapviewer.model.map.species.field.ElementModification; +import lcsb.mapviewer.model.map.species.field.ModificationResidue; +import lcsb.mapviewer.model.map.species.field.RnaRegion; import lcsb.mapviewer.services.SecurityException; import lcsb.mapviewer.services.UserAccessException; -import lcsb.mapviewer.services.interfaces.IModelService; -import lcsb.mapviewer.services.view.AnnotationViewFactory; import lcsb.mapviewer.services.view.OverviewImageViewFactory; @Transactional(value = "txManager") @@ -30,9 +34,6 @@ public class ElementsRestImpl extends BaseRestImpl { @SuppressWarnings("unused") private Logger logger = Logger.getLogger(ElementsRestImpl.class); - @Autowired - private IModelService modelService; - @Autowired private OverviewImageViewFactory factory; @@ -109,6 +110,8 @@ public class ElementsRestImpl extends BaseRestImpl { value = element.getFormula(); } else if (column.equals("notes")) { value = element.getNotes(); + } else if (column.equals("others")) { + value = getOthersForElement(element); } else if (column.equals("formersymbols")) { value = element.getFormerSymbols(); } else if (column.equals("hierarchyvisibilitylevel")) { @@ -127,6 +130,41 @@ public class ElementsRestImpl extends BaseRestImpl { return result; } + protected Map<String, Object> getOthersForElement(Element element) { + Map<String, Object> result = new HashMap<>(); + List<Map<String, Object>> modifications = new ArrayList<>(); + String structuralState = null; + if (element instanceof Protein) { + Protein protein = ((Protein) element); + modifications = getModifications(protein.getModificationResidues()); + structuralState = protein.getStructuralState(); + } else if (element instanceof Rna) { + Rna rna = ((Rna) element); + modifications = getModifications(((Rna) element).getRegions()); + structuralState = rna.getState(); + } else if (element instanceof AntisenseRna) { + AntisenseRna antisenseRna = ((AntisenseRna) element); + modifications = getModifications(((AntisenseRna) element).getRegions()); + structuralState = antisenseRna.getState(); + } + result.put("modifications", modifications); + result.put("structuralState", structuralState); + + return result; + } + + private List<Map<String, Object>> getModifications(List<? extends ElementModification> elements) { + List<Map<String, Object>> result = new ArrayList<>(); + for (ElementModification region : elements) { + Map<String, Object> row = new HashMap<>(); + row.put("name", region.getName()); + row.put("state", region.getState().name()); + result.add(row); + } + return result; + } + + private Map<String, Object> createBounds(Double x, Double y, Double width, Double height) { Map<String, Object> result = new HashMap<>(); result.put("x", x); @@ -159,6 +197,7 @@ public class ElementsRestImpl extends BaseRestImpl { columnsSet.add("bounds"); columnsSet.add("hierarchyVisibilityLevel"); columnsSet.add("linkedSubmodel"); + columnsSet.add("others"); } else { for (String str : columns.split(",")) { columnsSet.add(str); @@ -167,23 +206,6 @@ public class ElementsRestImpl extends BaseRestImpl { return columnsSet; } - /** - * @return the modelService - * @see #modelService - */ - public IModelService getModelService() { - return modelService; - } - - /** - * @param modelService - * the modelService to set - * @see #modelService - */ - public void setModelService(IModelService modelService) { - this.modelService = modelService; - } - /** * @return the factory * @see #factory diff --git a/rest-api/src/test/java/lcsb/mapviewer/api/projects/models/bioEntities/elements/ElementRestImplTest.java b/rest-api/src/test/java/lcsb/mapviewer/api/projects/models/bioEntities/elements/ElementRestImplTest.java index 8cc6d689ba44e9c600f646ff1290167135c709d9..91127938e5a200c328337136d6265bbb86f3c96b 100644 --- a/rest-api/src/test/java/lcsb/mapviewer/api/projects/models/bioEntities/elements/ElementRestImplTest.java +++ b/rest-api/src/test/java/lcsb/mapviewer/api/projects/models/bioEntities/elements/ElementRestImplTest.java @@ -21,7 +21,13 @@ import com.fasterxml.jackson.databind.ObjectMapper; import lcsb.mapviewer.api.RestTestFunctions; import lcsb.mapviewer.model.map.model.Model; +import lcsb.mapviewer.model.map.species.AntisenseRna; import lcsb.mapviewer.model.map.species.GenericProtein; +import lcsb.mapviewer.model.map.species.Rna; +import lcsb.mapviewer.model.map.species.field.AntisenseRnaRegion; +import lcsb.mapviewer.model.map.species.field.ModificationResidue; +import lcsb.mapviewer.model.map.species.field.ModificationState; +import lcsb.mapviewer.model.map.species.field.RnaRegion; import lcsb.mapviewer.services.interfaces.IModelService; public class ElementRestImplTest extends RestTestFunctions { @@ -47,7 +53,7 @@ public class ElementRestImplTest extends RestTestFunctions { @Test public void testGetElementsProcessAllColumns() throws Exception { try { - ElementsRestImpl projectRest = createMockProjectRest("testFiles/model/sample.xml"); + ElementsRestImpl projectRest = createMockElementRest("testFiles/model/sample.xml"); List<Map<String, Object>> result = projectRest.getElements("sample", "", "", "*", token.getId(), ""); for (Map<String, Object> element : result) { for (String paramName : element.keySet()) { @@ -72,8 +78,8 @@ public class ElementRestImplTest extends RestTestFunctions { public void testGetElementsByType() throws Exception { try { String proteinType = new GenericProtein("1").getStringType(); - ElementsRestImpl projectRest = createMockProjectRest("testFiles/model/sample.xml"); - List<Map<String, Object>> result = projectRest.getElements("sample", "", "", "*", token.getId(), proteinType); + ElementsRestImpl elementRest = createMockElementRest("testFiles/model/sample.xml"); + List<Map<String, Object>> result = elementRest.getElements("sample", "", "", "*", token.getId(), proteinType); assertEquals(12, result.size()); } catch (Exception e) { @@ -82,7 +88,70 @@ public class ElementRestImplTest extends RestTestFunctions { } } - private ElementsRestImpl createMockProjectRest(String string) throws Exception { + @Test + public void testGetModificationsForProtein() throws Exception { + try { + String state = "asd"; + GenericProtein protein = new GenericProtein("s1"); + ModificationResidue mr = new ModificationResidue(); + mr.setState(ModificationState.ACETYLATED); + mr.setName("S250"); + protein.addModificationResidue(mr); + protein.setStructuralState(state); + Map<String, Object> result = _elementsRestImpl.getOthersForElement(protein); + assertNotNull(result.get("modifications")); + assertEquals(state, result.get("structuralState")); + List<Map<String, Object>> modifications = (List<Map<String, Object>>) result.get("modifications"); + assertEquals(1, modifications.size()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testGetModificationsForRna() throws Exception { + try { + String state = "asd"; + Rna rna = new Rna("s1"); + RnaRegion mr = new RnaRegion(); + mr.setState(ModificationState.ACETYLATED); + mr.setName("S250"); + rna.addRegion(mr); + rna.setState(state); + Map<String, Object> result = _elementsRestImpl.getOthersForElement(rna); + assertNotNull(result.get("modifications")); + assertEquals(state, result.get("structuralState")); + List<Map<String, Object>> modifications = (List<Map<String, Object>>) result.get("modifications"); + assertEquals(1, modifications.size()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + @Test + public void testGetModificationsForAntisenseRna() throws Exception { + try { + String state = "asd"; + AntisenseRna antisenseRna = new AntisenseRna("s1"); + AntisenseRnaRegion mr = new AntisenseRnaRegion(); + mr.setState(ModificationState.ACETYLATED); + mr.setName("S250"); + antisenseRna.addRegion(mr); + antisenseRna.setState(state); + Map<String, Object> result = _elementsRestImpl.getOthersForElement(antisenseRna); + assertNotNull(result.get("modifications")); + assertEquals(state, result.get("structuralState")); + List<Map<String, Object>> modifications = (List<Map<String, Object>>) result.get("modifications"); + assertEquals(1, modifications.size()); + } catch (Exception e) { + e.printStackTrace(); + throw e; + } + } + + private ElementsRestImpl createMockElementRest(String string) throws Exception { Model model = super.getModelForFile(string, true); IModelService mockModelService = Mockito.mock(IModelService.class); Mockito.when(mockModelService.getLastModelByProjectId(anyString(), any())).thenReturn(model);