From 413b471a9a4398d9857bfaef90862c7a5fde4ba5 Mon Sep 17 00:00:00 2001 From: Matthias Arzt Date: Fri, 14 Jun 2024 15:30:45 +0200 Subject: [PATCH] LineageRegistrationPlugin: add copy spot labels functionality --- .../LineageRegistrationControlService.java | 25 +++++++++++++++++ .../LineageRegistrationFrame.java | 27 +++++++++++++++++++ .../LineageRegistrationUtils.java | 24 +++++++++++++++++ .../LineageRegistrationUtilsTest.java | 21 +++++++++++++++ 4 files changed, 97 insertions(+) diff --git a/src/main/java/org/mastodon/mamut/tomancak/lineage_registration/LineageRegistrationControlService.java b/src/main/java/org/mastodon/mamut/tomancak/lineage_registration/LineageRegistrationControlService.java index 560fb20d..08084a60 100644 --- a/src/main/java/org/mastodon/mamut/tomancak/lineage_registration/LineageRegistrationControlService.java +++ b/src/main/java/org/mastodon/mamut/tomancak/lineage_registration/LineageRegistrationControlService.java @@ -157,6 +157,31 @@ public void onColorLineagesClicked() } ); } + @Override + public void onCopyLabelsAtoB() + { + copyLabelsFromTo( dialog.getProjectA(), dialog.getProjectB() ); + } + + @Override + public void onCopyLabelsBtoA() + { + copyLabelsFromTo( dialog.getProjectB(), dialog.getProjectA() ); + } + + private void copyLabelsFromTo( SelectedProject fromProject, SelectedProject toProject ) + { + executeTask( true, fromProject, toProject, () -> { + dialog.clearLog(); + dialog.log( "Copy labels from project \"%s\" to project \"%s\"...", + fromProject.getName(), toProject.getName() ); + RegisteredGraphs registration = runRegistrationAlgorithm( fromProject, toProject ); + LineageRegistrationUtils.copySpotLabelsFromAtoB( registration ); + toProject.getModel().setUndoPoint(); + dialog.log( "done." ); + } ); + } + @Override public void onCopyTagSetAtoB() { diff --git a/src/main/java/org/mastodon/mamut/tomancak/lineage_registration/LineageRegistrationFrame.java b/src/main/java/org/mastodon/mamut/tomancak/lineage_registration/LineageRegistrationFrame.java index 900de3c8..c501bb8e 100644 --- a/src/main/java/org/mastodon/mamut/tomancak/lineage_registration/LineageRegistrationFrame.java +++ b/src/main/java/org/mastodon/mamut/tomancak/lineage_registration/LineageRegistrationFrame.java @@ -85,6 +85,14 @@ public class LineageRegistrationFrame extends JFrame + "" + ""; + private static final String COPY_LABELS_TOOLTIP = "" + + "Use the found correspondences to copy spot labels from one project to the other.
" + + "
" + + "The correspondences are on a level of cells / branches.
" + + "That is why the all the spots that belong to a cell / branch will get the same label.
" + + "Labels on individual spot can not copied." + + ""; + private static final String COPY_TAGSET_TOOLTIP = "" + "Use the found correspondences to copy a tag set from one project to the other.
" + "
" @@ -176,6 +184,9 @@ public LineageRegistrationFrame( Listener listener ) add( new JLabel( "Sort TrackScheme:" ) ); add( newOperationButton( "project A", SORT_TRACKSCHEME_TOOLTIP, listener::onSortTrackSchemeAClicked ), "split 2" ); add( newOperationButton( "project B", SORT_TRACKSCHEME_TOOLTIP, listener::onSortTrackSchemeBClicked ), "wrap" ); + add( new JLabel( "Copy spot labels:" ) ); + add( newOperationButton( "from A to B ...", COPY_LABELS_TOOLTIP, listener::onCopyLabelsAtoB ), "split 2" ); + add( newOperationButton( "from B to A ...", COPY_LABELS_TOOLTIP, listener::onCopyLabelsBtoA ), "wrap" ); add( new JLabel( "Copy tag set:" ) ); add( newOperationButton( "from A to B ...", COPY_TAGSET_TOOLTIP, listener::onCopyTagSetAtoB ), "split 2" ); add( newOperationButton( "from B to A ...", COPY_TAGSET_TOOLTIP, listener::onCopyTagSetBtoA ), "wrap" ); @@ -408,6 +419,10 @@ public interface Listener void onColorLineagesClicked(); + void onCopyLabelsAtoB(); + + void onCopyLabelsBtoA(); + void onCopyTagSetAtoB(); void onCopyTagSetBtoA(); @@ -446,6 +461,18 @@ public void onColorLineagesClicked() } + @Override + public void onCopyLabelsAtoB() + { + + } + + @Override + public void onCopyLabelsBtoA() + { + + } + @Override public void onCopyTagSetAtoB() { diff --git a/src/main/java/org/mastodon/mamut/tomancak/lineage_registration/LineageRegistrationUtils.java b/src/main/java/org/mastodon/mamut/tomancak/lineage_registration/LineageRegistrationUtils.java index bd6d8713..a6086eb4 100644 --- a/src/main/java/org/mastodon/mamut/tomancak/lineage_registration/LineageRegistrationUtils.java +++ b/src/main/java/org/mastodon/mamut/tomancak/lineage_registration/LineageRegistrationUtils.java @@ -318,4 +318,28 @@ public static void plotAngleAgainstTimepoint( RefDoubleMap< Spot > angles ) frame.pack(); frame.setVisible( true ); } + + public static void copySpotLabelsFromAtoB( final RegisteredGraphs registration ) + { + final ModelGraph graphB = registration.graphB; + final Spot refB = graphB.vertexRef(); + try + { + for ( final Spot spotA : registration.mapAB.keySet() ) + { + boolean hasLabel = !Integer.toString( spotA.getInternalPoolIndex() ).equals( spotA.getLabel() ); + if ( hasLabel ) + { + final Spot spotB = registration.mapAB.get( spotA, refB ); + final RefList< Spot > spotsOfBranchB = BranchGraphUtils.getBranchSpotsAndLinks( graphB, spotB ).getA(); + for ( final Spot spot : spotsOfBranchB ) + spot.setLabel( spotA.getLabel() ); + } + } + } + finally + { + graphB.releaseRef( refB ); + } + } } diff --git a/src/test/java/org/mastodon/mamut/tomancak/lineage_registration/LineageRegistrationUtilsTest.java b/src/test/java/org/mastodon/mamut/tomancak/lineage_registration/LineageRegistrationUtilsTest.java index 8f2017b3..20c3cf4b 100644 --- a/src/test/java/org/mastodon/mamut/tomancak/lineage_registration/LineageRegistrationUtilsTest.java +++ b/src/test/java/org/mastodon/mamut/tomancak/lineage_registration/LineageRegistrationUtilsTest.java @@ -116,6 +116,27 @@ public void testCopyTagSet() assertEquals( set( "B~2 -> B2" ), getTaggedEdges( barB ) ); } + @Test + public void testCopyLabels() + { + // setup: labels for embryoA + for ( Spot spot : embryoA.graph.vertices() ) + spot.setLabel( spot.getLabel() + "_test" ); + // process + LineageRegistrationUtils.copySpotLabelsFromAtoB( registration ); + // test: labels for embryoB + assertEquals( "A_test", embryoB.a.getLabel() ); + assertEquals( "A_test", embryoB.a.outgoingEdges().get( 0 ).getTarget().getLabel() ); + assertEquals( "A1_test", embryoB.a1.getLabel() ); + assertEquals( "A2_test", embryoB.a2.getLabel() ); + assertEquals( "B_test", embryoB.b.getLabel() ); + assertEquals( "B2_test", embryoB.b1.getLabel() ); + assertEquals( "B1_test", embryoB.b2.getLabel() ); + assertEquals( "C_test", embryoB.c.getLabel() ); + assertEquals( "C1_test", embryoB.c1.getLabel() ); + assertEquals( "C2_test", embryoB.c2.getLabel() ); + } + private static < T > Set< T > set( T... values ) { return new HashSet<>( Arrays.asList( values ) );