| 1 |
package barcode; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.Collection; |
| 6 |
import java.util.Collections; |
| 7 |
import java.util.HashMap; |
| 8 |
import java.util.Map.Entry; |
| 9 |
|
| 10 |
import fileIO.ByteFile; |
| 11 |
import fileIO.ByteStreamWriter; |
| 12 |
import fileIO.FileFormat; |
| 13 |
import fileIO.ReadWrite; |
| 14 |
import fileIO.TextFile; |
| 15 |
import hiseq.IlluminaHeaderParser2; |
| 16 |
import shared.LineParser2; |
| 17 |
import shared.Tools; |
| 18 |
import stream.ConcurrentReadInputStream; |
| 19 |
import stream.Read; |
| 20 |
import structures.ByteBuilder; |
| 21 |
import structures.ListNum; |
| 22 |
import structures.SuperLongList; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class BarcodeCounter { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public static final HashMap<String, Barcode> countBarcodes(String readsFile, long maxReads, boolean addTile) { |
| 40 |
FileFormat ff=FileFormat.testInput(readsFile, FileFormat.FASTQ, null, true, true); |
| 41 |
final byte delimiter=(byte)ff.barcodeDelimiter(); |
| 42 |
final int barcodesPerRead=ff.barcodesPerRead(); |
| 43 |
return countBarcodes(ff, maxReads, addTile); |
| 44 |
} |
| 45 |
|
| 46 |
public static final HashMap<String, Barcode> countBarcodes(FileFormat ff, long maxReads, boolean addTile) { |
| 47 |
final ConcurrentReadInputStream cris=makeCris(ff, maxReads); |
| 48 |
HashMap<String, Barcode> map=new HashMap<String, Barcode>(); |
| 49 |
|
| 50 |
|
| 51 |
ListNum<Read> ln=cris.nextList(); |
| 52 |
|
| 53 |
|
| 54 |
if(ln!=null && !ln.isEmpty()){ |
| 55 |
Read r=ln.get(0); |
| 56 |
assert(r.samline!=null || (r.mate!=null)==cris.paired()); |
| 57 |
} |
| 58 |
|
| 59 |
IlluminaHeaderParser2 ihp=new IlluminaHeaderParser2(); |
| 60 |
|
| 61 |
|
| 62 |
while(ln!=null && ln.size()>0){ |
| 63 |
processList(ln, cris, map, ihp, addTile); |
| 64 |
|
| 65 |
|
| 66 |
ln=cris.nextList(); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
if(ln!=null){ |
| 71 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 72 |
} |
| 73 |
|
| 74 |
errorState|=ReadWrite.closeStream(cris); |
| 75 |
|
| 76 |
return map; |
| 77 |
} |
| 78 |
|
| 79 |
private static final ConcurrentReadInputStream makeCris(FileFormat ff1, long maxReads){ |
| 80 |
ConcurrentReadInputStream cris=ConcurrentReadInputStream.getReadInputStream( |
| 81 |
maxReads, true, ff1, null); |
| 82 |
cris.start(); |
| 83 |
return cris; |
| 84 |
} |
| 85 |
|
| 86 |
private static final void processList(ListNum<Read> ln, final ConcurrentReadInputStream cris, |
| 87 |
HashMap<String, Barcode> codeMap, IlluminaHeaderParser2 ihp, boolean addTile){ |
| 88 |
|
| 89 |
|
| 90 |
final ArrayList<Read> reads=ln.list; |
| 91 |
|
| 92 |
|
| 93 |
for(int idx=0; idx<reads.size(); idx++){ |
| 94 |
final Read r1=reads.get(idx); |
| 95 |
if(!r1.validated()){r1.validate(true);} |
| 96 |
processRead(r1, codeMap, ihp, addTile); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
cris.returnList(ln); |
| 101 |
} |
| 102 |
|
| 103 |
private static final void processRead(final Read r1, HashMap<String, Barcode> codeMap, IlluminaHeaderParser2 ihp, boolean addTile){ |
| 104 |
ihp.parse(r1.id); |
| 105 |
final String key=ihp.barcode(); |
| 106 |
if(key==null){return;} |
| 107 |
String key2=key; |
| 108 |
int tile=0; |
| 109 |
if(addTile) { |
| 110 |
tile=ihp.tile(); |
| 111 |
assert(tile>10 && tile<10000); |
| 112 |
key2+=tile; |
| 113 |
} |
| 114 |
Barcode b=codeMap.get(key2); |
| 115 |
if(b==null){ |
| 116 |
b=new Barcode(key); |
| 117 |
b.tile=tile; |
| 118 |
codeMap.put(key2, b); |
| 119 |
} |
| 120 |
b.increment(1); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public static final ArrayList<String> loadBarcodes(String fname, int forceDelimiter){ |
| 126 |
if(fname==null){return null;} |
| 127 |
String[] codes; |
| 128 |
if(new File(fname).exists()) { |
| 129 |
codes=TextFile.toStringLines(fname); |
| 130 |
}else { |
| 131 |
codes=fname.split(","); |
| 132 |
} |
| 133 |
ArrayList<String> expected=new ArrayList<String>(); |
| 134 |
for(int i=0; i<codes.length; i++){ |
| 135 |
String s=codes[i]; |
| 136 |
if(!Tools.startsWith(s, '#')){ |
| 137 |
|
| 138 |
|
| 139 |
s=BarcodeStats.fixBarcode(s, forceDelimiter, false, false); |
| 140 |
expected.add(s); |
| 141 |
} |
| 142 |
} |
| 143 |
return expected; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public static final ArrayList<Barcode> loadCounts(String fname, long minCount){ |
| 149 |
LineParser2 lp=new LineParser2('\t'); |
| 150 |
ByteFile bf=ByteFile.makeByteFile(fname, true); |
| 151 |
ArrayList<Barcode> list=new ArrayList<Barcode>(); |
| 152 |
for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine()) { |
| 153 |
if(line[0]=='#') {continue;} |
| 154 |
lp.set(line); |
| 155 |
String a=lp.parseString(); |
| 156 |
assert(lp.hasMore()) : new String(line); |
| 157 |
long b=lp.parseLong(); |
| 158 |
if(b>=minCount) { |
| 159 |
Barcode c=new Barcode(a, b); |
| 160 |
list.add(c); |
| 161 |
} |
| 162 |
} |
| 163 |
errorState=bf.close()|errorState; |
| 164 |
return list; |
| 165 |
} |
| 166 |
|
| 167 |
public static final boolean writeCounts(Collection<Barcode> counts, long minCount, FileFormat ff, |
| 168 |
boolean sort, boolean overwrite, boolean append){ |
| 169 |
return writeCounts(counts, minCount, ff, sort, ff.overwrite(), ff.append()); |
| 170 |
} |
| 171 |
|
| 172 |
public static final boolean writeCounts(Collection<Barcode> counts, long minCount, String fname, |
| 173 |
boolean sort, boolean overwrite, boolean append){ |
| 174 |
if(sort) { |
| 175 |
ArrayList<Barcode> list=new ArrayList<Barcode>(counts); |
| 176 |
Collections.sort(list); |
| 177 |
counts=list; |
| 178 |
} |
| 179 |
|
| 180 |
ByteStreamWriter bsw=new ByteStreamWriter(fname, overwrite, append, true); |
| 181 |
bsw.start(); |
| 182 |
|
| 183 |
long sum=0; |
| 184 |
for(Barcode bc : counts) {sum+=bc.count();} |
| 185 |
bsw.println("#Barcodes\t"+sum); |
| 186 |
bsw.println("#Unique\t"+counts.size()); |
| 187 |
ByteBuilder bb=new ByteBuilder(128); |
| 188 |
for(Barcode b : counts) { |
| 189 |
if(b.count()>=minCount) { |
| 190 |
b.appendTo(bb).nl(); |
| 191 |
bsw.print(bb); |
| 192 |
bb.clear(); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
} |
| 198 |
boolean b=bsw.poisonAndWait(); |
| 199 |
errorState|=b; |
| 200 |
return b; |
| 201 |
} |
| 202 |
|
| 203 |
public static final boolean writeCounts(HashMap<String, Barcode> counts, String fname, |
| 204 |
boolean overwrite, boolean append){ |
| 205 |
ByteStreamWriter bsw=new ByteStreamWriter(fname, overwrite, append, true); |
| 206 |
bsw.start(); |
| 207 |
for(Entry<String, Barcode> e : counts.entrySet()) { |
| 208 |
Barcode b=e.getValue(); |
| 209 |
bsw.print(b.name).tab().print(b.count()).nl(); |
| 210 |
} |
| 211 |
boolean b=bsw.poisonAndWait(); |
| 212 |
errorState|=b; |
| 213 |
return b; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public static HashMap<String, String> loadAssignmentMap(String mapIn) { |
| 219 |
LineParser2 lp=new LineParser2('\t'); |
| 220 |
ByteFile bf=ByteFile.makeByteFile(mapIn, true); |
| 221 |
HashMap<String, String> map=new HashMap<String, String>(); |
| 222 |
for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine()) { |
| 223 |
if(line[0]=='#') {continue;} |
| 224 |
lp.set(line); |
| 225 |
String a=lp.parseString(); |
| 226 |
assert(lp.hasMore()) : new String(line); |
| 227 |
String b=lp.parseString(); |
| 228 |
map.put(a, b); |
| 229 |
} |
| 230 |
errorState=bf.close()|errorState; |
| 231 |
return map; |
| 232 |
} |
| 233 |
|
| 234 |
public static boolean writeAssignmentMap(HashMap<String, String> assignmentMap, |
| 235 |
String mapOut, boolean overwrite, boolean append) { |
| 236 |
|
| 237 |
ByteStreamWriter bsw=new ByteStreamWriter(mapOut, overwrite, append, true); |
| 238 |
bsw.start(); |
| 239 |
for(Entry<String, String> e : assignmentMap.entrySet()) { |
| 240 |
String a=e.getKey(), b=e.getValue(); |
| 241 |
bsw.print(a).tab().print(b); |
| 242 |
bsw.nl(); |
| 243 |
} |
| 244 |
boolean b=bsw.poisonAndWait(); |
| 245 |
errorState|=b; |
| 246 |
return b; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
public static SuperLongList makeCountList(Collection<Barcode> barcodes) { |
| 254 |
SuperLongList sll=new SuperLongList(100000); |
| 255 |
for(Barcode b : barcodes) { |
| 256 |
sll.increment(b.count()); |
| 257 |
} |
| 258 |
sll.sort(); |
| 259 |
return sll; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
public static long barcodeCountPercentile(Collection<Barcode> barcodes, float percentile) { |
| 270 |
|
| 271 |
SuperLongList sll=makeCountList(barcodes); |
| 272 |
long x=sll.percentileValueBySum(percentile); |
| 273 |
assert(false) : x; |
| 274 |
return x; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
public static boolean errorState=false; |
| 282 |
|
| 283 |
} |