-
Notifications
You must be signed in to change notification settings - Fork 2
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
pnrobinson
wants to merge
2
commits into
develop
Choose a base branch
from
subontology
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Subontology #31
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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()); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove superflous empty lines. |
||
|
||
|
||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please no star imports.