Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update publication info graph with publication date using PubMed Id #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
</build>

<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.nanopub</groupId>
<artifactId>nanopub</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,25 @@
*/
package nl.unimaas.bigcat.wikipathways.nanopubs;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.simple.parser.ParseException;

import org.nanopub.MalformedNanopubException;
import org.nanopub.Nanopub;
import org.nanopub.NanopubCreator;
import org.nanopub.NanopubImpl;
import org.nanopub.NanopubUtils;
import org.nanopub.trusty.MakeTrustyNanopub;
import org.openrdf.model.Resource;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.ValueFactory;
import org.openrdf.model.vocabulary.RDF;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.RepositoryResult;
Expand All @@ -57,6 +63,7 @@ protected static void createNanoPublications(String topic)
OPSWPRDFFiles.createNanopublications(data, "constructs/" + topic + ".insert");
SailRepositoryConnection conn = data.getConnection();
RepositoryResult<Resource> result = conn.getContextIDs();

StringBuffer buffer = new StringBuffer();
int pubCount = 0;
while (result.hasNext()) {
Expand Down Expand Up @@ -92,8 +99,9 @@ protected static void createNanoPublications(String topic)
namespaces.put("pmid", "http://identifiers.org/pubmed/");
namespaces.put("obo", "http://purl.obolibrary.org/obo/");
namespaces.put("pav", "http://purl.org/pav/>");
Nanopub nanopub = new NanopubImpl(data, (URI)nanopubId, prefixes, namespaces);
nanopub = MakeTrustyNanopub.transform(nanopub);
NanopubImpl nanopubImpl = new NanopubImpl(data, (URI)nanopubId, prefixes, namespaces);
Nanopub np = updateNanopublication(nanopubImpl, conn);
Nanopub nanopub = MakeTrustyNanopub.transform(np);
buffer.append(NanopubUtils.writeToString(nanopub, RDFFormat.TRIG)).append("\n\n");
}
conn.close();
Expand All @@ -104,4 +112,80 @@ protected static void createNanoPublications(String topic)
ResourceHelper.saveToFile(topic + "." + subsetPrefix + ".trig", buffer.toString());
System.out.println("Number of saved nanopubs: " + pubCount);
}

private static Nanopub updateNanopublication(NanopubImpl nanopub, SailRepositoryConnection conn) {
NanopubCreator npCreator = new NanopubCreator();
npCreator.setNanopubUri(nanopub.getUri());
ValueFactory factory = conn.getValueFactory();

// Add Namespaces
for(String prefix : nanopub.getNsPrefixes())
{
//System.out.println(prefix);
//System.out.println(np.getNamespace(prefix));
String namespace = nanopub.getNamespace(prefix);
npCreator.addNamespace(prefix, namespace);
}
npCreator.addNamespace("pav", "http://purl.org/pav/");

//Add Assertion Statements
npCreator.setAssertionUri(nanopub.getAssertionUri());
for(Statement st : nanopub.getAssertion())
{
npCreator.addAssertionStatement(st.getSubject(), st.getPredicate(), st.getObject());
}

//Add Provenance Statements
npCreator.setProvenanceUri(nanopub.getProvenanceUri());
Date pubDate = new Date();

for(Statement st : nanopub.getProvenance())
{
if(st.getObject().stringValue().startsWith("http://identifiers.org/pubmed/"))
{
try {
pubDate = PubMed.getPublicationDate(st.getObject().stringValue().split("/pubmed/")[1]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
npCreator.addProvenanceStatement(st.getSubject(), st.getPredicate(), st.getObject());
}

//Add PubInfo Statements
npCreator.setPubinfoUri(nanopub.getPubinfoUri());
Resource sub = null;
for(Statement st : nanopub.getPubinfo())
{
if(st.getSubject().equals(nanopub.getUri()))
sub = st.getSubject();
//sub = st.getSubject();
if(st.getPredicate().stringValue().equals("http://purl.org/dc/terms/created"))
{
npCreator.addPubinfoStatement(st.getSubject(), factory.createURI("http://purl.org/pav/","createdOn"), st.getObject());
}
else
{
npCreator.addPubinfoStatement(st.getSubject(), st.getPredicate(), st.getObject());
}
}

npCreator.addPubinfoStatement(sub, factory.createURI("http://purl.org/pav/","authoredOn"), factory.createLiteral(pubDate));

///////////////////////////////////////////////////////////////
npCreator.setNanopubUri(nanopub.getUri());

try {
return npCreator.finalizeNanopub();
} catch (MalformedNanopubException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}
}
96 changes: 96 additions & 0 deletions src/main/java/nl/unimaas/bigcat/wikipathways/nanopubs/PubMed.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package nl.unimaas.bigcat.wikipathways.nanopubs;
ImranAsif48 marked this conversation as resolved.
Show resolved Hide resolved

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class PubMed {
public static Date getPublicationDate(String pubmedId) throws IOException, ParseException {
if(isNumeric(pubmedId))
{
String postUrl = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id="+pubmedId+"&retmode=json"; //post url
ImranAsif48 marked this conversation as resolved.
Show resolved Hide resolved
//URLConnection conn = null;

// URL url = new URL(postUrl);
ImranAsif48 marked this conversation as resolved.
Show resolved Hide resolved
// conn = url.openConnection();
// conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");

URL url = new URL(postUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
int responsecode = conn.getResponseCode();
if(responsecode != 200)
{
System.out.println(pubmedId);
throw new RuntimeException("HttpResponseCode: " +responsecode);
}
else
{
InputStreamReader inputStreamReader = new InputStreamReader(conn.getInputStream());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
JSONParser parse = new JSONParser();
JSONObject jobj = (JSONObject)parse.parse(bufferedReader);
JSONObject resultJson = (JSONObject) jobj.get("result");
JSONObject pubmedJson = (JSONObject) resultJson.get(pubmedId);
String jsonDate = "";

conn.disconnect();

if (!((String)pubmedJson.get("epubdate")).equals(""))
ImranAsif48 marked this conversation as resolved.
Show resolved Hide resolved
jsonDate = (String)pubmedJson.get("epubdate");
else if(!((String)pubmedJson.get("pubdate")).equals(""))
jsonDate = (String)pubmedJson.get("pubdate");

String[] splitDate = jsonDate.split("-");
String date = "";
if(splitDate[0].split(" ").length == 3)
date = splitDate[0];
else if(splitDate[0].split(" ").length == 2)
date = splitDate[0] + " 01";
else
date = splitDate[0] + " Jan 01";

SimpleDateFormat format = new SimpleDateFormat("yyyy MMM dd");
String dateString = format.format( new Date());
//System.out.println(dateString);
try {
Date pubDate = format.parse ( date );
format = new SimpleDateFormat("yyyy-MM-dd");

return pubDate;
} catch (java.text.ParseException e) {
System.out.println(pubmedId + " " + jsonDate);
System.out.println(e);
}
}
}

return null;
}

public static boolean isNumeric(final String str) {

if (str == null || str.length() == 0) {
return false;
}

try {

Integer.parseInt(str);
return true;

} catch (NumberFormatException e) {
return false;
}

}
}