| 1 |
package tracker; |
| 2 |
|
| 3 |
import dna.AminoAcid; |
| 4 |
import fileIO.ReadWrite; |
| 5 |
import shared.Tools; |
| 6 |
import stream.Read; |
| 7 |
import structures.LongList; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
public class AdapterTracker { |
| 13 |
|
| 14 |
public AdapterTracker() { |
| 15 |
for(int i=0; i<counts.length; i++){ |
| 16 |
for(int j=0; j<counts[i].length; j++){ |
| 17 |
counts[i][j]=new LongList(151); |
| 18 |
} |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
public void storeAdapterSequence(Read r, int insert){ |
| 23 |
reads++; |
| 24 |
if(r.length()<=insert) {return;} |
| 25 |
shortInserts++; |
| 26 |
|
| 27 |
if(looksLikePhix(r, insert)){ |
| 28 |
phixLike++; |
| 29 |
if(ignorePhixAdapters){return;} |
| 30 |
} |
| 31 |
|
| 32 |
LongList[] lists=counts[r.pairnum()]; |
| 33 |
byte[] bases=r.bases; |
| 34 |
|
| 35 |
for(int i=insert, j=0; i<bases.length; i++, j++){ |
| 36 |
byte b=bases[i]; |
| 37 |
int num=AminoAcid.baseToNumber[b]; |
| 38 |
if(num>=0){ |
| 39 |
lists[num].increment(j); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
private boolean looksLikePhix(Read r, int insert){ |
| 45 |
return looksLikePhix(r.bases, insert) || looksLikePhix(r.mate.bases, insert); |
| 46 |
} |
| 47 |
|
| 48 |
private boolean looksLikePhix(byte[] bases, int insert){ |
| 49 |
int len=bases.length-insert; |
| 50 |
if(len<phixPrefix.length){return false;} |
| 51 |
for(int i=insert, j=0; i<bases.length && j<phixPrefix.length; i++, j++){ |
| 52 |
byte b=bases[i]; |
| 53 |
if(b!='N' && b!=phixPrefix[j]){ |
| 54 |
return false; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
public boolean makeSequence() { |
| 63 |
seq1=seq2=null; |
| 64 |
seq1=toAdapterSequence(counts[0], trimPolyAorG); |
| 65 |
seq2=toAdapterSequence(counts[1], trimPolyAorG); |
| 66 |
return hasSequence(); |
| 67 |
} |
| 68 |
|
| 69 |
public boolean hasSequence() { |
| 70 |
return (seq1!=null && seq1.length()>1) || (seq2!=null && seq2.length()>1); |
| 71 |
} |
| 72 |
|
| 73 |
public long writeAdapterConsensus(String fname){ |
| 74 |
StringBuilder sb=new StringBuilder(); |
| 75 |
{ |
| 76 |
sb.append(">Read1_adapter\n"); |
| 77 |
String adapter=toAdapterSequence(counts[0], trimPolyAorG); |
| 78 |
sb.append(adapter).append('\n'); |
| 79 |
} |
| 80 |
if(counts.length>1){ |
| 81 |
sb.append(">Read2_adapter\n"); |
| 82 |
String adapter=toAdapterSequence(counts[1], trimPolyAorG); |
| 83 |
sb.append(adapter).append('\n'); |
| 84 |
} |
| 85 |
long count=counts[0][0].get(0)+counts[0][1].get(0)+ |
| 86 |
counts[0][2].get(0)+counts[0][3].get(0); |
| 87 |
|
| 88 |
ReadWrite.writeString(sb, fname); |
| 89 |
return count; |
| 90 |
} |
| 91 |
|
| 92 |
private static String toAdapterSequence(LongList[] lists, boolean trimPolyAorG){ |
| 93 |
StringBuilder adapter=new StringBuilder(); |
| 94 |
long max=0; |
| 95 |
int lastBase=-1; |
| 96 |
for(int i=0; true; i++){ |
| 97 |
long a=lists[0].get(i); |
| 98 |
long c=lists[1].get(i); |
| 99 |
long g=lists[2].get(i); |
| 100 |
long t=lists[3].get(i); |
| 101 |
long sum=(a+c+g+t); |
| 102 |
max=Tools.max(max, sum); |
| 103 |
if(sum==0 || (sum<10 && sum<=max/1000) || (max>100 && sum<8)){break;} |
| 104 |
long thresh=(max>100 ? 4+(sum*2)/3 : (sum*2)/3); |
| 105 |
if(a>thresh){ |
| 106 |
adapter.append('A'); |
| 107 |
lastBase=i; |
| 108 |
}else if(c>thresh){ |
| 109 |
adapter.append('C'); |
| 110 |
lastBase=i; |
| 111 |
}else if(g>thresh){ |
| 112 |
adapter.append('G'); |
| 113 |
lastBase=i; |
| 114 |
}else if(t>thresh){ |
| 115 |
adapter.append('T'); |
| 116 |
lastBase=i; |
| 117 |
}else{ |
| 118 |
adapter.append('N'); |
| 119 |
} |
| 120 |
} |
| 121 |
if(lastBase<0){return "N";} |
| 122 |
|
| 123 |
String trimmed=trimPoly2(adapter.toString(), 'N'); |
| 124 |
if(trimPolyAorG){ |
| 125 |
for(int len=-1; len!=trimmed.length(); ) { |
| 126 |
len=trimmed.length(); |
| 127 |
trimmed=trimPoly2(trimmed, 'G'); |
| 128 |
trimmed=trimPoly2(trimmed, 'A'); |
| 129 |
} |
| 130 |
} |
| 131 |
if(trimJunk){ |
| 132 |
trimmed=trimJunk(trimmed, 6); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
if(trimmed.length()<1){return "N";} |
| 141 |
return trimmed; |
| 142 |
} |
| 143 |
|
| 144 |
private static String trimPoly(String adapter, char trim){ |
| 145 |
int lastBase=-1; |
| 146 |
for(int i=0; i<adapter.length(); i++){ |
| 147 |
char c=adapter.charAt(i); |
| 148 |
if(AminoAcid.isFullyDefined(c)){ |
| 149 |
lastBase=i; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
int aCount=0; |
| 154 |
int nCount=0; |
| 155 |
int count=0; |
| 156 |
while(lastBase>=0){ |
| 157 |
char c=adapter.charAt(lastBase); |
| 158 |
if(c=='N'){nCount++;} |
| 159 |
else if(c==trim){aCount++;} |
| 160 |
else{break;} |
| 161 |
count++; |
| 162 |
lastBase--; |
| 163 |
} |
| 164 |
|
| 165 |
if(lastBase<0){return "N";} |
| 166 |
if(count==nCount || (aCount>3)){ |
| 167 |
return adapter.substring(0, lastBase+1); |
| 168 |
} |
| 169 |
return adapter; |
| 170 |
} |
| 171 |
|
| 172 |
private static String trimPoly2(String adapter, char poly){ |
| 173 |
int last=adapter.length()-1; |
| 174 |
int trim=0; |
| 175 |
while(last>=0) { |
| 176 |
char c=adapter.charAt(last); |
| 177 |
|
| 178 |
if(c==poly || c=='N') { |
| 179 |
trim++; |
| 180 |
last--; |
| 181 |
}else{ |
| 182 |
break; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if(trim>3 || (trim>0 && poly=='N')) { |
| 187 |
adapter=adapter.substring(0, last+1); |
| 188 |
} |
| 189 |
|
| 190 |
return adapter==null || adapter.length()<1 ? "N" : adapter; |
| 191 |
} |
| 192 |
|
| 193 |
private static String trimJunk(String s, int minScore) { |
| 194 |
int score=0, last=s.length()-1; |
| 195 |
for(; last>=0 && score<minScore; last--) { |
| 196 |
char c=s.charAt(last); |
| 197 |
if(c=='N') { |
| 198 |
score--; |
| 199 |
}else { |
| 200 |
score+=2; |
| 201 |
} |
| 202 |
} |
| 203 |
last++; |
| 204 |
while(last<s.length() && s.charAt(last)!='N' || (last<s.length()-1 && s.charAt(last+1)!='N')) {last++;} |
| 205 |
return (last<1 ? "N" : last>s.length() ? s : s.substring(0, last)); |
| 206 |
} |
| 207 |
|
| 208 |
public void merge(AdapterTracker b){ |
| 209 |
for(int x=0; x<counts.length; x++){ |
| 210 |
for(int y=0; y<counts[x].length; y++){ |
| 211 |
counts[x][y].incrementBy(b.counts[x][y]); |
| 212 |
} |
| 213 |
} |
| 214 |
reads+=b.reads; |
| 215 |
shortInserts+=b.shortInserts; |
| 216 |
phixLike+=b.phixLike; |
| 217 |
} |
| 218 |
|
| 219 |
final LongList[][] counts=new LongList[2][4]; |
| 220 |
public String seq1=null; |
| 221 |
public String seq2=null; |
| 222 |
public long reads=0; |
| 223 |
public long shortInserts=0; |
| 224 |
public long phixLike=0; |
| 225 |
|
| 226 |
private static final byte[] phixPrefix="AGATCGGAAGAGCG".getBytes(); |
| 227 |
public static boolean ignorePhixAdapters=false; |
| 228 |
public static boolean trimPolyAorG=true; |
| 229 |
public static boolean trimJunk=true; |
| 230 |
|
| 231 |
} |