| 1 |
package assemble; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Arrays; |
| 7 |
import java.util.HashSet; |
| 8 |
|
| 9 |
import jgi.AssemblyStats2; |
| 10 |
import jgi.ReformatReads; |
| 11 |
import shared.Parse; |
| 12 |
import shared.PreParser; |
| 13 |
import shared.Shared; |
| 14 |
import shared.Tools; |
| 15 |
import ukmer.Kmer; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class TadpoleWrapper { |
| 24 |
|
| 25 |
|
| 26 |
public static void main(String[] args){ |
| 27 |
process(args); |
| 28 |
|
| 29 |
|
| 30 |
Shared.closeStream(outstream); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
public static int process(String[] args){ |
| 35 |
|
| 36 |
{ |
| 37 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), true); |
| 38 |
args=pp.args; |
| 39 |
outstream=pp.outstream; |
| 40 |
} |
| 41 |
|
| 42 |
HashSet<Integer> set=new HashSet<Integer>(); |
| 43 |
ArrayList<String> argList=new ArrayList<String>(); |
| 44 |
String contigsName="contigs%.fa"; |
| 45 |
String outFinal=null; |
| 46 |
bestAssembly=null; |
| 47 |
for(int i=0; i<args.length; i++){ |
| 48 |
final String arg=args[i]; |
| 49 |
String[] split=arg.split("="); |
| 50 |
String a=split[0].toLowerCase(); |
| 51 |
String b=split.length>1 ? split[1] : null; |
| 52 |
|
| 53 |
if(a.equals("k")){ |
| 54 |
assert(b!=null) : "Bad parameter: "+arg; |
| 55 |
for(String s2 : b.split(",")){ |
| 56 |
int x=Integer.parseInt(s2); |
| 57 |
x=Kmer.getKbig(x); |
| 58 |
set.add(x); |
| 59 |
} |
| 60 |
}else if(a.equals("out")){ |
| 61 |
assert(b!=null) : "Bad parameter: "+arg; |
| 62 |
contigsName=b; |
| 63 |
assert(b.contains("%")) : "Output name must contain % symbol."; |
| 64 |
}else if(a.equals("outfinal")){ |
| 65 |
outFinal=b; |
| 66 |
}else if(a.equals("quitearly")){ |
| 67 |
quitEarly=Parse.parseBoolean(b); |
| 68 |
}else if(a.equals("delete")){ |
| 69 |
delete=Parse.parseBoolean(b); |
| 70 |
}else if(a.equals("bisect")){ |
| 71 |
bisect=Parse.parseBoolean(b); |
| 72 |
}else if(a.equals("expand") ){ |
| 73 |
expand=Parse.parseBoolean(b); |
| 74 |
}else{ |
| 75 |
argList.add(arg); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if(outFinal==null && contigsName!=null && contigsName.indexOf('%')<0){ |
| 80 |
outFinal=contigsName; |
| 81 |
contigsName="contigs_k%.fa"; |
| 82 |
} |
| 83 |
|
| 84 |
if(set.isEmpty()){ |
| 85 |
kmers=new int[] {31}; |
| 86 |
} |
| 87 |
|
| 88 |
{ |
| 89 |
kmers=new int[set.size()]; |
| 90 |
int i=0; |
| 91 |
for(Integer x : set){ |
| 92 |
kmers[i]=x; |
| 93 |
i++; |
| 94 |
} |
| 95 |
Arrays.sort(kmers); |
| 96 |
} |
| 97 |
|
| 98 |
ArrayList<Record> records=new ArrayList<Record>(); |
| 99 |
|
| 100 |
argList.add(""); |
| 101 |
argList.add(""); |
| 102 |
|
| 103 |
int best=0; |
| 104 |
Record bestRecord=null; |
| 105 |
|
| 106 |
for(int i=0; i<kmers.length; i++){ |
| 107 |
int k=kmers[i]; |
| 108 |
argList.set(argList.size()-2, "k="+k); |
| 109 |
argList.set(argList.size()-1, "out="+contigsName.replace("%", ""+k)); |
| 110 |
String[] args2=argList.toArray(new String[0]); |
| 111 |
System.gc(); |
| 112 |
Tadpole.main(args2); |
| 113 |
|
| 114 |
Record r=new Record(k, AssemblyStats2.lastL50, AssemblyStats2.lastL90, |
| 115 |
AssemblyStats2.lastSize, AssemblyStats2.lastContigs, AssemblyStats2.lastMaxContig); |
| 116 |
records.add(r); |
| 117 |
|
| 118 |
if(bestRecord==null){ |
| 119 |
bestRecord=r; |
| 120 |
best=i; |
| 121 |
}else{ |
| 122 |
int x=bestRecord.compareTo(r); |
| 123 |
if(x<0){ |
| 124 |
bestRecord=r; |
| 125 |
best=i; |
| 126 |
}else if(x>0 && quitEarly){ |
| 127 |
System.err.println("Metrics stopped improving; quit early."); |
| 128 |
break; |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
if(expand || bisect){ |
| 134 |
best=bisect(records, best, argList, contigsName, true); |
| 135 |
bestRecord=records.get(best); |
| 136 |
} |
| 137 |
|
| 138 |
assert(bestRecord!=null); |
| 139 |
int bestK=bestRecord.k; |
| 140 |
bestAssembly=(contigsName.replace("%", ""+bestK)); |
| 141 |
System.err.println("Recommended K:\t"+bestK); |
| 142 |
|
| 143 |
if(outFinal!=null){ |
| 144 |
File f=new File(bestAssembly); |
| 145 |
boolean success=(Shared.WINDOWS ? false : f.renameTo(new File(outFinal))); |
| 146 |
if(!success && f.exists()){ |
| 147 |
try { |
| 148 |
ReformatReads.main(new String[] {"in="+bestAssembly, "out="+outFinal, "ow"}); |
| 149 |
} catch (Exception e) { |
| 150 |
|
| 151 |
e.printStackTrace(); |
| 152 |
} |
| 153 |
if(f.exists()){f.delete();} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
if(delete){ |
| 165 |
for(Record r : records){ |
| 166 |
if(r.k!=bestK){ |
| 167 |
String s=contigsName.replace("%", ""+r.k); |
| 168 |
File f=new File(s); |
| 169 |
if(f.exists()){ |
| 170 |
f.delete(); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
return bestK; |
| 177 |
} |
| 178 |
|
| 179 |
static int expandLeft(ArrayList<Record> list, int best, ArrayList<String> argList, String contigsName, boolean recur){ |
| 180 |
assert(best==0); |
| 181 |
Record mid=list.get(best); |
| 182 |
int k1=Kmer.getKbig((int)(mid.k*0.7f)); |
| 183 |
outstream.println("ExpandLeft: best="+mid.k+", next="+k1); |
| 184 |
if(k1>=mid.k || k1<1){return best;} |
| 185 |
|
| 186 |
{ |
| 187 |
argList.set(argList.size()-2, "k="+k1); |
| 188 |
argList.set(argList.size()-1, "out="+contigsName.replace("%", ""+k1)); |
| 189 |
String[] args2=argList.toArray(new String[0]); |
| 190 |
System.gc(); |
| 191 |
Tadpole.main(args2); |
| 192 |
|
| 193 |
Record r=new Record(k1, AssemblyStats2.lastL50, AssemblyStats2.lastL90, |
| 194 |
AssemblyStats2.lastSize, AssemblyStats2.lastContigs, AssemblyStats2.lastMaxContig); |
| 195 |
list.add(best, r); |
| 196 |
best++; |
| 197 |
|
| 198 |
if(r.compareTo(mid)>0){ |
| 199 |
return expandLeft(list, best-1, argList, contigsName, true); |
| 200 |
} |
| 201 |
} |
| 202 |
return best; |
| 203 |
} |
| 204 |
|
| 205 |
static int expandRight(ArrayList<Record> list, int best, ArrayList<String> argList, String contigsName, boolean recur){ |
| 206 |
assert(best==list.size()-1); |
| 207 |
Record mid=list.get(best); |
| 208 |
int k1=Kmer.getKbig(Tools.min(mid.k+40, (int)(10+mid.k*1.25f))); |
| 209 |
outstream.println("ExpandRight: best="+mid.k+", next="+k1); |
| 210 |
if(k1<=mid.k){return best;} |
| 211 |
|
| 212 |
{ |
| 213 |
argList.set(argList.size()-2, "k="+k1); |
| 214 |
argList.set(argList.size()-1, "out="+contigsName.replace("%", ""+k1)); |
| 215 |
String[] args2=argList.toArray(new String[0]); |
| 216 |
System.gc(); |
| 217 |
Tadpole.main(args2); |
| 218 |
|
| 219 |
Record r=new Record(k1, AssemblyStats2.lastL50, AssemblyStats2.lastL90, |
| 220 |
AssemblyStats2.lastSize, AssemblyStats2.lastContigs, AssemblyStats2.lastMaxContig); |
| 221 |
list.add(r); |
| 222 |
|
| 223 |
if(r.compareTo(mid)>0){ |
| 224 |
return expandRight(list, best+1, argList, contigsName, true); |
| 225 |
} |
| 226 |
} |
| 227 |
return best; |
| 228 |
} |
| 229 |
|
| 230 |
static int bisect(ArrayList<Record> list, int best, ArrayList<String> argList, String contigsName, boolean recur){ |
| 231 |
|
| 232 |
if(expand){ |
| 233 |
if(best==0){best=expandLeft(list, best, argList, contigsName, recur);} |
| 234 |
if(best==list.size()-1){best=expandRight(list, best, argList, contigsName, recur);} |
| 235 |
} |
| 236 |
|
| 237 |
if(!bisect || best==0 || best==list.size()-1){return best;} |
| 238 |
|
| 239 |
Record left=list.get(best-1); |
| 240 |
Record mid=list.get(best); |
| 241 |
Record right=list.get(best+1); |
| 242 |
|
| 243 |
int k1=Kmer.getKbig((left.k+mid.k+1)/2); |
| 244 |
int k2=Kmer.getKbig((mid.k+right.k+1)/2); |
| 245 |
|
| 246 |
outstream.println("Bisect: left="+left.k+", k1="+k1+", best="+mid.k+", k2="+k2+", right="+right.k); |
| 247 |
|
| 248 |
if(k1==left.k || k1==mid.k){return best;} |
| 249 |
if(k2==mid.k || k2==right.k){return best;} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
{ |
| 254 |
argList.set(argList.size()-2, "k="+k1); |
| 255 |
argList.set(argList.size()-1, "out="+contigsName.replace("%", ""+k1)); |
| 256 |
String[] args2=argList.toArray(new String[0]); |
| 257 |
System.gc(); |
| 258 |
Tadpole.main(args2); |
| 259 |
|
| 260 |
Record r=new Record(k1, AssemblyStats2.lastL50, AssemblyStats2.lastL90, |
| 261 |
AssemblyStats2.lastSize, AssemblyStats2.lastContigs, AssemblyStats2.lastMaxContig); |
| 262 |
list.add(best, r); |
| 263 |
best++; |
| 264 |
|
| 265 |
if(r.compareTo(mid)>0){ |
| 266 |
return bisect(list, best-1, argList, contigsName, true); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
{ |
| 271 |
argList.set(argList.size()-2, "k="+k2); |
| 272 |
argList.set(argList.size()-1, "out="+contigsName.replace("%", ""+k2)); |
| 273 |
String[] args2=argList.toArray(new String[0]); |
| 274 |
System.gc(); |
| 275 |
Tadpole.main(args2); |
| 276 |
|
| 277 |
Record r=new Record(k2, AssemblyStats2.lastL50, AssemblyStats2.lastL90, |
| 278 |
AssemblyStats2.lastSize, AssemblyStats2.lastContigs, AssemblyStats2.lastMaxContig); |
| 279 |
list.add(best+1, r); |
| 280 |
|
| 281 |
if(r.compareTo(mid)>0){ |
| 282 |
return bisect(list, best+1, argList, contigsName, true); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
if(recur){ |
| 287 |
best=bisect(list, best, argList, contigsName, false); |
| 288 |
} |
| 289 |
|
| 290 |
return best; |
| 291 |
} |
| 292 |
|
| 293 |
private static class Record implements Comparable<Record>{ |
| 294 |
|
| 295 |
public Record(){} |
| 296 |
|
| 297 |
public Record(int k_, long L50_, long L90_, long contigLen_, long contigs_, long maxContig_){ |
| 298 |
k=k_; |
| 299 |
L50=L50_; |
| 300 |
L90=L90_; |
| 301 |
contigLen=contigLen_; |
| 302 |
contigs=contigs_; |
| 303 |
maxContig=maxContig_; |
| 304 |
} |
| 305 |
|
| 306 |
@Override |
| 307 |
public int compareTo(Record b){ |
| 308 |
if(b==null){return 1;} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
if(L50<b.L50*0.99){return -1;} |
| 315 |
if(L50>b.L50*1.01){return 1;} |
| 316 |
|
| 317 |
if(L90<b.L90*0.99){return -1;} |
| 318 |
if(L90>b.L90*1.01){return 1;} |
| 319 |
|
| 320 |
if(maxContig<b.maxContig*0.99){return -1;} |
| 321 |
if(maxContig>b.maxContig*1.01){return 1;} |
| 322 |
|
| 323 |
if(contigs>b.contigs){return -1;} |
| 324 |
|
| 325 |
if(L50<b.L50*0.998){return -1;} |
| 326 |
if(L50>b.L50*1.002){return 1;} |
| 327 |
|
| 328 |
if(L90<b.L90*0.998){return -1;} |
| 329 |
if(L90>b.L90*1.002){return 1;} |
| 330 |
|
| 331 |
if(maxContig<b.maxContig*0.998){return -1;} |
| 332 |
if(maxContig>b.maxContig*1.002){return 1;} |
| 333 |
|
| 334 |
return b.k-k; |
| 335 |
} |
| 336 |
|
| 337 |
int k; |
| 338 |
long L50; |
| 339 |
long L90; |
| 340 |
long contigLen; |
| 341 |
long contigs; |
| 342 |
long maxContig; |
| 343 |
|
| 344 |
} |
| 345 |
|
| 346 |
private static int[] kmers; |
| 347 |
private static boolean quitEarly=false; |
| 348 |
private static boolean delete=false; |
| 349 |
private static boolean expand=false; |
| 350 |
private static boolean bisect=false; |
| 351 |
private static String bestAssembly=null; |
| 352 |
|
| 353 |
|
| 354 |
private static PrintStream outstream=System.err; |
| 355 |
|
| 356 |
} |