Skip to content

Commit

Permalink
JSON-LD output fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lszeremeta committed Apr 6, 2021
1 parent 82b498a commit 184af4c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
7 changes: 6 additions & 1 deletion src/main/java/pl/edu/uwb/ii/sdfeater/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class File {
* @param subject Subject type
*/
void parse(Molecule molecule, SDFEater.Format format, SDFEater.Subject subject) {
StringBuilder output_str = new StringBuilder();
try {
FileInputStream fstream = new FileInputStream(filename);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
Expand Down Expand Up @@ -250,7 +251,7 @@ void parse(Molecule molecule, SDFEater.Format format, SDFEater.Subject subject)
break;
case jsonldhtml:
case jsonld:
molecule.printJSONLDMolecule(subject);
output_str.append(molecule.constructJSONLDMolecule(subject));
break;
case rdfa:
molecule.printRDFaMolecule(subject);
Expand Down Expand Up @@ -387,6 +388,8 @@ void parse(Molecule molecule, SDFEater.Format format, SDFEater.Subject subject)
jenaModel.write(System.out, "NTRIPLES");
break;
case jsonld:
output_str.setLength(output_str.length() - 2);
System.out.println(output_str);
System.out.println(" ],\n" +
" \"@context\" : {\n" +
" \"identifier\" : {\n" +
Expand Down Expand Up @@ -440,6 +443,8 @@ void parse(Molecule molecule, SDFEater.Format format, SDFEater.Subject subject)
break;
// JSON-LD with HTML
case jsonldhtml:
output_str.setLength(output_str.length() - 2);
System.out.println(output_str);
System.out.println(" ],\n" +
" \"@context\" : {\n" +
" \"identifier\" : {\n" +
Expand Down
34 changes: 16 additions & 18 deletions src/main/java/pl/edu/uwb/ii/sdfeater/Molecule.java
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,24 @@ void addToJenaModel(SDFEater.Subject subject) {
}

/**
* Print main molecule data in JSON-LD
* Construct molecule data in JSON-LD
*
* @param subject subject type
* @return Molecule data in JSON-LD
*/
void printJSONLDMolecule(SDFEater.Subject subject) {
StringBuilder constructJSONLDMolecule(SDFEater.Subject subject) {
StringBuilder output_str = new StringBuilder();

output_str.append(" {\n");
if (subject == SDFEater.Subject.iri) {
output_str.append(" \"@id\" : \"https://example.com/molecule#entity" + createID() + "\",\n");
} else if (subject == SDFEater.Subject.uuid) {
output_str.append(" \"@id\" : \"urn:uuid:" + uuid + "\",\n");
} else if (subject == SDFEater.Subject.bnode) {
output_str.append(" \"@id\" : \"_:b" + createID() + "\",\n");
}
output_str.append(" \"@type\" : \"https://schema.org/MolecularEntity\",\n");

for (Map.Entry<String, List<String>> entry : properties.entrySet()) {
String key = entry.getKey();
List<String> values = entry.getValue();
Expand Down Expand Up @@ -435,23 +446,10 @@ void printJSONLDMolecule(SDFEater.Subject subject) {
}
}

if (output_str.length() > 0) {
output_str.setLength(output_str.length() - 2);
System.out.println(" {");
if (subject == SDFEater.Subject.iri) {
System.out.println(" \"@id\" : \"https://example.com/molecule#entity" + createID() + "\",");
} else if (subject == SDFEater.Subject.uuid) {
System.out.println(" \"@id\" : \"urn:uuid:" + uuid + "\",");
} else if (subject == SDFEater.Subject.bnode) {
System.out.println(" \"@id\" : \"_:b" + createID() + "\",");
}
System.out.println(" \"@type\" : \"https://schema.org/MolecularEntity\",");

System.out.print(output_str);
System.out.println();
System.out.println(" },");
}
output_str.setLength(output_str.length() - 2);
output_str.append("\n },\n");

return output_str;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/pl/edu/uwb/ii/sdfeater/OutputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
class OutputTest {
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
private final String[] testMoleculeDataFields = {"alternateName", "description", "identifier", "inChI", "inChIKey", "iupacName", "molecularFormula", "molecularWeight", "smiles"};
private final String[] testMoleculeData = {"(+)-2-fenchanone", "A fenchone that has", "4695-62-9", "InChI=1S/C10H16O/c1-9(2)7-4-5-10(3,6-7)8(9)11/h7H,4-6H2,1-3H3/t7-,10+/m1/s1", "LHXDLQBQYFFVNW-XCBNKYQSSA-N", "(1S,4R)-fenchan-2-one", "C10H16O", "152.23340", "CC1(C)[C@@H]2CC[C@@](C)(C2)C1=O", "(-)-Epicatechin", "A catechin with", "490-46-0", "InChI=1S/C15H14O6/c16-8-4-11(18)9-6-13(20)15(21-14(9)5-8)7-1-2-10(17)12(19)3-7/h1-5,13,15-20H,6H2/t13-,15-/m1/s1", "PFTAWBLQPZVEMU-UKRRQHHQSA-N", "(2R,3R)-2-(3,4-dihydroxyphenyl)-3,4-dihydro-2H-chromene-3,5,7-triol", "C15H14O6", "290.26810", "[H][C@@]1(Oc2cc(O)cc(O)c2C[C@H]1O)c1ccc(O)c(O)c1"};
private final String[] testMoleculeData = {"(+)-2-fenchanone", "A fenchone that has", "4695-62-9", "InChI", "1S/C10H16O/c1-9(2)7-4-5-10(3,6-7)8(9)11/h7H,4-6H2,1-3H3/t7-,10+/m1/s1", "LHXDLQBQYFFVNW-XCBNKYQSSA-N", "(1S,4R)-fenchan-2-one", "C10H16O", "152.23340", "CC1(C)[C@@H]2CC[C@@](C)(C2)C1", "(-)-Epicatechin", "A catechin with", "490-46-0", "1S/C15H14O6/c16-8-4-11(18)9-6-13(20)15(21-14(9)5-8)7-1-2-10(17)12(19)3-7/h1-5,13,15-20H,6H2/t13-,15-/m1/s1", "PFTAWBLQPZVEMU-UKRRQHHQSA-N", "(2R,3R)-2-(3,4-dihydroxyphenyl)-3,4-dihydro-2H-chromene-3,5,7-triol", "C15H14O6", "290.26810", "[H][C@@]1(Oc2cc(O)cc(O)c2C[C@H]1O)c1ccc(O)c(O)c1"};
private final File file = new File(Paths.get("src", "test", "resources", "chebi_test.sdf").toFile().getAbsolutePath());
private Molecule molecule;

Expand Down Expand Up @@ -577,7 +577,7 @@ void jsonldHaveUUIDSubject() {
void jsonldHaveBNodeSubject() {
file.parse(molecule, SDFEater.Format.jsonld, SDFEater.Subject.bnode);
String out = outputStreamCaptor.toString();
String[] required = {"_:b0"};
String[] required = {"_:b"};
assertTrue(stringContainsAllValues(out, required));
}

Expand Down

0 comments on commit 184af4c

Please sign in to comment.