use of htsjdk.samtools.util.ProgressLogger in project gridss by PapenfussLab.
the class SequentialCoverageAnnotator method createLookup.
private List<ReferenceCoverageLookup> createLookup(ProcessingContext context, List<SAMEvidenceSource> sources, int windowSize) {
List<ReferenceCoverageLookup> result = new ArrayList<>();
for (SAMEvidenceSource ses : sources) {
assert (ses.getSourceCategory() >= 0);
assert (ses.getSourceCategory() < context.getCategoryCount());
// one read-ahead thread per input file
SamReader reader = SamReaderFactory.makeDefault().open(ses.getFile());
SAMRecordIterator rawIterator = reader.iterator();
rawIterator.assertSorted(SortOrder.coordinate);
CloseableIterator<SAMRecord> sit = new AsyncBufferedIterator<SAMRecord>(rawIterator, ses.getFile().getName() + "-Coverage");
// close the async iterator first to prevent aysnc reading from a closed stream
toclose.add(sit);
toclose.add(rawIterator);
toclose.add(reader);
sit = new ProgressLoggingSAMRecordIterator(sit, new ProgressLogger(log, 10000000));
SequentialReferenceCoverageLookup sourceLookup = new SequentialReferenceCoverageLookup(sit, ses.getMetrics().getIdsvMetrics(), ses.getReadPairConcordanceCalculator(), windowSize, ses.getSourceCategory(), context.isFilterDuplicates());
context.registerBuffer(ses.getFile().getName(), sourceLookup);
result.add(sourceLookup);
}
return result;
}
use of htsjdk.samtools.util.ProgressLogger in project gridss by PapenfussLab.
the class ComputeSamTags method compute.
public static void compute(Iterator<SAMRecord> rawit, SAMFileWriter writer, ReferenceLookup reference, Set<String> tags, boolean softenHardClips, boolean fixMates, boolean recalculateSupplementary, String threadprefix) throws IOException {
ProgressLogger progress = new ProgressLogger(log);
try (CloseableIterator<SAMRecord> aysncit = new AsyncBufferedIterator<SAMRecord>(rawit, threadprefix + "raw")) {
Iterator<SAMRecord> it = aysncit;
if (tags.contains(SAMTag.NM.name()) || tags.contains(SAMTag.SA.name())) {
it = new AsyncBufferedIterator<SAMRecord>(it, threadprefix + "nm");
it = new NmTagIterator(it, reference);
}
if (!Sets.intersection(tags, SAMRecordUtil.TEMPLATE_TAGS).isEmpty() || softenHardClips) {
it = new TemplateTagsIterator(it, softenHardClips, fixMates, recalculateSupplementary, tags);
it = new AsyncBufferedIterator<SAMRecord>(it, threadprefix + "tags");
}
while (it.hasNext()) {
SAMRecord r = it.next();
writer.addAlignment(r);
progress.record(r);
}
}
}
use of htsjdk.samtools.util.ProgressLogger in project gridss by PapenfussLab.
the class ByReadNameSinglePassSamProgram method makeItSo.
public static void makeItSo(final File input, final File referenceSequence, final boolean assumeSorted, final long stopAfter, final Collection<ByReadNameSinglePassSamProgram> programs) throws FileNotFoundException {
// Setup the standard inputs
IOUtil.assertFileIsReadable(input);
SamReader in = SamReaderFactory.makeDefault().referenceSequence(referenceSequence).open(input);
// Optionally load up the reference sequence and double check sequence dictionaries
final ReferenceLookup lookup;
if (referenceSequence == null) {
lookup = null;
} else {
IOUtil.assertFileIsReadable(referenceSequence);
lookup = new TwoBitBufferedReferenceSequenceFile(new IndexedFastaSequenceFile(referenceSequence));
if (!in.getFileHeader().getSequenceDictionary().isEmpty()) {
SequenceUtil.assertSequenceDictionariesEqual(in.getFileHeader().getSequenceDictionary(), lookup.getSequenceDictionary());
}
}
// Check on the sort order of the BAM file
final SortOrder sort = in.getFileHeader().getSortOrder();
if (sort != SortOrder.queryname) {
if (assumeSorted) {
log.warn("File reports sort order '" + sort + "', assuming it's queryname sorted anyway.");
} else {
throw new PicardException("File " + input.getAbsolutePath() + " should be queryname sorted but " + "the header says the sort order is " + sort + ". If you believe the file " + "to be queryname sorted you may pass ASSUME_SORTED=true");
}
}
for (final ByReadNameSinglePassSamProgram program : programs) {
program.setReference(lookup);
program.setup(in.getFileHeader(), input);
}
final ProgressLogger progress = new ProgressLogger(log);
final SAMRecordIterator rawit = in.iterator();
final CloseableIterator<SAMRecord> it = new AsyncBufferedIterator<SAMRecord>(rawit, "ByReadNameSinglePassSamProgram " + input.getName());
try {
List<SAMRecord> currentRecords = new ArrayList<>();
String currentReadName = null;
while (it.hasNext()) {
SAMRecord r = it.next();
String readname = r.getReadName();
// if read name we have to just treat it as a single read
if (readname == null || !readname.equals(currentReadName)) {
if (currentRecords.size() > 0) {
for (final ByReadNameSinglePassSamProgram program : programs) {
program.acceptFragment(currentRecords, lookup);
}
}
currentRecords.clear();
currentReadName = readname;
if (stopAfter > 0 && progress.getCount() >= stopAfter) {
break;
}
}
currentRecords.add(r);
progress.record(r);
}
if (currentRecords.size() > 0) {
for (final ByReadNameSinglePassSamProgram program : programs) {
program.acceptFragment(currentRecords, lookup);
}
}
} finally {
CloserUtil.close(it);
CloserUtil.close(rawit);
CloserUtil.close(in);
}
for (final ByReadNameSinglePassSamProgram program : programs) {
program.finish();
}
}
use of htsjdk.samtools.util.ProgressLogger in project gridss by PapenfussLab.
the class VcfTransformCommandLineProgram method saveVcf.
protected void saveVcf(File file, Iterator<IdsvVariantContext> calls) throws IOException {
File tmp = gridss.Defaults.OUTPUT_TO_TEMP_FILE ? FileSystemContext.getWorkingFileFor(file) : file;
final ProgressLogger writeProgress = new ProgressLogger(log);
try (VariantContextWriter vcfWriter = getContext().getVariantContextWriter(tmp, true)) {
while (calls.hasNext()) {
IdsvVariantContext record = calls.next();
vcfWriter.add(record);
writeProgress.record(record.getContig(), record.getStart());
}
}
if (tmp != file) {
FileHelper.move(tmp, file, true);
}
}
use of htsjdk.samtools.util.ProgressLogger in project gridss by PapenfussLab.
the class ReadsToBedpe method doWork.
@Override
protected int doWork() {
log.debug("Setting language-neutral locale");
java.util.Locale.setDefault(Locale.ROOT);
validateParameters();
SamReaderFactory readerFactory = SamReaderFactory.make();
try {
try (SamReader reader = readerFactory.open(INPUT)) {
SAMFileHeader header = reader.getFileHeader();
SAMSequenceDictionary dict = header.getSequenceDictionary();
// ExecutorService threadpool = Executors.newFixedThreadPool(WORKER_THREADS, new ThreadFactoryBuilder().setDaemon(false).setNameFormat("Worker-%d").build());
try (CloseableIterator<SAMRecord> rawit = new AsyncBufferedIterator<SAMRecord>(reader.iterator(), 3, 64)) {
ProgressLoggingSAMRecordIterator logit = new ProgressLoggingSAMRecordIterator(rawit, new ProgressLogger(log));
// ParallelTransformIterator<SAMRecord, List<String>> it = new ParallelTransformIterator<>(logit, r -> asBedPe(dict, r), 16 + 2 * WORKER_THREADS, threadpool);
Iterator<List<String>> it = Iterators.transform(logit, r -> asBedPe(dict, r));
int i = 0;
try (BufferedWriter writer = new BufferedWriter(new FileWriter(OUTPUT))) {
while (it.hasNext()) {
for (String line : it.next()) {
if (line != null) {
writer.write(line);
writer.write('\n');
}
}
i++;
}
if (i % 1000 == 0) {
writer.flush();
}
}
}
}
} catch (IOException e) {
log.error(e);
return -1;
}
return 0;
}
Aggregations