use of net.idea.loom.nm.nanowiki.NanoWikiRDFReader in project ambit-mirror by ideaconsult.
the class NanoWikiRDFTest method testRead.
@Test
public void testRead() throws Exception {
NanoWikiRDFReader reader = null;
int records = 0;
try {
reader = new NanoWikiRDFReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(getNanoWikiFile()))));
while (reader.hasNext()) {
IStructureRecord record = reader.nextRecord();
Assert.assertTrue(record instanceof SubstanceRecord);
System.out.println(((SubstanceRecord) record).getPublicName());
records++;
}
} finally {
reader.close();
}
}
use of net.idea.loom.nm.nanowiki.NanoWikiRDFReader in project ambit-mirror by ideaconsult.
the class NanoWikiRDFTest method testWriteNanoWikiRDF.
@Test
public void testWriteNanoWikiRDF() throws Exception {
setUpDatabaseFromResource("ambit2/db/processors/test/empty-datasets.xml");
IDatabaseConnection c = getConnection();
IRawReader<IStructureRecord> parser = null;
try {
parser = new NanoWikiRDFReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(getNanoWikiFile()))));
write(parser, c.getConnection(), new ReferenceSubstanceUUID(), false);
} finally {
parser.close();
c.close();
}
}
use of net.idea.loom.nm.nanowiki.NanoWikiRDFReader in project ambit-mirror by ideaconsult.
the class Export2RDFTest method testNanoWiki2RDF.
@Test
public void testNanoWiki2RDF() throws Exception {
File nanowiki = NanoWikiRDFTest.getNanoWikiFile();
Assert.assertTrue(nanowiki.exists());
Request hack = new Request();
hack.setRootRef(new Reference("http://localhost/ambit2"));
SubstanceRDFReporter r = new SubstanceRDFReporter(hack, MediaType.TEXT_RDF_N3);
Model model = ModelFactory.createDefaultModel();
r.header(model, null);
r.setOutput(model);
NanoWikiRDFReader reader = null;
OutputStream writer = null;
StructureRecordValidator validator = new StructureRecordValidator() {
int idresult = 1;
int idcompound = 1;
Map<String, Integer> smileslookup = new TreeMap<String, Integer>();
@Override
public IStructureRecord validate(SubstanceRecord record, CompositionRelation rel) throws Exception {
/**
* compounds will be assigned ids if imported into database
* and resource url depends on the ids
*/
if (rel.getSecondStructure().getIdchemical() <= 0) {
String smi = rel.getSecondStructure().getSmiles();
if (smi != null) {
Integer index = smileslookup.get(smi);
if (index != null)
rel.getSecondStructure().setIdchemical(index);
else {
smileslookup.put(smi, idcompound);
rel.getSecondStructure().setIdchemical(idcompound);
idcompound++;
}
} else {
rel.getSecondStructure().setIdchemical(idcompound);
idcompound++;
}
}
return super.validate(record, rel);
}
@Override
public IStructureRecord validate(SubstanceRecord record, ProtocolApplication<Protocol, IParams, String, IParams, String> papp) throws Exception {
/*
* EffectRecords resource URL depends on th eidresult, when
* imported into DB it's automatically assigned, here it's set
* to subsequent numbers
*/
for (EffectRecord<String, IParams, String> effect : papp.getEffects()) {
if (effect.getIdresult() <= 0)
effect.setIdresult(idresult++);
}
return super.validate(record, papp);
}
};
int records = 0;
try {
reader = new NanoWikiRDFReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(nanowiki))));
while (reader.hasNext()) {
IStructureRecord record = reader.nextRecord();
Assert.assertTrue(record instanceof SubstanceRecord);
validator.validate((SubstanceRecord) record);
r.processItem((SubstanceRecord) record);
records++;
}
r.footer(model, null);
// one substance
ResIterator substances = model.listSubjectsWithProperty(RDF.type, RDFTermsSubstance.CHEBI_59999.getResource(model));
Assert.assertTrue(countResources(substances) >= 404);
// asssay
ResIterator assays = model.listSubjectsWithProperty(RDF.type, RDFTermsSubstance.BAO_0000015.getResource(model));
Assert.assertTrue(countResources(assays) >= 866);
// measure groups
ResIterator measuregroups = model.listSubjectsWithProperty(RDF.type, RDFTermsSubstance.BAO_0000040.getResource(model));
Assert.assertTrue(countResources(measuregroups) >= 86);
// protocols
ResIterator protocols = model.listSubjectsWithProperty(RDF.type, RDFTermsSubstance.OBI_0000272.getResource(model));
Assert.assertTrue(countResources(protocols) >= 65);
// endpoint
ResIterator endpoints = model.listSubjectsWithProperty(RDF.type, RDFTermsSubstance.BAO_0000179.getResource(model));
// thanks to the validator result ids are set and we have > 1 entry,
// otherwise all endpoints collapse into one
Assert.assertTrue(countResources(endpoints) >= 866);
File output = new File(System.getProperty("java.io.tmpdir") + "/" + "nanowiki_export.ttl");
System.out.println("Exported to " + output.getAbsolutePath());
writer = new FileOutputStream(output);
RDFDataMgr.write(writer, model, RDFFormat.TURTLE);
} finally {
reader.close();
if (writer != null)
writer.close();
}
}
use of net.idea.loom.nm.nanowiki.NanoWikiRDFReader in project ambit-mirror by ideaconsult.
the class NanoWikiRDFTest method testRoundtrip.
@Test
public void testRoundtrip() throws Exception {
NanoWikiRDFReader reader = null;
Request hack = new Request();
hack.setRootRef(new Reference("http://localhost/ambit2"));
SubstanceRDFReporter r = new SubstanceRDFReporter(hack, MediaType.TEXT_RDF_N3);
Model model = ModelFactory.createDefaultModel();
r.header(model, null);
r.setOutput(model);
try {
reader = new NanoWikiRDFReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(getNanoWikiFile()))));
while (reader.hasNext()) {
IStructureRecord record = reader.nextRecord();
Assert.assertTrue(record instanceof SubstanceRecord);
r.processItem((SubstanceRecord) record);
}
r.footer(model, null);
File output = new File(System.getProperty("java.io.tmpdir") + "/" + "nanordf_enm-export.ttl");
System.out.println("Exported to " + output.getAbsolutePath());
OutputStream writer = new FileOutputStream(output);
RDFDataMgr.write(writer, model, RDFFormat.TURTLE);
} finally {
if (reader != null)
reader.close();
}
}
use of net.idea.loom.nm.nanowiki.NanoWikiRDFReader in project ambit-mirror by ideaconsult.
the class Context method createParser.
protected IRawReader<IStructureRecord> createParser(InputStream stream, boolean xlsx) throws Exception {
_parsertype mode = getParserType();
InputStream in = null;
if (gzipped)
in = new GZIPInputStream(stream);
else
in = stream;
if (mode == null) {
logger_cli.log(Level.INFO, "MSG_IMPORT", new Object[] { "parser type not specified, assuming", xlsx ? "xlsx" : "xls" });
return new GenericExcelParser(in, jsonConfig, xlsx);
} else
switch(mode) {
case enmrdf:
return new ENanoMapperRDFReader(new InputStreamReader(in), "ENM3");
case nanowiki:
return new NanoWikiRDFReader(new InputStreamReader(in));
default:
if (jsonConfig == null)
throw new FileNotFoundException("JSON config file not specified!");
return new GenericExcelParser(in, jsonConfig, xlsx, prefix);
}
}
Aggregations