| 1 |
package var; |
| 2 |
|
| 3 |
import dna.ChromosomeArray; |
| 4 |
import dna.Data; |
| 5 |
import fileIO.ReadWrite; |
| 6 |
import fileIO.TextFile; |
| 7 |
import fileIO.TextStreamWriter; |
| 8 |
import shared.Parse; |
| 9 |
import shared.PreParser; |
| 10 |
import shared.Timer; |
| 11 |
import shared.Tools; |
| 12 |
import structures.CoverageArray; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public class GenerateConsensusVariations { |
| 20 |
|
| 21 |
public static void main(String[] args){ |
| 22 |
{ |
| 23 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); |
| 24 |
args=pp.args; |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
Timer t=new Timer(); |
| 29 |
|
| 30 |
String inVarsPattern=args[0]; |
| 31 |
String inCovPattern=args[1]; |
| 32 |
String outPattern=args[2]; |
| 33 |
|
| 34 |
assert(!inVarsPattern.equalsIgnoreCase(outPattern)); |
| 35 |
assert(!inCovPattern.equalsIgnoreCase(outPattern)); |
| 36 |
|
| 37 |
int minChrom=-1; |
| 38 |
int maxChrom=-1; |
| 39 |
int minCoverage=1; |
| 40 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 41 |
|
| 42 |
for(int i=3; i<args.length; i++){ |
| 43 |
final String arg=args[i].toLowerCase(); |
| 44 |
String[] split=arg.split("="); |
| 45 |
String a=split[0]; |
| 46 |
String b=split.length>1 ? split[1] : null; |
| 47 |
|
| 48 |
if(a.startsWith("mincov")){ |
| 49 |
minCoverage=Integer.parseInt(b); |
| 50 |
assert(minCoverage>0); |
| 51 |
}else if(a.startsWith("consensus")){ |
| 52 |
consensusRatio=Float.parseFloat(b); |
| 53 |
|
| 54 |
assert(consensusRatio>=0f && consensusRatio<=1f); |
| 55 |
consensusRatioNR=1-(1-consensusRatio)*.5f; |
| 56 |
|
| 57 |
}else if(a.equals("genome") || a.equals("build")){ |
| 58 |
Data.setGenome(Integer.parseInt(b)); |
| 59 |
if(minChrom==-1){minChrom=1;} |
| 60 |
if(maxChrom==-1){maxChrom=Data.numChroms;} |
| 61 |
}else if(a.equals("verbose")){ |
| 62 |
verbose=Parse.parseBoolean(b); |
| 63 |
}else if(a.equals("minchrom")){ |
| 64 |
minChrom=Integer.parseInt(b); |
| 65 |
}else if(a.equals("maxchrom")){ |
| 66 |
maxChrom=Integer.parseInt(b); |
| 67 |
}else if(a.equals("threads") || a.equals("t")){ |
| 68 |
THREADS=Integer.parseInt(b); |
| 69 |
}else if(a.startsWith("noref") || a.startsWith("undef")){ |
| 70 |
NOREF_CAP=Integer.parseInt(b); |
| 71 |
}else{ |
| 72 |
System.err.println("Unknown argument "+arg); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
for(int chrom=minChrom; chrom<=maxChrom; chrom++){ |
| 77 |
process(inVarsPattern.replaceFirst("#", ""+chrom), inCovPattern.replaceFirst("#", ""+chrom), outPattern.replaceFirst("#", ""+chrom), chrom, minCoverage); |
| 78 |
} |
| 79 |
|
| 80 |
t.stop(); |
| 81 |
|
| 82 |
System.out.println(); |
| 83 |
System.out.println("Vars in: \t"+(VARS_IN-NOREFS_IN)); |
| 84 |
System.out.println("Length Delta in: \t"+VARLEN_IN); |
| 85 |
System.out.println("No-refs in: \t"+NOREFS_IN); |
| 86 |
System.out.println(); |
| 87 |
System.out.println("Vars out: \t"+(VARS_OUT-NOREFS_OUT)); |
| 88 |
System.out.println("Length Delta out: \t"+VARLEN_OUT); |
| 89 |
System.out.println("No-refs out: \t"+NOREFS_OUT); |
| 90 |
System.out.println(); |
| 91 |
System.out.println("Time: \t"+t); |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
public static void process(final String invars, final String incov, final String outfile, final int chrom, final int mincov){ |
| 97 |
TextFile tf=new TextFile(invars, true); |
| 98 |
CoverageArray ca=ReadWrite.read(CoverageArray.class, incov, true); |
| 99 |
TextStreamWriter tsw=new TextStreamWriter(outfile, true, false, true); |
| 100 |
tsw.start(); |
| 101 |
|
| 102 |
ChromosomeArray cha=Data.getChromosome(chrom); |
| 103 |
|
| 104 |
Varlet prev=null; |
| 105 |
|
| 106 |
tsw.println(Varlet.header()); |
| 107 |
|
| 108 |
for(String s=tf.nextLine(); s!=null; s=tf.nextLine()){ |
| 109 |
if(s.charAt(0)!='#'){ |
| 110 |
Varlet v=Varlet.fromText(s); |
| 111 |
VARS_IN++; |
| 112 |
int dif=v.lengthDif(); |
| 113 |
VARLEN_IN+=dif; |
| 114 |
if(v.varType==Variation.NOREF){NOREFS_IN++;} |
| 115 |
|
| 116 |
boolean passes=passesFilter(v, ca, cha, mincov); |
| 117 |
boolean overlap=(prev==null ? false : v.beginLoc<=prev.endLoc); |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
if(!overlap){ |
| 123 |
if(prev!=null){ |
| 124 |
StringBuilder sb=prev.toText().append('\n'); |
| 125 |
tsw.print(sb); |
| 126 |
VARS_OUT++; |
| 127 |
VARLEN_OUT+=prev.lengthDif(); |
| 128 |
if(prev.varType==Variation.NOREF){NOREFS_OUT++;} |
| 129 |
} |
| 130 |
prev=null; |
| 131 |
}else{ |
| 132 |
if(passes && v.score()>prev.score()){ |
| 133 |
prev=null; |
| 134 |
}else{ |
| 135 |
v=null; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if(passes && v!=null){ |
| 140 |
prev=v; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
if(prev!=null){ |
| 156 |
StringBuilder sb=prev.toText().append('\n'); |
| 157 |
tsw.print(sb); |
| 158 |
VARS_OUT++; |
| 159 |
VARLEN_OUT+=prev.lengthDif(); |
| 160 |
if(prev.varType==Variation.NOREF){NOREFS_OUT++;} |
| 161 |
} |
| 162 |
|
| 163 |
tf.close(); |
| 164 |
tsw.poison(); |
| 165 |
Data.unload(chrom, true); |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
private static boolean passesFilter(Varlet v, CoverageArray ca, ChromosomeArray cha, int minCoverageToPass) { |
| 176 |
|
| 177 |
int dif=v.lengthDif(); |
| 178 |
|
| 179 |
int midLoc=(v.beginLoc+v.endLoc)/2; |
| 180 |
int midCov=ca.get(midLoc); |
| 181 |
int maxCov=midCov, minCov=midCov; |
| 182 |
|
| 183 |
int bound1, bound2; |
| 184 |
float ratio; |
| 185 |
|
| 186 |
if(verbose){System.err.println("\nConsidering varlet "+v);} |
| 187 |
|
| 188 |
if(v.varType==Variation.NOREF){ |
| 189 |
bound1=v.beginLoc; |
| 190 |
bound2=v.endLoc; |
| 191 |
minCoverageToPass=minCoverageToPass*2+5; |
| 192 |
ratio=consensusRatioNR; |
| 193 |
}else{ |
| 194 |
bound1=v.beginLoc; |
| 195 |
bound2=v.endLoc; |
| 196 |
ratio=consensusRatio; |
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
for(int i=bound1; i<=bound2; i++){ |
| 201 |
int cov=ca.get(i); |
| 202 |
minCov=Tools.min(minCov, cov); |
| 203 |
maxCov=Tools.max(maxCov, cov); |
| 204 |
if(verbose){System.err.println("minCov = "+minCov+", maxCov = "+maxCov);} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
if(minCov<minCoverageToPass){ |
| 209 |
if(verbose){System.err.println("Low coverage, "+minCov+"<"+minCoverageToPass+"\n"+v);} |
| 210 |
return false; |
| 211 |
} |
| 212 |
int minReads=(int)Math.ceil(ratio*minCov); |
| 213 |
if(v.numReads<minReads){ |
| 214 |
if(verbose){System.err.println("Low reads, mincov="+minCov+", "+v.numReads+"<"+minReads+"\n"+v);} |
| 215 |
return false; |
| 216 |
} |
| 217 |
if(v.minStrandReads()<1 && v.numSemiUniqueReads<2*minCoverageToPass){ |
| 218 |
if(verbose){System.err.println("Low strands, mincov="+minCov+", "+v.minStrandReads()+"<"+1+"\n"+v);} |
| 219 |
return false; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
if(v.varType==Variation.NOREF){ |
| 224 |
if(NOREF_CAP>=0){ |
| 225 |
int a=Tools.max(v.beginLoc-NOREF_CAP, cha.minIndex); |
| 226 |
int b=Tools.min(v.endLoc+NOREF_CAP, cha.maxIndex); |
| 227 |
if(cha.isFullyUndefined(a, b)){ |
| 228 |
if(verbose){System.err.println("Noref cap, mincov="+minCov+"\n"+v);} |
| 229 |
return false; |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
if(verbose){System.err.println("Retaining variation.");} |
| 234 |
return true; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public static int THREADS=1; |
| 240 |
public static int NOREF_CAP=-1; |
| 241 |
public static float consensusRatio=1f; |
| 242 |
public static float consensusRatioNR=1f; |
| 243 |
public static long VARS_IN=0; |
| 244 |
public static long VARLEN_IN=0; |
| 245 |
public static long NOREFS_IN=0; |
| 246 |
public static long VARS_OUT=0; |
| 247 |
public static long VARLEN_OUT=0; |
| 248 |
public static long NOREFS_OUT=0; |
| 249 |
public static boolean verbose=false; |
| 250 |
|
| 251 |
} |