Skip to content

Commit

Permalink
- #4556 - Unable to access PMC BioC service
Browse files Browse the repository at this point in the history
- Fix PubAnnotation document access
- Re-enable tests
  • Loading branch information
reckart committed Mar 4, 2024
1 parent 4631370 commit 215083f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public PubAnnotationProvider(EntrezClient aEntrezClient)
List<PubAnnotationDocumentHandle> query(PubAnnotationProviderTraits aTraits, String aQuery,
int aPage, int aPageSize)
{
Map<String, String> variables = new HashMap<>();
var variables = new HashMap<String, String>();
variables.put("keywords", aQuery);
variables.put("page", Integer.toString(aPage));
variables.put("per", Integer.toString(aPageSize));
Expand All @@ -84,12 +84,12 @@ List<PubAnnotationDocumentHandle> query(PubAnnotationProviderTraits aTraits, Str
public List<ExternalSearchResult> executeQuery(DocumentRepository aDocumentRepository,
PubAnnotationProviderTraits aTraits, String aQuery)
{
List<PubAnnotationDocumentHandle> response = query(aTraits, aQuery, 1, 100);
var response = query(aTraits, aQuery, 1, 100);

var documentSummaries = lookupDocumentSummaries(response);

List<ExternalSearchResult> results = new ArrayList<>();
for (PubAnnotationDocumentHandle handle : response) {
var results = new ArrayList<ExternalSearchResult>();
for (var handle : response) {
var summary = documentSummaries
.get(Pair.of(handle.getSourceDb(), parseInt(handle.getSourceId())));
var result = new ExternalSearchResult(aDocumentRepository, handle.getSourceDb(),
Expand All @@ -112,7 +112,7 @@ public List<ExternalSearchResult> executeQuery(DocumentRepository aDocumentRepos
private Map<Pair<String, Integer>, DocSum> lookupDocumentSummaries(
List<PubAnnotationDocumentHandle> response)
{
Map<Pair<String, Integer>, DocSum> documentDetails = new HashMap<>();
var documentDetails = new HashMap<Pair<String, Integer>, DocSum>();
var groupedByDb = response.stream()
.collect(groupingBy(PubAnnotationDocumentHandle::getSourceDb));
for (var group : groupedByDb.entrySet()) {
Expand All @@ -133,25 +133,24 @@ public ExternalSearchResult getDocumentResult(DocumentRepository aRepository,
PubAnnotationProviderTraits aTraits, String aCollectionId, String aDocumentId)
throws IOException
{
ExternalSearchResult result = new ExternalSearchResult(aRepository, aCollectionId,
aDocumentId);
return result;
return new ExternalSearchResult(aRepository, aCollectionId, aDocumentId);
}

@Override
public String getDocumentText(DocumentRepository aDocumentRepository,
PubAnnotationProviderTraits aTraits, String aCollectionId, String aDocumentId)
{
return getSections(aDocumentRepository, aTraits, aCollectionId, aDocumentId).stream()
.map(PubAnnotationDocumentSection::getText).collect(Collectors.joining("\n\n"));
.map(PubAnnotationDocumentSection::getText) //
.collect(Collectors.joining("\n\n"));
}

@Override
public InputStream getDocumentAsStream(DocumentRepository aDocumentRepository,
PubAnnotationProviderTraits aTraits, String aCollectionId, String aDocumentId)
throws IOException
{
String json = JSONUtil.toJsonString(
var json = JSONUtil.toJsonString(
getSections(aDocumentRepository, aTraits, aCollectionId, aDocumentId));

return IOUtils.toInputStream(json, UTF_8);
Expand All @@ -160,18 +159,16 @@ public InputStream getDocumentAsStream(DocumentRepository aDocumentRepository,
private List<PubAnnotationDocumentSection> getSections(DocumentRepository aDocumentRepository,
PubAnnotationProviderTraits aTraits, String aCollectionId, String aDocumentId)
{
Map<String, String> variables = new HashMap<>();
var variables = new HashMap<String, String>();
variables.put("collectionId", aCollectionId);
variables.put("documentId", aDocumentId);

RestTemplate restTemplate = new RestTemplate();
var restTemplate = new RestTemplate();

var url = aTraits.getUrl() + "/docs/sourcedb/{collectionId}/sourceid/{documentId}.json";
var url = aTraits.getUrl() + "/docs/sourcedb/{collectionId}/sourceid/{documentId}";
try {
// If the document has multiple sections, a list is returned...
ResponseEntity<List<PubAnnotationDocumentSection>> response = restTemplate.exchange(url,
GET, null, SPRING_LIST_TYPE_REF, variables);

var response = restTemplate.exchange(url, GET, null, SPRING_LIST_TYPE_REF, variables);
return response.getBody();
}
catch (RestClientException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

import org.apache.uima.UIMAException;

import de.tudarmstadt.ukp.clarin.webanno.model.Project;
import de.tudarmstadt.ukp.inception.externalsearch.ExternalSearchProvider;
import de.tudarmstadt.ukp.inception.externalsearch.ExternalSearchResult;
import de.tudarmstadt.ukp.inception.externalsearch.model.DocumentRepository;
Expand Down Expand Up @@ -81,10 +80,10 @@ public List<ExternalSearchResult> executeQuery(DocumentRepository aDocumentRepos
var summaryResponse = entrezClient.esummary(DB_PUB_MED_CENTRAL,
searchResponse.getIdList().stream().mapToInt(i -> i).toArray());

List<ExternalSearchResult> results = new ArrayList<>();
var results = new ArrayList<ExternalSearchResult>();
for (var summary : summaryResponse.getDocSumaries()) {
ExternalSearchResult result = new ExternalSearchResult(aDocumentRepository,
DB_PUB_MED_CENTRAL, summary.getId() + EXT_XML);
var result = new ExternalSearchResult(aDocumentRepository, DB_PUB_MED_CENTRAL,
summary.getId() + EXT_XML);
result.setOriginalUri(
"https://www.ncbi.nlm.nih.gov/pmc/articles/" + PMCID_PREFIX + summary.getId());
result.setOriginalSource(summary.source());
Expand All @@ -110,9 +109,7 @@ public ExternalSearchResult getDocumentResult(DocumentRepository aRepository,
PubMedProviderTraits aTraits, String aCollectionId, String aDocumentId)
throws IOException
{
ExternalSearchResult result = new ExternalSearchResult(aRepository, aCollectionId,
aDocumentId);
return result;
return new ExternalSearchResult(aRepository, aCollectionId, aDocumentId);
}

@Override
Expand All @@ -123,7 +120,7 @@ public String getDocumentText(DocumentRepository aDocumentRepository,
var biocXml = pmcoaClient.bioc(aTraits, PMCID_PREFIX + stripExtension(aDocumentId));

try {
Project project = aDocumentRepository.getProject();
var project = aDocumentRepository.getProject();
var cas = WebAnnoCasUtil.createCas(schemaService.getFullProjectTypeSystem(project));
new BioCToCas().parseXml(new ByteArrayInputStream(biocXml), cas.getJCas());
return cas.getDocumentText();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void thatGetDocumentTextWorks() throws Exception
when(annotationService.getFullProjectTypeSystem(any()))
.thenReturn(createTypeSystemDescription());

String results = sut.getDocumentText(repo, traits, "PMC", "PMC7096989");
String results = sut.getDocumentText(repo, traits, "PMC", "7096989");

// System.out.println(results);

Expand Down

0 comments on commit 215083f

Please sign in to comment.