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

Add new method to tag set utils #302

Merged
Merged
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
31 changes: 31 additions & 0 deletions src/main/java/org/mastodon/util/TagSetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@
*/
package org.mastodon.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

import org.mastodon.mamut.model.Link;
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;
Expand Down Expand Up @@ -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();
}
}
12 changes: 12 additions & 0 deletions src/main/java/org/mastodon/util/TreeUtils.java
stefanhahmann marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 );
}
}
38 changes: 38 additions & 0 deletions src/test/java/org/mastodon/util/TagSetUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 );
}
}
14 changes: 14 additions & 0 deletions src/test/java/org/mastodon/util/TreeUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
Loading