| 1 |
package var2; |
| 2 |
|
| 3 |
import java.util.HashSet; |
| 4 |
|
| 5 |
import fileIO.ReadWrite; |
| 6 |
import shared.Parse; |
| 7 |
import shared.Tools; |
| 8 |
import stream.SamLine; |
| 9 |
|
| 10 |
public class SamFilter { |
| 11 |
|
| 12 |
public boolean parse(String arg, String a, String b){ |
| 13 |
|
| 14 |
if(a.equals("min") || a.equals("minpos")){ |
| 15 |
minPos=Parse.parseIntKMG(b); |
| 16 |
assert(minPos<=maxPos) : "minPos>maxPos"; |
| 17 |
}else if(a.equals("max") || a.equals("maxpos")){ |
| 18 |
maxPos=Parse.parseIntKMG(b); |
| 19 |
assert(minPos<=maxPos) : "minPos>maxPos"; |
| 20 |
}else if(a.equals("minreadmapq") || a.equals("minsammapq") || a.equals("minmapq")){ |
| 21 |
minMapq=Parse.parseIntKMG(b); |
| 22 |
}else if(a.equals("maxreadmapq") || a.equals("maxsammapq") || a.equals("maxmapq")){ |
| 23 |
maxMapq=Parse.parseIntKMG(b); |
| 24 |
}else if(a.equals("mapped")){ |
| 25 |
includeMapped=Parse.parseBoolean(b); |
| 26 |
}else if(a.equals("unmapped")){ |
| 27 |
includeUnmapped=Parse.parseBoolean(b); |
| 28 |
}else if(a.equals("secondary") || a.equals("nonprimary")){ |
| 29 |
includeNonPrimary=Parse.parseBoolean(b); |
| 30 |
}else if(a.equals("supplimentary")){ |
| 31 |
includeSupplimentary=Parse.parseBoolean(b); |
| 32 |
}else if(a.equals("duplicate") || a.equals("duplicates")){ |
| 33 |
includeDuplicate=Parse.parseBoolean(b); |
| 34 |
}else if(a.equals("qfail") || a.equals("samqfail")){ |
| 35 |
includeQfail=Parse.parseBoolean(b); |
| 36 |
}else if(a.equals("lengthzero")){ |
| 37 |
includeLengthZero=Parse.parseBoolean(b); |
| 38 |
}else if(a.equals("invert")){ |
| 39 |
invert=Parse.parseBoolean(b); |
| 40 |
}else if(a.equals("minid")){ |
| 41 |
minId=Float.parseFloat(b); |
| 42 |
if(minId>1f){minId/=100;} |
| 43 |
assert(minId<=1f) : "minid should be between 0 and 1."; |
| 44 |
}else if(a.equals("maxid")){ |
| 45 |
maxId=Float.parseFloat(b); |
| 46 |
if(maxId>1f){maxId/=100;} |
| 47 |
assert(maxId<=1f) : "maxid should be between 0 and 1."; |
| 48 |
}else if(a.equals("contigs")){ |
| 49 |
addContig(b); |
| 50 |
}else{ |
| 51 |
return false; |
| 52 |
} |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
void addContig(String s){ |
| 61 |
if(s==null){return;} |
| 62 |
if(s.indexOf(',')>=0){ |
| 63 |
for(String s2 : s.split(",")){ |
| 64 |
addContig(s2); |
| 65 |
} |
| 66 |
} |
| 67 |
if(contigs==null){contigs=new HashSet<String>();} |
| 68 |
contigs.add(s); |
| 69 |
if(s.indexOf('_')>=0){addContig(s.replace('_', ' '));} |
| 70 |
String[] split=s.split("\\s+"); |
| 71 |
if(split.length>0 && !split[0].equals(s)){contigs.add(split[0]);} |
| 72 |
} |
| 73 |
|
| 74 |
public boolean passesFilter(SamLine sl){ |
| 75 |
if(sl==null){return false;} |
| 76 |
return invert^matchesFilter(sl); |
| 77 |
} |
| 78 |
|
| 79 |
boolean matchesFilter(SamLine sl){ |
| 80 |
if(sl==null){return false;} |
| 81 |
if(!includeLengthZero && sl.length()<1){return false;} |
| 82 |
|
| 83 |
if(!sl.mapped()){return includeUnmapped;} |
| 84 |
else if(!includeMapped){return false;} |
| 85 |
|
| 86 |
if(!includeNonPrimary && !sl.primary()){return false;} |
| 87 |
if(!includeSupplimentary && sl.supplementary()){return false;} |
| 88 |
if(!includeDuplicate && sl.duplicate()){return false;} |
| 89 |
|
| 90 |
if(minPos>Integer.MIN_VALUE || maxPos<Integer.MAX_VALUE){ |
| 91 |
final int start=sl.start(true, false); |
| 92 |
final int stop=sl.stop(start, true, false); |
| 93 |
if(!Tools.overlap(start, stop, minPos, maxPos)){return false;} |
| 94 |
} |
| 95 |
|
| 96 |
if(minMapq>Integer.MIN_VALUE || maxMapq<Integer.MAX_VALUE){ |
| 97 |
if(sl.mapq>maxMapq || sl.mapq<minMapq){return false;} |
| 98 |
} |
| 99 |
|
| 100 |
if(sl.cigar!=null && (minId>0 || maxId<1)){ |
| 101 |
float identity=sl.calcIdentity(); |
| 102 |
if(identity<minId || identity>maxId){return false;} |
| 103 |
} |
| 104 |
|
| 105 |
if(contigs!=null){ |
| 106 |
String rname=sl.rnameS(); |
| 107 |
if(rname==null){return false;} |
| 108 |
return contigs.contains(rname); |
| 109 |
} |
| 110 |
|
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
public boolean passesFilter(VCFLine vl){ |
| 115 |
if(vl==null){return false;} |
| 116 |
return invert^matchesFilter(vl); |
| 117 |
} |
| 118 |
|
| 119 |
boolean matchesFilter(VCFLine vl){ |
| 120 |
if(vl==null){return false;} |
| 121 |
|
| 122 |
if(minPos>Integer.MIN_VALUE || maxPos<Integer.MAX_VALUE){ |
| 123 |
final int start=vl.pos-1; |
| 124 |
final int stop=start+(Tools.max(0, vl.reflen-1)); |
| 125 |
if(!Tools.overlap(start, stop, minPos, maxPos)){return false;} |
| 126 |
} |
| 127 |
|
| 128 |
if(contigs!=null){ |
| 129 |
String rname=vl.scaf; |
| 130 |
if(rname==null){return false;} |
| 131 |
return contigs.contains(rname); |
| 132 |
} |
| 133 |
|
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
public boolean passesFilter(Var v, ScafMap map){ |
| 138 |
if(v==null){return false;} |
| 139 |
return invert^matchesFilter(v, map); |
| 140 |
} |
| 141 |
|
| 142 |
boolean matchesFilter(Var v, ScafMap map){ |
| 143 |
if(v==null){return false;} |
| 144 |
|
| 145 |
if(minPos>Integer.MIN_VALUE || maxPos<Integer.MAX_VALUE){ |
| 146 |
final int start=v.start; |
| 147 |
final int stop=v.stop; |
| 148 |
if(!Tools.overlap(start, stop, minPos, maxPos)){return false;} |
| 149 |
} |
| 150 |
|
| 151 |
if(contigs!=null){ |
| 152 |
String rname=map.getName(v.scafnum); |
| 153 |
if(rname==null){return false;} |
| 154 |
return contigs.contains(rname); |
| 155 |
} |
| 156 |
|
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
public boolean passesFilter(String name){ |
| 161 |
if(name==null){return false;} |
| 162 |
return invert^matchesFilter(name); |
| 163 |
} |
| 164 |
|
| 165 |
boolean matchesFilter(String name){ |
| 166 |
if(name==null){return false;} |
| 167 |
if(contigs!=null){ |
| 168 |
return contigs.contains(name); |
| 169 |
} |
| 170 |
return true; |
| 171 |
} |
| 172 |
|
| 173 |
public void clear() { |
| 174 |
minMapq=Integer.MIN_VALUE; |
| 175 |
maxMapq=Integer.MAX_VALUE; |
| 176 |
} |
| 177 |
|
| 178 |
public int minPos=Integer.MIN_VALUE; |
| 179 |
public int maxPos=Integer.MAX_VALUE; |
| 180 |
public int minMapq=Integer.MIN_VALUE; |
| 181 |
public int maxMapq=Integer.MAX_VALUE; |
| 182 |
public float minId=Integer.MIN_VALUE; |
| 183 |
public float maxId=Integer.MAX_VALUE; |
| 184 |
public boolean includeUnmapped=true; |
| 185 |
public boolean includeMapped=true; |
| 186 |
public boolean includeSupplimentary=true; |
| 187 |
public boolean includeQfail=false; |
| 188 |
public boolean includeDuplicate=true; |
| 189 |
public boolean includeNonPrimary=false; |
| 190 |
public boolean includeLengthZero=false; |
| 191 |
public HashSet<String> contigs=null; |
| 192 |
public boolean invert=false; |
| 193 |
|
| 194 |
public void setSamtoolsFilter(){ |
| 195 |
ReadWrite.SAMTOOLS_IGNORE_FLAG=0; |
| 196 |
if(!includeUnmapped){ReadWrite.SAMTOOLS_IGNORE_FLAG|=ReadWrite.SAM_UNMAPPED;} |
| 197 |
if(!includeNonPrimary){ReadWrite.SAMTOOLS_IGNORE_FLAG|=ReadWrite.SAM_SECONDARY;} |
| 198 |
if(!includeSupplimentary){ReadWrite.SAMTOOLS_IGNORE_FLAG|=ReadWrite.SAM_SUPPLIMENTARY;} |
| 199 |
if(!includeQfail){ReadWrite.SAMTOOLS_IGNORE_FLAG|=ReadWrite.SAM_QFAIL;} |
| 200 |
if(!includeDuplicate){ReadWrite.SAMTOOLS_IGNORE_FLAG|=ReadWrite.SAM_QFAIL;} |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
} |