File: Palindrome2.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 (203 lines) | stat: -rwxr-xr-x 5,199 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
package fun;

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

public class Palindrome2 {
	
	public static void main(String[] args){
		
		ArrayList<String> sequences=new ArrayList<String>();
		
		for(String s : args) {
			if(s.equalsIgnoreCase("rcomp") || s.equalsIgnoreCase("rc")) {
				rcomp=true;
			}else if(Character.isDigit(s.charAt(0))) {
				maxMismatches=Integer.parseInt(s);
			}else if(s.startsWith("loop")) {
				maxLoop=Integer.parseInt(s.split("=")[1]);
			}else if(new File(args[0]).exists()) {
				sequences.addAll(getSequence(args[0]));
			}else {
				sequences.add(s);
			}
		}
		
		String longest="";
		for(String s : sequences) {
			String p;
			if(maxLoop<1) {
				p=longestPalindrome(s);
			}else {
				p=longestPalindrome(s, maxLoop);
			}
			if(p.length()>longest.length()) {
				longest=p;
			}
		}
		System.out.println("Longest palindrome is length "+longest.length()+":\n'"+longest+"'");
	}
	
	public static ArrayList<String> getSequence(String fname){
		ArrayList<String> list=new ArrayList<String>();
		try {
			final BufferedReader reader=new BufferedReader(new FileReader(fname));

			StringBuilder sb=new StringBuilder();
			for(String line=reader.readLine(); line!=null; line=reader.readLine()) {
				if(line.length()>0) {
					if(line.charAt(0)=='>'){
						if(sb.length()>0) {
							list.add(sb.toString());
							sb.setLength(0);
						}
					}else{
						sb.append(line);
					}
				}
			}
			if(sb.length()>0) {list.add(sb.toString());}
			reader.close();
		}catch(Exception e){
			
		}
		return list;
	}
	
	public static String longestPalindrome(String s){
		int longestLength=0;
		int longestStart=0;
		String p="";
		for(int i=0; i<s.length(); i++){
			int lenEven=palindromeLengthEven(s, i);
			if(lenEven>longestLength){
				longestLength=lenEven;
				longestStart=i-lenEven/2+1;
				int a=longestStart, b=longestStart+longestLength;
				p=s.substring(a, b);
			}
			int lenOdd=palindromeLengthOdd(s, i);
			if(lenOdd>longestLength){
				longestLength=lenOdd;
				longestStart=i-lenOdd/2;
				p=s.substring(longestStart, longestStart+longestLength);
			}
		}
		return p;
	}
	
	public static String longestPalindrome(String s, int maxloop){
		int longestLength=0;
		int longestStart=0;
		String p="";
		for(int loop=0; loop<=maxloop; loop++) {
			for(int i=0; i<s.length(); i++){
				if((loop&1)==1) {//odd
					int lenOdd=palindromeLengthOdd(s, i, i+loop);
					if(lenOdd>longestLength){
						longestLength=lenOdd;
//						longestStart=i-lenOdd/2;
//						p=s.substring(longestStart, longestStart+longestLength-loop);
						p=s.substring(a_, b_+1);
					}
				}else {//even
					int lenEven=palindromeLengthEven(s, i, i+1+loop);
					if(lenEven>longestLength){
						longestLength=lenEven;
//						longestStart=i-lenEven/2+1;
//						int a=longestStart, b=longestStart+longestLength-loop;
//						p=s.substring(a, b);
						p=s.substring(a_, b_+1);
					}
				}
			}
		}
		return p;
	}
	
	public static int palindromeLengthOdd(String s, int middle){
		return palindromeLengthOdd(s, middle, middle);
	}
	public static int palindromeLengthOdd(String s, int a, int b){
		int length=b-a-1;
		int mismatches=0;
		while(a>=0 && b<s.length() && mismatches<=maxMismatches){
			if(!matches(s.charAt(a), s.charAt(b))){
				mismatches++;
				if(mismatches>maxMismatches) {break;}
			}
			length+=2;
			a--;
			b++;
		}
		if(a<0 || b>=s.length() || mismatches>maxMismatches) {a++; b--;}
		a_=a;
		b_=b;
		if(length==1 && rcomp && maxMismatches<1) {return 0;}
		return length<0 ? 0 : length;
//		return b-a+1;
	}

	public static int palindromeLengthEven(String s, int middle){
		return palindromeLengthEven(s, middle, middle+1);
	}
	public static int palindromeLengthEven(String s, int a, int b){
		int length=b-a-1;
		int mismatches=0;
		while(a>=0 && b<s.length() && mismatches<=maxMismatches){
			if(!matches(s.charAt(a), s.charAt(b))){
				mismatches++;
				if(mismatches>maxMismatches) {break;}
			}
			length+=2;
			a--;
			b++;
		}
		if(a<0 || b>=s.length() || mismatches>maxMismatches) {a++; b--;}
		a_=a;
		b_=b;
		return length;
//		return b-a+1;
	}
	
	static boolean matches(char a, char b) {
		return a==(rcomp ? baseToComp[b] : b);
	}
	
	static char[] makeBaseToComp() {
		char[] array=new char[128];
		Arrays.fill(array, 'N');
		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;
	}
	
	private static class Drome{
		
		public Drome(String ref, int start, int stop) {
			s=ref.substring(start, stop+1);
			final int half=s.length()/2;
			for(int i=0, j=s.length()-1; i<half; i++, j--) {
				final char ci=s.charAt(i);
				final char cj=s.charAt(j);
			}
		}
		
		String s;
		int loop;
		int mismatches;
	}
	
	static boolean rcomp=false;
	static int maxMismatches=0;
	static int maxLoop=0;
	static final char[] baseToComp=makeBaseToComp();
	
	static int a_, b_;
	
}