Search in sources :

Example 1 with FastqReader

use of com.github.lindenb.jvarkit.util.picard.FastqReader in project jvarkit by lindenb.

the class FastQName method countReads.

public long countReads() throws IOException {
    if (nReads == null) {
        long n = 0L;
        FastqReader r = null;
        try {
            r = new FourLinesFastqReader(this.getFile());
            r.setValidationStringency(ValidationStringency.LENIENT);
            while (r.hasNext()) {
                r.next();
                ++n;
            }
        } catch (Exception err) {
            nReads = -1L;
            throw new IOException(err);
        } finally {
            if (r != null)
                r.close();
        }
        nReads = n;
    }
    return nReads;
}
Also used : FastqReader(com.github.lindenb.jvarkit.util.picard.FastqReader) FourLinesFastqReader(com.github.lindenb.jvarkit.util.picard.FourLinesFastqReader) FourLinesFastqReader(com.github.lindenb.jvarkit.util.picard.FourLinesFastqReader) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with FastqReader

use of com.github.lindenb.jvarkit.util.picard.FastqReader 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 3 with FastqReader

use of com.github.lindenb.jvarkit.util.picard.FastqReader in project jvarkit by lindenb.

the class ConvertPhred64toFastq33 method convert.

private void convert(InputStream in) throws IOException {
    FastqReader r = new FourLinesFastqReader(in);
    while (r.hasNext() && !pw.checkError()) {
        final FastqRecord rec = r.next();
        byte[] quals = rec.getBaseQualityString().getBytes();
        for (int i = 0; i < quals.length; ++i) {
            quals[i] = (byte) (quals[i] - 64 + 33);
            if (quals[i] < 33 || quals[i] > 126) {
                r.close();
                throw new IOException("q=" + (int) quals[i]);
            }
        }
        String name = rec.getReadName();
        int diez = name.indexOf('#');
        if (diez != -1)
            name = name.substring(0, diez);
        pw.print(FastqConstants.SEQUENCE_HEADER);
        pw.println(name);
        pw.println(rec.getReadString());
        pw.print(FastqConstants.QUALITY_HEADER);
        pw.println(rec.getBaseQualityHeader() == null || rec.getReadName().equals(rec.getBaseQualityHeader()) ? "" : rec.getBaseQualityHeader());
        pw.println(new String(quals));
    }
    r.close();
}
Also used : FourLinesFastqReader(com.github.lindenb.jvarkit.util.picard.FourLinesFastqReader) FastqReader(com.github.lindenb.jvarkit.util.picard.FastqReader) FourLinesFastqReader(com.github.lindenb.jvarkit.util.picard.FourLinesFastqReader) FastqRecord(htsjdk.samtools.fastq.FastqRecord) IOException(java.io.IOException)

Example 4 with FastqReader

use of com.github.lindenb.jvarkit.util.picard.FastqReader in project jvarkit by lindenb.

the class FastqEntropy method convert.

private void convert(InputStream in) throws IOException {
    FastqReader r = new FourLinesFastqReader(in);
    while (r.hasNext()) {
        FastqRecord rec = r.next();
        BestCompressionOutputStream gzout = new BestCompressionOutputStream();
        gzout.write(rec.getBaseQualityString().getBytes());
        gzout.flush();
        gzout.close();
        this.length2count.incr(gzout.getByteWrittenCount());
    }
    r.close();
}
Also used : FourLinesFastqReader(com.github.lindenb.jvarkit.util.picard.FourLinesFastqReader) FastqReader(com.github.lindenb.jvarkit.util.picard.FastqReader) FourLinesFastqReader(com.github.lindenb.jvarkit.util.picard.FourLinesFastqReader) FastqRecord(htsjdk.samtools.fastq.FastqRecord)

Example 5 with FastqReader

use of com.github.lindenb.jvarkit.util.picard.FastqReader in project jvarkit by lindenb.

the class FastqRecordTreePack method doWork.

@Override
public int doWork(List<String> args) {
    setDimension(this.dimensionStr);
    FastqReader fqr = null;
    try {
        parseConfigFile();
        if (super.nodeFactoryChain.next == null) {
            LOG.error("no path defined");
            return -1;
        }
        if (args.isEmpty()) {
            LOG.info("Reading stdin");
            fqr = new FourLinesFastqReader(stdin());
            scan(fqr);
            CloserUtil.close(fqr);
            LOG.info("Done stdin");
        } else {
            for (final String filename : args) {
                InputStream in = null;
                LOG.info("Reading " + filename);
                if (IOUtils.isRemoteURI(filename)) {
                    in = IOUtils.openURIForReading(filename);
                    fqr = new FourLinesFastqReader(in);
                } else {
                    fqr = new FourLinesFastqReader(new File(filename));
                }
                scan(fqr);
                LOG.info("Done " + filename);
                CloserUtil.close(fqr);
                CloserUtil.close(in);
            }
        }
        this.layout();
        this.svg(this.outputFile);
        return RETURN_OK;
    } catch (Exception e) {
        LOG.error(e);
        return -1;
    } finally {
        CloserUtil.close(fqr);
    }
}
Also used : FourLinesFastqReader(com.github.lindenb.jvarkit.util.picard.FourLinesFastqReader) FastqReader(com.github.lindenb.jvarkit.util.picard.FastqReader) InputStream(java.io.InputStream) FourLinesFastqReader(com.github.lindenb.jvarkit.util.picard.FourLinesFastqReader) File(java.io.File) IOException(java.io.IOException) ScriptException(javax.script.ScriptException)

Aggregations

FastqReader (com.github.lindenb.jvarkit.util.picard.FastqReader)12 FourLinesFastqReader (com.github.lindenb.jvarkit.util.picard.FourLinesFastqReader)12 File (java.io.File)8 IOException (java.io.IOException)7 BasicFastqWriter (htsjdk.samtools.fastq.BasicFastqWriter)4 FastqWriter (htsjdk.samtools.fastq.FastqWriter)4 FastqRecord (htsjdk.samtools.fastq.FastqRecord)3 PrintStream (java.io.PrintStream)3 ScriptException (javax.script.ScriptException)2 NullOuputStream (com.github.lindenb.jvarkit.io.NullOuputStream)1 FastqWriterFactory (htsjdk.samtools.fastq.FastqWriterFactory)1 TribbleException (htsjdk.tribble.TribbleException)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1