private void outputRegions(BufferedWriter writer, Collection<Feature> regions) throws IOException { List<Feature> mergedRegions = RegionLoader.collapseRegions(regions, 0); for (Feature region : mergedRegions) { writer.write(region.getSeqname() + "\t" + region.getStart() + "\t" + region.getEnd() + "\n"); } }
// // Returns a downsampled set of reads for each sample. // private List<List<SAMRecord>> getReads( List<String> inputFiles, List<Feature> regions, ReAligner realigner) { int downsampleTarget = desiredNumberOfReads(regions); List<DownsampledReadList> readsList = new ArrayList<DownsampledReadList>(); for (String input : inputFiles) { Set<String> readIds = new HashSet<String>(); DownsampledReadList reads = new DownsampledReadList(downsampleTarget); readsList.add(reads); for (Feature region : regions) { SAMFileReader reader = new SAMFileReader(new File(input)); reader.setValidationStringency(ValidationStringency.SILENT); Iterator<SAMRecord> iter; if (region != null) { iter = reader.queryOverlapping( region.getSeqname(), (int) region.getStart(), (int) region.getEnd()); } else { iter = reader.iterator(); } while (iter.hasNext()) { SAMRecord read = iter.next(); // Don't allow same read to be counted twice. if ((!realigner.isFiltered(read)) && (!read.getDuplicateReadFlag()) && (!read.getReadFailsVendorQualityCheckFlag()) && (read.getMappingQuality() >= realigner.getMinMappingQuality() || read.getReadUnmappedFlag()) && (!readIds.contains(getIdentifier(read)))) { if (read.getReadString().length() > readLength) { reader.close(); throw new IllegalArgumentException( "Maximum read length of: " + readLength + " exceeded for: " + read.getSAMString()); } readIds.add(getIdentifier(read)); reads.add(read); } } if (reads.getTotalReadCount() != reads.getReads().size()) { if (isDebug) { System.err.println( "downsampled: " + regions.get(0).getDescriptor() + ": " + reads.getTotalReadCount() + " -> " + reads.getReads().size()); } } reader.close(); } } List<List<SAMRecord>> sampleReads = new ArrayList<List<SAMRecord>>(); for (DownsampledReadList downsampledReads : readsList) { sampleReads.add(downsampledReads.getReads()); } return sampleReads; }