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
|
package align2;
import dna.ChromosomeArray;
import dna.Data;
import structures.IntList;
public class CompressString {
public static void main(String[] args){
String s;
s=compressRepeats(args[0].getBytes(), 1);
s=compress(args[0]);
s=compressRepeatsUltra(args[0].getBytes(), 1, 3, null);
System.out.println(args[0]+"\n"+s);
System.exit(0);
ChromosomeArray cha=Data.getChromosome(1);
byte[] bytes=cha.array;
int letters=0;
for(int i=0; i<bytes.length; i++){if(bytes[i]!='N'){letters++;}}
System.out.println("cha bytes length = "+bytes.length);
System.out.println("cha letters length = "+letters);
System.out.println("min="+cha.minIndex+", max="+cha.maxIndex+", length="+(cha.maxIndex-cha.minIndex+1));
s=compressRepeatsUltra(bytes, 1, 1, null);
System.out.println("compress(1) length: "+s.length());
s=compressRepeatsUltra(bytes, 2, 2, null);
System.out.println("compress(2) length: "+s.length());
s=compressRepeatsUltra(bytes, 3, 3, null);
System.out.println("compress(3) length: "+s.length());
s=compressRepeatsUltra(bytes, 1, 2, null);
System.out.println("compress(1,2) length: "+s.length());
s=compressRepeatsUltra(bytes, 1, 3, null);
System.out.println("compress(1,3) length: "+s.length());
// s=compressRepeatsMultiperiod(bytes, 1, 1, null);
// System.out.println("compress(1) length: "+s.length());
//
// s=compressRepeatsMultiperiod(bytes, 2, 2, null);
// System.out.println("compress(2) length: "+s.length());
//
// s=compressRepeatsMultiperiod(bytes, 3, 3, null);
// System.out.println("compress(3) length: "+s.length());
//
// s=compressRepeatsMultiperiod(bytes, 1, 2, null);
// System.out.println("compress(1,2) length: "+s.length());
//
// s=compressRepeatsMultiperiod(bytes, 1, 3, null);
// System.out.println("compress(1,3) length: "+s.length());
}
public static String compress(String s){
String s1=compressRepeats(s.getBytes(), 1);
String s2=compressRepeats(s1.getBytes(), 2);
String s3=compressRepeats(s2.getBytes(), 3);
return s3;
}
public static String compressRepeats(byte[] array, int period){
StringBuilder sb=new StringBuilder(array.length);
for(int base=0; base<array.length; base++){
//Test for repeats of current pattern (array[i] to array[period-1])
int repeats=countRepeats(array, base, period);
int occurances=repeats+1;
// System.out.println("base = "+base+"\t, repeats = "+repeats);
if(repeats==0){
//Advance pointer by 1
sb.append((char)array[base]);
}else if(repeats==1){
//Still advance pointer by 1
sb.append((char)array[base]);
}else if(repeats==2){
//Jump ahead
base+=period-1;
}else{
//Compress and advance pointer by a factor of period
int log=(32-Integer.numberOfLeadingZeros(repeats+1))-1; //+1 is optional; gives lower compression
// System.out.println("repeats="+repeats+
// ", Integer.highestOneBit("+repeats+")="+Integer.highestOneBit(repeats)+
// ", log="+log+", "+Integer.toBinaryString(repeats));
assert(log>0 && log<=31);
//Append
for(int i=1; i<log; i++){
for(int j=0; j<period; j++){
sb.append((char)array[base+j]);
}
}
base=base+(period*(repeats))-1;
}
}
return sb.toString();
}
public static String compressRepeatsMultiperiod(byte[] array, int minPeriod, int maxPeriod, IntList list){
StringBuilder sb=new StringBuilder(array.length);
for(int base=0; base<array.length; base++){
//Test for repeats of current pattern (array[i] to array[period-1])
int period=0;
int repeats=0;
// for(int x=maxPeriod; x>=minPeriod; x--){
// int temp=countRepeats(array, base, x);
// if(temp>1){
// repeats=temp;
// period=x;
// break;
// }
// }
for(int x=minPeriod; x<=maxPeriod; x++){
int temp=countRepeats(array, base, x);
if(temp>1){
repeats=temp;
period=x;
break;
}
}
int occurances=repeats+1;
// System.out.println("base = "+base+"\t, repeats = "+repeats+"\t, period = "+period);
if(repeats==0){
//Advance pointer by 1
sb.append((char)array[base]);
if(list!=null){list.add(base);}
}else if(repeats==1){
//Still advance pointer by 1
sb.append((char)array[base]);
if(list!=null){list.add(base);}
}
// else if(repeats==2){
// for(int j=0; j<period; j++){
// sb.append((char)array[base+j]);
// }
// //Jump ahead
// base+=2*period-1;
// }
else{
//Compress and advance pointer by a factor of period
int log=(32-Integer.numberOfLeadingZeros(repeats+1))-1; //+1 is optional; gives lower compression
// System.out.println("repeats="+repeats+
// ", Integer.highestOneBit("+repeats+")="+Integer.highestOneBit(repeats)+
// ", log="+log+", "+Integer.toBinaryString(repeats));
assert(log>0 && log<=31);
//Append
for(int i=0; i<log; i++){
for(int j=0; j<period; j++){
sb.append((char)array[base+j]);
if(list!=null){list.add(base+j);}
}
}
base=base+(period*(repeats))-1;
}
}
return sb.toString();
}
public static String compressRepeatsUltra(byte[] array, int minPeriod, int maxPeriod, IntList list){
StringBuilder sb=new StringBuilder(array.length);
for(int base=0; base<array.length; base++){
//Test for repeats of current pattern (array[i] to array[period-1])
int period=0;
int repeats=0;
for(int x=minPeriod; x<=maxPeriod; x++){
int temp=countRepeats(array, base, x);
// System.out.println("*** temp="+temp+" for "+base+", "+x);
if(temp>1){
repeats=temp;
period=x;
break;
}
}
// System.err.println(repeats);
if(repeats==0){
//Advance pointer by 1
sb.append((char)array[base]);
if(list!=null){list.add(base);}
}else if(repeats==1){
//Still advance pointer by 1
sb.append((char)array[base]);
if(list!=null){list.add(base);}
// System.err.println(base);
base=base+(period*(repeats))-1;
// System.err.println(base);
}
// else if(repeats==2){
// for(int j=0; j<period; j++){
// sb.append((char)array[base+j]);
// }
// //Jump ahead
// base+=2*period-1;
// }
else{
//Compress and advance pointer
//Append
for(int j=0; j<period; j++){
sb.append((char)array[base+j]);
if(list!=null){list.add(base+j);}
}
base=base+(period*(repeats))-1;
}
}
return sb.toString();
}
public static int countRepeats(byte[] array, int base, int period){
int max=array.length-period+1;
int matches=0;
boolean fail=false;
for(int loc=base+period; loc<max && !fail; loc+=period){
for(int i=0; i<period && !fail; i++){
if(array[base+i]==array[loc+i]){
matches++;
// System.out.println("base = "+base+", loc = "+loc+", period = "+period+", and "+(base+i)+" == "+(loc+i));
}else{
// System.err.println("failed");
fail=true;
}
}
}
// System.err.println("matches = "+matches);
int repeats=matches/period;
// System.err.println("repeats = "+repeats);
return repeats;
}
}
|