Search in sources :

Example 1 with PubmedLookupException

use of uk.ac.ebi.spot.goci.service.exception.PubmedLookupException in project goci by EBISPOT.

the class DefaultPubMedSearchService method findPublicationSummary.

public Study findPublicationSummary(String pubmedId) throws PubmedLookupException {
    String summaryString;
    if (pubmedRoot != null && pubmedGwasSummary != null) {
        summaryString = pubmedRoot.concat(pubmedGwasSummary);
    } else {
        throw new PubmedLookupException("Unable to search pubmed - no URL configured. " + "Set pubmed.root, pubmed.gwas.summary and pubmed.xml.version in your config!");
    }
    Document response = null;
    // Run query and create study object
    try {
        response = dispatchSearch(summaryString.replace("{idlist}", pubmedId) + xmlVersion);
        NodeList docSumNodes = response.getElementsByTagName("DocumentSummary");
        // The document summary is present then we should have a publication
        if (docSumNodes.getLength() > 0) {
            Study newStudy = new Study();
            // Assuming here we only have one document summary as pubmed id should correspond to one publication
            Node docSumNode = docSumNodes.item(0);
            // Initialize study attributes
            String pmid = null;
            String title = "";
            Date pubDate = null;
            String author = "";
            String publication = "";
            // Get the ID element (should only be one, take first regardless)
            Element study = (Element) docSumNode;
            pmid = study.getAttribute("uid");
            if (study.getElementsByTagName("error").item(0) != null) {
                author = null;
                title = null;
                publication = null;
                pubDate = null;
            } else {
                title = study.getElementsByTagName("Title").item(0).getTextContent();
                publication = study.getElementsByTagName("Source").item(0).getTextContent();
                author = study.getElementsByTagName("SortFirstAuthor").item(0).getTextContent();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String date = study.getElementsByTagName("SortPubDate").item(0).getTextContent();
                if (date.contains("/")) {
                    date = date.replace("/", "-");
                }
                java.util.Date studyDate = null;
                try {
                    studyDate = format.parse(date);
                } catch (ParseException e1) {
                    e1.printStackTrace();
                }
                pubDate = new Date(studyDate.getTime());
            }
            if (pmid != null) {
                if (author != null && pubDate != null && publication != null && title != null) {
                    newStudy.setAuthor(author);
                    newStudy.setPubmedId(pmid);
                    newStudy.setPublication(publication);
                    newStudy.setTitle(title);
                    newStudy.setPublicationDate(pubDate);
                }
            }
            return newStudy;
        } else {
            throw new PubmedLookupException("Couldn't find pubmed id " + pubmedId + " in PubMed");
        }
    } catch (IOException e) {
        throw new PubmedLookupException("Couldn't find pubmed id " + pubmedId + " in PubMed", e);
    }
}
Also used : Study(uk.ac.ebi.spot.goci.model.Study) PubmedLookupException(uk.ac.ebi.spot.goci.service.exception.PubmedLookupException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) Date(java.sql.Date) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

IOException (java.io.IOException)1 Date (java.sql.Date)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1 Study (uk.ac.ebi.spot.goci.model.Study)1 PubmedLookupException (uk.ac.ebi.spot.goci.service.exception.PubmedLookupException)1