use of org.eol.globi.domain.InteractType in project eol-globi-data by jhpoelen.
the class InteractionListenerImpl method importValidLink.
private void importValidLink(Map<String, String> link) throws StudyImporterException, IOException {
Study study = nodeFactory.getOrCreateStudy(studyFromLink(link));
Specimen source = createSpecimen(link, study, SOURCE_TAXON_NAME, SOURCE_TAXON_ID, SOURCE_BODY_PART_NAME, SOURCE_BODY_PART_ID);
Specimen target = createSpecimen(link, study, TARGET_TAXON_NAME, TARGET_TAXON_ID, TARGET_BODY_PART_NAME, TARGET_BODY_PART_ID);
String interactionTypeId = link.get(INTERACTION_TYPE_ID);
InteractType type = InteractType.typeOf(interactionTypeId);
source.interactsWith(target, type, getOrCreateLocation(study, link));
}
use of org.eol.globi.domain.InteractType in project eol-globi-data by jhpoelen.
the class StudyImporterForINaturalist method parseSingleInteractions.
private void parseSingleInteractions(JsonNode jsonNode, List<Integer> typesIgnored, Map<Integer, InteractType> typeMap) throws NodeFactoryException, StudyImporterException, IOException {
Taxon targetTaxon = null;
Taxon sourceTaxon = null;
if (jsonNode.has("taxon") && jsonNode.has("observation")) {
targetTaxon = parseTaxon(jsonNode.get("taxon"));
JsonNode observation = jsonNode.get("observation");
if (jsonNode.has("taxon")) {
sourceTaxon = parseTaxon(observation.get("taxon"));
}
}
long observationId = jsonNode.get("observation_id").getLongValue();
if (targetTaxon == null) {
LOG.debug("skipping interaction with missing target taxon name for observation [" + observationId + "]");
} else if (sourceTaxon == null) {
LOG.warn("cannot create interaction with missing source taxon name for observation with id [" + observationId + "]");
} else {
JsonNode observationField = jsonNode.get("observation_field");
String interactionDataType = observationField.get("datatype").getTextValue();
String interactionTypeName = observationField.get("name").getTextValue();
Integer interactionTypeId = observationField.get("id").getIntValue();
if (typesIgnored.contains(interactionTypeId)) {
LOG.debug("ignoring taxon observation field type [" + interactionTypeName + "] with id [" + interactionTypeId + "] for observation with id [" + observationId + "]");
} else {
InteractType interactType = typeMap.get(interactionTypeId);
if (interactType == null) {
unsupportedInteractionTypes.put(observationId, interactionTypeName + ",https://www.inaturalist.org/observation_fields/" + interactionTypeId);
LOG.debug("no interaction type associated with observation field type [" + interactionTypeName + "] with id [" + interactionTypeId + "] for observation with id [" + observationId + "]");
} else {
handleObservation(jsonNode, targetTaxon, observationId, interactionDataType, interactType, interactionTypeName, sourceTaxon);
}
}
}
}
use of org.eol.globi.domain.InteractType in project eol-globi-data by jhpoelen.
the class TableInteractionListenerProxy method newLink.
@Override
public void newLink(final Map<String, String> properties) throws StudyImporterException {
final HashMap<String, String> enrichedProperties = new HashMap<String, String>() {
{
putAll(properties);
put(StudyImporterForTSV.STUDY_SOURCE_CITATION, dataSourceCitation);
final String referenceCitation = StringUtils.isBlank(properties.get(StudyImporterForTSV.REFERENCE_CITATION)) ? StudyImporterForMetaTable.generateReferenceCitation(properties) : properties.get(StudyImporterForTSV.REFERENCE_CITATION);
put(StudyImporterForTSV.REFERENCE_ID, dataSourceCitation + referenceCitation);
put(StudyImporterForTSV.REFERENCE_CITATION, StringUtils.isBlank(referenceCitation) ? dataSourceCitation : referenceCitation);
if (!properties.containsKey(StudyImporterForTSV.SOURCE_TAXON_NAME)) {
put(StudyImporterForTSV.SOURCE_TAXON_NAME, StudyImporterForMetaTable.generateSourceTaxonName(properties));
}
if (!properties.containsKey(StudyImporterForTSV.TARGET_TAXON_NAME)) {
put(StudyImporterForTSV.TARGET_TAXON_NAME, StudyImporterForMetaTable.generateTargetTaxonName(properties));
}
}
};
InteractType type = StudyImporterForMetaTable.generateInteractionType(enrichedProperties);
StudyImporterForMetaTable.setInteractionType(enrichedProperties, type);
interactionListener.newLink(enrichedProperties);
}
use of org.eol.globi.domain.InteractType in project eol-globi-data by jhpoelen.
the class StudyImporterForCoetzer method importStudy.
@Override
public void importStudy() throws StudyImporterException {
if (org.apache.commons.lang.StringUtils.isBlank(getResourceArchiveURI())) {
throw new StudyImporterException("failed to import [" + getDataset().getNamespace() + "]: no [archiveURL] specified");
}
DB db = DBMaker.newMemoryDirectDB().compressionEnable().transactionDisable().make();
final HTreeMap<Integer, String> taxonMap = db.createHashMap("taxonMap").make();
final HTreeMap<Integer, String> refMap = db.createHashMap("refMap").make();
try {
InputStream inputStream = DatasetUtil.getNamedResourceStream(getDataset(), "archive");
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
ZipEntry entry;
File taxonTempFile = null;
File assocTempFile = null;
File referencesTempFile = null;
File distributionTempFile = null;
while ((entry = zipInputStream.getNextEntry()) != null) {
if (entry.getName().matches("(^|(.*/))taxon.txt$")) {
taxonTempFile = FileUtils.saveToTmpFile(zipInputStream, entry);
} else if (entry.getName().matches("(^|(.*/))description.txt$")) {
assocTempFile = FileUtils.saveToTmpFile(zipInputStream, entry);
} else if (entry.getName().matches("(^|(.*/))references.txt$")) {
referencesTempFile = FileUtils.saveToTmpFile(zipInputStream, entry);
} else if (entry.getName().matches("(^|(.*/))distribution.txt$")) {
distributionTempFile = FileUtils.saveToTmpFile(zipInputStream, entry);
} else {
IOUtils.copy(zipInputStream, new NullOutputStream());
}
}
IOUtils.closeQuietly(zipInputStream);
if (taxonTempFile == null) {
throw new StudyImporterException("failed to find expected [taxon.txt] resource");
}
if (assocTempFile == null) {
throw new StudyImporterException("failed to find expected [description.txt] resource");
}
if (referencesTempFile == null) {
throw new StudyImporterException("failed to find expected [references.txt] resource");
}
if (distributionTempFile == null) {
throw new StudyImporterException("failed to find expected [distribution.txt] resource");
}
BufferedReader assocReader = FileUtils.getUncompressedBufferedReader(new FileInputStream(taxonTempFile), CharsetConstant.UTF8);
LabeledCSVParser parser = CSVTSVUtil.createLabeledCSVParser(assocReader);
parser.changeDelimiter('\t');
String[] line;
while ((line = parser.getLine()) != null) {
taxonMap.put(Integer.parseInt(line[0]), nameFor(line));
}
LabeledCSVParser refs = CSVTSVUtil.createLabeledCSVParser(new FileInputStream(referencesTempFile));
refs.changeDelimiter('\t');
String[] refsLine;
while ((refsLine = refs.getLine()) != null) {
refMap.put(Integer.parseInt(refsLine[0]), refsLine[1]);
}
LabeledCSVParser assoc = CSVTSVUtil.createLabeledCSVParser(new FileInputStream(assocTempFile));
assoc.changeDelimiter('\t');
final Map<String, InteractType> interactTypeMap = new HashMap<String, InteractType>() {
{
put("Visits flowers of", InteractType.VISITS_FLOWERS_OF);
put("Host of", InteractType.VISITS_FLOWERS_OF);
put("Parasite of", InteractType.PARASITE_OF);
put("Nests in", InteractType.INTERACTS_WITH);
}
};
String[] assocLine;
while ((assocLine = assoc.getLine()) != null) {
final Integer taxonId = Integer.parseInt(assocLine[0]);
final String[] parts = assocLine[2].split(":");
if (parts.length > 1) {
String interactionString = parts[0];
String[] targetTaxonNames = parts[1].split(",");
for (String targetTaxonName : targetTaxonNames) {
final String reference = refMap.get(taxonId);
final String sourceTaxonName = taxonMap.get(taxonId);
if (StringUtils.isNotBlank(reference) && StringUtils.isNotBlank(sourceTaxonName)) {
final Study study = nodeFactory.getOrCreateStudy(new StudyImpl(getSourceCitation() + reference, getSourceCitationLastAccessed(), null, reference));
final Specimen source = nodeFactory.createSpecimen(study, new TaxonImpl(StringUtils.trim(sourceTaxonName), null));
final Specimen target = nodeFactory.createSpecimen(study, new TaxonImpl(StringUtils.trim(targetTaxonName), null));
final InteractType relType = interactTypeMap.get(interactionString);
if (relType == null) {
throw new StudyImporterException("found unsupported interaction type [" + interactionString + "]");
}
source.interactsWith(target, relType);
}
}
}
}
} catch (IOException | NodeFactoryException e) {
throw new StudyImporterException(e);
}
db.close();
}
use of org.eol.globi.domain.InteractType in project eol-globi-data by jhpoelen.
the class StudyImporterForBioInfo method createRelations.
protected void createRelations(LabeledCSVParser parser, Map<String, String> refMap, Map<String, Taxon> taxonMap) throws StudyImporterException {
try {
long count = 1;
while (parser.getLine() != null) {
if (importFilter.shouldImportRecord(count)) {
String refIds = parser.getValueByLabel("list of reference ids");
if (StringUtils.isNotBlank(refIds)) {
String[] ids = StringUtils.split(refIds, ";");
for (String id : ids) {
String trimmedId = StringUtils.trim(id);
Study study = createStudy(trimmedId, refMap.get(trimmedId));
String relationship = parser.getValueByLabel("relationship");
if (StringUtils.isBlank(relationship)) {
getLogger().warn(study, "no relationship for record on line [" + (parser.lastLineNumber() + 1) + "]");
}
InteractType interactType = INTERACTION_MAPPING.get(relationship);
if (null == interactType) {
getLogger().warn(study, "no mapping found for relationship [" + relationship + "] for record on line [" + (parser.lastLineNumber() + 1) + "]");
} else {
importInteraction(parser, study, interactType, taxonMap);
}
}
}
}
count++;
}
} catch (IOException e1) {
throw new StudyImporterException("problem reading trophic relations data", e1);
}
}
Aggregations