Java Code Examples for org.semanticweb.owlapi.model.OWLAnnotation#getValue()

The following examples show how to use org.semanticweb.owlapi.model.OWLAnnotation#getValue() . 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: OntologyLoader.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
private Set<String> getAnnotation(OWLEntity entity, String property) {
  Set<String> annotations = new HashSet<>();
  try {
    OWLAnnotationProperty owlAnnotationProperty =
        factory.getOWLAnnotationProperty(IRI.create(property));
    for (OWLAnnotation annotation : entity.getAnnotations(ontology, owlAnnotationProperty)) {
      if (annotation.getValue() instanceof OWLLiteral) {
        OWLLiteral val = (OWLLiteral) annotation.getValue();
        annotations.add(val.getLiteral());
      }
    }
  } catch (Exception e) {
    throw new RuntimeException("Failed to get label for OWLClass " + entity);
  }
  return annotations;
}
 
Example 2
Source File: LegoMetadata.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static String getTitle(Iterable<OWLAnnotation> annotations) {
	if (annotations != null) {
		for (OWLAnnotation annotation : annotations) {
			String propertyId = annotation.getProperty().getIRI()
					.toString();
			OWLAnnotationValue annValue = annotation.getValue();
			String value = annValue.accept(LiteralValueVisitor.INSTANCE);
			if (value != null) {
				if ("http://purl.org/dc/elements/1.1/title"
						.equals(propertyId)) {
					return value;
				}
			}
		}
	}
	return null;
}
 
Example 3
Source File: GetLabelsTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String getLabel(OWLEntity obj) throws MultiLabelException {
	String label = null;
	OWLAnnotationProperty labelProperty = ont.getOWLOntologyManager().getOWLDataFactory().getRDFSLabel();
	for (OWLAnnotation ann : OwlHelper.getAnnotations(obj, labelProperty, ont)) {
		if (ann.getProperty().isLabel()) {
			OWLAnnotationValue v = ann.getValue();
			if (v instanceof OWLLiteral) {
				if (label != null) {
					throw new MultiLabelException(obj);
				}
				label = ((OWLLiteral)v).getLiteral();
			}
		}
	}
	return label;
}
 
Example 4
Source File: OWLGraphWrapperExtended.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String getOntologyAnnotationValue(OWLOntology o, OboFormatTag tag) {
	IRI dateTagIRI = Obo2Owl.trTagToIRI(tag.getTag());
	Set<OWLAnnotation> annotations = o.getAnnotations();
	for (OWLAnnotation annotation : annotations) {
		OWLAnnotationProperty property = annotation.getProperty();
		if(dateTagIRI.equals(property.getIRI())) {
			OWLAnnotationValue value = annotation.getValue();
			if (value != null) {
				if (value instanceof IRI) {
					return ((IRI) value).toString();
				}
				else if (value instanceof OWLLiteral) {
					return ((OWLLiteral) value).getLiteral();
				}
			}
		}
	}
	return null;
}
 
Example 5
Source File: OWLGraphWrapperExtended.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * fetches the value of a single-valued annotation property for an OWLObject
 * <p>
 * TODO: provide a flag that determines behavior in the case of >1 value
 * 
 * @param c
 * @param lap
 * @return value
 */
public String getAnnotationValue(OWLObject c, OWLAnnotationProperty lap) {
	Set<OWLAnnotation>anns = new HashSet<OWLAnnotation>();
	if (c instanceof OWLEntity) {
		for (OWLOntology ont : getAllOntologies()) {
			anns.addAll(OwlHelper.getAnnotations((OWLEntity) c, lap, ont));
		}
	}
	else {
		return null;
	}
	for (OWLAnnotation a : anns) {
		if (a.getValue() instanceof OWLLiteral) {
			OWLLiteral val = (OWLLiteral) a.getValue();
			return (String) SerializationUtils.clone(val.getLiteral()); // return first - TODO - check zero or one
		}
	}

	return null;
}
 
Example 6
Source File: AxiomAnnotationTools.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Retrieve the literal values for the axiom annotations with the given
 * annotation property IRI.
 * 
 * @param iri
 * @param axiom
 * @return literal values or null
 */
public static List<String> getAxiomAnnotationValues(IRI iri, OWLAxiom axiom) {
	List<String> result = null;
	for(OWLAnnotation annotation : axiom.getAnnotations()) {
		OWLAnnotationProperty property = annotation.getProperty();
		if (property.getIRI().equals(iri)) {
			OWLAnnotationValue value = annotation.getValue();
			if (value instanceof OWLLiteral) {
				String literal = ((OWLLiteral) value).getLiteral();
				if (result == null) {
					result = Collections.singletonList(literal);
				}
				else if (result.size() == 1) {
					result = new ArrayList<String>(result);
					result.add(literal);
				}
				else {
					result.add(literal);
				}
			}
		}
	}
	return result;
}
 
Example 7
Source File: OntologyMetadataMarkdownWriter.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static Set<String> getVals(String p, Set<OWLAnnotation> oAnns) {
	Set<OWLAnnotation> rmAnns = new HashSet<OWLAnnotation>();
	Set<String> vs = new HashSet<String>();
	System.err.println(" L: "+p);
	for (OWLAnnotation ann : oAnns) {
		String ps = ann.getProperty().getIRI().toString();
		ps = ps.replaceAll(".*/", "");
		if (ps.equals(p)) {
			String v = (ann.getValue() instanceof OWLLiteral) ? ((OWLLiteral)ann.getValue()).getLiteral() : ann.getValue().toString();
			//String v = ann.getValue().toString();
			vs.add(v);
			System.err.println("  P: "+ps+"="+v);
			rmAnns.add(ann);
		}
	}
	oAnns.removeAll(rmAnns);
	return vs;
}
 
Example 8
Source File: GraphOwlVisitor.java    From SciGraph with Apache License 2.0 6 votes vote down vote up
@Override
public Void visit(OWLSubClassOfAxiom axiom) {
  long subclass = getOrCreateNode(getIri(axiom.getSubClass()));
  long superclass = getOrCreateNode(getIri(axiom.getSuperClass()));
  long relationship =
      getOrCreateRelationship(subclass, superclass, OwlRelationships.RDFS_SUBCLASS_OF);
  for (OWLAnnotation annotation : axiom.getAnnotations()) {
    // TODO: Is there a more elegant way to process these annotations?
    String property = annotation.getProperty().getIRI().toString();
    if (annotation.getValue() instanceof OWLLiteral) {
      Optional<Object> value =
          OwlApiUtils.getTypedLiteralValue((OWLLiteral) annotation.getValue());
      if (value.isPresent()) {
        graph.addRelationshipProperty(relationship, property, value.get());
      }
    }
  }
  return null;
}
 
Example 9
Source File: SimpleOntologyValues.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * .
 */
public static Annotation mobiAnnotation(OWLAnnotation owlAnno) {
    // TODO: ??? Fix this.
    if (owlAnno == null) {
        return null;
    }

    AnnotationProperty property = mobiAnnotationProperty(owlAnno.getProperty());
    OWLAnnotationValue value = owlAnno.getValue();
    if (value instanceof OWLLiteral) {
        OWLLiteral literal = (OWLLiteral) value;
        Literal simpleLiteral = mobiLiteral(literal);
        return new SimpleAnnotation(property, simpleLiteral);
    } else if (value instanceof org.semanticweb.owlapi.model.IRI) {
        org.semanticweb.owlapi.model.IRI iri = (org.semanticweb.owlapi.model.IRI) value;
        IRI simpleIri = mobiIRI(iri);
        return new SimpleAnnotation(property, simpleIri);
    } else if (value instanceof OWLAnonymousIndividual) {
        OWLAnonymousIndividual individual = (OWLAnonymousIndividual) value;
        Individual simpleIndividual = mobiIndividual(individual);
        return new SimpleAnnotation(property, simpleIndividual);
    } else {
        throw new OWLRuntimeException("Invalid annotation value");
    }
}
 
Example 10
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 11
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 12
Source File: AbstractSimPreProcessor.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public boolean isUpperLevel(OWLClass c) {
	// TODO - cache
	Set<OWLAnnotation> anns = OwlHelper.getAnnotations(c, inputOntology);
	for (OWLAnnotation ann : anns) {
		String ap = ann.getProperty().getIRI().toString();
		OWLAnnotationValue v = ann.getValue();
		if (v instanceof IRI) {
			IRI iv = (IRI)v;
			if (ap.endsWith("inSubset")) {
				// TODO - formalize this
				if (iv.toString().contains("upper_level")) {
					return true;
				}
				// this tag is used in uberon
				if (iv.toString().contains("non_informative")) {
					return true;
				}
				// hack: representation of early dev a bit problematic
				// temporary: find a way to exclude these axiomatically
				if (iv.toString().contains("early_development")) {
					return true;
				}
			}

		}
	}
	return false;
}
 
Example 13
Source File: OWLGraphWrapperExtended.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get the definition xrefs (IAO_0000115)
 * 
 * @param c
 * @return list of definition xrefs
 */
public List<String> getDefXref(OWLObject c){
	OWLAnnotationProperty lap = getDataFactory().getOWLAnnotationProperty(Obo2OWLVocabulary.IRI_IAO_0000115.getIRI()); 
	OWLAnnotationProperty xap = getAnnotationProperty(OboFormatTag.TAG_XREF.getTag());

	if (c instanceof OWLEntity) {
		List<String> list = new ArrayList<String>();
		for (OWLOntology ont : getAllOntologies()) {
			Set<OWLAnnotationAssertionAxiom> axioms = ont.getAnnotationAssertionAxioms(((OWLEntity) c).getIRI());
			for (OWLAnnotationAssertionAxiom axiom :axioms){
				if(lap.equals(axiom.getProperty())){
					for(OWLAnnotation annotation: axiom.getAnnotations(xap)){
						OWLAnnotationValue value = annotation.getValue();
						if(value instanceof OWLLiteral){
							list.add(((OWLLiteral)value).getLiteral());
						}
					}
				}

			}
		}
		return list;
	}
	else {
		return null;
	}
}
 
Example 14
Source File: OWLGraphWrapperExtended.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * gets the OBO-style ID of the specified object. e.g., "GO:0008150"
 * SerializationUtils.clone is used to avoid memory leaks.
 * 
 * @param iriId
 * @return OBO-style identifier, using obo2owl mappings or the literals extracted from oboInowl#id.
 */
public String getIdentifier(IRI iriId) {
	if (iriId.toString().startsWith(Obo2OWLConstants.DEFAULT_IRI_PREFIX))
		return (String) SerializationUtils.clone(Owl2Obo.getIdentifier(iriId));

	final OWLAnnotationProperty oboIdInOwl = getDataFactory().getOWLAnnotationProperty(Obo2Owl.trTagToIRI(OboFormatTag.TAG_ID.getTag()));
	for (OWLOntology o : getAllOntologies()) {
		Collection<OWLAnnotation> oas = EntitySearcher.getAnnotations(iriId, o);
		if (oas == null) continue;

		for (OWLAnnotation oa: oas) {
			// We specifically look for the annotation property, oboInOwl:id; ignore others.
			if (oa.getProperty().equals(oboIdInOwl) != true)
				continue;

			// We then get the object value of this property, which is supposed to be a literal.
			OWLAnnotationValue objValue = oa.getValue();
			if (objValue.isLiteral() != true) {
				LOG.warn("Odd. " + objValue + " of oboInOwl#id for "+ iriId + ", is supposed to be an literal, but it is not.");
				continue;
			}

			Optional<OWLLiteral> literalOpt = objValue.asLiteral();
			if (literalOpt.isPresent() != true) {
				LOG.warn("Odd. " + objValue + " of oboInOwl#id for "+ iriId + ", does not exist.");
				continue;
			}

			OWLLiteral literal = literalOpt.get();
			return (String) SerializationUtils.clone(literal.getLiteral());
		}
	}

	// In the case where the input class does not have oboInOwl#id, we return its original URL.
	LOG.warn("Unable to retrieve the value of oboInOwl#id as the identifier for " + iriId + "; we will use an original iri as the identifier.");
	return (String) SerializationUtils.clone(iriId.toString());
}
 
Example 15
Source File: OWLGraphWrapperEdges.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void includeAllWith(OWLAnnotationProperty ap, OWLOntology o) {
	for (OWLObjectProperty p : o.getObjectPropertiesInSignature(Imports.INCLUDED)) {
		Set<OWLAnnotation> anns = OwlHelper.getAnnotations(p, ap, o);
		for (OWLAnnotation ann : anns) {
			if (ann.getValue() instanceof OWLLiteral) {
				OWLLiteral v = (OWLLiteral) ann.getValue();
				if (v.parseBoolean()) {
					includeProperty(p);
				}
			}

		}
	}
}
 
Example 16
Source File: OldSimpleOwlSim.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private String getLabel(OWLEntity e) {
	String label = null;		
	// todo - ontology import closure
	OWLAnnotationProperty property = owlDataFactory.getRDFSLabel();
	for (OWLAnnotation ann : OwlHelper.getAnnotations(e, property, sourceOntology)) {
		OWLAnnotationValue v = ann.getValue();
		if (v instanceof OWLLiteral) {
			label = ((OWLLiteral)v).getLiteral();
			break;
		}
	}
	return label;
}
 
Example 17
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 18
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 19
Source File: GraphOwlVisitorTestBase.java    From SciGraph with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() throws Exception {
  if (builtGraph) {
    // TODO: UGH - need a better pattern for this
    return;
  }
  graph = createInstance();

  String uri = Resources.getResource("ontologies/family.owl").toURI().toString();
  manager.loadOntologyFromOntologyDocument(IRI.create(uri));

  List<MappedProperty> propertyMap = new ArrayList<>();
  MappedProperty age = mock(MappedProperty.class);
  when(age.getName()).thenReturn("isAged");
  when(age.getProperties()).thenReturn(newArrayList(ROOT + "/hasAge"));
  propertyMap.add(age);
  MappedProperty versionInfo = mock(MappedProperty.class);
  when(versionInfo.getName()).thenReturn("versionInfo");
  when(versionInfo.getProperties()).thenReturn(newArrayList(OWL + "versionInfo"));
  propertyMap.add(versionInfo);
  OWLOntologyWalker walker = new OWLOntologyWalker(manager.getOntologies());
  GraphOwlVisitor visitor = new GraphOwlVisitor(walker, graph, propertyMap);
  walker.walkStructure(visitor);
  
  for (OWLOntology ontology : manager.getOntologies()){
    String ontologyIri = OwlApiUtils.getIri(ontology);
    for (OWLAnnotation annotation : ontology.getAnnotations()) { // Get annotations on ontology iri
      OWLAnnotationSubject ontologySubject = IRI.create(ontologyIri);
      OWLAnnotationAssertionAxiom object = 
          new OWLAnnotationAssertionAxiomImpl(ontologySubject,
              annotation.getProperty(), annotation.getValue(),
              new ArrayList<OWLAnnotation>());
      visitor.visit(object);
    }
  }
  graph.shutdown();
  graphDb = new TestGraphDatabaseFactory().newEmbeddedDatabase(new File(path));
  tx = graphDb.beginTx();
  nodeIndex = graphDb.index().getNodeAutoIndexer().getAutoIndex();
  builtGraph = true;
}
 
Example 20
Source File: LegoMetadata.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @param node
 * @param axiom
 */
public static void extractMetadata(LegoMetadata node, OWLAxiom axiom) {
	// extract meta data from annotations
	Set<OWLAnnotation> annotations = axiom.getAnnotations();
	for (OWLAnnotation annotation : annotations) {
		String propertyId = annotation.getProperty().getIRI().toString();
		OWLAnnotationValue annValue = annotation.getValue();
		String value = annValue.accept(LiteralValueVisitor.INSTANCE);
		if (value != null) {
			if ("http://geneontology.org/lego/evidence".equals(propertyId)) {
				Set<String> evidence = node.getEvidence();
				if (evidence == null) {
					evidence = new HashSet<String>();
					node.setEvidence(evidence);
				}
				evidence.add(value);
			}
			else if ("http://purl.org/dc/elements/1.1/date".equals(propertyId)) {
				Set<String> dates = node.getDates();
				if (dates == null) {
					dates = new HashSet<String>();
					node.setDates(dates);
				}
				dates.add(value);
			}
			else if ("http://purl.org/dc/elements/1.1/source".equals(propertyId)) {
				Set<String> sources = node.getSources();
				if (sources == null) {
					sources = new HashSet<String>();
					node.setSources(sources);
				}
				sources.add(value);
			}
			else if ("http://purl.org/dc/elements/1.1/contributor".equals(propertyId)) {
					Set<String> contributors = node.getContributors();
					if (contributors == null) {
						contributors = new HashSet<String>();
						node.setContributors(contributors);
					}
					contributors.add(value);
			}
		}
	}
}