Skip to content

Commit a6d9217

Browse files
committed
Add MultiEmbryoRegistration
1 parent 9c03ba2 commit a6d9217

1 file changed

Lines changed: 130 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)