Debian Codegraph @

current/structures/Crispr.java

raw symbols SCIP
1 package structures;
2
3 import repeat.Palindrome;
4 import shared.Tools;
5
6 public class Crispr implements Comparable<Crispr> {
7
8 public Crispr() {}
9
10 public Crispr(int a1, int b1, int a2, int b2) {
11 a=new Range(a1, b1);
12 b=new Range(a2, b2);
13 }
14
15 public int gap() {
16 return b.a-a.b-1;
17 }
18
19 public boolean containsInGap(Range r) {
20 return r.a>a.b && r.b<b.a;
21 }
22
23 public String toString() {
24 return appendTo(new ByteBuilder()).toString();
25 }
26
27 public String toString(byte[] bases) {
28 ByteBuilder bb=new ByteBuilder();
29 appendTo(bb, bases.length, bases);
30 return bb.toString();
31 }
32
33 public ByteBuilder appendTo(ByteBuilder bb) {
34 return appendTo(bb, 0, null);
35 }
36
37 public ByteBuilder appendTo(ByteBuilder bb, int len, byte[] bases) {
38 bb.append('[').append(a.a).dash().append(a.b).comma();
39 bb.append(b.a).dash().append(b.b);
40 if(len>0) {bb.semi().append(len);}
41 bb.append(']');
42 if(pa!=null) {pa.appendTo(bb.comma(), a.a, a.b);}
43 else if(pb!=null) {pb.appendTo(bb, a.a, a.b);}
44 if(bases!=null) {
45 bb.nl();
46 for(int i=a.a; i<=a.b; i++) {bb.append(bases[i]);}
47 bb.nl();
48 for(int i=b.a; i<=b.b; i++) {bb.append(bases[i]);}
49 }
50 return bb;
51 }
52
53 public void set(int a1, int b1, int a2, int b2) {
54 a.a=a1;
55 a.b=b1;
56 b.a=a2;
57 b.b=b2;
58
59 //These could happen and will probably be dealt with later, but are good to prevent if possible.
60 assert(a2>b1) : this;
61 assert(a1<b1) : this;
62 assert(a2<b2) : this;
63 }
64
65 public int edgeDist(int length) {
66 return Tools.min(a.a, length-b.b-1);
67 }
68
69 public boolean spans(int length) {
70 return a.a==0 && b.b+1==length;
71 }
72
73 public int minLength() {
74 return Tools.min(a.length(), b.length());
75 }
76
77 public int maxLength() {
78 return Tools.max(a.length(), b.length());
79 }
80
81 public int lengthDif() {
82 return b.length()-a.length();
83 }
84
85 public void fixBounds(int length) {
86 a.fixBounds(length);
87 b.fixBounds(length);
88 }
89
90 public float maxScore() {
91 return Tools.max(scoreA, scoreB);
92 }
93
94 public boolean sameLength() {
95 return a.length()==b.length();
96 }
97
98 // public boolean internal(byte[] seq) {
99 // return !touchesEdge(seq.length);
100 // }
101
102 public boolean internal(int seqLen) {
103 return a.a>0 && b.b<seqLen-1;
104 }
105
106 // public boolean touchesEdge(byte[] seq) {
107 // return touchesEdge(seq.length);
108 // }
109
110 public boolean touchesEdge(int seqLen) {
111 return a.a<=0 || b.b>=seqLen-1;
112 }
113
114 public boolean touchesBothEnds(int seqLen) {
115 return a.a<=0 && b.b>=seqLen-1;
116 }
117
118 @Override
119 /**
120 * So this sort of compares them but it's not really optimal.
121 * Not clear how to improve it though.
122 * @param o
123 * @return
124 */
125 public int compareTo(Crispr o) {
126 int x=a.compareTo(o.a);
127 if(x!=0) {return x;}
128 return b.compareTo(o.b);
129 }
130
131 public Range a;
132 public Range b;
133 public Palindrome pa, pb;
134 public float scoreA, scoreB;
135 public int matches=0, mismatches=0;
136 public int trimmedConsensus=0;
137 public int extendedConsensus=0;
138
139 }