Skip to content

Commit

Permalink
Fixing #28
Browse files Browse the repository at this point in the history
  • Loading branch information
pnrobinson committed Nov 15, 2017
1 parent 3ca4921 commit bed8906
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.github.phenomics.ontolib.ontology.data;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;

import com.github.phenomics.ontolib.graph.algo.BreadthFirstSearch;
import com.github.phenomics.ontolib.graph.algo.VertexVisitor;
Expand Down Expand Up @@ -193,9 +190,26 @@ public Ontology<T, R> subOntology(TermId subOntologyRoot) {
final Set<TermId> childTermIds = OntologyTerms.childrenOf(subOntologyRoot, this);
final ImmutableDirectedGraph<TermId, ImmutableEdge<TermId>> subGraph =
(ImmutableDirectedGraph<TermId, ImmutableEdge<TermId>>) graph.subGraph(childTermIds);
Set<TermId> intersectingTerms = Sets.intersection(nonObsoleteTermIds,childTermIds);
// make sure the Term map contains only terms from the subontology
final ImmutableMap.Builder<TermId,T> termBuilder = ImmutableMap.builder();

for (final TermId tid : intersectingTerms) {
termBuilder.put(tid,termMap.get(tid));
}
ImmutableMap<TermId,T> subsetTermMap=termBuilder.build();
// Only retain relations where both source and destination are terms in the subontology
final ImmutableMap.Builder<Integer,R> relationBuilder = ImmutableMap.builder();
for(Iterator<Map.Entry<Integer, R>> it = relationMap.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<Integer, R> entry = it.next();
TermRelation tr = entry.getValue();
if (subsetTermMap.containsKey(tr.getSource()) && subsetTermMap.containsKey(tr.getDest())) {
relationBuilder.put(entry.getKey(),entry.getValue());
}
}
return new ImmutableOntology<T, R>(metaInfo, subGraph, subOntologyRoot,
Sets.intersection(nonObsoleteTermIds, childTermIds),
Sets.intersection(obsoleteTermIds, childTermIds), termMap, relationMap);
intersectingTerms,
Sets.intersection(obsoleteTermIds, childTermIds), subsetTermMap, relationBuilder.build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

import org.junit.Test;

import java.util.Map;

public class ImmutableOntologyTest extends ImmutableOntologyTestBase {

@Test
Expand Down Expand Up @@ -36,4 +39,43 @@ public void test() {
ontology.getParentTermIds(id1).toString());
}

/**
* The subontology defined by Term with id4 should consist of only the terms id4 and id1.
* The termmap should thus contain only two terms. The subontology does not contain the original root of the ontology, id5.
*/
@Test
public void testSubontologyCreation() {
ImmutableOntology<TestTerm, TestTermRelation> subontology=(ImmutableOntology<TestTerm, TestTermRelation>)ontology.subOntology(id4);
assertTrue(subontology.getTermMap().containsKey(id4));
assertTrue(subontology.getTermMap().containsKey(id1));
assertTrue(ontology.getTermMap().size()==5);
assertTrue(subontology.getTermMap().size()==2);
assertFalse(subontology.getTermMap().containsKey(id5));
}

/**
* The parent ontology has six relations
* 1 TestTermRelation [source=ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000001], dest=ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000002], id=1]
2 TestTermRelation [source=ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000001], dest=ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000003], id=2]
3 TestTermRelation [source=ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000001], dest=ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000004], id=3]
4 TestTermRelation [source=ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000002], dest=ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000005], id=4]
5 TestTermRelation [source=ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000003], dest=ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000005], id=5]
6 TestTermRelation [source=ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000004], dest=ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000005], id=6]
The subontology has just the terms id1 and id4, and thus should just have only one relation./subontology
3 TestTermRelation [source=ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000001], dest=ImmutableTermId [prefix=ImmutableTermPrefix [value=HP], id=0000004], id=3]
*/
@Test
public void testSubontologyRelations() {
ImmutableOntology<TestTerm, TestTermRelation> subontology=(ImmutableOntology<TestTerm, TestTermRelation>)ontology.subOntology(id4);
Map<Integer, TestTermRelation> relationMap = ontology.getRelationMap();
int expectedSize=6;
assertEquals(expectedSize,relationMap.size());
relationMap = subontology.getRelationMap();
expectedSize=1;
assertEquals(expectedSize,relationMap.size());
}




}

0 comments on commit bed8906

Please sign in to comment.