| 1 |
package align2; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Arrays; |
| 5 |
|
| 6 |
import bloom.BloomFilter; |
| 7 |
import dna.AminoAcid; |
| 8 |
import dna.ChromosomeArray; |
| 9 |
import dna.Data; |
| 10 |
import jgi.CoveragePileup; |
| 11 |
import shared.Shared; |
| 12 |
import shared.Tools; |
| 13 |
import shared.TrimRead; |
| 14 |
import stream.ConcurrentReadInputStream; |
| 15 |
import stream.ConcurrentReadOutputStream; |
| 16 |
import stream.Read; |
| 17 |
import stream.SamLine; |
| 18 |
import stream.SiteScore; |
| 19 |
import structures.ListNum; |
| 20 |
import structures.LongList; |
| 21 |
import tracker.ReadStats; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public abstract class AbstractMapThread extends Thread { |
| 31 |
|
| 32 |
AbstractMapThread(ConcurrentReadInputStream cris_, |
| 33 |
ConcurrentReadOutputStream outStream_, ConcurrentReadOutputStream outStreamMapped_, ConcurrentReadOutputStream outStreamUnmapped_, ConcurrentReadOutputStream outStreamBlack_, |
| 34 |
CoveragePileup pileup_, boolean SLOW_ALIGN_, boolean LOCAL_ALIGN_, boolean AMBIGUOUS_TOSS_, |
| 35 |
boolean AMBIGUOUS_RANDOM_, boolean AMBIGUOUS_ALL_, boolean TRIM_LEFT_, boolean TRIM_RIGHT_, boolean UNTRIM_, float TRIM_QUAL_, int MIN_TRIM_LEN_, int THRESH_, |
| 36 |
int minChrom_, int maxChrom_, int KFILTER_, float IDFILTER_, boolean KILL_BAD_PAIRS_, boolean SAVE_AMBIGUOUS_XY_, |
| 37 |
boolean REQUIRE_CORRECT_STRANDS_PAIRS_, |
| 38 |
boolean SAME_STRAND_PAIRS_, boolean DO_RESCUE_, boolean STRICT_MAX_INDEL_, int SLOW_ALIGN_PADDING_, int SLOW_RESCUE_PADDING_, |
| 39 |
String MSA_TYPE_, int keylen_, boolean PERFECTMODE_, boolean SEMIPERFECTMODE_, boolean FORBID_SELF_MAPPING_, boolean RCOMP_MATE_, |
| 40 |
boolean MAKE_MATCH_STRING_, boolean OUTPUT_MAPPED_ONLY_, boolean DONT_OUTPUT_BLACKLISTED_READS_, boolean PRINT_SECONDARY_ALIGNMENTS_, |
| 41 |
boolean QUICK_MATCH_STRINGS_, int MAX_SITESCORES_TO_PRINT_, float MINIMUM_ALIGNMENT_SCORE_RATIO_, |
| 42 |
float keyDensity_, float maxKeyDensity_, float minKeyDensity_, int maxDesiredKeys_, |
| 43 |
int MIN_APPROX_HITS_TO_KEEP_, boolean USE_EXTENDED_SCORE_, int BASE_HIT_SCORE_, boolean USE_AFFINE_SCORE_, int MAX_INDEL_, |
| 44 |
boolean TRIM_LIST_, int TIP_DELETION_SEARCH_RANGE_, BloomFilter bloomFilter_){ |
| 45 |
|
| 46 |
|
| 47 |
cris=cris_; |
| 48 |
outStream=outStream_; |
| 49 |
outStreamMapped=outStreamMapped_; |
| 50 |
outStreamUnmapped=outStreamUnmapped_; |
| 51 |
outStreamBlack=outStreamBlack_; |
| 52 |
pileup=pileup_; |
| 53 |
|
| 54 |
SLOW_ALIGN=SLOW_ALIGN_; |
| 55 |
LOCAL_ALIGN=LOCAL_ALIGN_; |
| 56 |
AMBIGUOUS_TOSS=AMBIGUOUS_TOSS_; |
| 57 |
AMBIGUOUS_RANDOM=AMBIGUOUS_RANDOM_; |
| 58 |
AMBIGUOUS_ALL=AMBIGUOUS_ALL_; |
| 59 |
TRIM_LEFT=TRIM_LEFT_; |
| 60 |
TRIM_RIGHT=TRIM_RIGHT_; |
| 61 |
UNTRIM=UNTRIM_; |
| 62 |
TRIM_QUAL=TRIM_QUAL_; |
| 63 |
TRIM_ERROR_RATE=(float)QualityTools.phredToProbError(TRIM_QUAL); |
| 64 |
TRIM_MIN_LENGTH=MIN_TRIM_LEN_; |
| 65 |
THRESH=THRESH_; |
| 66 |
minChrom=minChrom_; |
| 67 |
maxChrom=maxChrom_; |
| 68 |
KFILTER=KFILTER_; |
| 69 |
IDFILTER=IDFILTER_; |
| 70 |
RenameByInsert=AbstractMapper.RenameByInsert; |
| 71 |
|
| 72 |
KILL_BAD_PAIRS=KILL_BAD_PAIRS_; |
| 73 |
SAVE_AMBIGUOUS_XY=SAVE_AMBIGUOUS_XY_; |
| 74 |
|
| 75 |
SLOW_ALIGN_PADDING=SLOW_ALIGN_PADDING_; |
| 76 |
SLOW_RESCUE_PADDING=SLOW_RESCUE_PADDING_; |
| 77 |
DO_RESCUE=DO_RESCUE_; |
| 78 |
STRICT_MAX_INDEL=STRICT_MAX_INDEL_; |
| 79 |
BANDWIDTH=MSA.bandwidth; |
| 80 |
PAIRED=cris.paired(); |
| 81 |
REQUIRE_CORRECT_STRANDS_PAIRS=REQUIRE_CORRECT_STRANDS_PAIRS_; |
| 82 |
SAME_STRAND_PAIRS=SAME_STRAND_PAIRS_; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
TRIM_LIST=TRIM_LIST_; |
| 87 |
TIP_DELETION_SEARCH_RANGE=TIP_DELETION_SEARCH_RANGE_; |
| 88 |
FIND_TIP_DELETIONS=TIP_DELETION_SEARCH_RANGE>0; |
| 89 |
|
| 90 |
MIN_APPROX_HITS_TO_KEEP=MIN_APPROX_HITS_TO_KEEP_; |
| 91 |
USE_EXTENDED_SCORE=USE_EXTENDED_SCORE_; |
| 92 |
BASE_HIT_SCORE=BASE_HIT_SCORE_; |
| 93 |
BASE_KEY_HIT_SCORE=BASE_HIT_SCORE*keylen_; |
| 94 |
USE_AFFINE_SCORE=USE_AFFINE_SCORE_; |
| 95 |
EXPECTED_LEN_LIMIT=(ALIGN_COLUMNS()*17)/20-(2*(SLOW_ALIGN_PADDING+10)); |
| 96 |
MAX_INDEL=MAX_INDEL_; |
| 97 |
ALIGN_COLUMNS_ABSTRACT=ALIGN_COLUMNS(); |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
KEYLEN=keylen_; |
| 103 |
keyDensity=keyDensity_; |
| 104 |
maxKeyDensity=maxKeyDensity_; |
| 105 |
minKeyDensity=minKeyDensity_; |
| 106 |
maxDesiredKeys=maxDesiredKeys_; |
| 107 |
|
| 108 |
MINIMUM_ALIGNMENT_SCORE_RATIO=MINIMUM_ALIGNMENT_SCORE_RATIO_; |
| 109 |
MINIMUM_ALIGNMENT_SCORE_RATIO_PAIRED=Tools.max(MINIMUM_ALIGNMENT_SCORE_RATIO*.80f, 1-((1-MINIMUM_ALIGNMENT_SCORE_RATIO)*1.4f)); |
| 110 |
MINIMUM_ALIGNMENT_SCORE_RATIO_PRE_RESCUE=Tools.max(MINIMUM_ALIGNMENT_SCORE_RATIO*.60f, 1-((1-MINIMUM_ALIGNMENT_SCORE_RATIO)*1.8f)); |
| 111 |
|
| 112 |
MAKE_MATCH_STRING=(MAKE_MATCH_STRING_ || STRICT_MAX_INDEL_); |
| 113 |
assert(SLOW_ALIGN_PADDING>=0); |
| 114 |
|
| 115 |
OUTPUT_MAPPED_ONLY=OUTPUT_MAPPED_ONLY_; |
| 116 |
DONT_OUTPUT_BLACKLISTED_READS=DONT_OUTPUT_BLACKLISTED_READS_; |
| 117 |
MAX_SITESCORES_TO_PRINT=MAX_SITESCORES_TO_PRINT_; |
| 118 |
PRINT_SECONDARY_ALIGNMENTS=PRINT_SECONDARY_ALIGNMENTS_; |
| 119 |
QUICK_MATCH_STRINGS=((QUICK_MATCH_STRINGS_ || STRICT_MAX_INDEL_) && MAKE_MATCH_STRING); |
| 120 |
|
| 121 |
RCOMP_MATE=RCOMP_MATE_; |
| 122 |
PERFECTMODE=PERFECTMODE_; |
| 123 |
SEMIPERFECTMODE=SEMIPERFECTMODE_; |
| 124 |
FORBID_SELF_MAPPING=FORBID_SELF_MAPPING_; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
MSA_TYPE=MSA_TYPE_; |
| 131 |
EXTRA_PADDING=(BANDWIDTH<1 && (MSA.bandwidthRatio<=0 || MSA.bandwidthRatio>=0.2f) ? |
| 132 |
EXTRA_PADDING : Tools.min(EXTRA_PADDING, Tools.max(BANDWIDTH/4, (int)(MSA.bandwidthRatio*60)))); |
| 133 |
|
| 134 |
AVERAGE_PAIR_DIST=INITIAL_AVERAGE_PAIR_DIST; |
| 135 |
|
| 136 |
if(SLOW_ALIGN || MAKE_MATCH_STRING){ |
| 137 |
msa=MSA.makeMSA(ALIGN_ROWS(), ALIGN_COLUMNS(), MSA_TYPE); |
| 138 |
POINTS_MATCH=msa.POINTS_MATCH(); |
| 139 |
POINTS_MATCH2=msa.POINTS_MATCH2(); |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
CLEARZONE1e=2*POINTS_MATCH2-POINTS_MATCH-msa.POINTS_SUB()+1; |
| 146 |
}else{ |
| 147 |
POINTS_MATCH=70; |
| 148 |
POINTS_MATCH2=100; |
| 149 |
msa=null; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
CLEARZONE1e=0; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
GENERATE_KEY_SCORES_FROM_QUALITY=AbstractIndex.GENERATE_KEY_SCORES_FROM_QUALITY; |
| 164 |
readstats=(ReadStats.collectingStats() ? new ReadStats() : null); |
| 165 |
bloomFilter=bloomFilter_; |
| 166 |
|
| 167 |
PROCESS_EDIT_FILTER=(SUBFILTER>=0 || DELFILTER>=0 || INSFILTER>=0 || INDELFILTER>=0 || DELLENFILTER>=0 || INSLENFILTER>=0 || EDITFILTER>=0 || NFILTER>=0); |
| 168 |
} |
| 169 |
|
| 170 |
public abstract int ALIGN_COLUMNS(); |
| 171 |
public abstract int ALIGN_ROWS(); |
| 172 |
abstract int CLEARZONE1(); |
| 173 |
|
| 174 |
abstract AbstractIndex index(); |
| 175 |
|
| 176 |
public final void postFilterRead(Read r, byte[] basesM, int maxImperfectSwScore, int maxSwScore){ |
| 177 |
if(!r.mapped() || r.perfect()){return;} |
| 178 |
assert(Read.CHECKSITES(r, basesM)); |
| 179 |
ensureMatchStringOnPrimary(r, basesM, maxImperfectSwScore, maxSwScore); |
| 180 |
if(!r.mapped() || r.perfect()){return;} |
| 181 |
assert(r.match!=null) : "Postfiltering does not work with cigar strings disabled."; |
| 182 |
boolean removedTop=false; |
| 183 |
if(verbose && (PROCESS_EDIT_FILTER || IDFILTER>0)){ |
| 184 |
System.err.println("\nBefore filtering: sites=\n"+r.sites); |
| 185 |
|
| 186 |
} |
| 187 |
removedTop=processMapqFilter(r, AbstractMapper.MIN_MAPQ, AbstractMapper.MIN_MAPQ_UNPAIRED) | removedTop; |
| 188 |
removedTop=processIDFilter(r, basesM, maxImperfectSwScore, maxSwScore) | removedTop; |
| 189 |
removedTop=processEditFilter(r, basesM, maxImperfectSwScore, maxSwScore) | removedTop; |
| 190 |
if(verbose && (PROCESS_EDIT_FILTER || IDFILTER>0)){ |
| 191 |
System.err.println("\nAfter filtering: removedTop="+removedTop+", sites=\n"+r.sites); |
| 192 |
} |
| 193 |
if(removedTop && r.mapped() && r.match==null){ |
| 194 |
ensureMatchStringOnPrimary(r, basesM, maxImperfectSwScore, maxSwScore); |
| 195 |
ensureMatchStringsOnSiteScores(r, basesM, maxImperfectSwScore, maxSwScore); |
| 196 |
postFilterRead(r, basesM, maxImperfectSwScore, maxSwScore); |
| 197 |
if(verbose){ |
| 198 |
System.err.println("\nAfter filtering2: sites=\n"+r.sites); |
| 199 |
} |
| 200 |
} |
| 201 |
if(removedTop && r.mapped()){ |
| 202 |
for(SiteScore ss : r.sites){ |
| 203 |
ss.setScore(Tools.min(ss.score, ss.score/4)); |
| 204 |
int newSlowScore=Tools.min(ss.slowScore, ss.slowScore/4); |
| 205 |
int newPairedScore=(ss.pairedScore<=ss.slowScore ? 0 : Tools.max(newSlowScore+1, Tools.min(ss.pairedScore, ss.pairedScore/4))); |
| 206 |
ss.setSlowPairedScore(newSlowScore, newPairedScore); |
| 207 |
} |
| 208 |
SiteScore top=r.topSite(); |
| 209 |
assert(top!=null) : r; |
| 210 |
r.mapScore=top.score; |
| 211 |
if(verbose){ |
| 212 |
System.err.println("\nAfter filtering3: sites=\n"+r.sites); |
| 213 |
} |
| 214 |
r.setAmbiguous(true); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
final int ensureMatchStringsOnSiteScores(Read r, byte[] basesM, int maxImperfectSwScore, int maxSwScore){ |
| 219 |
if(!r.mapped() || r.numSites()<1){return 0;} |
| 220 |
int removed=0; |
| 221 |
int generated=0; |
| 222 |
final SiteScore top=r.topSite(); |
| 223 |
for(int i=1, lim=MAX_SITESCORES_TO_PRINT; i<lim+removed && i<r.numSites(); i++){ |
| 224 |
SiteScore ss=r.sites.get(i); |
| 225 |
if(ss.match==null){ |
| 226 |
genMatchStringForSite(r.numericID, ss, r.bases, basesM, maxImperfectSwScore, maxSwScore, r.mate, PRINT_SECONDARY_ALIGNMENTS); |
| 227 |
if(ss.match==null){ |
| 228 |
r.sites.set(i, null); |
| 229 |
removed++; |
| 230 |
}else{ |
| 231 |
generated++; |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
if(removed>0){Tools.condenseStrict(r.sites);} |
| 236 |
if(generated>0){Shared.sort(r.sites);} |
| 237 |
if(r.topSite()!=top){ |
| 238 |
r.setFromTopSite(); |
| 239 |
if(r.mate!=null){ |
| 240 |
r.setPaired(false); |
| 241 |
r.mate.setPaired(false); |
| 242 |
} |
| 243 |
} |
| 244 |
return removed; |
| 245 |
} |
| 246 |
|
| 247 |
final int ensureMatchStringOnPrimary(final Read r, final byte[] basesM, final int maxImperfectSwScore, final int maxSwScore){ |
| 248 |
if(!r.mapped() || r.numSites()<1){return 0;} |
| 249 |
if(r.match!=null){ |
| 250 |
assert(r.numSites()>0); |
| 251 |
assert(r.sites.get(0).match==r.match); |
| 252 |
return 0; |
| 253 |
} |
| 254 |
int removed=0; |
| 255 |
int generated=0; |
| 256 |
final SiteScore top=r.topSite(); |
| 257 |
|
| 258 |
boolean success=false; |
| 259 |
for(int i=0, lim=r.numSites(); i<lim && !success; i++){ |
| 260 |
SiteScore ss=r.sites.get(i); |
| 261 |
if(ss.match==null){ |
| 262 |
genMatchStringForSite(r.numericID, ss, r.bases, basesM, maxImperfectSwScore, maxSwScore, r.mate, false); |
| 263 |
if(ss.match==null){ |
| 264 |
r.sites.set(i, null); |
| 265 |
removed++; |
| 266 |
}else{ |
| 267 |
generated++; |
| 268 |
} |
| 269 |
} |
| 270 |
success=ss.match!=null; |
| 271 |
} |
| 272 |
if(removed>0){Tools.condenseStrict(r.sites);} |
| 273 |
if(generated>0){Shared.sort(r.sites);} |
| 274 |
|
| 275 |
if(r.numSites()<1){ |
| 276 |
r.clearMapping(); |
| 277 |
return removed; |
| 278 |
} |
| 279 |
|
| 280 |
if(r.sites.get(0).match==null){return removed+ensureMatchStringOnPrimary(r, basesM, maxImperfectSwScore, maxSwScore);} |
| 281 |
|
| 282 |
r.setFromTopSite(); |
| 283 |
if(r.topSite()!=top && r.mate!=null){ |
| 284 |
r.setPaired(false); |
| 285 |
r.mate.setPaired(false); |
| 286 |
} |
| 287 |
|
| 288 |
return removed; |
| 289 |
} |
| 290 |
|
| 291 |
public final boolean processMapqFilter(Read r, int minMapq, int minMapqUnpaired) { |
| 292 |
if(minMapq<1 && minMapqUnpaired<1 || !r.mapped()) {return false;} |
| 293 |
int mapq=SamLine.toMapq(r, null); |
| 294 |
boolean clear=false; |
| 295 |
if(mapq<minMapq) {clear=true;} |
| 296 |
else if(mapq<minMapqUnpaired && r.mateMapped() && !r.paired()){ |
| 297 |
if(SamLine.toMapq(r.mate, null)>=mapq) {clear=true;} |
| 298 |
} |
| 299 |
if(!clear) {return false;} |
| 300 |
|
| 301 |
r.clearMapping(); |
| 302 |
if(r.mate!=null) {r.mate.setPaired(false);} |
| 303 |
r.sites=null; |
| 304 |
return true; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
public final boolean processIDFilter(Read r, byte[] basesM, int maxImperfectSwScore, int maxSwScore){ |
| 309 |
if(IDFILTER<=0){return false;} |
| 310 |
if(!r.mapped() || r.perfect()){return false;} |
| 311 |
assert(r.match!=null) : "Identity Filter does not work with cigar strings disabled."; |
| 312 |
if(!r.paired() && Read.identityFlat(r.match, true)<IDFILTER){ |
| 313 |
r.clearMapping(); |
| 314 |
if(r.mate!=null){ |
| 315 |
r.setPaired(false); |
| 316 |
r.mate.setPaired(false); |
| 317 |
} |
| 318 |
} |
| 319 |
boolean removedTop=false; |
| 320 |
if(r.sites!=null){ |
| 321 |
int removed=0; |
| 322 |
for(int i=r.sites.size()-1; i>0; i--){ |
| 323 |
SiteScore ss=r.sites.get(i); |
| 324 |
if(removedTop && ss.match==null){ |
| 325 |
genMatchStringForSite(r.numericID, ss, r.bases, basesM, maxImperfectSwScore, maxSwScore, r.mate, PRINT_SECONDARY_ALIGNMENTS); |
| 326 |
} |
| 327 |
if(ss.match!=null && !ss.perfect() && Read.identityFlat(ss.match, true)<IDFILTER){ |
| 328 |
r.sites.set(i, null); |
| 329 |
removed++; |
| 330 |
if(i==0){removedTop=true;} |
| 331 |
} |
| 332 |
} |
| 333 |
if(removed>0){Tools.condenseStrict(r.sites);} |
| 334 |
} |
| 335 |
if(removedTop){ |
| 336 |
if(r.mate!=null){ |
| 337 |
r.setPaired(false); |
| 338 |
r.mate.setPaired(false); |
| 339 |
} |
| 340 |
if(r.sites==null || r.sites.isEmpty() || r.topSite().match==null){ |
| 341 |
r.clearMapping(); |
| 342 |
}else{ |
| 343 |
r.setFromTopSite(); |
| 344 |
} |
| 345 |
} |
| 346 |
return removedTop; |
| 347 |
} |
| 348 |
|
| 349 |
public final boolean processEditFilter(Read r, byte[] basesM, int maxImperfectSwScore, int maxSwScore){ |
| 350 |
if(!PROCESS_EDIT_FILTER || !r.mapped() || r.match==null || r.perfect()){return false;} |
| 351 |
assert(r.match!=null) : "Edit Filter does not work with cigar strings disabled."; |
| 352 |
boolean removedTop=false; |
| 353 |
if(r.sites!=null){ |
| 354 |
int removed=0; |
| 355 |
for(int i=r.sites.size()-1; i>=0; i--){ |
| 356 |
SiteScore ss=r.sites.get(i); |
| 357 |
if(removedTop && ss.match==null){ |
| 358 |
genMatchStringForSite(r.numericID, ss, r.bases, basesM, maxImperfectSwScore, maxSwScore, r.mate, PRINT_SECONDARY_ALIGNMENTS); |
| 359 |
} |
| 360 |
if(ss.match!=null && !ss.semiperfect()){ |
| 361 |
final int sub=Read.countSubs(ss.match); |
| 362 |
final int ins=Read.countInsertions(ss.match); |
| 363 |
final int del=Read.countDeletions(ss.match); |
| 364 |
final int ns=Read.countNocalls(ss.match); |
| 365 |
final int inscount=Read.countInsertionEvents(ss.match); |
| 366 |
final int delcount=Read.countDeletionEvents(ss.match); |
| 367 |
|
| 368 |
boolean bad=false; |
| 369 |
bad=bad||(SUBFILTER>=0 && sub>SUBFILTER); |
| 370 |
|
| 371 |
bad=bad||(INSFILTER>=0 && inscount>INSFILTER); |
| 372 |
|
| 373 |
bad=bad||(DELFILTER>=0 && delcount>DELFILTER); |
| 374 |
|
| 375 |
bad=bad||(INSLENFILTER>=0 && Read.hasLongInsertion(ss.match, INSLENFILTER)); |
| 376 |
|
| 377 |
bad=bad||(DELLENFILTER>=0 && Read.hasLongDeletion(ss.match, DELLENFILTER)); |
| 378 |
|
| 379 |
bad=bad||(INDELFILTER>=0 && inscount+delcount>INDELFILTER); |
| 380 |
|
| 381 |
bad=bad||(EDITFILTER>=0 && sub+ins+del>EDITFILTER); |
| 382 |
|
| 383 |
bad=bad||(NFILTER>=0 && ns>NFILTER); |
| 384 |
|
| 385 |
|
| 386 |
if(bad){ |
| 387 |
r.sites.set(i, null); |
| 388 |
removed++; |
| 389 |
if(i==0){removedTop=true;} |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
} |
| 394 |
} |
| 395 |
if(removed>0){Tools.condenseStrict(r.sites);} |
| 396 |
} |
| 397 |
if(removedTop){ |
| 398 |
if(r.mate!=null){ |
| 399 |
r.setPaired(false); |
| 400 |
r.mate.setPaired(false); |
| 401 |
} |
| 402 |
if(r.sites==null || r.sites.isEmpty()){ |
| 403 |
r.clearMapping(); |
| 404 |
}else{ |
| 405 |
r.setFromTopSite(); |
| 406 |
} |
| 407 |
} |
| 408 |
return removedTop; |
| 409 |
} |
| 410 |
|
| 411 |
@Override |
| 412 |
public final void run() { |
| 413 |
|
| 414 |
|
| 415 |
ListNum<Read> ln=cris.nextList(); |
| 416 |
ArrayList<Read> readlist=ln.list; |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
final boolean black=(Blacklist.hasBlacklist()); |
| 426 |
final boolean MAKE_QUALITY_HISTOGRAM=(readstats==null ? false : ReadStats.COLLECT_QUALITY_STATS); |
| 427 |
final boolean MAKE_MATCH_HISTOGRAM=(readstats==null ? false : ReadStats.COLLECT_MATCH_STATS); |
| 428 |
final boolean MAKE_INSERT_HISTOGRAM=(readstats==null ? false : ReadStats.COLLECT_INSERT_STATS); |
| 429 |
final boolean MAKE_BASE_HISTOGRAM=(readstats==null ? false : ReadStats.COLLECT_BASE_STATS); |
| 430 |
final boolean MAKE_QUALITY_ACCURACY=(readstats==null ? false : ReadStats.COLLECT_QUALITY_ACCURACY); |
| 431 |
|
| 432 |
final boolean MAKE_EHIST=(readstats==null ? false : ReadStats.COLLECT_ERROR_STATS); |
| 433 |
final boolean MAKE_INDELHIST=(readstats==null ? false : ReadStats.COLLECT_INDEL_STATS); |
| 434 |
final boolean MAKE_LHIST=(readstats==null ? false : ReadStats.COLLECT_LENGTH_STATS); |
| 435 |
final boolean MAKE_GCHIST=(readstats==null ? false : ReadStats.COLLECT_GC_STATS); |
| 436 |
final boolean MAKE_IDHIST=(readstats==null ? false : ReadStats.COLLECT_IDENTITY_STATS); |
| 437 |
final boolean MAKE_TIMEHIST=(readstats==null ? false : ReadStats.COLLECT_TIME_STATS); |
| 438 |
final boolean MAKE_COVERAGE=(pileup==null ? false : true); |
| 439 |
|
| 440 |
if(SKIP_INITIAL>0){ |
| 441 |
while(!readlist.isEmpty()){ |
| 442 |
|
| 443 |
if(readlist.get(readlist.size()-1).numericID<SKIP_INITIAL){ |
| 444 |
|
| 445 |
}else{ |
| 446 |
while(readlist.get(0).numericID<SKIP_INITIAL){readlist.remove(0);} |
| 447 |
break; |
| 448 |
} |
| 449 |
|
| 450 |
writeList(new ArrayList<Read>(1), black, ln.id); |
| 451 |
|
| 452 |
cris.returnList(ln.id, readlist.isEmpty()); |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
ln=cris.nextList(); |
| 460 |
readlist=ln.list; |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
final LongList bloomBuffer=(bloomFilter==null ? null : new LongList(150)); |
| 465 |
while(!readlist.isEmpty()){ |
| 466 |
|
| 467 |
if(MAX_READ_LENGTH>0 || MIN_READ_LENGTH>0){ |
| 468 |
Tools.breakReads(readlist, MAX_READ_LENGTH, MIN_READ_LENGTH, verbose ? System.err : null); |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
for(int i=0; i<readlist.size(); i++){ |
| 473 |
|
| 474 |
long startTime=0; |
| 475 |
if(TIME_TAG){startTime=System.nanoTime();} |
| 476 |
|
| 477 |
Read r=readlist.get(i); |
| 478 |
readsIn1++; |
| 479 |
readsIn2+=r.mateCount(); |
| 480 |
basesIn1+=r.length(); |
| 481 |
basesIn2+=r.mateLength(); |
| 482 |
assert(r.mate==null || (r.pairnum()==0 && r.mate.pairnum()==1)) : r.pairnum()+", "+r.mate.pairnum(); |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
final boolean passesBloomFilter=(bloomFilter==null ? false : bloomFilter.passes(r, r.mate, bloomBuffer, 1)); |
| 491 |
|
| 492 |
if(passesBloomFilter){ |
| 493 |
basesUsed1+=r.length(); |
| 494 |
basesUsed2+=r.mateLength(); |
| 495 |
readsPassedBloomFilter+=r.pairCount(); |
| 496 |
basesPassedBloomFilter+=r.pairLength(); |
| 497 |
readsUsed1++; |
| 498 |
readsUsed2+=r.mateCount(); |
| 499 |
}else{ |
| 500 |
if(r.synthetic()){ |
| 501 |
syntheticReads++; |
| 502 |
if(r.originalSite==null){r.makeOriginalSite();} |
| 503 |
r.clearSite(); |
| 504 |
if(r.mate!=null){ |
| 505 |
assert(r.mate.synthetic()); |
| 506 |
if(r.mate.originalSite==null){r.mate.makeOriginalSite();} |
| 507 |
r.mate.clearSite(); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
r.clearAnswers(true); |
| 513 |
|
| 514 |
assert(r.bases==null || r.length()<=maxReadLength()) : "Read "+r.numericID+", length "+r.length()+", exceeds the limit of "+maxReadLength()+"\n"+ |
| 515 |
"You can map the reads in chunks by reformatting to fasta, then mapping with the setting 'fastareadlen="+maxReadLength()+"'"; |
| 516 |
final Read r2=r.mate; |
| 517 |
|
| 518 |
if(MAKE_QUALITY_HISTOGRAM){readstats.addToQualityHistogram(r);} |
| 519 |
if(MAKE_BASE_HISTOGRAM){readstats.addToBaseHistogram(r);} |
| 520 |
|
| 521 |
if(MAKE_LHIST){readstats.addToLengthHistogram(r);} |
| 522 |
if(MAKE_GCHIST){readstats.addToGCHistogram(r);} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
if(TRIM_LEFT || TRIM_RIGHT){ |
| 529 |
TrimRead.trim(r, TRIM_LEFT, TRIM_RIGHT, TRIM_QUAL, TRIM_ERROR_RATE, TRIM_MIN_LENGTH); |
| 530 |
TrimRead.trim(r2, TRIM_LEFT, TRIM_RIGHT, TRIM_QUAL, TRIM_ERROR_RATE, TRIM_MIN_LENGTH); |
| 531 |
} |
| 532 |
|
| 533 |
if(RCOMP){r.reverseComplement();} |
| 534 |
|
| 535 |
if(r2==null){ |
| 536 |
final byte[] basesP=r.bases; |
| 537 |
final byte[] basesM=AminoAcid.reverseComplementBases(basesP); |
| 538 |
basesUsed1+=(basesM==null ? 0 : basesM.length); |
| 539 |
processRead(r, basesM); |
| 540 |
capSiteList(r, MAX_SITESCORES_TO_PRINT, PRINT_SECONDARY_ALIGNMENTS); |
| 541 |
assert(Read.CHECKSITES(r, basesM)); |
| 542 |
}else{ |
| 543 |
if(RCOMP_MATE!=RCOMP){r2.reverseComplement();} |
| 544 |
final byte[] basesP1=r.bases; |
| 545 |
final byte[] basesM1=AminoAcid.reverseComplementBases(basesP1); |
| 546 |
final byte[] basesP2=r2.bases; |
| 547 |
final byte[] basesM2=AminoAcid.reverseComplementBases(basesP2); |
| 548 |
basesUsed1+=(basesM1==null ? 0 : basesM1.length); |
| 549 |
basesUsed2+=(basesM2==null ? 0 : basesM2.length); |
| 550 |
assert(r2.bases==null || r2.length()<=maxReadLength()) : |
| 551 |
"Read "+r2.numericID+", length "+r2.length()+", exceeds the limit of "+maxReadLength()+"\n"+ |
| 552 |
"You can map the reads in chunks by reformatting to fasta, then mapping with the setting 'fastareadlen="+maxReadLength()+"'"; |
| 553 |
processReadPair(r, basesM1, basesM2); |
| 554 |
capSiteList(r, MAX_SITESCORES_TO_PRINT, PRINT_SECONDARY_ALIGNMENTS); |
| 555 |
capSiteList(r2, MAX_SITESCORES_TO_PRINT, PRINT_SECONDARY_ALIGNMENTS); |
| 556 |
|
| 557 |
assert(Read.CHECKSITES(r, basesM1)); |
| 558 |
assert(Read.CHECKSITES(r2, basesM2)); |
| 559 |
|
| 560 |
} |
| 561 |
|
| 562 |
if(UNTRIM && (TRIM_LEFT || TRIM_RIGHT)){ |
| 563 |
TrimRead.untrim(r); |
| 564 |
TrimRead.untrim(r2); |
| 565 |
} |
| 566 |
|
| 567 |
if(MAKE_MATCH_HISTOGRAM){readstats.addToMatchHistogram(r);} |
| 568 |
if(MAKE_INSERT_HISTOGRAM && r.paired()){readstats.addToInsertHistogram(r, (SAME_STRAND_PAIRS || !REQUIRE_CORRECT_STRANDS_PAIRS));} |
| 569 |
if(MAKE_QUALITY_ACCURACY){readstats.addToQualityAccuracy(r);} |
| 570 |
|
| 571 |
if(MAKE_EHIST){readstats.addToErrorHistogram(r);} |
| 572 |
if(MAKE_INDELHIST){readstats.addToIndelHistogram(r);} |
| 573 |
if(MAKE_IDHIST){readstats.addToIdentityHistogram(r);} |
| 574 |
|
| 575 |
if(TIME_TAG){ |
| 576 |
final Long time=(System.nanoTime()-startTime+500)/1000; |
| 577 |
r.setObj(time); |
| 578 |
if(r2!=null){r2.setObj(time);} |
| 579 |
assert(r.fetchObject()!=null && r.getObjectClass()==Long.class); |
| 580 |
if(MAKE_TIMEHIST){readstats.addToTimeHistogram(r);} |
| 581 |
} |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
if(RenameByInsert){ |
| 586 |
boolean ignoreStrand=(!REQUIRE_CORRECT_STRANDS_PAIRS || SAME_STRAND_PAIRS); |
| 587 |
for(Read r : readlist){ |
| 588 |
if(r.mapped() && r.mateMapped() && r.paired()){ |
| 589 |
int insert=Read.insertSizeMapped(r, r.mate, ignoreStrand); |
| 590 |
String s="insert="+insert; |
| 591 |
r.id=s+" 1:"+r.numericID; |
| 592 |
r.mate.id=s+" 2:"+r.numericID; |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
if(MAKE_COVERAGE){ |
| 598 |
synchronized(pileup){ |
| 599 |
for(Read r : readlist){ |
| 600 |
pileup.processRead(r); |
| 601 |
if(r.mate!=null){pileup.processRead(r.mate);} |
| 602 |
} |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
writeList(readlist, black, ln.id); |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
cris.returnList(ln.id, false); |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
ln=cris.nextList(); |
| 620 |
readlist=ln.list; |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
assert(readlist.isEmpty()); |
| 627 |
cris.returnList(ln.id, true); |
| 628 |
finish(); |
| 629 |
} |
| 630 |
|
| 631 |
private final void writeList(ArrayList<Read> readlist, boolean black, long listNumID){ |
| 632 |
if(outStreamMapped!=null){ |
| 633 |
ArrayList<Read> x=new ArrayList<Read>(readlist.size()); |
| 634 |
for(Read r1 : readlist){ |
| 635 |
if(r1!=null){ |
| 636 |
Read r2=r1.mate; |
| 637 |
if(r1.mapped() || (r2!=null && r2.mapped())){ |
| 638 |
if(!black || !Blacklist.inBlacklist(r1)){x.add(r1);} |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
outStreamMapped.add(x, listNumID); |
| 643 |
} |
| 644 |
|
| 645 |
if(outStreamBlack!=null){ |
| 646 |
ArrayList<Read> x=new ArrayList<Read>(readlist.size()); |
| 647 |
for(Read r1 : readlist){ |
| 648 |
if(black && Blacklist.inBlacklist(r1)){x.add(r1);} |
| 649 |
} |
| 650 |
outStreamBlack.add(x, listNumID); |
| 651 |
} |
| 652 |
|
| 653 |
if(BBSplitter.streamTable!=null || BBSplitter.TRACK_SET_STATS || BBSplitter.TRACK_SCAF_STATS){ |
| 654 |
BBSplitter.printReads(readlist, listNumID, null, CLEARZONE1()); |
| 655 |
} |
| 656 |
|
| 657 |
if(outStreamUnmapped!=null){ |
| 658 |
ArrayList<Read> x=new ArrayList<Read>(readlist.size()); |
| 659 |
for(Read r1 : readlist){ |
| 660 |
if(r1!=null){ |
| 661 |
Read r2=r1.mate; |
| 662 |
if(!(r1.mapped() || (r2!=null && r2.mapped()))){ |
| 663 |
x.add(r1); |
| 664 |
} |
| 665 |
} |
| 666 |
} |
| 667 |
outStreamUnmapped.add(x, listNumID); |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
if(outStream!=null){ |
| 672 |
if(OUTPUT_MAPPED_ONLY){removeUnmapped(readlist);} |
| 673 |
if(black && DONT_OUTPUT_BLACKLISTED_READS){removeBlacklisted(readlist);} |
| 674 |
for(Read r : readlist){ |
| 675 |
if(r!=null){ |
| 676 |
if(CLEAR_ATTACHMENT){r.nullifyObject();} |
| 677 |
assert(r.bases!=null); |
| 678 |
if(r.sites!=null && r.sites.isEmpty()){r.sites=null;} |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
outStream.add(readlist, listNumID); |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
public final int quickMap(final Read r, final byte[] basesM){ |
| 689 |
final AbstractIndex index=index(); |
| 690 |
byte[] basesP=r.bases; |
| 691 |
if(basesP.length<KEYLEN){return 0;} |
| 692 |
assert(basesP.length>=KEYLEN); |
| 693 |
|
| 694 |
if(PERFECTMODE || SEMIPERFECTMODE){ |
| 695 |
if(r.containsUndefined()){return-1;} |
| 696 |
}else if(DISCARD_MOSTLY_UNDEFINED_READS){ |
| 697 |
int n=r.countUndefined(); |
| 698 |
if(n>25 && basesP.length-n<n){return -1;} |
| 699 |
} |
| 700 |
if(MIN_AVERAGE_QUALITY>0){ |
| 701 |
if(r.avgQualityByProbabilityDouble(false, MIN_AVERAGE_QUALITY_BASES)<MIN_AVERAGE_QUALITY){return -1;} |
| 702 |
} |
| 703 |
|
| 704 |
final int keyProbLen=basesP.length-KEYLEN+1; |
| 705 |
final float[] keyProbs=index.keyProbArray(); |
| 706 |
int[] offsets; |
| 707 |
|
| 708 |
float keyDen2=((maxDesiredKeys*KEYLEN)/(float)basesP.length); |
| 709 |
keyDen2=Tools.max(minKeyDensity, keyDen2); |
| 710 |
keyDen2=Tools.min(keyDensity, keyDen2, KEYLEN); |
| 711 |
|
| 712 |
float keyDen3; |
| 713 |
if(basesP.length<=50){ |
| 714 |
keyDen3=maxKeyDensity; |
| 715 |
}else if(basesP.length>=200){ |
| 716 |
keyDen3=maxKeyDensity-0.5f; |
| 717 |
}else{ |
| 718 |
keyDen3=maxKeyDensity-0.003333333333f*(basesP.length-50); |
| 719 |
} |
| 720 |
keyDen3=Tools.max(keyDensity, keyDen3); |
| 721 |
keyDen3=Tools.min(KEYLEN, keyDen3); |
| 722 |
|
| 723 |
if(GENERATE_KEY_SCORES_FROM_QUALITY || r.quality==null){ |
| 724 |
QualityTools.makeKeyProbs(r.quality, r.bases, KEYLEN, keyProbs, USE_MODULO); |
| 725 |
|
| 726 |
boolean offsetsMode3=true; |
| 727 |
if(offsetsMode3){ |
| 728 |
offsets=KeyRing.makeOffsets3(keyProbs, r.length(), KEYLEN, keyDen2, keyDen3, 2, (PERFECTMODE || SEMIPERFECTMODE)); |
| 729 |
}else{ |
| 730 |
|
| 731 |
offsets=KeyRing.makeOffsets2(keyProbs, r.length(), KEYLEN, keyDen2, keyDen3, 2); |
| 732 |
int numKeys=(offsets==null ? 0 : offsets.length+1); |
| 733 |
int maxRounds=0; |
| 734 |
while(maxRounds>0 && offsets!=null && offsets.length<numKeys){ |
| 735 |
numKeys=offsets.length; |
| 736 |
offsets=QualityTools.modifyOffsets(offsets, keyProbs); |
| 737 |
maxRounds--; |
| 738 |
} |
| 739 |
} |
| 740 |
}else{ |
| 741 |
offsets=KeyRing.makeOffsets(r.quality, KEYLEN, keyDensity, 2); |
| 742 |
} |
| 743 |
if(verbose){System.err.println("Made offsets: "+Arrays.toString(offsets));} |
| 744 |
|
| 745 |
if(offsets==null || offsets.length<AbstractIndex.MIN_APPROX_HITS_TO_KEEP || (r.quality!=null && r.avgQuality(false, 0)<2)){return -1;} |
| 746 |
|
| 747 |
|
| 748 |
final byte[] baseScoresP=index.getBaseScoreArray(basesP.length, 0); |
| 749 |
final int[] keyScoresP=index.getKeyScoreArray(offsets.length, 0); |
| 750 |
|
| 751 |
if(AbstractIndex.USE_EXTENDED_SCORE){ |
| 752 |
if(AbstractIndex.GENERATE_BASE_SCORES_FROM_QUALITY){ |
| 753 |
QualityTools.makeByteScoreArray(r.quality, 100, baseScoresP, true); |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
if(GENERATE_KEY_SCORES_FROM_QUALITY){ |
| 758 |
int a=BASE_KEY_HIT_SCORE; |
| 759 |
int baseKeyScore=a/8; |
| 760 |
int range=a-baseKeyScore; |
| 761 |
final int[] keyScoresAll=new int[keyProbLen]; |
| 762 |
QualityTools.makeKeyScores(keyProbs, keyProbLen, range, baseKeyScore, keyScoresAll); |
| 763 |
|
| 764 |
float probAllErrors=1f; |
| 765 |
for(int i=0; i<offsets.length; i++){ |
| 766 |
keyScoresP[i]=keyScoresAll[offsets[i]]; |
| 767 |
probAllErrors*=keyProbs[offsets[i]]; |
| 768 |
} |
| 769 |
if(probAllErrors>0.50f){return -1;} |
| 770 |
if(verbose){System.err.println("Prob all errors = "+probAllErrors+"\n\n");} |
| 771 |
}else{ |
| 772 |
Arrays.fill(keyScoresP, BASE_KEY_HIT_SCORE); |
| 773 |
} |
| 774 |
if(verbose){System.err.println("Made key scores: "+Arrays.toString(keyScoresP));} |
| 775 |
|
| 776 |
keysUsed+=offsets.length; |
| 777 |
int maxScore=index.maxScore(offsets, baseScoresP, keyScoresP, basesP.length, true); |
| 778 |
if(verbose){System.err.println("Max Score: "+maxScore);} |
| 779 |
assert(maxScore>0); |
| 780 |
|
| 781 |
ArrayList<SiteScore> list=index.findAdvanced(basesP, basesM, r.quality, baseScoresP, keyScoresP, offsets, r.numericID); |
| 782 |
if(verbose){System.err.println("list: "+list);} |
| 783 |
|
| 784 |
r.sites=list; |
| 785 |
removeOutOfBounds(r, OUTPUT_MAPPED_ONLY, OUTPUT_SAM, EXPECTED_LEN_LIMIT); |
| 786 |
assert(Read.CHECKSITES(list, r.bases, basesM, r.numericID, false)); |
| 787 |
if(FORBID_SELF_MAPPING){forbidSelfMapping(list, r.originalSite);} |
| 788 |
|
| 789 |
if(list==null || list.isEmpty()){ |
| 790 |
r.sites=null; |
| 791 |
}else{ |
| 792 |
r.sites=list; |
| 793 |
if(!SLOW_ALIGN && AbstractIndex.USE_AFFINE_SCORE){ |
| 794 |
for(SiteScore ss : list){ss.setSlowScore(ss.quickScore);} |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
return maxScore; |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
final int scoreNoIndels(final Read r, final byte[] basesP, final byte[] basesM, final int maxSwScore, final int maxImperfectSwScore){ |
| 808 |
|
| 809 |
if(!SLOW_ALIGN || r.numSites()==0){return 0;} |
| 810 |
|
| 811 |
int numPerfectScores=0; |
| 812 |
int numNearPerfectScores=0; |
| 813 |
int bestScoreNoIndel=Integer.MIN_VALUE; |
| 814 |
|
| 815 |
boolean forceSlow=false; |
| 816 |
|
| 817 |
for(int j=0; j<r.sites.size(); j++){ |
| 818 |
|
| 819 |
SiteScore ss=r.sites.get(j); |
| 820 |
int oldScore=ss.score; |
| 821 |
int sslen=ss.stop()-ss.start()+1; |
| 822 |
|
| 823 |
|
| 824 |
final byte[] bases=(ss.strand==Shared.PLUS ? basesP : basesM); |
| 825 |
|
| 826 |
if(AbstractIndex.USE_AFFINE_SCORE && ss.quickScore==maxSwScore){ |
| 827 |
assert(ss.stop()==ss.start()+r.length()-1) : ss.toText()+", "+maxSwScore+", "+maxImperfectSwScore+ |
| 828 |
", "+r.length()+", "+(ss.start()+r.length()-1); |
| 829 |
} |
| 830 |
|
| 831 |
if(verbose){System.err.print("C) Changing SiteScore from "+ss+"\n");} |
| 832 |
|
| 833 |
int slowScoreNoIndel; |
| 834 |
if(ss.perfect){ |
| 835 |
if(verbose){System.err.print("C1");} |
| 836 |
numNearPerfectScores++; |
| 837 |
assert(ss.semiperfect); |
| 838 |
assert(ss.stop()-ss.start()+1==bases.length); |
| 839 |
|
| 840 |
slowScoreNoIndel=maxSwScore; |
| 841 |
ss.setSlowScore(slowScoreNoIndel); |
| 842 |
ss.setScore(slowScoreNoIndel); |
| 843 |
ss.gaps=null; |
| 844 |
}else{ |
| 845 |
if(verbose){System.err.print("C2");} |
| 846 |
ChromosomeArray cha=Data.getChromosome(ss.chrom); |
| 847 |
slowScoreNoIndel=msa.scoreNoIndels(bases, cha.array, ss.start(), (sslen==bases.length ? ss : null)); |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
if(slowScoreNoIndel<oldScore && oldScore>=maxImperfectSwScore && ss.stop()-ss.start()+1!=bases.length){ |
| 852 |
int slowScoreNoIndel2=msa.scoreNoIndels(bases, cha.array, ss.stop()-bases.length+1, null); |
| 853 |
if(slowScoreNoIndel2>=maxImperfectSwScore){ |
| 854 |
slowScoreNoIndel=slowScoreNoIndel2; |
| 855 |
ss.setStart(ss.stop()-bases.length+1); |
| 856 |
ss.setPerfect(bases); |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
ss.setSlowScore(slowScoreNoIndel); |
| 861 |
ss.setScore(slowScoreNoIndel); |
| 862 |
|
| 863 |
|
| 864 |
if(slowScoreNoIndel>=maxImperfectSwScore){ |
| 865 |
if(verbose){System.err.print("C3");} |
| 866 |
numNearPerfectScores++; |
| 867 |
|
| 868 |
ss.setStop(ss.start()+bases.length-1); |
| 869 |
ss.gaps=null; |
| 870 |
if(slowScoreNoIndel>=maxSwScore){ |
| 871 |
if(verbose){System.err.print("C4");} |
| 872 |
assert(slowScoreNoIndel==maxSwScore) : slowScoreNoIndel+">"+maxSwScore; |
| 873 |
numPerfectScores++; |
| 874 |
ss.perfect=ss.semiperfect=true; |
| 875 |
}else{ |
| 876 |
if(verbose){System.err.print("C5");} |
| 877 |
assert(!ss.perfect); |
| 878 |
ss.setPerfect(bases); |
| 879 |
assert(!ss.perfect); |
| 880 |
} |
| 881 |
if(QUICK_MATCH_STRINGS && !ss.perfect && (PRINT_SECONDARY_ALIGNMENTS || slowScoreNoIndel>=bestScoreNoIndel)){ |
| 882 |
ss.match=msa.genMatchNoIndels(bases, cha.array, ss.start()); |
| 883 |
} |
| 884 |
}else if(oldScore>=maxImperfectSwScore){ |
| 885 |
if(verbose){System.err.print("C6");} |
| 886 |
forceSlow=true; |
| 887 |
}else if(PRINT_SECONDARY_ALIGNMENTS){ |
| 888 |
if(verbose){System.err.print("C7");} |
| 889 |
forceSlow=true; |
| 890 |
}else if(verbose){ |
| 891 |
if(verbose){System.err.print("C8");} |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
if(verbose){System.err.print("\nto "+ss+"\n");} |
| 896 |
|
| 897 |
bestScoreNoIndel=Tools.max(ss.slowScore, bestScoreNoIndel); |
| 898 |
|
| 899 |
} |
| 900 |
return (forceSlow ? -numNearPerfectScores : numNearPerfectScores); |
| 901 |
} |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
public final void genMatchString(final Read r, final byte[] basesP, final byte[] basesM, final int maxImperfectSwScore, final int maxSwScore, boolean setSSScore, final boolean recur){ |
| 906 |
if(verbose){System.err.println("\n\n\n\n\ngenMatchString for read\n"+r+"\n\n\n\n\n");} |
| 907 |
assert(Read.CHECKSITES(r, basesM)); |
| 908 |
assert(checkTopSite(r)); |
| 909 |
|
| 910 |
assert(r.mate!=null || r.numSites()==0 || r.topSite().score==r.mapScore) : "\n"+r.toText(false)+"\n"; |
| 911 |
assert(msa!=null); |
| 912 |
if(r.numSites()==0){ |
| 913 |
r.chrom=-1; |
| 914 |
assert(r.mate!=null || r.numSites()==0 || r.topSite().score==r.mapScore) : "\n"+r.toText(false)+"\n"; |
| 915 |
return; |
| 916 |
} |
| 917 |
|
| 918 |
if(PRINT_SECONDARY_ALIGNMENTS){ |
| 919 |
capSiteList(r, MAX_SITESCORES_TO_PRINT+3, PRINT_SECONDARY_ALIGNMENTS); |
| 920 |
} |
| 921 |
|
| 922 |
if(QUICK_MATCH_STRINGS && PRINT_SECONDARY_ALIGNMENTS && USE_SS_MATCH_FOR_PRIMARY){} |
| 923 |
|
| 924 |
int best=Integer.MIN_VALUE; |
| 925 |
int scoreChanged=0; |
| 926 |
|
| 927 |
for(int i=0; i<r.sites.size(); i++){ |
| 928 |
SiteScore ss=r.sites.get(i); |
| 929 |
|
| 930 |
if(verbose){System.err.println("**************** best="+best+", scoreChanged="+scoreChanged+"\nconsidering ss "+ss);} |
| 931 |
|
| 932 |
if(i>0){ |
| 933 |
if(best>=ss.slowScore && !PRINT_SECONDARY_ALIGNMENTS){ |
| 934 |
if(verbose){System.err.println("break triggered by low score: ");} |
| 935 |
break; |
| 936 |
} |
| 937 |
} |
| 938 |
final int oldSlowScore=ss.slowScore, oldScore=ss.score; |
| 939 |
if(ss.match==null || (i==0 && !USE_SS_MATCH_FOR_PRIMARY)){ |
| 940 |
genMatchStringForSite(r.numericID, ss, basesP, basesM, maxImperfectSwScore, maxSwScore, r.mate, PRINT_SECONDARY_ALIGNMENTS); |
| 941 |
if(setSSScore){ss.setScore(ss.slowScore);} |
| 942 |
} |
| 943 |
if(i>0 && ss.match==null && !r.paired()){ |
| 944 |
if(verbose){System.err.println("Removed site "+ss);} |
| 945 |
r.sites.remove(i); |
| 946 |
}else{ |
| 947 |
if(oldScore!=ss.score || oldSlowScore!=ss.slowScore){scoreChanged++;} |
| 948 |
best=Tools.max(ss.slowScore, best); |
| 949 |
} |
| 950 |
|
| 951 |
if(verbose){System.err.println("**************** best="+best+", scoreChanged="+scoreChanged+"\nconsidered ss "+ss);} |
| 952 |
} |
| 953 |
|
| 954 |
if(verbose){System.err.println("Finished basic match generation. best="+best+", scoreChanged="+scoreChanged+", AMBIGUOUS_RANDOM="+AMBIGUOUS_RANDOM+", ambiguous="+r.ambiguous());} |
| 955 |
|
| 956 |
boolean needsSorting=(scoreChanged>0 && !Read.CHECKORDER(r.sites)); |
| 957 |
if(verbose){ |
| 958 |
System.err.println("needsSorting="+needsSorting+", scoreChanged="+scoreChanged+", "+Read.CHECKORDER(r.sites)); |
| 959 |
|
| 960 |
} |
| 961 |
while(needsSorting){ |
| 962 |
needsSorting=false; |
| 963 |
final SiteScore top=r.topSite(); |
| 964 |
if(verbose){System.err.println("GMS 1");} |
| 965 |
Tools.mergeDuplicateSites(r.sites, false, false); |
| 966 |
Shared.sort(r.sites); |
| 967 |
for(int i=0; i<r.sites.size() && i<MAX_SITESCORES_TO_PRINT; i++){ |
| 968 |
if(verbose){System.err.println("GMS 2");} |
| 969 |
SiteScore ss=r.sites.get(i); |
| 970 |
if(ss.match==null){ |
| 971 |
if(verbose){System.err.println("GMS 3");} |
| 972 |
genMatchStringForSite(r.numericID, ss, basesP, basesM, maxImperfectSwScore, maxSwScore, r.mate, PRINT_SECONDARY_ALIGNMENTS); |
| 973 |
if(setSSScore){ss.setScore(ss.slowScore);} |
| 974 |
if(i>0 && ss.match==null){r.sites.remove(i);} |
| 975 |
else{needsSorting=true;} |
| 976 |
i--; |
| 977 |
} |
| 978 |
if(i>0 || !PRINT_SECONDARY_ALIGNMENTS){ |
| 979 |
if(verbose){System.err.println("GMS 4");} |
| 980 |
break; |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
if(r.paired() && r.topSite()!=top){ |
| 985 |
r.setPaired(false); |
| 986 |
r.mate.setPaired(false); |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
final SiteScore ss=r.topSite(); |
| 991 |
assert(ss==r.topSite()); |
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
r.start=ss.start(); |
| 996 |
r.stop=ss.stop(); |
| 997 |
r.chrom=ss.chrom; |
| 998 |
r.setStrand(ss.strand); |
| 999 |
r.match=ss.match; |
| 1000 |
r.gaps=ss.gaps; |
| 1001 |
r.mapScore=ss.slowScore; |
| 1002 |
r.setPerfect(ss.perfect()); |
| 1003 |
r.setRescued(ss.rescued()); |
| 1004 |
|
| 1005 |
assert(checkTopSite(r)) : r; |
| 1006 |
assert(Read.CHECKSITES(r, basesM)) : "\n\n"+ss.mappedLength()+", "+ss.mappedLength()+"\n\n"+r+"\n\n"+r.mate+"\n\n"+r.toFastq()+"\n\n"+r.mate.toFastq()+"\n\n"; |
| 1007 |
|
| 1008 |
|
| 1009 |
} |
| 1010 |
|
| 1011 |
|
| 1012 |
protected final int genMatchStringForSite(final long id, final SiteScore ss, final byte[] basesP, final byte[] basesM, |
| 1013 |
final int maxImperfectSwScore, final int maxSwScore, final Read mate, final boolean secondary){ |
| 1014 |
final byte[] bases=ss.plus() ? basesP : basesM; |
| 1015 |
assert(Read.CHECKSITE(ss, bases, id)); |
| 1016 |
assert(msa!=null); |
| 1017 |
|
| 1018 |
|
| 1019 |
final int minMsaLimit; |
| 1020 |
{ |
| 1021 |
final float mult=(PAIRED ? MINIMUM_ALIGNMENT_SCORE_RATIO_PAIRED : MINIMUM_ALIGNMENT_SCORE_RATIO)*(secondary ? SECONDARY_SITE_SCORE_RATIO : 1f); |
| 1022 |
minMsaLimit=-1+(int)(mult*maxSwScore); |
| 1023 |
} |
| 1024 |
|
| 1025 |
if(GEN_MATCH_FAST){ |
| 1026 |
|
| 1027 |
assert(!(SLOW_ALIGN || AbstractIndex.USE_EXTENDED_SCORE) || AbstractIndex.GENERATE_BASE_SCORES_FROM_QUALITY || |
| 1028 |
(ss.slowScore==maxSwScore) == ss.perfect()) : |
| 1029 |
bases.length+", "+ss.toText()+", "+maxSwScore+", "+ss.slowScore+", "+ss.perfect()+", "+ss.semiperfect(); |
| 1030 |
|
| 1031 |
|
| 1032 |
if((SLOW_ALIGN || AbstractIndex.USE_EXTENDED_SCORE) && ss.perfect()){ |
| 1033 |
assert(ss.stop()-ss.start()==(bases.length-1)); |
| 1034 |
ss.match=makePerfectMatchString(bases.length); |
| 1035 |
assert(ss.isPerfect(bases)) : id+", "+ss; |
| 1036 |
}else{ |
| 1037 |
int oldScore=ss.slowScore; |
| 1038 |
assert(ss.gaps==null || ss.gaps[0]==ss.start() && ss.gaps[ss.gaps.length-1]==ss.stop()) : "\nrid="+id+"; ss="+ss+"\n"+new String(basesP)+"\n"; |
| 1039 |
int padding=(ss.perfect || ss.semiperfect ? 0 : Tools.max(SLOW_ALIGN_PADDING, 6)); |
| 1040 |
|
| 1041 |
if(verbose){System.err.println("Attempting to realign read:\n"+id+", "+ss+"\npadding="+padding+"\nrescued="+ss.rescued());} |
| 1042 |
|
| 1043 |
TranslateColorspaceRead.realign_new(ss, bases, msa, padding, 1, minMsaLimit, MAX_INDEL<1, false, id); |
| 1044 |
ss.gaps=GapTools.fixGaps(ss.start(), ss.stop(), ss.gaps, Shared.MINGAP); |
| 1045 |
|
| 1046 |
if(verbose){System.err.println("Realigned read:\n"+id+", "+ss+"\npadding="+padding+"\nrescued="+ss.rescued()+"\nreflen="+(ss.stop()-ss.start()+1));} |
| 1047 |
assert(Read.CHECKSITE(ss, bases, id)); |
| 1048 |
|
| 1049 |
int leftPaddingNeeded=ss.leftPaddingNeeded(4, 5), rightPaddingNeeded=ss.rightPaddingNeeded(4, 5); |
| 1050 |
if(ss.slowScore<oldScore || leftPaddingNeeded>0 || rightPaddingNeeded>0){ |
| 1051 |
if(verbose){System.err.println("---- A ----");} |
| 1052 |
if(verbose){ |
| 1053 |
System.err.print("Read "+id+": "+ss.start()+","+ss.stop()+": "+oldScore+">"+ss.slowScore); |
| 1054 |
} |
| 1055 |
|
| 1056 |
int extra=(MAX_INDEL>0 ? 80 : 20)+SLOW_ALIGN_PADDING; |
| 1057 |
int expectedLen=GapTools.calcGrefLen(ss.start(), ss.stop(), ss.gaps); |
| 1058 |
int remaining=(msa.maxColumns-expectedLen-2); |
| 1059 |
extra=Tools.max(0, Tools.min(remaining/2, extra)); |
| 1060 |
TranslateColorspaceRead.realign_new(ss, bases, msa, extra, 2, minMsaLimit, false, true, id); |
| 1061 |
ss.gaps=GapTools.fixGaps(ss.start(), ss.stop(), ss.gaps, Shared.MINGAP); |
| 1062 |
assert(Read.CHECKSITE(ss, bases, id)); |
| 1063 |
|
| 1064 |
if(verbose){ |
| 1065 |
System.err.println("\n-> "+ss.start()+","+ss.stop()+","+ss.slowScore+ |
| 1066 |
"\t(extra = "+extra+")"); |
| 1067 |
} |
| 1068 |
} |
| 1069 |
if(verbose){System.err.println("---- B ----");} |
| 1070 |
assert(Read.CHECKSITE(ss, bases, id)); |
| 1071 |
|
| 1072 |
if(verbose){ |
| 1073 |
System.err.println("---- D3 ----"); |
| 1074 |
System.err.println(ss); |
| 1075 |
System.err.println("Checking perfect status: ss.perfect="+ss.perfect()+", ss.semi="+ss.semiperfect()+ |
| 1076 |
", maxSwScore="+maxSwScore+", ss.slowScore="+ss.slowScore); |
| 1077 |
} |
| 1078 |
ss.setPerfectFlag(maxSwScore, bases); |
| 1079 |
if(verbose){ |
| 1080 |
System.err.println("---- E ----"); |
| 1081 |
System.err.println("Checking perfect status: ss.perfect="+ss.perfect()+", ss.semi="+ss.semiperfect()+ |
| 1082 |
", maxSwScore="+maxSwScore+", ss.slowScore="+ss.slowScore); |
| 1083 |
} |
| 1084 |
|
| 1085 |
assert(Read.CHECKSITE(ss, bases, id)); |
| 1086 |
} |
| 1087 |
}else{ |
| 1088 |
if(verbose){System.err.println("---- F ----");} |
| 1089 |
ChromosomeArray cha=Data.getChromosome(ss.chrom); |
| 1090 |
|
| 1091 |
if(ss.perfect()){ |
| 1092 |
ss.match=makePerfectMatchString(bases.length); |
| 1093 |
}else{ |
| 1094 |
assert(false) : "TODO: This does not take strand into account"; |
| 1095 |
if(ss.slowScore>=maxImperfectSwScore){ |
| 1096 |
|
| 1097 |
} |
| 1098 |
|
| 1099 |
if(msa!=null){ |
| 1100 |
assert(false) : "0 is not good here; try a non-indel match string."; |
| 1101 |
int[] max=msa.fillLimited(bases, cha.array, ss.start(), ss.stop(), 0, ss.gaps); |
| 1102 |
|
| 1103 |
ss.match=msa.traceback(bases, cha.array, ss.start(), ss.stop(), max[0], max[1], max[2], ss.gaps!=null); |
| 1104 |
} |
| 1105 |
} |
| 1106 |
} |
| 1107 |
if(verbose){System.err.println("---- G ----");} |
| 1108 |
ss.clipTipIndels(bases, basesM, 4, 10, msa); |
| 1109 |
|
| 1110 |
assert(Read.CHECKSITE(ss, bases, id)); |
| 1111 |
return ss.slowScore; |
| 1112 |
} |
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
final void findTipDeletions(final Read r, final byte[] basesP, final byte[] basesM, final int maxSwScore, final int maxImperfectScore){ |
| 1120 |
|
| 1121 |
boolean findRight=r.quality==null || (r.minQualityLastNBases(TIP_DELETION_MAX_TIPLEN)>=TIP_DELETION_MIN_QUALITY && |
| 1122 |
r.avgQualityLastNBases(TIP_DELETION_MAX_TIPLEN)>=TIP_DELETION_AVG_QUALITY); |
| 1123 |
boolean findLeft=r.quality==null || (r.minQualityFirstNBases(TIP_DELETION_MAX_TIPLEN)>=TIP_DELETION_MIN_QUALITY && |
| 1124 |
r.avgQualityFirstNBases(TIP_DELETION_MAX_TIPLEN)>=TIP_DELETION_AVG_QUALITY); |
| 1125 |
if(!findRight && !findLeft){ |
| 1126 |
|
| 1127 |
return; |
| 1128 |
} |
| 1129 |
|
| 1130 |
|
| 1131 |
for(SiteScore ss : r.sites){ |
| 1132 |
final byte[] bases=(ss.strand==Shared.PLUS ? basesP : basesM); |
| 1133 |
if(!ss.semiperfect && ss.slowScore<maxImperfectScore){ |
| 1134 |
boolean changed=findTipDeletions(ss, bases, maxImperfectScore, findRight, findLeft); |
| 1135 |
if(changed){ |
| 1136 |
ss.match=null; |
| 1137 |
ss.setSlowScore(msa.scoreNoIndels(bases, ss.chrom, ss.start())); |
| 1138 |
assert(!ss.perfect); |
| 1139 |
if(ss.slowScore==maxSwScore){ |
| 1140 |
ss.setStop(ss.start()+bases.length-1); |
| 1141 |
ss.perfect=ss.semiperfect=true; |
| 1142 |
}else{ |
| 1143 |
ss.perfect=false; |
| 1144 |
ss.setPerfect(bases, true); |
| 1145 |
} |
| 1146 |
} |
| 1147 |
} |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
final boolean findTipDeletions(SiteScore ss, final byte[] bases, final int maxImperfectScore, boolean lookRight, boolean lookLeft){ |
| 1152 |
if(ss.slowScore>=maxImperfectScore ){return false;} |
| 1153 |
assert(lookRight || lookLeft); |
| 1154 |
assert(TIP_DELETION_MAX_TIPLEN>2); |
| 1155 |
if(bases.length<=2*TIP_DELETION_MAX_TIPLEN){return false;} |
| 1156 |
assert(TIP_DELETION_MAX_TIPLEN<bases.length); |
| 1157 |
assert(TIP_DELETION_SEARCH_RANGE>0); |
| 1158 |
|
| 1159 |
int maxSearch=TIP_DELETION_SEARCH_RANGE; |
| 1160 |
maxSearch=Tools.min(maxSearch, ALIGN_COLUMNS_ABSTRACT-(SLOW_RESCUE_PADDING+8+Tools.max(bases.length, ss.stop()-ss.start()))); |
| 1161 |
if(maxSearch<1){return false;} |
| 1162 |
|
| 1163 |
boolean changed=false; |
| 1164 |
|
| 1165 |
if(lookRight){ |
| 1166 |
int x=findTipDeletionsRight(bases, ss.chrom, ss.stop(), maxSearch, TIP_DELETION_MAX_TIPLEN); |
| 1167 |
if(x>0){ |
| 1168 |
assert(x+ss.stop()-ss.start()<ALIGN_COLUMNS_ABSTRACT); |
| 1169 |
ss.setStop(ss.stop()+x); |
| 1170 |
changed=true; |
| 1171 |
maxSearch=Tools.min(maxSearch, ALIGN_COLUMNS_ABSTRACT-(SLOW_RESCUE_PADDING+8+Tools.max(bases.length, ss.stop()-ss.start()))); |
| 1172 |
if(maxSearch<1){return changed;} |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
if(lookLeft){ |
| 1177 |
int y=findTipDeletionsLeft(bases, ss.chrom, ss.start(), maxSearch, TIP_DELETION_MAX_TIPLEN); |
| 1178 |
if(y>0){ |
| 1179 |
assert(y+ss.stop()-ss.start()<ALIGN_COLUMNS_ABSTRACT); |
| 1180 |
ss.setStart(ss.start()-y); |
| 1181 |
changed=true; |
| 1182 |
} |
| 1183 |
} |
| 1184 |
return changed; |
| 1185 |
} |
| 1186 |
|
| 1187 |
|
| 1188 |
final void rescue(Read anchor, Read loose, byte[] basesP, byte[] basesM, int searchDist){ |
| 1189 |
|
| 1190 |
if(mappedRetained2>1000 && numMated*20L<mappedRetained2){return;} |
| 1191 |
if(searchDist>MAX_RESCUE_DIST){return;} |
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
if(anchor.sites==null || anchor.sites.isEmpty()){return;} |
| 1196 |
if(loose.sites==null){ |
| 1197 |
loose.sites=new ArrayList<SiteScore>(anchor.sites.size()); |
| 1198 |
} |
| 1199 |
|
| 1200 |
final int maxLooseSwScore=msa.maxQuality(basesP.length); |
| 1201 |
final int maxAnchorSwScore=msa.maxQuality(anchor.length()); |
| 1202 |
final int maxImperfectScore=msa.maxImperfectScore(basesP.length); |
| 1203 |
|
| 1204 |
final int bestLooseScore=loose.sites.isEmpty() ? 0 : loose.topSite().slowScore; |
| 1205 |
final int bestAnchorScore=anchor.topSite().slowScore; |
| 1206 |
|
| 1207 |
if(bestLooseScore==maxLooseSwScore && bestAnchorScore==maxAnchorSwScore |
| 1208 |
&& anchor.topSite().pairedScore>0){return;} |
| 1209 |
|
| 1210 |
int rescueScoreLimit=(int)(0.95f*bestAnchorScore); |
| 1211 |
|
| 1212 |
int retainScoreLimit=Tools.max((int)(0.68f*bestLooseScore), (int)(0.4f*maxLooseSwScore)); |
| 1213 |
int retainScoreLimit2=Tools.max((int)(0.95f*bestLooseScore), (int)(0.55f*maxLooseSwScore)); |
| 1214 |
final int maxMismatches=(PERFECTMODE || SEMIPERFECTMODE) ? 0 : |
| 1215 |
(bestLooseScore>maxImperfectScore) ? 5 : Tools.min(MAX_RESCUE_MISMATCHES, (int)(0.60f*basesP.length-1)); |
| 1216 |
assert(PERFECTMODE || SEMIPERFECTMODE || maxMismatches>1 || loose.length()<16) : loose; |
| 1217 |
|
| 1218 |
final boolean findTipDeletions=FIND_TIP_DELETIONS && bestLooseScore<maxImperfectScore; |
| 1219 |
|
| 1220 |
|
| 1221 |
final boolean findRight=findTipDeletions && (loose.quality==null || (loose.minQualityLastNBases(TIP_DELETION_MAX_TIPLEN)>=TIP_DELETION_MIN_QUALITY |
| 1222 |
&& loose.avgQualityLastNBases(TIP_DELETION_MAX_TIPLEN)>=TIP_DELETION_AVG_QUALITY)); |
| 1223 |
final boolean findLeft=findTipDeletions && (loose.quality==null || (loose.minQualityFirstNBases(TIP_DELETION_MAX_TIPLEN)>=TIP_DELETION_MIN_QUALITY |
| 1224 |
&& loose.avgQualityFirstNBases(TIP_DELETION_MAX_TIPLEN)>=TIP_DELETION_AVG_QUALITY)); |
| 1225 |
|
| 1226 |
|
| 1227 |
for(SiteScore ssa : anchor.sites){ |
| 1228 |
if(ssa.slowScore<rescueScoreLimit){break;} |
| 1229 |
if(ssa.pairedScore==0 && !ssa.rescued){ |
| 1230 |
|
| 1231 |
int searchIntoAnchor=ssa.stop()-ssa.start()-1+(anchor.length()*11/16); |
| 1232 |
int loc; |
| 1233 |
int idealStart; |
| 1234 |
byte[] bases; |
| 1235 |
byte strand=(SAME_STRAND_PAIRS ? ssa.strand : (byte)(ssa.strand^1)); |
| 1236 |
boolean searchRight=(SAME_STRAND_PAIRS ? strand==Shared.PLUS : strand==Shared.MINUS); |
| 1237 |
assert(strand==0 || strand==1); |
| 1238 |
|
| 1239 |
if(SAME_STRAND_PAIRS){ |
| 1240 |
if(ssa.strand==Shared.MINUS){ |
| 1241 |
bases=basesM; |
| 1242 |
loc=ssa.start()+searchIntoAnchor; |
| 1243 |
idealStart=ssa.start()-AVERAGE_PAIR_DIST; |
| 1244 |
}else{ |
| 1245 |
bases=basesP; |
| 1246 |
loc=ssa.stop()-searchIntoAnchor; |
| 1247 |
idealStart=ssa.stop()+AVERAGE_PAIR_DIST; |
| 1248 |
} |
| 1249 |
}else{ |
| 1250 |
if(ssa.strand==Shared.PLUS){ |
| 1251 |
bases=basesM; |
| 1252 |
loc=ssa.stop()-searchIntoAnchor; |
| 1253 |
idealStart=ssa.stop()+AVERAGE_PAIR_DIST; |
| 1254 |
}else{ |
| 1255 |
bases=basesP; |
| 1256 |
loc=ssa.start()+searchIntoAnchor; |
| 1257 |
idealStart=ssa.start()-AVERAGE_PAIR_DIST; |
| 1258 |
} |
| 1259 |
} |
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
SiteScore ss=quickRescue(bases, ssa.chrom, strand, loc, searchDist+searchIntoAnchor, searchRight, |
| 1264 |
idealStart, maxMismatches, POINTS_MATCH, POINTS_MATCH2); |
| 1265 |
|
| 1266 |
if(ss!=null && ss.isInBounds()){ |
| 1267 |
int mismatches=ss.slowScore; |
| 1268 |
ss.setSlowScore(0); |
| 1269 |
if(mismatches<=maxMismatches){ |
| 1270 |
slowRescue(bases, ss, maxLooseSwScore, maxImperfectScore, findRight, findLeft); |
| 1271 |
if(ss.score>retainScoreLimit && ss.isInBounds()){ |
| 1272 |
if(ss.score>retainScoreLimit2){ |
| 1273 |
ss.setPairedScore(Tools.max(ss.pairedScore, ss.slowScore+ssa.slowScore/4)); |
| 1274 |
ssa.setPairedScore(Tools.max(ssa.pairedScore, ssa.slowScore+ss.slowScore/4)); |
| 1275 |
assert(ss.pairedScore>0); |
| 1276 |
assert(ssa.pairedScore>0); |
| 1277 |
} |
| 1278 |
loose.sites.add(ss); |
| 1279 |
} |
| 1280 |
} |
| 1281 |
} |
| 1282 |
}else{ |
| 1283 |
assert(ssa.pairedScore>0); |
| 1284 |
assert(ssa.pairedScore>ssa.quickScore || ssa.pairedScore>ssa.slowScore) : ssa.toText(); |
| 1285 |
} |
| 1286 |
} |
| 1287 |
} |
| 1288 |
|
| 1289 |
|
| 1290 |
final void slowRescue(final byte[] bases, SiteScore ss, final int maxScore, final int maxImperfectScore, |
| 1291 |
boolean findTipDeletionsRight, boolean findTipDeletionsLeft){ |
| 1292 |
|
| 1293 |
int swscoreNoIndel=msa.scoreNoIndels(bases, ss.chrom, ss.start()); |
| 1294 |
final int oldStart=ss.start(); |
| 1295 |
|
| 1296 |
if(swscoreNoIndel<maxImperfectScore && MAX_INDEL>0){ |
| 1297 |
ss.setSlowScore(swscoreNoIndel); |
| 1298 |
if(findTipDeletionsRight || findTipDeletionsLeft){ |
| 1299 |
boolean changed=findTipDeletions(ss, bases, maxImperfectScore, findTipDeletionsRight, findTipDeletionsLeft); |
| 1300 |
if(changed){ |
| 1301 |
ss.match=null; |
| 1302 |
swscoreNoIndel=msa.scoreNoIndels(bases, ss.chrom, ss.start()); |
| 1303 |
} |
| 1304 |
} |
| 1305 |
|
| 1306 |
final int minMsaLimit=-CLEARZONE1e+(int)(MINIMUM_ALIGNMENT_SCORE_RATIO_PAIRED*maxScore); |
| 1307 |
|
| 1308 |
final int minscore=Tools.max(swscoreNoIndel, minMsaLimit); |
| 1309 |
final int[] swscoreArray=msa.fillAndScoreLimited(bases, ss.chrom, ss.start(), ss.stop(), SLOW_RESCUE_PADDING, minscore, ss.gaps); |
| 1310 |
|
| 1311 |
if(swscoreArray!=null){ |
| 1312 |
ss.setSlowScore(swscoreArray[0]); |
| 1313 |
ss.setScore(ss.slowScore); |
| 1314 |
ss.setStart(swscoreArray[1]); |
| 1315 |
ss.setStop(swscoreArray[2]); |
| 1316 |
|
| 1317 |
if(verbose){System.err.println("ss="+ss);} |
| 1318 |
if(QUICK_MATCH_STRINGS && swscoreArray!=null && swscoreArray.length==6 && swscoreArray[0]>=minscore && (PRINT_SECONDARY_ALIGNMENTS || USE_SS_MATCH_FOR_PRIMARY)){ |
| 1319 |
assert(swscoreArray.length==6) : swscoreArray.length; |
| 1320 |
assert(swscoreArray[0]>=minscore) : "\n"+Arrays.toString(swscoreArray)+"\n"+minscore; |
| 1321 |
ss.match=msa.traceback(bases, Data.getChromosome(ss.chrom).array, ss.start()-SLOW_RESCUE_PADDING, ss.stop()+SLOW_RESCUE_PADDING, |
| 1322 |
swscoreArray[3], swscoreArray[4], swscoreArray[5], ss.gaps!=null); |
| 1323 |
if(ss.match!=null){ |
| 1324 |
ss.setLimits(swscoreArray[1], swscoreArray[2]); |
| 1325 |
if(verbose){System.err.println("After quick match: ss="+ss+"\nbases="+new String(bases)+"\nref= "+Data.getChromosome(ss.chrom).getString(ss.start, ss.stop));} |
| 1326 |
assert(ss.lengthsAgree()); |
| 1327 |
ss.fixXY(bases, true, msa); |
| 1328 |
ss.clipTipIndels(bases, 4, 10, msa); |
| 1329 |
assert(ss.lengthsAgree()); |
| 1330 |
if(verbose){System.err.println("After clipping: ss="+ss);} |
| 1331 |
} |
| 1332 |
}else{ss.match=null;} |
| 1333 |
|
| 1334 |
}else{ |
| 1335 |
ss.setSlowScore(swscoreNoIndel); |
| 1336 |
ss.setScore(ss.slowScore); |
| 1337 |
ss.setStart(oldStart); |
| 1338 |
ss.setStop(ss.start()+bases.length-1); |
| 1339 |
} |
| 1340 |
}else{ |
| 1341 |
ss.setSlowScore(swscoreNoIndel); |
| 1342 |
ss.setScore(ss.slowScore); |
| 1343 |
ss.setStop(ss.start()+bases.length-1); |
| 1344 |
} |
| 1345 |
ss.setPairedScore(ss.score+1); |
| 1346 |
assert(ss.slowScore<=maxScore); |
| 1347 |
ss.perfect=(ss.slowScore==maxScore); |
| 1348 |
if(ss.perfect){ss.semiperfect=true;} |
| 1349 |
else{ss.setPerfect(bases);} |
| 1350 |
} |
| 1351 |
|
| 1352 |
|
| 1353 |
protected static final void capSiteList(Read r, int cap, boolean printSecondary){ |
| 1354 |
if(r==null || r.sites==null || cap<0){return;} |
| 1355 |
if(cap==0){r.sites=null;} |
| 1356 |
else{ |
| 1357 |
for(int i=r.sites.size()-1; i>=cap; i--){r.sites.remove(i);} |
| 1358 |
} |
| 1359 |
if(!printSecondary || r.numSites()<2){return;} |
| 1360 |
int max=r.topSite().slowScore; |
| 1361 |
int min=Tools.min(max-500, (int)(max*SECONDARY_SITE_SCORE_RATIO)); |
| 1362 |
|
| 1363 |
if(r.ambiguous()){ |
| 1364 |
min=Tools.min(min, r.sites.get(1).slowScore); |
| 1365 |
} |
| 1366 |
for(int i=r.sites.size()-1; i>0; i--){ |
| 1367 |
if(r.sites.get(i).slowScore<min){ |
| 1368 |
r.sites.remove(i); |
| 1369 |
} |
| 1370 |
} |
| 1371 |
|
| 1372 |
|
| 1373 |
} |
| 1374 |
|
| 1375 |
protected final static int removeDuplicateBestSites(Read r){ |
| 1376 |
int x=0; |
| 1377 |
if(r.numSites()<2){return 0;} |
| 1378 |
|
| 1379 |
|
| 1380 |
final SiteScore ss1=r.topSite(); |
| 1381 |
for(int i=r.sites.size()-1; i>0; i--){ |
| 1382 |
SiteScore ss2=r.sites.get(i); |
| 1383 |
if(ss1.chrom==ss2.chrom && ss1.strand==ss2.strand && ss1.start()==ss2.start() && ss1.stop()==ss2.stop()){ |
| 1384 |
if(!Shared.anomaly){ |
| 1385 |
|
| 1386 |
|
| 1387 |
System.err.println("Ignoring anomalous duplicate site for rid="+r.numericID); |
| 1388 |
Shared.anomaly=true; |
| 1389 |
|
| 1390 |
} |
| 1391 |
r.sites.remove(i); |
| 1392 |
x++; |
| 1393 |
}else{break;} |
| 1394 |
} |
| 1395 |
return x; |
| 1396 |
} |
| 1397 |
|
| 1398 |
protected final static void removeUnmapped(ArrayList<Read> list){ |
| 1399 |
for(int i=0; i<list.size(); i++){ |
| 1400 |
Read r=list.get(i); |
| 1401 |
if(r.numSites()==0){ |
| 1402 |
if(r.mate==null || r.mate.numSites()==0){ |
| 1403 |
list.set(i, null); |
| 1404 |
} |
| 1405 |
} |
| 1406 |
} |
| 1407 |
} |
| 1408 |
|
| 1409 |
protected final static void removeBlacklisted(ArrayList<Read> list){ |
| 1410 |
for(int i=0; i<list.size(); i++){ |
| 1411 |
Read r=list.get(i); |
| 1412 |
if(Blacklist.inBlacklist(r)){ |
| 1413 |
list.set(i, null); |
| 1414 |
} |
| 1415 |
} |
| 1416 |
} |
| 1417 |
|
| 1418 |
protected final static void removeMapped(ArrayList<Read> list){ |
| 1419 |
for(int i=0; i<list.size(); i++){ |
| 1420 |
Read r=list.get(i); |
| 1421 |
if(r.numSites()==0){ |
| 1422 |
if(r.mate==null || r.mate.numSites()==0){ |
| 1423 |
list.set(i, null); |
| 1424 |
} |
| 1425 |
} |
| 1426 |
} |
| 1427 |
} |
| 1428 |
|
| 1429 |
|
| 1430 |
public abstract int trimList(ArrayList<SiteScore> list, boolean retainPaired, int maxScore, boolean specialCasePerfect, int minSitesToRetain, int maxSitesToRetain); |
| 1431 |
|
| 1432 |
public final static int trimListAdvanced(ArrayList<SiteScore> list, boolean retainPaired, boolean retainSemiperfect, int maxScore, boolean specialCasePerfect, |
| 1433 |
int minSitesToRetain, int maxSitesToRetain, boolean indexUsesExtendedScore, float thresh){ |
| 1434 |
if(list==null || list.size()==0){return -99999;} |
| 1435 |
if(list.size()==1){return list.get(0).score;} |
| 1436 |
|
| 1437 |
final int highestScore; |
| 1438 |
if(indexUsesExtendedScore){ |
| 1439 |
|
| 1440 |
highestScore=Tools.trimSiteList(list, .6f, retainPaired, true, minSitesToRetain, maxSitesToRetain); |
| 1441 |
if(highestScore==maxScore && specialCasePerfect){ |
| 1442 |
Tools.trimSiteList(list, .94f, retainPaired, true, minSitesToRetain, maxSitesToRetain); |
| 1443 |
if(list.size()>8){Tools.trimSiteList(list, .99f, retainPaired, true, minSitesToRetain, maxSitesToRetain);} |
| 1444 |
return highestScore; |
| 1445 |
} |
| 1446 |
|
| 1447 |
}else{ |
| 1448 |
highestScore=Tools.trimSiteList(list, .4f, retainPaired, true, minSitesToRetain, maxSitesToRetain); |
| 1449 |
thresh=thresh*0.5f; |
| 1450 |
} |
| 1451 |
|
| 1452 |
int lim, lastScore=list.get(0).score; |
| 1453 |
long area=lastScore; |
| 1454 |
for(lim=1; lim<list.size(); lim++){ |
| 1455 |
SiteScore ss=list.get(lim); |
| 1456 |
lastScore=ss.score; |
| 1457 |
area=area+lastScore; |
| 1458 |
if(lastScore<0 || (lastScore/(float)area)<thresh){break;} |
| 1459 |
} |
| 1460 |
lim=Tools.max(minSitesToRetain, lim); |
| 1461 |
Tools.trimSitesBelowCutoff(list, lastScore, retainPaired, true, minSitesToRetain, maxSitesToRetain); |
| 1462 |
|
| 1463 |
return highestScore; |
| 1464 |
} |
| 1465 |
|
| 1466 |
|
| 1467 |
public abstract void scoreSlow(final ArrayList<SiteScore> list, final byte[] basesP, final byte[] basesM, |
| 1468 |
final int maxSwScore, final int maxImperfectSwScore); |
| 1469 |
|
| 1470 |
|
| 1471 |
public final static boolean processAmbiguous(ArrayList<SiteScore> list, boolean primary, boolean removeAmbiguous, int clearzone, boolean save_xy){ |
| 1472 |
if(!save_xy){return true;} |
| 1473 |
assert(false) : "TODO: Needs to be redone with contig names."; |
| 1474 |
|
| 1475 |
assert(list.size()>1); |
| 1476 |
boolean ambiguous=true; |
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
assert(list.size()>=1); |
| 1513 |
|
| 1514 |
if(ambiguous){ |
| 1515 |
assert(list.size()>1); |
| 1516 |
if(removeAmbiguous){ |
| 1517 |
list.clear(); |
| 1518 |
} |
| 1519 |
} |
| 1520 |
|
| 1521 |
return ambiguous; |
| 1522 |
} |
| 1523 |
|
| 1524 |
int calcTrimmed(Read r){ |
| 1525 |
assert(UNTRIM && (TRIM_LEFT || TRIM_RIGHT)); |
| 1526 |
if(r==null || r.getObjectClass()!=TrimRead.class){return 0;} |
| 1527 |
TrimRead tr=(TrimRead) r.fetchTrimRead(); |
| 1528 |
return tr.trimmed(); |
| 1529 |
} |
| 1530 |
|
| 1531 |
public void calcStatistics1(final Read r, final int maxSwScore, final int maxPossibleQuickScore){ |
| 1532 |
final Read r2=r.mate; |
| 1533 |
final int len1=r.length(); |
| 1534 |
final int len2=(r2==null ? 0 : r.length()); |
| 1535 |
|
| 1536 |
if(OUTPUT_PAIRED_ONLY && r.mate!=null && !r.paired() && (r.mapped() || r.mate.mapped())){r.clearPairMapping();} |
| 1537 |
if(r.ambiguous() && (AMBIGUOUS_TOSS || r.mapped())){ |
| 1538 |
ambiguousBestAlignment1++; |
| 1539 |
ambiguousBestAlignmentBases1+=len1; |
| 1540 |
} |
| 1541 |
|
| 1542 |
int trimmed=0; |
| 1543 |
if((TRIM_LEFT || TRIM_RIGHT) && UNTRIM){ |
| 1544 |
trimmed=calcTrimmed(r)+calcTrimmed(r.mate); |
| 1545 |
} |
| 1546 |
if((!r.mapped() || (r.ambiguous() && AMBIGUOUS_TOSS)) && (r2==null || !r2.mapped() || (r2.ambiguous() && AMBIGUOUS_TOSS))){ |
| 1547 |
bothUnmapped+=r.pairCount(); |
| 1548 |
bothUnmappedBases+=r.pairLength()+trimmed; |
| 1549 |
}else{ |
| 1550 |
eitherMapped+=r.pairCount(); |
| 1551 |
eitherMappedBases+=r.pairLength()+trimmed; |
| 1552 |
} |
| 1553 |
|
| 1554 |
int[] correctness=calcCorrectness(r, THRESH); |
| 1555 |
int correctGroup=correctness[0]; |
| 1556 |
int correctGroupSize=correctness[1]; |
| 1557 |
int numGroups=correctness[2]; |
| 1558 |
int elements=correctness[3]; |
| 1559 |
int correctScore=correctness[4]; |
| 1560 |
int topScore=correctness[5]; |
| 1561 |
int sizeOfTopGroup=correctness[6]; |
| 1562 |
int numCorrect=correctness[7]; |
| 1563 |
boolean firstElementCorrect=(correctness[8]==1); |
| 1564 |
boolean firstElementCorrectLoose=(correctness[9]==1); |
| 1565 |
boolean firstGroupCorrectLoose=(correctness[10]==1); |
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
assert(elements>0 == r.mapped()); |
| 1570 |
|
| 1571 |
if(elements>0){ |
| 1572 |
|
| 1573 |
if(r.match!=null){ |
| 1574 |
int[] errors=r.countErrors(SamLine.INTRON_LIMIT); |
| 1575 |
matchCountM1+=errors[0]; |
| 1576 |
matchCountS1+=errors[1]; |
| 1577 |
matchCountD1+=errors[2]; |
| 1578 |
matchCountI1+=errors[3]; |
| 1579 |
matchCountN1+=errors[4]; |
| 1580 |
|
| 1581 |
readCountS1+=(errors[1]>0 ? 1 : 0); |
| 1582 |
readCountD1+=(errors[2]>0 ? 1 : 0); |
| 1583 |
readCountI1+=(errors[3]>0 ? 1 : 0); |
| 1584 |
|
| 1585 |
readCountN1+=(errors[4]>0 ? 1 : 0); |
| 1586 |
readCountSplice1+=(errors[5]>0 ? 1 : 0); |
| 1587 |
readCountE1+=((errors[1]>0 || errors[2]>0 || errors[3]>0)? 1 : 0); |
| 1588 |
} |
| 1589 |
|
| 1590 |
|
| 1591 |
mappedRetained1++; |
| 1592 |
mappedRetainedBases1+=len1; |
| 1593 |
if(r.rescued()){ |
| 1594 |
if(r.strand()==Shared.PLUS){ |
| 1595 |
rescuedP1++; |
| 1596 |
}else{ |
| 1597 |
rescuedM1++; |
| 1598 |
} |
| 1599 |
} |
| 1600 |
if(r.paired()){ |
| 1601 |
numMated++; |
| 1602 |
numMatedBases+=(len1+len2); |
| 1603 |
int inner; |
| 1604 |
int outer; |
| 1605 |
if(r.start<=r2.start){ |
| 1606 |
inner=r2.start-r.stop; |
| 1607 |
outer=r2.stop-r.start; |
| 1608 |
}else{ |
| 1609 |
inner=r.start-r2.stop; |
| 1610 |
outer=r.stop-r2.start; |
| 1611 |
} |
| 1612 |
|
| 1613 |
inner=Tools.min(MAX_PAIR_DIST, inner); |
| 1614 |
inner=Tools.max(MIN_PAIR_DIST, inner); |
| 1615 |
innerLengthSum+=inner; |
| 1616 |
outerLengthSum+=outer; |
| 1617 |
insertSizeSum+=(inner+r.length()+r2.length()); |
| 1618 |
}else if(r2!=null && r2.mapped()){ |
| 1619 |
badPairs++; |
| 1620 |
badPairBases+=(len1+len2); |
| 1621 |
} |
| 1622 |
|
| 1623 |
if(r.perfect() || (maxSwScore>0 && r.topSite().slowScore==maxSwScore)){ |
| 1624 |
perfectMatch1++; |
| 1625 |
perfectMatchBases1+=len1; |
| 1626 |
}else if(SLOW_ALIGN){ |
| 1627 |
assert(r.topSite().slowScore<maxSwScore) : maxSwScore+"\t"+r.topSite().toText(); |
| 1628 |
} |
| 1629 |
|
| 1630 |
int foundSemi=0; |
| 1631 |
for(SiteScore ss : r.sites){ |
| 1632 |
if(ss.perfect){ |
| 1633 |
perfectHitCount1++; |
| 1634 |
assert(ss.semiperfect); |
| 1635 |
} |
| 1636 |
if(ss.semiperfect){ |
| 1637 |
semiPerfectHitCount1++; |
| 1638 |
foundSemi=1; |
| 1639 |
} |
| 1640 |
} |
| 1641 |
semiperfectMatch1+=foundSemi; |
| 1642 |
if(foundSemi>0){semiperfectMatchBases1+=len1;} |
| 1643 |
|
| 1644 |
if(firstElementCorrect){ |
| 1645 |
if(r.strand()==Shared.PLUS){firstSiteCorrectP1++;} |
| 1646 |
else{firstSiteCorrectM1++;} |
| 1647 |
if(r.paired()){firstSiteCorrectPaired1++;} |
| 1648 |
else{firstSiteCorrectSolo1++;} |
| 1649 |
if(r.rescued()){firstSiteCorrectRescued1++;} |
| 1650 |
}else{ |
| 1651 |
firstSiteIncorrect1++; |
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
} |
| 1656 |
|
| 1657 |
if(firstElementCorrectLoose){ |
| 1658 |
firstSiteCorrectLoose1++; |
| 1659 |
}else{ |
| 1660 |
firstSiteIncorrectLoose1++; |
| 1661 |
} |
| 1662 |
|
| 1663 |
siteSum1+=elements; |
| 1664 |
topSiteSum1+=sizeOfTopGroup; |
| 1665 |
|
| 1666 |
if(topScore==maxPossibleQuickScore){perfectHit1++;} |
| 1667 |
if(sizeOfTopGroup==1){uniqueHit1++;} |
| 1668 |
|
| 1669 |
if(correctGroup>0){ |
| 1670 |
|
| 1671 |
if(r.strand()==Shared.PLUS){truePositiveP1++;} |
| 1672 |
else{truePositiveM1++;} |
| 1673 |
totalCorrectSites1+=numCorrect; |
| 1674 |
|
| 1675 |
if(correctGroup==1){ |
| 1676 |
if(sizeOfTopGroup==1){ |
| 1677 |
correctUniqueHit1++; |
| 1678 |
}else{ |
| 1679 |
correctMultiHit1++; |
| 1680 |
} |
| 1681 |
}else{ |
| 1682 |
correctLowHit1++; |
| 1683 |
} |
| 1684 |
|
| 1685 |
}else{ |
| 1686 |
|
| 1687 |
falsePositive1++; |
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
} |
| 1692 |
}else if(maxPossibleQuickScore==-1){ |
| 1693 |
lowQualityReadsDiscarded1++; |
| 1694 |
lowQualityBasesDiscarded1+=len1; |
| 1695 |
r.setDiscarded(true); |
| 1696 |
}else{ |
| 1697 |
noHit1++; |
| 1698 |
} |
| 1699 |
} |
| 1700 |
|
| 1701 |
|
| 1702 |
public void calcStatistics2(final Read r, final int maxSwScore, final int maxPossibleQuickScore){ |
| 1703 |
final int len=r.length(); |
| 1704 |
|
| 1705 |
if(r.ambiguous() && (AMBIGUOUS_TOSS || r.mapped())){ |
| 1706 |
ambiguousBestAlignment2++; |
| 1707 |
ambiguousBestAlignmentBases2+=len; |
| 1708 |
} |
| 1709 |
|
| 1710 |
int[] correctness=calcCorrectness(r, THRESH); |
| 1711 |
int correctGroup=correctness[0]; |
| 1712 |
int correctGroupSize=correctness[1]; |
| 1713 |
int numGroups=correctness[2]; |
| 1714 |
int elements=correctness[3]; |
| 1715 |
int correctScore=correctness[4]; |
| 1716 |
int topScore=correctness[5]; |
| 1717 |
int sizeOfTopGroup=correctness[6]; |
| 1718 |
int numCorrect=correctness[7]; |
| 1719 |
boolean firstElementCorrect=(correctness[8]==1); |
| 1720 |
boolean firstElementCorrectLoose=(correctness[9]==1); |
| 1721 |
boolean firstGroupCorrectLoose=(correctness[10]==1); |
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
if(elements>0){ |
| 1726 |
|
| 1727 |
if(r.match!=null){ |
| 1728 |
int[] errors=r.countErrors(SamLine.INTRON_LIMIT); |
| 1729 |
matchCountM2+=errors[0]; |
| 1730 |
matchCountS2+=errors[1]; |
| 1731 |
matchCountD2+=errors[2]; |
| 1732 |
matchCountI2+=errors[3]; |
| 1733 |
matchCountN2+=errors[4]; |
| 1734 |
|
| 1735 |
readCountS2+=(errors[1]>0 ? 1 : 0); |
| 1736 |
readCountD2+=(errors[2]>0 ? 1 : 0); |
| 1737 |
readCountI2+=(errors[3]>0 ? 1 : 0); |
| 1738 |
readCountN2+=(errors[4]>0 ? 1 : 0); |
| 1739 |
readCountSplice2+=(errors[5]>0 ? 1 : 0); |
| 1740 |
readCountE2+=((errors[1]>0 || errors[2]>0 || errors[3]>0)? 1 : 0); |
| 1741 |
} |
| 1742 |
|
| 1743 |
mappedRetained2++; |
| 1744 |
mappedRetainedBases2+=len; |
| 1745 |
if(r.rescued()){ |
| 1746 |
if(r.strand()==Shared.PLUS){ |
| 1747 |
rescuedP2++; |
| 1748 |
}else{ |
| 1749 |
rescuedM2++; |
| 1750 |
} |
| 1751 |
} |
| 1752 |
|
| 1753 |
if(r.perfect() || (maxSwScore>0 && r.topSite().slowScore==maxSwScore)){ |
| 1754 |
perfectMatch2++; |
| 1755 |
perfectMatchBases2+=len; |
| 1756 |
}else if(SLOW_ALIGN){ |
| 1757 |
assert(r.topSite().slowScore<maxSwScore) : maxSwScore+"\t"+r.topSite().toText(); |
| 1758 |
} |
| 1759 |
|
| 1760 |
int foundSemi=0; |
| 1761 |
for(SiteScore ss : r.sites){ |
| 1762 |
if(ss.perfect){ |
| 1763 |
perfectHitCount2++; |
| 1764 |
assert(ss.semiperfect); |
| 1765 |
} |
| 1766 |
if(ss.semiperfect){ |
| 1767 |
semiPerfectHitCount2++; |
| 1768 |
foundSemi=1; |
| 1769 |
} |
| 1770 |
} |
| 1771 |
semiperfectMatch2+=foundSemi; |
| 1772 |
if(foundSemi>0){semiperfectMatchBases2+=len;} |
| 1773 |
|
| 1774 |
if(firstElementCorrect){ |
| 1775 |
if(r.strand()==Shared.PLUS){firstSiteCorrectP2++;} |
| 1776 |
else{firstSiteCorrectM2++;} |
| 1777 |
if(r.paired()){firstSiteCorrectPaired2++;} |
| 1778 |
else{firstSiteCorrectSolo2++;} |
| 1779 |
if(r.rescued()){firstSiteCorrectRescued2++;} |
| 1780 |
}else{ |
| 1781 |
firstSiteIncorrect2++; |
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
} |
| 1786 |
|
| 1787 |
if(firstElementCorrectLoose){ |
| 1788 |
firstSiteCorrectLoose2++; |
| 1789 |
}else{ |
| 1790 |
firstSiteIncorrectLoose2++; |
| 1791 |
} |
| 1792 |
|
| 1793 |
siteSum2+=elements; |
| 1794 |
topSiteSum2+=sizeOfTopGroup; |
| 1795 |
|
| 1796 |
if(topScore==maxPossibleQuickScore){perfectHit2++;} |
| 1797 |
if(sizeOfTopGroup==1){uniqueHit2++;} |
| 1798 |
|
| 1799 |
if(correctGroup>0){ |
| 1800 |
|
| 1801 |
if(r.strand()==Shared.PLUS){truePositiveP2++;} |
| 1802 |
else{truePositiveM2++;} |
| 1803 |
totalCorrectSites2+=numCorrect; |
| 1804 |
|
| 1805 |
if(correctGroup==1){ |
| 1806 |
if(sizeOfTopGroup==1){ |
| 1807 |
correctUniqueHit2++; |
| 1808 |
}else{ |
| 1809 |
correctMultiHit2++; |
| 1810 |
} |
| 1811 |
}else{ |
| 1812 |
correctLowHit2++; |
| 1813 |
} |
| 1814 |
|
| 1815 |
}else{ |
| 1816 |
|
| 1817 |
falsePositive2++; |
| 1818 |
|
| 1819 |
|
| 1820 |
|
| 1821 |
} |
| 1822 |
}else if(maxPossibleQuickScore==-1){ |
| 1823 |
lowQualityReadsDiscarded2++; |
| 1824 |
lowQualityBasesDiscarded2+=len; |
| 1825 |
}else{ |
| 1826 |
noHit2++; |
| 1827 |
} |
| 1828 |
} |
| 1829 |
|
| 1830 |
public abstract void processRead(Read r, final byte[] basesM); |
| 1831 |
|
| 1832 |
@Deprecated |
| 1833 |
protected final static boolean applyClearzone3_old(Read r, int CLEARZONE3, float INV_CLEARZONE3){ |
| 1834 |
|
| 1835 |
assert(!r.paired()); |
| 1836 |
if(!r.mapped() || r.ambiguous() || r.discarded() || r.numSites()<2){return false;} |
| 1837 |
|
| 1838 |
final int score1=r.topSite().slowScore; |
| 1839 |
final int score2=r.sites.get(1).slowScore; |
| 1840 |
final int score3=(r.sites.size()>2 ? r.sites.get(2).slowScore : -1); |
| 1841 |
int dif=score1-score2; |
| 1842 |
|
| 1843 |
assert(r.mapScore==score1) : r.mapScore+", "+r.topSite().toText(); |
| 1844 |
|
| 1845 |
assert(score1==r.mapScore); |
| 1846 |
assert(score1>=score2) : "\n"+r.topSite().toText()+"\t<\t"+r.sites.get(1).toText()+"\n"+r.toText(false)+"\n"; |
| 1847 |
if(dif>=CLEARZONE3){return false;} |
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
int dif2=(CLEARZONE3-dif); |
| 1852 |
|
| 1853 |
float f=dif2*INV_CLEARZONE3; |
| 1854 |
|
| 1855 |
int sub=(dif2+2*(int)(f*dif2)); |
| 1856 |
|
| 1857 |
if(score3!=-1){ |
| 1858 |
assert(score1>=score3); |
| 1859 |
dif=score1-score3; |
| 1860 |
assert(score1>=score3); |
| 1861 |
if(dif<CLEARZONE3){ |
| 1862 |
dif2=(CLEARZONE3-dif); |
| 1863 |
|
| 1864 |
f=dif2*INV_CLEARZONE3; |
| 1865 |
sub=sub+(dif2+2*(int)(f*dif2))/4; |
| 1866 |
|
| 1867 |
|
| 1868 |
} |
| 1869 |
} |
| 1870 |
|
| 1871 |
for(SiteScore ss : r.sites){ |
| 1872 |
ss.setSlowScore(ss.slowScore-sub); |
| 1873 |
ss.setScore(ss.score-sub); |
| 1874 |
} |
| 1875 |
r.mapScore-=sub; |
| 1876 |
return sub>0; |
| 1877 |
} |
| 1878 |
|
| 1879 |
|
| 1880 |
protected final boolean applyClearzone3(Read r, int CLEARZONE3, float INV_CLEARZONE3){ |
| 1881 |
|
| 1882 |
assert(!r.paired()); |
| 1883 |
final ArrayList<SiteScore> list=r.sites; |
| 1884 |
if(!r.mapped() || r.ambiguous() || r.discarded() || list==null || list.size()<2){return false;} |
| 1885 |
|
| 1886 |
final int score1=list.get(0).slowScore; |
| 1887 |
assert(r.mapScore==score1) : r.mapScore+", "+list.get(0).toText()+"\n"+r; |
| 1888 |
|
| 1889 |
float sub=0; |
| 1890 |
final int max=Tools.min(CZ3_MULTS.length, list.size()); |
| 1891 |
for(int i=1; i<max; i++){ |
| 1892 |
final SiteScore ss2=list.get(i); |
| 1893 |
assert(ss2!=null) : r; |
| 1894 |
if(ss2!=null){ |
| 1895 |
if(i>2 && ss2.slowScore<list.get(i-1).slowScore){break;} |
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
float f=calcCZ3_fraction(score1, ss2.slowScore, CLEARZONE3, INV_CLEARZONE3); |
| 1903 |
if(f<=0){break;} |
| 1904 |
sub+=(f*CZ3_MULTS[i]); |
| 1905 |
} |
| 1906 |
} |
| 1907 |
assert(sub>=0); |
| 1908 |
if(sub<=0){return false;} |
| 1909 |
|
| 1910 |
float sub2; |
| 1911 |
|
| 1912 |
float asymptote=4f+0.03f*r.length(); |
| 1913 |
sub=sub*1.8f; |
| 1914 |
sub2=CLEARZONE3*((asymptote*sub)/(sub+asymptote)); |
| 1915 |
|
| 1916 |
|
| 1917 |
int subi=(int)(sub2+0.5f); |
| 1918 |
if(subi>=r.mapScore-300){ |
| 1919 |
subi=r.mapScore-300; |
| 1920 |
} |
| 1921 |
if(subi<=0){return false;} |
| 1922 |
|
| 1923 |
for(SiteScore ss : list){ |
| 1924 |
ss.setSlowScore(ss.slowScore-subi); |
| 1925 |
ss.setScore(ss.score-subi); |
| 1926 |
} |
| 1927 |
r.mapScore-=subi; |
| 1928 |
assert(r.mapScore>200); |
| 1929 |
return true; |
| 1930 |
} |
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
|
| 1936 |
|
| 1937 |
|
| 1938 |
|
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
protected float calcCZ3_fraction(int score1, int score2, int CLEARZONE3, float INV_CLEARZONE3){ |
| 1954 |
|
| 1955 |
int dif=score1-score2; |
| 1956 |
if(dif>=CLEARZONE3){return 0;} |
| 1957 |
|
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
int dif2=(CLEARZONE3-dif); |
| 1962 |
|
| 1963 |
float f=dif2*INV_CLEARZONE3; |
| 1964 |
|
| 1965 |
float f2=f*f; |
| 1966 |
|
| 1967 |
|
| 1968 |
|
| 1969 |
return f+2f*f2+2f*f2*f; |
| 1970 |
} |
| 1971 |
|
| 1972 |
|
| 1973 |
public abstract int pairSiteScoresInitial(Read r, Read r2, boolean trim); |
| 1974 |
|
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
protected static void pairSiteScoresFinal(Read r, Read r2, boolean trim, boolean setScore, int MAX_PAIR_DIST, int AVERAGE_PAIR_DIST, |
| 1980 |
boolean SAME_STRAND_PAIRS, boolean REQUIRE_CORRECT_STRANDS_PAIRS, int maxTrimSitesToRetain){ |
| 1981 |
|
| 1982 |
if(r.sites!=null){ |
| 1983 |
for(SiteScore ss : r.sites){ss.setPairedScore(0);} |
| 1984 |
} |
| 1985 |
if(r2.sites!=null){ |
| 1986 |
for(SiteScore ss : r2.sites){ss.setPairedScore(0);} |
| 1987 |
} |
| 1988 |
|
| 1989 |
if(r.numSites()<1 || r2.numSites()<1){return;} |
| 1990 |
|
| 1991 |
SiteScore.PCOMP.sort(r.sites); |
| 1992 |
SiteScore.PCOMP.sort(r2.sites); |
| 1993 |
|
| 1994 |
int maxPairedScore1=-1; |
| 1995 |
int maxPairedScore2=-1; |
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
|
| 2007 |
|
| 2008 |
|
| 2009 |
|
| 2010 |
|
| 2011 |
final float mult1=Tools.min(1/2f, Tools.max(1/4f, (r.length()/(4f*r2.length())))); |
| 2012 |
final float mult2=Tools.min(1/2f, Tools.max(1/4f, (r2.length()/(4f*r.length())))); |
| 2013 |
|
| 2014 |
final int ilimit=r.sites.size()-1; |
| 2015 |
final int jlimit=r2.sites.size()-1; |
| 2016 |
|
| 2017 |
final int outerDistLimit=(Tools.max(r.length(), r2.length())*OUTER_DIST_MULT)/OUTER_DIST_DIV; |
| 2018 |
final int expectedFragLength=AVERAGE_PAIR_DIST+r.length()+r2.length(); |
| 2019 |
|
| 2020 |
if(verboseS){ |
| 2021 |
System.err.println("************************** PAIRING ********************************"); |
| 2022 |
System.err.println("outerDistLimit="+outerDistLimit+", MAX_PAIR_DIST="+MAX_PAIR_DIST); |
| 2023 |
} |
| 2024 |
|
| 2025 |
for(int i=0, j=0; i<=ilimit && j<=jlimit; i++){ |
| 2026 |
SiteScore ss1=r.sites.get(i); |
| 2027 |
SiteScore ss2=r2.sites.get(j); |
| 2028 |
|
| 2029 |
while(j<jlimit && (ss2.chrom<ss1.chrom || (ss2.chrom==ss1.chrom && ss1.start()-ss2.stop()>MAX_PAIR_DIST))){ |
| 2030 |
j++; |
| 2031 |
|
| 2032 |
ss2=r2.sites.get(j); |
| 2033 |
} |
| 2034 |
|
| 2035 |
for(int k=j; k<=jlimit; k++){ |
| 2036 |
ss2=r2.sites.get(k); |
| 2037 |
|
| 2038 |
if(verboseS){ |
| 2039 |
System.err.println("Considering sites:\n"+ss1+"\n"+ss2); |
| 2040 |
} |
| 2041 |
|
| 2042 |
if(ss2.chrom>ss1.chrom){break;} |
| 2043 |
|
| 2044 |
if(ss2.start()-ss1.stop()>MAX_PAIR_DIST){break;} |
| 2045 |
|
| 2046 |
final int innerdist; |
| 2047 |
final int outerdist; |
| 2048 |
|
| 2049 |
|
| 2050 |
|
| 2051 |
if(REQUIRE_CORRECT_STRANDS_PAIRS){ |
| 2052 |
if(ss1.strand!=ss2.strand){ |
| 2053 |
if(ss1.strand==Shared.PLUS){ |
| 2054 |
innerdist=ss2.start()-ss1.stop(); |
| 2055 |
outerdist=ss2.stop()-ss1.start(); |
| 2056 |
}else{ |
| 2057 |
innerdist=ss1.start()-ss2.stop(); |
| 2058 |
outerdist=ss1.stop()-ss2.start(); |
| 2059 |
} |
| 2060 |
}else{ |
| 2061 |
if(ss1.start()<=ss2.start()){ |
| 2062 |
innerdist=ss2.start()-ss1.stop(); |
| 2063 |
outerdist=ss2.stop()-ss1.start(); |
| 2064 |
}else{ |
| 2065 |
innerdist=ss1.start()-ss2.stop(); |
| 2066 |
outerdist=ss1.stop()-ss2.start(); |
| 2067 |
} |
| 2068 |
} |
| 2069 |
}else{ |
| 2070 |
if(ss1.start()<=ss2.start()){ |
| 2071 |
innerdist=ss2.start()-ss1.stop(); |
| 2072 |
outerdist=ss2.stop()-ss1.start(); |
| 2073 |
}else{ |
| 2074 |
innerdist=ss1.start()-ss2.stop(); |
| 2075 |
outerdist=ss1.stop()-ss2.start(); |
| 2076 |
} |
| 2077 |
} |
| 2078 |
|
| 2079 |
if(verboseS){ |
| 2080 |
System.err.println("innerdist="+innerdist+", outerdist="+outerdist); |
| 2081 |
} |
| 2082 |
|
| 2083 |
|
| 2084 |
|
| 2085 |
|
| 2086 |
|
| 2087 |
|
| 2088 |
|
| 2089 |
|
| 2090 |
assert(outerdist>=innerdist) : "outerdist<innerdist:\n"+innerdist+", "+outerdist+", "+ss1+", "+ss2; |
| 2091 |
|
| 2092 |
if(outerdist>=outerDistLimit && innerdist<=MAX_PAIR_DIST){ |
| 2093 |
|
| 2094 |
boolean strandOK=((ss1.strand==ss2.strand)==SAME_STRAND_PAIRS); |
| 2095 |
|
| 2096 |
|
| 2097 |
if(strandOK || !REQUIRE_CORRECT_STRANDS_PAIRS){ |
| 2098 |
|
| 2099 |
int deviation=absdif(AVERAGE_PAIR_DIST, innerdist); |
| 2100 |
|
| 2101 |
final int pairedScore1; |
| 2102 |
final int pairedScore2; |
| 2103 |
if(strandOK){ |
| 2104 |
|
| 2105 |
|
| 2106 |
|
| 2107 |
pairedScore1=ss1.score+1+ |
| 2108 |
Tools.max(1, (int)(ss2.score*mult1)-(((deviation)*ss2.score)/Tools.max(100,(10*expectedFragLength+100)))); |
| 2109 |
pairedScore2=ss2.score+1+ |
| 2110 |
Tools.max(1, (int)(ss1.score*mult2)-(((deviation)*ss1.score)/Tools.max(100,(10*expectedFragLength+100)))); |
| 2111 |
|
| 2112 |
|
| 2113 |
}else{ |
| 2114 |
pairedScore1=ss1.score+ss2.score/16; |
| 2115 |
pairedScore2=ss2.score+ss1.score/16; |
| 2116 |
} |
| 2117 |
|
| 2118 |
if(verboseS){ |
| 2119 |
System.err.println("strandOK="+strandOK+"\tpairedScore1="+pairedScore1+", pairedScore2="+pairedScore2); |
| 2120 |
System.err.println(" \tscore1="+ss1.score+", score2="+ss2.score); |
| 2121 |
} |
| 2122 |
|
| 2123 |
ss1.setPairedScore(Tools.max(ss1.pairedScore, pairedScore1)); |
| 2124 |
ss2.setPairedScore(Tools.max(ss2.pairedScore, pairedScore2)); |
| 2125 |
maxPairedScore1=Tools.max(ss1.score, maxPairedScore1); |
| 2126 |
maxPairedScore2=Tools.max(ss2.score, maxPairedScore2); |
| 2127 |
|
| 2128 |
} |
| 2129 |
}else{ |
| 2130 |
|
| 2131 |
} |
| 2132 |
} |
| 2133 |
|
| 2134 |
|
| 2135 |
} |
| 2136 |
|
| 2137 |
if(setScore){ |
| 2138 |
for(SiteScore ss : r.sites){ |
| 2139 |
if(ss.pairedScore>ss.score){ss.setScore(ss.pairedScore);} |
| 2140 |
else{assert(ss.pairedScore==0);} |
| 2141 |
} |
| 2142 |
for(SiteScore ss : r2.sites){ |
| 2143 |
if(ss.pairedScore>ss.score){ss.setScore(ss.pairedScore);} |
| 2144 |
else{assert(ss.pairedScore==0);} |
| 2145 |
} |
| 2146 |
} |
| 2147 |
|
| 2148 |
if(trim){ |
| 2149 |
|
| 2150 |
|
| 2151 |
float f=Tools.min(SECONDARY_SITE_SCORE_RATIO, 0.95f); |
| 2152 |
Tools.trimSitesBelowCutoff(r.sites, (int)(maxPairedScore1*f), false, true, 1, maxTrimSitesToRetain); |
| 2153 |
Tools.trimSitesBelowCutoff(r2.sites, (int)(maxPairedScore2*f), false, true, 1, maxTrimSitesToRetain); |
| 2154 |
} |
| 2155 |
} |
| 2156 |
|
| 2157 |
protected final static boolean canPair(SiteScore ss1, SiteScore ss2, int len1, int len2, |
| 2158 |
boolean REQUIRE_CORRECT_STRANDS_PAIRS, boolean SAME_STRAND_PAIRS, int MAX_PAIR_DIST){ |
| 2159 |
if(ss1.chrom!=ss2.chrom){return false;} |
| 2160 |
if(REQUIRE_CORRECT_STRANDS_PAIRS){ |
| 2161 |
boolean strandOK=((ss1.strand==ss2.strand)==SAME_STRAND_PAIRS); |
| 2162 |
if(!strandOK){return false;} |
| 2163 |
} |
| 2164 |
|
| 2165 |
|
| 2166 |
|
| 2167 |
|
| 2168 |
|
| 2169 |
|
| 2170 |
|
| 2171 |
|
| 2172 |
|
| 2173 |
|
| 2174 |
|
| 2175 |
|
| 2176 |
final int outerDistLimit=(Tools.max(len1, len2)*(OUTER_DIST_MULT))/OUTER_DIST_DIV; |
| 2177 |
int innerdist=0; |
| 2178 |
int outerdist=0; |
| 2179 |
|
| 2180 |
if(verboseS){ |
| 2181 |
System.err.println("canPair: outerDistLimit="+outerDistLimit); |
| 2182 |
} |
| 2183 |
|
| 2184 |
|
| 2185 |
|
| 2186 |
|
| 2187 |
|
| 2188 |
|
| 2189 |
|
| 2190 |
|
| 2191 |
|
| 2192 |
|
| 2193 |
|
| 2194 |
|
| 2195 |
if(REQUIRE_CORRECT_STRANDS_PAIRS){ |
| 2196 |
if(ss1.strand!=ss2.strand){ |
| 2197 |
if(ss1.strand==Shared.PLUS){ |
| 2198 |
innerdist=ss2.start()-ss1.stop(); |
| 2199 |
outerdist=ss2.stop()-ss1.start(); |
| 2200 |
}else{ |
| 2201 |
innerdist=ss1.start()-ss2.stop(); |
| 2202 |
outerdist=ss1.stop()-ss2.start(); |
| 2203 |
} |
| 2204 |
}else{ |
| 2205 |
if(ss1.start()<=ss2.start()){ |
| 2206 |
innerdist=ss2.start()-ss1.stop(); |
| 2207 |
outerdist=ss2.stop()-ss1.start(); |
| 2208 |
}else{ |
| 2209 |
innerdist=ss1.start()-ss2.stop(); |
| 2210 |
outerdist=ss1.stop()-ss2.start(); |
| 2211 |
} |
| 2212 |
} |
| 2213 |
}else{ |
| 2214 |
if(ss1.start()<=ss2.start()){ |
| 2215 |
innerdist=ss2.start()-ss1.stop(); |
| 2216 |
outerdist=ss2.stop()-ss1.start(); |
| 2217 |
}else{ |
| 2218 |
innerdist=ss1.start()-ss2.stop(); |
| 2219 |
outerdist=ss1.stop()-ss2.start(); |
| 2220 |
} |
| 2221 |
} |
| 2222 |
|
| 2223 |
return (outerdist>=outerDistLimit && innerdist<=MAX_PAIR_DIST); |
| 2224 |
} |
| 2225 |
|
| 2226 |
|
| 2227 |
|
| 2228 |
|
| 2229 |
|
| 2230 |
|
| 2231 |
|
| 2232 |
|
| 2233 |
|
| 2234 |
|
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
protected final static int findTipDeletionsRight(final byte[] bases, final int chrom, |
| 2239 |
int originalStop, int searchDist, int tiplen){ |
| 2240 |
ChromosomeArray cha=Data.getChromosome(chrom); |
| 2241 |
byte[] ref=cha.array; |
| 2242 |
if(originalStop<cha.minIndex+tiplen-1){return 0;} |
| 2243 |
|
| 2244 |
int minMismatches=tiplen; |
| 2245 |
int bestStart=originalStop; |
| 2246 |
|
| 2247 |
final int tipCoord=bases.length-1; |
| 2248 |
|
| 2249 |
int lastMismatch=0; |
| 2250 |
int originalMismatches=0; |
| 2251 |
int contig=0; |
| 2252 |
for(int i=0; i<tiplen && contig<5; i++){ |
| 2253 |
if(bases[tipCoord-i]!=ref[originalStop-i]){ |
| 2254 |
originalMismatches++; |
| 2255 |
lastMismatch=i; |
| 2256 |
contig=0; |
| 2257 |
}else{ |
| 2258 |
contig++; |
| 2259 |
} |
| 2260 |
} |
| 2261 |
|
| 2262 |
if(originalMismatches<3){return 0;} |
| 2263 |
minMismatches=originalMismatches; |
| 2264 |
tiplen=lastMismatch+1; |
| 2265 |
assert(tiplen>1); |
| 2266 |
if(tiplen<4){return 0;} |
| 2267 |
|
| 2268 |
|
| 2269 |
|
| 2270 |
searchDist=Tools.min(searchDist, 30*originalMismatches); |
| 2271 |
int lastIndexToStart=Tools.min(ref.length-1, originalStop+searchDist); |
| 2272 |
for(int start=originalStop+1; start<=lastIndexToStart && minMismatches>0; start++){ |
| 2273 |
|
| 2274 |
int mismatches=0; |
| 2275 |
for(int j=0; j<tiplen && mismatches<minMismatches; j++){ |
| 2276 |
if(bases[tipCoord-j]!=ref[start-j]){ |
| 2277 |
mismatches++; |
| 2278 |
} |
| 2279 |
} |
| 2280 |
|
| 2281 |
if(mismatches<minMismatches){ |
| 2282 |
bestStart=start; |
| 2283 |
minMismatches=mismatches; |
| 2284 |
} |
| 2285 |
} |
| 2286 |
|
| 2287 |
if(minMismatches>2 || originalMismatches-minMismatches<2){ |
| 2288 |
return 0; |
| 2289 |
} |
| 2290 |
|
| 2291 |
return bestStart-originalStop; |
| 2292 |
} |
| 2293 |
|
| 2294 |
|
| 2295 |
|
| 2296 |
|
| 2297 |
|
| 2298 |
protected final static int findTipDeletionsLeft(final byte[] bases, final int chrom, |
| 2299 |
final int originalStart, int searchDist, int tiplen){ |
| 2300 |
ChromosomeArray cha=Data.getChromosome(chrom); |
| 2301 |
byte[] ref=cha.array; |
| 2302 |
if(originalStart+tiplen>=ref.length){return 0;} |
| 2303 |
|
| 2304 |
if(cha.minIndex>=originalStart){return 0;} |
| 2305 |
|
| 2306 |
int minMismatches=tiplen; |
| 2307 |
int bestStart=originalStart; |
| 2308 |
|
| 2309 |
int lastMismatch=0; |
| 2310 |
int originalMismatches=0; |
| 2311 |
int contig=0; |
| 2312 |
for(int i=0; i<tiplen && contig<5; i++){ |
| 2313 |
if(bases[i]!=ref[originalStart+i]){ |
| 2314 |
originalMismatches++; |
| 2315 |
lastMismatch=i; |
| 2316 |
contig=0; |
| 2317 |
}else{ |
| 2318 |
contig++; |
| 2319 |
} |
| 2320 |
} |
| 2321 |
|
| 2322 |
if(originalMismatches<3){return 0;} |
| 2323 |
minMismatches=originalMismatches; |
| 2324 |
tiplen=lastMismatch+1; |
| 2325 |
assert(tiplen>1); |
| 2326 |
if(tiplen<4){return 0;} |
| 2327 |
|
| 2328 |
|
| 2329 |
|
| 2330 |
searchDist=Tools.min(searchDist, 16+16*originalMismatches+8*tiplen); |
| 2331 |
int lastIndexToStart=Tools.max(cha.minIndex, originalStart-searchDist); |
| 2332 |
for(int start=originalStart-1; start>=lastIndexToStart && minMismatches>0; start--){ |
| 2333 |
|
| 2334 |
int mismatches=0; |
| 2335 |
for(int j=0; j<tiplen && mismatches<minMismatches; j++){ |
| 2336 |
if(bases[j]!=ref[start+j]){ |
| 2337 |
mismatches++; |
| 2338 |
} |
| 2339 |
} |
| 2340 |
|
| 2341 |
if(mismatches<minMismatches){ |
| 2342 |
bestStart=start; |
| 2343 |
minMismatches=mismatches; |
| 2344 |
} |
| 2345 |
} |
| 2346 |
|
| 2347 |
if(minMismatches>2 || originalMismatches-minMismatches<2){ |
| 2348 |
return 0; |
| 2349 |
} |
| 2350 |
|
| 2351 |
return originalStart-bestStart; |
| 2352 |
} |
| 2353 |
|
| 2354 |
|
| 2355 |
|
| 2356 |
|
| 2357 |
|
| 2358 |
|
| 2359 |
|
| 2360 |
|
| 2361 |
|
| 2362 |
|
| 2363 |
public final SiteScore quickRescue(final byte[] bases, final int chrom, final byte strand, final int loc, final int searchDist, |
| 2364 |
final boolean searchRight, final int idealStart, final int maxAllowedMismatches, int POINTS_MATCH, int POINTS_MATCH2){ |
| 2365 |
if(bases==null || bases.length<10){return null;} |
| 2366 |
ChromosomeArray cha=Data.getChromosome(chrom); |
| 2367 |
byte[] ref=cha.array; |
| 2368 |
|
| 2369 |
int lowerBound, upperBound; |
| 2370 |
if(searchRight){ |
| 2371 |
lowerBound=Tools.max(cha.minIndex, loc); |
| 2372 |
upperBound=Tools.min(ref.length-bases.length, loc+searchDist); |
| 2373 |
}else{ |
| 2374 |
lowerBound=Tools.max(cha.minIndex, loc-searchDist); |
| 2375 |
upperBound=Tools.min(ref.length-bases.length, loc); |
| 2376 |
} |
| 2377 |
|
| 2378 |
|
| 2379 |
int minMismatches=maxAllowedMismatches+1; |
| 2380 |
|
| 2381 |
|
| 2382 |
int maxContigMatches=0; |
| 2383 |
int bestScore=0; |
| 2384 |
int bestStart=-1; |
| 2385 |
int bestAbsdif=Integer.MAX_VALUE; |
| 2386 |
|
| 2387 |
if(searchRight){ |
| 2388 |
for(int start=lowerBound; start<=upperBound; start++){ |
| 2389 |
int mismatches=0; |
| 2390 |
int contig=0; |
| 2391 |
int currentContig=0; |
| 2392 |
for(int j=0; j<bases.length && mismatches<=minMismatches; j++){ |
| 2393 |
final byte c=bases[j], r=ref[start+j]; |
| 2394 |
if(c!=r || c=='N'){ |
| 2395 |
mismatches++; |
| 2396 |
contig=Tools.max(contig, currentContig); |
| 2397 |
currentContig=0; |
| 2398 |
}else{ |
| 2399 |
currentContig++; |
| 2400 |
} |
| 2401 |
} |
| 2402 |
|
| 2403 |
int score=(bases.length-mismatches)+contig; |
| 2404 |
int absdif=absdif(start, idealStart); |
| 2405 |
if(mismatches<=minMismatches && (score>bestScore || (score==bestScore && absdif<bestAbsdif))){ |
| 2406 |
bestStart=start; |
| 2407 |
minMismatches=mismatches; |
| 2408 |
maxContigMatches=contig; |
| 2409 |
bestScore=score; |
| 2410 |
bestAbsdif=absdif; |
| 2411 |
if(mismatches==0){upperBound=Tools.min(upperBound, idealStart+absdif);} |
| 2412 |
|
| 2413 |
|
| 2414 |
|
| 2415 |
} |
| 2416 |
} |
| 2417 |
}else{ |
| 2418 |
for(int start=upperBound; start>=lowerBound; start--){ |
| 2419 |
int mismatches=0; |
| 2420 |
int contig=0; |
| 2421 |
int currentContig=0; |
| 2422 |
for(int j=0; j<bases.length && mismatches<=minMismatches; j++){ |
| 2423 |
final byte c=bases[j], r=ref[start+j]; |
| 2424 |
if(c!=r || c=='N'){ |
| 2425 |
mismatches++; |
| 2426 |
contig=Tools.max(contig, currentContig); |
| 2427 |
currentContig=0; |
| 2428 |
}else{ |
| 2429 |
currentContig++; |
| 2430 |
} |
| 2431 |
} |
| 2432 |
|
| 2433 |
int score=(bases.length-mismatches)+contig; |
| 2434 |
int absdif=absdif(start, idealStart); |
| 2435 |
if(mismatches<=minMismatches && (score>bestScore || (score==bestScore && absdif<bestAbsdif))){ |
| 2436 |
bestStart=start; |
| 2437 |
minMismatches=mismatches; |
| 2438 |
maxContigMatches=contig; |
| 2439 |
bestScore=score; |
| 2440 |
bestAbsdif=absdif; |
| 2441 |
if(mismatches==0){lowerBound=Tools.max(lowerBound, idealStart-absdif);} |
| 2442 |
|
| 2443 |
|
| 2444 |
|
| 2445 |
} |
| 2446 |
} |
| 2447 |
} |
| 2448 |
|
| 2449 |
if(bestStart<0){return null;} |
| 2450 |
|
| 2451 |
|
| 2452 |
final int scoreOut; |
| 2453 |
if(USE_AFFINE_SCORE){ |
| 2454 |
scoreOut=POINTS_MATCH+(POINTS_MATCH2*(bases.length-1-minMismatches)); |
| 2455 |
}else{ |
| 2456 |
scoreOut=maxContigMatches+(BASE_HIT_SCORE*(bases.length-minMismatches)); |
| 2457 |
} |
| 2458 |
|
| 2459 |
SiteScore ss=new SiteScore(chrom, strand, bestStart, bestStart+bases.length-1, 0, scoreOut); |
| 2460 |
ss.setPerfect(bases); |
| 2461 |
ss.rescued=true; |
| 2462 |
ss.setSlowScore(minMismatches); |
| 2463 |
return ss; |
| 2464 |
} |
| 2465 |
|
| 2466 |
|
| 2467 |
|
| 2468 |
protected final static int[] quickerRescue(final byte[] bases, final int chrom, int loc, final int searchDist){ |
| 2469 |
ChromosomeArray cha=Data.getChromosome(chrom); |
| 2470 |
byte[] ref=cha.array; |
| 2471 |
if(loc<cha.minIndex){loc=cha.minIndex;} |
| 2472 |
|
| 2473 |
int lastIndexToStart=loc+searchDist-1; |
| 2474 |
final int limit=Tools.min(lastIndexToStart, ref.length-bases.length)+1; |
| 2475 |
|
| 2476 |
int minMismatches=bases.length; |
| 2477 |
int bestStart=-1; |
| 2478 |
for(int start=loc; start<limit && minMismatches>0; start++){ |
| 2479 |
int mismatches=0; |
| 2480 |
for(int j=0; j<bases.length && mismatches<minMismatches; j++){ |
| 2481 |
if(bases[j]!=ref[start+j]){ |
| 2482 |
mismatches++; |
| 2483 |
} |
| 2484 |
} |
| 2485 |
if(mismatches<minMismatches){ |
| 2486 |
bestStart=start; |
| 2487 |
minMismatches=mismatches; |
| 2488 |
} |
| 2489 |
} |
| 2490 |
|
| 2491 |
return new int[] {bestStart, bestStart+bases.length-1, minMismatches}; |
| 2492 |
} |
| 2493 |
|
| 2494 |
|
| 2495 |
public abstract void processReadPair(final Read r, final byte[] basesM1, final byte[] basesM2); |
| 2496 |
|
| 2497 |
|
| 2498 |
|
| 2499 |
|
| 2500 |
|
| 2501 |
|
| 2502 |
|
| 2503 |
|
| 2504 |
protected final static int removeOutOfBounds(Read r, boolean DONT_OUTPUT_UNMAPPED_READS, boolean SAM_OUT, int EXPECTED_LEN_LIMIT){ |
| 2505 |
|
| 2506 |
ArrayList<SiteScore> ssl=r.sites; |
| 2507 |
if(ssl==null){return 0;} |
| 2508 |
int initial=ssl.size(); |
| 2509 |
for(int i=0; i<ssl.size(); i++){ |
| 2510 |
SiteScore ss=ssl.get(i); |
| 2511 |
|
| 2512 |
ChromosomeArray cha=Data.getChromosome(ss.chrom); |
| 2513 |
int max=cha.maxIndex; |
| 2514 |
if(ss.start()<0 || ss.stop()>max){ |
| 2515 |
ssl.remove(i); |
| 2516 |
i--; |
| 2517 |
ss=null; |
| 2518 |
}else if(SAM_OUT){ |
| 2519 |
if(!Data.isSingleScaffold(ss.chrom, ss.start(), ss.stop())){ |
| 2520 |
|
| 2521 |
ssl.remove(i); |
| 2522 |
i--; |
| 2523 |
ss=null; |
| 2524 |
} |
| 2525 |
} |
| 2526 |
if(ss!=null){ |
| 2527 |
int expectedLen=GapTools.calcGrefLen(ss); |
| 2528 |
if(expectedLen>=EXPECTED_LEN_LIMIT){ |
| 2529 |
|
| 2530 |
ss.setStop(ss.start()+Tools.min(r.length()+40, EXPECTED_LEN_LIMIT)); |
| 2531 |
if(ss.gaps!=null){GapTools.fixGaps(ss);} |
| 2532 |
} |
| 2533 |
} |
| 2534 |
} |
| 2535 |
|
| 2536 |
|
| 2537 |
|
| 2538 |
|
| 2539 |
return initial-ssl.size(); |
| 2540 |
} |
| 2541 |
|
| 2542 |
protected static final int forbidSelfMapping(ArrayList<SiteScore> ssl, SiteScore original){ |
| 2543 |
|
| 2544 |
if(ssl==null || ssl.isEmpty() || original==null){return 0;} |
| 2545 |
int removed=0; |
| 2546 |
for(int i=0; i<ssl.size(); i++){ |
| 2547 |
SiteScore ss=ssl.get(i); |
| 2548 |
if(ss.overlaps(original, true)){ |
| 2549 |
ssl.set(i, null); |
| 2550 |
removed++; |
| 2551 |
} |
| 2552 |
} |
| 2553 |
if(removed>0){Tools.condenseStrict(ssl);} |
| 2554 |
return removed; |
| 2555 |
} |
| 2556 |
|
| 2557 |
|
| 2558 |
|
| 2559 |
public static int calcTipScorePenalty(final Read r, final int maxScore, final int tiplen){ |
| 2560 |
if(!r.mapped() || r.match==null || r.length()<2*tiplen){return 0;} |
| 2561 |
|
| 2562 |
int points=0; |
| 2563 |
final byte[] match=r.match; |
| 2564 |
final byte[] bases=r.bases; |
| 2565 |
final int last=r.length()-1; |
| 2566 |
byte prev='m'; |
| 2567 |
for(int i=0, cpos=0; cpos<=tiplen; i++){ |
| 2568 |
byte b=match[i]; |
| 2569 |
if(b=='m'){ |
| 2570 |
cpos++; |
| 2571 |
}else if(b=='D'){ |
| 2572 |
if(prev!='D'){points+=2*(tiplen+2-cpos);} |
| 2573 |
}else if(b=='N' || b=='C'){ |
| 2574 |
points+=(tiplen+2-cpos); |
| 2575 |
cpos++; |
| 2576 |
}else{ |
| 2577 |
if(Tools.isDigit(b)){ |
| 2578 |
r.toLongMatchString(false); |
| 2579 |
return calcTipScorePenalty(r, maxScore, tiplen); |
| 2580 |
} |
| 2581 |
assert(b=='I' || b=='S') : ((char)b)+"\n"+new String(match)+"\n"+new String(bases)+"\n"; |
| 2582 |
points+=2*(tiplen+2-cpos); |
| 2583 |
cpos++; |
| 2584 |
} |
| 2585 |
prev=b; |
| 2586 |
} |
| 2587 |
|
| 2588 |
prev='m'; |
| 2589 |
for(int i=match.length-1, cpos=0; cpos<=tiplen; i--){ |
| 2590 |
byte b=match[i]; |
| 2591 |
if(b=='m'){ |
| 2592 |
cpos++; |
| 2593 |
}else if(b=='D'){ |
| 2594 |
if(prev!='D'){points+=2*(tiplen+2-cpos);} |
| 2595 |
}else if(b=='N' || b=='C'){ |
| 2596 |
points+=(tiplen+2-cpos); |
| 2597 |
cpos++; |
| 2598 |
}else{ |
| 2599 |
assert(b=='I' || b=='S'); |
| 2600 |
points+=2*(tiplen+2-cpos); |
| 2601 |
cpos++; |
| 2602 |
} |
| 2603 |
prev=b; |
| 2604 |
} |
| 2605 |
|
| 2606 |
byte b=bases[0]; |
| 2607 |
|
| 2608 |
if(b!='N' && b==bases[1]){ |
| 2609 |
for(int i=2; i<=tiplen && bases[i]==b; i++){points++;} |
| 2610 |
} |
| 2611 |
|
| 2612 |
|
| 2613 |
b=bases[last]; |
| 2614 |
if(b!='N' && b==bases[last-1]){ |
| 2615 |
for(int i=last-2; i>=(last-tiplen) && bases[i]==b; i--){points++;} |
| 2616 |
} |
| 2617 |
|
| 2618 |
|
| 2619 |
|
| 2620 |
|
| 2621 |
|
| 2622 |
|
| 2623 |
if(points<1){return 0;} |
| 2624 |
|
| 2625 |
|
| 2626 |
float asymptote=80; |
| 2627 |
float f=((asymptote*points)/(points+asymptote)); |
| 2628 |
|
| 2629 |
int penalty=(int)(f*.0022f*maxScore); |
| 2630 |
int maxPenalty=r.mapScore-maxScore/10; |
| 2631 |
if(maxPenalty<=0){return 0;} |
| 2632 |
return Tools.min(penalty, maxPenalty); |
| 2633 |
|
| 2634 |
|
| 2635 |
|
| 2636 |
|
| 2637 |
|
| 2638 |
|
| 2639 |
|
| 2640 |
|
| 2641 |
|
| 2642 |
|
| 2643 |
|
| 2644 |
|
| 2645 |
|
| 2646 |
|
| 2647 |
|
| 2648 |
|
| 2649 |
|
| 2650 |
|
| 2651 |
|
| 2652 |
|
| 2653 |
|
| 2654 |
|
| 2655 |
|
| 2656 |
|
| 2657 |
|
| 2658 |
} |
| 2659 |
|
| 2660 |
|
| 2661 |
public static void applyScorePenalty(Read r, int penalty){ |
| 2662 |
if(penalty>0){ |
| 2663 |
r.mapScore-=penalty; |
| 2664 |
for(SiteScore ss : r.sites){ |
| 2665 |
ss.setSlowScore(ss.slowScore-penalty); |
| 2666 |
ss.setScore(ss.score-penalty); |
| 2667 |
} |
| 2668 |
} |
| 2669 |
} |
| 2670 |
|
| 2671 |
|
| 2672 |
|
| 2673 |
|
| 2674 |
|
| 2675 |
protected int[] calcCorrectness(Read r, int thresh){ |
| 2676 |
|
| 2677 |
ArrayList<SiteScore> ssl=r.sites; |
| 2678 |
|
| 2679 |
if(ssl==null || ssl.isEmpty()){ |
| 2680 |
return new int[] {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 2681 |
} |
| 2682 |
|
| 2683 |
SiteScore original=r.originalSite; |
| 2684 |
assert((original==null) != (r.synthetic())); |
| 2685 |
if(original==null){ |
| 2686 |
original=ssl.get(0); |
| 2687 |
} |
| 2688 |
|
| 2689 |
int group=0; |
| 2690 |
int correctGroup=-1; |
| 2691 |
int groupSize=0; |
| 2692 |
int correctGroupSize=-1; |
| 2693 |
int prevScore=Integer.MAX_VALUE; |
| 2694 |
int sizeOfTopGroup=0; |
| 2695 |
SiteScore correct=null; |
| 2696 |
|
| 2697 |
int firstElementCorrect=0; |
| 2698 |
int firstElementCorrectLoose=0; |
| 2699 |
int firstGroupCorrectLoose=0; |
| 2700 |
|
| 2701 |
int numCorrect=0; |
| 2702 |
|
| 2703 |
for(int i=0; i<ssl.size(); i++){ |
| 2704 |
SiteScore ss=ssl.get(i); |
| 2705 |
if(ss.score==ssl.get(0).score){sizeOfTopGroup++;} |
| 2706 |
|
| 2707 |
if(prevScore!=ss.score){ |
| 2708 |
assert(prevScore>ss.score || (AMBIGUOUS_RANDOM && r.ambiguous()) || r.mate!=null) : "prevScore="+prevScore+", score="+ss.score+ |
| 2709 |
", i="+i+", r="+r+"\n\nss"+i+" = "+ss+"\n\n"+(i==0 ? "" : "ss"+(i-1)+" = "+ssl.get(i-1)); |
| 2710 |
|
| 2711 |
if(correctGroup==group){ |
| 2712 |
correctGroupSize=groupSize; |
| 2713 |
} |
| 2714 |
|
| 2715 |
group++; |
| 2716 |
groupSize=0; |
| 2717 |
prevScore=ss.score; |
| 2718 |
} |
| 2719 |
groupSize++; |
| 2720 |
|
| 2721 |
|
| 2722 |
|
| 2723 |
boolean b=isCorrectHit(ss, original.chrom, original.strand, original.start(), original.stop(), thresh); |
| 2724 |
boolean b2=isCorrectHitLoose(ss, original.chrom, original.strand, original.start(), original.stop(), thresh+20); |
| 2725 |
|
| 2726 |
if(b){ |
| 2727 |
if(i==0){firstElementCorrect=1;} |
| 2728 |
numCorrect++; |
| 2729 |
if(correct==null){ |
| 2730 |
correct=ss; |
| 2731 |
correctGroup=group; |
| 2732 |
} |
| 2733 |
} |
| 2734 |
if(b2){ |
| 2735 |
if(i==0){firstElementCorrectLoose=1;} |
| 2736 |
if(group==0){firstGroupCorrectLoose=1;} |
| 2737 |
} |
| 2738 |
} |
| 2739 |
if(correctGroup==group){ |
| 2740 |
correctGroupSize=groupSize; |
| 2741 |
} |
| 2742 |
|
| 2743 |
assert(correctGroup!=0 && correctGroup<=group); |
| 2744 |
assert(group<=ssl.size()); |
| 2745 |
assert(sizeOfTopGroup>0 && sizeOfTopGroup<=ssl.size()); |
| 2746 |
assert((correctGroup>0) == (correctGroupSize>0)); |
| 2747 |
return new int[] {correctGroup, correctGroupSize, group, ssl.size(), |
| 2748 |
correct==null ? 0 : correct.score, ssl.get(0).score, sizeOfTopGroup, numCorrect, firstElementCorrect, |
| 2749 |
firstElementCorrectLoose, firstGroupCorrectLoose}; |
| 2750 |
} |
| 2751 |
|
| 2752 |
|
| 2753 |
public static final boolean isCorrectHit(SiteScore ss, int trueChrom, byte trueStrand, int trueStart, int trueStop, int thresh){ |
| 2754 |
|
| 2755 |
if(ss.chrom!=trueChrom || ss.strand!=trueStrand){return false;} |
| 2756 |
|
| 2757 |
assert(ss.stop()>ss.start()) : ss.toText()+", "+trueStart+", "+trueStop; |
| 2758 |
assert(trueStop>trueStart) : ss.toText()+", "+trueStart+", "+trueStop; |
| 2759 |
|
| 2760 |
return (absdif(ss.start(), trueStart)<=thresh && absdif(ss.stop(), trueStop)<=thresh); |
| 2761 |
|
| 2762 |
|
| 2763 |
|
| 2764 |
|
| 2765 |
|
| 2766 |
|
| 2767 |
|
| 2768 |
|
| 2769 |
|
| 2770 |
} |
| 2771 |
|
| 2772 |
|
| 2773 |
public static final boolean isCorrectHitLoose(SiteScore ss, int trueChrom, byte trueStrand, int trueStart, int trueStop, int thresh){ |
| 2774 |
|
| 2775 |
if(ss.chrom!=trueChrom || ss.strand!=trueStrand){return false;} |
| 2776 |
|
| 2777 |
assert(ss.stop()>ss.start()) : ss.toText()+", "+trueStart+", "+trueStop; |
| 2778 |
assert(trueStop>trueStart) : ss.toText()+", "+trueStart+", "+trueStop; |
| 2779 |
|
| 2780 |
return (absdif(ss.start(), trueStart)<=thresh || absdif(ss.stop(), trueStop)<=thresh); |
| 2781 |
|
| 2782 |
|
| 2783 |
|
| 2784 |
|
| 2785 |
|
| 2786 |
|
| 2787 |
|
| 2788 |
|
| 2789 |
} |
| 2790 |
|
| 2791 |
protected static final byte[] makePerfectMatchString(int len){ |
| 2792 |
byte[] r=new byte[len]; |
| 2793 |
Arrays.fill(r, (byte)'m'); |
| 2794 |
return r; |
| 2795 |
} |
| 2796 |
|
| 2797 |
protected static final int absdif(int a, int b){ |
| 2798 |
return a>b ? a-b : b-a; |
| 2799 |
} |
| 2800 |
|
| 2801 |
|
| 2802 |
public abstract int maxReadLength(); |
| 2803 |
|
| 2804 |
|
| 2805 |
protected static final boolean checkTopSite(Read r){ |
| 2806 |
if(!r.mapped()){return true;} |
| 2807 |
if(r.numSites()==0){return false;} |
| 2808 |
SiteScore ss=r.topSite(); |
| 2809 |
if(ss==null){return false;} |
| 2810 |
boolean b=(ss.start()==r.start) && (ss.stop()==r.stop) && (ss.strand==r.strand()) && (ss.chrom==r.chrom) && (ss.match==r.match); |
| 2811 |
assert(b) : "\nread="+r+"\nmate="+r.mate+"\nss="+ss+"\n"+(ss==null ? "ss is null" : |
| 2812 |
((ss.start()==r.start)+", "+(ss.stop()==r.stop)+", "+(ss.strand==r.strand())+", "+(ss.chrom==r.chrom)+", "+(ss.match==r.match))+"\nlist="+r.sites); |
| 2813 |
return b; |
| 2814 |
} |
| 2815 |
|
| 2816 |
|
| 2817 |
protected static final int removeLongIndels(ArrayList<SiteScore> list, int maxlen){ |
| 2818 |
if(list==null || list.size()<1){return 0;} |
| 2819 |
int removed=0; |
| 2820 |
for(int i=list.size()-1; i>=0; i--){ |
| 2821 |
SiteScore ss=list.get(i); |
| 2822 |
if(hasLongIndel(ss.match, maxlen)){ |
| 2823 |
list.remove(i); |
| 2824 |
removed++; |
| 2825 |
} |
| 2826 |
} |
| 2827 |
return removed; |
| 2828 |
} |
| 2829 |
|
| 2830 |
protected static final boolean hasLongIndel(byte[] match, int maxlen){ |
| 2831 |
if(match==null || match.length<maxlen){return false;} |
| 2832 |
byte prev='0'; |
| 2833 |
int len=0; |
| 2834 |
for(byte b : match){ |
| 2835 |
if(b=='D' || b=='I' || b=='X' || b=='Y'){ |
| 2836 |
if(b==prev){len++;} |
| 2837 |
else{len=1;} |
| 2838 |
if(len>maxlen){return true;} |
| 2839 |
}else{ |
| 2840 |
len=0; |
| 2841 |
} |
| 2842 |
prev=b; |
| 2843 |
} |
| 2844 |
return false; |
| 2845 |
} |
| 2846 |
|
| 2847 |
|
| 2848 |
final void processReadSplit(Read r, byte[] basesM, int minlen, int maxlen){ |
| 2849 |
assert(minlen>=KEYLEN && maxlen>=minlen) : KEYLEN+", "+maxlen+", "+minlen; |
| 2850 |
int len=r.length(); |
| 2851 |
if(len<=maxlen){ |
| 2852 |
processRead(r, basesM); |
| 2853 |
return; |
| 2854 |
} |
| 2855 |
ArrayList<Read> subreads=r.split(minlen, maxlen); |
| 2856 |
} |
| 2857 |
|
| 2858 |
public final synchronized boolean finished(){return finished;} |
| 2859 |
|
| 2860 |
public final synchronized boolean working(){return !finished;} |
| 2861 |
|
| 2862 |
final synchronized void finish(){ |
| 2863 |
assert(!finished); |
| 2864 |
finished=true; |
| 2865 |
notifyAll(); |
| 2866 |
} |
| 2867 |
|
| 2868 |
private boolean finished=false; |
| 2869 |
|
| 2870 |
private static final float[] CZ3_MULTS=new float[] {0f, 1f, .75f, 0.5f, 0.25f, 0.125f, 0.0625f}; |
| 2871 |
|
| 2872 |
|
| 2873 |
|
| 2874 |
|
| 2875 |
protected final ConcurrentReadInputStream cris; |
| 2876 |
|
| 2877 |
|
| 2878 |
|
| 2879 |
|
| 2880 |
protected final ConcurrentReadOutputStream outStream; |
| 2881 |
|
| 2882 |
protected final ConcurrentReadOutputStream outStreamMapped; |
| 2883 |
|
| 2884 |
protected final ConcurrentReadOutputStream outStreamUnmapped; |
| 2885 |
|
| 2886 |
protected final ConcurrentReadOutputStream outStreamBlack; |
| 2887 |
|
| 2888 |
|
| 2889 |
|
| 2890 |
|
| 2891 |
|
| 2892 |
public final String MSA_TYPE; |
| 2893 |
final MSA msa; |
| 2894 |
public final ReadStats readstats; |
| 2895 |
public final CoveragePileup pileup; |
| 2896 |
public final int POINTS_MATCH, POINTS_MATCH2; |
| 2897 |
public final int KEYLEN; |
| 2898 |
|
| 2899 |
protected final boolean PERFECTMODE; |
| 2900 |
protected final boolean SEMIPERFECTMODE; |
| 2901 |
protected final boolean FORBID_SELF_MAPPING; |
| 2902 |
protected final boolean RCOMP_MATE; |
| 2903 |
protected static boolean RCOMP=false; |
| 2904 |
|
| 2905 |
protected final boolean MAKE_MATCH_STRING; |
| 2906 |
|
| 2907 |
protected final boolean OUTPUT_MAPPED_ONLY; |
| 2908 |
protected final boolean DONT_OUTPUT_BLACKLISTED_READS; |
| 2909 |
protected final boolean PRINT_SECONDARY_ALIGNMENTS; |
| 2910 |
protected final boolean QUICK_MATCH_STRINGS; |
| 2911 |
protected final boolean USE_SS_MATCH_FOR_PRIMARY=true; |
| 2912 |
|
| 2913 |
protected final int MAX_SITESCORES_TO_PRINT; |
| 2914 |
|
| 2915 |
|
| 2916 |
|
| 2917 |
protected final float MINIMUM_ALIGNMENT_SCORE_RATIO; |
| 2918 |
protected final float MINIMUM_ALIGNMENT_SCORE_RATIO_PRE_RESCUE; |
| 2919 |
protected final float MINIMUM_ALIGNMENT_SCORE_RATIO_PAIRED; |
| 2920 |
|
| 2921 |
protected final float keyDensity; |
| 2922 |
protected final float maxKeyDensity; |
| 2923 |
protected final float minKeyDensity; |
| 2924 |
protected final int maxDesiredKeys; |
| 2925 |
|
| 2926 |
|
| 2927 |
|
| 2928 |
final int CLEARZONE1e; |
| 2929 |
|
| 2930 |
|
| 2931 |
|
| 2932 |
final int MIN_APPROX_HITS_TO_KEEP; |
| 2933 |
final boolean USE_EXTENDED_SCORE; |
| 2934 |
public static final boolean GENERATE_BASE_SCORES_FROM_QUALITY=AbstractIndex.GENERATE_BASE_SCORES_FROM_QUALITY; |
| 2935 |
final int BASE_HIT_SCORE; |
| 2936 |
final int BASE_KEY_HIT_SCORE; |
| 2937 |
final boolean USE_AFFINE_SCORE; |
| 2938 |
final int EXPECTED_LEN_LIMIT; |
| 2939 |
final int MAX_INDEL; |
| 2940 |
|
| 2941 |
final boolean TRIM_LIST; |
| 2942 |
final int TIP_DELETION_SEARCH_RANGE; |
| 2943 |
final boolean FIND_TIP_DELETIONS; |
| 2944 |
final int ALIGN_COLUMNS_ABSTRACT; |
| 2945 |
|
| 2946 |
|
| 2947 |
|
| 2948 |
|
| 2949 |
|
| 2950 |
protected final boolean SLOW_ALIGN; |
| 2951 |
|
| 2952 |
protected final boolean LOCAL_ALIGN; |
| 2953 |
|
| 2954 |
protected final boolean AMBIGUOUS_TOSS; |
| 2955 |
|
| 2956 |
protected final boolean AMBIGUOUS_RANDOM; |
| 2957 |
|
| 2958 |
protected final boolean AMBIGUOUS_ALL; |
| 2959 |
|
| 2960 |
protected final boolean TRIM_LEFT; |
| 2961 |
|
| 2962 |
protected final boolean TRIM_RIGHT; |
| 2963 |
|
| 2964 |
protected final boolean UNTRIM; |
| 2965 |
|
| 2966 |
protected final float TRIM_QUAL; |
| 2967 |
protected final float TRIM_ERROR_RATE; |
| 2968 |
|
| 2969 |
protected final int TRIM_MIN_LENGTH; |
| 2970 |
|
| 2971 |
protected final int THRESH; |
| 2972 |
|
| 2973 |
protected final int minChrom; |
| 2974 |
|
| 2975 |
protected final int maxChrom; |
| 2976 |
|
| 2977 |
protected final int KFILTER; |
| 2978 |
|
| 2979 |
protected final float IDFILTER; |
| 2980 |
|
| 2981 |
protected final boolean PROCESS_EDIT_FILTER; |
| 2982 |
|
| 2983 |
|
| 2984 |
protected final boolean RenameByInsert; |
| 2985 |
|
| 2986 |
|
| 2987 |
protected final boolean KILL_BAD_PAIRS; |
| 2988 |
|
| 2989 |
protected final boolean SAVE_AMBIGUOUS_XY; |
| 2990 |
|
| 2991 |
protected final boolean GEN_MATCH_FAST=true; |
| 2992 |
|
| 2993 |
|
| 2994 |
protected final int SLOW_ALIGN_PADDING; |
| 2995 |
|
| 2996 |
protected final int SLOW_RESCUE_PADDING; |
| 2997 |
|
| 2998 |
protected final boolean DO_RESCUE; |
| 2999 |
|
| 3000 |
protected final boolean STRICT_MAX_INDEL; |
| 3001 |
|
| 3002 |
protected final int BANDWIDTH; |
| 3003 |
|
| 3004 |
protected final boolean PAIRED; |
| 3005 |
protected final boolean REQUIRE_CORRECT_STRANDS_PAIRS; |
| 3006 |
protected final boolean SAME_STRAND_PAIRS; |
| 3007 |
|
| 3008 |
protected final BloomFilter bloomFilter; |
| 3009 |
|
| 3010 |
|
| 3011 |
|
| 3012 |
static int INITIAL_AVERAGE_PAIR_DIST=100; |
| 3013 |
protected int AVERAGE_PAIR_DIST; |
| 3014 |
protected float AVERAGE_PAIRING_RATE=0; |
| 3015 |
|
| 3016 |
|
| 3017 |
protected int EXTRA_PADDING=10; |
| 3018 |
|
| 3019 |
protected final boolean GENERATE_KEY_SCORES_FROM_QUALITY; |
| 3020 |
|
| 3021 |
|
| 3022 |
|
| 3023 |
protected static boolean PENALIZE_AMBIG=true; |
| 3024 |
protected static int SUBFILTER=-1; |
| 3025 |
protected static int DELFILTER=-1; |
| 3026 |
protected static int INSFILTER=-1; |
| 3027 |
protected static int INDELFILTER=-1; |
| 3028 |
protected static int DELLENFILTER=-1; |
| 3029 |
protected static int INSLENFILTER=-1; |
| 3030 |
protected static int EDITFILTER=-1; |
| 3031 |
protected static int NFILTER=-1; |
| 3032 |
|
| 3033 |
protected static boolean OUTPUT_SAM=false; |
| 3034 |
|
| 3035 |
protected static float SECONDARY_SITE_SCORE_RATIO=.95f; |
| 3036 |
protected static boolean PRINT_SECONDARY_ALIGNMENTS_ONLY_FOR_AMBIGUOUS_READS=false; |
| 3037 |
|
| 3038 |
protected static boolean CALC_STATISTICS=true; |
| 3039 |
protected static int MIN_PAIR_DIST=-160; |
| 3040 |
protected static int MAX_PAIR_DIST=32000; |
| 3041 |
protected static int MAX_RESCUE_DIST=1200; |
| 3042 |
protected static int MAX_RESCUE_MISMATCHES=32; |
| 3043 |
|
| 3044 |
protected static boolean DYNAMIC_INSERT_LENGTH=true; |
| 3045 |
|
| 3046 |
protected static final boolean DISCARD_MOSTLY_UNDEFINED_READS=true; |
| 3047 |
protected static float MIN_AVERAGE_QUALITY=0; |
| 3048 |
protected static int MIN_AVERAGE_QUALITY_BASES=0; |
| 3049 |
protected static boolean TIME_TAG=false; |
| 3050 |
protected static boolean CLEAR_ATTACHMENT=true; |
| 3051 |
|
| 3052 |
protected static final byte TIP_DELETION_MIN_QUALITY=6; |
| 3053 |
protected static final byte TIP_DELETION_AVG_QUALITY=14; |
| 3054 |
protected static final int TIP_DELETION_MAX_TIPLEN=8; |
| 3055 |
|
| 3056 |
protected static final int OUTER_DIST_MULT=14; |
| 3057 |
|
| 3058 |
protected static final int OUTER_DIST_DIV=32; |
| 3059 |
|
| 3060 |
protected static long SKIP_INITIAL=0; |
| 3061 |
|
| 3062 |
protected static boolean OUTPUT_PAIRED_ONLY=false; |
| 3063 |
|
| 3064 |
protected static int MAX_READ_LENGTH=0; |
| 3065 |
protected static int MIN_READ_LENGTH=0; |
| 3066 |
|
| 3067 |
protected static boolean USE_MODULO=false; |
| 3068 |
|
| 3069 |
protected static int MAX_TRIM_SITES_TO_RETAIN=800; |
| 3070 |
|
| 3071 |
|
| 3072 |
|
| 3073 |
|
| 3074 |
|
| 3075 |
public long totalNumCorrect1=0; |
| 3076 |
public long totalNumIncorrect1=0; |
| 3077 |
public long totalNumIncorrectPrior1=0; |
| 3078 |
public long totalNumCapturedAllCorrect1=0; |
| 3079 |
public long totalNumCapturedAllCorrectTop1=0; |
| 3080 |
public long totalNumCapturedAllCorrectOnly1=0; |
| 3081 |
|
| 3082 |
public long totalNumCorrect2=0; |
| 3083 |
public long totalNumIncorrect2=0; |
| 3084 |
public long totalNumIncorrectPrior2=0; |
| 3085 |
public long totalNumCapturedAllCorrect2=0; |
| 3086 |
public long totalNumCapturedAllCorrectTop2=0; |
| 3087 |
public long totalNumCapturedAllCorrectOnly2=0; |
| 3088 |
|
| 3089 |
|
| 3090 |
|
| 3091 |
public boolean verbose=false; |
| 3092 |
public static final boolean verboseS=false; |
| 3093 |
|
| 3094 |
public long readsUsed1=0; |
| 3095 |
public long readsUsed2=0; |
| 3096 |
public long basesUsed1=0; |
| 3097 |
public long basesUsed2=0; |
| 3098 |
public long readsIn1=0; |
| 3099 |
public long readsIn2=0; |
| 3100 |
public long basesIn1=0; |
| 3101 |
public long basesIn2=0; |
| 3102 |
public long readsPassedBloomFilter=0; |
| 3103 |
public long basesPassedBloomFilter=0; |
| 3104 |
public long numMated=0; |
| 3105 |
public long numMatedBases=0; |
| 3106 |
public long badPairs=0; |
| 3107 |
public long badPairBases=0; |
| 3108 |
public long innerLengthSum=0; |
| 3109 |
public long outerLengthSum=0; |
| 3110 |
public long insertSizeSum=0; |
| 3111 |
public long keysUsed=0; |
| 3112 |
public long syntheticReads=0; |
| 3113 |
public long bothUnmapped=0; |
| 3114 |
public long bothUnmappedBases=0; |
| 3115 |
public long eitherMapped=0; |
| 3116 |
public long eitherMappedBases=0; |
| 3117 |
|
| 3118 |
public long mapped1=0; |
| 3119 |
public long mappedRetained1=0; |
| 3120 |
public long mappedRetainedBases1=0; |
| 3121 |
public long rescuedP1=0; |
| 3122 |
public long rescuedM1=0; |
| 3123 |
public long truePositiveP1=0; |
| 3124 |
public long truePositiveM1=0; |
| 3125 |
public long falsePositive1=0; |
| 3126 |
public long totalCorrectSites1=0; |
| 3127 |
|
| 3128 |
public long firstSiteCorrectP1=0; |
| 3129 |
public long firstSiteCorrectM1=0; |
| 3130 |
public long firstSiteIncorrect1=0; |
| 3131 |
public long firstSiteCorrectLoose1=0; |
| 3132 |
public long firstSiteIncorrectLoose1=0; |
| 3133 |
public long firstSiteCorrectPaired1=0; |
| 3134 |
public long firstSiteCorrectSolo1=0; |
| 3135 |
public long firstSiteCorrectRescued1=0; |
| 3136 |
|
| 3137 |
public long matchCountS1=0; |
| 3138 |
public long matchCountI1=0; |
| 3139 |
public long matchCountD1=0; |
| 3140 |
public long matchCountM1=0; |
| 3141 |
public long matchCountN1=0; |
| 3142 |
|
| 3143 |
public long readCountE1=0; |
| 3144 |
public long readCountS1=0; |
| 3145 |
public long readCountI1=0; |
| 3146 |
public long readCountD1=0; |
| 3147 |
public long readCountN1=0; |
| 3148 |
public long readCountSplice1=0; |
| 3149 |
|
| 3150 |
public long perfectHit1=0; |
| 3151 |
public long uniqueHit1=0; |
| 3152 |
public long correctUniqueHit1=0; |
| 3153 |
public long correctMultiHit1=0; |
| 3154 |
public long correctLowHit1=0; |
| 3155 |
public long noHit1=0; |
| 3156 |
|
| 3157 |
|
| 3158 |
public long perfectHitCount1=0; |
| 3159 |
|
| 3160 |
public long semiPerfectHitCount1=0; |
| 3161 |
|
| 3162 |
|
| 3163 |
public long perfectMatch1=0; |
| 3164 |
public long semiperfectMatch1=0; |
| 3165 |
public long perfectMatchBases1=0; |
| 3166 |
public long semiperfectMatchBases1=0; |
| 3167 |
public long ambiguousBestAlignment1=0; |
| 3168 |
public long ambiguousBestAlignmentBases1=0; |
| 3169 |
|
| 3170 |
public long initialSiteSum1=0; |
| 3171 |
public long postTrimSiteSum1=0; |
| 3172 |
public long postRescueSiteSum1=0; |
| 3173 |
public long siteSum1=0; |
| 3174 |
public long topSiteSum1=0; |
| 3175 |
|
| 3176 |
public long lowQualityReadsDiscarded1=0; |
| 3177 |
public long lowQualityBasesDiscarded1=0; |
| 3178 |
|
| 3179 |
public long mapped2=0; |
| 3180 |
public long mappedRetained2=0; |
| 3181 |
public long mappedRetainedBases2=0; |
| 3182 |
public long rescuedP2=0; |
| 3183 |
public long rescuedM2=0; |
| 3184 |
public long truePositiveP2=0; |
| 3185 |
public long truePositiveM2=0; |
| 3186 |
public long falsePositive2=0; |
| 3187 |
public long totalCorrectSites2=0; |
| 3188 |
|
| 3189 |
public long firstSiteCorrectP2=0; |
| 3190 |
public long firstSiteCorrectM2=0; |
| 3191 |
public long firstSiteIncorrect2=0; |
| 3192 |
public long firstSiteCorrectLoose2=0; |
| 3193 |
public long firstSiteIncorrectLoose2=0; |
| 3194 |
public long firstSiteCorrectPaired2=0; |
| 3195 |
public long firstSiteCorrectSolo2=0; |
| 3196 |
public long firstSiteCorrectRescued2=0; |
| 3197 |
|
| 3198 |
public long matchCountS2=0; |
| 3199 |
public long matchCountI2=0; |
| 3200 |
public long matchCountD2=0; |
| 3201 |
public long matchCountM2=0; |
| 3202 |
public long matchCountN2=0; |
| 3203 |
|
| 3204 |
public long readCountE2=0; |
| 3205 |
public long readCountS2=0; |
| 3206 |
public long readCountI2=0; |
| 3207 |
public long readCountD2=0; |
| 3208 |
public long readCountN2=0; |
| 3209 |
public long readCountSplice2=0; |
| 3210 |
|
| 3211 |
public long perfectHit2=0; |
| 3212 |
public long uniqueHit2=0; |
| 3213 |
public long correctUniqueHit2=0; |
| 3214 |
public long correctMultiHit2=0; |
| 3215 |
public long correctLowHit2=0; |
| 3216 |
public long noHit2=0; |
| 3217 |
|
| 3218 |
|
| 3219 |
public long perfectHitCount2=0; |
| 3220 |
|
| 3221 |
public long semiPerfectHitCount2=0; |
| 3222 |
|
| 3223 |
public long perfectMatch2=0; |
| 3224 |
public long semiperfectMatch2=0; |
| 3225 |
public long perfectMatchBases2=0; |
| 3226 |
public long semiperfectMatchBases2=0; |
| 3227 |
public long ambiguousBestAlignment2=0; |
| 3228 |
public long ambiguousBestAlignmentBases2=0; |
| 3229 |
|
| 3230 |
public long initialSiteSum2=0; |
| 3231 |
public long postTrimSiteSum2=0; |
| 3232 |
public long postRescueSiteSum2=0; |
| 3233 |
public long siteSum2=0; |
| 3234 |
public long topSiteSum2=0; |
| 3235 |
|
| 3236 |
public long lowQualityReadsDiscarded2=0; |
| 3237 |
public long lowQualityBasesDiscarded2=0; |
| 3238 |
|
| 3239 |
|
| 3240 |
|
| 3241 |
int idmodulo; |
| 3242 |
} |