My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package fr.inserm.umr915.core.bio.ucsc;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;


/**
* inspired from Kent, et. al. Genome Research 2002.12:996-1006
* http://genome.cshlp.org/content/12/6/996.full
* http://genomewiki.ucsc.edu/index.php/Bin_indexing_system
* Bin
*/
public abstract class Bin
{



/** original Jim Kent's implementation */
public static int binFromRangeStandard(int start, int end)

{
final int binOffsets[] =
{
512+64+8+1,
64+8+1,
8+1,
1,
0};


/** How much to shift to get to finest bin. */
final int _binFirstShift=17;
/** How much to shift to get to next larger bin.*/
final int _binNextShift=3;

int startBin = start;
int endBin = end-1;


//System.out.println("1 startBin="+startBin+" endBin="+endBin);

startBin >>= _binFirstShift;
endBin >>= _binFirstShift;


//System.out.println("2 startBin="+startBin+" endBin="+endBin);

for(int binOffset : binOffsets)
{
if (startBin == endBin)
{
return binOffset + startBin;
}
startBin >>= _binNextShift;
endBin >>= _binNextShift;
// System.out.println("3 startBin="+startBin+" endBin="+endBin+" binOffset:"+binOffset);
}
throw new IllegalArgumentException("start "+start+", end "+ end+" out of range in findBin (max is 512M)");
}

/** to be implemented */
/*private class BinIterator
implements Iterator<Integer>
{
private int chromStart;
private int chromEnd;
private int level=0;

@Override
public Integer next()
{
return null;
}
@Override
public boolean hasNext()
{

return false;
}

@Override
public void remove()
{
throw new UnsupportedOperationException();
}
}*/

private Set<Integer> collectBins(
final int chromStart,
final int chromEnd,
int binId,
int level,
int binRowStart,
int rowIndex,
int binRowCount,
int genomicPos,
int genomicLength,
Set<Integer> set
)
{
set.add(binId);
if(level>=getMaxLevel()) return set;

int childLength=genomicLength/getChildrenCount();
int childBinRowCount=binRowCount*getChildrenCount();
int childRowBinStart=binRowStart+binRowCount;
int firstChildIndex=rowIndex*getChildrenCount();
int firstChildBin=childRowBinStart+firstChildIndex;
for(int i=0;i< getChildrenCount();++i)
{
int childStart=genomicPos+i*childLength;

if( chromStart>(childStart+childLength) ||
chromEnd<childStart )
{
continue;
}
collectBins(
chromStart,
chromEnd,
firstChildBin+i,
level+1,
childRowBinStart,
firstChildIndex+i,
childBinRowCount,
childStart,
childLength,
set
);
}
return set;
}


private int calcBin(
final int chromStart,
final int chromEnd,
int binId,
int level,
int binRowStart,
int rowIndex,
int binRowCount,
int genomicPos,
int genomicLength
)
{

if(
chromStart>=genomicPos &&
chromEnd<= (genomicPos+genomicLength))
{
if(level>=getMaxLevel()) return binId;

int childLength=genomicLength/getChildrenCount();
int childBinRowCount=binRowCount*getChildrenCount();
int childRowBinStart=binRowStart+binRowCount;
int firstChildIndex=rowIndex*getChildrenCount();
int firstChildBin=childRowBinStart+firstChildIndex;
for(int i=0;i< getChildrenCount();++i)
{
//System.out.println("i:"+i+"=>"+(firstChildBin+i));
//System.out.println(" "+(genomicPos+i*childLength)+" <=" +chromStart+" < "+chromEnd+"<"+(genomicPos+(i+1)*childLength));
int n= calcBin(
chromStart,
chromEnd,
firstChildBin+i,
level+1,
childRowBinStart,
firstChildIndex+i,
childBinRowCount,
genomicPos+i*childLength,
childLength
);
if(n!=-1)
{
//System.out.println("returning n="+n);
return n;
}
}
return binId;
}

return -1;
}

/** length for level 0 */
protected abstract int getMaxGenomicLengthLevel();
/** maximum level in Jim Kent's algorithm */
protected abstract int getMaxLevel();
/** how many children for one node ? */
protected abstract int getChildrenCount();


/** Given start,end in chromosome coordinates assign it
* a bin. There's a bin for each 128k segment, for each
* 1M segment, for each 8M segment, for each 64M segment,
* and for each chromosome (which is assumed to be less than
* 512M.) A range goes into the smallest bin it will fit in. */
public int getBin(int chromStart,int chromEnd)
{
int genomicLength=getMaxGenomicLengthLevel();
return calcBin(chromStart,chromEnd,0,0,0,0,1,0,genomicLength);
}

/** returns all the bin-ids overlapping the given range */
public Set<Integer> getBins(int chromStart,int chromEnd)
{
int genomicLength=getMaxGenomicLengthLevel();
return collectBins(chromStart,chromEnd,0,0,0,0,1,0,genomicLength,new HashSet<Integer>());
}


public Iterator<Integer> getIterator(int chromStart,int chromEnd)
{
//TODO , use an iterator rather than calling getBins
return getBins(chromStart,chromEnd).iterator();
}


/** default used by ucsc level 0: 512Mo */
public final static Bin Ucsc512=new Bin()
{
@Override
protected int getChildrenCount()
{
return 8;
}

@Override
protected int getMaxLevel()
{
return 4;
}

@Override
protected int getMaxGenomicLengthLevel()
{
return 536870912;//2^29
}
};

public static void main(String[] args)
{
try
{
System.out.println(binFromRangeStandard(10003,10004));
System.out.println(Ucsc512.getBin(10003,10004));
System.out.print("(");
for(int i:Ucsc512.getBins(10000,20000))
{
System.out.print(i);
System.out.print(",");
}
System.out.println(")");
/*for(int i=0;false && i< 536870912-1;++i )
{
int n1;
int n2;
if((n1=Ucsc512.getBin(i,i+1))!=(n2=binFromRangeStandard(i,i+1)))
{
System.err.println(i+"\t"+n1+"\t"+n2);
}
}*/
System.out.println("Done.");
}
catch (Exception e)
{
e.printStackTrace();
}
}

}

Change log

r110 by plindenbaum.u915 on May 28, 2010   Diff
cont
Go to: 
Project members, sign in to write a code review

Older revisions

r107 by plindenbaum.u915 on May 27, 2010   Diff
cont
All revisions of this file

File info

Size: 6107 bytes, 273 lines
Powered by Google Project Hosting