diff --git a/core-parser/src/main/java/org/uniprot/core/parser/tsv/uniprot/EntryReferenceMap.java b/core-parser/src/main/java/org/uniprot/core/parser/tsv/uniprot/EntryReferenceMap.java index 9dd6218ae..4870bf73e 100644 --- a/core-parser/src/main/java/org/uniprot/core/parser/tsv/uniprot/EntryReferenceMap.java +++ b/core-parser/src/main/java/org/uniprot/core/parser/tsv/uniprot/EntryReferenceMap.java @@ -9,7 +9,9 @@ import org.uniprot.core.uniprotkb.UniProtKBReference; public class EntryReferenceMap implements NamedValueMap { - static final List FIELDS = Arrays.asList("lit_pubmed_id"); + static final String LIT_PUBMED_ID = "lit_pubmed_id"; + static final String LIT_DOI_ID = "lit_doi_id"; + static final List FIELDS = Arrays.asList(LIT_PUBMED_ID, LIT_DOI_ID); private final List references; public EntryReferenceMap(List references) { @@ -26,16 +28,20 @@ public Map attributeValues() { return Collections.emptyMap(); } - String result = + Map idMaps = references.stream() .map(UniProtKBReference::getCitation) .filter(val -> val.getCitationCrossReferences() != null) .flatMap(val -> val.getCitationCrossReferences().stream()) - .filter(val -> val.getDatabase().equals(CitationDatabase.PUBMED)) - .map(CrossReference::getId) - .collect(Collectors.joining("; ")); + .collect( + Collectors.groupingBy( + CrossReference::getDatabase, + Collectors.mapping( + CrossReference::getId, Collectors.joining("; ")))); + Map map = new HashMap<>(); - map.put(FIELDS.get(0), result); + map.put(LIT_PUBMED_ID, idMaps.getOrDefault(CitationDatabase.PUBMED, "")); + map.put(LIT_DOI_ID, idMaps.getOrDefault(CitationDatabase.DOI, "")); return map; } diff --git a/core-parser/src/test/java/org/uniprot/core/parser/tsv/uniprot/EntryReferenceMapTest.java b/core-parser/src/test/java/org/uniprot/core/parser/tsv/uniprot/EntryReferenceMapTest.java new file mode 100644 index 000000000..751f58085 --- /dev/null +++ b/core-parser/src/test/java/org/uniprot/core/parser/tsv/uniprot/EntryReferenceMapTest.java @@ -0,0 +1,117 @@ +package org.uniprot.core.parser.tsv.uniprot; + +import static org.junit.jupiter.api.Assertions.*; +import static org.uniprot.core.parser.tsv.uniprot.EntryReferenceMap.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.Test; +import org.uniprot.core.citation.Citation; +import org.uniprot.core.citation.CitationDatabase; +import org.uniprot.core.citation.impl.ElectronicArticleBuilder; +import org.uniprot.core.impl.CrossReferenceBuilder; +import org.uniprot.core.uniprotkb.UniProtKBReference; +import org.uniprot.core.uniprotkb.impl.UniProtKBReferenceBuilder; + +class EntryReferenceMapTest { + + private static final String FIELD_SEPARATOR = "; "; + + @Test + void testFields() { + List expected = Arrays.asList(LIT_PUBMED_ID, LIT_DOI_ID); + assertEquals(expected, FIELDS); + } + + @Test + void testLitDoiId() { + String doiId = "doiId1"; + List references = getUniProtKBReferences(CitationDatabase.DOI, doiId); + EntryReferenceMap mapper = new EntryReferenceMap(references); + Map result = mapper.attributeValues(); + + verifyFieldValue(result, LIT_DOI_ID, doiId); + } + + @Test + void testMultipleLitDoiId() { + String doiId = "doiId1"; + String doiId2 = "doiId2"; + List references = + getUniProtKBReferences(CitationDatabase.DOI, doiId, doiId2); + EntryReferenceMap mapper = new EntryReferenceMap(references); + Map result = mapper.attributeValues(); + + verifyFieldValue(result, LIT_DOI_ID, doiId + FIELD_SEPARATOR + doiId2); + } + + @Test + void testLitPubmedId() { + String pubmedId = "pubmedId1"; + List references = + getUniProtKBReferences(CitationDatabase.PUBMED, pubmedId); + EntryReferenceMap mapper = new EntryReferenceMap(references); + Map result = mapper.attributeValues(); + + verifyFieldValue(result, LIT_PUBMED_ID, pubmedId); + } + + @Test + void testMultipleLitPubmedId() { + String pubmedId = "pubmedId1"; + String pubmedId2 = "pubmedId2"; + List references = + getUniProtKBReferences(CitationDatabase.PUBMED, pubmedId, pubmedId2); + + EntryReferenceMap mapper = new EntryReferenceMap(references); + Map result = mapper.attributeValues(); + + verifyFieldValue(result, LIT_PUBMED_ID, pubmedId + FIELD_SEPARATOR + pubmedId2); + } + + @Test + void testMixedIds() { + String pubmedId = "pubmedId1"; + String pubmedId2 = "pubmedId2"; + String doiId = "doiId1"; + String doiId2 = "doiId2"; + List references = + getUniProtKBReferences(CitationDatabase.PUBMED, pubmedId, pubmedId2); + references.addAll(getUniProtKBReferences(CitationDatabase.DOI, doiId, doiId2)); + + EntryReferenceMap mapper = new EntryReferenceMap(references); + Map result = mapper.attributeValues(); + + verifyFieldValue(result, LIT_PUBMED_ID, pubmedId + FIELD_SEPARATOR + pubmedId2); + verifyFieldValue(result, LIT_DOI_ID, doiId + FIELD_SEPARATOR + doiId2); + } + + private List getUniProtKBReferences( + CitationDatabase database, String... ids) { + List result = new ArrayList<>(); + for (String id : ids) { + Citation citation = + new ElectronicArticleBuilder() + .citationCrossReferencesAdd( + new CrossReferenceBuilder() + .database(database) + .id(id) + .build()) + .build(); + UniProtKBReference reference = + new UniProtKBReferenceBuilder().citation(citation).build(); + result.add(reference); + } + return result; + } + + private void verifyFieldValue( + Map result, String fieldName, String expectedValue) { + String resultValue = result.get(fieldName); + assertNotNull(resultValue); + assertEquals(expectedValue, resultValue); + } +}