001 /* 002 * The contents of this file are subject to the Mozilla Public 003 * License Version 1.1 (the "License"); you may not use this file 004 * except in compliance with the License. You may obtain a copy of 005 * the License at http://www.mozilla.org/MPL/ 006 * 007 * Software distributed under the License is distributed on an "AS 008 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 009 * implied. See the License for the specific language governing 010 * rights and limitations under the License. 011 * 012 * The Original Code is Knowtator. 013 * 014 * The Initial Developer of the Original Code is University of Colorado. 015 * Copyright (C) 2005 - 2008. All Rights Reserved. 016 * 017 * Knowtator was developed by the Center for Computational Pharmacology 018 * (http://compbio.uchcs.edu) at the University of Colorado Health 019 * Sciences Center School of Medicine with support from the National 020 * Library of Medicine. 021 * 022 * Current information about Knowtator can be obtained at 023 * http://knowtator.sourceforge.net/ 024 * 025 * Contributor(s): 026 * Philip V. Ogren <philip@ogren.info> (Original Author) 027 */ 028 package edu.uchsc.ccp.knowtator.util; 029 030 import java.util.Collection; 031 import java.util.HashMap; 032 import java.util.HashSet; 033 import java.util.Map; 034 import java.util.Set; 035 036 import edu.stanford.smi.protege.model.Cls; 037 import edu.stanford.smi.protege.model.KnowledgeBase; 038 import edu.stanford.smi.protege.model.SimpleInstance; 039 import edu.stanford.smi.protege.util.CollectionUtilities; 040 import edu.uchsc.ccp.knowtator.AnnotationUtil; 041 import edu.uchsc.ccp.knowtator.FilterUtil; 042 import edu.uchsc.ccp.knowtator.InvalidSpanException; 043 import edu.uchsc.ccp.knowtator.KnowtatorManager; 044 import edu.uchsc.ccp.knowtator.KnowtatorProjectUtil; 045 import edu.uchsc.ccp.knowtator.MentionUtil; 046 import edu.uchsc.ccp.knowtator.textsource.TextSourceAccessException; 047 048 public class ConsensusAnnotations { 049 050 public static ConsensusSet recreateConsensusAnnotations(SimpleInstance textSource, SimpleInstance consensusSet, 051 KnowtatorManager manager) throws ConsensusException { 052 KnowledgeBase kb = manager.getKnowledgeBase(); 053 KnowtatorProjectUtil kpu = manager.getKnowtatorProjectUtil(); 054 AnnotationUtil annotationUtil = manager.getAnnotationUtil(); 055 MentionUtil mentionUtil = manager.getMentionUtil(); 056 FilterUtil filterUtil = manager.getFilterUtil(); 057 058 SimpleInstance consensusFilter = (SimpleInstance) consensusSet.getOwnSlotValue(kpu 059 .getConsensusSetConsensusFilterSlot()); 060 061 Collection<SimpleInstance> annotations = annotationUtil.getAnnotations(textSource); 062 if (annotations != null) { 063 Collection<SimpleInstance> oldConsensusAnnotations = filterUtil.filterAnnotations(annotations, 064 consensusFilter); 065 for (SimpleInstance oldAnnotation : oldConsensusAnnotations) { 066 SimpleInstance mention = annotationUtil.getMention(oldAnnotation); 067 Set<SimpleInstance> mentions = mentionUtil.getAllConnectedMentions(mention); 068 for (SimpleInstance oldMention : mentions) { 069 kb.deleteFrame(oldMention); 070 } 071 kb.deleteFrame(oldAnnotation); 072 } 073 } 074 075 return createConsensusSet(textSource, manager, consensusSet); 076 } 077 078 public static void createConsensusAnnotations(KnowtatorManager manager, SimpleInstance filter, 079 String consensusSetName, Collection<SimpleInstance> textSources) throws ConsensusException { 080 KnowledgeBase kb = manager.getKnowledgeBase(); 081 KnowtatorProjectUtil kpu = manager.getKnowtatorProjectUtil(); 082 083 // need to document the fact that we are relying on the passed in filter 084 // to have the annotators specified. 085 // Or we need to throw an appropriate exception and display an error 086 // message. 087 Set<SimpleInstance> annotators = new HashSet<SimpleInstance>(FilterUtil.getAnnotators(filter)); 088 089 SimpleInstance teamAnnotator = kb.createSimpleInstance(null, null, CollectionUtilities.createCollection(kpu 090 .getTeamAnnotatorCls()), true); 091 teamAnnotator.setDirectOwnSlotValues(kpu.getAnnotatorTeamMembersSlot(), annotators); 092 teamAnnotator.setDirectOwnSlotValue(kpu.getAnnotatorTeamNameSlot(), consensusSetName + " annotator team"); 093 094 SimpleInstance consensusSet = kb.createSimpleInstance(null, null, CollectionUtilities.createCollection(kpu 095 .getConsensusSetCls()), true); 096 consensusSet.setDirectOwnSlotValue(kpu.getSetNameSlot(), consensusSetName); 097 consensusSet.setDirectOwnSlotValue(kpu.getSetDescriptionSlot(), 098 "This set corresponds to a consensus set of annotations generated by Knowtator."); 099 consensusSet.setDirectOwnSlotValue(kpu.getConsensusSetIndividualFilterSlot(), filter); 100 consensusSet.setDirectOwnSlotValue(kpu.getConsensusSetTeamAnnotatorSlot(), teamAnnotator); 101 102 SimpleInstance consensusFilter = kb.createSimpleInstance(null, null, CollectionUtilities.createCollection(kpu 103 .getConsensusFilterCls()), true); 104 consensusFilter.setDirectOwnSlotValue(kpu.getFilterNameSlot(), consensusSetName + " filter"); 105 consensusFilter.setDirectOwnSlotValue(kpu.getFilterSetSlot(), consensusSet); 106 consensusFilter.setDirectOwnSlotValues(kpu.getFilterAnnotatorSlot(), annotators); 107 consensusFilter.addOwnSlotValue(kpu.getFilterAnnotatorSlot(), teamAnnotator); 108 consensusSet.setDirectOwnSlotValue(kpu.getConsensusSetConsensusFilterSlot(), consensusFilter); 109 110 Set<Cls> filterTypes = FilterUtil.getTypes(filter); 111 if (filterTypes.size() > 0) 112 consensusFilter.setDirectOwnSlotValues(kpu.getFilterTypeSlot(), filterTypes); 113 else { 114 // we need to add a root cls so that IAA will not throw an 115 // exception. 116 consensusFilter.setDirectOwnSlotValues(kpu.getFilterTypeSlot(), manager.getRootClses()); 117 } 118 119 for (SimpleInstance textSource : textSources) { 120 ConsensusSet textSourceConsensusSet = createConsensusSet(textSource, manager, consensusSet); 121 textSourceConsensusSet.destroy(); 122 } 123 124 manager.addActiveFilter(consensusFilter); 125 manager.setSelectedFilter(consensusFilter); 126 manager.setSelectedAnnotator(teamAnnotator); 127 manager.setSelectedAnnotationSet(consensusSet); 128 } 129 130 public static ConsensusSet createConsensusSet(SimpleInstance textSource, KnowtatorManager manager, 131 SimpleInstance consensusSet) throws ConsensusException { 132 KnowtatorProjectUtil kpu = manager.getKnowtatorProjectUtil(); 133 AnnotationUtil annotationUtil = manager.getAnnotationUtil(); 134 FilterUtil filterUtil = manager.getFilterUtil(); 135 MentionUtil mentionUtil = manager.getMentionUtil(); 136 SimpleInstance individualFilter = (SimpleInstance) consensusSet.getOwnSlotValue(kpu 137 .getConsensusSetIndividualFilterSlot()); 138 Set<SimpleInstance> annotators = new HashSet<SimpleInstance>(FilterUtil.getAnnotators(individualFilter)); 139 if (annotators.size() < 2) 140 throw new ConsensusException("The filter corresponding to the individually annotated " 141 + "\nannotations for the selected consensus set does not " 142 + "\nhave two or more annotators specified."); 143 144 // The key is an original mention, and the value is a copied mention 145 // created for the consensus set. 146 Map<SimpleInstance, SimpleInstance> mentionCopies = new HashMap<SimpleInstance, SimpleInstance>(); 147 148 java.util.Set<SimpleInstance> consensusAnnotations = new HashSet<SimpleInstance>(); 149 // java.util.Set<SimpleInstance> consensusMentions = new 150 // HashSet<SimpleInstance>(); 151 152 Collection<SimpleInstance> annotations = annotationUtil.getAnnotations(textSource); 153 annotations = filterUtil.filterAnnotations(annotations, individualFilter); 154 155 for (SimpleInstance annotation : annotations) { 156 SimpleInstance mention = annotationUtil.getMention(annotation); 157 SimpleInstance mentionCopy = mentionCopies.get(mention); 158 if (mentionCopy == null) { 159 mentionCopy = mentionUtil.copyMention(mention, mentionCopies); 160 mentionCopies.put(mention, mentionCopy); 161 } 162 163 SimpleInstance annotator = annotationUtil.getAnnotator(annotation); 164 try { 165 SimpleInstance consensusAnnotation = annotationUtil.createAnnotation(mentionCopy, annotator, 166 annotationUtil.getSpans(annotation), textSource, consensusSet); 167 // collect all of the annotations created for the consensus set. 168 consensusAnnotations.add(consensusAnnotation); 169 } catch (TextSourceAccessException tsae) { 170 tsae.printStackTrace(); 171 } catch (InvalidSpanException ise) { 172 ise.printStackTrace(); 173 } 174 } 175 ConsensusSet textSourceConsensusSet = new ConsensusSet(consensusAnnotations, consensusSet, manager); 176 textSourceConsensusSet.consolidateAnnotations(); 177 return textSourceConsensusSet; 178 } 179 }