| 1 |
package prok; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
|
| 7 |
import dna.Data; |
| 8 |
import fileIO.ByteStreamWriter; |
| 9 |
import fileIO.FileFormat; |
| 10 |
import fileIO.ReadWrite; |
| 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 structures.ByteBuilder; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class PGMTools extends ProkObject { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public static void main(String[] args){ |
| 34 |
|
| 35 |
|
| 36 |
Timer t=new Timer(); |
| 37 |
|
| 38 |
{ |
| 39 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); |
| 40 |
args=pp.args; |
| 41 |
outstream=pp.outstream; |
| 42 |
} |
| 43 |
|
| 44 |
ProkObject.call18S=true; |
| 45 |
boolean overwrite=true; |
| 46 |
boolean allowDupes=false; |
| 47 |
String out=null; |
| 48 |
ArrayList<String> in=new ArrayList<String>(); |
| 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("in")){ |
| 60 |
assert(b!=null); |
| 61 |
Tools.addFiles(b, in); |
| 62 |
}else if(parseStatic(arg, a, b)){ |
| 63 |
|
| 64 |
}else if(a.equals("allowdupes") || a.equals("allowduplicates") || a.equals("dupes")){ |
| 65 |
allowDupes=Parse.parseBoolean(b); |
| 66 |
}else if(a.equals("addcdsonly")){ |
| 67 |
GeneModel.ADD_CDS_ONLY=Parse.parseBoolean(b); |
| 68 |
}else if(a.equals("verbose")){ |
| 69 |
verbose=Parse.parseBoolean(b); |
| 70 |
ReadWrite.verbose=verbose; |
| 71 |
} |
| 72 |
|
| 73 |
else if(parser.parse(arg, a, b)){ |
| 74 |
|
| 75 |
}else if(b==null && new File(arg.split("@")[0]).exists()){ |
| 76 |
in.add(arg); |
| 77 |
}else{ |
| 78 |
outstream.println("Unknown parameter "+args[i]); |
| 79 |
assert(false) : "Unknown parameter "+args[i]; |
| 80 |
|
| 81 |
} |
| 82 |
} |
| 83 |
overwrite=parser.overwrite; |
| 84 |
out=parser.out1; |
| 85 |
} |
| 86 |
|
| 87 |
checkFileExistence(in, out, overwrite, allowDupes); |
| 88 |
|
| 89 |
ArrayList<GeneModel> models=loadModels(in); |
| 90 |
GeneModel gm=mergeModels(models); |
| 91 |
boolean errorState=writeModel(gm, out, overwrite); |
| 92 |
|
| 93 |
|
| 94 |
Shared.closeStream(outstream); |
| 95 |
} |
| 96 |
|
| 97 |
public static boolean parseStatic(String arg, String a, String b){ |
| 98 |
if(a.equals("kinnercds")){ |
| 99 |
int k=Integer.parseInt(b); |
| 100 |
GeneModel.setInnerK(k); |
| 101 |
}else if(a.equals("kstartcds")){ |
| 102 |
int k=Integer.parseInt(b); |
| 103 |
GeneModel.setStartK(k); |
| 104 |
}else if(a.equals("kstopcds")){ |
| 105 |
int k=Integer.parseInt(b); |
| 106 |
GeneModel.setStopK(k); |
| 107 |
}else if(a.equals("kinnerrna")){ |
| 108 |
int k=Integer.parseInt(b); |
| 109 |
kInnerRNA=k; |
| 110 |
}else if(a.equals("kstartrna")){ |
| 111 |
int k=Integer.parseInt(b); |
| 112 |
kStartRNA=k; |
| 113 |
}else if(a.equals("kstoprna")){ |
| 114 |
int k=Integer.parseInt(b); |
| 115 |
kStopRNA=k; |
| 116 |
}else if(a.equals("startleftoffset")){ |
| 117 |
int x=Integer.parseInt(b); |
| 118 |
GeneModel.setStartLeftOffset(x); |
| 119 |
}else if(a.equals("startrightoffset")){ |
| 120 |
int x=Integer.parseInt(b); |
| 121 |
GeneModel.setStartRightOffset(x); |
| 122 |
}else if(a.equals("stopleftoffset")){ |
| 123 |
int x=Integer.parseInt(b); |
| 124 |
GeneModel.setStopLeftOffset(x); |
| 125 |
}else if(a.equals("stoprightoffset")){ |
| 126 |
int x=Integer.parseInt(b); |
| 127 |
GeneModel.setStopRightOffset(x); |
| 128 |
}else if(a.equalsIgnoreCase("callcdsonly") || a.equalsIgnoreCase("cdsonly")){ |
| 129 |
callCDS=Parse.parseBoolean(b); |
| 130 |
calltRNA=call16S=call23S=call5S=call18S=!callCDS; |
| 131 |
}else if(a.equalsIgnoreCase("call16sonly") || a.equalsIgnoreCase("16sonly")){ |
| 132 |
call16S=Parse.parseBoolean(b); |
| 133 |
calltRNA=call23S=call5S=call18S=callCDS=!call16S; |
| 134 |
}else if(a.equalsIgnoreCase("call23sonly") || a.equalsIgnoreCase("23sonly")){ |
| 135 |
call23S=Parse.parseBoolean(b); |
| 136 |
calltRNA=call16S=call5S=call18S=callCDS=!call23S; |
| 137 |
}else if(a.equalsIgnoreCase("call5sonly") || a.equalsIgnoreCase("5sonly")){ |
| 138 |
call5S=Parse.parseBoolean(b); |
| 139 |
calltRNA=call16S=call23S=call18S=callCDS=!call5S; |
| 140 |
}else if(a.equalsIgnoreCase("calltrnaonly") || a.equalsIgnoreCase("trnaonly")){ |
| 141 |
calltRNA=Parse.parseBoolean(b); |
| 142 |
call16S=call23S=call5S=call18S=callCDS=!calltRNA; |
| 143 |
}else if(a.equalsIgnoreCase("call18sonly") || a.equalsIgnoreCase("18sonly")){ |
| 144 |
call18S=Parse.parseBoolean(b); |
| 145 |
calltRNA=call16S=call23S=call5S=callCDS=!call18S; |
| 146 |
} |
| 147 |
|
| 148 |
else if(a.equalsIgnoreCase("callcds") || a.equalsIgnoreCase("cds")){ |
| 149 |
callCDS=Parse.parseBoolean(b); |
| 150 |
}else if(a.equalsIgnoreCase("calltrna") || a.equalsIgnoreCase("trna")){ |
| 151 |
calltRNA=Parse.parseBoolean(b); |
| 152 |
}else if(a.equalsIgnoreCase("call16s") || a.equalsIgnoreCase("16s")){ |
| 153 |
call16S=Parse.parseBoolean(b); |
| 154 |
}else if(a.equalsIgnoreCase("call18s") || a.equalsIgnoreCase("18s")){ |
| 155 |
call18S=Parse.parseBoolean(b); |
| 156 |
}else if(a.equalsIgnoreCase("call23s") || a.equalsIgnoreCase("23s")){ |
| 157 |
call23S=Parse.parseBoolean(b); |
| 158 |
}else if(a.equalsIgnoreCase("call5s") || a.equalsIgnoreCase("5s")){ |
| 159 |
call5S=Parse.parseBoolean(b); |
| 160 |
}else if(a.equalsIgnoreCase("callrna") || a.equalsIgnoreCase("rna")){ |
| 161 |
calltRNA=call16S=call18S=call5S=call23S=Parse.parseBoolean(b); |
| 162 |
} |
| 163 |
|
| 164 |
else if(a.equalsIgnoreCase("normalize")){ |
| 165 |
normalize=Parse.parseBoolean(b); |
| 166 |
} |
| 167 |
|
| 168 |
else{ |
| 169 |
return false; |
| 170 |
} |
| 171 |
return true; |
| 172 |
} |
| 173 |
|
| 174 |
public static ArrayList<GeneModel> loadModels(ArrayList<String> fnames){ |
| 175 |
ArrayList<GeneModel> models=new ArrayList<GeneModel>(fnames.size()); |
| 176 |
ArrayList<Double> mults=new ArrayList<Double>(fnames.size()); |
| 177 |
for(String s : fnames){ |
| 178 |
double mult=1; |
| 179 |
String fname=s; |
| 180 |
if(s.indexOf('@')>=0){ |
| 181 |
String[] split=s.split("@"); |
| 182 |
fname=split[0]; |
| 183 |
mult=Double.parseDouble(split[1]); |
| 184 |
} |
| 185 |
GeneModel pgm=GeneModelParser.loadModel(fname); |
| 186 |
mults.add(mult); |
| 187 |
models.add(pgm); |
| 188 |
} |
| 189 |
if(normalize){ |
| 190 |
long max=0; |
| 191 |
for(GeneModel gm : models){ |
| 192 |
max=Tools.max(gm.basesProcessed, max); |
| 193 |
} |
| 194 |
for(GeneModel gm : models){ |
| 195 |
if(max!=gm.basesProcessed){ |
| 196 |
double mult=max/(double)(Tools.max(100, gm.basesProcessed)); |
| 197 |
if(mult!=1) { |
| 198 |
gm.multiplyBy(mult); |
| 199 |
gm.stats5S.multiplyBy(0.01); |
| 200 |
|
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
for(int i=0; i<models.size(); i++){ |
| 206 |
double mult=mults.get(i); |
| 207 |
GeneModel gm=models.get(i); |
| 208 |
if(mult!=1){ |
| 209 |
gm.multiplyBy(mult); |
| 210 |
|
| 211 |
} |
| 212 |
} |
| 213 |
return models; |
| 214 |
} |
| 215 |
|
| 216 |
public static GeneModel mergeModels(ArrayList<GeneModel> models){ |
| 217 |
if(models.size()==1){return models.get(0);} |
| 218 |
GeneModel pgmSum=new GeneModel(true); |
| 219 |
for(GeneModel pgm : models){ |
| 220 |
pgmSum.add(pgm); |
| 221 |
} |
| 222 |
return pgmSum; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
public static GeneModel mix(double cdsMult, double rRnaMult, double tRnaMult, boolean normalize, GeneModel...models) { |
| 227 |
assert(models.length>1); |
| 228 |
GeneModel pgm0=models[0]; |
| 229 |
GeneModel pgm=new GeneModel(true); |
| 230 |
pgm.add(pgm0); |
| 231 |
long max=Tools.max(100, pgm0.basesProcessed); |
| 232 |
for(int i=1; i<models.length; i++) { |
| 233 |
GeneModel pgmI=models[i]; |
| 234 |
GeneModel temp=new GeneModel(true); |
| 235 |
temp.add(pgmI); |
| 236 |
|
| 237 |
assert(temp.basesProcessed<max) : temp.basesProcessed+", "+max+"\nThis function is specifically for mixing a small model in with a large model."; |
| 238 |
double mult=max/(double)(Tools.max(100, temp.basesProcessed)); |
| 239 |
temp.statsCDS.multiplyBy(mult*cdsMult); |
| 240 |
temp.stats5S.multiplyBy(mult*rRnaMult); |
| 241 |
temp.stats16S.multiplyBy(mult*rRnaMult); |
| 242 |
temp.stats18S.multiplyBy(mult*rRnaMult); |
| 243 |
temp.stats23S.multiplyBy(mult*rRnaMult); |
| 244 |
temp.statstRNA.multiplyBy(mult*tRnaMult); |
| 245 |
pgm.add(temp); |
| 246 |
} |
| 247 |
return pgm; |
| 248 |
} |
| 249 |
|
| 250 |
public static GeneModel loadAndMerge(ArrayList<String> in) { |
| 251 |
ArrayList<GeneModel> models=loadModels(in); |
| 252 |
return mergeModels(models); |
| 253 |
} |
| 254 |
|
| 255 |
public static boolean writeModel(GeneModel pgm, String out, boolean overwrite){ |
| 256 |
FileFormat ffout=FileFormat.testOutput(out, FileFormat.PGM, null, true, overwrite, false, false); |
| 257 |
return writeModel(pgm, ffout); |
| 258 |
} |
| 259 |
|
| 260 |
public static boolean writeModel(GeneModel pgm, FileFormat ffout){ |
| 261 |
ByteStreamWriter bsw=ByteStreamWriter.makeBSW(ffout); |
| 262 |
|
| 263 |
ByteBuilder bb=new ByteBuilder(); |
| 264 |
pgm.appendTo(bb); |
| 265 |
|
| 266 |
boolean errorState=false; |
| 267 |
if(bsw!=null){ |
| 268 |
bsw.addJob(bb); |
| 269 |
errorState|=bsw.poisonAndWait(); |
| 270 |
} |
| 271 |
return errorState; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
private static void checkFileExistence(ArrayList<String> in, String out, boolean overwrite, boolean allowDupes){ |
| 276 |
|
| 277 |
if(!Tools.testOutputFiles(overwrite, false, false, out)){ |
| 278 |
outstream.println((out==null)+", "+out); |
| 279 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out+"\n"); |
| 280 |
} |
| 281 |
ArrayList<String> in2=new ArrayList<String>(); |
| 282 |
for(String s : in){ |
| 283 |
in2.add(s.split("@")[0]); |
| 284 |
} |
| 285 |
in=null; |
| 286 |
|
| 287 |
for(int i=0; i<in2.size(); i++){ |
| 288 |
String s=in2.get(i); |
| 289 |
if(s.equalsIgnoreCase("auto") || s.equalsIgnoreCase("default")){ |
| 290 |
in2.set(i, Data.findPath("?model.pgm")); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
ArrayList<String> foo=new ArrayList<String>(); |
| 296 |
foo.addAll(in2); |
| 297 |
if(!Tools.testInputFiles(allowDupes, true, foo.toArray(new String[0]))){ |
| 298 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
if(!allowDupes){ |
| 303 |
foo.add(out); |
| 304 |
if(!Tools.testForDuplicateFiles(true, foo.toArray(new String[0]))){ |
| 305 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
private static PrintStream outstream=System.err; |
| 313 |
public static boolean verbose=false; |
| 314 |
|
| 315 |
public static boolean normalize=false; |
| 316 |
|
| 317 |
} |