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

search by database id implemented

parent 9d1f1ab0
No related branches found
No related tags found
1 merge request!99Resolve "Link from literature file to the related reaction in the map does not work"
......@@ -28,10 +28,10 @@ import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelSubmodelConnection;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.reaction.ReactionNode;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.Complex;
import lcsb.mapviewer.model.map.species.Degraded;
import lcsb.mapviewer.model.map.species.Drug;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.Gene;
import lcsb.mapviewer.model.map.species.Ion;
import lcsb.mapviewer.model.map.species.Phenotype;
......@@ -77,6 +77,12 @@ public class SearchService implements ISearchService {
*/
public static final String REACTION_SEARCH_PREFIX = "reaction";
/**
* Prefix used in search by name interface to limit results only to
* {@link Element}.
*/
private static final String ELEMENT_SEARCH_PREFIX = "element";
/**
* Default class logger.
*/
......@@ -240,6 +246,8 @@ public class SearchService implements ISearchService {
return getReactionById(model, query.replaceFirst(REACTION_SEARCH_PREFIX, "").toLowerCase());
} else if (query.startsWith(SPECIES_SEARCH_PREFIX)) {
result.add(fullAliasViewFactory.create(model.getElementByElementId(query.replaceFirst(SPECIES_SEARCH_PREFIX, ""))));
} else if (query.startsWith(ELEMENT_SEARCH_PREFIX)) {
return getElementById(model, query.replaceFirst(ELEMENT_SEARCH_PREFIX, "").toLowerCase());
} else {
Set<Element> aliases = model.getElements();
......@@ -309,14 +317,45 @@ public class SearchService implements ISearchService {
private List<IHeavyView> getReactionById(Model model, String reactionId) {
Set<Reaction> reactions = model.getReactions();
for (Reaction reaction : reactions) {
if (searchIndexer.getQueryStringForIndex(reaction.getIdReaction().toLowerCase(), new ArrayList<>()).equals(reactionId)) {
return reactionToResultList(reaction);
}
if (Integer.toString(reaction.getId()).equals(reactionId)) {
return reactionToResultList(reaction);
}
}
return new ArrayList<>();
}
/**
* Returns list with the element with a given id. If element with such id
* doesn't exist then empty list is returned.
*
* @param topModel
* where the search is performed
* @param elementId
* id of the element
* @return list that contains element with given id (or empty list if such
* element doesn't exist)
*/
private List<IHeavyView> getElementById(Model topModel, String elementId) {
List<IHeavyView> result = new ArrayList<>();
Set<Model> models = new HashSet<>();
models.add(topModel);
models.addAll(topModel.getSubmodels());
for (Model model : models) {
for (Element element : model.getElements()) {
if (searchIndexer.getQueryStringForIndex(element.getElementId().toLowerCase(), new ArrayList<>()).equals(elementId)) {
result.add(fullAliasViewFactory.create(element));
} else if (Integer.toString(element.getId()).equals(elementId)) {
result.add(fullAliasViewFactory.create(element));
}
}
}
return result;
}
/**
* Transform {@link Reaction} into set of result entries.
*
......
......@@ -31,6 +31,7 @@ import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Complex;
import lcsb.mapviewer.model.map.species.Degraded;
import lcsb.mapviewer.model.map.species.Drug;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.Gene;
import lcsb.mapviewer.model.map.species.GenericProtein;
import lcsb.mapviewer.model.map.species.Ion;
......@@ -50,7 +51,6 @@ import lcsb.mapviewer.services.utils.SearchIndexer;
public class SearchServiceTest extends ServiceTestFunctions {
static Logger logger = Logger.getLogger(SearchServiceTest.class);
private Model model = null;
SearchIndexer indexer = new SearchIndexer();
Map<Class<?>, String> speciesSearchPrefix = new HashMap<Class<?>, String>();
......@@ -75,9 +75,10 @@ public class SearchServiceTest extends ServiceTestFunctions {
speciesSearchReversePrefix.put(prefix, clazz);
}
protected void createFullModel() throws Exception {
model = getModelForFile("testFiles/searchModel.xml", false);
protected Model createFullModel() throws Exception {
Model model = getModelForFile("testFiles/searchModel.xml", false);
model.setProject(new Project("unknown project"));
return model;
}
@After
......@@ -87,7 +88,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
@Test
public void testSearchByName() throws Exception {
try {
createFullModel();
Model model = createFullModel();
long count = searchHistoryDao.getCount();
SearchElementResult result = searchService.searchByQuery(model, "reaction:re21", 50, null, "127.0.0.1");
......@@ -109,7 +110,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
@Test
public void testSearchByName2() throws Exception {
try {
createFullModel();
Model model = createFullModel();
SearchElementResult result = searchService.searchByQuery(model, "BAD:BCL-2", 50, null, "127.0.0.1");
assertNotNull(result);
......@@ -126,7 +127,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
@Test
public void testSearchById() throws Exception {
try {
createFullModel();
Model model = createFullModel();
MiriamData md = new MiriamData(MiriamRelationType.BQ_BIOL_HAS_VERSION, MiriamType.CAS, "123");
model.getElements().iterator().next().addMiriamData(md);
SearchElementResult global = searchService.searchByQuery(model, "CAS:123", 50, null, "127.0.0.1");
......@@ -138,10 +139,25 @@ public class SearchServiceTest extends ServiceTestFunctions {
}
}
@Test
public void testSearchByElementId() throws Exception {
try {
Model model = createFullModel();
Element element =model.getElements().iterator().next();
element.setId(907);
SearchElementResult global = searchService.searchByQuery(model, "element:907", 50, null, "127.0.0.1");
assertNotNull(global);
assertEquals(1, global.size());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testSearchByName4() throws Exception {
try {
createFullModel();
Model model = createFullModel();
SearchElementResult result = searchService.searchByQuery(model, "BC", 50, true, "127.0.0.1");
assertNotNull(result);
......@@ -166,7 +182,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
@Test
public void testQueryName() throws Exception {
try {
createFullModel();
Model model = createFullModel();
SearchElementResult result = searchService.searchByQuery(model, indexer.getQueryStringForIndex("reaction:re21", new HashSet<>()), 50, null, "127.0.0.1");
assertNotNull(result);
......@@ -185,7 +201,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
@Test
public void testQueryReactionId() throws Exception {
try {
createFullModel();
Model model = createFullModel();
model.getReactions().iterator().next().setIdReaction("R_x1");
SearchElementResult result = searchService.searchByQuery(model, indexer.getQueryStringForIndex("reaction:r_x1", new HashSet<>()), 50, null, "127.0.0.1");
......@@ -199,10 +215,30 @@ public class SearchServiceTest extends ServiceTestFunctions {
}
}
@Test
public void testQueryReactionId2() throws Exception {
try {
Model model = createFullModel();
Reaction reaction = model.getReactions().iterator().next();
reaction.setId(154);
reaction.setIdReaction("test-name");
SearchElementResult result = searchService.searchByQuery(model, indexer.getQueryStringForIndex("reaction:154", new HashSet<>()), 50, null, "127.0.0.1");
assertNotNull(result);
assertTrue(result.size() > 0);
FullReactionView resultReaction = (FullReactionView) result.get(0);
logger.debug(resultReaction.getName());
assertTrue(resultReaction.getName().contains("test-name"));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testQueryName2() throws Exception {
try {
createFullModel();
Model model = createFullModel();
model.getModelData().setId(-13);
SearchElementResult result = searchService
.searchByQuery(model, indexer.getQueryStringForIndex("BCL-2", speciesSearchReversePrefix.keySet()), 50, null, "127.0.0.1");
......@@ -219,7 +255,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
@Test
public void testQueryName3() throws Exception {
try {
createFullModel();
Model model = createFullModel();
SearchElementResult result = searchService.searchByQuery(model, "fdhgjkhdsfsdhfgfhsd", 50, null, "127.0.0.1");
assertNotNull(result);
......@@ -233,7 +269,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
@Test
public void testQueryName4() throws Exception {
try {
createFullModel();
Model model = createFullModel();
SearchElementResult result = searchService
.searchByQuery(model, indexer.getQueryStringForIndex("protein:BCL-2", speciesSearchReversePrefix.keySet()), 50, null, "127.0.0.1");
assertNotNull(result);
......@@ -249,7 +285,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
@Test
public void testSearchClosest() throws Exception {
try {
model = getModelForFile("testFiles/graph_path_example3.xml", true);
Model model = getModelForFile("testFiles/graph_path_example3.xml", true);
List<BioEntity> elements = searchService.getClosestElements(model, new Point2D.Double(0, 0), 5, false);
assertNotNull(elements);
assertEquals(5, elements.size());
......@@ -278,7 +314,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
@Test
public void testSearchClosest2() throws Exception {
try {
model = new ModelFullIndexed(null);
Model model = new ModelFullIndexed(null);
GenericProtein protein1 = new GenericProtein("s1");
protein1.setWidth(20);
protein1.setHeight(20);
......@@ -317,7 +353,7 @@ public class SearchServiceTest extends ServiceTestFunctions {
@Test
public void testSearchClosestWithEmptyModel() throws Exception {
try {
model = new ModelFullIndexed(null);
Model model = new ModelFullIndexed(null);
List<BioEntity> elements = searchService.getClosestElements(model, new Point2D.Double(0, 0), 5, false);
assertNotNull(elements);
assertEquals(0, elements.size());
......
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