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);