Search in sources :

Example 6 with BasicFastqWriter

use of htsjdk.samtools.fastq.BasicFastqWriter in project gridss by PapenfussLab.

the class ExternalProcessStreamingAligner method ensureAligner.

private void ensureAligner() throws IOException {
    if (aligner == null) {
        log.info("Starting external aligner");
        log.info(commandlinestr);
        aligner = new ProcessBuilder(args).redirectInput(Redirect.PIPE).redirectOutput(Redirect.PIPE).redirectError(Redirect.INHERIT).start();
        toExternalProgram = new BasicFastqWriter(new PrintStream(new BufferedOutputStream(aligner.getOutputStream())));
        reader = new Thread(() -> readAllAlignments(readerFactory));
        reader.setName("ExternalProcessStreamingAligner");
        reader.start();
    }
}
Also used : PrintStream(java.io.PrintStream) BasicFastqWriter(htsjdk.samtools.fastq.BasicFastqWriter) BufferedOutputStream(java.io.BufferedOutputStream)

Example 7 with BasicFastqWriter

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

the class FastqGrep method doWork.

@Override
public int doWork(List<String> args) {
    BufferedReader in = null;
    FastqWriter out = null;
    try {
        if (this.readNameFile != null) {
            in = IOUtils.openFileForBufferedReading(this.readNameFile);
            String line;
            while ((line = in.readLine()) != null) {
                line = line.trim();
                if (line.isEmpty())
                    continue;
                this.readNames.put(getReadName(line), 0);
            }
            in.close();
        }
        for (final String r : this.readNamesInput) {
            this.readNames.put(getReadName(r), 0);
        }
        if (readNames.isEmpty()) {
            LOG.warn("no read name found.");
        }
        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");
            FastqReader fqR = new FourLinesFastqReader(System.in);
            run(fqR, out);
            fqR.close();
        } else
            for (String fname : args) {
                File f = new File(fname);
                LOG.info("Reading from " + f);
                FastqReader fqR = new FourLinesFastqReader(f);
                run(fqR, out);
                fqR.close();
            }
        CloserUtil.close(out);
        return 0;
    } catch (Exception err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(in);
    }
}
Also used : FourLinesFastqReader(com.github.lindenb.jvarkit.util.picard.FourLinesFastqReader) FastqReader(com.github.lindenb.jvarkit.util.picard.FastqReader) BufferedReader(java.io.BufferedReader) BasicFastqWriter(htsjdk.samtools.fastq.BasicFastqWriter) FastqWriter(htsjdk.samtools.fastq.FastqWriter) FourLinesFastqReader(com.github.lindenb.jvarkit.util.picard.FourLinesFastqReader) BasicFastqWriter(htsjdk.samtools.fastq.BasicFastqWriter) File(java.io.File)

Example 8 with BasicFastqWriter

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

the class FastqJavascript method doWork.

private void doWork(final FastqReader r) throws IOException, ScriptException {
    final FastqWriter[] fastqWriters = { null, null };
    try {
        if (!interleaved) {
            if (this.R1FileOut != null || this.R2FileOut != null) {
                throw new IllegalStateException("This is not an interleaved input but option R1FILEOUT / OPTION_R2FILEOUT were defined.");
            }
            if (this.outputFile == null) {
                fastqWriters[0] = new BasicFastqWriter(new PrintStream(stdout()));
            } else {
                fastqWriters[0] = new BasicFastqWriter(outputFile);
            }
            fastqWriters[1] = fastqWriters[0];
        } else /* interleaved */
        {
            if ((this.R1FileOut == null && this.R2FileOut != null) || (this.R1FileOut != null && this.R2FileOut == null)) {
                throw new IllegalStateException("Option  _R1FILEOUT  /  OPTION_R2FILEOUT  must be both defined");
            } else if (this.R1FileOut == null && this.R2FileOut == null) {
                if (this.outputFile == null) {
                    fastqWriters[0] = new BasicFastqWriter(new PrintStream(stdout()));
                } else {
                    fastqWriters[0] = new BasicFastqWriter(this.outputFile);
                }
                fastqWriters[1] = fastqWriters[0];
            } else {
                fastqWriters[0] = new BasicFastqWriter(this.R1FileOut);
                fastqWriters[1] = new BasicFastqWriter(this.R2FileOut);
            }
        }
        long count = 0L;
        final Bindings bindings = this.script.getEngine().createBindings();
        while (r.hasNext()) {
            final Record record = new Record(r.next());
            record.nLine = count;
            if (this.interleaved) {
                if (!r.hasNext())
                    throw new IOException("interleaved: mate missing");
                final Record mate = new Record(r.next());
                mate.nLine = count;
                final Pair pair = new Pair(record, mate);
                pair.nLine = count;
                bindings.put("pair", pair);
                if (!super.evalJavaScriptBoolean(this.script, bindings)) {
                    failing(pair.get(0));
                    failing(pair.get(1));
                } else {
                    fastqWriters[0].write(pair.get(0).toFastqRecord());
                    fastqWriters[1].write(pair.get(1).toFastqRecord());
                }
            } else {
                bindings.put("rec", record);
                if (!super.evalJavaScriptBoolean(this.script, bindings)) {
                    failing(record);
                } else {
                    fastqWriters[0].write(record.toFastqRecord());
                }
            }
            ++count;
            if (this.LIMIT > 0L && count >= this.LIMIT)
                break;
        }
        openFailing();
    } finally {
        CloserUtil.close(fastqWriters[0]);
        CloserUtil.close(fastqWriters[1]);
    }
}
Also used : PrintStream(java.io.PrintStream) BasicFastqWriter(htsjdk.samtools.fastq.BasicFastqWriter) FastqWriter(htsjdk.samtools.fastq.FastqWriter) FastqRecord(htsjdk.samtools.fastq.FastqRecord) BasicFastqWriter(htsjdk.samtools.fastq.BasicFastqWriter) IOException(java.io.IOException) Bindings(javax.script.Bindings)

Example 9 with BasicFastqWriter

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

the class FastqShuffle method doWork.

@Override
public int doWork(final List<String> args) {
    FastqReader r1 = null;
    FastqReader r2 = null;
    FastqWriter w = null;
    try {
        if (fileout == null) {
            w = new BasicFastqWriter(stdout());
        } else {
            w = new BasicFastqWriter(this.fileout);
        }
        if (args.isEmpty()) {
            LOG.info("Reading from stdin");
            r1 = new FourLinesFastqReader(stdin());
            if (interleaved_input) {
                runPaired(r1, null, w);
            } else {
                runSingle(r1, w);
            }
        } else if (args.size() == 1) {
            r1 = new FourLinesFastqReader(new File(args.get(0)));
            if (interleaved_input) {
                runPaired(r1, null, w);
            } else {
                runSingle(r1, w);
            }
        } else if (args.size() == 2) {
            r1 = new FourLinesFastqReader(new File(args.get(0)));
            r2 = new FourLinesFastqReader(new File(args.get(1)));
            runPaired(r1, r2, w);
        } else {
            LOG.error("illegal.number.of.arguments");
            return -1;
        }
        return 0;
    } catch (final Exception err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(r1);
        CloserUtil.close(r2);
        CloserUtil.close(w);
    }
}
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) FourLinesFastqReader(com.github.lindenb.jvarkit.util.picard.FourLinesFastqReader) BasicFastqWriter(htsjdk.samtools.fastq.BasicFastqWriter) File(java.io.File) IOException(java.io.IOException)

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