use of org.eol.globi.util.NodeTypeDirection in project eol-globi-data by jhpoelen.
the class ExporterMeasurementOrFact method doExportStudy.
@Override
public void doExportStudy(StudyNode study, ExportUtil.Appender writer, boolean includeHeader) throws IOException {
Map<String, String> properties = new TreeMap<String, String>();
AtomicReference<IOException> lastException = new AtomicReference<>();
RelationshipListener handler = collectedRel -> {
Node specimenNode = collectedRel.getEndNode();
if (isSpecimenClassified(specimenNode)) {
try {
writeMeasurements(writer, properties, specimenNode, collectedRel, study);
} catch (IOException ex) {
lastException.set(ex);
}
}
};
NodeUtil.handleCollectedRelationships(new NodeTypeDirection(study.getUnderlyingNode()), handler);
if (lastException.get() != null) {
throw lastException.get();
}
}
use of org.eol.globi.util.NodeTypeDirection in project eol-globi-data by jhpoelen.
the class IndexInteractionsTest method indexInteractions.
@Test
public void indexInteractions() throws NodeFactoryException {
TaxonIndex taxonIndex = getOrCreateTaxonIndex();
// see https://github.com/globalbioticinteractions/globalbioticinteractions/wiki/Nanopubs
StudyImpl study = new StudyImpl("some study", new DOI("123.23", "222"), "some study citation");
NodeFactoryWithDatasetContext factory = new NodeFactoryWithDatasetContext(nodeFactory, new DatasetImpl("some/namespace", URI.create("https://some.uri"), inStream -> inStream));
Study interaction = factory.getOrCreateStudy(study);
TaxonImpl donaldTaxon = new TaxonImpl("donald duck", "NCBI:1234");
Specimen donald = factory.createSpecimen(interaction, donaldTaxon);
donald.classifyAs(taxonIndex.getOrCreateTaxon(donaldTaxon));
TaxonImpl mickeyTaxon = new TaxonImpl("mickey mouse", "NCBI:4444");
Taxon mickeyTaxonNCBI = taxonIndex.getOrCreateTaxon(new TaxonImpl("mickey mouse", "EOL:567"));
NodeUtil.connectTaxa(mickeyTaxon, (TaxonNode) mickeyTaxonNCBI, getGraphDb(), RelTypes.SAME_AS);
Specimen mickey = factory.createSpecimen(interaction, mickeyTaxon);
mickey.classifyAs(taxonIndex.getOrCreateTaxon(mickeyTaxon));
donald.ate(mickey);
new IndexInteractions(new GraphServiceFactoryProxy(getGraphDb())).index();
NodeFactoryNeo4j nodeFactoryNeo4j = new NodeFactoryNeo4j2(getGraphDb());
StudyImpl study1 = new StudyImpl("some study", new DOI("123.23", "222"), "come citation");
study1.setOriginatingDataset(new DatasetImpl("some/namespace", URI.create("some:uri"), inStream -> inStream));
StudyNode someStudy = nodeFactoryNeo4j.getOrCreateStudy(study1);
assertThat(interaction.getOriginatingDataset().getNamespace(), is(someStudy.getOriginatingDataset().getNamespace()));
assertThat(interaction.getTitle(), is(someStudy.getTitle()));
RelationshipType hasParticipant = NodeUtil.asNeo4j(RelTypes.HAS_PARTICIPANT);
Set<Long> ids = new HashSet<>();
List<Long> idList = new ArrayList<>();
NodeUtil.handleCollectedRelationships(new NodeTypeDirection(someStudy.getUnderlyingNode()), new RelationshipListener() {
@Override
public void on(Relationship specimen) {
assertThat(specimen.getEndNode().hasRelationship(Direction.INCOMING, hasParticipant), Is.is(true));
Iterable<Relationship> relationships = specimen.getEndNode().getRelationships(hasParticipant, Direction.INCOMING);
for (Relationship relationship : relationships) {
long id = relationship.getStartNode().getId();
ids.add(id);
idList.add(id);
}
}
});
assertThat(ids.size(), Is.is(1));
assertThat(idList.size(), Is.is(2));
Node interactionNode = getGraphDb().getNodeById(idList.get(0));
assertTrue(interactionNode.hasRelationship(Direction.OUTGOING, NodeUtil.asNeo4j(RelTypes.DERIVED_FROM)));
assertTrue(interactionNode.hasRelationship(Direction.OUTGOING, NodeUtil.asNeo4j(RelTypes.ACCESSED_AT)));
}
use of org.eol.globi.util.NodeTypeDirection in project eol-globi-data by jhpoelen.
the class StudyTest method populateStudy.
@Test
public void populateStudy() throws NodeFactoryException {
Study study = nodeFactory.createStudy(new StudyImpl("Our first study", null, null));
taxonIndex.getOrCreateTaxon(new TaxonImpl(CARCHARODON_CARCHARIAS, null));
Specimen goldFish = nodeFactory.createSpecimen(study, new TaxonImpl(CARASSIUS_AURATUS_AURATUS, null));
Specimen shark = nodeFactory.createSpecimen(study, new TaxonImpl(CARCHARODON_CARCHARIAS, null));
Specimen fuzzyShark = nodeFactory.createSpecimen(study, new TaxonImpl(CARCHARODON, null));
shark.ate(goldFish);
fuzzyShark.ate(goldFish);
Location bolinasBay = nodeFactory.getOrCreateLocation(new LocationImpl(12.2d, 12.1d, -100.0d, null));
shark.caughtIn(bolinasBay);
Season winter = nodeFactory.createSeason("winter");
shark.caughtDuring(winter);
shark.setLengthInMm(1.2d);
resolveNames();
StudyNode foundStudy = getStudySingleton(getGraphDb());
assertEquals(study.getTitle(), foundStudy.getTitle());
RelationshipListener handler = rel -> {
Specimen specimen = new SpecimenNode(rel.getEndNode());
Relationship caughtDuringRel = rel.getEndNode().getSingleRelationship(NodeUtil.asNeo4j(RelTypes.CAUGHT_DURING), Direction.OUTGOING);
if (caughtDuringRel != null) {
Node seasonNode = caughtDuringRel.getEndNode();
if (seasonNode != null && seasonNode.getProperty(SeasonNode.TITLE).equals("winter")) {
Relationship next = NodeUtil.getClassifications(specimen).iterator().next();
Node endNode = next.getEndNode();
String speciesName = (String) endNode.getProperty("name");
assertEquals(CARCHARODON_CARCHARIAS, speciesName);
assertEquals(new Double(-100.0d), specimen.getSampleLocation().getAltitude());
assertEquals(new Double(1.2d), specimen.getLengthInMm());
} else {
fail("expected to findNamespaces a specimen");
}
} else if (specimen.equals(goldFish)) {
Node genusNode = NodeUtil.getClassifications(specimen).iterator().next().getEndNode();
assertEquals(CARASSIUS_AURATUS_AURATUS, genusNode.getProperty("name"));
} else if (specimen.equals(fuzzyShark)) {
Node genusNode = NodeUtil.getClassifications(specimen).iterator().next().getEndNode();
assertEquals(CARCHARODON, genusNode.getProperty("name"));
} else {
fail("found unexpected specimen [" + specimen + "] in study");
}
};
NodeUtil.handleCollectedRelationships(new NodeTypeDirection(foundStudy.getUnderlyingNode()), handler);
}
use of org.eol.globi.util.NodeTypeDirection in project eol-globi-data by jhpoelen.
the class DatasetImporterForBlewettTest method importLines.
@Test
public void importLines() throws StudyImporterException {
String predatorPreyMapping = "\"Collection #\",\"Sp#\",\"Standard Length\",\"ID\",\"Far duoraum\",\"Cal sapidus\",\"Unid fish\",\"Anchoa spp\",\"Mug gyrans\",\"Bai chrysoura\",\"Portunus spp\",\"Bivalves\",\"Portunidae\",\"Lag rhomboides\",\"Xanthidae\",\"Palaemonidae\",\"Eucinostomus spp\",\"Mugil spp\",\"Alpheidae\",\"Atherinidae\",\"Syn foetens\",\"Ort chrysoptera\",\"Snails\",\"Euc gula\",\"Cynoscion spp\",\"Cyp. Variegatus\",\"Fun majalis\",\"Poe latipinna\",\"Unid crab\",\"Har jaguana\",\"Arm mierii\",\"Fun grandis\",\"Mic gulosus\",\"Ari felis\",\"Clupeidae\",\"Fundulus spp\",\"Diapterus/Eugerres spp\",\"Isopods\",\"Cyn nebulosus\",\"Opi oglinum\",\"Flo carpio\",\"Luc parva\",\"Uca spp\",\"Majidae\",\"Mug cephalus\",\"Squ empusa\",\"Opi robinsi\",\"Ariidae\",\"Sci ocellatus\",\"Unid shrimp\",\"Uca thayeri\",\"Grapsidae\",\"Lei xanthurus\",\"Elo saurus\",\"Brevoortia spp\"\n" + "\"CHD01101502\",1,549,,,,,,,,,,,1,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n" + "\"CHD01102504\",1,548,\"E\",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n" + "\"CHD01102504\",2,550,,3,,,,,,,,,1,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n" + "\"CHM000152\",1,580,\"E\",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n" + "\"CHM000152\",2,556,,,1,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,";
String dateLocationString = "\"Collection #\",\"Longitude\",\"Latitude\",\"Time\",\"Date\",\"Temperature\",\"Salinity\"\n" + "\"CHD01101502\",-82.1625,26.72,10:55:00,1-Mar-00,22.4,33.8\n" + "\"CHD01102504\",-82.1625,26.72,10:55:00,1-Mar-00,22.4,33.8\n" + "\"CHM000151\",-82.1625,26.72,10:55:00,1-Mar-00,22.4,33.8\n" + "\"CHM000152\",-82.103833,26.651833,12:40:00,1-Mar-00,24.8,30.3\n" + "\"CHM000153\",-82.087333,26.644833,13:40:00,1-Mar-00,25.1,30.1\n" + "\"CHM000154\",-82.083167,26.671167,14:40:00,1-Mar-00,26,30.4\n" + "\"CHM000175\",-82.197833,26.688167,10:00:00,8-Mar-00,22.2,35.05\n" + "\"CHM000176\",-82.191333,26.667333,11:00:00,8-Mar-00,22.7,35.25";
final TestParserFactory preyPredatorFactory = new TestParserFactory(predatorPreyMapping);
final TestParserFactory dateLocationFactory = new TestParserFactory(dateLocationString);
ParserFactory testFactory = (studyResource, characterEncoding) -> {
LabeledCSVParser parser;
if (studyResource.toString().contains("abundance")) {
parser = preyPredatorFactory.createParser(studyResource, characterEncoding);
} else {
parser = dateLocationFactory.createParser(studyResource, characterEncoding);
}
return parser;
};
DatasetImporter importer = new StudyImporterTestFactory(testFactory, nodeFactory).instantiateImporter(DatasetImporterForBlewett.class);
importStudy(importer);
StudyNode study = getStudySingleton(getGraphDb());
AtomicBoolean success = new AtomicBoolean(false);
RelationshipListener handler1 = collectedRel -> {
Date unixEpochProperty = null;
try {
unixEpochProperty = nodeFactory.getUnixEpochProperty(new SpecimenNode(collectedRel.getEndNode()));
} catch (NodeFactoryException e) {
fail(e.getMessage());
}
assertThat(unixEpochProperty, is(not(nullValue())));
String actual = dateToString(unixEpochProperty);
Node predatorNode = collectedRel.getEndNode();
if (StringUtils.equals(actual, "2000-03-01T10:55:00.000-06:00") && predatorNode.hasProperty(SpecimenConstant.LIFE_STAGE_LABEL) && predatorNode.hasRelationship(NodeUtil.asNeo4j(InteractType.ATE), Direction.OUTGOING)) {
assertThat(predatorNode.getProperty(SpecimenConstant.LIFE_STAGE_LABEL), is("post-juvenile adult stage"));
assertThat(predatorNode.getProperty(SpecimenConstant.LIFE_STAGE_ID), is("UBERON:0000113"));
Node predatorTaxonNode = predatorNode.getRelationships(NodeUtil.asNeo4j(RelTypes.CLASSIFIED_AS), Direction.OUTGOING).iterator().next().getEndNode();
assertThat(predatorTaxonNode.getProperty(PropertyAndValueDictionary.NAME), is("Centropomus undecimalis"));
Set<String> preyNames = new HashSet<>();
Iterable<Relationship> ate = predatorNode.getRelationships(NodeUtil.asNeo4j(InteractType.ATE), Direction.OUTGOING);
for (Relationship preyRels : ate) {
Node preyNode = preyRels.getEndNode();
assertThat(preyNode, is(not(nullValue())));
Node taxonNode = preyNode.getRelationships(NodeUtil.asNeo4j(RelTypes.CLASSIFIED_AS), Direction.OUTGOING).iterator().next().getEndNode();
assertThat(taxonNode, is(not(nullValue())));
preyNames.add((String) taxonNode.getProperty(PropertyAndValueDictionary.NAME));
}
assertThat(preyNames, hasItem("Lag rhomboides"));
success.set(true);
}
};
NodeUtil.handleCollectedRelationships(new NodeTypeDirection(study.getUnderlyingNode()), handler1);
assertTrue(success.get());
success.set(false);
RelationshipListener handler2 = collectedRel -> {
Node predatorNode = collectedRel.getEndNode();
if (predatorNode.hasProperty(SpecimenConstant.LENGTH_IN_MM) && (Double) predatorNode.getProperty(SpecimenConstant.LENGTH_IN_MM) == 548.0) {
Iterable<Relationship> ate = predatorNode.getRelationships(NodeUtil.asNeo4j(InteractType.ATE), Direction.OUTGOING);
assertThat(ate.iterator().hasNext(), is(false));
Location location = null;
try {
location = nodeFactory.findLocation(new LocationImpl(26.651833, -82.103833, 0.0, null));
} catch (NodeFactoryException e) {
fail(e.getMessage());
}
assertThat(location, is(not(nullValue())));
Iterable<Relationship> specimenCaughtHere = NodeUtil.getSpecimenCaughtHere(location);
Iterator<Relationship> iterator = specimenCaughtHere.iterator();
assertThat(iterator.hasNext(), is(true));
iterator.next();
assertThat(iterator.hasNext(), is(true));
iterator.next();
assertThat(iterator.hasNext(), is(true));
iterator.next();
assertThat(iterator.hasNext(), is(false));
success.set(true);
}
};
NodeUtil.handleCollectedRelationships(new NodeTypeDirection(study.getUnderlyingNode()), handler2);
assertTrue(success.get());
}
use of org.eol.globi.util.NodeTypeDirection in project eol-globi-data by jhpoelen.
the class DatasetImporterForCookTest method importFirstFewLines.
@Test
public void importFirstFewLines() throws IOException, NodeFactoryException, StudyImporterException {
String firstFiveLines = "\"Date\",\"Individual\",\"Fish Length\",\"Infected\",\"Iso 1\",\"Iso 2\",\"Iso 3\",\"Iso 4 \",\"Iso 5\",\"Iso #\",,\n" + "8/10/2010,22,15.6,1,\"NA\",\"NA\",0,0,0,2,,\"Notes: All fish collected were Atlantic Croaker (Micropogonias undulatus) and measured in total length (cm). Isopods collected were Cymothoa excisa. For the Infected column 1= infected and 0= not infected. Numbers for the isopods are total length in cm and the Iso# represents the number of isopods found per fish. \"\n" + "8/10/2010,1,14.7,1,1.6,0.67,0,0,0,2,,\n" + "8/10/2010,5,14.2,1,1.53,0.7,0,0,0,2,,\n" + "8/10/2010,2,13.2,1,1.42,0.71,0.52,0.45,0,4,,\n";
DatasetImporterForCook importer = new DatasetImporterForCook(new TestParserFactory(firstFiveLines), nodeFactory);
importStudy(importer);
StudyNode study = getStudySingleton(getGraphDb());
Taxon hostTaxon = taxonIndex.findTaxonByName("Micropogonias undulatus");
assertThat(hostTaxon, is(notNullValue()));
Taxon parasiteTaxon = taxonIndex.findTaxonByName("Cymothoa excisa");
assertThat(parasiteTaxon, is(notNullValue()));
assertThat("missing location", nodeFactory.findLocation(new LocationImpl(27.85, -(97.0 + 8.0 / 60.0), -3.0, null)), is(notNullValue()));
AtomicInteger count = new AtomicInteger(0);
AtomicBoolean foundFirstHost = new AtomicBoolean(false);
RelationshipListener handler = relationship -> {
assertThat(relationship.getProperty(SpecimenConstant.EVENT_DATE), is(notNullValue()));
Node specimen = relationship.getEndNode();
if (specimen.hasProperty(SpecimenConstant.LENGTH_IN_MM)) {
Object property = specimen.getProperty(SpecimenConstant.LENGTH_IN_MM);
if (new Double(156.0).equals(property)) {
assertTaxonClassification(specimen, ((NodeBacked) hostTaxon).getUnderlyingNode());
foundFirstHost.set(true);
Iterable<Relationship> parasiteRel = specimen.getRelationships(Direction.INCOMING, NodeUtil.asNeo4j(InteractType.PARASITE_OF));
for (Relationship rel : parasiteRel) {
Node parasite = rel.getStartNode();
assertThat(parasite.hasProperty(SpecimenConstant.LENGTH_IN_MM), is(false));
assertTaxonClassification(parasite, ((NodeBacked) parasiteTaxon).getUnderlyingNode());
}
}
}
count.incrementAndGet();
};
NodeUtil.handleCollectedRelationships(new NodeTypeDirection(study.getUnderlyingNode()), handler);
assertThat(count.get(), is(14));
assertThat(foundFirstHost.get(), is(true));
}
Aggregations