Search in sources :

Example 21 with InteractType

use of org.eol.globi.domain.InteractType in project eol-globi-data by jhpoelen.

the class StudyImporterForINaturalist method buildTypeMap.

public static Map<Integer, InteractType> buildTypeMap(String resource, LabeledCSVParser labeledCSVParser) throws IOException {
    LabeledCSVParser parser = labeledCSVParser;
    Map<Integer, InteractType> typeMap = new TreeMap<Integer, InteractType>();
    while (parser.getLine() != null) {
        String inatIdString = parser.getValueByLabel("observation_field_id");
        Integer inatId = null;
        String prefix = PREFIX_OBSERVATION_FIELD;
        if (StringUtils.startsWith(inatIdString, prefix)) {
            inatId = Integer.parseInt(inatIdString.replace(prefix, ""));
        }
        if (inatId == null) {
            LOG.warn("failed to map observation field id [" + inatIdString + "] in line [" + resource + ":" + parser.lastLineNumber() + "]");
        } else {
            String interactionTypeId = parser.getValueByLabel("interaction_type_id");
            InteractType interactType = InteractType.typeOf(interactionTypeId);
            if (interactType == null) {
                LOG.warn("failed to map interaction type [" + interactionTypeId + "] in line [" + resource + ":" + parser.lastLineNumber() + "]");
            } else {
                typeMap.put(inatId, interactType);
            }
        }
    }
    return typeMap;
}
Also used : InteractType(org.eol.globi.domain.InteractType) LabeledCSVParser(com.Ostermiller.util.LabeledCSVParser) TreeMap(java.util.TreeMap)

Example 22 with InteractType

use of org.eol.globi.domain.InteractType in project eol-globi-data by jhpoelen.

the class TaxonInteractionIndexer method createTaxonInteractions.

public void createTaxonInteractions(Map<Fun.Tuple3<Long, String, Long>, Long> taxonInteractions) {
    StopWatch watchForEntireRun = new StopWatch();
    watchForEntireRun.start();
    long count = 0;
    Transaction tx = null;
    for (Fun.Tuple3<Long, String, Long> uniqueTaxonInteraction : taxonInteractions.keySet()) {
        if (count % 1000 == 0) {
            finalizeTx(tx);
            tx = graphService.beginTx();
        }
        final Node sourceTaxon = graphService.getNodeById(uniqueTaxonInteraction.a);
        final Node targetTaxon = graphService.getNodeById(uniqueTaxonInteraction.c);
        if (sourceTaxon != null && targetTaxon != null) {
            final InteractType relType = InteractType.valueOf(uniqueTaxonInteraction.b);
            final Long interactionCount = taxonInteractions.get(uniqueTaxonInteraction);
            createInteraction(sourceTaxon, targetTaxon, relType, false, interactionCount);
            createInteraction(targetTaxon, sourceTaxon, InteractType.inverseOf(relType), true, interactionCount);
        }
        count++;
    }
    finalizeTx(tx);
    watchForEntireRun.stop();
    LOG.info("created [" + count + "] taxon interactions in " + getProgressMsg(count, watchForEntireRun.getTime()));
}
Also used : InteractType(org.eol.globi.domain.InteractType) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) SpecimenNode(org.eol.globi.domain.SpecimenNode) Fun(org.mapdb.Fun) StopWatch(org.apache.commons.lang.time.StopWatch)

Example 23 with InteractType

use of org.eol.globi.domain.InteractType in project eol-globi-data by jhpoelen.

the class StudyImporterForINaturalistTest method importTestResponse.

@Test
public void importTestResponse() throws IOException, StudyImporterException {
    importer.parseJSON(getClass().getResourceAsStream("inaturalist/sample_inaturalist_response.json"), new ArrayList<Integer>() {

        {
            add(47);
        }
    }, new HashMap<Integer, InteractType>() {

        {
            put(13, InteractType.ATE);
        }
    });
    resolveNames();
    assertThat(NodeUtil.findAllStudies(getGraphDb()).size(), is(22));
    Study anotherStudy = nodeFactory.findStudy("INAT:831");
    assertThat(anotherStudy, is(notNullValue()));
    assertThat(anotherStudy.getCitation(), containsString("Ken-ichi Ueda. 2008. Argiope eating Orthoptera. iNaturalist.org. Accessed at <https://www.inaturalist.org/observations/831> on "));
    assertThat(anotherStudy.getExternalId(), is("https://www.inaturalist.org/observations/831"));
    anotherStudy = nodeFactory.findStudy("INAT:97380");
    assertThat(anotherStudy, is(notNullValue()));
    assertThat(anotherStudy.getCitation(), containsString("annetanne. 2012. Misumena vatia eating Eristalis nemorum."));
    assertThat(anotherStudy.getExternalId(), is("https://www.inaturalist.org/observations/97380"));
    Taxon sourceTaxonNode = taxonIndex.findTaxonByName("Arenaria interpres");
    assertThat(sourceTaxonNode, is(not(nullValue())));
    Iterable<Relationship> relationships = ((NodeBacked) sourceTaxonNode).getUnderlyingNode().getRelationships(Direction.INCOMING, NodeUtil.asNeo4j(RelTypes.CLASSIFIED_AS));
    for (Relationship relationship : relationships) {
        Node sourceSpecimen = relationship.getStartNode();
        assertThat(new SpecimenNode(sourceSpecimen).getBasisOfRecord().getName(), is("HumanObservation"));
        assertThat(new SpecimenNode(sourceSpecimen).getBasisOfRecord().getId(), is("TEST:HumanObservation"));
        assertThat(new SpecimenNode(sourceSpecimen).getExternalId(), containsString(TaxonomyProvider.ID_PREFIX_INATURALIST));
        Relationship ateRel = sourceSpecimen.getSingleRelationship(NodeUtil.asNeo4j(InteractType.ATE), Direction.OUTGOING);
        Node preySpecimen = ateRel.getEndNode();
        assertThat(preySpecimen, is(not(nullValue())));
        Relationship preyClassification = preySpecimen.getSingleRelationship(NodeUtil.asNeo4j(RelTypes.CLASSIFIED_AS), Direction.OUTGOING);
        String actualPreyName = (String) preyClassification.getEndNode().getProperty("name");
        assertThat(actualPreyName, is("Crepidula fornicata"));
        Relationship locationRel = sourceSpecimen.getSingleRelationship(NodeUtil.asNeo4j(RelTypes.COLLECTED_AT), Direction.OUTGOING);
        assertThat((Double) locationRel.getEndNode().getProperty("latitude"), is(41.249813));
        assertThat((Double) locationRel.getEndNode().getProperty("longitude"), is(-72.542556));
        Relationship collectedRel = sourceSpecimen.getSingleRelationship(NodeUtil.asNeo4j(RelTypes.COLLECTED), Direction.INCOMING);
        assertThat((Long) collectedRel.getProperty(SpecimenConstant.DATE_IN_UNIX_EPOCH), is(any(Long.class)));
    }
}
Also used : InteractType(org.eol.globi.domain.InteractType) Study(org.eol.globi.domain.Study) Taxon(org.eol.globi.domain.Taxon) Relationship(org.neo4j.graphdb.Relationship) JsonNode(org.codehaus.jackson.JsonNode) Node(org.neo4j.graphdb.Node) SpecimenNode(org.eol.globi.domain.SpecimenNode) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) SpecimenNode(org.eol.globi.domain.SpecimenNode) Test(org.junit.Test)

Aggregations

InteractType (org.eol.globi.domain.InteractType)23 Study (org.eol.globi.domain.Study)9 HashMap (java.util.HashMap)8 LabeledCSVParser (com.Ostermiller.util.LabeledCSVParser)7 IOException (java.io.IOException)7 Specimen (org.eol.globi.domain.Specimen)7 StudyImpl (org.eol.globi.domain.StudyImpl)5 TaxonImpl (org.eol.globi.domain.TaxonImpl)4 Test (org.junit.Test)4 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 JsonNode (org.codehaus.jackson.JsonNode)3 Node (org.neo4j.graphdb.Node)3 CSVPrint (com.Ostermiller.util.CSVPrint)2 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 Date (java.util.Date)2 ZipEntry (java.util.zip.ZipEntry)2