Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subontology #31

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please no star imports.


import com.github.phenomics.ontolib.graph.algo.BreadthFirstSearch;
import com.github.phenomics.ontolib.graph.algo.VertexVisitor;
Expand Down Expand Up @@ -193,9 +190,33 @@ 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);
return new ImmutableOntology<T, R>(metaInfo, subGraph, subOntologyRoot,
Sets.intersection(nonObsoleteTermIds, childTermIds),
Sets.intersection(obsoleteTermIds, childTermIds), termMap, relationMap);
Set<TermId> intersectingTerms = Sets.intersection(nonObsoleteTermIds,childTermIds);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whitespace is inconsistent. Your IDE should be able to help you with auto-formatting.

// 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());
}
}
// Note: natural order returns a builder whose keys are ordered by their natural ordering.
final ImmutableSortedMap.Builder<String,String> metaInfoBuilder = ImmutableSortedMap.naturalOrder();
for (String key : metaInfo.keySet()) {
metaInfoBuilder.put(key,metaInfo.get(key));
}
metaInfoBuilder.put("provenance",String.format("Ontology created as a subset from original ontology with root %s",getTermMap().get(rootTermId).getName() ));
ImmutableSortedMap<String,String> extendedMetaInfo=metaInfoBuilder.build();

return new ImmutableOntology<T, R>(extendedMetaInfo, subGraph, subOntologyRoot,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());
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove superflous empty lines.




}