Debian Codegraph @

current/repeat/Palindrome.java

raw symbols SCIP
1 package repeat;
2
3 import shared.Tools;
4 import structures.ByteBuilder;
5
6 public class Palindrome implements Comparable<Palindrome>, Cloneable {
7
8 public Palindrome(){}
9
10 public Palindrome(int a_, int b_, int matches_, int mismatches_){
11 set(a_, b_, matches_, mismatches_);
12 }
13
14 public void set(int a_, int b_, int matches_, int mismatches_){
15 a=a_;
16 b=b_;
17 matches=matches_;
18 mismatches=mismatches_;
19 }
20
21 public void setFrom(Palindrome p){
22 assert(p!=this);
23 set(p.a, p.b, p.matches, p.mismatches);
24 }
25
26 public String toString() {
27 return "("+a+"-"+b+",matches="+matches+",mismatches="+mismatches+")";
28 }
29
30 public String toString(final int a0, final int b0) {
31 int tail1=a-a0, tail2=b0-b;
32 return "("+a+"-"+b+",matches="+matches+",mismatches="+mismatches+
33 ",loop="+loop()+",tail1="+Tools.min(tail1,tail2)+
34 ",tail2="+Tools.max(tail1,tail2)+",taildif="+Tools.absdif(tail1,tail2)+")";
35 }
36
37 // public ByteBuilder appendTo(ByteBuilder bb, final int a0, final int b0) {
38 // int tail1=a-a0, tail2=b0-b;
39 // int tmin=Tools.min(tail1, tail2), tmax=Tools.max(tail1, tail2);
40 // int tdif=tail2-tail1;
41 // bb.append('(').append(a).dash().append(b).comma();
42 // bb.append('m','=').append(matches).comma();
43 // bb.append('m','m','=').append(mismatches).comma();
44 // bb.append('l','=').append(loop()).comma();
45 // bb.append('t','1','=').append(tmin).comma();
46 // bb.append('t','2','=').append(tmax).comma();
47 // bb.append('t','d','=').append(tdif);
48 // return bb.append(')');
49 // }
50
51 public ByteBuilder appendTo(ByteBuilder bb, final int a0, final int b0) {
52 int tail1=a-a0, tail2=b0-b;
53 int tmin=Tools.min(tail1, tail2), tmax=Tools.max(tail1, tail2);
54 // int tdif=tail2-tail1;
55 bb.append('(').append(a).dash().append(b).comma();
56 bb.append('P','=').append(matches+mismatches).comma();
57 bb.append('M','=').append(matches).comma();
58 bb.append('L','=').append(loop()).comma();
59 bb.append('T','=').append(tmin).plus().append(tmax);
60 return bb.append(')');
61 }
62
63 public int plen() {return matches+mismatches;}
64
65 public int length() {return b-a+1;}
66
67 public int loop() {return length()-2*matches;}
68
69 public Palindrome clear() {
70 a=b=matches=mismatches=0;
71 return this;
72 }
73
74 public Palindrome clone() {
75 Palindrome p=null;
76 try {p=(Palindrome) super.clone();}
77 catch (CloneNotSupportedException e) {e.printStackTrace();}
78 return p;
79 }
80
81 /**
82 * The greater of the two will have more matches, or fewer mismatches,
83 * or be longer, or more to the left.
84 */
85 public int compareTo(Palindrome p) {
86 if(p==null) {return 1;}
87 if(matches!=p.matches) {return matches-p.matches;}
88 if(mismatches!=p.mismatches) {return p.mismatches-mismatches;}
89 int lenDif=length()-p.length();
90 if(lenDif!=0) {return lenDif;}
91 return p.a-a;
92 }
93
94 /** Start location */
95 public int a=0;
96 /** Stop location */
97 public int b=0;
98 /** Length of the palindromic sequence, excluding the loop */
99 public int matches=0;
100 /** Number of mismatches in the palindrome */
101 public int mismatches=0;
102
103 }