Skip to content

Commit 2d4fed2

Browse files
committed
Add FourBits Encoder
1 parent cd945e4 commit 2d4fed2

File tree

4 files changed

+378
-0
lines changed

4 files changed

+378
-0
lines changed

.classpath

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<classpath>
33
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
44
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
5+
<classpathentry kind="src" path="src/main/ruby"/>
56
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
67
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
78
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.lifetech.hadoop.bioseq;
2+
3+
abstract public class BioSeqEncoder {
4+
char[] bases = {'A','C','G','T','N'};
5+
char[] colors = {'0','1','2','3','.'};
6+
7+
abstract byte[] encode(byte [] data,int size);
8+
abstract byte[] decode(byte [] data,int size);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
1+
package com.lifetech.hadoop.bioseq;
2+
3+
/*
4+
* codes:
5+
* 'A' => 0000, 0x00
6+
* 'C' => 0001, 0x01
7+
* 'G' => 0010, 0x
8+
* 'T' => 0011,
9+
* 'N' => 0100,
10+
* '0' => 1000,
11+
* '1' => 1001,
12+
* '2' => 1010,
13+
* '3' => 1011,
14+
* '.' => 1100
15+
*
16+
* padding 1111
17+
* 00 => A
18+
* 01 => C
19+
* 02 => G
20+
* 03 => T
21+
* 04 => N
22+
* 08 => 0
23+
* 09 => 1
24+
* 0A => 2
25+
* 0B => 3
26+
* 0C => .
27+
*
28+
* 0T 21
29+
*/
30+
31+
public class FourBitsEncoder extends BioSeqEncoder {
32+
33+
private static byte[] encodingVector={
34+
0x04 , // 0x0
35+
0x04 , // 0x1
36+
0x04 , // 0x2
37+
0x04 , // 0x3
38+
0x04 , // 0x4
39+
0x04 , // 0x5
40+
0x04 , // 0x6
41+
0x04 , // 0x7
42+
0x04 , // 0x8
43+
0x04 , // 0x9
44+
0x04 , // 0xa
45+
0x04 , // 0xb
46+
0x04 , // 0xc
47+
0x04 , // 0xd
48+
0x04 , // 0xe
49+
0x04 , // 0xf
50+
0x04 , // 0x10
51+
0x04 , // 0x11
52+
0x04 , // 0x12
53+
0x04 , // 0x13
54+
0x04 , // 0x14
55+
0x04 , // 0x15
56+
0x04 , // 0x16
57+
0x04 , // 0x17
58+
0x04 , // 0x18
59+
0x04 , // 0x19
60+
0x04 , // 0x1a
61+
0x04 , // 0x1b
62+
0x04 , // 0x1c
63+
0x04 , // 0x1d
64+
0x04 , // 0x1e
65+
0x04 , // 0x1f
66+
0x04 , //
67+
0x04 , // !
68+
0x04 , // "
69+
0x04 , // #
70+
0x04 , // $
71+
0x04 , // %
72+
0x04 , // &
73+
0x04 , // '
74+
0x04 , // (
75+
0x04 , // )
76+
0x04 , // *
77+
0x04 , // +
78+
0x04 , // ,
79+
0x04 , // -
80+
0x0c , // .
81+
0x04 , // /
82+
0x08 , // 0
83+
0x09 , // 1
84+
0x0a , // 2
85+
0x0b , // 3
86+
0x04 , // 4
87+
0x04 , // 5
88+
0x04 , // 6
89+
0x04 , // 7
90+
0x04 , // 8
91+
0x04 , // 9
92+
0x04 , // :
93+
0x04 , // ;
94+
0x04 , // <
95+
0x04 , // =
96+
0x04 , // >
97+
0x04 , // ?
98+
0x04 , // @
99+
0x00 , // A
100+
0x04 , // B
101+
0x01 , // C
102+
0x04 , // D
103+
0x04 , // E
104+
0x04 , // F
105+
0x02 , // G
106+
0x04 , // H
107+
0x04 , // I
108+
0x04 , // J
109+
0x04 , // K
110+
0x04 , // L
111+
0x04 , // M
112+
0x04 , // N
113+
0x04 , // O
114+
0x04 , // P
115+
0x04 , // Q
116+
0x04 , // R
117+
0x04 , // S
118+
0x03 , // T
119+
0x04 , // U
120+
0x04 , // V
121+
0x04 , // W
122+
0x04 , // X
123+
0x04 , // Y
124+
0x04 , // Z
125+
0x04 , // [
126+
0x04 , // \
127+
0x04 , // ]
128+
0x04 , // ^
129+
0x04 , // _
130+
0x04 , // `
131+
0x00 , // a
132+
0x04 , // b
133+
0x01 , // c
134+
0x04 , // d
135+
0x04 , // e
136+
0x04 , // f
137+
0x02 , // g
138+
0x04 , // h
139+
0x04 , // i
140+
0x04 , // j
141+
0x04 , // k
142+
0x04 , // l
143+
0x04 , // m
144+
0x04 , // n
145+
0x04 , // o
146+
0x04 , // p
147+
0x04 , // q
148+
0x04 , // r
149+
0x04 , // s
150+
0x03 , // t
151+
0x04 , // u
152+
0x04 , // v
153+
0x04 , // w
154+
0x04 , // x
155+
0x04 , // y
156+
0x04 , // z
157+
0x04 , // {
158+
0x04 , // |
159+
0x04 , // }
160+
0x04 , // ~
161+
0x04 , // 0x7f
162+
0x04 , // 0x80
163+
0x04 , // 0x81
164+
0x04 , // 0x82
165+
0x04 , // 0x83
166+
0x04 , // 0x84
167+
0x04 , // 0x85
168+
0x04 , // 0x86
169+
0x04 , // 0x87
170+
0x04 , // 0x88
171+
0x04 , // 0x89
172+
0x04 , // 0x8a
173+
0x04 , // 0x8b
174+
0x04 , // 0x8c
175+
0x04 , // 0x8d
176+
0x04 , // 0x8e
177+
0x04 , // 0x8f
178+
0x04 , // 0x90
179+
0x04 , // 0x91
180+
0x04 , // 0x92
181+
0x04 , // 0x93
182+
0x04 , // 0x94
183+
0x04 , // 0x95
184+
0x04 , // 0x96
185+
0x04 , // 0x97
186+
0x04 , // 0x98
187+
0x04 , // 0x99
188+
0x04 , // 0x9a
189+
0x04 , // 0x9b
190+
0x04 , // 0x9c
191+
0x04 , // 0x9d
192+
0x04 , // 0x9e
193+
0x04 , // 0x9f
194+
0x04 , // 0xa0
195+
0x04 , // 0xa1
196+
0x04 , // 0xa2
197+
0x04 , // 0xa3
198+
0x04 , // 0xa4
199+
0x04 , // 0xa5
200+
0x04 , // 0xa6
201+
0x04 , // 0xa7
202+
0x04 , // 0xa8
203+
0x04 , // 0xa9
204+
0x04 , // 0xaa
205+
0x04 , // 0xab
206+
0x04 , // 0xac
207+
0x04 , // 0xad
208+
0x04 , // 0xae
209+
0x04 , // 0xaf
210+
0x04 , // 0xb0
211+
0x04 , // 0xb1
212+
0x04 , // 0xb2
213+
0x04 , // 0xb3
214+
0x04 , // 0xb4
215+
0x04 , // 0xb5
216+
0x04 , // 0xb6
217+
0x04 , // 0xb7
218+
0x04 , // 0xb8
219+
0x04 , // 0xb9
220+
0x04 , // 0xba
221+
0x04 , // 0xbb
222+
0x04 , // 0xbc
223+
0x04 , // 0xbd
224+
0x04 , // 0xbe
225+
0x04 , // 0xbf
226+
0x04 , // 0xc0
227+
0x04 , // 0xc1
228+
0x04 , // 0xc2
229+
0x04 , // 0xc3
230+
0x04 , // 0xc4
231+
0x04 , // 0xc5
232+
0x04 , // 0xc6
233+
0x04 , // 0xc7
234+
0x04 , // 0xc8
235+
0x04 , // 0xc9
236+
0x04 , // 0xca
237+
0x04 , // 0xcb
238+
0x04 , // 0xcc
239+
0x04 , // 0xcd
240+
0x04 , // 0xce
241+
0x04 , // 0xcf
242+
0x04 , // 0xd0
243+
0x04 , // 0xd1
244+
0x04 , // 0xd2
245+
0x04 , // 0xd3
246+
0x04 , // 0xd4
247+
0x04 , // 0xd5
248+
0x04 , // 0xd6
249+
0x04 , // 0xd7
250+
0x04 , // 0xd8
251+
0x04 , // 0xd9
252+
0x04 , // 0xda
253+
0x04 , // 0xdb
254+
0x04 , // 0xdc
255+
0x04 , // 0xdd
256+
0x04 , // 0xde
257+
0x04 , // 0xdf
258+
0x04 , // 0xe0
259+
0x04 , // 0xe1
260+
0x04 , // 0xe2
261+
0x04 , // 0xe3
262+
0x04 , // 0xe4
263+
0x04 , // 0xe5
264+
0x04 , // 0xe6
265+
0x04 , // 0xe7
266+
0x04 , // 0xe8
267+
0x04 , // 0xe9
268+
0x04 , // 0xea
269+
0x04 , // 0xeb
270+
0x04 , // 0xec
271+
0x04 , // 0xed
272+
0x04 , // 0xee
273+
0x04 , // 0xef
274+
0x04 , // 0xf0
275+
0x04 , // 0xf1
276+
0x04 , // 0xf2
277+
0x04 , // 0xf3
278+
0x04 , // 0xf4
279+
0x04 , // 0xf5
280+
0x04 , // 0xf6
281+
0x04 , // 0xf7
282+
0x04 , // 0xf8
283+
0x04 , // 0xf9
284+
0x04 , // 0xfa
285+
0x04 , // 0xfb
286+
0x04 , // 0xfc
287+
0x04 , // 0xfd
288+
0x04 , // 0xfe
289+
0x04 // 0xff
290+
};
291+
292+
@Override
293+
public byte[] decode(byte[] data, int size) {
294+
int newsize = (size << 1);
295+
// test for padding
296+
if ((data[size-1] & 0x0f) == 0x0f) {
297+
newsize--;
298+
}
299+
byte[] r = new byte[newsize];
300+
301+
for(int i=0;i<size;i++) {
302+
byte fst = (byte) ((data[i] >> 4) & 0x07);
303+
byte snd = (byte) (data[i] & 0x07);
304+
System.out.printf("D: %d fst = %x %d snd = %x\n",i<<1,fst,(i<<1)+1,snd);
305+
306+
r[i<<1] = (byte) ((data[i] & 0x80) == 0x80 ? colors[fst] : bases[fst]);
307+
if (snd == 7) break;
308+
r[(i<<1) + 1] = (byte) ((data[i] & 0x08) == 0x08 ? colors[snd] : bases[snd]);
309+
}
310+
return r;
311+
}
312+
313+
public void printBytes(byte[] x,int size) {
314+
for(int i=0;i<size;i++) {
315+
System.out.printf("%x ",x[i]);
316+
}
317+
System.out.print("\n");
318+
}
319+
320+
@Override
321+
public byte[] encode(byte[] data, int size) {
322+
int newsize = ((size+1) >> 1);
323+
byte[] r = new byte[newsize];
324+
325+
for(int i=0;i<size;i+=2) {
326+
r[i >> 1] = (byte) (encodingVector[data[i]] <<4);
327+
328+
if (i+1 < size) {
329+
r[i >> 1] += (byte) (encodingVector[data[i+1]]);
330+
System.out.printf("E: %d %c %c r=%x\n",i,data[i],data[i+1],r[i>>1]);
331+
} else {
332+
r[i >> 1] += 0x0f;
333+
System.out.printf("E: %d %c pad r=%x\n",i,data[i],r[i>>1]);
334+
}
335+
}
336+
return r;
337+
}
338+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.lifetech.hadoop.bioseq;
2+
3+
import org.junit.Test;
4+
import static org.junit.Assert.assertEquals;
5+
6+
public class FourBitsEncoderTest {
7+
8+
private FourBitsEncoder encoder = new FourBitsEncoder();
9+
10+
@Test
11+
public void test1() {
12+
byte[] a = "T012".getBytes();
13+
byte[] b = encoder.encode(a, 4);
14+
encoder.printBytes(b,2);
15+
byte[] c = encoder.decode(b,2);
16+
System.out.println(new String(c));
17+
assertEquals(new String(a),new String(c));
18+
}
19+
20+
@Test
21+
public void test2() {
22+
byte[] a = "T0123".getBytes();
23+
byte[] b = encoder.encode(a, 5);
24+
encoder.printBytes(b,3);
25+
byte[] c = encoder.decode(b,3);
26+
System.out.println(new String(a) + " [" + new String(c) + "|");
27+
assertEquals(new String(a),new String(c));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)