-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSTDataText.java
More file actions
126 lines (101 loc) · 3.69 KB
/
Copy pathSTDataText.java
File metadata and controls
126 lines (101 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package data;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import net.imglib2.RandomAccess;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.img.Img;
import net.imglib2.img.array.ArrayImgs;
import net.imglib2.img.cell.CellImgFactory;
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.util.Pair;
/**
* A text-input based implementation that can be used to convert to e.g. N5.
* <p>
* Computes the ImgLib2 datastructures on demand from a list of locations and a HashMap of geneName to expression values
*
*
* @author spreibi
*
*/
public class STDataText extends STDataImgLib2
{
public STDataText( final List< Pair< double[], String > > locations, final HashMap< String, double[] > exprValues )
{
super( create( locations, exprValues ) );
}
protected static STDataImgLib2Factory create( final List< Pair< double[], String > > locations, final HashMap< String, double[] > exprValues )
{
final STDataImgLib2Factory factory = new STDataImgLib2Factory();
factory.geneNames = new ArrayList<>( exprValues.keySet() );
Collections.sort( factory.geneNames );
factory.geneLookup = new HashMap<>();
for ( int i = 0; i < factory.geneNames.size(); ++i )
factory.geneLookup.put( factory.geneNames.get( i ), i );
factory.barcodes = locations.stream().map(Pair::getB).collect( Collectors.toList() );
factory.locations = locationsToImgLib2( locations );
factory.exprValues = exprValuesToImgLib2( factory.geneNames, exprValues );
return factory;
}
/**
* @param locations - the list of sequenced locations
* @return - a 2d datastructure that holds all sequenced locations, size: [numLocations x numDimensions]
*/
public static Img< DoubleType > locationsToImgLib2( final List< Pair< double[], String > > locations )
{
final int n = locations.get( 0 ).getA().length;
final int numLocations = locations.size();
final Img< DoubleType > img = ArrayImgs.doubles(numLocations, n);
setLocations( locations.stream().map(Pair::getA).collect( Collectors.toList() ), img );
return img;
}
public static void setLocations( final List< double[] > locations, final RandomAccessibleInterval< DoubleType > img )
{
final int numLocations = (int)img.dimension( 0 );
final int n = (int)img.dimension( 1 );
// TODO: use cursor
final RandomAccess< DoubleType > ra = img.randomAccess();
for ( int i = 0; i < numLocations; ++i )
{
ra.setPosition( i, 0 );
ra.setPosition( 0, 1 );
final double[] coord = locations.get( i );
for ( int d = 0; d < n; ++d )
{
ra.get().set( coord[ d ] );
if ( d != n - 1 )
ra.fwd( 1 );
}
}
}
/**
* @param geneList - the order of the genes defines the order in the imglib2 img
* @param exprValues - a map that links geneNames to their values
*
* @return a 2d datastructure that holds all expression values, size: [numGenes x numLocations]
*/
public static Img< DoubleType > exprValuesToImgLib2( final List< String > geneList, final Map< String, double[] > exprValues )
{
final long numGenes = geneList.size();
final long numLocations = exprValues.values().iterator().next().length;
final Img< DoubleType > img = new CellImgFactory<>( new DoubleType(), 128 ).create(numGenes, numLocations);
// TODO: use cursor
final RandomAccess< DoubleType > ra = img.randomAccess();
for ( int i = 0; i < numGenes; ++i )
{
ra.setPosition( i, 0 );
ra.setPosition( 0, 1 );
final double[] values = exprValues.get( geneList.get( i ) );
for ( int j = 0; j < numLocations; ++j )
{
ra.get().set( values[ j ] );
if ( j != numLocations - 1 )
ra.fwd( 1 );
}
}
return img;
}
}