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
|
package fun;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
public class Palindrome {
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+1);
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;
}
static boolean rcomp=false;
static int maxMismatches=0;
static int maxLoop=0;
static final char[] baseToComp=makeBaseToComp();
static int a_, b_;
}
|