| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
|
| 5 |
import align2.BandedAligner; |
| 6 |
import fileIO.FileFormat; |
| 7 |
import fileIO.ReadWrite; |
| 8 |
import fileIO.TextStreamWriter; |
| 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.ConcurrentCollectionReadInputStream; |
| 16 |
import stream.ConcurrentReadInputStream; |
| 17 |
import stream.FASTQ; |
| 18 |
import stream.Read; |
| 19 |
import structures.ListNum; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class IdentityMatrix { |
| 28 |
|
| 29 |
public static void main(String[] args){ |
| 30 |
Timer t=new Timer(); |
| 31 |
IdentityMatrix x=new IdentityMatrix(args); |
| 32 |
x.process(t); |
| 33 |
|
| 34 |
|
| 35 |
Shared.closeStream(x.outstream); |
| 36 |
} |
| 37 |
|
| 38 |
public IdentityMatrix(String[] args){ |
| 39 |
|
| 40 |
{ |
| 41 |
PreParser pp=new PreParser(args, getClass(), false); |
| 42 |
args=pp.args; |
| 43 |
outstream=pp.outstream; |
| 44 |
} |
| 45 |
|
| 46 |
FileFormat.PRINT_WARNING=false; |
| 47 |
int maxEdits_=-1; |
| 48 |
int maxWidth_=-1; |
| 49 |
|
| 50 |
Parser parser=new Parser(); |
| 51 |
for(int i=0; i<args.length; i++){ |
| 52 |
String arg=args[i]; |
| 53 |
String[] split=arg.split("="); |
| 54 |
String a=split[0].toLowerCase(); |
| 55 |
String b=split.length>1 ? split[1] : null; |
| 56 |
|
| 57 |
if(parser.parse(arg, a, b)){ |
| 58 |
|
| 59 |
}else if(a.equals("parse_flag_goes_here")){ |
| 60 |
|
| 61 |
}else if(a.equals("edits") || a.equals("maxedits")){ |
| 62 |
maxEdits_=Integer.parseInt(b); |
| 63 |
}else if(a.equals("width") || a.equals("maxwidth")){ |
| 64 |
maxWidth_=Integer.parseInt(b); |
| 65 |
}else if(a.equals("percent")){ |
| 66 |
percent=Parse.parseBoolean(b); |
| 67 |
}else{ |
| 68 |
outstream.println("Unknown parameter "+args[i]); |
| 69 |
assert(false) : "Unknown parameter "+args[i]; |
| 70 |
|
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
{ |
| 75 |
Parser.processQuality(); |
| 76 |
|
| 77 |
maxReads=parser.maxReads; |
| 78 |
in1=parser.in1; |
| 79 |
out1=parser.out1; |
| 80 |
} |
| 81 |
FASTQ.FORCE_INTERLEAVED=false; |
| 82 |
FASTQ.TEST_INTERLEAVED=false; |
| 83 |
|
| 84 |
maxEdits=maxEdits_==-1 ? BandedAligner.big : maxEdits_; |
| 85 |
maxWidth=maxWidth_==-1 ? (int)(Tools.min(maxEdits, BandedAligner.big)*2L+1) : maxWidth_; |
| 86 |
|
| 87 |
ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, true, false, false); |
| 88 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, null, true, true); |
| 89 |
} |
| 90 |
|
| 91 |
void process(Timer t){ |
| 92 |
|
| 93 |
allReads=load(); |
| 94 |
Shared.setBufferLen(4); |
| 95 |
ConcurrentCollectionReadInputStream cris=new ConcurrentCollectionReadInputStream(allReads, null, -1); |
| 96 |
cris.start(); |
| 97 |
|
| 98 |
|
| 99 |
ArrayList<ProcessThread> threads=new ArrayList<ProcessThread>(); |
| 100 |
final int tmax=Tools.max(Shared.threads(), 1); |
| 101 |
for(int i=0; i<tmax; i++){ |
| 102 |
threads.add(new ProcessThread(cris)); |
| 103 |
} |
| 104 |
for(ProcessThread pt : threads){pt.start();} |
| 105 |
for(ProcessThread pt : threads){ |
| 106 |
while(pt.getState()!=Thread.State.TERMINATED){ |
| 107 |
try { |
| 108 |
pt.join(); |
| 109 |
} catch (InterruptedException e) { |
| 110 |
|
| 111 |
e.printStackTrace(); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
ReadWrite.closeStreams(cris); |
| 116 |
|
| 117 |
final int numReads=allReads.size(); |
| 118 |
for(int i=1; i<numReads; i++){ |
| 119 |
Read r1=allReads.get(i); |
| 120 |
assert(r1.numericID==i); |
| 121 |
for(int j=0; j<i; j++){ |
| 122 |
Read r2=allReads.get(j); |
| 123 |
assert(r2.numericID==j); |
| 124 |
((float[])r2.obj)[i]=((float[])r1.obj)[j]; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
if(ffout1!=null){ |
| 129 |
TextStreamWriter tsw=new TextStreamWriter(ffout1); |
| 130 |
tsw.start(); |
| 131 |
for(Read r : allReads){ |
| 132 |
float[] obj=(float[])r.obj; |
| 133 |
tsw.print(r.id); |
| 134 |
if(percent){ |
| 135 |
for(float f : obj){ |
| 136 |
tsw.print(Tools.format("\t%.2f", f)); |
| 137 |
} |
| 138 |
}else{ |
| 139 |
for(float f : obj){ |
| 140 |
tsw.print(Tools.format("\t%.4f", f)); |
| 141 |
} |
| 142 |
} |
| 143 |
tsw.print("\n"); |
| 144 |
r.obj=null; |
| 145 |
} |
| 146 |
tsw.poisonAndWait(); |
| 147 |
} |
| 148 |
|
| 149 |
t.stop(); |
| 150 |
outstream.println("Total Time: \t"+t); |
| 151 |
outstream.println("Reads Processed: "+allReads.size()+" \t"+Tools.format("%.2fk alignments/sec", (allReads.size()*(long)(allReads.size())/(double)(t.elapsed))*1000000)); |
| 152 |
outstream.println("Min Similarity: "+Tools.format("%.5f", minID)); |
| 153 |
outstream.println("Max Similarity: "+Tools.format("%.5f", maxID)); |
| 154 |
outstream.println("Avg Similarity: "+Tools.format("%.5f", avgID)); |
| 155 |
} |
| 156 |
|
| 157 |
private ArrayList<Read> load(){ |
| 158 |
Timer t=new Timer(); |
| 159 |
final ConcurrentReadInputStream cris; |
| 160 |
{ |
| 161 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, null); |
| 162 |
if(verbose){outstream.println("Started cris");} |
| 163 |
cris.start(); |
| 164 |
} |
| 165 |
boolean paired=cris.paired(); |
| 166 |
assert(!paired) : "This program is not designed for paired reads."; |
| 167 |
|
| 168 |
long readsProcessed=0; |
| 169 |
int maxLen=0; |
| 170 |
ArrayList<Read> bigList=new ArrayList<Read>(); |
| 171 |
{ |
| 172 |
|
| 173 |
ListNum<Read> ln=cris.nextList(); |
| 174 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 175 |
|
| 176 |
if(reads!=null && !reads.isEmpty()){ |
| 177 |
Read r=reads.get(0); |
| 178 |
assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 179 |
} |
| 180 |
|
| 181 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 182 |
if(verbose){outstream.println("Fetched "+reads.size()+" reads.");} |
| 183 |
|
| 184 |
for(int idx=0; idx<reads.size(); idx++){ |
| 185 |
final Read r1=reads.get(idx); |
| 186 |
|
| 187 |
bigList.add(r1); |
| 188 |
maxLen=Tools.max(maxLen, r1.length()); |
| 189 |
|
| 190 |
readsProcessed++; |
| 191 |
} |
| 192 |
|
| 193 |
cris.returnList(ln); |
| 194 |
if(verbose){outstream.println("Returned a list.");} |
| 195 |
ln=cris.nextList(); |
| 196 |
reads=(ln!=null ? ln.list : null); |
| 197 |
} |
| 198 |
if(ln!=null){ |
| 199 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 200 |
} |
| 201 |
} |
| 202 |
ReadWrite.closeStreams(cris); |
| 203 |
if(verbose){outstream.println("Finished loading "+readsProcessed+" sequences.");} |
| 204 |
|
| 205 |
longestSequence=maxLen; |
| 206 |
|
| 207 |
t.stop(); |
| 208 |
outstream.println("Load Time: \t"+t); |
| 209 |
|
| 210 |
return bigList; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
private class ProcessThread extends Thread { |
| 216 |
|
| 217 |
ProcessThread(ConcurrentReadInputStream cris_){ |
| 218 |
cris=cris_; |
| 219 |
maxEdits2=Tools.min(maxEdits, longestSequence); |
| 220 |
int width=Tools.min(maxEdits2*2+1, maxWidth); |
| 221 |
bandy=BandedAligner.makeBandedAligner(width); |
| 222 |
} |
| 223 |
|
| 224 |
@Override |
| 225 |
public void run(){ |
| 226 |
final int numReads=allReads.size(); |
| 227 |
ListNum<Read> ln=cris.nextList(); |
| 228 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 229 |
|
| 230 |
if(reads!=null && !reads.isEmpty()){ |
| 231 |
Read r=reads.get(0); |
| 232 |
assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 233 |
} |
| 234 |
|
| 235 |
double sum=0; |
| 236 |
long compares=0; |
| 237 |
|
| 238 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 239 |
if(verbose){outstream.println("Fetched "+reads.size()+" reads.");} |
| 240 |
|
| 241 |
for(int idx=0; idx<reads.size(); idx++){ |
| 242 |
final Read r1=reads.get(idx); |
| 243 |
float[] obj=new float[numReads]; |
| 244 |
r1.obj=obj; |
| 245 |
for(Read r2 : allReads){ |
| 246 |
if(r2.numericID>r1.numericID){break;} |
| 247 |
|
| 248 |
int edits=bandy.alignQuadrupleProgressive(r1.bases, r2.bases, 10, maxEdits2, false); |
| 249 |
System.err.println(r1.id+"->"+r2.id+": Edits="+edits); |
| 250 |
float editRate=edits/(float)Tools.max(r1.length(), r2.length()); |
| 251 |
float similarity=1-editRate; |
| 252 |
if(r1!=r2){ |
| 253 |
compares++; |
| 254 |
sum+=similarity; |
| 255 |
minID=Tools.min(minID, similarity); |
| 256 |
maxID=Tools.max(maxID, similarity); |
| 257 |
} |
| 258 |
if(percent){ |
| 259 |
float id=100*similarity; |
| 260 |
obj[(int)r2.numericID]=id; |
| 261 |
}else{ |
| 262 |
obj[(int)r2.numericID]=similarity; |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
cris.returnList(ln); |
| 268 |
if(verbose){outstream.println("Returned a list.");} |
| 269 |
ln=cris.nextList(); |
| 270 |
reads=(ln!=null ? ln.list : null); |
| 271 |
} |
| 272 |
if(ln!=null){ |
| 273 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 274 |
} |
| 275 |
|
| 276 |
avgID=sum/compares; |
| 277 |
} |
| 278 |
|
| 279 |
private final ConcurrentReadInputStream cris; |
| 280 |
private final BandedAligner bandy; |
| 281 |
private final int maxEdits2; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
private String in1=null; |
| 289 |
private String out1=null; |
| 290 |
|
| 291 |
private final FileFormat ffin1; |
| 292 |
private final FileFormat ffout1; |
| 293 |
private boolean percent=false; |
| 294 |
|
| 295 |
private ArrayList<Read> allReads; |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
private long maxReads=-1; |
| 300 |
private final int maxEdits; |
| 301 |
private final int maxWidth; |
| 302 |
private int longestSequence; |
| 303 |
|
| 304 |
private double minID=1, maxID=0, avgID=0; |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
private java.io.PrintStream outstream=System.err; |
| 309 |
public static boolean verbose=false; |
| 310 |
|
| 311 |
} |