Skip to content

Commit

Permalink
Add --base option
Browse files Browse the repository at this point in the history
  • Loading branch information
lszeremeta committed Apr 8, 2021
1 parent bcb69d5 commit f62b083
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Running SDFEater without parameters displays help.
* `-i,--input <arg>` - input SDF file path (required)
* `-f,--format <arg>` - output format (e.g. `cypher`, `jsonld`, `cvme`, `smiles`, `inchi`) (required; full list below)
* `-s,--subject <arg>` - subject type (`iri`, `uuid`, `bnode`; `iri` by default; for all formats excluding cypher, cvme, smiles, inchi)
* `-b,--base <arg>` - molecule subject base for 'iri' subject type ('https://example.com/molecule#entity' by default)

Remember about the appropriate file path when using Docker image. Suppose you mounted your local directory `/home/user/input` under `/app/input` and the path to the SDF file you want to use in SDFEater is `/home/user/input/file.sdf`. In this case, enter the path `/app/input/file.sdf` or `input/file.sdf` as the value of the `-i` argument.

Expand Down
27 changes: 23 additions & 4 deletions src/main/java/pl/edu/uwb/ii/sdfeater/Molecule.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ class Molecule {
* Stores all properties of the chemical molecule
*/
private final Map<String, List<String>> properties = new HashMap<>();

/**
* Subject base of molecule
*/
String subjectBase = "https://example.com/molecule#entity";

private UUID uuid;

Molecule() {
Expand Down Expand Up @@ -332,7 +338,7 @@ void addToJenaModel(SDFEater.Subject subject) {
Resource me = ResourceFactory.createResource();

if (subject == SDFEater.Subject.iri) {
me = ResourceFactory.createResource("https://example.com/molecule#entity" + createID());
me = ResourceFactory.createResource(subjectBase + createID());
} else if (subject == SDFEater.Subject.uuid) {
me = ResourceFactory.createResource("urn:uuid:" + uuid);
} else if (subject == SDFEater.Subject.bnode) {
Expand Down Expand Up @@ -401,7 +407,7 @@ StringBuilder constructJSONLDMolecule(SDFEater.Subject subject) {

output_str.append(" {\n");
if (subject == SDFEater.Subject.iri) {
output_str.append(" \"@id\" : \"https://example.com/molecule#entity" + createID() + "\",\n");
output_str.append(" \"@id\" : \"" + subjectBase + createID() + "\",\n");
} else if (subject == SDFEater.Subject.uuid) {
output_str.append(" \"@id\" : \"urn:uuid:" + uuid + "\",\n");
} else if (subject == SDFEater.Subject.bnode) {
Expand Down Expand Up @@ -499,7 +505,14 @@ void printRDFaMolecule(SDFEater.Subject subject) {
if (output_str.length() > 0) {
if (subject == SDFEater.Subject.iri) {
String mID = createID();
System.out.println(" <div typeof='schema:MolecularEntity' about='https://example.com/molecule#entity" + mID + "' id='entity" + mID + "'>");
System.out.print(" <div typeof='schema:MolecularEntity' about='" + subjectBase + mID + "'");

if (subjectBase.contains("#")){
System.out.print(" id='" + subjectBase.substring(subjectBase.lastIndexOf('#') + 1) + mID + "'");
}

System.out.print(">\n");

} else if (subject == SDFEater.Subject.uuid) {
System.out.println(" <div typeof='schema:MolecularEntity' about='urn:uuid:" + uuid + "'>");
} else if (subject == SDFEater.Subject.bnode) {
Expand Down Expand Up @@ -559,7 +572,13 @@ void printMicrodataMolecule(SDFEater.Subject subject) {
if (output_str.length() > 0) {
if (subject == SDFEater.Subject.iri) {
String mID = createID();
System.out.println(" <div itemscope itemtype='http://schema.org/MolecularEntity' itemid='https://example.com/molecule#entity" + mID + "' id='entity" + mID + "'>");
System.out.print(" <div itemscope itemtype='http://schema.org/MolecularEntity' itemid='" + subjectBase + mID + "'");

if (subjectBase.contains("#")){
System.out.print(" id='" + subjectBase.substring(subjectBase.lastIndexOf('#') + 1) + mID + "'");
}

System.out.print(">\n");
} else if (subject == SDFEater.Subject.uuid) {
System.out.println(" <div itemscope itemtype='http://schema.org/MolecularEntity' itemid='urn:uuid:" + uuid + "'>");
} else if (subject == SDFEater.Subject.bnode) {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/pl/edu/uwb/ii/sdfeater/SDFEater.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public static void main(String[] args) {
Option subject = new Option("s", "subject", true, "subject type (iri, uuid, bnode; iri by default); for all formats excluding cypher, cvme, smiles, inchi");
subject.setRequired(false);
options.addOption(subject);
Option base = new Option("b", "base", true, "molecule subject base for 'iri' subject type ('" + molecule.subjectBase + "' by default)");
base.setRequired(false);
options.addOption(base);
CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
CommandLine cmd;
Expand All @@ -111,6 +114,12 @@ public static void main(String[] args) {
initializeJenaModel();
break;
}

// replace default base molecule IRI
if (cmd.getOptionValue("subject", Subject.iri.toString()).equals(Subject.iri.toString())) {
molecule.subjectBase = cmd.getOptionValue("base", molecule.subjectBase);
}

file.parse(molecule, Format.valueOf(cmd.getOptionValue("format")), Subject.valueOf(cmd.getOptionValue("subject", Subject.iri.toString())));
} catch (IllegalArgumentException e) {
System.err.println("Incorrect option selected");
Expand Down

0 comments on commit f62b083

Please sign in to comment.