Skip to content

Commit

Permalink
Add new plugin to create a dummy tag set with specifiable number of t…
Browse files Browse the repository at this point in the history
…ags in random colors
  • Loading branch information
stefanhahmann committed Nov 28, 2024
1 parent 149ac19 commit 3be5000
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 3 deletions.
112 changes: 112 additions & 0 deletions src/main/java/org/mastodon/mamut/tomancak/CreateDummyTagSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.mastodon.mamut.tomancak;

import java.awt.Color;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.commons.lang3.tuple.Pair;
import org.mastodon.mamut.ProjectModel;
import org.mastodon.mamut.model.Model;
import org.mastodon.model.tag.TagSetStructure;
import org.mastodon.util.TagSetUtils;
import org.scijava.Context;
import org.scijava.ItemVisibility;
import org.scijava.command.Command;
import org.scijava.command.CommandService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CreateDummyTagSet
{
private static final Logger logger = LoggerFactory.getLogger( MethodHandles.lookup().lookupClass() );

private static final Random random = new Random();

private CreateDummyTagSet()
{
// Prevent instantiation.
}

static void createDummyTagSet( final ProjectModel projectModel, final CommandService commandService )
{
commandService.run( DummyTagSetCommand.class, true, "model", projectModel.getModel() );
}

@Plugin( type = Command.class, label = "Create dummy tag set" )
public static class DummyTagSetCommand implements Command
{

private static final int WIDTH = 15;

@SuppressWarnings( "all" )
@Parameter( visibility = ItemVisibility.MESSAGE, required = false, persist = false )
private String documentation = "<html>\n"
+ "<body width=" + WIDTH + "cm align=left>\n"
+ "<h2>Create a dummy tag set with a specifiable number of tags</h2>\n"
+ "<p>"
+ "This plugin generates a new tag set with a specifiable number of tags in random colors.<br><br>"
+ "Mastodon can handle tousands of tags, but the maximum number is limited by the system of the user.<br><br>"
+ "An error message while trying to create the tag set might indicate that the specified number of tags exceeds the maximum number the Mastodon installation can handle.<br>"
+ "</p>\n"
+ "</body>\n"
+ "</html>\n";

@Parameter
private Model model;

@Parameter
private Context context;

@Parameter( label = "Tag set name", description = "Specify the name of the tag set that should be generated." )
private String tagSetName;

@Parameter( label = "Maximum number of tags", description = "Specify the number tags that should be generated in a dummy tag set.", min = "0", stepSize = "1" )
private int maxTags;

@Override
public void run()
{
createTagSet( model, tagSetName, maxTags );
}

private static void createTagSet( final Model model, final String tagSetName, final int numTags )
{
logger.info( "Creating tag set '{}' with {} dummy tags...", tagSetName, numTags );
final ReentrantReadWriteLock lock = model.getGraph().getLock();
lock.writeLock().lock();
try
{
Collection< Pair< String, Integer > > labelColorPairs = new ArrayList<>();
for ( int i = 0; i < numTags; i++ )
labelColorPairs.add( Pair.of( "tag" + i, getRandomColor().getRGB() ) );

TagSetStructure.TagSet tagSet = TagSetUtils.addNewTagSetToModel( model, tagSetName, labelColorPairs );
model.setUndoPoint();

logger.info( "Successfully added tag set." );
logger.info( "Tag set name: {} ", tagSet.getName() );
logger.info( "Number of tags: {}", tagSet.getTags().size() );
}
finally
{
lock.writeLock().unlock();
}
}

private static Color getRandomColor()
{
// Generate random RGB values
int red = random.nextInt( 256 );
int green = random.nextInt( 256 );
int blue = random.nextInt( 256 );

// Create the color using the RGB values
return new Color( red, green, blue );
}
}
}
19 changes: 16 additions & 3 deletions src/main/java/org/mastodon/mamut/tomancak/TomancakPlugins.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public class TomancakPlugins extends AbstractContextual implements MamutPlugin
private static final String COPY_TAG = "[tomancak] copy tag";
private static final String INTERPOLATE_SPOTS = "[tomancak] interpolate missing spots";
private static final String LABEL_SELECTED_SPOTS = "[tomancak] label selected spots";

private static final String SET_RADIUS_SELECTED_SPOTS = "[tomancak] set radius selected spots";
private static final String CHANGE_BRANCH_LABELS = "[tomancak] change branch labels";
private static final String COMPACT_LINEAGE_VIEW = "[tomancak] show compact lineage";
Expand All @@ -101,13 +100,13 @@ public class TomancakPlugins extends AbstractContextual implements MamutPlugin
private static final String FUSE_SPOTS = "[tomancak] fuse selected spots";
private static final String LOCATE_TAGS = "[tomancak] locate tags";
private static final String CELL_DIVISIONS_TAG_SET = "[tomancak] create cell divisions tag set";
private static final String CREATE_DUMMY_TAG_SET = "[tomancak] create dummy tag set";

private static final String[] EXPORT_PHYLOXML_KEYS = { "not mapped" };
private static final String[] FLIP_DESCENDANTS_KEYS = { "ctrl E" };
private static final String[] COPY_TAG_KEYS = { "not mapped" };
private static final String[] INTERPOLATE_SPOTS_KEYS = { "not mapped" };
private static final String[] LABEL_SELECTED_SPOTS_KEYS = { "F2" };

private static final String[] SET_RADIUS_SELECTED_SPOTS_KEYS = { "F3" };
private static final String[] CHANGE_BRANCH_LABELS_KEYS = { "shift F2" };
private static final String[] COMPACT_LINEAGE_VIEW_KEYS = { "not mapped" };
Expand All @@ -126,6 +125,7 @@ public class TomancakPlugins extends AbstractContextual implements MamutPlugin
private static final String[] FUSE_SPOTS_KEYS = { "ctrl alt F" };
private static final String[] LOCATE_TAGS_KEYS = { "not mapped" };
private static final String[] CELL_DIVISIONS_TAG_SET_KEYS = { "not mapped" };
private static final String[] CREATE_DUMMY_TAG_SET_KEYS = { "not mapped" };

private static Map< String, String > menuTexts = new HashMap<>();

Expand Down Expand Up @@ -153,6 +153,7 @@ public class TomancakPlugins extends AbstractContextual implements MamutPlugin
menuTexts.put( FUSE_SPOTS, "Fuse selected spots" );
menuTexts.put( LOCATE_TAGS, "Locate tags" );
menuTexts.put( CELL_DIVISIONS_TAG_SET, "Add tag set to highlight cell divisions" );
menuTexts.put( CREATE_DUMMY_TAG_SET, "Create dummy tag set" );
}

/*
Expand Down Expand Up @@ -193,6 +194,8 @@ public void getCommandDescriptions( final CommandDescriptions descriptions )
descriptions.add( FUSE_SPOTS, FUSE_SPOTS_KEYS, "Fuse selected spots into a single spot. Average spot position and shape." );
descriptions.add( LOCATE_TAGS, LOCATE_TAGS_KEYS, "Open a dialog that allows to jump to specific tags." );
descriptions.add( CELL_DIVISIONS_TAG_SET, CELL_DIVISIONS_TAG_SET_KEYS, "Adds a tag set to highlight cell divisions." );
descriptions.add( CREATE_DUMMY_TAG_SET, CREATE_DUMMY_TAG_SET_KEYS,
"Creates a dummy tag set with a specifiable maximum number of tags." );
}
}

Expand Down Expand Up @@ -245,6 +248,8 @@ public void getCommandDescriptions( final CommandDescriptions descriptions )

private final AbstractNamedAction cellDivisionsTagSetAction;

private final AbstractNamedAction createDummyTagSet;

private ProjectModel projectModel;

public TomancakPlugins()
Expand All @@ -271,6 +276,7 @@ public TomancakPlugins()
fuseSpots = new RunnableAction( FUSE_SPOTS, this::fuseSpots );
locateTags = new RunnableAction( LOCATE_TAGS, this::locateTags );
cellDivisionsTagSetAction = new RunnableAction( CELL_DIVISIONS_TAG_SET, this::runCellDivisionsTagSet );
createDummyTagSet = new RunnableAction( CREATE_DUMMY_TAG_SET, this::createDummyTagSet );
}

@Override
Expand All @@ -295,7 +301,8 @@ public List< ViewMenuBuilder.MenuItem > getMenuItems()
menu( "Tags",
item( LOCATE_TAGS ),
item( COPY_TAG ),
item( CELL_DIVISIONS_TAG_SET ) ),
item( CELL_DIVISIONS_TAG_SET ),
item( CREATE_DUMMY_TAG_SET ) ),
menu( "Spots management",
menu( "Rename spots",
item( LABEL_SELECTED_SPOTS ),
Expand Down Expand Up @@ -351,6 +358,7 @@ public void installGlobalActions( final Actions actions )
actions.namedAction( fuseSpots, FUSE_SPOTS_KEYS );
actions.namedAction( locateTags, LOCATE_TAGS_KEYS );
actions.namedAction( cellDivisionsTagSetAction, CELL_DIVISIONS_TAG_SET_KEYS );
actions.namedAction( createDummyTagSet, CREATE_DUMMY_TAG_SET_KEYS );
}

private void exportPhyloXml()
Expand Down Expand Up @@ -476,4 +484,9 @@ private void runCellDivisionsTagSet()
{
commandService.run( CellDivisionsTagSetCommand.class, true, "projectModel", projectModel );
}

private void createDummyTagSet()
{
CreateDummyTagSet.createDummyTagSet( projectModel, commandService );
}
}

0 comments on commit 3be5000

Please sign in to comment.