| 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 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 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 |
|
| 74 |
int repeats=countRepeats(array, base, period); |
| 75 |
int occurances=repeats+1; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
if(repeats==0){ |
| 80 |
|
| 81 |
sb.append((char)array[base]); |
| 82 |
}else if(repeats==1){ |
| 83 |
|
| 84 |
sb.append((char)array[base]); |
| 85 |
}else if(repeats==2){ |
| 86 |
|
| 87 |
base+=period-1; |
| 88 |
}else{ |
| 89 |
|
| 90 |
|
| 91 |
int log=(32-Integer.numberOfLeadingZeros(repeats+1))-1; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
assert(log>0 && log<=31); |
| 96 |
|
| 97 |
|
| 98 |
|
| 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 |
|
| 120 |
|
| 121 |
|
| 122 |
int period=0; |
| 123 |
int repeats=0; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 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 |
|
| 143 |
|
| 144 |
if(repeats==0){ |
| 145 |
|
| 146 |
sb.append((char)array[base]); |
| 147 |
if(list!=null){list.add(base);} |
| 148 |
}else if(repeats==1){ |
| 149 |
|
| 150 |
sb.append((char)array[base]); |
| 151 |
if(list!=null){list.add(base);} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
else{ |
| 161 |
|
| 162 |
|
| 163 |
int log=(32-Integer.numberOfLeadingZeros(repeats+1))-1; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
assert(log>0 && log<=31); |
| 168 |
|
| 169 |
|
| 170 |
|
| 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 |
|
| 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 |
|
| 201 |
if(temp>1){ |
| 202 |
repeats=temp; |
| 203 |
period=x; |
| 204 |
break; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
if(repeats==0){ |
| 209 |
|
| 210 |
sb.append((char)array[base]); |
| 211 |
if(list!=null){list.add(base);} |
| 212 |
}else if(repeats==1){ |
| 213 |
|
| 214 |
sb.append((char)array[base]); |
| 215 |
if(list!=null){list.add(base);} |
| 216 |
|
| 217 |
|
| 218 |
base=base+(period*(repeats))-1; |
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
else{ |
| 229 |
|
| 230 |
|
| 231 |
|
| 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 |
|
| 257 |
}else{ |
| 258 |
|
| 259 |
fail=true; |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
int repeats=matches/period; |
| 265 |
|
| 266 |
return repeats; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
} |