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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package org.lindenb.tinytools;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;

import org.lindenb.awt.ColorUtils;
import org.lindenb.io.IOUtils;
import org.lindenb.sw.vocabulary.SVG;
import org.lindenb.sw.vocabulary.XLINK;
import org.lindenb.util.Cast;
import org.lindenb.util.Compilation;
import org.lindenb.xml.XMLUtilities;



/**
* Program Drawin a HilbertSequence for a set of Gene
* Mostly inspired by Gareth Palidwor's post at
* http://www.palidwor.com/blog/?p=123
*
* and the code was copied from the Wikpedia page
* http://en.wikipedia.org/wiki/Hilbert_curve
*
*/
public class HilbertSequence
{
private static class Gene
{
String name;
int start;
int end;
}

private List<Gene> genes= new ArrayList<Gene>();
/** with/height of the final picture */
private int imageSize=512;
/** size of an edge */
private int dist=imageSize;
/** size (in pb) of an edge */
private double d_base=0;
/** last index of the position in the genome */
private double prev_base=0;
/** ouput stream */
private PrintStream out;
/** last time we plot X */
private int prev_x=0;
/** last time we plot Y */
private int prev_y=0;
/** genome size */
private int genomeSize=-1;
/** level of recursion */
private int recursionLevel=5;
/** current segment index (for coloring )*/
private int segmentIndex=0;

/** ctor */
private HilbertSequence()
{
}

/** do our stuff , algorithm mostly copied from wikipedia */
void paint(PrintStream writer) throws IOException
{
this.out=writer;
this.dist=imageSize;
this.prev_base=0;
Collections.sort(this.genes, new Comparator<Gene>()
{
@Override
public int compare(Gene o1, Gene o2) {
return o1.start-o2.start;
}
});
if(this.genomeSize<2)
{
this.genomeSize=2;
for(Gene g:this.genes) this.genomeSize=Math.max(this.genomeSize, g.end);
}


out.println("<?xml version=\"1.0\"?>");
out.println(SVG.DOCTYPE);
out.println("<svg xmlns=\""+SVG.NS+"\" " +
" xmlns:xlink=\""+XLINK.NS+"\" version=\"1.1\" "+
" width=\""+imageSize+"\" "+
" height=\""+imageSize+"\" "+
" style='stroke:black;' "+
">");

for (int i=recursionLevel; i>0; i--)
{
dist /= 2;
}
//4 16 64 256 1023
d_base= ((double)genomeSize/Math.pow(4,recursionLevel));

prev_x= dist/2;
prev_y=dist/2;
segmentIndex=0;
HilbertU(recursionLevel); // start recursion

out.println("</svg>");
out.flush();
out=null;
}
/**
* Draw a line on the picture
* @param deltaX
* @param deltaY
*/
private void lineRel(int deltaX, int deltaY)
{
double stepRatio= (double)segmentIndex/Math.pow(4,recursionLevel);
int r= 255-(int)(255.0*stepRatio);
int g= (int)(155.0*stepRatio);
int b= 60+(int)(155.0*(1.0-stepRatio));


int x2= prev_x + deltaX;
int y2= prev_y + deltaY;
out.print(
"<line x1='"+prev_x+"' y1='"+prev_y+"' style='stroke:"+ColorUtils.toRGB(new Color(r,g,b))+"' "+
" x2='"+x2+"' y2='"+y2+"'/>"
);
int chromStart=(int)prev_base;
int chromEnd=chromStart+(int)d_base;

//look for a gene in this segment
for(Gene gene:this.genes)
{
if(gene.end< chromStart || gene.start >chromEnd) continue;
int geneStart = Math.max(chromStart, gene.start);
int geneEnd = Math.min(chromEnd, gene.end);
double r1= ((geneStart-chromStart)/d_base);
double r2= ((geneEnd-chromStart)/d_base);
double gx = prev_x+(x2-prev_x)*r1;
double gy = prev_y+(y2-prev_y)*r1;
out.print("<line x1='"+gx+"' y1='"+gy+"' ");

gx = prev_x+(x2-prev_x)*r2;
gy = prev_y+(y2-prev_y)*r2;
out.print(" x2='"+gx+"' y2='"+gy+"' style='stroke-width:4;stroke:red; fill:yellow;'" +
" title='"+ XMLUtilities.escape(gene.name+":"+gene.start+"-"+gene.end)+"'/>"
);
}

prev_x =x2;
prev_y =y2;
prev_base+= d_base;
++segmentIndex;
}
// Make U-shaped curve at this scale:
private void HilbertU(int level)
{
if (level > 0) {
HilbertD(level-1); this.lineRel(0, dist);
HilbertU(level-1); this.lineRel(dist, 0);
HilbertU(level-1); this.lineRel(0, -dist);
HilbertC(level-1);
}
}

// Make D-shaped (really "]" shaped) curve at this scale:
private void HilbertD(int level) {
if (level > 0) {
HilbertU(level-1); this.lineRel(dist, 0);
HilbertD(level-1); this.lineRel(0, dist);
HilbertD(level-1); this.lineRel(-dist, 0);
HilbertA(level-1);
}
}

// Make C-shaped (really "[" shaped) curve at this scale:
private void HilbertC(int level) {
if (level > 0) {
HilbertA(level-1); this.lineRel(-dist, 0);
HilbertC(level-1); this.lineRel(0, -dist);
HilbertC(level-1); this.lineRel(dist, 0);
HilbertU(level-1);
}
}

// Make A-shaped (really "⊓" shaped) curve at this scale:
private void HilbertA(int level) {
if (level > 0) {
HilbertC(level-1); this.lineRel(0, -dist);
HilbertA(level-1); this.lineRel(-dist, 0);
HilbertA(level-1); this.lineRel(0, dist);
HilbertD(level-1);
}
}

private void readGeneList(BufferedReader in) throws IOException
{
final Pattern tab=Pattern.compile("[\t]");
String line=null;
while((line=in.readLine())!=null)
{
if(line.trim().length()==0 || line.startsWith("#")) continue;
String token[]=tab.split(line);
if(token.length<3 || token[0].trim().length()==0 ||
!Cast.Integer.isA(token[1]) ||
!Cast.Integer.isA(token[2])
)
{
System.err.println("Bad input in "+line);
continue;
}
Gene g= new Gene();
g.name=token[0];
g.start=Cast.Integer.cast(token[1]) ;
g.end=Cast.Integer.cast(token[2]);
this.genes.add(g);
}
}

public static void main(String[] args)
{
try
{
HilbertSequence app=new HilbertSequence();
File fileout=null;
int optind=0;
while(optind< args.length)
{
if(args[optind].equals("-h"))
{
System.err.println(Compilation.getLabel());
System.err.println("Displays a SVG Hilbert Curve for a DNA sequence, displaying the genes.");
System.err.println("Idea from Gareth Palidwor.");
System.err.println("Algorithm from wikipedia.");
System.err.println(" -o <file> output file");
System.err.println(" -s <int> image size");
System.err.println(" -L <int> genome size (default=gene.max-start");
System.err.println(" -X <int> level of recursion");
System.err.println("<input|stdin> (= tab delim file: name,start,end");
}
else if(args[optind].equals("-s"))
{
app.imageSize= Integer.parseInt(args[++optind]);

}
else if(args[optind].equals("-L"))
{
app.genomeSize= Integer.parseInt(args[++optind]);
}
else if(args[optind].equals("-X"))
{
app.recursionLevel= Integer.parseInt(args[++optind]);
}
else if(args[optind].equals("-o"))
{
fileout=new File(args[++optind]);
}
else if(args[optind].equals("--"))
{
optind++;
break;
}
else if(args[optind].startsWith("-"))
{
System.err.println("Unknown option "+args[optind]);
}
else
{
break;
}
++optind;
}
if(app.recursionLevel<1 || app.recursionLevel>20)
{
System.err.println("Invalid recursion Level");
return;
}
if(app.imageSize<1)
{
System.err.println("Invalid image size");
return;
}

if(optind==args.length)
{
app.readGeneList(new BufferedReader(new InputStreamReader(System.in)));
}
else
{
while(optind< args.length)
{
BufferedReader r= IOUtils.openFile(new File(args[optind++]));
app.readGeneList(r);
r.close();
}
}

PrintStream out= System.out;
if(fileout!=null)
{
out=new PrintStream(fileout);
}
app.paint(out);
if(fileout!=null)
{
out.flush();
out.close();
}
}
catch(Throwable err)
{
err.printStackTrace();
}
}

}

Change log

r229 by plindenbaum on Feb 21, 2009   Diff
hilbert
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 8474 bytes, 322 lines
Powered by Google Project Hosting