| 1 |
package repeat; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.Arrays; |
| 6 |
|
| 7 |
import fileIO.FileFormat; |
| 8 |
import shared.Tools; |
| 9 |
import stream.Read; |
| 10 |
import stream.ReadInputStream; |
| 11 |
import structures.IntList; |
| 12 |
import tracker.PalindromeTracker; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class PalindromeFinder { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public static void main(String[] args){ |
| 30 |
|
| 31 |
ArrayList<byte[]> sequences=new ArrayList<byte[]>(); |
| 32 |
|
| 33 |
int minPLen=1; |
| 34 |
int maxMismatches=0; |
| 35 |
int minMatches=0; |
| 36 |
int minLoop=0; |
| 37 |
int maxLoop=40; |
| 38 |
int minTail=0; |
| 39 |
int maxTail=Integer.MAX_VALUE; |
| 40 |
int maxTailDif=Integer.MAX_VALUE; |
| 41 |
|
| 42 |
String fname=null; |
| 43 |
|
| 44 |
for(String s : args) { |
| 45 |
if(Character.isDigit(s.charAt(0))) { |
| 46 |
maxMismatches=Integer.parseInt(s); |
| 47 |
}else if(s.startsWith("mismatch") || s.startsWith("maxmismatch")) { |
| 48 |
maxMismatches=Integer.parseInt(s.split("=")[1]); |
| 49 |
}else if(s.startsWith("minmatch")) { |
| 50 |
minMatches=Integer.parseInt(s.split("=")[1]); |
| 51 |
}else if(s.startsWith("loop") || s.startsWith("maxloop")) { |
| 52 |
maxLoop=Integer.parseInt(s.split("=")[1]); |
| 53 |
}else if(s.startsWith("minloop")) { |
| 54 |
minLoop=Integer.parseInt(s.split("=")[1]); |
| 55 |
}else if(s.startsWith("plen") || s.startsWith("minplen")) { |
| 56 |
minPLen=Integer.parseInt(s.split("=")[1]); |
| 57 |
}else if(s.startsWith("maxtaildif")) { |
| 58 |
maxTailDif=Integer.parseInt(s.split("=")[1]); |
| 59 |
}else if(s.startsWith("mintail")) { |
| 60 |
minTail=Integer.parseInt(s.split("=")[1]); |
| 61 |
}else if(s.startsWith("maxtail")) { |
| 62 |
maxTail=Integer.parseInt(s.split("=")[1]); |
| 63 |
}else if(new File(s).exists()) { |
| 64 |
fname=s; |
| 65 |
}else { |
| 66 |
sequences.add(s.getBytes()); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
if(fname!=null) { |
| 71 |
ArrayList<Read> reads=ReadInputStream.toReads(FileFormat.testInput(fname, null, true), -1); |
| 72 |
for(Read r : reads) {sequences.add(r.bases);} |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
final int histlen=30; |
| 77 |
long found=0; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
long symmetric=0; |
| 84 |
Palindrome best=null; |
| 85 |
PalindromeFinder pf=new PalindromeFinder(minPLen, minLoop, maxLoop, minMatches, maxMismatches, |
| 86 |
minTail, maxTail, maxTailDif); |
| 87 |
|
| 88 |
|
| 89 |
for(byte[] s : sequences) { |
| 90 |
Palindrome p=pf.longestPalindrome(s); |
| 91 |
if(p!=null) { |
| 92 |
|
| 93 |
|
| 94 |
found++; |
| 95 |
int tail1=p.a, tail2=s.length-p.b-1; |
| 96 |
if(tail1==tail2) {symmetric++;} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
pf.tracker.add(p, 0, s.length-1); |
| 108 |
if(p.compareTo(best)>0) {best=p;} |
| 109 |
} |
| 110 |
|
| 111 |
} |
| 112 |
System.out.println("Longest palindrome is: "+best); |
| 113 |
System.out.println("Palindromes found: "+found+"/"+sequences.size()+ |
| 114 |
" = "+String.format("%.2f%%", found*100.0/sequences.size())); |
| 115 |
System.out.println("Symmetric: "+symmetric+"/"+found); |
| 116 |
System.out.println("Histogram:"); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
System.out.println(pf.tracker.toString()); |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public PalindromeFinder(int minPLen_, int minLoop_, int maxLoop_, |
| 134 |
int minMatches_, int maxMismatches_, int minTail_, int maxTail_, int maxTailDif_) { |
| 135 |
minLoop=minLoop_; |
| 136 |
maxLoop=maxLoop_; |
| 137 |
minMatches=Tools.max(1, minMatches_); |
| 138 |
minPLen=Tools.max(minMatches, minPLen_); |
| 139 |
maxMismatches=maxMismatches_; |
| 140 |
minTail=minTail_; |
| 141 |
maxTail=maxTail_; |
| 142 |
maxTailDif=maxTailDif_; |
| 143 |
assert(minLoop>=0); |
| 144 |
assert(maxLoop>=minLoop); |
| 145 |
halfMinLoopEven=minLoop/2; |
| 146 |
halfMinLoopOdd=((minLoop|1)+1)/2; |
| 147 |
int minLength=minLoop+2*minPLen; |
| 148 |
halfMinLength=Tools.max(0, minLength/2); |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public Palindrome longestPalindrome(byte[] s){ |
| 159 |
return longestPalindrome(s, 0, s.length-1); |
| 160 |
} |
| 161 |
|
| 162 |
public Palindrome longestPalindrome(byte[] s, final int minPos, final int maxPos){ |
| 163 |
Palindrome best=tempB.clear(), p; |
| 164 |
|
| 165 |
final int minStart=minPos+halfMinLength; |
| 166 |
final int maxStop=maxPos-halfMinLength; |
| 167 |
for(int i=minStart; i<=maxStop; i++){ |
| 168 |
|
| 169 |
|
| 170 |
p=longestPalindromeOdd(s, i, minPos, maxPos); |
| 171 |
if(p!=null && p.compareTo(best)>0){ |
| 172 |
best.setFrom(p); |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
p=longestPalindromeEven(s, i, minPos, maxPos); |
| 177 |
if(p!=null && p.compareTo(best)>0){ |
| 178 |
best.setFrom(p); |
| 179 |
|
| 180 |
} |
| 181 |
} |
| 182 |
if(best==null) {return null;} |
| 183 |
int loop=best.loop(), plen=best.plen(); |
| 184 |
Palindrome ret=(plen<minPLen || loop<minLoop || loop>maxLoop || best.mismatches>maxMismatches) ? null : best.clone(); |
| 185 |
|
| 186 |
|
| 187 |
return ret; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
private Palindrome longestPalindromeOddIgnoringLoop(byte[] s, int middle, final int minPos, final int maxPos){ |
| 196 |
return (maxMismatches<1 ? longestPerfectPalindrome(s, middle-halfMinLoopOdd, middle+halfMinLoopOdd, minPos, maxPos) |
| 197 |
: longestImperfectPalindrome(s, middle-halfMinLoopOdd, middle+halfMinLoopOdd, minPos, maxPos)); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
private Palindrome longestPalindromeEvenIgnoringLoop(byte[] s, int middle, final int minPos, final int maxPos){ |
| 202 |
return (maxMismatches<1 ? longestPerfectPalindrome(s, middle-halfMinLoopEven, middle+halfMinLoopEven+1, minPos, maxPos) |
| 203 |
: longestImperfectPalindrome(s, middle-halfMinLoopEven, middle+halfMinLoopEven+1, minPos, maxPos)); |
| 204 |
} |
| 205 |
|
| 206 |
private Palindrome longestPalindromeOdd(byte[] s, int middle, final int minPos, final int maxPos){ |
| 207 |
return (maxMismatches<1 ? longestPerfectPalindrome(s, middle-1, middle+1, minPos, maxPos) |
| 208 |
: longestImperfectPalindrome(s, middle-1, middle+1, minPos, maxPos)); |
| 209 |
} |
| 210 |
|
| 211 |
private Palindrome longestPalindromeEven(byte[] s, int middle, final int minPos, final int maxPos){ |
| 212 |
return (maxMismatches<1 ? longestPerfectPalindrome(s, middle-1, middle+2, minPos, maxPos) |
| 213 |
: longestImperfectPalindrome(s, middle-1, middle+2, minPos, maxPos)); |
| 214 |
} |
| 215 |
|
| 216 |
private Palindrome longestPerfectPalindrome(final byte[] s, final int a0, final int b0, final int minPos, final int maxPos){ |
| 217 |
|
| 218 |
assert(b0>a0) : a0+", "+b0; |
| 219 |
if(a0<minPos || b0>maxPos) { |
| 220 |
|
| 221 |
return null; |
| 222 |
} |
| 223 |
final Palindrome p=tempC.clear(), best=tempD.clear(); |
| 224 |
int a=a0, b=b0; |
| 225 |
int matches=0, mismatches=0; |
| 226 |
|
| 227 |
for(; a>=minPos && b<=maxPos; a--, b++){ |
| 228 |
|
| 229 |
if(matches(s[a], s[b])){ |
| 230 |
matches++; |
| 231 |
}else{ |
| 232 |
if(matches>=minMatches && matches>=best.matches) { |
| 233 |
|
| 234 |
p.set(a+1, b-1, matches, mismatches); |
| 235 |
int tail1=p.a-minPos, tail2=maxPos-p.b; |
| 236 |
int tailDif=Tools.absdif(tail1, tail2); |
| 237 |
int loop=p.loop(); |
| 238 |
int plen=p.plen(); |
| 239 |
if(tailDif<=maxTailDif && tail1<=maxTail && tail2<=maxTail |
| 240 |
&& tail1>=minTail && tail2>=minTail && loop>=minLoop && loop<=maxLoop |
| 241 |
&& plen>=minPLen && p.mismatches<=maxMismatches) { |
| 242 |
|
| 243 |
|
| 244 |
if(p.compareTo(best)>0) { |
| 245 |
best.setFrom(p); |
| 246 |
assert(tailDif<=maxTailDif); |
| 247 |
|
| 248 |
} |
| 249 |
}else { |
| 250 |
|
| 251 |
|
| 252 |
} |
| 253 |
} |
| 254 |
matches=0; |
| 255 |
|
| 256 |
|
| 257 |
if(b-a+1>maxLoop) {break;} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
while(a<minPos || b>maxPos) {a++; b--;} |
| 262 |
if(matches>=minMatches && matches>=best.matches) { |
| 263 |
p.set(a, b, matches, mismatches); |
| 264 |
|
| 265 |
int tail1=p.a-minPos, tail2=maxPos-p.b; |
| 266 |
int tailDif=Tools.absdif(tail1, tail2); |
| 267 |
int loop=p.loop(); |
| 268 |
int plen=p.plen(); |
| 269 |
if(tailDif<=maxTailDif && tail1<=maxTail && tail2<=maxTail |
| 270 |
&& tail1>=minTail && tail2>=minTail && loop>=minLoop && loop<=maxLoop |
| 271 |
&& plen>=minPLen && p.mismatches<=maxMismatches) { |
| 272 |
if(p.compareTo(best)>0) { |
| 273 |
assert(tailDif<=maxTailDif); |
| 274 |
best.setFrom(p); |
| 275 |
|
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
return best; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
private Palindrome longestImperfectPalindrome(final byte[] s, final int a0, final int b0, final int minPos, final int maxPos){ |
| 286 |
|
| 287 |
assert(b0>a0) : a0+", "+b0; |
| 288 |
if(a0<minPos || b0>maxPos) { |
| 289 |
|
| 290 |
return null; |
| 291 |
} |
| 292 |
final Palindrome p=tempC.clear(), best=tempD.clear(); |
| 293 |
int a=a0, b=b0; |
| 294 |
int a2=a0, b2=b0; |
| 295 |
int matches=0, mismatches=0; |
| 296 |
|
| 297 |
for(; a>=minPos && b<=maxPos; a--, b++){ |
| 298 |
|
| 299 |
if(matches(s[a], s[b])){ |
| 300 |
matches++; |
| 301 |
}else{ |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
for(; mismatches>maxMismatches; a2--, b2++) { |
| 306 |
if(matches(s[a2], s[b2])) { |
| 307 |
matches--; |
| 308 |
}else { |
| 309 |
mismatches--; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
for(; mismatches>0 && !matches(s[a2], s[b2]); a2--, b2++) { |
| 314 |
mismatches--; |
| 315 |
} |
| 316 |
assert(mismatches<=maxMismatches); |
| 317 |
assert(matches+mismatches==a2-a) : matches+", "+mismatches+", "+a+"-"+a2+", "+b+"-"+b2; |
| 318 |
assert(matches==0 || matches(s[a2],s[b2])); |
| 319 |
if(matches>=minMatches && matches>=best.matches) { |
| 320 |
|
| 321 |
p.set(a+1, b-1, matches, mismatches); |
| 322 |
while(p.mismatches>0 && !matches(s[p.a], s[p.b])) { |
| 323 |
p.mismatches--; p.a++; p.b--; |
| 324 |
} |
| 325 |
assert(matches(s[p.a],s[p.b])) : matches+", "+minMatches; |
| 326 |
int tail1=p.a-minPos, tail2=maxPos-p.b; |
| 327 |
int tailDif=Tools.absdif(tail1, tail2); |
| 328 |
int loop=p.loop(); |
| 329 |
int plen=p.plen(); |
| 330 |
if(tailDif<=maxTailDif && tail1<=maxTail && tail2<=maxTail |
| 331 |
&& tail1>=minTail && tail2>=minTail && loop>=minLoop && loop<=maxLoop |
| 332 |
&& plen>=minPLen && p.mismatches<=maxMismatches) { |
| 333 |
|
| 334 |
|
| 335 |
if(p.compareTo(best)>0) { |
| 336 |
best.setFrom(p); |
| 337 |
|
| 338 |
} |
| 339 |
}else { |
| 340 |
|
| 341 |
|
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
mismatches++; |
| 347 |
assert(matches==0 || matches+mismatches==a2-a+1) : matches+", "+mismatches+", "+a+"-"+a2+", "+b2+"-"+b; |
| 348 |
|
| 349 |
if(b-a+1>maxLoop) {break;} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
while(a<minPos || b>maxPos) {a++; b--;} |
| 354 |
assert(matches==0 || matches+mismatches==a2-a+1) : matches+", "+mismatches+", "+a+"-"+a2+", "+b2+"-"+b; |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
for(; mismatches>maxMismatches; a2--, b2++) { |
| 359 |
if(matches(s[a2], s[b2])) { |
| 360 |
matches--; |
| 361 |
}else { |
| 362 |
mismatches--; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
for(; mismatches>0 && !matches(s[a2], s[b2]); a2--, b2++) { |
| 367 |
mismatches--; |
| 368 |
} |
| 369 |
while(mismatches>0 && !matches(s[a], s[b])) { |
| 370 |
a++; b--; mismatches--; |
| 371 |
} |
| 372 |
assert(mismatches<=maxMismatches); |
| 373 |
assert(matches==0 || matches+mismatches==a2-a+1) : matches+", "+mismatches+", "+a+"-"+a2+", "+b2+"-"+b; |
| 374 |
assert(matches==0 || matches(s[a],s[b])); |
| 375 |
assert(matches==0 || matches(s[a2],s[b2])); |
| 376 |
|
| 377 |
if(matches>=minMatches && matches>=best.matches) { |
| 378 |
p.set(a, b, matches, mismatches); |
| 379 |
|
| 380 |
int tail1=p.a-minPos, tail2=maxPos-p.b; |
| 381 |
int tailDif=Tools.absdif(tail1, tail2); |
| 382 |
int loop=p.loop(); |
| 383 |
int plen=p.plen(); |
| 384 |
if(tailDif<=maxTailDif && tail1<=maxTail && tail2<=maxTail |
| 385 |
&& tail1>=minTail && tail2>=minTail && loop>=minLoop && loop<=maxLoop |
| 386 |
&& plen>=minPLen && p.mismatches<=maxMismatches) { |
| 387 |
|
| 388 |
if(p.compareTo(best)>0) { |
| 389 |
best.setFrom(p); |
| 390 |
|
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
return best; |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
@Override |
| 404 |
public String toString() { |
| 405 |
return "minMatches="+minMatches+", maxMismatches="+maxMismatches+", minLoop="+minLoop+ |
| 406 |
", maxLoop="+maxLoop+", minPLen="+minPLen+ |
| 407 |
", halfMinLoopOdd="+halfMinLoopOdd+", halfMinLoopEven="+halfMinLoopEven+ |
| 408 |
", halfMinLength="+halfMinLength+ |
| 409 |
", minTail="+minTail+", maxTail="+maxTail+", maxTailDif="+maxTailDif; |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
static boolean matches(byte a, byte b) { |
| 417 |
return a==(rcomp ? baseToComp[b] : b); |
| 418 |
} |
| 419 |
|
| 420 |
static byte[] makeBaseToComp() { |
| 421 |
byte[] array=new byte[128]; |
| 422 |
Arrays.fill(array, (byte)'~'); |
| 423 |
array['A']=array['a']='T'; |
| 424 |
array['C']=array['c']='G'; |
| 425 |
array['G']=array['g']='C'; |
| 426 |
array['T']=array['t']=array['U']=array['u']='A'; |
| 427 |
return array; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
private final Palindrome tempB=new Palindrome(); |
| 435 |
private final Palindrome tempC=new Palindrome(); |
| 436 |
private final Palindrome tempD=new Palindrome(); |
| 437 |
private final IntList mismatchList=new IntList(); |
| 438 |
|
| 439 |
public PalindromeTracker tracker=new PalindromeTracker(); |
| 440 |
public PalindromeTracker trackerFull=new PalindromeTracker(); |
| 441 |
|
| 442 |
public final int maxMismatches; |
| 443 |
public final int minMatches; |
| 444 |
public final int minLoop; |
| 445 |
public final int maxLoop; |
| 446 |
public final int minPLen; |
| 447 |
|
| 448 |
public final int minTail; |
| 449 |
public final int maxTail; |
| 450 |
public final int maxTailDif; |
| 451 |
|
| 452 |
public final int halfMinLoopOdd; |
| 453 |
public final int halfMinLoopEven; |
| 454 |
public final int halfMinLength; |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
static final boolean rcomp=true; |
| 461 |
static final byte[] baseToComp=makeBaseToComp(); |
| 462 |
|
| 463 |
} |