Search in sources :

Example 1 with BasicFastqWriter

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

the class FastqSplitInterleaved method doWork.

@Override
public int doWork(final List<String> args) {
    final String[] fileout = { this.fileA, this.fileB };
    FastqReader r1 = null;
    FastqWriter[] writers = { null, null };
    try {
        if (args.isEmpty()) {
            r1 = new FourLinesFastqReader(stdin());
        } else if (args.size() == 1) {
            r1 = new FourLinesFastqReader(new File(args.get(0)));
        } else {
            LOG.error("illegal.number.of.arguments");
            return -1;
        }
        if (fileout[0] == null && fileout[1] == null) {
            LOG.error("Both outputs are undefined.");
            return -1;
        }
        for (int i = 0; i < 2; ++i) {
            if (fileout[i] == null) {
                writers[i] = new BasicFastqWriter(new PrintStream(new NullOuputStream()));
            } else if (fileout[i].equals("-")) {
                if (i == 1 && "-".equals(fileout[0])) {
                    writers[i] = writers[0];
                } else {
                    writers[i] = new BasicFastqWriter(System.out);
                }
            } else {
                if (i == 1 && fileout[1].equals(fileout[0])) {
                    writers[i] = writers[0];
                } else {
                    writers[i] = new BasicFastqWriter(new File(fileout[i]));
                }
            }
        }
        FastqRecord[] records = { null, null };
        while (r1.hasNext()) {
            records[0] = r1.next();
            if (!r1.hasNext()) {
                r1.close();
                r1 = null;
                throw new IOException("fastq.paired.read.missing");
            }
            records[1] = r1.next();
            for (int i = 0; i < 2; ++i) {
                writers[i].write(records[i]);
            }
        }
        if (r1.hasNext()) {
            throw new IOException("Illegal number of reads in fastq");
        }
        return 0;
    } catch (Exception err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(r1);
        CloserUtil.close(writers[0]);
        CloserUtil.close(writers[1]);
    }
}
Also used : PrintStream(java.io.PrintStream) FourLinesFastqReader(com.github.lindenb.jvarkit.util.picard.FourLinesFastqReader) FastqRecord(htsjdk.samtools.fastq.FastqRecord) IOException(java.io.IOException) IOException(java.io.IOException) 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) NullOuputStream(com.github.lindenb.jvarkit.io.NullOuputStream) BasicFastqWriter(htsjdk.samtools.fastq.BasicFastqWriter) File(java.io.File)

Example 2 with BasicFastqWriter

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

the class BamToFastq method doWork.

@Override
public int doWork(List<String> args) {
    SamReader sfr = null;
    SortingCollection<MappedFastq> fastqCollection = null;
    try {
        boolean found_single = false;
        boolean found_paired = false;
        long non_primary_alignmaned_flag = 0L;
        sfr = super.openSamReader(oneFileOrNull(args));
        fastqCollection = SortingCollection.newInstance(MappedFastq.class, new MappedFastqCodec(), new MappedFastqComparator(), this.maxRecordsInRam, this.tmpDir.toPath());
        fastqCollection.setDestructiveIteration(true);
        SAMRecordIterator iter = sfr.iterator();
        SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(sfr.getFileHeader().getSequenceDictionary());
        while (iter.hasNext()) {
            final SAMRecord rec = progress.watch(iter.next());
            if (rec.isSecondaryOrSupplementary()) {
                if (non_primary_alignmaned_flag == 0) {
                    LOG.warn("SKIPPING NON-PRIMARY " + (non_primary_alignmaned_flag + 1) + " ALIGNMENTS");
                }
                non_primary_alignmaned_flag++;
                continue;
            }
            MappedFastq m = new MappedFastq();
            m.name = rec.getReadName();
            if (m.name == null)
                m.name = "";
            m.seq = rec.getReadString();
            if (m.seq.equals(SAMRecord.NULL_SEQUENCE_STRING))
                m.seq = "";
            m.qual = rec.getBaseQualityString();
            if (m.qual.equals(SAMRecord.NULL_QUALS_STRING))
                m.qual = "";
            if (!rec.getReadUnmappedFlag() && rec.getReadNegativeStrandFlag()) {
                m.seq = AcidNucleics.reverseComplement(m.seq);
                m.qual = new StringBuilder(m.qual).reverse().toString();
            }
            if (m.seq.length() != m.qual.length()) {
                LOG.error("length(seq)!=length(qual) in " + m.name);
                continue;
            }
            if (m.seq.isEmpty() && m.qual.isEmpty()) {
                m.seq = "N";
                m.qual = "#";
            }
            if (rec.getReadPairedFlag()) {
                found_paired = true;
                if (found_single) {
                    sfr.close();
                    throw new RuntimeException("input is a mix of paired/singled reads");
                }
                m.side = (byte) (rec.getSecondOfPairFlag() ? 2 : 1);
            } else {
                found_single = true;
                if (found_paired) {
                    sfr.close();
                    throw new RuntimeException("input is a mix of paired/singled reads");
                }
                m.side = (byte) 0;
            }
            fastqCollection.add(m);
        }
        iter.close();
        CloserUtil.close(iter);
        CloserUtil.close(sfr);
        progress.finish();
        fastqCollection.doneAdding();
        LOG.info("Done reading.");
        if (found_paired) {
            FastqWriter fqw1 = null;
            FastqWriter fqw2 = null;
            if (forwardFile != null) {
                LOG.info("Writing to " + forwardFile);
                fqw1 = new BasicFastqWriter(forwardFile);
            } else {
                LOG.info("Writing to stdout");
                fqw1 = new BasicFastqWriter(new PrintStream(stdout()));
            }
            if (reverseFile != null) {
                LOG.info("Writing to " + reverseFile);
                fqw2 = new BasicFastqWriter(reverseFile);
            } else {
                LOG.info("Writing to interlaced stdout");
                fqw2 = fqw1;
            }
            List<MappedFastq> row = new ArrayList<MappedFastq>();
            CloseableIterator<MappedFastq> r = fastqCollection.iterator();
            for (; ; ) {
                MappedFastq curr = null;
                if (r.hasNext())
                    curr = r.next();
                if (curr == null || (!row.isEmpty() && !row.get(0).name.equals(curr.name))) {
                    if (!row.isEmpty()) {
                        if (row.size() > 2) {
                            LOG.warn("WTF :" + row);
                        }
                        boolean found_F = false;
                        boolean found_R = false;
                        for (MappedFastq m : row) {
                            switch((int) m.side) {
                                case 1:
                                    if (found_F)
                                        throw new RuntimeException("two forward reads found for " + row.get(0).name);
                                    found_F = true;
                                    echo(fqw1, m);
                                    break;
                                case 2:
                                    if (found_R)
                                        throw new RuntimeException("two reverse reads found for " + row.get(0).name);
                                    found_R = true;
                                    echo(fqw2, m);
                                    break;
                                default:
                                    throw new IllegalStateException("uh???");
                            }
                        }
                        if (!found_F) {
                            if (this.repair_missing_read) {
                                LOG.warn("forward not found for " + row.get(0));
                                MappedFastq pad = new MappedFastq();
                                pad.side = (byte) 1;
                                pad.name = row.get(0).name;
                                pad.seq = "N";
                                pad.qual = "#";
                                echo(fqw1, pad);
                            } else {
                                throw new RuntimeException("forward not found for " + row);
                            }
                        }
                        if (!found_R) {
                            if (repair_missing_read) {
                                LOG.warn("reverse not found for " + row.get(0));
                                MappedFastq pad = new MappedFastq();
                                pad.side = (byte) 2;
                                pad.name = row.get(0).name;
                                pad.seq = "N";
                                pad.qual = "#";
                                echo(fqw2, pad);
                            } else {
                                throw new RuntimeException("reverse not found for " + row);
                            }
                        }
                    }
                    if (curr == null)
                        break;
                    row.clear();
                }
                row.add(curr);
            }
            r.close();
            fqw1.close();
            fqw2.close();
        } else if (found_single) {
            FastqWriter fqw1 = null;
            if (forwardFile != null) {
                LOG.info("Writing to " + forwardFile);
                fqw1 = new BasicFastqWriter(forwardFile);
            } else {
                LOG.info("Writing to stdout");
                fqw1 = new BasicFastqWriter(new PrintStream(stdout()));
            }
            final CloseableIterator<MappedFastq> r = fastqCollection.iterator();
            while (r.hasNext()) {
                echo(fqw1, r.next());
            }
            r.close();
            fqw1.close();
        }
        return RETURN_OK;
    } catch (final Exception err) {
        LOG.error(err);
        return -1;
    } finally {
        if (fastqCollection != null)
            fastqCollection.cleanup();
    }
}
Also used : PrintStream(java.io.PrintStream) CloseableIterator(htsjdk.samtools.util.CloseableIterator) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) SAMSequenceDictionaryProgress(com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SamReader(htsjdk.samtools.SamReader) SAMRecord(htsjdk.samtools.SAMRecord) BasicFastqWriter(htsjdk.samtools.fastq.BasicFastqWriter) FastqWriter(htsjdk.samtools.fastq.FastqWriter) BasicFastqWriter(htsjdk.samtools.fastq.BasicFastqWriter)

Example 3 with BasicFastqWriter

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

the class FastqJavascript method openFailing.

/* open failing bam if it was not already open */
private boolean openFailing() {
    if (this.failingReadsFile == null)
        return false;
    if (this.failingReadsWriter == null) {
        LOG.info("Writing failings to " + this.failingReadsFile);
        failingReadsWriter = new BasicFastqWriter(this.failingReadsFile);
    }
    return true;
}
Also used : BasicFastqWriter(htsjdk.samtools.fastq.BasicFastqWriter)

Example 4 with BasicFastqWriter

use of htsjdk.samtools.fastq.BasicFastqWriter 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 5 with BasicFastqWriter

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

the class SamExtractClip method doWork.

@Override
public int doWork(List<String> args) {
    SamReader r = null;
    BasicFastqWriter out = null;
    try {
        if (this.outputFile != null) {
            LOG.info("writing to " + this.outputFile);
            out = new BasicFastqWriter(this.outputFile);
        } else {
            LOG.info("writing to stdout");
            out = new BasicFastqWriter(stdout());
        }
        if (args.isEmpty()) {
            LOG.info("Reading from stdin");
            r = createSamReaderFactory().open(SamInputResource.of(stdin()));
            run(r, out);
            r.close();
        } else {
            for (final String filename : args) {
                LOG.info("Reading from " + filename);
                r = createSamReaderFactory().open(SamInputResource.of(filename));
                run(r, out);
                r.close();
                r = null;
            }
        }
        out.flush();
        return RETURN_OK;
    } catch (final Exception err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(r);
        CloserUtil.close(out);
    }
}
Also used : SamReader(htsjdk.samtools.SamReader) BasicFastqWriter(htsjdk.samtools.fastq.BasicFastqWriter)

Aggregations

BasicFastqWriter (htsjdk.samtools.fastq.BasicFastqWriter)9 FastqWriter (htsjdk.samtools.fastq.FastqWriter)6 FastqReader (com.github.lindenb.jvarkit.util.picard.FastqReader)4 FourLinesFastqReader (com.github.lindenb.jvarkit.util.picard.FourLinesFastqReader)4 File (java.io.File)4 IOException (java.io.IOException)4 PrintStream (java.io.PrintStream)4 SamReader (htsjdk.samtools.SamReader)2 FastqRecord (htsjdk.samtools.fastq.FastqRecord)2 NullOuputStream (com.github.lindenb.jvarkit.io.NullOuputStream)1 SAMSequenceDictionaryProgress (com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress)1 SAMRecord (htsjdk.samtools.SAMRecord)1 SAMRecordIterator (htsjdk.samtools.SAMRecordIterator)1 FastqWriterFactory (htsjdk.samtools.fastq.FastqWriterFactory)1 CloseableIterator (htsjdk.samtools.util.CloseableIterator)1 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedReader (java.io.BufferedReader)1 ArrayList (java.util.ArrayList)1 Bindings (javax.script.Bindings)1