| 1 |
package ml; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
|
| 5 |
import dna.AminoAcid; |
| 6 |
import fileIO.ByteStreamWriter; |
| 7 |
import fileIO.FileFormat; |
| 8 |
import fileIO.ReadWrite; |
| 9 |
import shared.Parse; |
| 10 |
import shared.Parser; |
| 11 |
import shared.PreParser; |
| 12 |
import shared.Shared; |
| 13 |
import shared.Timer; |
| 14 |
import shared.Tools; |
| 15 |
import stream.ConcurrentReadInputStream; |
| 16 |
import stream.Read; |
| 17 |
import structures.ByteBuilder; |
| 18 |
import structures.ListNum; |
| 19 |
import structures.LongList; |
| 20 |
import tracker.PalindromeTracker; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class ScoreSequence { |
| 28 |
|
| 29 |
public static void main(String[] args){ |
| 30 |
|
| 31 |
Timer t=new Timer(); |
| 32 |
|
| 33 |
|
| 34 |
ScoreSequence x=new ScoreSequence(args); |
| 35 |
|
| 36 |
|
| 37 |
x.process(t); |
| 38 |
|
| 39 |
|
| 40 |
Shared.closeStream(x.outstream); |
| 41 |
} |
| 42 |
|
| 43 |
public ScoreSequence(String[] args){ |
| 44 |
|
| 45 |
{ |
| 46 |
PreParser pp=new PreParser(args, getClass(), false); |
| 47 |
args=pp.args; |
| 48 |
outstream=pp.outstream; |
| 49 |
} |
| 50 |
|
| 51 |
Parser parser=new Parser(); |
| 52 |
for(int i=0; i<args.length; i++){ |
| 53 |
String arg=args[i]; |
| 54 |
String[] split=arg.split("="); |
| 55 |
String a=split[0].toLowerCase(); |
| 56 |
String b=split.length>1 ? split[1] : null; |
| 57 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 58 |
|
| 59 |
if(a.equals("parse_flag_goes_here")){ |
| 60 |
|
| 61 |
}else if(a.equals("net") || a.equals("nn")){ |
| 62 |
netFile=b; |
| 63 |
}else if(a.equals("hist")){ |
| 64 |
histFile=b; |
| 65 |
}else if(a.equals("width")){ |
| 66 |
width=Integer.parseInt(b); |
| 67 |
}else if(a.equals("k")){ |
| 68 |
k=Integer.parseInt(b); |
| 69 |
}else if(a.equals("rcomp")){ |
| 70 |
rcomp=Parse.parseBoolean(b); |
| 71 |
}else if(a.equals("parse")){ |
| 72 |
parseHeader=Parse.parseBoolean(b); |
| 73 |
}else if(a.equals("cutoff")){ |
| 74 |
cutoff=Float.parseFloat(b); |
| 75 |
filter=true; |
| 76 |
}else if(a.equals("highpass")){ |
| 77 |
highpass=Parse.parseBoolean(b); |
| 78 |
filter=true; |
| 79 |
}else if(a.equals("lowpass")){ |
| 80 |
highpass=!Parse.parseBoolean(b); |
| 81 |
filter=true; |
| 82 |
}else if(a.equals("filter")){ |
| 83 |
filter=Parse.parseBoolean(b); |
| 84 |
}else if(a.equals("annotate") || a.equals("rename")){ |
| 85 |
annotate=Parse.parseBoolean(b); |
| 86 |
}else if(parser.parse(arg, a, b)){ |
| 87 |
|
| 88 |
}else{ |
| 89 |
|
| 90 |
assert(false) : "Unknown parameter "+args[i]; |
| 91 |
outstream.println("Unknown parameter "+args[i]); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
{ |
| 96 |
Parser.processQuality(); |
| 97 |
|
| 98 |
maxReads=parser.maxReads; |
| 99 |
in1=parser.in1; |
| 100 |
out1=parser.out1; |
| 101 |
} |
| 102 |
|
| 103 |
ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, true, false, false); |
| 104 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, null, true, true); |
| 105 |
ffnet=FileFormat.testOutput(netFile, FileFormat.BBNET, null, true, true, false, false); |
| 106 |
net=CellNetParser.load(netFile); |
| 107 |
assert(net!=null) : netFile; |
| 108 |
if(width<0) {width=(net.numInputs()-4)/4;} |
| 109 |
else {assert(width==(net.numInputs()-4)/4) : width+", "+net.numInputs();} |
| 110 |
} |
| 111 |
|
| 112 |
void process(Timer t){ |
| 113 |
|
| 114 |
final ConcurrentReadInputStream cris; |
| 115 |
{ |
| 116 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, null); |
| 117 |
cris.start(); |
| 118 |
} |
| 119 |
|
| 120 |
int inputs=width*4+4; |
| 121 |
final ByteStreamWriter bsw=(ffout1==null ? null : new ByteStreamWriter(ffout1)); |
| 122 |
if(bsw!=null) { |
| 123 |
bsw.start(); |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
final float[] vec=new float[width*4+4]; |
| 128 |
long readsProcessed=0, basesProcessed=0; |
| 129 |
final ByteBuilder bb=new ByteBuilder(); |
| 130 |
{ |
| 131 |
|
| 132 |
ListNum<Read> ln=cris.nextList(); |
| 133 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 134 |
|
| 135 |
if(reads!=null && !reads.isEmpty()){ |
| 136 |
Read r=reads.get(0); |
| 137 |
assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 138 |
} |
| 139 |
|
| 140 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 141 |
if(verbose){outstream.println("Fetched "+reads.size()+" reads.");} |
| 142 |
|
| 143 |
for(int idx=0; idx<reads.size(); idx++){ |
| 144 |
final Read r1=reads.get(idx); |
| 145 |
final Read r2=r1.mate; |
| 146 |
readsProcessed+=r1.pairCount(); |
| 147 |
basesProcessed+=r1.pairLength(); |
| 148 |
|
| 149 |
|
| 150 |
processRead(r1, bb, bsw, vec); |
| 151 |
processRead(r2, bb, bsw, vec); |
| 152 |
} |
| 153 |
|
| 154 |
cris.returnList(ln); |
| 155 |
if(verbose){outstream.println("Returned a list.");} |
| 156 |
ln=cris.nextList(); |
| 157 |
reads=(ln!=null ? ln.list : null); |
| 158 |
} |
| 159 |
if(ln!=null){ |
| 160 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 161 |
} |
| 162 |
} |
| 163 |
errorState=ReadWrite.closeStreams(cris) | errorState; |
| 164 |
if(verbose){outstream.println("Finished reading data.");} |
| 165 |
|
| 166 |
if(bsw!=null) {errorState=bsw.poisonAndWait() | errorState;} |
| 167 |
|
| 168 |
if(histFile!=null) { |
| 169 |
bb.clear(); |
| 170 |
PalindromeTracker.append(bb, "#count\tneg\tpos", new LongList[] {mhist, phist}, 101); |
| 171 |
ReadWrite.writeStringInThread(bb, histFile, false); |
| 172 |
} |
| 173 |
|
| 174 |
t.stop(); |
| 175 |
outstream.println("Time: \t"+t); |
| 176 |
outstream.println("Reads Processed: "+readsProcessed+" \t"+Tools.format("%.2fk reads/sec", (readsProcessed/(double)(t.elapsed))*1000000)); |
| 177 |
outstream.println("Reads Out: "+readsOut); |
| 178 |
assert(!errorState) : "An error was encountered."; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
private boolean processRead(Read r, ByteBuilder bb, ByteStreamWriter bsw, float[] vec) { |
| 184 |
if(r==null) {return false;} |
| 185 |
float result=(parseHeader ? Parse.parseFloat(r.id, "result=", '\t') : 0); |
| 186 |
float score=score(r.bases, vec, k, net, rcomp); |
| 187 |
int iscore=Tools.mid(0, Math.round(score*100), 100); |
| 188 |
boolean pass=(!filter ? true : (score>=cutoff)==highpass); |
| 189 |
readsOut+=(pass ? 1 : 0); |
| 190 |
if(result<0.5f) {mhist.increment(iscore);} |
| 191 |
else {phist.increment(iscore);} |
| 192 |
if(bsw!=null && pass) { |
| 193 |
if(annotate) {r.id+=String.format("\tscore=%.4f", score);} |
| 194 |
bsw.print(r.toFasta(bb.clear()).nl()); |
| 195 |
} |
| 196 |
return pass; |
| 197 |
} |
| 198 |
|
| 199 |
public static float score(byte[] bases, float[] vec, int k, CellNet net, boolean rcomp) { |
| 200 |
SequenceToVector.fillVector(bases, vec, k); |
| 201 |
net.applyInput(vec); |
| 202 |
final float r; |
| 203 |
final float f=net.feedForward(); |
| 204 |
if(rcomp) { |
| 205 |
AminoAcid.reverseComplementBasesInPlace(bases); |
| 206 |
SequenceToVector.fillVector(bases, vec, k); |
| 207 |
net.applyInput(vec); |
| 208 |
r=net.feedForward(); |
| 209 |
AminoAcid.reverseComplementBasesInPlace(bases); |
| 210 |
}else {r=f;} |
| 211 |
return Tools.max(r, f); |
| 212 |
} |
| 213 |
|
| 214 |
public static float score(byte[] bases, float[] vec, int k, CellNet net) { |
| 215 |
assert(vec!=null); |
| 216 |
SequenceToVector.fillVector(bases, vec, k); |
| 217 |
net.applyInput(vec); |
| 218 |
final float f=net.feedForward(); |
| 219 |
return f; |
| 220 |
} |
| 221 |
|
| 222 |
public static float score(byte[] bases, float[] vec, int k, CellNet net, int from, int to) { |
| 223 |
SequenceToVector.fillVector(bases, vec, k, from, to); |
| 224 |
net.applyInput(vec); |
| 225 |
final float f=net.feedForward(); |
| 226 |
return f; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
private String in1=null; |
| 232 |
private String out1=null; |
| 233 |
private String netFile=null; |
| 234 |
private String histFile=null; |
| 235 |
|
| 236 |
private final FileFormat ffin1; |
| 237 |
private final FileFormat ffout1; |
| 238 |
private final FileFormat ffnet; |
| 239 |
|
| 240 |
private LongList phist=new LongList(101); |
| 241 |
private LongList mhist=new LongList(101); |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
private long readsOut=0; |
| 246 |
private long maxReads=-1; |
| 247 |
private boolean errorState=false; |
| 248 |
|
| 249 |
private int k=0; |
| 250 |
private boolean rcomp=false; |
| 251 |
private boolean parseHeader=false; |
| 252 |
private int width=-1; |
| 253 |
private final CellNet net; |
| 254 |
|
| 255 |
private boolean filter=false; |
| 256 |
private boolean highpass=true; |
| 257 |
private boolean annotate=true; |
| 258 |
private float cutoff=0.5f; |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
private java.io.PrintStream outstream=System.err; |
| 263 |
public static boolean verbose=false; |
| 264 |
|
| 265 |
} |