File: PalindromeFinder.java

package info (click to toggle)
bbmap 39.20%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,024 kB
  • sloc: java: 312,743; sh: 18,099; python: 5,247; ansic: 2,074; perl: 96; makefile: 39; xml: 38
file content (463 lines) | stat: -rwxr-xr-x 17,678 bytes parent folder | download | duplicates (2)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package repeat;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

import fileIO.FileFormat;
import shared.Tools;
import stream.Read;
import stream.ReadInputStream;
import structures.IntList;
import tracker.PalindromeTracker;

/**
 * Finds the longest palindrome in a specified region.
 * Designed for hairpins, so it allows both a bounded loop size,
 * number of mismatches, and tail length.
 * 
 * @author Brian Bushnell
 * @date August 30, 2023
 *
 */
public class PalindromeFinder {
	
	/*--------------------------------------------------------------*/
	/*----------------             Main             ----------------*/
	/*--------------------------------------------------------------*/
	
	public static void main(String[] args){
		
		ArrayList<byte[]> sequences=new ArrayList<byte[]>();
		
		int minPLen=1;
		int maxMismatches=0;
		int minMatches=0;
		int minLoop=0;//must be>=0
		int maxLoop=40;
		int minTail=0;
		int maxTail=Integer.MAX_VALUE;
		int maxTailDif=Integer.MAX_VALUE;
		
		String fname=null;
		
		for(String s : args) {
			if(Character.isDigit(s.charAt(0))) {
				maxMismatches=Integer.parseInt(s);
			}else if(s.startsWith("mismatch") || s.startsWith("maxmismatch")) {
				maxMismatches=Integer.parseInt(s.split("=")[1]);
			}else if(s.startsWith("minmatch")) {
				minMatches=Integer.parseInt(s.split("=")[1]);
			}else if(s.startsWith("loop") || s.startsWith("maxloop")) {
				maxLoop=Integer.parseInt(s.split("=")[1]);
			}else if(s.startsWith("minloop")) {
				minLoop=Integer.parseInt(s.split("=")[1]);
			}else if(s.startsWith("plen") || s.startsWith("minplen")) {
				minPLen=Integer.parseInt(s.split("=")[1]);
			}else if(s.startsWith("maxtaildif")) {
				maxTailDif=Integer.parseInt(s.split("=")[1]);
			}else if(s.startsWith("mintail")) {
				minTail=Integer.parseInt(s.split("=")[1]);
			}else if(s.startsWith("maxtail")) {
				maxTail=Integer.parseInt(s.split("=")[1]);
			}else if(new File(s).exists()) {
				fname=s;
			}else {
				sequences.add(s.getBytes());
			}
		}
		
		if(fname!=null) {
			ArrayList<Read> reads=ReadInputStream.toReads(FileFormat.testInput(fname, null, true), -1);
			for(Read r : reads) {sequences.add(r.bases);}
			
		}

		final int histlen=30;
		long found=0;
//		long[] plenHist=new long[histlen+1];
//		long[] loopHist=new long[histlen+1];
//		long[] tailHist=new long[histlen+1];
//		long[] tailDifHist=new long[histlen+1];
//		long[] mismatchHist=new long[histlen+1];
		long symmetric=0;
		Palindrome best=null;
		PalindromeFinder pf=new PalindromeFinder(minPLen, minLoop, maxLoop, minMatches, maxMismatches,
				minTail, maxTail, maxTailDif);
//		assert(false):pf;
//		PalindromeFinder pf2=new PalindromeFinder(minPLen, minLoop-1, maxLoop, maxMismatches);
		for(byte[] s : sequences) {
			Palindrome p=pf.longestPalindrome(s);
			if(p!=null) {
//				Palindrome p2=pf2.longestPalindrome(s);
//				assert(p2!=null) : "\n"+new String(s)+"\n"+p+"\n"+p2+"\n"+pf+"\n"+pf2;
				found++;
				int tail1=p.a, tail2=s.length-p.b-1;
				if(tail1==tail2) {symmetric++;}
//				int tailDif=Tools.absdif(tail1, tail2);
//				assert(tailDif<=maxTailDif) : tail1+", "+tail2+", "+tailDif+", "+maxTailDif+"\n"
//						+ p+", len="+s.length;
//				int loop=p.loop();
//				int plen=p.plen();
//				plenHist[Tools.min(plen, histlen)]++;
//				loopHist[Tools.min(loop, histlen)]++;
//				mismatchHist[Tools.min(p.mismatches, histlen)]++;
//				tailHist[Tools.min(Tools.max(tail1, tail2), histlen)]++;
//				tailDifHist[Tools.min(tailDif, histlen)]++;
				pf.tracker.add(p, 0, s.length-1);
				if(p.compareTo(best)>0) {best=p;}
			}
			
		}
		System.out.println("Longest palindrome is: "+best);
		System.out.println("Palindromes found:     "+found+"/"+sequences.size()+
				" = "+String.format("%.2f%%", found*100.0/sequences.size()));
		System.out.println("Symmetric:             "+symmetric+"/"+found);
		System.out.println("Histogram:");
//		System.out.println("Length\tplen\tloop\ttail\ttaildif\tmismatches");
//		ByteBuilder bb=new ByteBuilder();
//		for(int i=0; i<plenHist.length; i++) {
//			bb.append(i).tab().append(plenHist[i]).tab().append(loopHist[i]).tab();
//			bb.append(tailHist[i]).tab().append(tailDifHist[i]).tab().append(mismatchHist[i]);
//			System.out.println(bb);
//			bb.clear();
//		}
		System.out.println(pf.tracker.toString());
		
	}
	
	/*--------------------------------------------------------------*/
	/*----------------        Initialization        ----------------*/
	/*--------------------------------------------------------------*/
	
	public PalindromeFinder(int minPLen_, int minLoop_, int maxLoop_, 
			int minMatches_, int maxMismatches_, int minTail_, int maxTail_, int maxTailDif_) {
		minLoop=minLoop_;
		maxLoop=maxLoop_;
		minMatches=Tools.max(1, minMatches_);
		minPLen=Tools.max(minMatches, minPLen_);
		maxMismatches=maxMismatches_;
		minTail=minTail_;
		maxTail=maxTail_;
		maxTailDif=maxTailDif_;
		assert(minLoop>=0);
		assert(maxLoop>=minLoop);
		halfMinLoopEven=minLoop/2;
		halfMinLoopOdd=((minLoop|1)+1)/2;
		int minLength=minLoop+2*minPLen;
		halfMinLength=Tools.max(0, minLength/2);
//		System.err.println("minPLen="+minPLen+", minLoop="+minLoop+", maxLoop="+maxLoop+", maxMismatches="+
//				maxMismatches+", halfMinLoopEven="+halfMinLoopEven+", halfMinLoopOdd="+halfMinLoopOdd);
//		assert(maxMismatches==0) : "maxMismatches!=0 is not yet supported.";
	}
	
	/*--------------------------------------------------------------*/
	/*----------------        Outer Methods         ----------------*/
	/*--------------------------------------------------------------*/
	
	public Palindrome longestPalindrome(byte[] s){
		return longestPalindrome(s, 0, s.length-1);
	}
	
	public Palindrome longestPalindrome(byte[] s, final int minPos, final int maxPos){
		Palindrome best=tempB.clear(), p;
//		System.err.println("longestPalindrome()");
		final int minStart=minPos+halfMinLength;
		final int maxStop=maxPos-halfMinLength;
		for(int i=minStart; i<=maxStop; i++){
//			System.err.println();
//			System.err.println("longestPalindrome cycle "+i+" odd");
			p=longestPalindromeOdd(s, i, minPos, maxPos);
			if(p!=null && p.compareTo(best)>0){
				best.setFrom(p);
//				System.err.println("New best: "+best);
			}
//			System.err.println("longestPalindrome cycle "+i+" even");
			p=longestPalindromeEven(s, i, minPos, maxPos);
			if(p!=null && p.compareTo(best)>0){
				best.setFrom(p);
//				System.err.println("New best: "+best);
			}
		}
		if(best==null) {return null;}
		int loop=best.loop(), plen=best.plen();
		Palindrome ret=(plen<minPLen || loop<minLoop || loop>maxLoop || best.mismatches>maxMismatches) ? null : best.clone();
//		System.err.println("plen="+plen+", minPLen="+minPLen+", loop="+loop+
//				", minLoop="+minLoop+", maxLoop="+maxLoop+", mismatches="+best.mismatches+", maxMismatches="+maxMismatches);
		return ret;
	}
	
	/*--------------------------------------------------------------*/
	/*----------------         Inner Methods        ----------------*/
	/*--------------------------------------------------------------*/
	
	//Ignores palindromes in the loop which may violate minLoop
	private Palindrome longestPalindromeOddIgnoringLoop(byte[] s, int middle, final int minPos, final int maxPos){
		return (maxMismatches<1 ? longestPerfectPalindrome(s, middle-halfMinLoopOdd, middle+halfMinLoopOdd, minPos, maxPos)
				: longestImperfectPalindrome(s, middle-halfMinLoopOdd, middle+halfMinLoopOdd, minPos, maxPos));
	}

	//Ignores palindromes in the loop which may violate minLoop
	private Palindrome longestPalindromeEvenIgnoringLoop(byte[] s, int middle, final int minPos, final int maxPos){
		return (maxMismatches<1 ? longestPerfectPalindrome(s, middle-halfMinLoopEven, middle+halfMinLoopEven+1, minPos, maxPos)
				: longestImperfectPalindrome(s, middle-halfMinLoopEven, middle+halfMinLoopEven+1, minPos, maxPos));
	}
	
	private Palindrome longestPalindromeOdd(byte[] s, int middle, final int minPos, final int maxPos){
		return (maxMismatches<1 ? longestPerfectPalindrome(s, middle-1, middle+1, minPos, maxPos)
				: longestImperfectPalindrome(s, middle-1, middle+1, minPos, maxPos));
	}

	private Palindrome longestPalindromeEven(byte[] s, int middle, final int minPos, final int maxPos){
		return (maxMismatches<1 ? longestPerfectPalindrome(s, middle-1, middle+2, minPos, maxPos)
				: longestImperfectPalindrome(s, middle-1, middle+2, minPos, maxPos));
	}
	
	private Palindrome longestPerfectPalindrome(final byte[] s, final int a0, final int b0, final int minPos, final int maxPos){
//		System.err.println("longestPerfectPalindrome("+a0+", "+b0+")");
		assert(b0>a0) : a0+", "+b0;
		if(a0<minPos || b0>maxPos) {
//			System.err.println("Out of bounds: minPos="+minPos+", maxPos="+maxPos);
			return null;
		}
		final Palindrome p=tempC.clear(), best=tempD.clear();
		int a=a0, b=b0;
		int matches=0, mismatches=0;
//		int lastMismatch=-1;
		for(; a>=minPos && b<=maxPos; a--, b++){
//			System.err.println("a="+a+", b="+b+", matches="+matches);
			if(matches(s[a], s[b])){
				matches++;
			}else{
				if(matches>=minMatches && matches>=best.matches) {

					p.set(a+1, b-1, matches, mismatches);//This is so I can use the compareTo method
					int tail1=p.a-minPos, tail2=maxPos-p.b;
					int tailDif=Tools.absdif(tail1, tail2);
					int loop=p.loop();
					int plen=p.plen();
					if(tailDif<=maxTailDif && tail1<=maxTail && tail2<=maxTail
							&& tail1>=minTail && tail2>=minTail && loop>=minLoop && loop<=maxLoop
							&& plen>=minPLen && p.mismatches<=maxMismatches) {

						//					System.err.println("Considering "+p);
						if(p.compareTo(best)>0) {
							best.setFrom(p);
							assert(tailDif<=maxTailDif);
							//						System.err.println("Set to "+best);
						}
					}else {
//						System.err.println(tail1+", "+tail2+", "+
//								tailDif+", "+loop+", "+plen+", "+p.mismatches);
					}
				}
				matches=0;
//				mismatches|=1;
//				lastMismatch=b;
				if(b-a+1>maxLoop) {break;}
			}
		}
//		System.err.println("Exit loop: a="+a+", b="+b+", matches="+matches);
		while(a<minPos || b>maxPos) {a++; b--;}
		if(matches>=minMatches && matches>=best.matches) {
			p.set(a, b, matches, mismatches);
			//			System.err.println("Considering "+p);
			int tail1=p.a-minPos, tail2=maxPos-p.b;
			int tailDif=Tools.absdif(tail1, tail2);
			int loop=p.loop();
			int plen=p.plen();
			if(tailDif<=maxTailDif && tail1<=maxTail && tail2<=maxTail
					&& tail1>=minTail && tail2>=minTail && loop>=minLoop && loop<=maxLoop
					&& plen>=minPLen && p.mismatches<=maxMismatches) {
				if(p.compareTo(best)>0) {
					assert(tailDif<=maxTailDif);
					best.setFrom(p);
					//				System.err.println("Set to "+best);
				}
			}
		}

//		System.err.println("Returning "+best);
		return best;
	}
	
	/** TODO: Make a new dynamic programming version with +10 for match and -9 for mismatch */
	private Palindrome longestImperfectPalindrome(final byte[] s, final int a0, final int b0, final int minPos, final int maxPos){
//		System.err.println("longestPerfectPalindrome("+a0+", "+b0+")");
		assert(b0>a0) : a0+", "+b0;
		if(a0<minPos || b0>maxPos) {
//			System.err.println("Out of bounds: minPos="+minPos+", maxPos="+maxPos);
			return null;
		}
		final Palindrome p=tempC.clear(), best=tempD.clear();
		int a=a0, b=b0;
		int a2=a0, b2=b0;
		int matches=0, mismatches=0;

		for(; a>=minPos && b<=maxPos; a--, b++){
//			System.err.println("a="+a+", b="+b+", matches="+matches);
			if(matches(s[a], s[b])){
				matches++;
			}else{
//				System.err.println(matches+", "+mismatches+", "+a+"-"+a2+", "+b+"-"+b2);
			
				//Shrink palindrome inner bounds until mismatches is under thresh
				for(; mismatches>maxMismatches; a2--, b2++) {
					if(matches(s[a2], s[b2])) {
						matches--;
					}else {
						mismatches--;
					}
				}
				//Shrink palindrome inner bounds until the outermost base is a match
				for(; mismatches>0 && !matches(s[a2], s[b2]); a2--, b2++) {
					mismatches--;
				}
				assert(mismatches<=maxMismatches);
				assert(matches+mismatches==a2-a) : matches+", "+mismatches+", "+a+"-"+a2+", "+b+"-"+b2;
				assert(matches==0 || matches(s[a2],s[b2]));
				if(matches>=minMatches && matches>=best.matches) {

					p.set(a+1, b-1, matches, mismatches);//This is so I can use the compareTo method
					while(p.mismatches>0 && !matches(s[p.a], s[p.b])) {
						p.mismatches--; p.a++; p.b--;
					}
					assert(matches(s[p.a],s[p.b])) : matches+", "+minMatches;
					int tail1=p.a-minPos, tail2=maxPos-p.b;
					int tailDif=Tools.absdif(tail1, tail2);
					int loop=p.loop();
					int plen=p.plen();
					if(tailDif<=maxTailDif && tail1<=maxTail && tail2<=maxTail
							&& tail1>=minTail && tail2>=minTail && loop>=minLoop && loop<=maxLoop
							&& plen>=minPLen && p.mismatches<=maxMismatches) {

						//					System.err.println("Considering "+p);
						if(p.compareTo(best)>0) {
							best.setFrom(p);
							//						System.err.println("Set to "+best);
						}
					}else {
//						System.err.println(tail1+", "+tail2+", "+
//								tailDif+", "+loop+", "+plen+", "+p.mismatches);
					}
				}
//				mismatches|=1;
//				lastMismatch=b;
				mismatches++;
				assert(matches==0 || matches+mismatches==a2-a+1) : matches+", "+mismatches+", "+a+"-"+a2+", "+b2+"-"+b;
				
				if(b-a+1>maxLoop) {break;}
			}
		}
//		System.err.println("Exit loop: a="+a+", b="+b+", matches="+matches);
		while(a<minPos || b>maxPos) {a++; b--;}
		assert(matches==0 || matches+mismatches==a2-a+1) : matches+", "+mismatches+", "+a+"-"+a2+", "+b2+"-"+b;
		
		
		//Shrink palindrome inner bounds until mismatches is under thresh
		for(; mismatches>maxMismatches; a2--, b2++) {
			if(matches(s[a2], s[b2])) {
				matches--;
			}else {
				mismatches--;
			}
		}
		//Shrink palindrome inner bounds until the outermost base is a match
		for(; mismatches>0 && !matches(s[a2], s[b2]); a2--, b2++) {
			mismatches--;
		}
		while(mismatches>0 && !matches(s[a], s[b])) {
			a++; b--; mismatches--;
		}
		assert(mismatches<=maxMismatches);
		assert(matches==0 || matches+mismatches==a2-a+1) : matches+", "+mismatches+", "+a+"-"+a2+", "+b2+"-"+b;
		assert(matches==0 || matches(s[a],s[b]));
		assert(matches==0 || matches(s[a2],s[b2]));
		
		if(matches>=minMatches && matches>=best.matches) {
			p.set(a, b, matches, mismatches);
			//			System.err.println("Considering "+p);
			int tail1=p.a-minPos, tail2=maxPos-p.b;
			int tailDif=Tools.absdif(tail1, tail2);
			int loop=p.loop();
			int plen=p.plen();
			if(tailDif<=maxTailDif && tail1<=maxTail && tail2<=maxTail
					&& tail1>=minTail && tail2>=minTail && loop>=minLoop && loop<=maxLoop
					&& plen>=minPLen && p.mismatches<=maxMismatches) {
				//			System.err.println("Considering "+p);
				if(p.compareTo(best)>0) {
					best.setFrom(p);
					//				System.err.println("Set to "+best);
				}
			}
		}

//		System.err.println("Returning "+best);
		return best;
	}
	
	/*--------------------------------------------------------------*/
	/*----------------        Initialization        ----------------*/
	/*--------------------------------------------------------------*/
	
	@Override
	public String toString() {
		return "minMatches="+minMatches+", maxMismatches="+maxMismatches+", minLoop="+minLoop+
				", maxLoop="+maxLoop+", minPLen="+minPLen+
				", halfMinLoopOdd="+halfMinLoopOdd+", halfMinLoopEven="+halfMinLoopEven+
				", halfMinLength="+halfMinLength+
				", minTail="+minTail+", maxTail="+maxTail+", maxTailDif="+maxTailDif;
	}
	
	/*--------------------------------------------------------------*/
	/*----------------        Initialization        ----------------*/
	/*--------------------------------------------------------------*/
	
	static boolean matches(byte a, byte b) {
		return a==(rcomp ? baseToComp[b] : b);
	}
	
	static byte[] makeBaseToComp() {
		byte[] array=new byte[128];
		Arrays.fill(array, (byte)'~');//Nothing should match this since it is invalid
		array['A']=array['a']='T';
		array['C']=array['c']='G';
		array['G']=array['g']='C';
		array['T']=array['t']=array['U']=array['u']='A';
		return array;
	}
	
	/*--------------------------------------------------------------*/
	/*----------------            Fields            ----------------*/
	/*--------------------------------------------------------------*/
	
	private final Palindrome tempB=new Palindrome();
	private final Palindrome tempC=new Palindrome();
	private final Palindrome tempD=new Palindrome();
	private final IntList mismatchList=new IntList();

	public PalindromeTracker tracker=new PalindromeTracker();
	public PalindromeTracker trackerFull=new PalindromeTracker();

	public final int maxMismatches;
	public final int minMatches;//Should be >=1
	public final int minLoop;//must be>=0
	public final int maxLoop;
	public final int minPLen;
	
	public final int minTail;
	public final int maxTail;
	public final int maxTailDif;
	
	public final int halfMinLoopOdd;
	public final int halfMinLoopEven;
	public final int halfMinLength;
	
	/*--------------------------------------------------------------*/
	/*----------------        Static Fields         ----------------*/
	/*--------------------------------------------------------------*/
	
	static final boolean rcomp=true;
	static final byte[] baseToComp=makeBaseToComp();
	
}