Skip to content

Commit 0c05540

Browse files
authored
First part for implementaion of a Europe PMC fetcher (#13389)
* First part for implementaion of a Europe PMC fetcher * fix checkstyle and var name * fix checkstyle and var name * add pages cleanup and add changelog entry * remove comment
1 parent 4fdf5b4 commit 0c05540

File tree

4 files changed

+207
-0
lines changed

4 files changed

+207
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
1414
- We introduced a settings parameter to manage citations' relations local storage time-to-live with a default value set to 30 days. [#11189](https://github.com/JabRef/jabref/issues/11189)
1515
- We distribute arm64 images for Linux. [#10842](https://github.com/JabRef/jabref/issues/10842)
1616
- We added the field `monthfiled` to the default list of fields to resolve BibTeX-Strings for [#13375](https://github.com/JabRef/jabref/issues/13375)
17+
- We added a new ID based fetcher for [EuropePMC](https://europepmc.org/). [#13389](https://github.com/JabRef/jabref/pull/13389)
1718

1819
### Changed
1920

jablib/src/main/java/org/jabref/logic/importer/WebFetchers.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.jabref.logic.importer.fetcher.DiVA;
2727
import org.jabref.logic.importer.fetcher.DoiFetcher;
2828
import org.jabref.logic.importer.fetcher.DoiResolution;
29+
import org.jabref.logic.importer.fetcher.EuropePmcFetcher;
2930
import org.jabref.logic.importer.fetcher.GvkFetcher;
3031
import org.jabref.logic.importer.fetcher.IEEE;
3132
import org.jabref.logic.importer.fetcher.INSPIREFetcher;
@@ -147,6 +148,7 @@ public static SortedSet<IdBasedFetcher> getIdBasedFetchers(ImportFormatPreferenc
147148
// .addRetryFetcher(new DoiToBibtexConverterComIsbnFetcher(importFormatPreferences)));
148149
set.add(new DiVA(importFormatPreferences));
149150
set.add(new DoiFetcher(importFormatPreferences));
151+
set.add(new EuropePmcFetcher());
150152
set.add(new MedlineFetcher());
151153
set.add(new TitleFetcher(importFormatPreferences));
152154
set.add(new MathSciNet(importFormatPreferences));
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package org.jabref.logic.importer.fetcher;
2+
3+
import java.net.MalformedURLException;
4+
import java.net.URI;
5+
import java.net.URISyntaxException;
6+
import java.net.URL;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Optional;
10+
11+
import org.jabref.logic.cleanup.FieldFormatterCleanup;
12+
import org.jabref.logic.formatter.bibtexfields.NormalizePagesFormatter;
13+
import org.jabref.logic.importer.IdBasedParserFetcher;
14+
import org.jabref.logic.importer.ParseException;
15+
import org.jabref.logic.importer.Parser;
16+
import org.jabref.logic.importer.util.JsonReader;
17+
import org.jabref.model.entry.Author;
18+
import org.jabref.model.entry.AuthorList;
19+
import org.jabref.model.entry.BibEntry;
20+
import org.jabref.model.entry.Month;
21+
import org.jabref.model.entry.field.StandardField;
22+
import org.jabref.model.entry.field.UnknownField;
23+
import org.jabref.model.entry.types.EntryType;
24+
import org.jabref.model.entry.types.StandardEntryType;
25+
26+
import kong.unirest.core.json.JSONArray;
27+
import kong.unirest.core.json.JSONException;
28+
import kong.unirest.core.json.JSONObject;
29+
30+
public class EuropePmcFetcher implements IdBasedParserFetcher {
31+
32+
@Override
33+
public URL getUrlForIdentifier(String identifier) throws URISyntaxException, MalformedURLException {
34+
return URI.create("https://www.ebi.ac.uk/europepmc/webservices/rest/search?query=" + identifier + "&resultType=core&format=json").toURL();
35+
}
36+
37+
@Override
38+
public Parser getParser() {
39+
return inputStream -> {
40+
JSONObject response = JsonReader.toJsonObject(inputStream);
41+
if (response.isEmpty()) {
42+
return List.of();
43+
}
44+
return List.of(jsonItemToBibEntry(response));
45+
};
46+
}
47+
48+
private BibEntry jsonItemToBibEntry(JSONObject item) throws ParseException {
49+
try {
50+
JSONObject result = item.getJSONObject("resultList").getJSONArray("result").getJSONObject(0);
51+
52+
System.out.println(result.toString(2));
53+
54+
EntryType entryType = StandardEntryType.Article;
55+
if (result.has("pubTypeList")) {
56+
for (Object o : result.getJSONObject("pubTypeList").getJSONArray("pubType")) {
57+
if ("letter".equalsIgnoreCase(o.toString())) {
58+
entryType = StandardEntryType.Article;
59+
break;
60+
// TODO: handle other types e.g. books
61+
}
62+
}
63+
}
64+
65+
BibEntry entry = new BibEntry(entryType);
66+
67+
entry.setField(StandardField.TITLE, result.optString("title"));
68+
entry.setField(StandardField.ABSTRACT, result.optString("abstractText"));
69+
70+
entry.setField(StandardField.YEAR, result.optString("pubYear"));
71+
entry.setField(StandardField.VOLUME, result.optString("journalVolume"));
72+
entry.setField(StandardField.ISSUE, result.optString("journalIssue"));
73+
74+
String pages = result.optString("pageInfo");
75+
entry.setField(StandardField.PAGES, pages);
76+
77+
entry.setField(StandardField.DOI, result.optString("doi"));
78+
entry.setField(StandardField.PMID, result.optString("pmid"));
79+
80+
// Handle URL
81+
if (result.has("pmid")) {
82+
entry.setField(StandardField.URL, "https://pubmed.ncbi.nlm.nih.gov/" + result.getString("pmid") + "/");
83+
}
84+
85+
if (result.has("journalInfo") && result.getJSONObject("journalInfo").has("issn")) {
86+
entry.setField(StandardField.ISSN, result.getJSONObject("journalInfo").getString("issn"));
87+
}
88+
89+
// Handle authors
90+
if (result.has("authorList") && result.getJSONObject("authorList").has("author")) {
91+
JSONArray authors = result.getJSONObject("authorList").getJSONArray("author");
92+
93+
List<Author> authorList = new ArrayList<>();
94+
95+
for (int i = 0; i < authors.length(); i++) {
96+
JSONObject author = authors.getJSONObject(i);
97+
98+
String lastName = author.optString("lastName", "");
99+
String firstName = author.optString("firstName", "");
100+
authorList.add(new Author(firstName, "", "", lastName, ""));
101+
102+
entry.setField(StandardField.AUTHOR, AuthorList.of(authorList).getAsLastFirstNamesWithAnd(false));
103+
}
104+
}
105+
106+
if (result.has("pubTypeList") && result.getJSONObject("pubTypeList").has("pubType")) {
107+
JSONArray pubTypes = result.getJSONObject("pubTypeList").getJSONArray("pubType");
108+
if (!pubTypes.isEmpty()) {
109+
entry.setField(StandardField.PUBSTATE, pubTypes.getString(0));
110+
}
111+
}
112+
113+
if (result.has("pubModel")) {
114+
Optional.ofNullable(result.optString("pubModel")).ifPresent(pubModel -> entry.setField(StandardField.HOWPUBLISHED, pubModel));
115+
}
116+
if (result.has("publicationStatus")) {
117+
Optional.ofNullable(result.optString("publicationStatus")).ifPresent(pubStatus -> entry.setField(StandardField.PUBSTATE, pubStatus));
118+
}
119+
120+
if (result.has("journalInfo")) {
121+
JSONObject journalInfo = result.getJSONObject("journalInfo");
122+
Optional.ofNullable(journalInfo.optString("issue")).ifPresent(issue -> entry.setField(StandardField.ISSUE, issue));
123+
Optional.ofNullable(journalInfo.optString("volume")).ifPresent(volume -> entry.setField(StandardField.VOLUME, volume));
124+
Optional.of(journalInfo.optInt("yearOfPublication")).ifPresent(year -> entry.setField(StandardField.YEAR, year.toString()));
125+
Optional.of(journalInfo.optInt("monthOfPublication"))
126+
.flatMap(month -> Month.parse(month.toString()))
127+
.ifPresent(parsedMonth -> entry.setField(StandardField.MONTH, parsedMonth.getJabRefFormat()));
128+
if (journalInfo.has("journal")) {
129+
JSONObject journal = journalInfo.getJSONObject("journal");
130+
Optional.ofNullable(journal.optString("title")).ifPresent(title -> entry.setField(StandardField.JOURNAL, title));
131+
Optional.ofNullable(journal.optString("nlmid")).ifPresent(nlmid -> entry.setField(new UnknownField("nlmid"), nlmid));
132+
Optional.ofNullable(journal.optString("issn")).ifPresent(issn -> entry.setField(StandardField.ISSN, issn));
133+
}
134+
}
135+
136+
return entry;
137+
} catch (JSONException e) {
138+
throw new ParseException("Error parsing EuropePMC response", e);
139+
}
140+
}
141+
142+
@Override
143+
public void doPostCleanup(BibEntry entry) {
144+
new FieldFormatterCleanup(StandardField.PAGES, new NormalizePagesFormatter()).cleanup(entry);
145+
}
146+
147+
@Override
148+
public String getName() {
149+
return "Europe/PMCID";
150+
}
151+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.jabref.logic.importer.fetcher;
2+
3+
import java.util.Optional;
4+
5+
import org.jabref.logic.importer.FetcherException;
6+
import org.jabref.model.entry.BibEntry;
7+
import org.jabref.model.entry.field.StandardField;
8+
import org.jabref.model.entry.field.UnknownField;
9+
import org.jabref.model.entry.types.StandardEntryType;
10+
import org.jabref.testutils.category.FetcherTest;
11+
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
17+
18+
@FetcherTest
19+
class EuropePmcFetcherTest {
20+
21+
private EuropePmcFetcher fetcher;
22+
private BibEntry entryWijedasa;
23+
24+
@BeforeEach
25+
void setUp() {
26+
fetcher = new EuropePmcFetcher();
27+
entryWijedasa = new BibEntry(StandardEntryType.Article)
28+
.withField(StandardField.AUTHOR, "Wijedasa, Lahiru S. and Jauhiainen, Jyrki and Könönen, Mari and Lampela, Maija and Vasander, Harri and Leblanc, Marie-Claire and Evers, Stephanie and Smith, Thomas E. L. and Yule, Catherine M. and Varkkey, Helena and Lupascu, Massimo and Parish, Faizal and Singleton, Ian and Clements, Gopalasamy R. and Aziz, Sheema Abdul and Harrison, Mark E. and Cheyne, Susan and Anshari, Gusti Z. and Meijaard, Erik and Goldstein, Jenny E. and Waldron, Susan and Hergoualc'h, Kristell and Dommain, Rene and Frolking, Steve and Evans, Christopher D. and Posa, Mary Rose C. and Glaser, Paul H. and Suryadiputra, Nyoman and Lubis, Reza and Santika, Truly and Padfield, Rory and Kurnianto, Sofyan and Hadisiswoyo, Panut and Lim, Teck Wyn and Page, Susan E. and Gauci, Vincent and Van Der Meer, Peter J. and Buckland, Helen and Garnier, Fabien and Samuel, Marshall K. and Choo, Liza Nuriati Lim Kim and O'Reilly, Patrick and Warren, Matthew and Suksuwan, Surin and Sumarga, Elham and Jain, Anuj and Laurance, William F. and Couwenberg, John and Joosten, Hans and Vernimmen, Ronald and Hooijer, Aljosja and Malins, Chris and Cochrane, Mark A. and Perumal, Balu and Siegert, Florian and Peh, Kelvin S.-H. and Comeau, Louis-Pierre and Verchot, Louis and Harvey, Charles F. and Cobb, Alex and Jaafar, Zeehan and Wösten, Henk and Manuri, Solichin and Müller, Moritz and Giesen, Wim and Phelps, Jacob and Yong, Ding Li and Silvius, Marcel and Wedeux, Béatrice M. M. and Hoyt, Alison and Osaki, Mitsuru and Hirano, Takashi and Takahashi, Hidenori and Kohyama, Takashi S. and Haraguchi, Akira and Nugroho, Nunung P. and Coomes, David A. and Quoi, Le Phat and Dohong, Alue and Gunawan, Haris and Gaveau, David L. A. and Langner, Andreas and Lim, Felix K. S. and Edwards, David P. and Giam, Xingli and Van Der Werf, Guido and Carmenta, Rachel and Verwer, Caspar C. and Gibson, Luke and Gandois, Laure and Graham, Laura Linda Bozena and Regalino, Jhanson and Wich, Serge A. and Rieley, Jack and Kettridge, Nicholas and Brown, Chloe and Pirard, Romain and Moore, Sam and Capilla, B. Ripoll and Ballhorn, Uwe and Ho, Hua Chew and Hoscilo, Agata and Lohberger, Sandra and Evans, Theodore A. and Yulianti, Nina and Blackham, Grace and Onrizal and Husson, Simon and Murdiyarso, Daniel and Pangala, Sunita and Cole, Lydia E. S. and Tacconi, Luca and Segah, Hendrik and Tonoto, Prayoto and Lee, Janice S. H. and Schmilewski, Gerald and Wulffraat, Stephan and Putra, Erianto Indra and Cattau, Megan E. and Clymo, R. S. and Morrison, Ross and Mujahid, Aazani and Miettinen, Jukka and Liew, Soo Chin and Valpola, Samu and Wilson, David and D'Arcy, Laura and Gerding, Michiel and Sundari, Siti and Thornton, Sara A. and Kalisz, Barbara and Chapman, Stephen J. and Su, Ahmad Suhaizi Mat and Basuki, Imam and Itoh, Masayuki and Traeholt, Carl and Sloan, Sean and Sayok, Alexander K. and Andersen, Roxane")
29+
.withField(StandardField.DOI, "10.1111/gcb.13516")
30+
.withField(StandardField.ISSN, "1354-1013") // there is also an essn
31+
.withField(StandardField.ISSUE, "3")
32+
.withField(StandardField.JOURNAL, "Global change biology")
33+
.withField(StandardField.MONTH, "#mar#")
34+
.withField(StandardField.PAGES, "977--982")
35+
.withField(StandardField.PMID, "27670948")
36+
.withField(StandardField.HOWPUBLISHED, "Print-Electronic")
37+
.withField(new UnknownField("nlmid"), "9888746")
38+
.withField(StandardField.PUBSTATE, "ppublish")
39+
.withField(StandardField.TITLE, "Denial of long-term issues with agriculture on tropical peatlands will have devastating consequences.")
40+
.withField(StandardField.VOLUME, "23")
41+
.withField(StandardField.URL, "https://pubmed.ncbi.nlm.nih.gov/27670948/")
42+
.withField(StandardField.YEAR, "2017");
43+
}
44+
45+
@Test
46+
void searchByIDWijedasa() throws FetcherException {
47+
Optional<BibEntry> fetchedEntry = fetcher.performSearchById("27670948");
48+
assertTrue(fetchedEntry.isPresent());
49+
50+
fetchedEntry.get().clearField(StandardField.ABSTRACT); // Remove abstract due to copyright
51+
assertEquals(Optional.of(entryWijedasa), fetchedEntry);
52+
}
53+
}

0 commit comments

Comments
 (0)