| 1 |
package barcode; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.LinkedHashSet; |
| 6 |
|
| 7 |
import fileIO.FileFormat; |
| 8 |
import fileIO.ReadWrite; |
| 9 |
import fileIO.TextStreamWriter; |
| 10 |
import shared.LineParserS4; |
| 11 |
import shared.Parse; |
| 12 |
import shared.Parser; |
| 13 |
import shared.PreParser; |
| 14 |
import shared.Shared; |
| 15 |
import shared.Timer; |
| 16 |
import shared.Tools; |
| 17 |
import shared.TrimRead; |
| 18 |
import stream.ConcurrentReadInputStream; |
| 19 |
import stream.ConcurrentReadOutputStream; |
| 20 |
import stream.Read; |
| 21 |
import structures.ByteBuilder; |
| 22 |
import structures.ListNum; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class TagAndMerge { |
| 35 |
|
| 36 |
public static void main(String[] args){ |
| 37 |
|
| 38 |
Timer t=new Timer(); |
| 39 |
|
| 40 |
|
| 41 |
TagAndMerge x=new TagAndMerge(args); |
| 42 |
|
| 43 |
|
| 44 |
x.process(t); |
| 45 |
|
| 46 |
|
| 47 |
Shared.closeStream(x.outstream); |
| 48 |
} |
| 49 |
|
| 50 |
public TagAndMerge(String[] args){ |
| 51 |
|
| 52 |
{ |
| 53 |
PreParser pp=new PreParser(args, getClass(), false); |
| 54 |
args=pp.args; |
| 55 |
outstream=pp.outstream; |
| 56 |
} |
| 57 |
|
| 58 |
Parser parser=new Parser(); |
| 59 |
for(int i=0; i<args.length; i++){ |
| 60 |
String arg=args[i]; |
| 61 |
String[] split=arg.split("="); |
| 62 |
String a=split[0].toLowerCase(); |
| 63 |
String b=split.length>1 ? split[1] : null; |
| 64 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 65 |
|
| 66 |
if(a.equals("parse_flag_goes_here")){ |
| 67 |
|
| 68 |
}else if(a.equals("trim")){ |
| 69 |
trimLen=(b==null ? 1 : Integer.parseInt(b)); |
| 70 |
}else if(a.equals("r1only") || a.equals("read1only") || a.equals("dropr2")){ |
| 71 |
dropR2=Parse.parseBoolean(b); |
| 72 |
}else if(a.equalsIgnoreCase("shrinkHeader") || a.equals("shrink")){ |
| 73 |
shrinkHeader=Parse.parseBoolean(b); |
| 74 |
}else if(a.equals("barcodes") || a.equals("barcodesout")){ |
| 75 |
barcodesOutFile=b; |
| 76 |
}else if(a.equals("remap")){ |
| 77 |
remap=(b==null ? null : b.toCharArray()); |
| 78 |
}else if(a.equals("in")){ |
| 79 |
in.clear(); |
| 80 |
String[] b2=(b==null) ? null : (new File(b).exists() ? new String[] {b} : b.split(",")); |
| 81 |
for(String b3 : b2){in.add(b3);} |
| 82 |
}else if(b==null && new File(arg).exists()){ |
| 83 |
in.add(arg); |
| 84 |
}else if(parser.parse(arg, a, b)){ |
| 85 |
|
| 86 |
}else{ |
| 87 |
assert(false) : "Unknown parameter "+args[i]; |
| 88 |
outstream.println("Unknown parameter "+args[i]); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
{ |
| 93 |
Parser.processQuality(); |
| 94 |
|
| 95 |
maxReads=parser.maxReads; |
| 96 |
out1=parser.out1; |
| 97 |
} |
| 98 |
|
| 99 |
ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, null, true, overwrite, append, false); |
| 100 |
} |
| 101 |
|
| 102 |
void process(Timer t){ |
| 103 |
|
| 104 |
final ConcurrentReadOutputStream ros; |
| 105 |
if(out1!=null){ |
| 106 |
final int buff=4; |
| 107 |
|
| 108 |
for(String s : in) { |
| 109 |
assert(!out1.equalsIgnoreCase(s)) : "Input file and output file have same name."; |
| 110 |
} |
| 111 |
|
| 112 |
ros=ConcurrentReadOutputStream.getStream(ffout1, null, buff, null, false); |
| 113 |
ros.start(); |
| 114 |
}else{ros=null;} |
| 115 |
|
| 116 |
for(String s : in) { |
| 117 |
processInner(s, ros); |
| 118 |
} |
| 119 |
|
| 120 |
ReadWrite.closeStream(ros); |
| 121 |
outstream.println("Finished processing "+in.size()+" files."); |
| 122 |
|
| 123 |
if(barcodesOutFile!=null) { |
| 124 |
TextStreamWriter tsw=new TextStreamWriter(barcodesOutFile, overwrite, append, false); |
| 125 |
tsw.start(); |
| 126 |
for(String tag : barcodes) {tsw.println(tag);} |
| 127 |
tsw.poisonAndWait(); |
| 128 |
} |
| 129 |
|
| 130 |
t.stop(); |
| 131 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 132 |
outstream.println(Tools.readsBasesOut(readsProcessed, basesProcessed, readsOut, basesOut, 8, false)); |
| 133 |
} |
| 134 |
|
| 135 |
void processInner(String fname, ConcurrentReadOutputStream ros) { |
| 136 |
FileFormat ffin=FileFormat.testInput(fname, FileFormat.FASTQ, null, true, true); |
| 137 |
String tag=Barcode.parseBarcodeFromFname(fname); |
| 138 |
assert(tag!=null) : "Can't find barcode in filename "+fname; |
| 139 |
if(remap!=null) { |
| 140 |
for(int i=0; i<remap.length; i+=2) { |
| 141 |
tag=tag.replace(remap[i], remap[i+1]); |
| 142 |
} |
| 143 |
} |
| 144 |
if(tag!=null && Barcode.isBarcode(tag)) { |
| 145 |
barcodes.add(tag); |
| 146 |
} |
| 147 |
|
| 148 |
final ConcurrentReadInputStream cris; |
| 149 |
{ |
| 150 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin, null); |
| 151 |
cris.start(); |
| 152 |
} |
| 153 |
boolean paired=cris.paired(); |
| 154 |
|
| 155 |
{ |
| 156 |
ListNum<Read> ln=cris.nextList(); |
| 157 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 158 |
|
| 159 |
if(reads!=null && !reads.isEmpty()){ |
| 160 |
Read r=reads.get(0); |
| 161 |
assert((ffin==null || ffin.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 166 |
if(verbose){outstream.println("Fetched "+reads.size()+" reads.");} |
| 167 |
|
| 168 |
for(int idx=0; idx<reads.size(); idx++){ |
| 169 |
final Read r1=reads.get(idx); |
| 170 |
processPair(r1, tag); |
| 171 |
} |
| 172 |
|
| 173 |
if(ros!=null){ros.add(reads, ln.id);} |
| 174 |
|
| 175 |
cris.returnList(ln); |
| 176 |
if(verbose){outstream.println("Returned a list.");} |
| 177 |
ln=cris.nextList(); |
| 178 |
reads=(ln!=null ? ln.list : null); |
| 179 |
} |
| 180 |
if(ln!=null){ |
| 181 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 182 |
} |
| 183 |
} |
| 184 |
ReadWrite.closeStream(cris); |
| 185 |
} |
| 186 |
|
| 187 |
private void processPair(Read r, String tag) { |
| 188 |
readsProcessed+=r.pairCount(); |
| 189 |
basesProcessed+=r.pairLength(); |
| 190 |
if(dropR2) {r.mate=null;} |
| 191 |
processRead(r, tag); |
| 192 |
processRead(r.mate, tag); |
| 193 |
readsOut+=r.pairCount(); |
| 194 |
basesOut+=r.pairLength(); |
| 195 |
} |
| 196 |
|
| 197 |
private void processRead(Read r, String tag) { |
| 198 |
if(r==null) {return;} |
| 199 |
if(trimLen>=0 && trimLen<r.length()) { |
| 200 |
final int x=TrimRead.trimToPosition(r, 0, trimLen-1, 1); |
| 201 |
assert(x>0) : x+", "+trimLen+", "+r.length(); |
| 202 |
} |
| 203 |
bb.clear(); |
| 204 |
if(!shrinkHeader) { |
| 205 |
bb.append(r.id).tab().append(tag); |
| 206 |
}else { |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
lp.set(r.id); |
| 211 |
bb.colon().colon().colon().appendTerm(lp, 3).colon().appendTerm(lp, 4).colon(); |
| 212 |
bb.space().appendTerm(lp, 7); |
| 213 |
|
| 214 |
bb.tab().append(tag); |
| 215 |
} |
| 216 |
r.id=bb.toString(); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
private ArrayList<String> in=new ArrayList<String>(); |
| 224 |
private String out1=null; |
| 225 |
private String barcodesOutFile=null; |
| 226 |
private LinkedHashSet<String> barcodes=new LinkedHashSet<String>(); |
| 227 |
|
| 228 |
private int trimLen=-1; |
| 229 |
private boolean dropR2=false; |
| 230 |
private boolean shrinkHeader=false; |
| 231 |
private char[] remap=new char[] {'-', '+'}; |
| 232 |
|
| 233 |
private final FileFormat ffout1; |
| 234 |
private boolean overwrite=true; |
| 235 |
private boolean append=false; |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
private long maxReads=-1; |
| 240 |
private long readsProcessed=0, basesProcessed=0; |
| 241 |
private long readsOut=0, basesOut=0; |
| 242 |
private final ByteBuilder bb=new ByteBuilder(); |
| 243 |
|
| 244 |
private final LineParserS4 lp=new LineParserS4(":::::: "); |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
private java.io.PrintStream outstream=System.err; |
| 249 |
public static boolean verbose=false; |
| 250 |
|
| 251 |
} |