Skip to content

Commit

Permalink
Merge pull request #1154 from pk-mitre/robot-header
Browse files Browse the repository at this point in the history
Robot header
  • Loading branch information
jamesaoverton committed Mar 29, 2024
2 parents a26de24 + 11d9aea commit 7173c55
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
-- Added option to input template strings from external file [#1152]

### Fixed
- '--annotate-with-source true' does not work with extract --method subset [#1160]
- Fix how Template adds entities to the QuotedEntityChecker [#1104]
Expand Down Expand Up @@ -532,3 +535,4 @@ First official release of ROBOT!
[#246]: https://github.com/ontodev/robot/issues/246
[#174]: https://github.com/ontodev/robot/issues/174
[#158]: https://github.com/ontodev/robot/issues/158
[#1152]: https://github.com/ontodev/robot/issues/1152
55 changes: 55 additions & 0 deletions docs/examples/animals_ext_template.owl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.w3.org/2002/07/owl#"
xml:base="http://www.w3.org/2002/07/owl"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<Ontology/>



<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Classes
//
///////////////////////////////////////////////////////////////////////////////////////
-->




<!-- http://purl.obolibrary.org/obo/0000001 -->

<Class rdf:about="http://purl.obolibrary.org/obo/0000001">
<rdfs:comment>Any animal in the world.</rdfs:comment>
<rdfs:label>animal</rdfs:label>
</Class>



<!-- http://purl.obolibrary.org/obo/0000002 -->

<Class rdf:about="http://purl.obolibrary.org/obo/0000002">
<rdfs:subClassOf rdf:resource="http://purl.obolibrary.org/obo/0000001"/>
<rdfs:comment>A member of the genus Canis.</rdfs:comment>
<rdfs:label>canine</rdfs:label>
</Class>



<!-- http://purl.obolibrary.org/obo/0000003 -->

<Class rdf:about="http://purl.obolibrary.org/obo/0000003">
<rdfs:subClassOf rdf:resource="http://purl.obolibrary.org/obo/0000001"/>
<rdfs:comment>A member of the genus Felis.</rdfs:comment>
<rdfs:label>feline</rdfs:label>
</Class>
</rdf:RDF>



<!-- Generated by the OWL API (version 4.5.25) https://github.com/owlcs/owlapi -->

2 changes: 2 additions & 0 deletions docs/examples/animals_ext_template.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CURIE Label Parent Comment
ID LABEL SC % A rdfs:comment
4 changes: 4 additions & 0 deletions docs/examples/animals_ext_template_error.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CURIE Label Parent Comment
obo:0000001 animal Any animal in the world.
0000002 canine animal A member of the genus Canis.
obo:0000003 feline animal A member of the genus Felis.
4 changes: 4 additions & 0 deletions docs/examples/animals_template.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CURIE Label Parent Comment
obo:0000001 animal Any animal in the world.
obo:0000002 canine animal A member of the genus Canis.
obo:0000003 feline animal A member of the genus Felis.
5 changes: 5 additions & 0 deletions docs/examples/animals_template_error.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CURIE Label Parent Comment
ID LABEL SC % A rdfs:comment
obo:0000001 animal Any animal in the world.
0000002 canine animal A member of the genus Canis.
obo:0000003 feline animal A member of the genus Felis.
20 changes: 18 additions & 2 deletions docs/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ If the `TYPE` is a defined class, `owl:Individual`, or `owl:NamedIndividual`, an

#### Example of Individual Template Strings

| Label | Entity Type | Individual Role | Property Assertions | Different Individuals |
| Label | Entity Type | Individual Role | Property Assertions | Different Individuals |
| ------------ | ----------- | -------------------- | ------------------- | --------------------- |
| LABEL | TYPE | TI 'has role' some % | I part_of | DI % |
| Individual 1 | Class 1 | Role Class 1 | Individual 2 | |
Expand Down Expand Up @@ -223,6 +223,21 @@ robot template --merge-before \
--output results/test_template.owl
```

ROBOT template data read from separate external file

robot template --template animals_template.tsv \
--external-template animals_ext_template.tsv \
--output results/animals_ext_template.owl

Adjusted line numbers for error reporting for template data read from separate external file
<!-- DO NOT TEST -->
```
robot template --template animals_ext_template_error.tsv \
--ext-template animals_ext_template.tsv \
--output results/animals_ext_template.owl
```


\* NOTE: the imports would be merged into the output if `--collapse-import-closure true` is included instead.

Further examples can be found [in the OBI repository](https://github.com/obi-ontology/obi/tree/master/src/ontology/templates)
Expand Down Expand Up @@ -313,7 +328,7 @@ AL rdfs:label@en

The provided value cannot be parsed and may not be in proper Manchester syntax. See [Manchester Syntax](http://www.w3.org/2007/OWL/wiki/ManchesterSyntax) for more details. If you are using labels, make sure the labels are defined in the `--input` ontology or using the `LABEL` column. Also ensure that all properties use a label instead of a CURIE or IRI.

When using a restriction (`some`, `only`, `min`, `max`, `exactly`, or `value`) the term that preceeds the restriction must be a property.
When using a restriction (`some`, `only`, `min`, `max`, `exactly`, or `value`) the term that preceeds the restriction must be a property.

Terms joined using `and` or `or` must be of the same entity type, e.g., you cannot join an object property and a class in an expression.

Expand Down Expand Up @@ -366,3 +381,4 @@ An invalid `CHARACTERISTIC` value was passed. If you are providing multiple char
### Unknown Template Error

Valid template strings are limited to the [described above](#template-strings). If a different template string is provided, this error message will be returned.

55 changes: 55 additions & 0 deletions results/animals_ext_template.owl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.w3.org/2002/07/owl#"
xml:base="http://www.w3.org/2002/07/owl"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<Ontology/>



<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Classes
//
///////////////////////////////////////////////////////////////////////////////////////
-->




<!-- http://purl.obolibrary.org/obo/0000001 -->

<Class rdf:about="http://purl.obolibrary.org/obo/0000001">
<rdfs:comment>Any animal in the world.</rdfs:comment>
<rdfs:label>animal</rdfs:label>
</Class>



<!-- http://purl.obolibrary.org/obo/0000002 -->

<Class rdf:about="http://purl.obolibrary.org/obo/0000002">
<rdfs:subClassOf rdf:resource="http://purl.obolibrary.org/obo/0000001"/>
<rdfs:comment>A member of the genus Canis.</rdfs:comment>
<rdfs:label>canine</rdfs:label>
</Class>



<!-- http://purl.obolibrary.org/obo/0000003 -->

<Class rdf:about="http://purl.obolibrary.org/obo/0000003">
<rdfs:subClassOf rdf:resource="http://purl.obolibrary.org/obo/0000001"/>
<rdfs:comment>A member of the genus Felis.</rdfs:comment>
<rdfs:label>feline</rdfs:label>
</Class>
</rdf:RDF>



<!-- Generated by the OWL API (version 4.5.26) https://github.com/owlcs/owlapi -->

Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public class TemplateCommand implements Command {
private static final String missingTemplateError =
NS + "MISSING TEMPLATE ERROR at least one template is required";

/** Error message when the header file is not provided */
private static final String missingRobotHeaderError =
NS + "MISSING ROBOT HEADER ERROR header is required";

private static final String mismatchedRobotHeaderError =
NS
+ "MISMATCHED ROBOT HEADER mismatched template file header and external template file header";

/** Store the command-line options for the command. */
private Options options;

Expand All @@ -49,6 +57,7 @@ public TemplateCommand() {
"A", "include-annotations", true, "if true, include ontology annotations from merge input");
o.addOption("f", "force", true, "if true, do not exit on error");
o.addOption("e", "errors", true, "write errors to this path (TSV or CSV)");
o.addOption("E", "external-template", true, "external robot template data file");

options = o;
}
Expand Down Expand Up @@ -143,6 +152,30 @@ public CommandState execute(CommandState state, String[] args) throws Exception
tables.put(templatePath, TemplateHelper.readTable(templatePath));
}

for (String templatePath : templatePaths) {
tables.put(templatePath, TemplateHelper.readTable(templatePath));
}
// Read the robot header line in
List<String> robotHeaderPath = CommandLineHelper.getOptionValues(line, "external-template");
if (robotHeaderPath.size() > 0) {
// For now only a single header line file is considered
List<List<String>> headerLine = new ArrayList<>();
headerLine = TemplateHelper.readTable(robotHeaderPath.get(0));
if (headerLine.size() == 0) {
throw new IllegalArgumentException(missingRobotHeaderError);
}
// Insert headerLine into all the template file data appropriately
for (String templatePath : templatePaths) {
List<List<String>> template = tables.get(templatePath);
// check that header lines match
if (checkHeaders(template.get(0), headerLine.get(0))) {
template.add(1, headerLine.get(1));
} else {
throw new IllegalArgumentException(mismatchedRobotHeaderError);
}
}
}

// Process the templates
OWLOntology outputOntology =
TemplateOperation.template(inputOntology, ioHelper, tables, templateOptions);
Expand Down Expand Up @@ -194,4 +227,23 @@ public CommandState execute(CommandState state, String[] args) throws Exception

return state;
}

/**
* Compare the headers for the template file and the external template file Return true or false
* for match and mismatch situations respectively
*
* @param templateHeader header from template file
* @param externalTemplateHeader header from external template file
* @return true for match, false for mismatch
*/
private boolean checkHeaders(List<String> templateHeader, List<String> externalTemplateHeader) {
if (templateHeader.size() == externalTemplateHeader.size()) {
int numElements = templateHeader.size();
for (int index = 0; index < numElements; index++) {
if (!templateHeader.get(index).equals(externalTemplateHeader.get(index))) return false;
}
return true;
}
return false;
}
}
7 changes: 7 additions & 0 deletions robot-core/src/main/java/org/obolibrary/robot/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,13 @@ public OWLOntology generateOutputOntology(String outputIRI, boolean force, Strin
return outputOntology;
}

public int getRowNum() {
return rowNum;
}

public void setRowNum(int rowNum) {
this.rowNum = rowNum;
}
/**
* Given a list of rows for a table, first validate the headers and template strings. Then, get
* the location of important columns (e.g. IDs and labels). Finally, add all template rows to the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public static Map<String, String> getDefaultOptions() {
Map<String, String> options = new HashMap<>();
options.put("force", "false");
options.put("errors", null);
options.put("external-template", null);
return options;
}

Expand Down Expand Up @@ -176,6 +177,10 @@ public static OWLOntology template(
List<OWLOntology> outputOntologies = new ArrayList<>();
for (Map.Entry<String, List<List<String>>> t : tables.entrySet()) {
Template template = new Template(t.getKey(), t.getValue(), intermediate, ioHelper, checker);
// sufficient to check if ext-template option has a not null value
if (options.get("ext-template") != null) {
template.setRowNum(1);
}
// Update the checker with new labels
checker = template.getChecker();
boolean force = OptionsHelper.optionIsTrue(options, "force");
Expand Down

0 comments on commit 7173c55

Please sign in to comment.