use of ambit2.base.data.study.StructureRecordValidator in project ambit-mirror by ideaconsult.
the class ISATABImportTest method testWriteISATAB.
@Test
public void testWriteISATAB() throws Exception {
setUpDatabase("src/test/resources/ambit2/db/processors/test/empty-datasets.xml");
IDatabaseConnection c = getConnection();
IRawReader<IStructureRecord> parser = null;
try {
LiteratureEntry entry = new LiteratureEntry("ISA-TAB", "test");
entry.setType(_type.Dataset);
String root = "C://ToxBank//ISAcreator.SEURAT-v1.7.2//ISAcreator.SEURAT";
// String investigation = root + "//isatab files//BII-I-1";
String investigation = root + "//isatab files//qHTSexample";
ISAConfigurationSet.setConfigPath(root + "//Configurations/toxbank-config");
parser = new ISAReader(new File(investigation));
validator = new StructureRecordValidator(investigation, true);
write(parser, c.getConnection(), new ReferenceSubstanceUUID(), false);
} finally {
parser.close();
c.close();
}
}
use of ambit2.base.data.study.StructureRecordValidator in project ambit-mirror by ideaconsult.
the class Context method validate.
protected void validate(StructureRecordValidator validator, IStructureRecord record) throws Exception {
if (validator != null) {
if (record instanceof SubstanceRecord) {
_parsertype mode = getParserType();
SubstanceRecord srecord = (SubstanceRecord) record;
if (mode == null) {
cleanReferenceStructure(srecord);
List<ProtocolApplication> m = srecord.getMeasurements();
cleanupEmptyRecords(srecord, m);
validator.process((IStructureRecord) record);
} else
switch(mode) {
case i5z:
{
if (validator != null)
validator.process((IStructureRecord) record);
break;
}
case i6z:
{
if (validator != null)
validator.process((IStructureRecord) record);
break;
}
case xlsx:
{
cleanReferenceStructure(srecord);
List<ProtocolApplication> m = srecord.getMeasurements();
cleanupEmptyRecords(srecord, m);
validator.process((IStructureRecord) record);
break;
}
case xls:
{
cleanReferenceStructure(srecord);
List<ProtocolApplication> m = srecord.getMeasurements();
cleanupEmptyRecords(srecord, m);
validator.process((IStructureRecord) record);
break;
}
case nanowiki:
{
validator.process((IStructureRecord) record);
break;
}
default:
{
cleanReferenceStructure(srecord);
List<ProtocolApplication> m = srecord.getMeasurements();
cleanupEmptyRecords(srecord, m);
validator.process((IStructureRecord) record);
break;
}
}
}
}
}
use of ambit2.base.data.study.StructureRecordValidator in project ambit-mirror by ideaconsult.
the class Context method importFile.
protected int importFile(boolean splitRecord, final boolean xlsx, boolean importBundles) throws Exception {
IRawReader<IStructureRecord> parser = null;
Connection c = null;
try (FileInputStream fin = new FileInputStream(inputFile)) {
parser = createParser(fin, xlsx);
logger_cli.log(Level.INFO, "MSG_IMPORT", new Object[] { parser.getClass().getName(), inputFile.getAbsolutePath() });
StructureRecordValidator validator = createValidator(xlsx);
IProcessor<IStructureRecord, IStructureRecord> mapper = createMapper(xlsx);
DBConnectionConfigurable<Context> dbc = null;
dbc = getConnection(getConfigFile());
c = dbc.getConnection();
c.setAutoCommit(true);
return write(parser, c, matchByKey, splitRecord, clearMeasurements, clearComposition, validator, mapper, false, importBundles);
} catch (Exception x) {
throw x;
} finally {
if (parser != null)
parser.close();
try {
c.close();
} catch (Exception x) {
}
}
}
use of ambit2.base.data.study.StructureRecordValidator 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 ambit2.base.data.study.StructureRecordValidator in project ambit-mirror by ideaconsult.
the class Context method write.
public int write(IRawReader<IStructureRecord> reader, Connection connection, IStructureKey key, boolean splitRecord, boolean clearMeasurements, boolean clearComposition, StructureRecordValidator validator, IProcessor<IStructureRecord, IStructureRecord> mapper, boolean i5mode, boolean importBundles) throws Exception {
DBSubstanceWriter writer = new DBSubstanceWriter(DBSubstanceWriter.datasetMeta(), new SubstanceRecord(), clearMeasurements, clearComposition, key);
writer.setImportBundles(importBundles);
writer.setI5mode(i5mode);
writer.setSplitRecord(splitRecord);
writer.setConnection(connection);
writer.setClearComposition(clearComposition);
writer.setClearMeasurements(clearMeasurements);
writer.setKeepEffectRecordsForDocUUID(keepEffectRecords);
writer.open();
int records = 0;
try {
while (reader.hasNext()) {
Object record = reader.next();
if (record == null)
continue;
if (record instanceof SubstanceRecord)
configure(writer, (SubstanceRecord) record);
if (mapper != null)
record = map(mapper, (IStructureRecord) record);
validate(validator, (IStructureRecord) record);
writer.process((IStructureRecord) record);
records++;
}
} catch (Exception x) {
x.printStackTrace();
} finally {
writer.close();
logger_cli.log(Level.INFO, "MSG_IMPORTED", new Object[] { records });
}
return records;
}
Aggregations