Skip to content

Commit 2ec9d87

Browse files
committed
Add MultiEmbryoRegistration
1 parent 9c03ba2 commit 2ec9d87

1 file changed

Lines changed: 140 additions & 0 deletions

File tree

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package org.mastodon.mamut.tomancak.lineage_registration;
2+
3+
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.stream.Collectors;
10+
import java.util.stream.IntStream;
11+
12+
import org.apache.commons.lang3.tuple.Pair;
13+
import org.mastodon.collection.RefIntMap;
14+
import org.mastodon.collection.RefRefMap;
15+
import org.mastodon.collection.ref.RefIntHashMap;
16+
import org.mastodon.mamut.ProjectModel;
17+
import org.mastodon.mamut.io.ProjectLoader;
18+
import org.mastodon.mamut.model.Link;
19+
import org.mastodon.mamut.model.Model;
20+
import org.mastodon.mamut.model.Spot;
21+
import org.mastodon.mamut.tomancak.lineage_registration.spatial_registration.SpatialRegistrationMethod;
22+
import org.mastodon.mamut.views.trackscheme.MamutBranchViewTrackScheme;
23+
import org.mastodon.model.tag.ObjTagMap;
24+
import org.mastodon.model.tag.TagSetStructure;
25+
import org.mastodon.util.TagSetUtils;
26+
import org.scijava.Context;
27+
28+
import mpicbg.spim.data.SpimDataException;
29+
30+
/**
31+
* Do pairwise {@link LineageRegistrationAlgorithm lineage registration} of multiple embryos.
32+
* Tag the cells that are uniquely identified by the registration, without contradiction.
33+
*/
34+
public class MultiEmbryoRegistration
35+
{
36+
public static void main( final String... args )
37+
{
38+
final List< String > projectPaths = Arrays.asList(
39+
"/home/arzt/Datasets/Mette/E1.mastodon",
40+
"/home/arzt/Datasets/Mette/E2.mastodon",
41+
"/home/arzt/Datasets/Mette/E3.mastodon"
42+
);
43+
try (final Context context = new Context())
44+
{
45+
final List< ProjectModel > projectModels = projectPaths.stream().map( path -> {
46+
try
47+
{
48+
return ProjectLoader.open( path, context, false, true );
49+
}
50+
catch ( IOException | SpimDataException e )
51+
{
52+
throw new RuntimeException( e );
53+
}
54+
} ).collect( Collectors.toList() );
55+
final List< Model > models = projectModels.stream().map( ProjectModel::getModel ).collect( Collectors.toList() );
56+
// FIXME: models.forEach( model -> ImproveAnglesDemo.removeBackEdges( model.getGraph() ) );
57+
createTags( models.get( 0 ), computeAgreement( models ) );
58+
projectModels.get( 0 ).getWindowManager().createView( MamutBranchViewTrackScheme.class );
59+
}
60+
}
61+
62+
private static RefIntMap< Spot > computeAgreement( final List< Model > models )
63+
{
64+
final Map< Pair< Model, Model >, RefRefMap< Spot, Spot > > registrations = new HashMap<>();
65+
for ( final Pair< Model, Model > pair : makePairs( models ) )
66+
registrations.put( pair, register( pair.getLeft(), pair.getRight() ) );
67+
68+
final Model firstModel = models.get( 0 );
69+
final RefIntMap< Spot > agreement = new RefIntHashMap<>( firstModel.getGraph().vertices().getRefPool(), 0 );
70+
71+
final List< Model > otherModels = models.subList( 1, models.size() );
72+
for ( final Pair< Model, Model > pair : makePairs( otherModels ) )
73+
{
74+
final Model otherModelA = pair.getLeft();
75+
final Model otherModelB = pair.getRight();
76+
final RefRefMap< Spot, Spot > registrationA = registrations.get( Pair.of( firstModel, otherModelA ) );
77+
final RefRefMap< Spot, Spot > registrationB = registrations.get( Pair.of( firstModel, otherModelB ) );
78+
final RefRefMap< Spot, Spot > registrationAB = registrations.get( pair );
79+
incrementAgreement( agreement, registrationA, registrationB, registrationAB );
80+
}
81+
82+
return agreement;
83+
}
84+
85+
private static void incrementAgreement( final RefIntMap< Spot > agreement, final RefRefMap< Spot, Spot > registrationA, final RefRefMap< Spot, Spot > registrationB,
86+
final RefRefMap< Spot, Spot > registrationAB )
87+
{
88+
final Spot refA = registrationA.createValueRef();
89+
final Spot refB = registrationB.createValueRef();
90+
final Spot refB2 = registrationAB.createValueRef();
91+
for ( final Spot key : registrationA.keySet() )
92+
if ( registrationB.containsKey( key ) )
93+
{
94+
final Spot spotA = registrationA.get( key, refA );
95+
final Spot spotB = registrationB.get( key, refB );
96+
final Spot crossSpotB = registrationAB.get( spotA, refB2 );
97+
final boolean correct = spotB.equals( crossSpotB );
98+
if ( correct )
99+
agreement.put( key, agreement.get( key ) + 1 );
100+
}
101+
}
102+
103+
private static RefRefMap< Spot, Spot > register( final Model firstModel, final Model otherModel )
104+
{
105+
return LineageRegistrationAlgorithm.run( firstModel, 0, otherModel, 0, SpatialRegistrationMethod.DYNAMIC_ROOTS ).mapAB;
106+
}
107+
108+
private static List< Pair< Model, Model > > makePairs( final List< Model > otherModels )
109+
{
110+
final List< Pair< Model, Model > > spokes = new ArrayList<>();
111+
for ( int i = 0; i < otherModels.size(); i++ )
112+
for ( int j = i + 1; j < otherModels.size(); j++ )
113+
spokes.add( Pair.of( otherModels.get( i ), otherModels.get( j ) ) );
114+
return spokes;
115+
}
116+
117+
private static void createTags( final Model firstModel, final RefIntMap< Spot > agreement )
118+
{
119+
int max = 0;
120+
for ( final Spot spot : agreement.keySet() )
121+
max = Math.max( max, agreement.get( spot ) );
122+
final List< Pair< String, Integer > > correct =
123+
IntStream.rangeClosed( 0, max ).mapToObj( i -> Pair.of( String.valueOf( i ), Glasbey.GLASBEY[ i + 1 ] ) ).collect( Collectors.toList() );
124+
125+
final TagSetStructure.TagSet tagSet = TagSetUtils.addNewTagSetToModel( firstModel, "UnifiedEmbryo", correct );
126+
final List< TagSetStructure.Tag > tags = tagSet.getTags();
127+
final ObjTagMap< Link, TagSetStructure.Tag > edgeTags = firstModel.getTagSetModel().getEdgeTags().tags( tagSet );
128+
for ( final Spot spot : agreement.keySet() )
129+
{
130+
final int value = agreement.get( spot );
131+
final TagSetStructure.Tag tag = tags.get( max - value );
132+
TagSetUtils.tagBranch( firstModel, tagSet, tag, spot );
133+
if ( spot.incomingEdges().size() == 1 )
134+
{
135+
final Link link = spot.incomingEdges().iterator().next();
136+
edgeTags.set( link, tag );
137+
}
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)