| 1 |
package tracker; |
| 2 |
|
| 3 |
import java.util.LinkedHashMap; |
| 4 |
import java.util.Map.Entry; |
| 5 |
|
| 6 |
import fileIO.ByteFile; |
| 7 |
import fileIO.ReadWrite; |
| 8 |
import shared.LineParser1; |
| 9 |
import shared.Tools; |
| 10 |
|
| 11 |
public class SealStats { |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public SealStats(String fname_) { |
| 20 |
fname=fname_; |
| 21 |
load(fname); |
| 22 |
} |
| 23 |
|
| 24 |
void load(String fname) { |
| 25 |
ByteFile bf=ByteFile.makeByteFile1(fname, true); |
| 26 |
LineParser1 lp=new LineParser1('\t'); |
| 27 |
for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine()) { |
| 28 |
lp.set(line); |
| 29 |
if(Tools.startsWith(line, '#')) { |
| 30 |
if(lp.startsWith("#File")) { |
| 31 |
fname=lp.parseString(1); |
| 32 |
}else if(lp.startsWith("#Total")) { |
| 33 |
totalReads=lp.parseLong(1); |
| 34 |
totalBases=lp.parseLong(2); |
| 35 |
}else if(lp.startsWith("#Matched")) { |
| 36 |
matchedReads=lp.parseLong(1); |
| 37 |
} |
| 38 |
}else { |
| 39 |
String name=lp.parseString(0); |
| 40 |
long reads=lp.parseLong(1); |
| 41 |
long bases=lp.parseLong(3); |
| 42 |
SealStatsLine ssl=map.get(name); |
| 43 |
assert(ssl==null); |
| 44 |
ssl=new SealStatsLine(name, reads, bases); |
| 45 |
matchedBases+=bases; |
| 46 |
map.put(name, ssl); |
| 47 |
} |
| 48 |
} |
| 49 |
bf.close(); |
| 50 |
} |
| 51 |
|
| 52 |
public SealStatsLine countNonmatching(String name) { |
| 53 |
SealStatsLine sum=new SealStatsLine("!"+name, 0, 0); |
| 54 |
for(Entry<String, SealStatsLine> e : map.entrySet()) { |
| 55 |
String key=e.getKey(); |
| 56 |
if(!key.equals(name)) { |
| 57 |
SealStatsLine ssl=e.getValue(); |
| 58 |
sum.add(ssl); |
| 59 |
} |
| 60 |
} |
| 61 |
return sum; |
| 62 |
} |
| 63 |
|
| 64 |
public SealStatsLine countNonprimary(String name) { |
| 65 |
SealStatsLine primary=primary(); |
| 66 |
return countNonmatching(primary.name); |
| 67 |
} |
| 68 |
|
| 69 |
public SealStatsLine primary() { |
| 70 |
SealStatsLine primary=null; |
| 71 |
for(Entry<String, SealStatsLine> e : map.entrySet()) { |
| 72 |
SealStatsLine ssl=e.getValue(); |
| 73 |
if(primary==null || ssl.compareTo(primary)>0) {primary=ssl;} |
| 74 |
} |
| 75 |
return primary; |
| 76 |
} |
| 77 |
|
| 78 |
public String fnamePrefix() { |
| 79 |
return ReadWrite.stripToCore(fname); |
| 80 |
} |
| 81 |
|
| 82 |
public static class SealStatsLine implements Comparable<SealStatsLine> { |
| 83 |
|
| 84 |
public SealStatsLine(String name_, long reads_, long bases_) { |
| 85 |
name=name_; |
| 86 |
reads=reads_; |
| 87 |
bases=bases_; |
| 88 |
} |
| 89 |
|
| 90 |
public void add(SealStatsLine ssl) { |
| 91 |
reads+=ssl.reads; |
| 92 |
bases+=ssl.bases; |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public int compareTo(SealStatsLine o) { |
| 97 |
if(reads!=o.reads) {return reads>o.reads ? 1 : -1;} |
| 98 |
if(bases!=o.bases) {return bases>o.bases ? 1 : -1;} |
| 99 |
return name.compareTo(o.name); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public boolean equals(Object o) {return equals((SealStatsLine)o);} |
| 104 |
public boolean equals(SealStatsLine o) {return name.equals(o.name);} |
| 105 |
|
| 106 |
public String name; |
| 107 |
public long reads; |
| 108 |
public long bases; |
| 109 |
} |
| 110 |
|
| 111 |
public String fname; |
| 112 |
public long totalReads, totalBases; |
| 113 |
public long matchedReads, matchedBases; |
| 114 |
public LinkedHashMap<String, SealStatsLine> map=new LinkedHashMap<String, SealStatsLine>(); |
| 115 |
|
| 116 |
} |