diff --git a/src/main/java/org/mastodon/util/TagSetUtils.java b/src/main/java/org/mastodon/util/TagSetUtils.java index 1952be552..0afd96d3e 100644 --- a/src/main/java/org/mastodon/util/TagSetUtils.java +++ b/src/main/java/org/mastodon/util/TagSetUtils.java @@ -28,7 +28,9 @@ */ package org.mastodon.util; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; import java.util.Map; import java.util.NoSuchElementException; @@ -36,6 +38,7 @@ import org.mastodon.mamut.model.Model; import org.mastodon.mamut.model.ModelGraph; import org.mastodon.mamut.model.Spot; +import org.mastodon.mamut.model.branch.BranchSpot; import org.mastodon.model.tag.ObjTagMap; import org.mastodon.model.tag.TagSetModel; import org.mastodon.model.tag.TagSetStructure; @@ -351,4 +354,32 @@ public static TagSetStructure.Tag findTag( final TagSetStructure.TagSet tagSet, return tag; throw new NoSuchElementException( "Did not find a tag with the given label: " + tagLabel ); } + + /** + * Returns the names of all tag sets in the model. + * @param model the model to get the tag-set model from. + * @return the names of all tag sets in the model. + */ + public static List< String > getTagSetNames( final Model model ) + { + List< String > tagSetNames = new ArrayList<>(); + model.getTagSetModel().getTagSetStructure().getTagSets().forEach( tagSet -> tagSetNames.add( tagSet.getName() ) ); + return tagSetNames; + } + + /** + * Gets the tag label of the first spot in the given branchSpot within the given tagSet. + * @param model the model to which the branch belongs + * @param branchSpot the branch spot + * @param tagSet the tag set + * @return the tag label + */ + public static String getTagLabel( final Model model, final BranchSpot branchSpot, final TagSetStructure.TagSet tagSet, final Spot ref ) + { + if ( model == null || branchSpot == null || tagSet == null ) + return null; + Spot first = TreeUtils.getFirstSpot( model, branchSpot, ref ); + TagSetStructure.Tag tag = TagSetUtils.getBranchTag( model, tagSet, first ); + return tag == null ? null : tag.label(); + } } diff --git a/src/main/java/org/mastodon/util/TreeUtils.java b/src/main/java/org/mastodon/util/TreeUtils.java index 0a8734354..b1c6ae44b 100644 --- a/src/main/java/org/mastodon/util/TreeUtils.java +++ b/src/main/java/org/mastodon/util/TreeUtils.java @@ -39,6 +39,7 @@ import org.mastodon.graph.Vertex; import org.mastodon.mamut.model.Model; import org.mastodon.mamut.model.Spot; +import org.mastodon.mamut.model.branch.BranchSpot; public class TreeUtils { @@ -255,4 +256,15 @@ public static int getMaxTimepoint( final Model model ) max = Math.max( max, spot.getTimepoint() ); return max; } + + /** + * Gets the first {@link Spot} within the given {@link BranchSpot}. + * @param model the {@link Model} to which the {@link BranchSpot} belongs + * @param branchSpot the {@link BranchSpot} to query + * @return the first {@link Spot} + */ + public static Spot getFirstSpot( final Model model, final BranchSpot branchSpot, final Spot ref ) + { + return model.getBranchGraph().getFirstLinkedVertex( branchSpot, ref ); + } } diff --git a/src/test/java/org/mastodon/util/TagSetUtilsTest.java b/src/test/java/org/mastodon/util/TagSetUtilsTest.java index 48f0a1421..34c30b7b6 100644 --- a/src/test/java/org/mastodon/util/TagSetUtilsTest.java +++ b/src/test/java/org/mastodon/util/TagSetUtilsTest.java @@ -38,9 +38,13 @@ import java.awt.Color; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; +import java.util.Collections; +import java.util.List; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; public class TagSetUtilsTest @@ -110,4 +114,38 @@ public void tagSpotAndOutgoingEdges() // 3 links are tagged: spot0 -> spot1, spot2 -> spot3, spot2 -> spot11 assertEquals( 3, model.getTagSetModel().getEdgeTags().getTaggedWith( tag0 ).size() ); } + + @Test + public void testGetTagSetNames() + { + ExampleGraph1 exampleGraph1 = new ExampleGraph1(); + String tagSetName1 = "TagSet1"; + String tagSetName2 = "TagSet2"; + String tagSetName3 = "TagSet2"; + Collection< Pair< String, Integer > > emptyTagsAndColors = Collections.emptyList(); + TagSetUtils.addNewTagSetToModel( exampleGraph1.getModel(), tagSetName1, emptyTagsAndColors ); + TagSetUtils.addNewTagSetToModel( exampleGraph1.getModel(), tagSetName2, emptyTagsAndColors ); + TagSetUtils.addNewTagSetToModel( exampleGraph1.getModel(), tagSetName3, emptyTagsAndColors ); + Collection< String > tagSetNames = TagSetUtils.getTagSetNames( exampleGraph1.getModel() ); + List< String > expected = Arrays.asList( tagSetName1, tagSetName2, tagSetName3 ); + assertEquals( expected, tagSetNames ); + } + + @Test + public void testGetTagLabel() + { + ExampleGraph2 exampleGraph2 = new ExampleGraph2(); + String tagSetName = "TagSet"; + Pair< String, Integer > tag0 = Pair.of( "Tag", 0 ); + Collection< Pair< String, Integer > > tagAndColor = Collections.singletonList( tag0 ); + TagSetStructure.TagSet tagSet = TagSetUtils.addNewTagSetToModel( exampleGraph2.getModel(), tagSetName, tagAndColor ); + TagSetStructure.Tag tag = tagSet.getTags().get( 0 ); + TagSetUtils.tagBranch( exampleGraph2.getModel(), tagSet, tag, exampleGraph2.spot5 ); + Spot ref = exampleGraph2.getModel().getGraph().vertexRef(); + assertEquals( tag.label(), TagSetUtils.getTagLabel( exampleGraph2.getModel(), exampleGraph2.branchSpotD, tagSet, ref ) ); + assertNull( TagSetUtils.getTagLabel( null, exampleGraph2.branchSpotD, tagSet, ref ) ); + assertNull( TagSetUtils.getTagLabel( exampleGraph2.getModel(), null, tagSet, ref ) ); + assertNull( TagSetUtils.getTagLabel( exampleGraph2.getModel(), exampleGraph2.branchSpotD, null, ref ) ); + exampleGraph2.getModel().getGraph().releaseRef( ref ); + } } diff --git a/src/test/java/org/mastodon/util/TreeUtilsTest.java b/src/test/java/org/mastodon/util/TreeUtilsTest.java index 803f991f7..fbaf9df3c 100644 --- a/src/test/java/org/mastodon/util/TreeUtilsTest.java +++ b/src/test/java/org/mastodon/util/TreeUtilsTest.java @@ -191,4 +191,18 @@ public void testGetMaxTimepoint() assertEquals( 3, TreeUtils.getMaxTimepoint( new ExampleGraph1().getModel() ) ); assertEquals( 7, TreeUtils.getMaxTimepoint( new ExampleGraph2().getModel() ) ); } + + @Test + public void testGetFirstSpot() + { + ExampleGraph1 exampleGraph1 = new ExampleGraph1(); + Spot ref = exampleGraph1.getModel().getGraph().vertexRef(); + assertEquals( exampleGraph1.spot0, TreeUtils.getFirstSpot( exampleGraph1.getModel(), exampleGraph1.branchSpotA, ref ) ); + exampleGraph1.getModel().getGraph().releaseRef( ref ); + + ExampleGraph2 exampleGraph2 = new ExampleGraph2(); + ref = exampleGraph2.getModel().getGraph().vertexRef(); + assertEquals( exampleGraph2.spot5, TreeUtils.getFirstSpot( exampleGraph2.getModel(), exampleGraph2.branchSpotD, ref ) ); + exampleGraph2.getModel().getGraph().releaseRef( ref ); + } }