Debian Codegraph @

current/align2/CompressString.java

raw symbols SCIP
1 package align2;
2
3 import dna.ChromosomeArray;
4 import dna.Data;
5 import structures.IntList;
6
7 public class CompressString {
8
9 public static void main(String[] args){
10
11 String s;
12
13 s=compressRepeats(args[0].getBytes(), 1);
14 s=compress(args[0]);
15 s=compressRepeatsUltra(args[0].getBytes(), 1, 3, null);
16 System.out.println(args[0]+"\n"+s);
17
18 System.exit(0);
19
20 ChromosomeArray cha=Data.getChromosome(1);
21 byte[] bytes=cha.array;
22
23 int letters=0;
24 for(int i=0; i<bytes.length; i++){if(bytes[i]!='N'){letters++;}}
25
26 System.out.println("cha bytes length = "+bytes.length);
27 System.out.println("cha letters length = "+letters);
28 System.out.println("min="+cha.minIndex+", max="+cha.maxIndex+", length="+(cha.maxIndex-cha.minIndex+1));
29
30 s=compressRepeatsUltra(bytes, 1, 1, null);
31 System.out.println("compress(1) length: "+s.length());
32
33 s=compressRepeatsUltra(bytes, 2, 2, null);
34 System.out.println("compress(2) length: "+s.length());
35
36 s=compressRepeatsUltra(bytes, 3, 3, null);
37 System.out.println("compress(3) length: "+s.length());
38
39 s=compressRepeatsUltra(bytes, 1, 2, null);
40 System.out.println("compress(1,2) length: "+s.length());
41
42 s=compressRepeatsUltra(bytes, 1, 3, null);
43 System.out.println("compress(1,3) length: "+s.length());
44
45 // s=compressRepeatsMultiperiod(bytes, 1, 1, null);
46 // System.out.println("compress(1) length: "+s.length());
47 //
48 // s=compressRepeatsMultiperiod(bytes, 2, 2, null);
49 // System.out.println("compress(2) length: "+s.length());
50 //
51 // s=compressRepeatsMultiperiod(bytes, 3, 3, null);
52 // System.out.println("compress(3) length: "+s.length());
53 //
54 // s=compressRepeatsMultiperiod(bytes, 1, 2, null);
55 // System.out.println("compress(1,2) length: "+s.length());
56 //
57 // s=compressRepeatsMultiperiod(bytes, 1, 3, null);
58 // System.out.println("compress(1,3) length: "+s.length());
59 }
60
61 public static String compress(String s){
62 String s1=compressRepeats(s.getBytes(), 1);
63 String s2=compressRepeats(s1.getBytes(), 2);
64 String s3=compressRepeats(s2.getBytes(), 3);
65 return s3;
66 }
67
68 public static String compressRepeats(byte[] array, int period){
69
70 StringBuilder sb=new StringBuilder(array.length);
71
72 for(int base=0; base<array.length; base++){
73 //Test for repeats of current pattern (array[i] to array[period-1])
74 int repeats=countRepeats(array, base, period);
75 int occurances=repeats+1;
76
77 // System.out.println("base = "+base+"\t, repeats = "+repeats);
78
79 if(repeats==0){
80 //Advance pointer by 1
81 sb.append((char)array[base]);
82 }else if(repeats==1){
83 //Still advance pointer by 1
84 sb.append((char)array[base]);
85 }else if(repeats==2){
86 //Jump ahead
87 base+=period-1;
88 }else{
89 //Compress and advance pointer by a factor of period
90
91 int log=(32-Integer.numberOfLeadingZeros(repeats+1))-1; //+1 is optional; gives lower compression
92 // System.out.println("repeats="+repeats+
93 // ", Integer.highestOneBit("+repeats+")="+Integer.highestOneBit(repeats)+
94 // ", log="+log+", "+Integer.toBinaryString(repeats));
95 assert(log>0 && log<=31);
96
97
98 //Append
99 for(int i=1; i<log; i++){
100 for(int j=0; j<period; j++){
101 sb.append((char)array[base+j]);
102 }
103 }
104
105 base=base+(period*(repeats))-1;
106
107 }
108
109 }
110
111 return sb.toString();
112 }
113
114 public static String compressRepeatsMultiperiod(byte[] array, int minPeriod, int maxPeriod, IntList list){
115
116 StringBuilder sb=new StringBuilder(array.length);
117
118 for(int base=0; base<array.length; base++){
119 //Test for repeats of current pattern (array[i] to array[period-1])
120
121
122 int period=0;
123 int repeats=0;
124 // for(int x=maxPeriod; x>=minPeriod; x--){
125 // int temp=countRepeats(array, base, x);
126 // if(temp>1){
127 // repeats=temp;
128 // period=x;
129 // break;
130 // }
131 // }
132 for(int x=minPeriod; x<=maxPeriod; x++){
133 int temp=countRepeats(array, base, x);
134 if(temp>1){
135 repeats=temp;
136 period=x;
137 break;
138 }
139 }
140 int occurances=repeats+1;
141
142 // System.out.println("base = "+base+"\t, repeats = "+repeats+"\t, period = "+period);
143
144 if(repeats==0){
145 //Advance pointer by 1
146 sb.append((char)array[base]);
147 if(list!=null){list.add(base);}
148 }else if(repeats==1){
149 //Still advance pointer by 1
150 sb.append((char)array[base]);
151 if(list!=null){list.add(base);}
152 }
153 // else if(repeats==2){
154 // for(int j=0; j<period; j++){
155 // sb.append((char)array[base+j]);
156 // }
157 // //Jump ahead
158 // base+=2*period-1;
159 // }
160 else{
161 //Compress and advance pointer by a factor of period
162
163 int log=(32-Integer.numberOfLeadingZeros(repeats+1))-1; //+1 is optional; gives lower compression
164 // System.out.println("repeats="+repeats+
165 // ", Integer.highestOneBit("+repeats+")="+Integer.highestOneBit(repeats)+
166 // ", log="+log+", "+Integer.toBinaryString(repeats));
167 assert(log>0 && log<=31);
168
169
170 //Append
171 for(int i=0; i<log; i++){
172 for(int j=0; j<period; j++){
173 sb.append((char)array[base+j]);
174 if(list!=null){list.add(base+j);}
175 }
176 }
177
178 base=base+(period*(repeats))-1;
179
180 }
181
182 }
183
184 return sb.toString();
185 }
186
187 public static String compressRepeatsUltra(byte[] array, int minPeriod, int maxPeriod, IntList list){
188
189 StringBuilder sb=new StringBuilder(array.length);
190
191 for(int base=0; base<array.length; base++){
192 //Test for repeats of current pattern (array[i] to array[period-1])
193
194
195 int period=0;
196 int repeats=0;
197
198 for(int x=minPeriod; x<=maxPeriod; x++){
199 int temp=countRepeats(array, base, x);
200 // System.out.println("*** temp="+temp+" for "+base+", "+x);
201 if(temp>1){
202 repeats=temp;
203 period=x;
204 break;
205 }
206 }
207 // System.err.println(repeats);
208 if(repeats==0){
209 //Advance pointer by 1
210 sb.append((char)array[base]);
211 if(list!=null){list.add(base);}
212 }else if(repeats==1){
213 //Still advance pointer by 1
214 sb.append((char)array[base]);
215 if(list!=null){list.add(base);}
216
217 // System.err.println(base);
218 base=base+(period*(repeats))-1;
219 // System.err.println(base);
220 }
221 // else if(repeats==2){
222 // for(int j=0; j<period; j++){
223 // sb.append((char)array[base+j]);
224 // }
225 // //Jump ahead
226 // base+=2*period-1;
227 // }
228 else{
229 //Compress and advance pointer
230
231 //Append
232 for(int j=0; j<period; j++){
233 sb.append((char)array[base+j]);
234 if(list!=null){list.add(base+j);}
235 }
236
237 base=base+(period*(repeats))-1;
238
239 }
240
241 }
242
243 return sb.toString();
244 }
245
246 public static int countRepeats(byte[] array, int base, int period){
247
248 int max=array.length-period+1;
249
250 int matches=0;
251 boolean fail=false;
252 for(int loc=base+period; loc<max && !fail; loc+=period){
253 for(int i=0; i<period && !fail; i++){
254 if(array[base+i]==array[loc+i]){
255 matches++;
256 // System.out.println("base = "+base+", loc = "+loc+", period = "+period+", and "+(base+i)+" == "+(loc+i));
257 }else{
258 // System.err.println("failed");
259 fail=true;
260 }
261 }
262 }
263 // System.err.println("matches = "+matches);
264 int repeats=matches/period;
265 // System.err.println("repeats = "+repeats);
266 return repeats;
267 }
268
269
270 }