forked from davecom/ClassicComputerScienceProblemsInJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompressedGene.java
More file actions
98 lines (90 loc) · 2.96 KB
/
CompressedGene.java
File metadata and controls
98 lines (90 loc) · 2.96 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
// CompressedGene.java
// From Classic Computer Science Problems in Java Chapter 1
// Copyright 2020 David Kopec
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package chapter1;
import java.util.BitSet;
public class CompressedGene {
private BitSet bitSet;
private int length;
public CompressedGene(String gene) {
compress(gene);
}
private void compress(String gene) {
length = gene.length();
// reserve enough capacity for all of the bits
bitSet = new BitSet(length * 2);
// convert to upper case for consistency
final String upperGene = gene.toUpperCase();
// convert String to bit representation
for (int i = 0; i < length; i++) {
final int firstLocation = 2 * i;
final int secondLocation = 2 * i + 1;
switch (upperGene.charAt(i)) {
case 'A': // 00 are next two bits
bitSet.set(firstLocation, false);
bitSet.set(secondLocation, false);
break;
case 'C': // 01 are next two bits
bitSet.set(firstLocation, false);
bitSet.set(secondLocation, true);
break;
case 'G': // 10 are next two bits
bitSet.set(firstLocation, true);
bitSet.set(secondLocation, false);
break;
case 'T': // 11 are next two bits
bitSet.set(firstLocation, true);
bitSet.set(secondLocation, true);
break;
default:
throw new IllegalArgumentException("The provided gene String contains characters other than ACGT");
}
}
}
public String decompress() {
if (bitSet == null) {
return "";
}
// create a mutable place for characters with right capacity
StringBuilder builder = new StringBuilder(length);
for (int i = 0; i < (length * 2); i += 2) {
final int firstBit = (bitSet.get(i) ? 1 : 0);
final int secondBit = (bitSet.get(i + 1) ? 1 : 0);
final int lastBits = firstBit << 1 | secondBit;
switch (lastBits) {
case 0b00: // 00 is 'A'
builder.append('A');
break;
case 0b01: // 01 is 'C'
builder.append('C');
break;
case 0b10: // 10 is 'G'
builder.append('G');
break;
case 0b11: // 11 is 'T'
builder.append('T');
break;
}
}
return builder.toString();
}
public static void main(String[] args) {
final String original = "TAGGGATTAACCGTTATATATATATAGCCATGGATCGATTATATAGGGATTAACCGTTATATATATATAGCCATGGATCGATTATA";
CompressedGene compressed = new CompressedGene(original);
final String decompressed = compressed.decompress();
System.out.println(decompressed);
System.out.println("original is the same as decompressed: " + original.equalsIgnoreCase(decompressed));
}
}