Search in sources :

Example 1 with FastqWriterFactory

use of htsjdk.samtools.fastq.FastqWriterFactory in project jvarkit by lindenb.

the class PadEmptyFastq method doWork.

@Override
public int doWork(final List<String> args) {
    FastqWriter fqw = null;
    try {
        if (this.outFile == null) {
            LOG.info("writing to stdout");
            fqw = new BasicFastqWriter(stdout());
        } else {
            LOG.info("writing to " + this.outFile);
            fqw = new FastqWriterFactory().newWriter(this.outFile);
        }
        if (args.isEmpty()) {
            LOG.info("Reading from stdin");
            FastqReader fqr = new FourLinesFastqReader(stdin());
            copyTo(fqr, fqw);
            fqr.close();
        } else {
            for (final String filename : args) {
                LOG.info("Reading from " + filename);
                FastqReader fqr = new FourLinesFastqReader(new File(filename));
                copyTo(fqr, fqw);
                fqr.close();
            }
        }
        return 0;
    } catch (final Exception err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(fqw);
    }
}
Also used : FourLinesFastqReader(com.github.lindenb.jvarkit.util.picard.FourLinesFastqReader) FastqReader(com.github.lindenb.jvarkit.util.picard.FastqReader) BasicFastqWriter(htsjdk.samtools.fastq.BasicFastqWriter) FastqWriter(htsjdk.samtools.fastq.FastqWriter) FastqWriterFactory(htsjdk.samtools.fastq.FastqWriterFactory) FourLinesFastqReader(com.github.lindenb.jvarkit.util.picard.FourLinesFastqReader) BasicFastqWriter(htsjdk.samtools.fastq.BasicFastqWriter) File(java.io.File)

Example 2 with FastqWriterFactory

use of htsjdk.samtools.fastq.FastqWriterFactory in project gatk by broadinstitute.

the class SamToFastq method doWork.

@Override
protected Object doWork() {
    IOUtil.assertFileIsReadable(INPUT);
    final SamReader reader = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT);
    final Map<String, SAMRecord> firstSeenMates = new HashMap<>();
    final FastqWriterFactory factory = new FastqWriterFactory();
    factory.setCreateMd5(CREATE_MD5_FILE);
    final Map<SAMReadGroupRecord, FastqWriters> writers = generateWriters(reader.getFileHeader().getReadGroups(), factory);
    final ProgressLogger progress = new ProgressLogger(logger);
    for (final SAMRecord currentRecord : reader) {
        if (currentRecord.isSecondaryOrSupplementary() && !INCLUDE_NON_PRIMARY_ALIGNMENTS)
            continue;
        // Skip non-PF reads as necessary
        if (currentRecord.getReadFailsVendorQualityCheckFlag() && !INCLUDE_NON_PF_READS)
            continue;
        final FastqWriters fq = writers.get(currentRecord.getReadGroup());
        if (currentRecord.getReadPairedFlag()) {
            final String currentReadName = currentRecord.getReadName();
            final SAMRecord firstRecord = firstSeenMates.remove(currentReadName);
            if (firstRecord == null) {
                firstSeenMates.put(currentReadName, currentRecord);
            } else {
                assertPairedMates(firstRecord, currentRecord);
                final SAMRecord read1 = currentRecord.getFirstOfPairFlag() ? currentRecord : firstRecord;
                final SAMRecord read2 = currentRecord.getFirstOfPairFlag() ? firstRecord : currentRecord;
                writeRecord(read1, 1, fq.getFirstOfPair(), READ1_TRIM, READ1_MAX_BASES_TO_WRITE);
                final FastqWriter secondOfPairWriter = fq.getSecondOfPair();
                if (secondOfPairWriter == null) {
                    throw new UserException("Input contains paired reads but no SECOND_END_FASTQ specified.");
                }
                writeRecord(read2, 2, secondOfPairWriter, READ2_TRIM, READ2_MAX_BASES_TO_WRITE);
            }
        } else {
            writeRecord(currentRecord, null, fq.getUnpaired(), READ1_TRIM, READ1_MAX_BASES_TO_WRITE);
        }
        progress.record(currentRecord);
    }
    CloserUtil.close(reader);
    // Close all the fastq writers being careful to close each one only once!
    for (final FastqWriters writerMapping : new HashSet<>(writers.values())) {
        writerMapping.closeAll();
    }
    if (!firstSeenMates.isEmpty()) {
        SAMUtils.processValidationError(new SAMValidationError(SAMValidationError.Type.MATE_NOT_FOUND, "Found " + firstSeenMates.size() + " unpaired mates", null), VALIDATION_STRINGENCY);
    }
    return null;
}
Also used : HashMap(java.util.HashMap) SAMReadGroupRecord(htsjdk.samtools.SAMReadGroupRecord) ProgressLogger(org.broadinstitute.hellbender.utils.runtime.ProgressLogger) SamReader(htsjdk.samtools.SamReader) SAMValidationError(htsjdk.samtools.SAMValidationError) SAMRecord(htsjdk.samtools.SAMRecord) FastqWriterFactory(htsjdk.samtools.fastq.FastqWriterFactory) FastqWriter(htsjdk.samtools.fastq.FastqWriter) UserException(org.broadinstitute.hellbender.exceptions.UserException) HashSet(java.util.HashSet)

Aggregations

FastqWriter (htsjdk.samtools.fastq.FastqWriter)2 FastqWriterFactory (htsjdk.samtools.fastq.FastqWriterFactory)2 FastqReader (com.github.lindenb.jvarkit.util.picard.FastqReader)1 FourLinesFastqReader (com.github.lindenb.jvarkit.util.picard.FourLinesFastqReader)1 SAMReadGroupRecord (htsjdk.samtools.SAMReadGroupRecord)1 SAMRecord (htsjdk.samtools.SAMRecord)1 SAMValidationError (htsjdk.samtools.SAMValidationError)1 SamReader (htsjdk.samtools.SamReader)1 BasicFastqWriter (htsjdk.samtools.fastq.BasicFastqWriter)1 File (java.io.File)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 UserException (org.broadinstitute.hellbender.exceptions.UserException)1 ProgressLogger (org.broadinstitute.hellbender.utils.runtime.ProgressLogger)1