use of ambit2.rest.substance.study.Substance2BucketJsonReporter in project ambit-mirror by ideaconsult.
the class SubstanceStudyReporterTest method main.
public static void main(String[] args) {
Substance2BucketJsonReporter reporter = new Substance2BucketJsonReporter(null, null, Substance2BucketJsonReporter._JSON_MODE.substance, null, null, null);
Reader in = null;
try {
OutputStreamWriter writer;
if (args.length < 2)
writer = new OutputStreamWriter(System.out);
else
writer = new OutputStreamWriter(new FileOutputStream(new File(args[1])));
reporter.setOutput(writer);
in = new FileReader(new File(args[0]));
RawIteratingCSVReader reader = new RawIteratingCSVReader(in, CSVFormat.TDF) {
protected SubstanceRecord prevrecord = null;
protected String getRecordid(CSVRecord record) {
return record.get(0);
}
protected String getExternalId(CSVRecord record) {
return record.get(1);
}
protected String getExternalIdSystem(CSVRecord record) {
return record.get(5);
}
protected String getAssayid(CSVRecord record) {
return record.get(6);
}
protected String getSummaryActivity(CSVRecord record) {
return record.get(3);
}
protected double getActivityValue(CSVRecord record) throws NumberFormatException {
return Double.parseDouble(record.get(4));
}
protected String getOrthologgroup(CSVRecord record) {
return record.get(9);
}
protected String getGeneSymbol(CSVRecord record) {
return record.get(8);
}
protected String getSpecies(CSVRecord record) {
return record.get(7);
}
@Override
protected IStructureRecord transform(CSVRecord record) {
SubstanceRecord substance = prevrecord;
String id = getRecordid(record);
if (prevrecord == null || !id.equals(prevrecord.getSubstanceName())) {
substance = new SubstanceRecord();
substance.setContent(null);
// substance.setSubstancetype("standardized");
substance.setSubstanceUUID(I5Utils.getPrefixedUUID("PC", UUID.nameUUIDFromBytes(id.getBytes())));
substance.setSubstanceName(id);
prevrecord = substance;
}
structureRecord = substance;
String externalid = getExternalId(record);
String externaldb = getExternalIdSystem(record);
ExternalIdentifier eid = new ExternalIdentifier(externaldb, externalid);
boolean newid = true;
if (substance.getExternalids() == null)
substance.setExternalids(new ArrayList<ExternalIdentifier>());
else
for (ExternalIdentifier e : substance.getExternalids()) if (e.getSystemDesignator().equals(eid.getSystemDesignator()) && e.getSystemIdentifier().equals(eid.getSystemIdentifier())) {
newid = false;
break;
}
if (newid)
substance.getExternalids().add(eid);
String assayid = getAssayid(record);
Protocol p = new Protocol(String.format("%s", externalid));
p.setTopCategory(null);
p.setCategory(null);
// p.addGuideline(String.format("%s_AID%s", externaldb,assayid));
ProtocolApplication<Protocol, IParams, String, IParams, String> papp = new ProtocolApplication<Protocol, IParams, String, IParams, String>(p);
// papp.setDocumentUUID(I5Utils.getPrefixedUUID("PC",UUID.nameUUIDFromBytes(p.getEndpoint().getBytes())));
papp.setDocumentUUID(null);
papp.setInterpretationResult(getSummaryActivity(record));
papp.setReference(String.format("AID%s", assayid));
papp.setReferenceOwner(externaldb);
IParams params = new Params();
params.put("gene", getGeneSymbol(record));
params.put("taxid", "TaxId:" + getSpecies(record));
// params.put("OG_GENE", String.format("OG%s_%s", record.get(9), record.get(8)));
params.put("og", "OG" + getOrthologgroup(record));
params.put("ez", "entrez:" + record.get(2));
papp.setParameters(params);
try {
EffectRecord<String, IParams, String> effect = new EffectRecord<String, IParams, String>();
effect.setEndpoint("pXC50");
effect.setIdresult(1);
double value = getActivityValue(record);
effect.setLoValue(value);
effect.setUnit("nM");
papp.addEffect(effect);
} catch (Exception x) {
// x.printStackTrace();
}
substance.addMeasurement(papp);
return structureRecord;
}
};
int n = 0;
IStructureRecord prevrecord = null;
reporter.header(writer, null);
while (reader.hasNext()) {
IStructureRecord record = reader.nextRecord();
if (prevrecord != null && (prevrecord != record)) {
reporter.processItem((SubstanceRecord) prevrecord);
writer.flush();
}
n++;
prevrecord = record;
if ((n % 100000) == 0) {
System.err.println();
System.err.print(n);
} else if ((n % 10000) == 0)
System.err.print(".");
}
reporter.processItem((SubstanceRecord) prevrecord);
reporter.footer(writer, null);
writer.flush();
} catch (Exception x) {
x.printStackTrace();
} finally {
try {
in.close();
} catch (Exception x) {
}
try {
reporter.close();
} catch (Exception x) {
}
}
}
use of ambit2.rest.substance.study.Substance2BucketJsonReporter in project ambit-mirror by ideaconsult.
the class SubstanceExportResource method createJSONReporter.
@Override
protected IProcessor<Q, Representation> createJSONReporter(String filenamePrefix) {
String jsonpcallback = getParams().getFirstValue("jsonp");
if (jsonpcallback == null)
jsonpcallback = getParams().getFirstValue("callback");
String command = "results";
try {
if (Boolean.parseBoolean(getParams().getFirstValue("array").toString()))
command = null;
} catch (Exception x) {
}
ProcessorsChain chain = new ProcessorsChain<>();
chain.add(new SubstanceStudyDetailsProcessor());
getCompositionProcessors(chain);
SubstanceRecordAnnotationProcessor annotator = null;
try {
annotator = new SubstanceRecordAnnotationProcessor(new File(((AmbitFreeMarkerApplication) getApplication()).getProperties().getMapFolder()), false);
} catch (Exception x) {
Logger.getGlobal().log(Level.WARNING, x.getMessage());
annotator = null;
}
return new OutputWriterConvertor<SubstanceRecord, Q>((QueryAbstractReporter<SubstanceRecord, Q, Writer>) new Substance2BucketJsonReporter(command, chain, jsonmode, summaryMeasurement, dbTag, annotator), jsonpcallback == null ? MediaType.APPLICATION_JSON : MediaType.APPLICATION_JAVASCRIPT, filenamePrefix);
}
Aggregations