AnnotationRulesEngine { public AnnotationRulesEngineResult validateAnnotations(GafDocument doc) throws AnnotationRuleCheckException{ List<AnnotationRule> annotationRules = rulesFactory.getGeneAnnotationRules(); List<AnnotationRule> documentRules = rulesFactory.getGafDocumentRules(); List<AnnotationRule> owlRules = rulesFactory.getOwlRules(); List<AnnotationRule> inferenceRules = null; if (createInferences) { inferenceRules = rulesFactory.getInferenceRules(); } List<AnnotationRule> experimentalInferenceRules = null; if (useExperimentalInferences) { experimentalInferenceRules = rulesFactory.getExperimentalInferenceRules(); } if(annotationRules == null || annotationRules.isEmpty()){ throw new AnnotationRuleCheckException("Rules are not initialized. Please check the annotation_qc.xml file for errors"); } AnnotationRulesEngineResult result = new AnnotationRulesEngineResult(); LOG.info("Start validation on annotation level with "+annotationRules.size()+" rules."); try{ final List<GeneAnnotation> geneAnnotations = doc.getGeneAnnotations(); int count = 0; final int length = geneAnnotations.size(); result.setAnnotationCount(length); double size = (double) length; double last = 0.0d; for(GeneAnnotation annotation : geneAnnotations){ for(AnnotationRule rule : annotationRules){ if (!isGrandFatheredAnnotation(annotation, rule)) { result.addViolations(rule.getRuleViolations(annotation)); } } count += 1; double current = count / size; if (Math.abs(current - last) > 0.05d) { NumberFormat percentInstance = DecimalFormat.getPercentInstance(); LOG.info("Progress: "+percentInstance.format(current)); last = current; } } if (documentRules != null && !documentRules.isEmpty()) { LOG.info("Start validation on document level with "+documentRules.size()+" rules."); for (AnnotationRule rule : documentRules) { result.addViolations(rule.getRuleViolations(doc)); } } OWLGraphWrapper graph = rulesFactory.getGraph(); final boolean hasOwlRules = owlRules != null && !owlRules.isEmpty(); final boolean hasInferenceRules = createInferences && inferenceRules != null && !inferenceRules.isEmpty(); final boolean hasExperimentalInferenceRules = useExperimentalInferences && experimentalInferenceRules != null && !experimentalInferenceRules.isEmpty(); boolean buildTranslatedGraph = graph != null && (hasOwlRules || hasInferenceRules); if (graph != null) { result.setOntologyVersions(graph.getVersions()); } OWLGraphWrapper translatedGraph = null; if (buildTranslatedGraph) { LOG.info("Creating OWL represenation of annotations."); final OWLOntologyManager m = graph.getManager(); OWLOntology translated = m.createOntology(IRI.generateDocumentIRI()); final OWLOntology source = graph.getSourceOntology(); final OWLOntologyID sourceId = source.getOntologyID(); if (sourceId != null && sourceId.getDefaultDocumentIRI() != null) { OWLDataFactory f = m.getOWLDataFactory(); m.applyChange(new AddImport(translated, f.getOWLImportsDeclaration(sourceId.getDefaultDocumentIRI().get()))); } else { m.addAxioms(translated, source.getAxioms()); Set<OWLImportsDeclaration> importsDeclarations = source.getImportsDeclarations(); for (OWLImportsDeclaration importsDeclaration : importsDeclarations) { m.applyChange(new AddImport(translated, importsDeclaration)); } } GAFOWLBridge bridge = new GAFOWLBridge(graph, translated); bridge.setGenerateIndividuals(false); bridge.setBasicAboxMapping(false); bridge.setBioentityMapping(BioentityMapping.NAMED_CLASS); bridge.setSkipNotAnnotations(true); translatedGraph = new OWLGraphWrapper(translated); } if (hasOwlRules && translatedGraph != null) { LOG.info("Start validation using OWL representation with "+owlRules.size()+" rules."); for(AnnotationRule rule : owlRules) { result.addViolations(rule.getRuleViolations(doc, translatedGraph)); } LOG.info("Finished validation in OWL."); } if (hasInferenceRules && translatedGraph != null) { LOG.info("Start prediction/inference of annotations."); for(AnnotationRule rule : inferenceRules) { result.addInferences(rule.getPredictedAnnotations(doc, translatedGraph)); } LOG.info("Finished prediction/inference of new annotations. Found: "+result.predictions.size()); } if (hasExperimentalInferenceRules && translatedGraph != null) { LOG.info("Start experimental prediction/inference of annotations."); for(AnnotationRule rule : experimentalInferenceRules) { result.addExperimentalInferences(rule.getPredictedAnnotations(doc, translatedGraph)); } LOG.info("Finished experimental prediction/inference of new annotations. Found: "+result.experimentalPredictions.size()); } }catch(Exception ex){ LOG.error(ex.getMessage(), ex); throw new RuntimeException(ex); } LOG.info("Finished validation of annotations."); return result; } AnnotationRulesEngine(AnnotationRulesFactory rulesFactory, boolean createInferences, boolean useExperimental); AnnotationRule getRule(String id); AnnotationRulesEngineResult validateAnnotations(GafDocument doc);  }
@Test public void testValidateAnnotations() throws Exception { GafObjectsBuilder builder = new GafObjectsBuilder(); GafDocument gafdoc = builder.buildDocument(getResource("test_gene_association_mgi.gaf")); AnnotationRulesEngineResult result = engine.validateAnnotations(gafdoc); if (renderViolations) { renderViolations(result); } assertTrue(result.hasWarnings()); Map<String, List<AnnotationRuleViolation>> warnings = result.getViolations(ViolationType.Warning); assertEquals(4, warnings.size()); assertEquals(2, warnings.get("GO_AR:0000008").size()); assertEquals(3, warnings.get("GO_AR:0000013").size()); assertEquals(1, warnings.get("GO_AR:0000018").size()); assertEquals(2, warnings.get("GO_AR:0000019").size()); assertTrue(result.hasRecommendations()); Map<String, List<AnnotationRuleViolation>> recommendations = result.getViolations(ViolationType.Recommendation); assertEquals(5, recommendations.get("GO_AR:0000004").size()); assertTrue(result.hasErrors()); Map<String, List<AnnotationRuleViolation>> errors = result.getViolations(ViolationType.Error); assertEquals(4, errors.size()); assertEquals(154, errors.get("GO_AR:0000001").size()); assertEquals(1, errors.get("GO_AR:0000011").size()); assertEquals(2, errors.get("GO_AR:0000013").size()); assertEquals(1, errors.get("GO_AR:0000014").size()); }