Search in sources :

Example 1 with EuropePMCData

use of uk.ac.ebi.spot.goci.utils.EuropePMCData in project goci by EBISPOT.

the class EuropepmcPubMedSearchService method createStudyByPubmed.

public EuropePMCData createStudyByPubmed(String pubmedId) throws PubmedLookupException {
    EuropePMCData europePMCData = new EuropePMCData();
    String urlRequest;
    if (europepmcRoot != null && europepmcSearch != null) {
        urlRequest = europepmcRoot.concat(europepmcSearch);
    } else {
        throw new PubmedLookupException("Unable to search pubmed - no URL configured. " + "Set europepmc properties in your config!");
    }
    String queryUrl = urlRequest.replace("{idlist}", pubmedId);
    ResponseEntity<String> out;
    RestResponseResult result = new RestResponseResult();
    RestTemplate restTemplate = new RestTemplate();
    // restTemplate.setErrorHandler(new CustomResponseErrorHandler());
    // Add the Jackson message converter
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    List<MediaType> mediaTypes = new ArrayList<MediaType>();
    mediaTypes.add(MediaType.TEXT_HTML);
    mediaTypes.add(MediaType.APPLICATION_JSON);
    mediaTypes.add(MediaType.ALL);
    headers.setAccept(mediaTypes);
    // headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON,MediaType.TEXT_HTML));
    HttpEntity<Object> entity = new HttpEntity<Object>(headers);
    getLog().debug("Querying " + queryUrl);
    // and do I need this JSON media type for my use case?
    try {
        out = restTemplate.exchange(queryUrl, HttpMethod.GET, entity, String.class);
        result.setStatus(out.getStatusCode().value());
        result.setUrl(queryUrl);
        System.out.println(queryUrl);
        JsonNode body = new JsonNode(out.getBody().toString());
        result.setRestResult(body);
    } catch (Exception e) {
        throw new PubmedLookupException("EuropePMC : REST API Failed");
    }
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addDeserializer(EuropePMCData.class, new EuropePMCDeserializer());
    mapper.registerModule(module);
    try {
        europePMCData = mapper.readValue(result.getRestResult().toString(), EuropePMCData.class);
    } catch (IOException ioe) {
        System.out.println("EuropePMC : IO Exception - JSON conversion");
        throw new PubmedLookupException("EuropePMC : IO Exception - JSON conversion");
    } catch (Exception e) {
        System.out.println("EuropePMC : Generic Error conversion JSON");
        throw new PubmedLookupException("EuropePMC : Generic Error conversion JSON");
    }
    return europePMCData;
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) PubmedLookupException(uk.ac.ebi.spot.goci.service.exception.PubmedLookupException) ArrayList(java.util.ArrayList) RestResponseResult(uk.ac.ebi.spot.goci.model.RestResponseResult) JsonNode(com.mashape.unirest.http.JsonNode) IOException(java.io.IOException) EuropePMCData(uk.ac.ebi.spot.goci.utils.EuropePMCData) PubmedLookupException(uk.ac.ebi.spot.goci.service.exception.PubmedLookupException) IOException(java.io.IOException) EuropePMCDeserializer(uk.ac.ebi.spot.goci.utils.EuropePMCDeserializer) RestTemplate(org.springframework.web.client.RestTemplate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 2 with EuropePMCData

use of uk.ac.ebi.spot.goci.utils.EuropePMCData in project goci by EBISPOT.

the class PublicationOperationsService method importSinglePublication.

public Publication importSinglePublication(String pubmedId, Boolean newImport) throws PubmedLookupException {
    Publication addedPublication = null;
    try {
        EuropePMCData europePMCResult = europepmcPubMedSearchService.createStudyByPubmed(pubmedId);
        addedPublication = addPublication(pubmedId, europePMCResult, newImport);
    } catch (Exception exception) {
        throw new PubmedLookupException(exception.getCause().getMessage());
    }
    return addedPublication;
}
Also used : PubmedLookupException(uk.ac.ebi.spot.goci.service.exception.PubmedLookupException) Publication(uk.ac.ebi.spot.goci.model.Publication) EuropePMCData(uk.ac.ebi.spot.goci.utils.EuropePMCData) PubmedLookupException(uk.ac.ebi.spot.goci.service.exception.PubmedLookupException) NoStudyDirectoryException(uk.ac.ebi.spot.goci.curation.exception.NoStudyDirectoryException)

Example 3 with EuropePMCData

use of uk.ac.ebi.spot.goci.utils.EuropePMCData in project goci by EBISPOT.

the class PublicationController method matchPublication.

@RequestMapping(value = "/match", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<Map<String, Object>> matchPublication(Model model, @RequestBody String pubmedId) {
    Map<String, Object> results = new HashMap<>();
    CosineDistance cosScore = new CosineDistance();
    LevenshteinDistance levenshteinDistance = new LevenshteinDistance();
    JaroWinklerSimilarity jwDistance = new JaroWinklerSimilarity();
    EuropePMCData europePMCResult = europepmcPubMedSearchService.createStudyByPubmed(pubmedId);
    Map<String, String> searchProps = new HashMap<>();
    List<Map<String, String>> data = new ArrayList<>();
    if (!europePMCResult.getError()) {
        try {
            searchProps.put("pubMedID", europePMCResult.getPublication().getPubmedId());
            searchProps.put("author", europePMCResult.getFirstAuthor().getFullname());
            searchProps.put("title", europePMCResult.getPublication().getTitle());
            searchProps.put("doi", europePMCResult.getDoi());
            results.put("search", searchProps);
            String searchTitle = europePMCResult.getPublication().getTitle();
            String searchAuthor = europePMCResult.getFirstAuthor().getFullname();
            CharSequence searchString = buildSearch(searchAuthor, searchTitle);
            Map<String, Submission> submissionMap = submissionService.getSubmissionsBasic();
            for (Map.Entry<String, Submission> e : submissionMap.entrySet()) {
                Map<String, String> props = new HashMap<>();
                Submission submission = e.getValue();
                String matchTitle = submission.getTitle();
                String matchAuthor = submission.getAuthor();
                CharSequence matchString = buildSearch(matchAuthor, matchTitle);
                props.put("submissionID", submission.getId());
                props.put("pubMedID", submission.getPubMedID());
                props.put("author", submission.getAuthor());
                props.put("title", submission.getTitle());
                props.put("doi", submission.getDoi());
                if (matchString.equals("")) {
                    props.put("cosScore", new Integer(0).toString());
                    props.put("levDistance", new Integer(0).toString());
                    props.put("jwScore", new Integer(0).toString());
                } else {
                    Double score = cosScore.apply(searchString, matchString) * 100;
                    Integer ldScore = levenshteinDistance.apply(searchString, matchString);
                    Double jwScore = jwDistance.apply(searchString, matchString) * 100;
                    props.put("cosScore", normalizeScore(score.intValue()).toString());
                    props.put("levDistance", normalizeScore(ldScore).toString());
                    props.put("jwScore", new Integer(jwScore.intValue()).toString());
                }
                data.add(props);
            }
            data.sort((o1, o2) -> Integer.decode(o2.get("cosScore")).compareTo(Integer.decode(o1.get("cosScore"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        results.put("error", "ID " + pubmedId + " not found");
    }
    results.put("data", data);
    model.addAttribute("baseUrl", depositionUiURL);
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.add("Content-Type", "application/json; charset=utf-8");
    return new ResponseEntity<>(results, responseHeaders, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Submission(uk.ac.ebi.spot.goci.model.deposition.Submission) LevenshteinDistance(org.apache.commons.text.similarity.LevenshteinDistance) IOException(java.io.IOException) EuropePMCData(uk.ac.ebi.spot.goci.utils.EuropePMCData) ResponseEntity(org.springframework.http.ResponseEntity) JaroWinklerSimilarity(org.apache.commons.text.similarity.JaroWinklerSimilarity) CosineDistance(org.apache.commons.text.similarity.CosineDistance)

Aggregations

EuropePMCData (uk.ac.ebi.spot.goci.utils.EuropePMCData)3 IOException (java.io.IOException)2 PubmedLookupException (uk.ac.ebi.spot.goci.service.exception.PubmedLookupException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1 JsonNode (com.mashape.unirest.http.JsonNode)1 ArrayList (java.util.ArrayList)1 CosineDistance (org.apache.commons.text.similarity.CosineDistance)1 JaroWinklerSimilarity (org.apache.commons.text.similarity.JaroWinklerSimilarity)1 LevenshteinDistance (org.apache.commons.text.similarity.LevenshteinDistance)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)1 RestTemplate (org.springframework.web.client.RestTemplate)1 NoStudyDirectoryException (uk.ac.ebi.spot.goci.curation.exception.NoStudyDirectoryException)1 Publication (uk.ac.ebi.spot.goci.model.Publication)1 RestResponseResult (uk.ac.ebi.spot.goci.model.RestResponseResult)1 Submission (uk.ac.ebi.spot.goci.model.deposition.Submission)1 EuropePMCDeserializer (uk.ac.ebi.spot.goci.utils.EuropePMCDeserializer)1