| 1 |
package driver; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.Arrays; |
| 6 |
import java.util.LinkedHashMap; |
| 7 |
|
| 8 |
import fileIO.TextFile; |
| 9 |
import fileIO.TextStreamWriter; |
| 10 |
import shared.Parse; |
| 11 |
import shared.Parser; |
| 12 |
import shared.PreParser; |
| 13 |
import shared.Tools; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public class SummarizeQuast { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public static void main(String[] args){ |
| 27 |
|
| 28 |
SummarizeQuast sq=new SummarizeQuast(args); |
| 29 |
|
| 30 |
|
| 31 |
LinkedHashMap<String, LinkedHashMap<String, ArrayList<Double>>> map=sq.summarize(); |
| 32 |
|
| 33 |
sq.print(map); |
| 34 |
} |
| 35 |
|
| 36 |
public SummarizeQuast(String[] args){ |
| 37 |
|
| 38 |
{ |
| 39 |
PreParser pp=new PreParser(args, getClass(), false); |
| 40 |
args=pp.args; |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
ArrayList<String> names=new ArrayList<String>(); |
| 45 |
Parser parser=new Parser(); |
| 46 |
|
| 47 |
|
| 48 |
for(int i=0; i<args.length; i++){ |
| 49 |
|
| 50 |
final String arg=args[i]; |
| 51 |
String[] split=arg.split("="); |
| 52 |
String a=split[0].toLowerCase(); |
| 53 |
String b=split.length>1 ? split[1] : null; |
| 54 |
|
| 55 |
if(a.equals("required")){ |
| 56 |
requiredString=b; |
| 57 |
}else if(a.equals("normalize")){ |
| 58 |
normalize=Parse.parseBoolean(b); |
| 59 |
}else if(a.equals("box")){ |
| 60 |
box=Parse.parseBoolean(b); |
| 61 |
}else if(parser.parse(arg, a, b)){ |
| 62 |
|
| 63 |
}else if(!arg.contains("=")){ |
| 64 |
String[] x=(new File(arg).exists() ? new String[] {arg} : arg.split(",")); |
| 65 |
for(String x2 : x){ |
| 66 |
if(new File(x2).exists()){ |
| 67 |
names.add(x2); |
| 68 |
} |
| 69 |
} |
| 70 |
}else{ |
| 71 |
throw new RuntimeException("Unknown parameter "+arg); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
{ |
| 76 |
out=(parser.out1==null ? "stdout" : parser.out1); |
| 77 |
if(parser.in1!=null){ |
| 78 |
String[] x=(new File(parser.in1).exists() ? new String[] {parser.in1} : parser.in1.split(",")); |
| 79 |
for(String x2 : x){names.add(x2);} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
in=new ArrayList<String>(); |
| 84 |
for(String s : names){ |
| 85 |
Tools.getFileOrFiles(s, in, false, false, false, true); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
public LinkedHashMap<String, LinkedHashMap<String, ArrayList<Double>>> summarize(){ |
| 90 |
|
| 91 |
ArrayList<QuastSummary> alqs=new ArrayList<QuastSummary>(); |
| 92 |
for(String path : in){ |
| 93 |
QuastSummary qs=new QuastSummary(path); |
| 94 |
if(normalize){qs.normalize();} |
| 95 |
alqs.add(qs); |
| 96 |
} |
| 97 |
|
| 98 |
LinkedHashMap<String, LinkedHashMap<String, ArrayList<Double>>> metricMap=new LinkedHashMap<String, LinkedHashMap<String, ArrayList<Double>>>(); |
| 99 |
for(QuastSummary qs : alqs){ |
| 100 |
for(String metricName : qs.metrics.keySet()){ |
| 101 |
LinkedHashMap<String, ArrayList<Double>> asmMap=metricMap.get(metricName); |
| 102 |
if(asmMap==null){ |
| 103 |
asmMap=new LinkedHashMap<String, ArrayList<Double>>(); |
| 104 |
metricMap.put(metricName, asmMap); |
| 105 |
} |
| 106 |
ArrayList<Entry> ale=qs.metrics.get(metricName); |
| 107 |
assert(ale!=null); |
| 108 |
|
| 109 |
for(Entry e : ale){ |
| 110 |
ArrayList<Double> ald=asmMap.get(e.assembly); |
| 111 |
if(ald==null){ |
| 112 |
ald=new ArrayList<Double>(); |
| 113 |
asmMap.put(e.assembly, ald); |
| 114 |
} |
| 115 |
ald.add(e.value); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return metricMap; |
| 121 |
} |
| 122 |
|
| 123 |
public void print(LinkedHashMap<String, LinkedHashMap<String, ArrayList<Double>>> metricMap){ |
| 124 |
TextStreamWriter tsw=new TextStreamWriter(out, true, false, false); |
| 125 |
tsw.start(); |
| 126 |
for(String metricName : metricMap.keySet()){ |
| 127 |
LinkedHashMap<String, ArrayList<Double>> asmMap=metricMap.get(metricName); |
| 128 |
if(asmMap!=null && !asmMap.isEmpty()){ |
| 129 |
tsw.println("\n"+metricName); |
| 130 |
assert(!asmMap.isEmpty()); |
| 131 |
for(String asm : asmMap.keySet()){ |
| 132 |
ArrayList<Double> ald=asmMap.get(asm); |
| 133 |
assert(ald!=null); |
| 134 |
assert(!ald.isEmpty()); |
| 135 |
if(ald!=null && !ald.isEmpty()){ |
| 136 |
tsw.print(asm); |
| 137 |
if(box){ |
| 138 |
double[] array=new double[ald.size()]; |
| 139 |
for(int i=0; i<ald.size(); i++){array[i]=ald.get(i);} |
| 140 |
Arrays.sort(array); |
| 141 |
final int len=array.length-1; |
| 142 |
tsw.print("\t"+array[(int)Math.round(0.1*len)]); |
| 143 |
tsw.print("\t"+array[(int)Math.round(0.25*len)]); |
| 144 |
tsw.print("\t"+array[(int)Math.round(0.5*len)]); |
| 145 |
tsw.print("\t"+array[(int)Math.round(0.75*len)]); |
| 146 |
tsw.print("\t"+array[(int)Math.round(0.9*len)]); |
| 147 |
}else{ |
| 148 |
for(Double d : ald){ |
| 149 |
tsw.print("\t"+d); |
| 150 |
} |
| 151 |
} |
| 152 |
tsw.println(); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
tsw.poisonAndWait(); |
| 158 |
} |
| 159 |
|
| 160 |
private class QuastSummary{ |
| 161 |
|
| 162 |
QuastSummary(String path_){ |
| 163 |
path=path_; |
| 164 |
metrics=process(path); |
| 165 |
} |
| 166 |
|
| 167 |
LinkedHashMap<String, ArrayList<Entry>> process(String fname){ |
| 168 |
LinkedHashMap<String, ArrayList<Entry>> map=new LinkedHashMap<String, ArrayList<Entry>>(); |
| 169 |
TextFile tf=new TextFile(fname); |
| 170 |
String[] header=null; |
| 171 |
for(String line=tf.nextLine(); line!=null; line=tf.nextLine()){ |
| 172 |
String[] split=line.split("\t"); |
| 173 |
if(header==null){ |
| 174 |
header=split; |
| 175 |
}else{ |
| 176 |
final String row=split[0]; |
| 177 |
ArrayList<Entry> list=new ArrayList<Entry>(split.length-1); |
| 178 |
map.put(row, list); |
| 179 |
for(int i=1; i<split.length; i++){ |
| 180 |
final String col=header[i]; |
| 181 |
if(requiredString==null || col.contains(requiredString)){ |
| 182 |
try { |
| 183 |
Entry e=new Entry(col, split[i]); |
| 184 |
if(!Double.isNaN(e.value) && !Double.isInfinite(e.value)){ |
| 185 |
list.add(e); |
| 186 |
} |
| 187 |
} catch (NumberFormatException ex) { |
| 188 |
|
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
} |
| 196 |
} |
| 197 |
return map; |
| 198 |
} |
| 199 |
|
| 200 |
void normalize(){ |
| 201 |
for(ArrayList<Entry> list : metrics.values()){ |
| 202 |
normalize(list); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
private void normalize(ArrayList<Entry> list){ |
| 207 |
if(list.isEmpty()){return;} |
| 208 |
if(list==null || list.isEmpty()){return;} |
| 209 |
double sum=0; |
| 210 |
for(Entry e : list){ |
| 211 |
sum+=e.value; |
| 212 |
} |
| 213 |
double avg=sum/list.size(); |
| 214 |
double mult=(avg==0 ? 1 : 1/avg); |
| 215 |
for(Entry e : list){ |
| 216 |
e.value*=mult; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
final LinkedHashMap<String, ArrayList<Entry>> metrics; |
| 221 |
final String path; |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
private class Entry{ |
| 226 |
|
| 227 |
Entry(String assembly_, String value_) throws NumberFormatException { |
| 228 |
this(assembly_, Double.parseDouble(value_)); |
| 229 |
} |
| 230 |
|
| 231 |
Entry(String assembly_, double value_){ |
| 232 |
assembly=assembly_; |
| 233 |
value=value_; |
| 234 |
} |
| 235 |
|
| 236 |
String assembly; |
| 237 |
double value; |
| 238 |
|
| 239 |
} |
| 240 |
|
| 241 |
final ArrayList<String> in; |
| 242 |
final String out; |
| 243 |
|
| 244 |
String requiredString=null; |
| 245 |
boolean normalize=true; |
| 246 |
boolean box=true; |
| 247 |
|
| 248 |
} |