//assert (tiles.size() > 0);
if (corruptIndex) {
return t;
}
final PreferenceManager prefMgr = PreferenceManager.getInstance();
boolean filterFailedReads = prefMgr.getAsBoolean(PreferenceManager.SAM_FILTER_FAILED_READS);
boolean filterSecondaryAlignments = prefMgr.getAsBoolean(PreferenceManager.SAM_FILTER_SECONDARY_ALIGNMENTS);
boolean filterSupplementaryAlignments = prefMgr.getAsBoolean(PreferenceManager.SAM_FILTER_SUPPLEMENTARY_ALIGNMENTS);
ReadGroupFilter filter = ReadGroupFilter.getFilter();
boolean showDuplicates = prefMgr.getAsBoolean(PreferenceManager.SAM_SHOW_DUPLICATES);
int qualityThreshold = prefMgr.getAsInt(PreferenceManager.SAM_QUALITY_THRESHOLD);
CloseableIterator<Alignment> iter = null;
//log.debug("Loading : " + start + " - " + end);
int alignmentCount = 0;
WeakReference<AlignmentTileLoader> ref = new WeakReference(this);
try {
ObjectCache<String, Alignment> mappedMates = new ObjectCache<String, Alignment>(1000);
ObjectCache<String, Alignment> unmappedMates = new ObjectCache<String, Alignment>(1000);
activeLoaders.add(ref);
iter = reader.query(chr, start, end, false);
while (iter != null && iter.hasNext()) {
if (cancel) {
return t;
}
Alignment record = iter.next();
// Set mate sequence of unmapped mates
// Put a limit on the total size of this collection.
String readName = record.getReadName();
if (record.isPaired()) {
pairedEnd = true;
if (record.isMapped()) {
if (!record.getMate().isMapped()) {
// record is mapped, mate is not
Alignment mate = unmappedMates.get(readName);
if (mate == null) {
mappedMates.put(readName, record);
} else {
record.setMateSequence(mate.getReadSequence());
unmappedMates.remove(readName);
mappedMates.remove(readName);
}
}
} else if (record.getMate().isMapped()) {
// record not mapped, mate is
Alignment mappedMate = mappedMates.get(readName);
if (mappedMate == null) {
unmappedMates.put(readName, record);
} else {
mappedMate.setMateSequence(record.getReadSequence());
unmappedMates.remove(readName);
mappedMates.remove(readName);
}
}
}
if (!record.isMapped() || (!showDuplicates && record.isDuplicate()) ||
(filterFailedReads && record.isVendorFailedRead()) ||
(filterSecondaryAlignments && !record.isPrimary()) ||
(filterSupplementaryAlignments && record.isSupplementary()) ||
record.getMappingQuality() < qualityThreshold ||
(filter != null && filter.filterAlignment(record))) {
continue;
}
t.addRecord(record);
alignmentCount++;
int interval = Globals.isTesting() ? 100000 : 1000;
if (alignmentCount % interval == 0) {
if (cancel) return null;
String msg = "Reads loaded: " + alignmentCount;
MessageUtils.setStatusBarMessage(msg);
if (monitor != null) {
monitor.updateStatus(msg);
}
if (memoryTooLow()) {
if (monitor != null) monitor.fireProgressChange(100);
cancelReaders();
t.finish();
return t; // <= TODO need to cancel all readers
}
}
// Update pe stats
if (peStats != null && record.isPaired() && record.isProperPair()) {
String lb = record.getLibrary();
if (lb == null) lb = "null";
PEStats stats = peStats.get(lb);
if (stats == null) {
stats = new PEStats(lb);
peStats.put(lb, stats);
}
stats.update(record);
}
}
// End iteration over alignments
// Compute peStats
if (peStats != null) {
// TODO -- something smarter re the percentiles. For small samples these will revert to min and max
double minPercentile = prefMgr.getAsFloat(PreferenceManager.SAM_MIN_INSERT_SIZE_PERCENTILE);
double maxPercentile = prefMgr.getAsFloat(PreferenceManager.SAM_MAX_INSERT_SIZE_PERCENTILE);
for (PEStats stats : peStats.values()) {
stats.compute(minPercentile, maxPercentile);
}
}