001package org.biopax.paxtools.io.jsonld; 002 003import java.io.File; 004import java.io.FileInputStream; 005import java.io.FileOutputStream; 006import java.io.IOException; 007import java.io.InputStream; 008import java.io.OutputStream; 009import java.text.SimpleDateFormat; 010import java.util.Calendar; 011 012import org.apache.jena.riot.Lang; 013import org.apache.jena.riot.RDFDataMgr; 014import org.biopax.paxtools.io.SimpleIOHandler; 015import org.biopax.paxtools.model.BioPAXLevel; 016import org.slf4j.Logger; 017import org.slf4j.LoggerFactory; 018 019import com.hp.hpl.jena.rdf.model.ModelFactory; 020 021public class JsonldBiopaxConverter implements JsonldConverter { 022 023 private final static Logger LOGGER = LoggerFactory.getLogger(JsonldBiopaxConverter.class); 024 025 /* 026 * Convert inputstream in owl/rdf format to outputsream in jsonld format 027 */ 028 public void convertToJsonld(InputStream in, OutputStream os) 029 throws IOException { 030 031 File inputProcessedFile = preProcessFile(in); 032 LOGGER.info("OWl File processed successfully "); 033 034 // print current time 035 SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); 036 LOGGER.info("Conversion RDF to JSONLD started " 037 + sdf.format(Calendar.getInstance().getTime())); 038 039 // create an empty model 040 com.hp.hpl.jena.rdf.model.Model modelJena = ModelFactory.createDefaultModel(); 041 InputStream internalInputStream = new FileInputStream(inputProcessedFile); 042 043 // read the RDF/XML file 044 RDFDataMgr.read(modelJena, internalInputStream, Lang.RDFXML); 045 LOGGER.info("Read into Model finished " 046 + sdf.format(Calendar.getInstance().getTime())); 047 048 RDFDataMgr.write(os, modelJena, Lang.JSONLD); 049 LOGGER.info("Conversion RDF to JSONLD finished " 050 + sdf.format(Calendar.getInstance().getTime())); 051 LOGGER.info(" JSONLD file " + " is written successfully."); 052 053 try { //close, flush quietly 054 os.close(); 055 } catch(Exception e) {} 056 057 } 058 059 060 /* 061 * Convert inputstream in jsonld format to outputsream if owl/rdf format 062 */ 063 public void convertFromJsonld(InputStream in, OutputStream out) { 064 065 com.hp.hpl.jena.rdf.model.Model modelJena = ModelFactory.createDefaultModel(); 066 067 if (in == null) { 068 throw new IllegalArgumentException("Input File: " + " not found"); 069 } 070 if (out == null) { 071 throw new IllegalArgumentException("Output File: " + " not found"); 072 } 073 074 // read the JSONLD file 075 modelJena.read(in, null, "JSONLD"); 076 077 RDFDataMgr.write(out, modelJena, Lang.RDFXML); 078 LOGGER.info(" RDF file " + " is written successfully."); 079 080 } 081 082 // Instantiate a simple (StAX based) biopax reader/writer - SimpleIOHandler 083 084 public File preProcessFile(InputStream in) throws IOException { 085 086 SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); 087 LOGGER.info("BIOPAX Conversion started " 088 + sdf.format(Calendar.getInstance().getTime())); 089 090 if (in == null) { 091 throw new IllegalArgumentException("Input File: " + " is not found"); 092 } 093 094 SimpleIOHandler simpleIO = new SimpleIOHandler(BioPAXLevel.L3); 095 096 // create a Paxtools Model from the BioPAX L3 RDF/XML input file 097 // (stream) 098 099 org.biopax.paxtools.model.Model model = simpleIO.convertFromOWL(in); 100 101 // set for the IO to output full URIs: 102 103 simpleIO.absoluteUris(true); 104 105 File fullUriBiopaxInput = File.createTempFile("biopaxTemp", "owl"); 106 107 fullUriBiopaxInput.deleteOnExit(); // delete on JVM exits 108 FileOutputStream outputStream = new FileOutputStream(fullUriBiopaxInput); 109 110 // write to an output stream (back to RDF/XML) 111 112 simpleIO.convertToOWL((org.biopax.paxtools.model.Model) model, 113 outputStream); // it closes the stream internally 114 115 model = null; 116 117 LOGGER.info("BIOPAX Conversion finished " 118 + sdf.format(Calendar.getInstance().getTime())); 119 return fullUriBiopaxInput; 120 } 121 122}