Java Code Examples for org.semanticweb.owlapi.model.OWLLiteral#getLiteral()

The following examples show how to use org.semanticweb.owlapi.model.OWLLiteral#getLiteral() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpeciesMergeUtil.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private OWLAxiom tr(OWLClass c, OWLAnnotationAssertionAxiom ax) {
	OWLAnnotationProperty p = ax.getProperty();
	if (!ecmap.containsKey(c)) {
		// preserve as-is, exception for labels
		if (p.isLabel()) {
			OWLLiteral lit = (OWLLiteral) ax.getValue();
			String newVal = lit.getLiteral() + " (" + suffix + ")";
			return fac.getOWLAnnotationAssertionAxiom(ax.getProperty(),
					ax.getSubject(), fac.getOWLLiteral(newVal));
		}
		return ax;

	} else {
		// the class is merged - ditch its axioms
		// (in future some may be preserved - syns?)
		// fac.getOWLAnnotationAssertionAxiom(ax.getProperty(), ecmap,
		// ax.getValue());
		return null;
	}
}
 
Example 2
Source File: OwlApiUtils.java    From SciGraph with Apache License 2.0 6 votes vote down vote up
/*** 
 * @param literal An OWLLiteral
 * @return an optional correctly typed Java object from the OWLLiteral
 */
public static Optional<Object> getTypedLiteralValue(OWLLiteral literal) {
  Object literalValue = null;
  if (literal.isBoolean()) {
    literalValue = literal.parseBoolean();
  } else if (literal.isInteger()) {
    literalValue = literal.parseInteger();
  } else if (literal.isFloat()) {
    literalValue = literal.parseFloat();
  } else if (literal.isDouble()) {
    literalValue = literal.parseDouble();
  } else {
    // This needs to be addressed  by #110
    if (literal.hasLang() && !literal.getLang().startsWith("en")) {
      if (!unknownLanguages.contains(literal.getLang())) {
        unknownLanguages.add(literal.getLang());
        logger.warning("Ignoring *all* literals with unsupported language: \"" + literal.getLang() + "\".");
      }
      return Optional.empty();
    }
    literalValue = literal.getLiteral();
  }
  return Optional.of(literalValue);
}
 
Example 3
Source File: ChEBIParser.java    From act with GNU General Public License v3.0 5 votes vote down vote up
private static String labelFor(OWLClass clazz, OWLOntology ontology) {
  // LabelExtractor is not available in the version of OWLAPI
  // Instead: Use the getAnnotations like we do in getData
  System.out.println("This is unverified. ChEBI parser:");
  System.out.println("LabelExtractor is not available in this OWLAPI v");
  System.out.println("So we extract the label manually. But not sure");
  System.out.println("if this code is correct. Need to check. Pause...");
  System.console().readLine();

  String chebiID = clazz.getIRI().getFragment();
  String label = null;
  for (OWLAnnotation a : clazz.getAnnotations(ontology)) {
    // We got the "if code" below from
    // http://grepcode.com/file/repo1.maven.org/maven2/net.sourceforge.owlapi/owlapi-contract/3.4/uk/ac/manchester/owl/owlapi/tutorial/LabelExtractor.java
    if (a.getProperty().isLabel()) {
      OWLLiteral c = (OWLLiteral) a.getValue();
      label = c.getLiteral();
    }
  }
  // OLD code using LabelExtractor:
  // LabelExtractor le = new LabelExtractor();
  // Set<OWLAnnotation> annotations = clazz.getAnnotations(ontology);
  // for (OWLAnnotation anno : annotations) {
  //   anno.accept(le);
  // }
  // label = le.getResult();

  /* Print out the label if there is one. Else ID */
  if (label != null) {
    return chebiID + "(" + label + ")";
  } else {
    return chebiID;
  }
}
 
Example 4
Source File: OWLGraphWrapper.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Set<ISynonym> getOBOSynonyms(OWLEntity e, Obo2OWLVocabulary vocabulary, OWLOntology ont) {
	OWLAnnotationProperty synonymProperty = getDataFactory().getOWLAnnotationProperty(vocabulary.getIRI());
	Set<OWLAnnotation> anns = OwlHelper.getAnnotations(e, synonymProperty, ont);
	Set<OWLAnnotationAssertionAxiom> annotationAssertionAxioms = ont.getAnnotationAssertionAxioms(e.getIRI());
	if (anns != null && !anns.isEmpty()) {
		Set<ISynonym> set = new HashSet<ISynonym>();
		for (OWLAnnotation a : anns) {
			if (a.getValue() instanceof OWLLiteral) {
				OWLLiteral val = (OWLLiteral) a.getValue();
				String label = val.getLiteral();
				if (label != null && label.length() > 0) {
					String category = null;
					Set<String> xrefs = null;
					SynonymDetails details = getOBOSynonymDetails(annotationAssertionAxioms, val, synonymProperty);
					if (details != null) {
						category = details.category;
						xrefs = details.xrefs;
					}
					Synonym s = new Synonym(label, vocabulary.getMappedTag(), category, xrefs);
					set.add(s);
				}
			}
		}
		if (!set.isEmpty()) {
			return set;
		}
	}
	return null;
}
 
Example 5
Source File: OWLConverter.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Convenience method to get the first string literal of an
 * annotation property.
 *
 * @param ontology the current ontology
 * @param taxon the subject
 * @param property the property
 * @return null or the label content
 */
protected static String getFirstLiteral(OWLOntology ontology,
		OWLEntity taxon, OWLAnnotationProperty property) {
	Set<OWLAnnotationAssertionAxiom> axioms = ontology.getAnnotationAssertionAxioms(taxon.getIRI());
	for (OWLAnnotationAssertionAxiom axiom : axioms) {
		if (property.equals(axiom.getProperty())) {
			OWLAnnotationValue value = axiom.getValue();
			if (value instanceof OWLLiteral) {
				OWLLiteral literal = (OWLLiteral)value;
				return literal.getLiteral();
			}
		}
	}
	return null;
}
 
Example 6
Source File: OntologyLoader.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Set<String> getDatabaseIds(OWLClass entity) {
  Set<String> dbAnnotations = new HashSet<>();
  for (OWLAnnotation annotation : entity.getAnnotations(ontology)) {
    if (annotation.getValue() instanceof OWLLiteral) {
      OWLLiteral val = (OWLLiteral) annotation.getValue();
      String value = val.getLiteral();
      if (value.matches(DB_ID_PATTERN)) {
        dbAnnotations.add(value);
      }
    }
  }
  return dbAnnotations;
}
 
Example 7
Source File: OntologyLoader.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
public String getOntologyLabel() {
  OWLAnnotationProperty labelProperty =
      factory.getOWLAnnotationProperty(OWLRDFVocabulary.RDFS_LABEL.getIRI());
  String ontologyLabel = StringUtils.EMPTY;
  for (OWLAnnotation annotation : ontology.getAnnotations()) {
    if (annotation.getProperty().equals(labelProperty)
        && annotation.getValue() instanceof OWLLiteral) {
      OWLLiteral val = (OWLLiteral) annotation.getValue();
      ontologyLabel = val.getLiteral();
    }
  }
  return ontologyLabel;
}
 
Example 8
Source File: OntologyMetadataMarkdownWriter.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static String renderMarkdown(OWLGraphWrapper g, String baseDir, boolean isIncludeImage) {

		OWLOntology ont = g.getSourceOntology();
		StringBuilder out = new StringBuilder();

		String ontId = g.getOntologyId();

		Set<OWLAnnotation> oAnns = ont.getAnnotations();
		System.err.println("NUM1:"+oAnns.size());

		String title = getVal("title", oAnns);

		out.append("## Ontology: "+title+"\n\n");
		out.append("IRI: "+rurl(ont)+"\n\n");

		String desc = getVal("description", oAnns);
		out.append("### Description\n\n");
		out.append(desc+"\n\n");

		Set<OWLOntology> imports = ont.getImports();
		if (imports.size() > 0) {
			out.append("### Imports\n\n");
			for (OWLOntology im : imports) {
				out.append(" * "+rurl(im)+"\n");
			}
		}
		if (isIncludeImage) {
			String imgFn = baseDir + "/" + ontId + ".png";
			out.append("![]("+imgFn+")\n\n");
		}

		System.err.println("NUM:"+oAnns.size());
		if (oAnns.size() > 0) {
			out.append("### Annotations:\n\n");
			for (OWLAnnotation ann : oAnns) {
				String annLabel = g.getLabelOrDisplayId(ann.getProperty());
				OWLAnnotationValue v = ann.getValue();
				String dv = v.toString();
				if (v instanceof OWLLiteral) {
					OWLLiteral lv = ((OWLLiteral)v);
					dv = lv.getLiteral();
					IRI dt = lv.getDatatype().getIRI();
					//System.out.println("DT = "+dt);
					if (dt.equals(OWL2Datatype.XSD_ANY_URI.getIRI())) {
						dv = href(lv.getLiteral());
					}
				}
				out.append(" * "+href(ann.getProperty().getIRI().toString(),annLabel)+" : "+dv+"\n");
			}
		}

		return out.toString();
	}
 
Example 9
Source File: OWLGraphWrapperExtended.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public IRI getIRIByIdentifier(String id, boolean isAutoResolve) {
	if (isAutoResolve) {
		OWLObject obj = this.getObjectByAltId(id);
		if (obj != null) {
			return ((OWLNamedObject) obj).getIRI();
		}
	}

	// special magic for finding IRIs from a non-standard identifier
	// This is the case for relations (OWLObject properties) with a short hand
	// or for relations with a non identifiers with-out a colon, e.g. negative_regulation
	// we first collect all candidate matching properties in candIRISet.
	Set<IRI> candIRISet = Sets.newHashSet();
	if (!id.contains(":")) {
		final OWLAnnotationProperty shortHand = getDataFactory().getOWLAnnotationProperty(Obo2OWLVocabulary.IRI_OIO_shorthand.getIRI());
		final OWLAnnotationProperty oboIdInOwl = getDataFactory().getOWLAnnotationProperty(Obo2Owl.trTagToIRI(OboFormatTag.TAG_ID.getTag()));
		for (OWLOntology o : getAllOntologies()) {
			for(OWLObjectProperty p : o.getObjectPropertiesInSignature()) {
				// check for short hand or obo ID in owl
				Set<OWLAnnotation> annotations = OwlHelper.getAnnotations(p, o);
				if (annotations != null) {
					for (OWLAnnotation owlAnnotation : annotations) {
						OWLAnnotationProperty property = owlAnnotation.getProperty();
						if ((shortHand != null && shortHand.equals(property)) 
								|| (oboIdInOwl != null && oboIdInOwl.equals(property)))
						{
							OWLAnnotationValue value = owlAnnotation.getValue();
							if (value != null && value instanceof OWLLiteral) {
								OWLLiteral literal = (OWLLiteral) value;
								String shortHandLabel = literal.getLiteral();
								if (id.equals(shortHandLabel)) {
									candIRISet.add(p.getIRI());
								}
							}
						}
					}
				}
			}
		}
	}

	// In the case where we find multiple candidate IRIs, we give priorities for IRIs from BFO or RO ontologies.
	IRI returnIRI = null;
	for (IRI iri: candIRISet) {
		String iriStr = iri.toString();
		if (iriStr.contains("BFO") || iriStr.contains("RO")) {
			returnIRI = iri;
		}
	}

	// If we were not able to find RO/BFO candidate IRIs for id
	if (returnIRI == null) {
		// We return it only if we have only one candidate. 
		if (candIRISet.size() == 1)
			return new ArrayList<IRI>(candIRISet).get(0);
		// This is the unexpected case. Multiple non-RO/BPO properties are mapped to given id and it's not clear what to return.
		else if (candIRISet.size() > 1)
			throw new RuntimeException("Multiple candidate IRIs are found for id: " +  id + ". None of them are from BFO or RO.");
	}
	// If we were able to find the property from RO/BFO, just return it. 
	else {
		return returnIRI;
	}

	// otherwise use the obo2owl method
	Obo2Owl b = new Obo2Owl(getManager()); // re-use manager, creating a new one can be expensive as this is a highly used code path
	b.setObodoc(new OBODoc());
	return b.oboIdToIRI(id);
}
 
Example 10
Source File: LegoMetadata.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public String visit(OWLLiteral literal) {
	return literal.getLiteral();
}