Search in sources :

Example 11 with InteractType

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

the class ReportGenerator method countInteractionsAndTaxa.

private void countInteractionsAndTaxa(Iterable<Relationship> specimens, Set<Long> ids, Counter interactionCounter, Set<Long> idsNoMatch) {
    for (Relationship specimen : specimens) {
        Iterable<Relationship> relationships = specimen.getEndNode().getRelationships();
        for (Relationship relationship : relationships) {
            InteractType[] types = InteractType.values();
            for (InteractType type : types) {
                if (relationship.isType(NodeUtil.asNeo4j(type)) && !relationship.hasProperty(PropertyAndValueDictionary.INVERTED)) {
                    interactionCounter.count();
                    break;
                }
            }
        }
        Relationship classifiedAs = specimen.getEndNode().getSingleRelationship(NodeUtil.asNeo4j(RelTypes.CLASSIFIED_AS), Direction.OUTGOING);
        if (classifiedAs != null) {
            Node taxonNode = classifiedAs.getEndNode();
            ids.add(taxonNode.getId());
            if (!TaxonUtil.isResolved(new TaxonNode(taxonNode))) {
                idsNoMatch.add(taxonNode.getId());
            }
        }
    }
}
Also used : InteractType(org.eol.globi.domain.InteractType) TaxonNode(org.eol.globi.domain.TaxonNode) Relationship(org.neo4j.graphdb.Relationship) StudyNode(org.eol.globi.domain.StudyNode) Node(org.neo4j.graphdb.Node) TaxonNode(org.eol.globi.domain.TaxonNode)

Example 12 with InteractType

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

the class StudyImporterForVertNetTest method parseAssOcc.

protected Map<String, InteractType> parseAssOcc(String str) {
    Map<String, InteractType> mapping = new TreeMap<String, InteractType>() {

        {
            put("ate", InteractType.ATE);
            put("eaten by", InteractType.EATEN_BY);
            put("parasite of", InteractType.PARASITE_OF);
        }
    };
    String[] occurrences = StringUtils.split(str, ";");
    Map<String, InteractType> assoc = new TreeMap<String, InteractType>();
    for (String occurrence : occurrences) {
        int index = StringUtils.indexOf(occurrence, ")");
        if (index > 0) {
            String association = StringUtils.substring(StringUtils.trim(StringUtils.substring(occurrence, 0, index)), 1);
            String rest = StringUtils.substring(occurrence, index + 1);
            InteractType interactType = mapping.get(association);
            if (interactType != null) {
                String trim = StringUtils.trim(rest);
                int nsIndex = StringUtils.indexOf(trim, "http://arctos.database.museum/guid/");
                String strip = nsIndex < 0 ? trim : StringUtils.substring(trim, nsIndex, trim.length());
                String ns = StringUtils.replace(strip, "institutional catalog number ", "http://arctos.database.museum/guid/");
                assoc.put(StringUtils.replace(ns, " ", ":"), interactType);
            }
        }
    }
    return assoc;
}
Also used : InteractType(org.eol.globi.domain.InteractType) TreeMap(java.util.TreeMap) CSVPrint(com.Ostermiller.util.CSVPrint)

Example 13 with InteractType

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

the class StudyImporterForBrose method addInteractionForConsumer.

private void addInteractionForConsumer(LabeledCSVParser parser, Study localStudy, String predatorName) throws NodeFactoryException, StudyImporterException {
    Location location = null;
    String locationString = parser.getValueByLabel("Geographic location");
    LatLng latLng = LOC_MAP.get(StringUtils.trim(locationString));
    if (latLng == null) {
        getLogger().warn(localStudy, "failed to find location for [" + locationString + "]");
    } else {
        location = nodeFactory.getOrCreateLocation(new LocationImpl(latLng.getLat(), latLng.getLng(), null, null));
        String habitat = StringUtils.join(parser.getValueByLabel("General habitat"), " ", parser.getValueByLabel("Specific habitat"));
        String habitatId = "BROSE:" + habitat.replaceAll("\\W", "_");
        nodeFactory.getOrCreateEnvironments(location, habitatId, habitat);
    }
    Specimen consumer = nodeFactory.createSpecimen(localStudy, new TaxonImpl(predatorName, null));
    consumer.caughtIn(location);
    addLifeStage(parser, consumer, "Lifestage consumer");
    String name = getName(parser, "Taxonomy resource", "Common name(s) resource");
    if (StringUtils.isBlank(name) || StringUtils.length(name) < 2) {
        String message = "found (near) empty prey name on line [" + parser.lastLineNumber() + "] + [" + name + "]";
        LOG.warn(message);
        getLogger().warn(localStudy, message);
    } else {
        Specimen resource = nodeFactory.createSpecimen(localStudy, new TaxonImpl(name, null));
        resource.caughtIn(location);
        addLifeStage(parser, resource, "Lifestage - resource");
        String interactionType = parser.getValueByLabel("Type of feeding interaction");
        Map<String, InteractType> typeMapping = new HashMap<String, InteractType>() {

            {
                put("predacious", InteractType.PREYS_UPON);
                put("predator", InteractType.PREYS_UPON);
                put("herbivorous", InteractType.ATE);
                put("parasitoid", InteractType.PARASITE_OF);
                put("parasitic", InteractType.PARASITE_OF);
                put("bacterivorous", InteractType.ATE);
                put("omnivore", InteractType.ATE);
                put("detritivorous", InteractType.ATE);
                put("pathogen", InteractType.PATHOGEN_OF);
            }
        };
        InteractType interactType = typeMapping.get(interactionType);
        if (interactType == null) {
            throw new StudyImporterException("found unsupported interaction type [" + interactionType + "]");
        }
        consumer.interactsWith(resource, interactType);
    }
}
Also used : InteractType(org.eol.globi.domain.InteractType) Specimen(org.eol.globi.domain.Specimen) HashMap(java.util.HashMap) TaxonImpl(org.eol.globi.domain.TaxonImpl) LocationImpl(org.eol.globi.domain.LocationImpl) LatLng(org.eol.globi.geo.LatLng) Location(org.eol.globi.domain.Location)

Example 14 with InteractType

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

the class StudyImporterForSeltmann method importStudy.

@Override
public void importStudy() throws StudyImporterException {
    final String archiveURL = DatasetUtil.getNamedResourceURI(getDataset(), "archive");
    if (org.apache.commons.lang.StringUtils.isBlank(archiveURL)) {
        throw new StudyImporterException("failed to import [" + getDataset().getNamespace() + "]: no [archiveURL] specified");
    }
    DB db = DBMaker.newMemoryDirectDB().compressionEnable().transactionDisable().make();
    final HTreeMap<String, Map<String, String>> assocMap = db.createHashMap("assocMap").make();
    try {
        InputStream inputStream = DatasetUtil.getNamedResourceStream(getDataset(), "archive");
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        ZipEntry entry;
        File assocTempFile = null;
        File occTempFile = null;
        while ((entry = zipInputStream.getNextEntry()) != null) {
            if (entry.getName().matches("(^|(.*/))associatedTaxa.tsv$")) {
                assocTempFile = FileUtils.saveToTmpFile(zipInputStream, entry);
            } else if (entry.getName().matches("(^|(.*/))occurrences.tsv$")) {
                occTempFile = FileUtils.saveToTmpFile(zipInputStream, entry);
            } else {
                IOUtils.copy(zipInputStream, new NullOutputStream());
            }
        }
        IOUtils.closeQuietly(zipInputStream);
        if (assocTempFile == null) {
            throw new StudyImporterException("failed to find expected [associatedTaxa.tsv] resource");
        }
        if (occTempFile == null) {
            throw new StudyImporterException("failed to find expected [occurrences.tsv] resource");
        }
        BufferedReader assocReader = FileUtils.getUncompressedBufferedReader(new FileInputStream(assocTempFile), CharsetConstant.UTF8);
        LabeledCSVParser parser = CSVTSVUtil.createLabeledCSVParser(assocReader);
        parser.changeDelimiter('\t');
        while (parser.getLine() != null) {
            Map<String, String> prop = new HashMap<String, String>();
            addKeyValue(parser, prop, "dwc:coreid");
            addKeyValue(parser, prop, "dwc:basisOfRecord");
            addKeyValue(parser, prop, FIELD_IDIGBIO_RECORD_ID);
            addKeyValue(parser, prop, FIELD_ASSOCIATED_GENUS);
            addKeyValue(parser, prop, FIELD_ASSOCIATED_SPECIFIC_EPITHET);
            addKeyValue(parser, prop, FIELD_ASSOCIATED_SCIENTIFIC_NAME);
            addKeyValue(parser, prop, "dwc:basisOfRecord");
            addKeyValue(parser, prop, "aec:associatedRelationshipTerm");
            addKeyValue(parser, prop, "aec:associatedRelationshipURI");
            addKeyValue(parser, prop, "aec:associatedLocationOnHost");
            addKeyValue(parser, prop, "aec:associatedEmergenceVerbatimDate");
            String coreId = parser.getValueByLabel("dwc:coreid");
            if (StringUtils.isBlank(coreId)) {
                LOG.warn("no coreid for line [" + parser.getLastLineNumber() + 1 + "]");
            } else {
                assocMap.put(coreId, prop);
            }
        }
        LabeledCSVParser occurrence = CSVTSVUtil.createLabeledCSVParser(new FileInputStream(occTempFile));
        occurrence.changeDelimiter('\t');
        while (occurrence.getLine() != null) {
            String references = occurrence.getValueByLabel("dcterms:references");
            Study study = nodeFactory.getOrCreateStudy(new StudyImpl("seltmann" + references, CitationUtil.sourceCitationLastAccessed(this.getDataset(), references), null, references));
            String recordId = occurrence.getValueByLabel(FIELD_IDIGBIO_RECORD_ID);
            Map<String, String> assoc = assocMap.get(recordId);
            if (assoc != null) {
                String targetName = getTargetNameFromAssocMap(assoc);
                String sourceName = occurrence.getValueByLabel("scientificName");
                String eventDate = occurrence.getValueByLabel("eventDate");
                Date date = null;
                if (StringUtils.equals(eventDate, "0000-00-00")) {
                    getLogger().warn(study, "found suspicious event date [" + eventDate + "]" + getLineMsg(occurrence));
                } else if (StringUtils.isBlank(eventDate)) {
                    getLogger().warn(study, "found suspicious event date [" + eventDate + "]" + getLineMsg(occurrence));
                } else {
                    DateTimeFormatter fmtDateTime1 = DateTimeFormat.forPattern("yyyy-MM-dd").withZoneUTC();
                    String dateString = eventDate.split("/")[0];
                    try {
                        date = fmtDateTime1.parseDateTime(dateString).toDate();
                    } catch (IllegalArgumentException e) {
                        getLogger().warn(study, "invalid date [" + dateString + "] " + getLineMsg(occurrence));
                    }
                }
                if (StringUtils.isBlank(sourceName)) {
                    getLogger().warn(study, "found blank source taxon name" + getLineMsg(occurrence));
                }
                if (StringUtils.isBlank(targetName)) {
                    getLogger().warn(study, "found blank associated target taxon name" + getLineMsg(occurrence));
                }
                InteractType interactType = parseInteractType(occurrence, assoc);
                if (interactType != null && StringUtils.isNotBlank(sourceName) && StringUtils.isNotBlank(targetName)) {
                    try {
                        createInteraction(occurrence, study, assoc, targetName, sourceName, date, interactType);
                    } catch (NodeFactoryException ex) {
                        String message = "failed to import interaction because of [" + ex.getMessage() + "]" + getLineMsg(occurrence);
                        LOG.warn(message);
                        getLogger().warn(study, message);
                    }
                }
            }
        }
    } catch (IOException | NodeFactoryException e) {
        throw new StudyImporterException(e);
    }
    db.close();
}
Also used : InteractType(org.eol.globi.domain.InteractType) Study(org.eol.globi.domain.Study) HashMap(java.util.HashMap) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) StudyImpl(org.eol.globi.domain.StudyImpl) LabeledCSVParser(com.Ostermiller.util.LabeledCSVParser) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Date(java.util.Date) ZipInputStream(java.util.zip.ZipInputStream) BufferedReader(java.io.BufferedReader) HashMap(java.util.HashMap) Map(java.util.Map) HTreeMap(org.mapdb.HTreeMap) File(java.io.File) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DB(org.mapdb.DB) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Example 15 with InteractType

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

the class StudyImporterForSIAD method downloadAndImportResource.

private void downloadAndImportResource(String resource, String source) throws StudyImporterException {
    try {
        LabeledCSVParser labeledCSVParser = parserFactory.createParser(resource, "UTF-8");
        labeledCSVParser.changeDelimiter('\t');
        while (labeledCSVParser.getLine() != null) {
            String name = labeledCSVParser.getValueByLabel("name");
            String ref = labeledCSVParser.getValueByLabel("source");
            String title = "SIAD-" + ref;
            String citation = "ABRS 2009. Australian Faunal Directory. " + name + ". Australian Biological Resources StudyNode, Canberra. " + CitationUtil.createLastAccessedString(ref);
            StudyImpl study1 = new StudyImpl(title, source, null, citation);
            study1.setExternalId(ref);
            Study study = nodeFactory.getOrCreateStudy(study1);
            Specimen specimen = nodeFactory.createSpecimen(study, new TaxonImpl(name, null));
            String hostName = labeledCSVParser.getValueByLabel("host name");
            Specimen hostSpecimen = nodeFactory.createSpecimen(study, new TaxonImpl(hostName, null));
            InteractType type = map.get(labeledCSVParser.getValueByLabel("interaction"));
            specimen.interactsWith(hostSpecimen, type);
        }
    } catch (FileNotFoundException e) {
        throw new StudyImporterException("failed to open tmp file", e);
    } catch (NodeFactoryException e) {
        throw new StudyImporterException("failed to map data", e);
    } catch (IOException e) {
        throw new StudyImporterException("failed to read resource [" + resource + "]", e);
    }
}
Also used : InteractType(org.eol.globi.domain.InteractType) Study(org.eol.globi.domain.Study) Specimen(org.eol.globi.domain.Specimen) TaxonImpl(org.eol.globi.domain.TaxonImpl) StudyImpl(org.eol.globi.domain.StudyImpl) FileNotFoundException(java.io.FileNotFoundException) LabeledCSVParser(com.Ostermiller.util.LabeledCSVParser) IOException(java.io.IOException)

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