Search in sources :

Example 1 with NodeListener

use of org.eol.globi.util.NodeListener in project eol-globi-data by jhpoelen.

the class DatasetImporterForTSVTest method assertArgumentType.

private void assertArgumentType(String firstFewLines, final RelTypes argumentType) throws StudyImporterException {
    DatasetImpl dataset = getDataset(new TreeMap<URI, String>() {

        {
            put(URI.create("/interactions.tsv"), firstFewLines);
        }
    });
    DatasetImporterForTSV importer = new DatasetImporterForTSV(null, nodeFactory);
    importer.setDataset(dataset);
    importStudy(importer);
    final AtomicBoolean foundStudy = new AtomicBoolean(false);
    NodeUtil.findStudies(getGraphDb(), new NodeListener() {

        @Override
        public void on(Node node) {
            assertTrue(node.hasRelationship(Direction.OUTGOING, NodeUtil.asNeo4j(argumentType)));
            foundStudy.set(true);
        }
    });
    assertThat(foundStudy.get(), is(true));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeListener(org.eol.globi.util.NodeListener) Node(org.neo4j.graphdb.Node) StudyNode(org.eol.globi.domain.StudyNode) DatasetImpl(org.globalbioticinteractions.dataset.DatasetImpl) URI(java.net.URI)

Example 2 with NodeListener

use of org.eol.globi.util.NodeListener in project eol-globi-data by jhpoelen.

the class NameResolver method resolveNames.

public void resolveNames(Long batchSize, GraphDatabaseService graphService) {
    StopWatch watchForEntireRun = new StopWatch();
    watchForEntireRun.start();
    StopWatch watchForBatch = new StopWatch();
    watchForBatch.start();
    final AtomicLong nameCount = new AtomicLong(0L);
    final TransactionPerBatch batchListener = new TransactionPerBatch(graphService);
    NodeListener listener = node -> nameCount.set(resolveNamesInStudy(batchSize, watchForBatch, nameCount.get(), node, batchListener));
    LOG.info("resolving names...");
    NodeUtil.processNodes(batchSize, graphService, listener, StudyConstant.TITLE, "*", "studies", batchListener);
    watchForEntireRun.stop();
    LOG.info("resolved [" + nameCount + "] names in " + getProgressMsg(nameCount.get(), watchForEntireRun.getTime()));
}
Also used : Taxon(org.eol.globi.domain.Taxon) TaxonIndex(org.eol.globi.data.TaxonIndex) NodeUtil(org.eol.globi.util.NodeUtil) Logger(org.slf4j.Logger) NodeListener(org.eol.globi.util.NodeListener) Direction(org.neo4j.graphdb.Direction) LoggerFactory(org.slf4j.LoggerFactory) StudyConstant(org.eol.globi.domain.StudyConstant) StudyNode(org.eol.globi.domain.StudyNode) RelTypes(org.eol.globi.domain.RelTypes) Node(org.neo4j.graphdb.Node) AtomicLong(java.util.concurrent.atomic.AtomicLong) Relationship(org.neo4j.graphdb.Relationship) GraphServiceFactory(org.eol.globi.db.GraphServiceFactory) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) NodeFactoryException(org.eol.globi.data.NodeFactoryException) BatchListener(org.eol.globi.util.BatchListener) StopWatch(org.apache.commons.lang.time.StopWatch) SpecimenNode(org.eol.globi.domain.SpecimenNode) Study(org.eol.globi.domain.Study) TaxonNode(org.eol.globi.domain.TaxonNode) AtomicLong(java.util.concurrent.atomic.AtomicLong) NodeListener(org.eol.globi.util.NodeListener) StopWatch(org.apache.commons.lang.time.StopWatch)

Example 3 with NodeListener

use of org.eol.globi.util.NodeListener in project eol-globi-data by jhpoelen.

the class DatasetImporterForWrastTest method createAndPopulateStudyFromLavacaBay.

@Test
public void createAndPopulateStudyFromLavacaBay() throws StudyImporterException {
    String csvString = "\"Month\",\"Day\",\"Year\",\"Region\",\"Season\",\"Habitat\",\"Site\",\"Family\",\"Predator Species\",\"TL (mm)\",\"Prey Item Species\",\"Prey item\",\"Number\",\"Condition Index\",\"Volume\",\"Percent Content\",\"Prey Item Trophic Level\",\"Notes\",\"Call #\"\n";
    csvString += "7,24,2001,\"Lower\",\"Fall\",\"Marsh\",1,\"Sciaenidae\",\"Sciaenops ocellatus\",420,\"Acrididae spp. \",\"Acrididae \",1,\"III\",0.4,3.2520325203,2.5,,1\n";
    csvString += "7,25,2001,\"Lower\",\"Spring\",\"Non-Veg \",1,\"Ariidae\",\"Arius felis\",176,\"Aegathoa oculata \",\"Aegathoa oculata\",4,\"I\",0.01,3.3333333333,2.1,,2\n";
    csvString += "7,26,2001,\"Upper\",\"Spring\",\"Reef\",2,\"Depth\",\"Missing depth\",176,\"Aegathoa oculata \",\"Aegathoa oculata\",4,\"I\",0.01,3.3333333333,2.1,,2\n";
    Map<String, String> contentMap = new TreeMap<>();
    String locationString = "\"Location\",\"Latitude\",\"Longitude\",,\"Region\",\"Habitat\",\"Site\"\n" + "\"LM1\",28.595267,-96.477033,,\"Lower\",\"Marsh edge\",1\n" + "\"LSG1\",28.596233,-96.476483,,\"Lower\",\"Marsh edge\",1\n" + "\"LM2\",28.593150,-96.474633,,\"Lower\",\"Marsh edge\",2\n" + "\"LSG2\",28.594833,-96.473967,,\"Lower\",\"Marsh edge\",2\n" + "\"LNV1A\",28.608417,-96.475517,,\"Lower\",\"Non-Veg \",1\n" + "\"LNV1B\",28.607217,-96.474500,,\"Lower\",\"Non-Veg \",1\n" + "\"LNV2A\",28.592400,-96.483033,,\"Lower\",\"Non-Veg \",2\n" + "\"LNV2B\",28.590883,-96.484133,,\"Lower\",\"Non-Veg \",2\n" + "\"UR2B\",28.656483,-96.597217,,\"Upper\",\"Reef\",2";
    String envString = "\"Date\",\"Season\",\"Upper/Lower\",\"Habitat\",\"Site\",\"Air Temp (ºC)\",\"Wind Chill (ºC)\",\"Relative Humidity (%)\",\"Heat Index (ºC)\",\"Dew Point (ºC)\",\"Max Wind intensity (mph)\",\"Ave Wind intensity (mph)\",\"Wind Direction\",\"Cloud Cover (%)\",\"Rain?\",\"Depth (m)\",\"Temp (ºC) Surface\",\"Temp (ºC) Bottom\",\"Mean Temp Surface/Bottom\",\"pH Surface\",\"pH Bottom\",\"Mean pH Surface/Bottom\",\"DO (mg/L) Surface\",\"DO (mg/L) Bottom\",\"Mean DO Surface/Bottom\",\"Sal (o/oo) Surface\",\"Sal (o/oo) Bottom\",\"Mean Sal Surface/Bottom\",\"Secchi (m)\"\n" + "7/24/2006,\"Summer\",\"L\",\"R\",1,\"81.3 (F)\",\"81.5 (F)\",71.8,\"86.2 (F)\",\"70.6 (F)\",8.5,8.1,\"SE\",75,\"Yes\",1.9,30.79,30.34,30.565,8.11,8.13,8.12,5.16,4.8,4.98,22.8,23.07,22.935,0.4\n" + "7/24/2006,\"Summer\",\"L\",\"R\",2,\"81.3 (F)\",\"81.5 (F)\",71.8,\"86.2 (F)\",\"70.6 (F)\",8.5,8.1,\"SE\",75,\"Yes\",2.1,31.07,30.14,30.605,8.11,8.1,8.105,5.14,4.64,4.89,23.37,23.84,23.605,0.48\n" + "10/26/2006,\"Fall\",\"L\",\"M\",1,26.8,26.5,78,30,22.4,17.9,15,\"S \",95,\"No\",0.8,25.57,,25.57,8.29,,8.29,8.32,,8.32,21.26,,21.26,\"Bottom\"\n" + "10/26/2006,\"Fall\",\"L\",\"M\",2,26.8,26.5,78,30,22.4,17.9,15,\"S \",95,\"No\",0.4,25.84,,25.84,8.32,,8.32,8.21,,8.21,21.47,,21.47,\"Bottom\"\n" + "10/26/2006,\"Fall\",\"L\",\"SG\",1,26.8,26.5,78,30,22.4,17.9,15,\"S \",95,\"No\",0.8,25.57,,25.57,8.29,,8.29,8.32,,8.32,21.26,,21.26,\"Bottom\"\n" + "4/27/2007,\"Spring\",\"L\",\"R\",1,22.4,22.3,75,23.1,18.1,10.4,9.1,\"NW\",98,\"No\",1.6,23.43,23.39,23.41,,,,7.31,7.09,7.2,14.64,14.64,14.64,0.32\n" + "4/27/2007,\"Spring\",\"L\",\"R\",2,22.4,22.3,75,23.1,18.1,10.4,9.1,\"NW\",98,\"No\",2,23.77,23.77,23.77,,,,7.01,6.82,6.915,15.04,15.04,15.04,0.38\n" + "4/27/2007,\"Spring\",\"L\",\"NV\",1,22.4,22.3,75,23.1,18.1,10.4,9.1,\"NW\",98,\"No\",2,23.85,23.85,23.85,,,,6.96,6.81,6.885,14.65,14.65,14.65,0.48\n" + "4/27/2007,\"Spring\",\"L\",\"NV\",2,22.4,22.3,75,23.1,18.1,10.4,9.1,\"NW\",98,\"No\",1.8,23.78,23.78,23.78,,,,6.87,6.65,6.76,15.11,15.18,15.145,0.45";
    contentMap.put(DatasetImporterForWrast.LAVACA_BAY_LOCATIONS.toString(), locationString);
    contentMap.put(DatasetImporterForWrast.LAVACA_BAY_DATA_SOURCE.toString(), csvString);
    contentMap.put(DatasetImporterForWrast.LAVACA_BAY_ENVIRONMENTAL.toString(), envString);
    DatasetImporterForWrast importer = new DatasetImporterForWrast(new TestParserFactory(contentMap), nodeFactory);
    importStudy(importer);
    assertThat(getSpecimenCount(getStudySingleton(getGraphDb())), is(5));
    assertNotNull(taxonIndex.findTaxonByName("Sciaenops ocellatus"));
    assertNotNull(taxonIndex.findTaxonByName("Arius felis"));
    assertNotNull(taxonIndex.findTaxonByName("Aegathoa oculata"));
    assertNotNull(nodeFactory.findSeason("spring"));
    assertNotNull(nodeFactory.findSeason("fall"));
    AtomicReference<StudyNode> foundStudyRef = new AtomicReference<>();
    NodeUtil.findStudies(getGraphDb(), new NodeListener() {

        @Override
        public void on(Node node) {
            foundStudyRef.set(new StudyNode(node));
        }
    });
    StudyNode foundStudy = foundStudyRef.get();
    assertNotNull(foundStudy);
    assertThat(foundStudy.getTitle(), is("Wrast 2008"));
    RelationshipListener handler = relationship -> {
        Date unixEpochProperty = null;
        try {
            unixEpochProperty = nodeFactory.getUnixEpochProperty(new SpecimenNode(relationship.getEndNode()));
        } catch (NodeFactoryException e) {
            fail(e.getMessage());
        }
        SimpleDateFormat simpleDateFormat = DatasetImporterForWrast.getSimpleDateFormat();
        Date endDate = null;
        try {
            endDate = simpleDateFormat.parse("7/27/2001");
        } catch (ParseException e) {
            fail(e.getMessage());
        }
        Date startDate = null;
        try {
            startDate = simpleDateFormat.parse("7/23/2001");
        } catch (ParseException e) {
            fail(e.getMessage());
        }
        assertThat(unixEpochProperty.before(endDate), is(true));
        assertThat(unixEpochProperty.after(startDate), is(true));
        Specimen specimen = new SpecimenNode(relationship.getEndNode());
        for (Relationship ignored : NodeUtil.getStomachContents(specimen)) {
            TaxonNode taxon = new TaxonNode(relationship.getEndNode().getSingleRelationship(NodeUtil.asNeo4j(RelTypes.CLASSIFIED_AS), Direction.OUTGOING).getEndNode());
            String scientificName = taxon.getName();
            if ("Sciaenops ocellatus".equals(scientificName)) {
                Location location = specimen.getSampleLocation();
                assertThat(location, is(not(nullValue())));
                assertThat(location.getLatitude(), is((28.595267 + 28.596233) / 2.0));
                assertThat(location.getLongitude(), is((-96.477033 - 96.476483) / 2.0));
                assertThat(location.getAltitude(), is(-0.8));
                Iterable<Relationship> stomachContents = NodeUtil.getStomachContents(specimen);
                int count = 0;
                for (Relationship containsRel : stomachContents) {
                    Node endNode = containsRel.getEndNode().getSingleRelationship(NodeUtil.asNeo4j(RelTypes.CLASSIFIED_AS), Direction.OUTGOING).getEndNode();
                    Object name = endNode.getProperty("name");
                    String name1 = (String) name;
                    assertThat(name1.replaceAll("\\W", ""), is("Acrididae"));
                    count++;
                }
                assertEquals(1, count);
                Season season = specimen.getSeason();
                assertEquals("fall", season.getTitle());
                assertEquals(420.0d, specimen.getLengthInMm(), 0.01);
            } else if ("Arius felis".equals(scientificName)) {
                Location location = specimen.getSampleLocation();
                assertThat(location, is(not(nullValue())));
                assertThat(location.getLatitude(), is((28.608417 + 28.607217) / 2.0));
                assertThat(location.getLongitude(), is((-96.475517 - 96.474500) / 2.0));
                assertThat(location.getAltitude(), is(-2.0d));
                Iterable<Relationship> stomachContents = NodeUtil.getStomachContents(specimen);
                int count = 0;
                for (Relationship containsRel : stomachContents) {
                    Object name = containsRel.getEndNode().getSingleRelationship(NodeUtil.asNeo4j(RelTypes.CLASSIFIED_AS), Direction.OUTGOING).getEndNode().getProperty("name");
                    assertEquals("Aegathoa oculata", name);
                    count++;
                }
                assertEquals(2, count);
                Season season = specimen.getSeason();
                assertEquals("spring", season.getTitle());
                assertEquals(176.0d, specimen.getLengthInMm(), 0.01);
            } else if ("Missing depth".equals(scientificName)) {
                Location location = specimen.getSampleLocation();
                assertThat(location, is(not(nullValue())));
                assertThat(location.getAltitude(), is(nullValue()));
            } else {
                fail("unexpected scientificName of predator [" + scientificName + "]");
            }
        }
    };
    NodeUtil.handleCollectedRelationships(new NodeTypeDirection(getStudySingleton(getGraphDb()).getUnderlyingNode()), handler);
}
Also used : NodeUtil(org.eol.globi.util.NodeUtil) Specimen(org.eol.globi.domain.Specimen) Location(org.eol.globi.domain.Location) Date(java.util.Date) Direction(org.neo4j.graphdb.Direction) CoreMatchers.not(org.hamcrest.CoreMatchers.not) SimpleDateFormat(java.text.SimpleDateFormat) RelTypes(org.eol.globi.domain.RelTypes) Season(org.eol.globi.domain.Season) AtomicReference(java.util.concurrent.atomic.AtomicReference) StudyImpl(org.eol.globi.domain.StudyImpl) Node(org.neo4j.graphdb.Node) RelationshipListener(org.eol.globi.util.RelationshipListener) Map(java.util.Map) Is.is(org.hamcrest.core.Is.is) Assert.fail(org.junit.Assert.fail) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ParseException(java.text.ParseException) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) NodeListener(org.eol.globi.util.NodeListener) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) StudyNode(org.eol.globi.domain.StudyNode) NodeTypeDirection(org.eol.globi.util.NodeTypeDirection) Relationship(org.neo4j.graphdb.Relationship) TreeMap(java.util.TreeMap) SpecimenNode(org.eol.globi.domain.SpecimenNode) Study(org.eol.globi.domain.Study) Assert.assertEquals(org.junit.Assert.assertEquals) TaxonNode(org.eol.globi.domain.TaxonNode) TaxonNode(org.eol.globi.domain.TaxonNode) Node(org.neo4j.graphdb.Node) StudyNode(org.eol.globi.domain.StudyNode) SpecimenNode(org.eol.globi.domain.SpecimenNode) TaxonNode(org.eol.globi.domain.TaxonNode) SpecimenNode(org.eol.globi.domain.SpecimenNode) RelationshipListener(org.eol.globi.util.RelationshipListener) Specimen(org.eol.globi.domain.Specimen) Season(org.eol.globi.domain.Season) NodeTypeDirection(org.eol.globi.util.NodeTypeDirection) NodeListener(org.eol.globi.util.NodeListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) TreeMap(java.util.TreeMap) Date(java.util.Date) StudyNode(org.eol.globi.domain.StudyNode) Relationship(org.neo4j.graphdb.Relationship) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Location(org.eol.globi.domain.Location) Test(org.junit.Test)

Example 4 with NodeListener

use of org.eol.globi.util.NodeListener in project eol-globi-data by jhpoelen.

the class GraphExporterImpl method exportNames.

private void exportNames(GraphDatabaseService graphService, File baseDir, StudyExporter exporter, String filename) throws StudyImporterException {
    try {
        File filePath = new File(baseDir, filename);
        OutputStreamWriter writer = openStream(filePath);
        NodeUtil.findStudies(graphService, new NodeListener() {

            final AtomicBoolean isFirst = new AtomicBoolean(true);

            @Override
            public void on(Node node) {
                boolean includeHeader = isFirst.getAndSet(false);
                try {
                    exporter.exportStudy(new StudyNode(node), ExportUtil.AppenderWriter.of(writer), includeHeader);
                } catch (IOException e) {
                    throw new IllegalStateException("failed to export names to [" + filePath + "]");
                }
            }
        });
        closeStream(filePath, writer);
    } catch (IOException e) {
        throw new StudyImporterException("failed to export unmatched source taxa", e);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StudyImporterException(org.eol.globi.data.StudyImporterException) NodeListener(org.eol.globi.util.NodeListener) Node(org.neo4j.graphdb.Node) StudyNode(org.eol.globi.domain.StudyNode) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File) StudyNode(org.eol.globi.domain.StudyNode)

Example 5 with NodeListener

use of org.eol.globi.util.NodeListener in project eol-globi-data by jhpoelen.

the class GraphExporterImpl method export.

private void export(GraphDatabaseService graphService, File exportPath, String filename, DarwinCoreExporter studyExporter, FileWriter darwinCoreMeta) throws IOException {
    File exportFile = new File(exportPath, filename);
    OutputStreamWriter writer = openStream(exportFile);
    NodeUtil.findStudies(graphService, new NodeListener() {

        final AtomicBoolean isFirst = new AtomicBoolean(true);

        @Override
        public void on(Node node) {
            try {
                studyExporter.exportStudy(new StudyNode(node), ExportUtil.AppenderWriter.of(writer), isFirst.getAndSet(false));
            } catch (IOException e) {
                throw new IllegalStateException("failed to export to [" + exportFile.getAbsolutePath() + "]", e);
            }
        }
    });
    closeStream(exportFile, writer);
    LOG.info("darwin core meta file writing... ");
    studyExporter.exportDarwinCoreMetaTable(darwinCoreMeta, filename);
    LOG.info("darwin core meta file written. ");
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeListener(org.eol.globi.util.NodeListener) Node(org.neo4j.graphdb.Node) StudyNode(org.eol.globi.domain.StudyNode) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File) StudyNode(org.eol.globi.domain.StudyNode)

Aggregations

StudyNode (org.eol.globi.domain.StudyNode)5 NodeListener (org.eol.globi.util.NodeListener)5 Node (org.neo4j.graphdb.Node)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 File (java.io.File)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2 RelTypes (org.eol.globi.domain.RelTypes)2 SpecimenNode (org.eol.globi.domain.SpecimenNode)2 Study (org.eol.globi.domain.Study)2 TaxonNode (org.eol.globi.domain.TaxonNode)2 NodeUtil (org.eol.globi.util.NodeUtil)2 Direction (org.neo4j.graphdb.Direction)2 Relationship (org.neo4j.graphdb.Relationship)2 URI (java.net.URI)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1