Java Code Examples for org.semanticweb.owlapi.model.OWLOntology#getAnnotationAssertionAxioms()

The following examples show how to use org.semanticweb.owlapi.model.OWLOntology#getAnnotationAssertionAxioms() . 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: Mooncat.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
void addSubAnnotationProperties(Set<OWLAxiom> axioms) {
	// add ALL subannotprop axioms
	// - this is quite geared towards obo ontologies, where
	//   we want to preserve obo headers.
	// TODO: make this configurable
	LOG.info("adding SubAnnotationProperties");
	Set<OWLAxiom> sapAxioms = new HashSet<OWLAxiom>();
	for (OWLOntology refOnt : this.getReferencedOntologies()) {
		for (OWLSubAnnotationPropertyOfAxiom a : refOnt.getAxioms(AxiomType.SUB_ANNOTATION_PROPERTY_OF)) {
			sapAxioms.add(a);
			Set<OWLAnnotationAssertionAxiom> s = refOnt.getAnnotationAssertionAxioms(a.getSubProperty().getIRI());
			if (s != null && !s.isEmpty()) {
				for (OWLAnnotationAssertionAxiom owlAnnotationAssertionAxiom : s) {
					sapAxioms.add(owlAnnotationAssertionAxiom);
				}
			}
		}
	}
	axioms.addAll(sapAxioms);
}
 
Example 2
Source File: TableToAxiomConverter.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void buildClassMap(OWLGraphWrapper g) {
	IRI x = Obo2OWLVocabulary.IRI_OIO_hasDbXref.getIRI();
	for (OWLOntology ont : g.getAllOntologies()) {
		for (OWLClass c : ont.getClassesInSignature()) {
			for (OWLAnnotationAssertionAxiom aa : ont.getAnnotationAssertionAxioms(c.getIRI())) {
				if (aa.getProperty().getIRI().equals(x)) {
					OWLAnnotationValue v = aa.getValue();
					if (v instanceof OWLLiteral) {
						String xid =((OWLLiteral)v).getLiteral();
						OWLClass xc = (OWLClass) g.getOWLObjectByIdentifier(xid);
						if (xc == null) {
							LOG.error("Cannot get class for: "+xid);
						}
						else {
							config.classMap.put(xc, c);
						}
						//LOG.info(c + " ===> "+xid);
					}
				}					
			}
		}
	}
}
 
Example 3
Source File: NCBI2OWLTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void testSpecies(OWLOntology ontology) {
	IRI iri = IRI.create("http://purl.obolibrary.org/obo/NCBITaxon_species");
	OWLDataFactory df = ontology.getOWLOntologyManager().
		getOWLDataFactory();
	OWLClass taxon = df.getOWLClass(iri);
	assertTrue("Species class in signature",
		ontology.containsClassInSignature(iri));
	
	// Check axioms
	Set<OWLClassAxiom> axioms = ontology.getAxioms(taxon, Imports.EXCLUDED);
	assertEquals("Count class axioms", 1, axioms.size());
	assertEquals("SubClassOf(<http://purl.obolibrary.org/obo/NCBITaxon_species> <http://purl.obolibrary.org/obo/NCBITaxon#_taxonomic_rank>)", axioms.toArray()[0].toString());

	// Check annotations
	List<String> values = new ArrayList<String>();
	values.add("AnnotationAssertion(<http://www.geneontology.org/formats/oboInOwl#hasOBONamespace> <http://purl.obolibrary.org/obo/NCBITaxon_species> \"ncbi_taxonomy\"^^xsd:string)");
	values.add("AnnotationAssertion(rdfs:label <http://purl.obolibrary.org/obo/NCBITaxon_species> \"species\"^^xsd:string)");

	Set<OWLAnnotationAssertionAxiom> annotations = 
		ontology.getAnnotationAssertionAxioms(iri);
	assertEquals("Count annotations for Species", 2, annotations.size());

	checkAnnotations(annotations, values);
}
 
Example 4
Source File: NCBI2OWLTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void testExactSynonym(OWLOntology ontology) {
	IRI iri = IRI.create("http://www.geneontology.org/formats/oboInOwl#hasExactSynonym");
	OWLDataFactory df = ontology.getOWLOntologyManager().
		getOWLDataFactory();
	OWLAnnotationProperty property = df.getOWLAnnotationProperty(iri);
	assertTrue("Exact Synonym property in signature",
		ontology.containsAnnotationPropertyInSignature(iri));
	
	// Check axioms
	Set<OWLAnnotationAxiom> axioms = ontology.getAxioms(property, Imports.EXCLUDED);
	assertEquals("Count class axioms", 0, axioms.size());

	// Check annotations
	List<String> values = new ArrayList<String>();
	values.add("AnnotationAssertion(rdfs:label <http://www.geneontology.org/formats/oboInOwl#hasExactSynonym> \"has_exact_synonym\"^^xsd:string)");

	Set<OWLAnnotationAssertionAxiom> annotations = 
		ontology.getAnnotationAssertionAxioms(iri);
	assertEquals("Count annotations for Exact", 1, annotations.size());

	checkAnnotations(annotations, values);
}
 
Example 5
Source File: Mooncat.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * finds the full closure of all external referenced entities.
 * TODO: include object properties
 * 
 * calls {@link #getExternalReferencedEntities()} and then finds all reflexive ancestors of this set.
 * 
 * to configure the traversal, see {@link OWLGraphWrapper}
 * 
 * @return closure of all external referenced entities
 */
public Set<OWLObject> getClosureOfExternalReferencedEntities() {
	// the closure set, to be returned
	Set<OWLObject> objs = new HashSet<OWLObject>();

	// set of entities in external ontologies referenced in source ontology
	Set<OWLEntity> refs = getExternalReferencedEntities();
	LOG.info("direct external referenced entities: "+refs.size());

	// build the closure
	for (OWLEntity ref : refs) {
		// todo - allow per-relation control
		// todo - make more efficient, allow passing of set of entities
		// todo - include object properties
		Set<OWLObject> ancs = graph.getAncestorsReflexive(ref);
		objs.addAll(ancs);
	}
	LOG.info("closure of direct external referenced entities: "+objs.size());

	// extraObjs is the set of properties (annotation and object)
	// that are in the signatures of all referencing axioms
	Set<OWLObject> extraObjs = new HashSet<OWLObject>();
	for (OWLObject obj : objs) {
		if (obj instanceof OWLEntity) {
			for (OWLOntology refOnt : this.getReferencedOntologies()) {
				for (OWLAnnotationAssertionAxiom aaa : refOnt.getAnnotationAssertionAxioms(((OWLEntity)obj).getIRI())) {
					extraObjs.add(aaa.getProperty());
					extraObjs.add(aaa.getValue());
				}
				
				for (OWLAxiom ax : refOnt.getReferencingAxioms((OWLEntity)obj)) {
					extraObjs.addAll(ax.getObjectPropertiesInSignature());
				}
			}
		}
	}
	objs.addAll(extraObjs);
	return objs;
}
 
Example 6
Source File: Mooncat.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Check, if the named object has the annotation property IAO:0000412, 
 * declaring the object as imported.
 * 
 * @param named
 * @param ontology
 * @return true if the item has the annotation property IAO:0000412
 */
public static boolean isImportMarkedEntity(OWLNamedObject named, OWLOntology ontology) {
	for (OWLAnnotationAssertionAxiom axiom : ontology.getAnnotationAssertionAxioms(named.getIRI())) {
		OWLAnnotationProperty property = axiom.getProperty();
		if (importedMarkerIRI.equals(property.getIRI())) {
			return true;
		}
	}
	return false;
}
 
Example 7
Source File: OwlHelper.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Set<OWLAnnotation> getAnnotations(OWLEntity e, OWLAnnotationProperty property, OWLOntology ont) {
	Set<OWLAnnotation> annotations;
	if (e != null && property != null && ont != null) {
		annotations = new HashSet<>();
		for (OWLAnnotationAssertionAxiom ax : ont.getAnnotationAssertionAxioms(e.getIRI())) {
			if (property.equals(ax.getProperty())) {
				annotations.add(ax.getAnnotation());
			}
		}
	}
	else {
		annotations = Collections.emptySet();
	}
	return annotations;
}
 
Example 8
Source File: OwlHelper.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Set<OWLAnnotation> getAnnotations(OWLEntity e, OWLOntology ont) {
	Set<OWLAnnotation> annotations;
	if (e != null && ont != null) {
		Set<OWLAnnotationAssertionAxiom> axioms = ont.getAnnotationAssertionAxioms(e.getIRI());
		annotations = new HashSet<>(axioms.size());
		for(OWLAnnotationAssertionAxiom ax : axioms) {
			annotations.add(ax.getAnnotation());
		}
	}
	else {
		annotations = Collections.emptySet();
	}
	return annotations;
}
 
Example 9
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 10
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 11
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 12
Source File: NCBI2OWLTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void testBacteria(OWLOntology ontology) {
	String curie = "ncbi:2";
	IRI iri = OWLConverter.format.getIRI(curie);
	OWLDataFactory df = ontology.getOWLOntologyManager().
		getOWLDataFactory();
	OWLClass taxon = df.getOWLClass(iri);
	assertTrue("Bacteria class in signature",
		ontology.containsClassInSignature(iri));

	// Check axioms
	Set<OWLClassAxiom> axioms = ontology.getAxioms(taxon, Imports.EXCLUDED);
	assertEquals("Count class axioms for Bacteria", 1, axioms.size());
	assertEquals("SubClassOf(<http://purl.obolibrary.org/obo/NCBITaxon_2> <http://purl.obolibrary.org/obo/NCBITaxon_131567>)", axioms.toArray()[0].toString());

	// Check annotations
	List<String> values = new ArrayList<String>();
	values.add(expandAnnotation(curie, "ncbitaxon:has_rank", OWLConverter.format.getIRI("ncbi:superkingdom")));
	values.add(expandAnnotation(curie, "oio:hasOBONamespace", "ncbi_taxonomy"));
	values.add(expandAnnotation(curie, "oio:hasDbXref", "GC_ID:11"));
	values.add(expandLabel(curie, "rdfs:label", "Bacteria"));
	values.add(expandSynonym(curie, "ncbitaxon:genbank_common_name", "oio:hasExactSynonym", "eubacteria"));
	values.add(expandSynonym(curie, "ncbitaxon:synonym", "oio:hasRelatedSynonym", "not Bacteria Haeckel 1894"));
	values.add(expandSynonym(curie, "ncbitaxon:in_part", "oio:hasRelatedSynonym", "Prokaryota"));
	values.add(expandSynonym(curie, "ncbitaxon:in_part", "oio:hasRelatedSynonym", "Monera"));
	values.add(expandSynonym(curie, "ncbitaxon:in_part", "oio:hasRelatedSynonym", "Procaryotae"));
	values.add(expandSynonym(curie, "ncbitaxon:in_part", "oio:hasRelatedSynonym", "Prokaryotae"));
	values.add(expandSynonym(curie, "ncbitaxon:blast_name", "oio:hasRelatedSynonym", "eubacteria"));

	Set<OWLAnnotationAssertionAxiom> annotations = 
		ontology.getAnnotationAssertionAxioms(iri);
	assertEquals("Count annotations for Bacteria", values.size(), annotations.size());

	checkAnnotations(annotations, values);
}
 
Example 13
Source File: NCBI2OWLTest.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void testActinobacteria(OWLOntology ontology) {
	String curie = "ncbi:201174";
	IRI iri = OWLConverter.format.getIRI(curie);
	OWLDataFactory df = ontology.getOWLOntologyManager().
		getOWLDataFactory();
	OWLClass taxon = df.getOWLClass(iri);
	assertTrue("Actinobacteria class in signature",
		ontology.containsClassInSignature(iri));

	// Check axioms
	Set<OWLClassAxiom> axioms = ontology.getAxioms(taxon, Imports.EXCLUDED);
	assertEquals("Count class axioms for Actinobacteria", 1, axioms.size());
	assertEquals("SubClassOf(<http://purl.obolibrary.org/obo/NCBITaxon_201174> <http://purl.obolibrary.org/obo/NCBITaxon_2>)", axioms.toArray()[0].toString());

	// Check annotations
	List<String> values = new ArrayList<String>();
	values.add(expandAnnotation(curie, "ncbitaxon:has_rank", OWLConverter.format.getIRI("ncbi:phylum")));
	values.add(expandAnnotation(curie, "oio:hasOBONamespace", "ncbi_taxonomy"));
	values.add(expandAnnotation(curie, "oio:hasDbXref", "GC_ID:11"));
	values.add(expandLabel(curie, "rdfs:label", "Actinobacteria [NCBITaxon:201174]"));
	values.add(expandSynonym(curie, "ncbitaxon:scientific_name", "oio:hasExactSynonym", "Actinobacteria"));
	values.add(expandSynonym(curie, "ncbitaxon:synonym", "oio:hasRelatedSynonym", "'Actinobacteria'"));
	values.add(expandSynonym(curie, "ncbitaxon:synonym", "oio:hasRelatedSynonym", "not Actinobacteria Cavalier-Smith 2002"));
	values.add(expandSynonym(curie, "ncbitaxon:blast_name", "oio:hasRelatedSynonym", "actinobacteria"));

	Set<OWLAnnotationAssertionAxiom> annotations = 
		ontology.getAnnotationAssertionAxioms(iri);
	assertEquals("Count annotations for Actinobacteria",
			values.size(), annotations.size());

	checkAnnotations(annotations, values);
}
 
Example 14
Source File: OWLGraphManipulator.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
     * Merges {@code OWLAxiom}s from the import ontology closure, with the source ontology.
     * This method is similar to {@link OWLGraphWrapperBasic#mergeImportClosure(boolean)}, 
     * except that: i) an {@code OWLAxiom} will be added to the source ontology only if 
     * it is not already present in the source (without taking annotations into account); 
     * ii) a check is performed to ensure that there will not be more than 
     * one annotation axiom for a given subject, when the annotation property 
     * corresponds to a tag with max cardinality one in OBO format 
     * (see {@code org.obolibrary.oboformat.model.Frame#check()}). As for 
     * {@link OWLGraphWrapperBasic#mergeImportClosure(boolean)}, annotations on the ontology 
     * itself are not imported, and the import declarations are removed after execution. 
     */
    private void mergeImportClosure() {
        log.info("Merging axioms from import closure with source ontology...");
        OWLOntology sourceOnt = this.getOwlGraphWrapper().getSourceOntology();
        
        //the method OWLOntology.containsAxiomIgnoreAnnotations is really 
        //not well optimized, so we get all OWLAxioms without annotations 
        //from the source ontology. 
        log.debug("Retrieving OWLAxioms without annotations from source ontology...");
        Set<OWLAxiom> sourceAxiomsNoAnnots = new HashSet<OWLAxiom>();
        for (OWLAxiom ax: sourceOnt.getAxioms()) {
            sourceAxiomsNoAnnots.add(ax.getAxiomWithoutAnnotations());
        }
        if (log.isDebugEnabled()) {
            log.debug(sourceAxiomsNoAnnots.size() + " axioms without annotations retrieved " +
            		"over " + sourceOnt.getAxiomCount() + " axioms.");
        }
        
        for (OWLOntology importedOnt: sourceOnt.getImportsClosure()) {
            if (importedOnt.equals(sourceOnt)) {
                continue;
            }
            log.info("Merging " + importedOnt);
            
//            OWLDataFactory df = sourceOnt.getOWLOntologyManager().getOWLDataFactory();
//            OWLAnnotationProperty p = 
//                    df.getOWLAnnotationProperty(OWLRDFVocabulary.RDFS_COMMENT.getIRI());
//            OWLLiteral v = df.getOWLLiteral("Imported " + importedOnt);
//            OWLAnnotation ann = df.getOWLAnnotation(p, v);
//            AddOntologyAnnotation addAnn = 
//                    new AddOntologyAnnotation(sourceOnt, ann);
//            sourceOnt.getOWLOntologyManager().applyChange(addAnn);
            
            //filter the axioms imported to avoid redundancy (too bad there is not 
            //a method OWLOntology.getAxiomsIgnoreAnnotations())
            int importedAxiomCount = 0;
            importAxioms: for (OWLAxiom importedAx: importedOnt.getAxioms()) {
                if (sourceAxiomsNoAnnots.contains(importedAx.getAxiomWithoutAnnotations())) {
                    continue importAxioms;
                }
                    
                //if it is an annotation axion, we need to ensure that 
                //there will not be more than one axiom for a given subject, 
                //when the annotation corresponds to a tag with max cardinality one 
                //in OBO format (see org.obolibrary.oboformat.model.Frame#check()).
                if (importedAx instanceof OWLAnnotationAssertionAxiom) {
                    OWLAnnotationAssertionAxiom castAx = 
                            (OWLAnnotationAssertionAxiom) importedAx;

                    if (maxOneCardinalityAnnots.contains(
                            castAx.getProperty().getIRI().toString()) || 
                            maxOneCardinalityAnnots.contains(
                                    this.getOwlGraphWrapper().getIdentifier(
                                            castAx.getProperty()))) {

                        //check whether we have an annotation for the same subject 
                        //in the source ontology.
                        for (OWLAnnotationAssertionAxiom sourceAnnotAx: 
                            sourceOnt.getAnnotationAssertionAxioms(
                                    castAx.getSubject())) {
                            if (sourceAnnotAx.getProperty().equals(
                                    castAx.getProperty())) {
                                //discard the axiom from import ontology, there is already 
                                //an annotation with same property on same subject
                                log.trace("Discarding axiom: " + castAx);
                                continue importAxioms;
                            }
                        }
                    }
                }
                
                //all verifications passed, include the axiom
                importedAxiomCount++;
                sourceAxiomsNoAnnots.add(importedAx.getAxiomWithoutAnnotations());
                sourceOnt.getOWLOntologyManager().addAxiom(sourceOnt, importedAx);
            }
            log.info(importedAxiomCount + " axioms imported.");
        }

        //remove the import declarations
        Set<OWLImportsDeclaration> oids = sourceOnt.getImportsDeclarations();
        for (OWLImportsDeclaration oid : oids) {
            RemoveImport ri = new RemoveImport(sourceOnt, oid);
            sourceOnt.getOWLOntologyManager().applyChange(ri);
        }
        
        log.info("Done merging axioms.");
    }