use of org.eol.globi.util.CypherQuery in project eol-globi-data by jhpoelen.
the class CypherQueryBuilderTest method findDistinctPlantParasiteWithoutLocation.
@Test
public void findDistinctPlantParasiteWithoutLocation() throws IOException {
HashMap<String, String[]> params = new HashMap<String, String[]>();
CypherQuery query = buildInteractionQuery("Homo sapiens", "parasiteOf", "Plantae", params, SINGLE_TAXON_DISTINCT);
assertThat(query.getQuery(), is("START sourceTaxon = node:taxonPaths({source_taxon_name}) MATCH sourceTaxon<-[:CLASSIFIED_AS]-sourceSpecimen-[interaction:" + InteractUtil.interactionsCypherClause(PARASITE_OF) + "]->targetSpecimen-[:CLASSIFIED_AS]->targetTaxon, sourceSpecimen<-[collected_rel:COLLECTED]-study WHERE " + HAS_TARGET_TAXON_PLANTAE + "RETURN sourceTaxon.name as source_taxon_name,interaction.label as interaction_type,collect(distinct(targetTaxon.name)) as target_taxon_name"));
assertThat(query.getParams().toString(), is("{source_taxon_name=path:\\\"Homo sapiens\\\", target_taxon_name=path:\\\"Plantae\\\"}"));
}
use of org.eol.globi.util.CypherQuery in project eol-globi-data by jhpoelen.
the class CypherQueryBuilderTest method findInteractionsAccordingToWithSourceTaxaOnly.
@Test
public void findInteractionsAccordingToWithSourceTaxaOnly() throws IOException {
HashMap<String, String[]> params = new HashMap<String, String[]>() {
{
put("accordingTo", new String[] { "inaturalist" });
put("sourceTaxon", new String[] { "Arthropoda" });
put("field", new String[] { "source_taxon_name", "target_taxon_name" });
}
};
CypherQuery query = buildInteractionQuery(params, MULTI_TAXON_DISTINCT);
assertThat(query.getQuery(), is("START study = node:studies('*:*') WHERE (has(study.externalId) AND study.externalId =~ {accordingTo}) OR (has(study.citation) AND study.citation =~ {accordingTo}) OR (has(study.source) AND study.source =~ {accordingTo}) WITH study " + EXPECTED_MATCH_CLAUSE_DISTINCT + "WHERE " + hasSourceTaxon("Arthropoda") + "WITH distinct targetTaxon, interaction.label as iType, sourceTaxon RETURN sourceTaxon.name as source_taxon_name,targetTaxon.name as target_taxon_name"));
assertThat(query.getParams().toString(), is(is("{accordingTo=.*(\\\\Qinaturalist\\\\E).*, source_taxon_name=path:\\\"Arthropoda\\\"}")));
}
use of org.eol.globi.util.CypherQuery in project eol-globi-data by jhpoelen.
the class CypherQueryBuilderTest method findInteractionsTaxaInteractionIndexTargetTaxaNumberOfInteractions.
@Test
public void findInteractionsTaxaInteractionIndexTargetTaxaNumberOfInteractions() throws IOException {
HashMap<String, String[]> params = new HashMap<String, String[]>() {
{
put("targetTaxon", new String[] { "Arthropoda" });
put("field", new String[] { "source_taxon_name", "target_taxon_name", "number_of_interactions" });
}
};
CypherQuery query = buildInteractionQuery(params, MULTI_TAXON_DISTINCT_BY_NAME_ONLY);
assertThat(query.getQuery(), is("START targetTaxon = node:taxonPaths({target_taxon_name}) " + "MATCH sourceTaxon-[interaction:" + InteractUtil.interactionsCypherClause(INTERACTS_WITH) + "]->targetTaxon " + "RETURN sourceTaxon.name as source_taxon_name,targetTaxon.name as target_taxon_name,interaction.count as number_of_interactions"));
Map<String, String> expected = new HashMap<String, String>() {
{
put("target_taxon_name", "path:\\\"Arthropoda\\\"");
}
};
assertThat(query.getParams(), is(expected));
}
use of org.eol.globi.util.CypherQuery in project eol-globi-data by jhpoelen.
the class CypherQueryBuilderTest method findInteractions.
@Test
public void findInteractions() throws IOException {
HashMap<String, String[]> params = new HashMap<String, String[]>() {
{
put("sourceTaxon", new String[] { "" });
put("targetTaxon", new String[] { "" });
put("field", new String[] { "source_taxon_name", "interaction_type", "target_taxon_name" });
}
};
String expectedQuery = "START sourceTaxon = node:taxons('*:*') MATCH sourceTaxon-[interaction:INTERACTS_WITH|PREYS_UPON|PARASITE_OF|HAS_HOST|HOST_OF|POLLINATES|PERCHING_ON|ATE|SYMBIONT_OF|PREYED_UPON_BY|POLLINATED_BY|EATEN_BY|HAS_PARASITE|PERCHED_ON_BY|HAS_PATHOGEN|PATHOGEN_OF|HAS_VECTOR|VECTOR_OF|VISITED_BY|VISITS|FLOWERS_VISITED_BY|VISITS_FLOWERS_OF|INHABITED_BY|INHABITS|ADJACENT_TO|CREATES_HABITAT_FOR|IS_HABITAT_OF|LIVED_ON_BY|LIVES_ON|LIVED_INSIDE_OF_BY|LIVES_INSIDE_OF|LIVED_NEAR_BY|LIVES_NEAR|LIVED_UNDER_BY|LIVES_UNDER|LIVES_WITH|ENDOPARASITE_OF|HAS_ENDOPARASITE|HYPERPARASITE_OF|HAS_HYPERPARASITE|HYPERPARASITOID_OF|HAS_HYPERPARASITOID|ECTOPARASITE_OF|HAS_ECTOPARASITE|KLEPTOPARASITE_OF|HAS_KLEPTOPARASITE|PARASITOID_OF|HAS_PARASITOID|ENDOPARASITOID_OF|HAS_ENDOPARASITOID|ECTOPARASITOID_OF|HAS_ECTOPARASITOID|GUEST_OF|HAS_GUEST_OF|FARMED_BY|FARMS|DAMAGED_BY|DAMAGES|DISPERSAL_VECTOR_OF|HAS_DISPERAL_VECTOR|KILLED_BY|KILLS|EPIPHITE_OF|HAS_EPIPHITE|LAYS_EGGS_ON|HAS_EGGS_LAYED_ON_BY]->targetTaxon RETURN sourceTaxon.name as source_taxon_name,interaction.label? as interaction_type,targetTaxon.name as target_taxon_name";
CypherQuery query = buildInteractionQuery(params, MULTI_TAXON_DISTINCT_BY_NAME_ONLY);
assertThat(query.getQuery(), is(expectedQuery));
assertThat(query.getParams().toString(), is("{}"));
}
use of org.eol.globi.util.CypherQuery in project eol-globi-data by jhpoelen.
the class TaxonSearchImpl method taxonLinks.
@RequestMapping(value = "/taxonLinks/{taxonPath}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
@ResponseBody
public Collection<String> taxonLinks(@PathVariable("taxonPath") final String taxonPath, HttpServletRequest request) throws IOException {
final String pathQuery = CypherQueryBuilder.lucenePathQuery(Collections.singletonList(taxonPath), true);
StringBuilder query = new StringBuilder("START someTaxon = node:taxons({pathQuery}) ");
Map<ResultField, String> selectors = new HashMap<ResultField, String>() {
{
put(ResultField.TAXON_EXTERNAL_ID, "externalId");
put(ResultField.TAXON_EXTERNAL_URL, "externalUrl");
}
};
ResultField[] returnFieldsCloseMatches = new ResultField[] { ResultField.TAXON_EXTERNAL_ID, ResultField.TAXON_EXTERNAL_URL };
List<String> requestedFields = new ArrayList<String>();
if (request != null) {
requestedFields.addAll(CypherQueryBuilder.collectRequestedFields(request.getParameterMap()));
}
query.append(" MATCH someTaxon-[:SAME_AS*0..1]->taxon WHERE has(taxon.externalId) WITH DISTINCT(taxon.externalId) as externalId, taxon.externalUrl? as externalUrl ");
CypherReturnClauseBuilder.appendReturnClauseDistinctz(query, CypherReturnClauseBuilder.actualReturnFields(requestedFields, Arrays.asList(returnFieldsCloseMatches), selectors.keySet()), selectors);
final CypherQuery query1 = new CypherQuery(query.toString(), new HashMap() {
{
put("pathQuery", pathQuery);
}
});
final CypherQuery pagedQuery = CypherQueryBuilder.createPagedQuery(request, query1, 15);
final String response = CypherUtil.executeRemote(pagedQuery);
JsonNode node = new ObjectMapper().readTree(response);
JsonNode dataNode = node.get("data");
Collection<String> links = new HashSet<>();
if (dataNode != null) {
for (JsonNode jsonNode : dataNode) {
if (jsonNode.isArray() && jsonNode.size() > 1) {
String externalId = jsonNode.get(0).asText();
String externalUrl = jsonNode.get(1).asText();
String resolvedUrl = ExternalIdUtil.urlForExternalId(externalId);
if (StringUtils.isNotBlank(resolvedUrl)) {
links.add(resolvedUrl);
} else if (StringUtils.isNotBlank(externalUrl)) {
links.add(externalUrl);
}
}
}
}
return links;
}
Aggregations