Skip to content

Commit

Permalink
IUCN
Browse files Browse the repository at this point in the history
  • Loading branch information
djtfmartin committed Sep 6, 2024
1 parent cb8553f commit 07ca4a5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.stream.Collectors;
import life.catalogue.matching.model.*;
import life.catalogue.matching.service.MatchingService;
import life.catalogue.matching.util.IUCNUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand Down Expand Up @@ -671,15 +672,15 @@ public Map<String, Object> iucnRedListV1(@PathVariable(value = "usageKey", requi
return Map.of();
}
NameUsageMatch.Status status = statusList.get(0);
String formatted = formatIucn(status.getStatus());
String formatted = IUCNUtils.formatIucn(status.getStatus());
if (formatted == null || formatted.isEmpty()) {
return Map.of();
}

String scientificName = match.getAcceptedUsage() != null ? match.getAcceptedUsage().getCanonicalName() : match.getUsage().getCanonicalName();

try {
IUCN iucn = IUCN.valueOf(formatted); // throws IllegalArgumentException if not found
IUCNUtils.IUCN iucn = IUCNUtils.IUCN.valueOf(formatted); // throws IllegalArgumentException if not found
watch.stop();
log("v1/species/iucnRedListCategory", usageKey, watch);
return Map.of(
Expand All @@ -689,7 +690,7 @@ public Map<String, Object> iucnRedListV1(@PathVariable(value = "usageKey", requi
"taxonomicStatus", NameUsageMatchV1.TaxonomicStatusV1.convert(
match.getDiagnostics().getStatus()),
"iucnTaxonID", status.getSourceId(),
"code", iucn.code
"code", iucn.getCode()
);
} catch (IllegalArgumentException e) {
log.error("IUCN category not found: {}", formatted, e);
Expand Down Expand Up @@ -751,37 +752,6 @@ private static void addIfNotNull(StringJoiner joiner, Object value) {
}
}

String formatIucn(String original){
if (original == null) {
return null;
}
// Trim the string
String trimmed = original.trim();
// Convert to uppercase
String uppercased = trimmed.toUpperCase();
// Replace any whitespace with a single underscore
return uppercased.replaceAll("\\s+", "_");
}

enum IUCN {
EXTINCT("EX"),
EXTINCT_IN_THE_WILD("EW"),
CRITICALLY_ENDANGERED ("CR"),
ENDANGERED ("EN"),
VULNERABLE ("VU"),
NEAR_THREATENED ("NT"),
CONSERVATION_DEPENDENT ("CD"),
LEAST_CONCERN ("LC"),
DATA_DEFICIENT ("DD"),
NOT_EVALUATED ("NE");

private final String code;

IUCN(String code) {
this.code = code;
}
}

@Data
@Builder
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import life.catalogue.api.vocab.MatchType;
import life.catalogue.api.vocab.TaxonomicStatus;
import life.catalogue.matching.model.*;
import life.catalogue.matching.util.IUCNUtils;
import life.catalogue.matching.util.LuceneUtils;
import life.catalogue.matching.Main;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -806,9 +807,13 @@ private NameUsageMatch fromDoc(Document doc) {
TopDocs docs = ancillarySearcher.search(query, 3);
if (docs.totalHits.value > 0) {
Document ancillaryDoc = ancillarySearcher.storedFields().document(docs.scoreDocs[0].doc);
String status = ancillaryDoc.get(FIELD_CATEGORY);
NameUsageMatch.Status ancillaryStatus = new NameUsageMatch.Status();
ancillaryStatus.setStatus(status);
ancillaryStatus.setStatus(ancillaryDoc.get(FIELD_CATEGORY));
//FIXME - this needs to removed from here - use a vocab
String formattedIUCN = IUCNUtils.formatIucn(ancillaryDoc.get(FIELD_CATEGORY));
IUCNUtils.IUCN iucn = IUCNUtils.IUCN.valueOf(formattedIUCN);
ancillaryStatus.setStatus(formattedIUCN);
ancillaryStatus.setStatusCode(iucn.getCode());
ancillaryStatus.setDatasetKey(dataset.getKey().toString());
ancillaryStatus.setGbifKey(dataset.getGbifKey());
ancillaryStatus.setDatasetAlias(dataset.getAlias());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public static class RankedName implements Serializable {
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@Schema(description = "A status value derived from a dataset or external source. E.g. IUCN Red List status.",
@Schema(description = "A status value derived from a dataset or external source. E.g. IUCN Red List.",
title = "Status", type = "object")
public static class Status {
@Schema(description = "The dataset key for the dataset that the status is associated with")
Expand All @@ -431,6 +431,8 @@ public static class Status {
private String gbifKey;
@Schema(description = "The status value")
private String status;
@Schema(description = "The status code value")
private String statusCode;
@Schema(description = "The ID in the source dataset for this status. e.g. the IUCN ID for this taxon")
private String sourceId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package life.catalogue.matching.util;

public class IUCNUtils {

public static String formatIucn(String original){
if (original == null) {
return null;
}
// Trim the string
String trimmed = original.trim();
// Convert to uppercase
String uppercased = trimmed.toUpperCase();
// Replace any whitespace with a single underscore
return uppercased.replaceAll("\\s+", "_");
}

public enum IUCN {
EXTINCT("EX"),
EXTINCT_IN_THE_WILD("EW"),
CRITICALLY_ENDANGERED ("CR"),
ENDANGERED ("EN"),
VULNERABLE ("VU"),
NEAR_THREATENED ("NT"),
CONSERVATION_DEPENDENT ("CD"),
LEAST_CONCERN ("LC"),
DATA_DEFICIENT ("DD"),
NOT_EVALUATED ("NE");

private final String code;

IUCN(String code) {
this.code = code;
}

public String getCode() {
return code;
}

}
}
6 changes: 5 additions & 1 deletion matching-ws/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
<pattern> %highlight(%boldGreen([%date{'yyyy-MM-dd HH:mm:ss'}]) %blue([${LOG_LEVEL_PATTERN:-%4p}]) %boldCyan([%logger{0}]) %boldYellow(%msg%n))</pattern>
</encoder>
</appender>
<root level="WARN">
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
<!-- Suppress INFO logs from life.catalogue -->
<!-- <logger name="com.ibatis" level="DEBUG"/>-->
<!-- <logger name="java.sql" level="DEBUG"/>-->
<!-- <logger name="life.catalogue.matching.db.IndexingMapper" level="TRACE"/>-->
<logger name="life.catalogue" level="INFO"/>
<logger name="life.catalogue.common.tax" level="ERROR"/>
<logger name="org.springframework.web" level="ERROR"/>
Expand Down

0 comments on commit 07ca4a5

Please sign in to comment.