| 1 |
package repeat; |
| 2 |
|
| 3 |
import java.util.Arrays; |
| 4 |
import java.util.Comparator; |
| 5 |
|
| 6 |
import dna.AminoAcid; |
| 7 |
import shared.Tools; |
| 8 |
import stream.Read; |
| 9 |
import structures.ByteBuilder; |
| 10 |
import structures.CRange; |
| 11 |
import tracker.EntropyTracker; |
| 12 |
|
| 13 |
public class Repeat implements Cloneable, Comparable<Repeat> { |
| 14 |
|
| 15 |
public Repeat(Read contig_, int start_, int depth_, int k_, int maxGap_, int minRepeat_, char type_) { |
| 16 |
contig=contig_; |
| 17 |
contigNum=(contig==null ? -1 : contig.numericID); |
| 18 |
contigName=(contig==null ? null : contig.id); |
| 19 |
start=start_; |
| 20 |
depth=depth_; |
| 21 |
k=k_; |
| 22 |
maxGap=maxGap_; |
| 23 |
minRepeat=minRepeat_; |
| 24 |
type=type_; |
| 25 |
} |
| 26 |
|
| 27 |
public Repeat clone() { |
| 28 |
assert(start>=0) : this; |
| 29 |
try { |
| 30 |
return (Repeat)super.clone(); |
| 31 |
} catch (CloneNotSupportedException e) { |
| 32 |
throw new RuntimeException(e); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public void clear() { |
| 39 |
contigNum=start=stop=-1; |
| 40 |
gapLen=gapBP=gapCount=0; |
| 41 |
|
| 42 |
depthSum=tipdist=0; |
| 43 |
minDepth=Integer.MAX_VALUE; |
| 44 |
maxDepth=-1; |
| 45 |
|
| 46 |
contig=null; |
| 47 |
contigName=null; |
| 48 |
gc=entropy=0; |
| 49 |
} |
| 50 |
|
| 51 |
public Repeat increment(Read currentContig, int pos, int currentDepth) { |
| 52 |
assert(currentDepth>=depth); |
| 53 |
final int gap=pos-stop-1; |
| 54 |
if(contigNum==currentContig.numericID && gap<=maxGap) { |
| 55 |
|
| 56 |
stop=pos; |
| 57 |
gapLen+=gap; |
| 58 |
gapCount+=(gap>0 ? 1 : 0); |
| 59 |
gapBP+=(gap>=k ? gap-k+1 : 0); |
| 60 |
depthSum+=currentDepth; |
| 61 |
maxDepth=Tools.max(currentDepth, maxDepth); |
| 62 |
return null; |
| 63 |
} |
| 64 |
|
| 65 |
Repeat r=null; |
| 66 |
if(contigNum>=0 && length()>=minRepeat) {r=this.clone();} |
| 67 |
|
| 68 |
clear(); |
| 69 |
contigNum=currentContig.numericID; |
| 70 |
start=pos-k+1; |
| 71 |
assert(start>=0) : start+", "+pos+", "+k; |
| 72 |
stop=pos; |
| 73 |
assert(stop<currentContig.bases.length) : stop+", "+currentContig.length(); |
| 74 |
minDepth=maxDepth=depth; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
contig=currentContig; |
| 79 |
contigName=currentContig.name(); |
| 80 |
|
| 81 |
return r; |
| 82 |
} |
| 83 |
|
| 84 |
public int length() { |
| 85 |
return start<0 ? 0 : stop-start+1; |
| 86 |
} |
| 87 |
|
| 88 |
public final boolean overlaps(Repeat r) { |
| 89 |
return (contigNum==r.contigNum && start<=r.stop && stop>=r.start); |
| 90 |
} |
| 91 |
|
| 92 |
public final boolean spans(Repeat r) { |
| 93 |
return (contigNum==r.contigNum && start<=r.start && stop>=r.stop); |
| 94 |
} |
| 95 |
|
| 96 |
public final boolean subsumes(Repeat r, boolean weak) { |
| 97 |
return spans(r) && depth>=r.depth && (gapLen<=r.gapLen || weak); |
| 98 |
} |
| 99 |
|
| 100 |
void calcStats(EntropyTracker et) { |
| 101 |
et.clear(); |
| 102 |
int[] acgtn=new int[5]; |
| 103 |
byte[] bases=contig.bases; |
| 104 |
assert(stop<bases.length && start>=0) : start+", "+stop+", "+bases.length+"\n"+this; |
| 105 |
for(int i=start; i<=stop; i++) { |
| 106 |
byte b=bases[i]; |
| 107 |
int num=AminoAcid.baseToNumber4[b]; |
| 108 |
acgtn[num]++; |
| 109 |
} |
| 110 |
int atCount=acgtn[0]+acgtn[3]; |
| 111 |
int gcCount=acgtn[1]+acgtn[2]; |
| 112 |
gc=(gcCount)/(float)Tools.max(1, atCount+gcCount); |
| 113 |
entropy=et.averageEntropy(bases, false, start, stop); |
| 114 |
tipdist=Tools.min(start, bases.length-stop-1); |
| 115 |
} |
| 116 |
|
| 117 |
void setSeq(ByteBuilder bb) { |
| 118 |
bb.clear(); |
| 119 |
appendPreview(bb); |
| 120 |
seq=bb.toBytes(); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public CRange toRange() { |
| 126 |
return new CRange(contigNum, start, stop, contig); |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public int compareTo(Repeat r) { |
| 131 |
if(depth!=r.depth) {return depth-r.depth;} |
| 132 |
int lenDif=length()-r.length(); |
| 133 |
if(lenDif!=0) {return lenDif;} |
| 134 |
if(gapLen!=r.gapLen) {return r.gapLen-gapLen;} |
| 135 |
if(entropy!=r.entropy) {return entropy>r.entropy ? 1 : -1;} |
| 136 |
if(gc!=r.gc) {return gc<r.gc ? 1 : -1;} |
| 137 |
if(contigNum!=r.contigNum) {return r.contigNum>contigNum ? 1 : -1;} |
| 138 |
return r.start-start; |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public String toString() { |
| 143 |
return toBytes().toString(); |
| 144 |
} |
| 145 |
|
| 146 |
public ByteBuilder toBytes() { |
| 147 |
ByteBuilder bb=new ByteBuilder(); |
| 148 |
return appendTo(bb); |
| 149 |
} |
| 150 |
|
| 151 |
public ByteBuilder appendTo(ByteBuilder bb) { |
| 152 |
bb.append(depth).tab().append(length()).tab(); |
| 153 |
bb.append(gapLen).tab().append(gapBP).tab().append(gapCount).tab(); |
| 154 |
bb.append(contigNum).tab().append(start).tab().append(stop).tab().append(maxDepth).tab(); |
| 155 |
bb.append(tipdist).tab().append(gc, 3).tab().append(entropy, 3).tab().append(contigName); |
| 156 |
|
| 157 |
if(SEQ_AFFIX_LEN>0) { |
| 158 |
bb.tab(); |
| 159 |
appendPreview(bb); |
| 160 |
} |
| 161 |
return bb; |
| 162 |
} |
| 163 |
|
| 164 |
ByteBuilder appendPreview(ByteBuilder bb) { |
| 165 |
if(seq!=null) { |
| 166 |
bb.append(seq); |
| 167 |
}else { |
| 168 |
byte[] bases=contig.bases; |
| 169 |
assert(stop<bases.length && start>=0) : start+", "+stop+", "+bases.length; |
| 170 |
int lim=SEQ_AFFIX_LEN; |
| 171 |
if(length()<=2*lim+3) { |
| 172 |
for(int i=start; i<=stop; i++) {bb.append(bases[i]);} |
| 173 |
}else { |
| 174 |
for(int i=0; i<lim; i++) {bb.append(bases[start+i]);} |
| 175 |
bb.append('.').append('.').append('.'); |
| 176 |
for(int i=stop-lim+1; i<=stop; i++) {bb.append(bases[i]);} |
| 177 |
} |
| 178 |
} |
| 179 |
return bb; |
| 180 |
} |
| 181 |
|
| 182 |
public Read toRead(){ |
| 183 |
Read r=new Read(fullSequence(), null, readHeader(), 0L); |
| 184 |
return r; |
| 185 |
} |
| 186 |
|
| 187 |
public byte[] fullSequence(){ |
| 188 |
return Arrays.copyOfRange(contig.bases, start, stop+1); |
| 189 |
} |
| 190 |
|
| 191 |
public String readHeader() { |
| 192 |
ByteBuilder bb=new ByteBuilder(); |
| 193 |
bb.append("seq").append(contigNum).under().append(start).append('-').append(stop); |
| 194 |
bb.under().append("depth").append(depth).under().append("max").append(maxDepth); |
| 195 |
bb.under().append("gaplen").append(gapLen); |
| 196 |
bb.under().append("gc").append(gc, 3).under().append("entropy").append(entropy, 3); |
| 197 |
bb.tab().append(contigName); |
| 198 |
return bb.toString(); |
| 199 |
} |
| 200 |
|
| 201 |
public static String tsvHeader() { |
| 202 |
ByteBuilder bb=new ByteBuilder(); |
| 203 |
bb.append("#"); |
| 204 |
bb.append("depth").tab().append("length").tab(); |
| 205 |
bb.append("gapLen").tab().append("gapBP").tab().append("gaps").tab(); |
| 206 |
bb.append("contig").tab().append("start").tab().append("stop").tab().append("maxDp").tab(); |
| 207 |
bb.append("tipDist").tab().append("gc").tab().append("entropy").tab().append("cName"); |
| 208 |
bb.tab().append("seq"); |
| 209 |
return bb.toString(); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public static class PosComparator implements Comparator<Repeat>{ |
| 216 |
|
| 217 |
@Override |
| 218 |
public int compare(Repeat a, Repeat b) { |
| 219 |
|
| 220 |
if(a.contigNum!=b.contigNum) {return a.contigNum>b.contigNum ? 1 : -1;} |
| 221 |
|
| 222 |
if(a.start!=b.start) {return a.start-b.start;} |
| 223 |
|
| 224 |
if(a.stop!=b.stop) {return a.stop-b.stop;} |
| 225 |
|
| 226 |
return b.depth-a.depth; |
| 227 |
} |
| 228 |
|
| 229 |
public static final PosComparator comparator=new PosComparator(); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
public static class PosComparator2 implements Comparator<Repeat>{ |
| 234 |
|
| 235 |
@Override |
| 236 |
public int compare(Repeat a, Repeat b) { |
| 237 |
|
| 238 |
if(a.contigNum!=b.contigNum) {return a.contigNum>b.contigNum ? 1 : -1;} |
| 239 |
|
| 240 |
if(a.start!=b.start) {return a.start-b.start;} |
| 241 |
|
| 242 |
if(a.stop!=b.stop) {return b.stop-a.stop;} |
| 243 |
|
| 244 |
return b.depth-a.depth; |
| 245 |
} |
| 246 |
|
| 247 |
public static final PosComparator2 comparator=new PosComparator2(); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public final int k; |
| 253 |
public final int maxGap; |
| 254 |
public final int minRepeat; |
| 255 |
public final char type; |
| 256 |
|
| 257 |
public long contigNum=-1; |
| 258 |
public int start=-1; |
| 259 |
public int stop=-1; |
| 260 |
|
| 261 |
public int gapCount=0; |
| 262 |
public int gapLen=0; |
| 263 |
public int gapBP=0; |
| 264 |
public long depthSum=0; |
| 265 |
public int minDepth=Integer.MAX_VALUE; |
| 266 |
public int maxDepth=-1; |
| 267 |
|
| 268 |
public Read contig; |
| 269 |
public String contigName; |
| 270 |
public byte[] seq; |
| 271 |
public float gc; |
| 272 |
public float entropy; |
| 273 |
int tipdist; |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
public final int depth; |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public static int SEQ_AFFIX_LEN=12; |
| 286 |
|
| 287 |
} |