Debian Codegraph @

current/repeat/PalindromeFinder.java

raw symbols SCIP
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 * Finds the longest palindrome in a specified region.
16 * Designed for hairpins, so it allows both a bounded loop size,
17 * number of mismatches, and tail length.
18 *
19 * @author Brian Bushnell
20 * @date August 30, 2023
21 *
22 */
23 public class PalindromeFinder {
24
25 /*--------------------------------------------------------------*/
26 /*---------------- Main ----------------*/
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;//must be>=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 // long[] plenHist=new long[histlen+1];
79 // long[] loopHist=new long[histlen+1];
80 // long[] tailHist=new long[histlen+1];
81 // long[] tailDifHist=new long[histlen+1];
82 // long[] mismatchHist=new long[histlen+1];
83 long symmetric=0;
84 Palindrome best=null;
85 PalindromeFinder pf=new PalindromeFinder(minPLen, minLoop, maxLoop, minMatches, maxMismatches,
86 minTail, maxTail, maxTailDif);
87 // assert(false):pf;
88 // PalindromeFinder pf2=new PalindromeFinder(minPLen, minLoop-1, maxLoop, maxMismatches);
89 for(byte[] s : sequences) {
90 Palindrome p=pf.longestPalindrome(s);
91 if(p!=null) {
92 // Palindrome p2=pf2.longestPalindrome(s);
93 // assert(p2!=null) : "\n"+new String(s)+"\n"+p+"\n"+p2+"\n"+pf+"\n"+pf2;
94 found++;
95 int tail1=p.a, tail2=s.length-p.b-1;
96 if(tail1==tail2) {symmetric++;}
97 // int tailDif=Tools.absdif(tail1, tail2);
98 // assert(tailDif<=maxTailDif) : tail1+", "+tail2+", "+tailDif+", "+maxTailDif+"\n"
99 // + p+", len="+s.length;
100 // int loop=p.loop();
101 // int plen=p.plen();
102 // plenHist[Tools.min(plen, histlen)]++;
103 // loopHist[Tools.min(loop, histlen)]++;
104 // mismatchHist[Tools.min(p.mismatches, histlen)]++;
105 // tailHist[Tools.min(Tools.max(tail1, tail2), histlen)]++;
106 // tailDifHist[Tools.min(tailDif, histlen)]++;
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 // System.out.println("Length\tplen\tloop\ttail\ttaildif\tmismatches");
118 // ByteBuilder bb=new ByteBuilder();
119 // for(int i=0; i<plenHist.length; i++) {
120 // bb.append(i).tab().append(plenHist[i]).tab().append(loopHist[i]).tab();
121 // bb.append(tailHist[i]).tab().append(tailDifHist[i]).tab().append(mismatchHist[i]);
122 // System.out.println(bb);
123 // bb.clear();
124 // }
125 System.out.println(pf.tracker.toString());
126
127 }
128
129 /*--------------------------------------------------------------*/
130 /*---------------- Initialization ----------------*/
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 // System.err.println("minPLen="+minPLen+", minLoop="+minLoop+", maxLoop="+maxLoop+", maxMismatches="+
150 // maxMismatches+", halfMinLoopEven="+halfMinLoopEven+", halfMinLoopOdd="+halfMinLoopOdd);
151 // assert(maxMismatches==0) : "maxMismatches!=0 is not yet supported.";
152 }
153
154 /*--------------------------------------------------------------*/
155 /*---------------- Outer Methods ----------------*/
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 // System.err.println("longestPalindrome()");
165 final int minStart=minPos+halfMinLength;
166 final int maxStop=maxPos-halfMinLength;
167 for(int i=minStart; i<=maxStop; i++){
168 // System.err.println();
169 // System.err.println("longestPalindrome cycle "+i+" odd");
170 p=longestPalindromeOdd(s, i, minPos, maxPos);
171 if(p!=null && p.compareTo(best)>0){
172 best.setFrom(p);
173 // System.err.println("New best: "+best);
174 }
175 // System.err.println("longestPalindrome cycle "+i+" even");
176 p=longestPalindromeEven(s, i, minPos, maxPos);
177 if(p!=null && p.compareTo(best)>0){
178 best.setFrom(p);
179 // System.err.println("New best: "+best);
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 // System.err.println("plen="+plen+", minPLen="+minPLen+", loop="+loop+
186 // ", minLoop="+minLoop+", maxLoop="+maxLoop+", mismatches="+best.mismatches+", maxMismatches="+maxMismatches);
187 return ret;
188 }
189
190 /*--------------------------------------------------------------*/
191 /*---------------- Inner Methods ----------------*/
192 /*--------------------------------------------------------------*/
193
194 //Ignores palindromes in the loop which may violate minLoop
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 //Ignores palindromes in the loop which may violate minLoop
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 // System.err.println("longestPerfectPalindrome("+a0+", "+b0+")");
218 assert(b0>a0) : a0+", "+b0;
219 if(a0<minPos || b0>maxPos) {
220 // System.err.println("Out of bounds: minPos="+minPos+", maxPos="+maxPos);
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 // int lastMismatch=-1;
227 for(; a>=minPos && b<=maxPos; a--, b++){
228 // System.err.println("a="+a+", b="+b+", matches="+matches);
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);//This is so I can use the compareTo method
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 // System.err.println("Considering "+p);
244 if(p.compareTo(best)>0) {
245 best.setFrom(p);
246 assert(tailDif<=maxTailDif);
247 // System.err.println("Set to "+best);
248 }
249 }else {
250 // System.err.println(tail1+", "+tail2+", "+
251 // tailDif+", "+loop+", "+plen+", "+p.mismatches);
252 }
253 }
254 matches=0;
255 // mismatches|=1;
256 // lastMismatch=b;
257 if(b-a+1>maxLoop) {break;}
258 }
259 }
260 // System.err.println("Exit loop: a="+a+", b="+b+", matches="+matches);
261 while(a<minPos || b>maxPos) {a++; b--;}
262 if(matches>=minMatches && matches>=best.matches) {
263 p.set(a, b, matches, mismatches);
264 // System.err.println("Considering "+p);
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 // System.err.println("Set to "+best);
276 }
277 }
278 }
279
280 // System.err.println("Returning "+best);
281 return best;
282 }
283
284 /** TODO: Make a new dynamic programming version with +10 for match and -9 for mismatch */
285 private Palindrome longestImperfectPalindrome(final byte[] s, final int a0, final int b0, final int minPos, final int maxPos){
286 // System.err.println("longestPerfectPalindrome("+a0+", "+b0+")");
287 assert(b0>a0) : a0+", "+b0;
288 if(a0<minPos || b0>maxPos) {
289 // System.err.println("Out of bounds: minPos="+minPos+", maxPos="+maxPos);
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 // System.err.println("a="+a+", b="+b+", matches="+matches);
299 if(matches(s[a], s[b])){
300 matches++;
301 }else{
302 // System.err.println(matches+", "+mismatches+", "+a+"-"+a2+", "+b+"-"+b2);
303
304 //Shrink palindrome inner bounds until mismatches is under thresh
305 for(; mismatches>maxMismatches; a2--, b2++) {
306 if(matches(s[a2], s[b2])) {
307 matches--;
308 }else {
309 mismatches--;
310 }
311 }
312 //Shrink palindrome inner bounds until the outermost base is a match
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);//This is so I can use the compareTo method
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 // System.err.println("Considering "+p);
335 if(p.compareTo(best)>0) {
336 best.setFrom(p);
337 // System.err.println("Set to "+best);
338 }
339 }else {
340 // System.err.println(tail1+", "+tail2+", "+
341 // tailDif+", "+loop+", "+plen+", "+p.mismatches);
342 }
343 }
344 // mismatches|=1;
345 // lastMismatch=b;
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 // System.err.println("Exit loop: a="+a+", b="+b+", matches="+matches);
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 //Shrink palindrome inner bounds until mismatches is under thresh
358 for(; mismatches>maxMismatches; a2--, b2++) {
359 if(matches(s[a2], s[b2])) {
360 matches--;
361 }else {
362 mismatches--;
363 }
364 }
365 //Shrink palindrome inner bounds until the outermost base is a match
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 // System.err.println("Considering "+p);
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 // System.err.println("Considering "+p);
388 if(p.compareTo(best)>0) {
389 best.setFrom(p);
390 // System.err.println("Set to "+best);
391 }
392 }
393 }
394
395 // System.err.println("Returning "+best);
396 return best;
397 }
398
399 /*--------------------------------------------------------------*/
400 /*---------------- Initialization ----------------*/
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 /*---------------- Initialization ----------------*/
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)'~');//Nothing should match this since it is invalid
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 /*---------------- Fields ----------------*/
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;//Should be >=1
444 public final int minLoop;//must be>=0
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 /*---------------- Static Fields ----------------*/
458 /*--------------------------------------------------------------*/
459
460 static final boolean rcomp=true;
461 static final byte[] baseToComp=makeBaseToComp();
462
463 }