| 1 |
package fun; |
| 2 |
|
| 3 |
import java.io.BufferedReader; |
| 4 |
import java.io.File; |
| 5 |
import java.io.FileReader; |
| 6 |
import java.util.ArrayList; |
| 7 |
import java.util.Arrays; |
| 8 |
|
| 9 |
public class Palindrome { |
| 10 |
|
| 11 |
public static void main(String[] args){ |
| 12 |
|
| 13 |
ArrayList<String> sequences=new ArrayList<String>(); |
| 14 |
|
| 15 |
for(String s : args) { |
| 16 |
if(s.equalsIgnoreCase("rcomp") || s.equalsIgnoreCase("rc")) { |
| 17 |
rcomp=true; |
| 18 |
}else if(Character.isDigit(s.charAt(0))) { |
| 19 |
maxMismatches=Integer.parseInt(s); |
| 20 |
}else if(s.startsWith("loop")) { |
| 21 |
maxLoop=Integer.parseInt(s.split("=")[1]); |
| 22 |
}else if(new File(args[0]).exists()) { |
| 23 |
sequences.addAll(getSequence(args[0])); |
| 24 |
}else { |
| 25 |
sequences.add(s); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
String longest=""; |
| 30 |
for(String s : sequences) { |
| 31 |
String p; |
| 32 |
if(maxLoop<1) { |
| 33 |
p=longestPalindrome(s); |
| 34 |
}else { |
| 35 |
p=longestPalindrome(s, maxLoop); |
| 36 |
} |
| 37 |
if(p.length()>longest.length()) { |
| 38 |
longest=p; |
| 39 |
} |
| 40 |
} |
| 41 |
System.out.println("Longest palindrome is length "+longest.length()+":\n'"+longest+"'"); |
| 42 |
} |
| 43 |
|
| 44 |
public static ArrayList<String> getSequence(String fname){ |
| 45 |
ArrayList<String> list=new ArrayList<String>(); |
| 46 |
try { |
| 47 |
final BufferedReader reader=new BufferedReader(new FileReader(fname)); |
| 48 |
|
| 49 |
StringBuilder sb=new StringBuilder(); |
| 50 |
for(String line=reader.readLine(); line!=null; line=reader.readLine()) { |
| 51 |
if(line.length()>0) { |
| 52 |
if(line.charAt(0)=='>'){ |
| 53 |
if(sb.length()>0) { |
| 54 |
list.add(sb.toString()); |
| 55 |
sb.setLength(0); |
| 56 |
} |
| 57 |
}else{ |
| 58 |
sb.append(line); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
if(sb.length()>0) {list.add(sb.toString());} |
| 63 |
reader.close(); |
| 64 |
}catch(Exception e){ |
| 65 |
|
| 66 |
} |
| 67 |
return list; |
| 68 |
} |
| 69 |
|
| 70 |
public static String longestPalindrome(String s){ |
| 71 |
int longestLength=0; |
| 72 |
int longestStart=0; |
| 73 |
String p=""; |
| 74 |
for(int i=0; i<s.length(); i++){ |
| 75 |
int lenEven=palindromeLengthEven(s, i); |
| 76 |
if(lenEven>longestLength){ |
| 77 |
longestLength=lenEven; |
| 78 |
longestStart=i-lenEven/2+1; |
| 79 |
int a=longestStart, b=longestStart+longestLength; |
| 80 |
p=s.substring(a, b); |
| 81 |
} |
| 82 |
int lenOdd=palindromeLengthOdd(s, i); |
| 83 |
if(lenOdd>longestLength){ |
| 84 |
longestLength=lenOdd; |
| 85 |
longestStart=i-lenOdd/2; |
| 86 |
p=s.substring(longestStart, longestStart+longestLength); |
| 87 |
} |
| 88 |
} |
| 89 |
return p; |
| 90 |
} |
| 91 |
|
| 92 |
public static String longestPalindrome(String s, int maxloop){ |
| 93 |
int longestLength=0; |
| 94 |
int longestStart=0; |
| 95 |
String p=""; |
| 96 |
for(int loop=0; loop<=maxloop; loop++) { |
| 97 |
for(int i=0; i<s.length(); i++){ |
| 98 |
if((loop&1)==1) { |
| 99 |
int lenOdd=palindromeLengthOdd(s, i, i+loop+1); |
| 100 |
if(lenOdd>longestLength){ |
| 101 |
longestLength=lenOdd; |
| 102 |
|
| 103 |
|
| 104 |
p=s.substring(a_, b_+1); |
| 105 |
} |
| 106 |
}else { |
| 107 |
int lenEven=palindromeLengthEven(s, i, i+1+loop); |
| 108 |
if(lenEven>longestLength){ |
| 109 |
longestLength=lenEven; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
p=s.substring(a_, b_+1); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
return p; |
| 119 |
} |
| 120 |
|
| 121 |
public static int palindromeLengthOdd(String s, int middle){ |
| 122 |
return palindromeLengthOdd(s, middle, middle); |
| 123 |
} |
| 124 |
public static int palindromeLengthOdd(String s, int a, int b){ |
| 125 |
int length=b-a-1; |
| 126 |
int mismatches=0; |
| 127 |
while(a>=0 && b<s.length() && mismatches<=maxMismatches){ |
| 128 |
if(!matches(s.charAt(a), s.charAt(b))){ |
| 129 |
mismatches++; |
| 130 |
if(mismatches>maxMismatches) {break;} |
| 131 |
} |
| 132 |
length+=2; |
| 133 |
a--; |
| 134 |
b++; |
| 135 |
} |
| 136 |
if(a<0 || b>=s.length() || mismatches>maxMismatches) {a++; b--;} |
| 137 |
a_=a; |
| 138 |
b_=b; |
| 139 |
if(length==1 && rcomp && maxMismatches<1) {return 0;} |
| 140 |
return length<0 ? 0 : length; |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
public static int palindromeLengthEven(String s, int middle){ |
| 145 |
return palindromeLengthEven(s, middle, middle+1); |
| 146 |
} |
| 147 |
public static int palindromeLengthEven(String s, int a, int b){ |
| 148 |
int length=b-a-1; |
| 149 |
int mismatches=0; |
| 150 |
while(a>=0 && b<s.length() && mismatches<=maxMismatches){ |
| 151 |
if(!matches(s.charAt(a), s.charAt(b))){ |
| 152 |
mismatches++; |
| 153 |
if(mismatches>maxMismatches) {break;} |
| 154 |
} |
| 155 |
length+=2; |
| 156 |
a--; |
| 157 |
b++; |
| 158 |
} |
| 159 |
if(a<0 || b>=s.length() || mismatches>maxMismatches) {a++; b--;} |
| 160 |
a_=a; |
| 161 |
b_=b; |
| 162 |
return length; |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
static boolean matches(char a, char b) { |
| 167 |
return a==(rcomp ? baseToComp[b] : b); |
| 168 |
} |
| 169 |
|
| 170 |
static char[] makeBaseToComp() { |
| 171 |
char[] array=new char[128]; |
| 172 |
Arrays.fill(array, 'N'); |
| 173 |
array['A']=array['a']='T'; |
| 174 |
array['C']=array['c']='G'; |
| 175 |
array['G']=array['g']='C'; |
| 176 |
array['T']=array['t']=array['U']=array['u']='A'; |
| 177 |
return array; |
| 178 |
} |
| 179 |
|
| 180 |
static boolean rcomp=false; |
| 181 |
static int maxMismatches=0; |
| 182 |
static int maxLoop=0; |
| 183 |
static final char[] baseToComp=makeBaseToComp(); |
| 184 |
|
| 185 |
static int a_, b_; |
| 186 |
|
| 187 |
} |