Search in sources :

Example 1 with FastqPairedReaderFactory

use of com.github.lindenb.jvarkit.fastq.FastqPairedReaderFactory in project jvarkit by lindenb.

the class FastqSplitInterleaved method doWork.

@Override
public int doWork(final List<String> args) {
    if (this.fileA.equals(this.fileB)) {
        LOG.error("R1 file==R2.file.");
        return -1;
    }
    CloseableIterator<FastqRecordPair> iter1 = null;
    FastqPairedWriter pairedWriter = null;
    try {
        final String input = oneFileOrNull(args);
        final FastqPairedReaderFactory fqprf = new FastqPairedReaderFactory().setValidateReadNames(this.validate_read_names);
        if (input == null) {
            iter1 = fqprf.open(stdin());
        } else {
            iter1 = fqprf.open(Paths.get(input));
        }
        final FastqPairedWriterFactory fqwf = new FastqPairedWriterFactory().setCreateMd5(this.write_md5).setAsyncIo(this.with_asynio);
        pairedWriter = fqwf.open(fileA, fileB);
        while (iter1.hasNext()) {
            pairedWriter.write(iter1.next());
        }
        iter1.close();
        pairedWriter.close();
        return 0;
    } catch (final Throwable err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(iter1);
        CloserUtil.close(pairedWriter);
    }
}
Also used : FastqRecordPair(com.github.lindenb.jvarkit.fastq.FastqRecordPair) FastqPairedWriter(com.github.lindenb.jvarkit.fastq.FastqPairedWriter) FastqPairedReaderFactory(com.github.lindenb.jvarkit.fastq.FastqPairedReaderFactory) FastqPairedWriterFactory(com.github.lindenb.jvarkit.fastq.FastqPairedWriterFactory)

Example 2 with FastqPairedReaderFactory

use of com.github.lindenb.jvarkit.fastq.FastqPairedReaderFactory in project jvarkit by lindenb.

the class FastqSplit method doWork.

@Override
public int doWork(final List<String> args) {
    if (this.per_file_number < 1 && this.split_number < 1) {
        LOG.error("Option -n or -s are undefined");
        return -1;
    }
    if (this.per_file_number > 0 && this.split_number > 0) {
        LOG.error("Both Options -n and -s are defined");
        return -1;
    }
    if (!this.basename.contains(TAG)) {
        LOG.error("basename doesn't contain " + TAG + ": " + basename);
        return -1;
    }
    PrintWriter manifest = null;
    try {
        if (this.manifestPath == null) {
            manifest = new PrintWriter(new NullOuputStream());
        } else {
            manifest = super.openPathOrStdoutAsPrintWriter(this.manifestPath);
        }
        if (args.size() == 2 || (this.input_is_interleaved && (args.isEmpty() || args.size() == 1))) {
            final List<FastqPairedWriter> fastqWriters = new ArrayList<>();
            FastqPairedWriter previous = null;
            int count_files = 0;
            long n = 0L;
            try (final CloseableIterator<FastqRecordPair> iter = new FastqPairedReaderFactory().setValidateReadNames(this.validate_read_names).open(args)) {
                while (iter.hasNext()) {
                    final FastqRecordPair pair = iter.next();
                    final FastqPairedWriter w;
                    /* split by number of reads per file */
                    if (this.per_file_number > 0) {
                        if (previous == null || n % this.per_file_number == 0) {
                            if (previous != null)
                                previous.close();
                            previous = openPairedWriter(count_files, manifest);
                            count_files++;
                            n = 0L;
                        }
                        w = previous;
                    } else /* split by file */
                    {
                        final int idx = (int) (n % this.split_number);
                        if (idx >= fastqWriters.size()) {
                            w = openPairedWriter(idx, manifest);
                            fastqWriters.add(w);
                        } else {
                            w = fastqWriters.get(idx);
                        }
                    }
                    w.write(pair);
                    n++;
                }
            }
            if (previous != null)
                previous.close();
            for (final FastqPairedWriter w : fastqWriters) w.close();
        } else if (args.isEmpty() || args.size() == 1) {
            if (this.output_is_interleaved) {
                LOG.error("Cannot set output is interleaved if input is not paired.");
                return -1;
            }
            final List<FastqWriter> fastqWriters = new ArrayList<>();
            FastqWriter previous = null;
            long n = 0L;
            int count_files = 0;
            final FastqReader iter;
            if (args.size() == 1) {
                iter = new FastqReader(new File(args.get(0)));
            } else {
                iter = new FastqReader(IOUtils.openStreamForBufferedReader(stdin()));
            }
            while (iter.hasNext()) {
                final FastqRecord rec = iter.next();
                final FastqWriter w;
                /* split by number of reads per file */
                if (this.per_file_number > 0) {
                    if (previous == null || n % this.per_file_number == 0) {
                        if (previous != null)
                            previous.close();
                        previous = this.openSingleWriter(count_files, manifest);
                        count_files++;
                        n = 0L;
                    }
                    w = previous;
                } else /* split by file */
                {
                    final int idx = (int) (n % this.split_number);
                    if (idx >= fastqWriters.size()) {
                        w = this.openSingleWriter(idx, manifest);
                        fastqWriters.add(w);
                    } else {
                        w = fastqWriters.get(idx);
                    }
                }
                w.write(rec);
                n++;
            }
            iter.close();
            for (final FastqWriter w : fastqWriters) w.close();
        } else {
            LOG.error("Illegal number of arguments.");
            return -1;
        }
        manifest.flush();
        manifest.close();
        return 0;
    } catch (final Throwable err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(manifest);
    }
}
Also used : ArrayList(java.util.ArrayList) FastqRecord(htsjdk.samtools.fastq.FastqRecord) FastqPairedReaderFactory(com.github.lindenb.jvarkit.fastq.FastqPairedReaderFactory) FastqRecordPair(com.github.lindenb.jvarkit.fastq.FastqRecordPair) FastqPairedWriter(com.github.lindenb.jvarkit.fastq.FastqPairedWriter) FastqReader(htsjdk.samtools.fastq.FastqReader) FastqWriter(htsjdk.samtools.fastq.FastqWriter) NullOuputStream(com.github.lindenb.jvarkit.io.NullOuputStream) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) PrintWriter(java.io.PrintWriter)

Aggregations

FastqPairedReaderFactory (com.github.lindenb.jvarkit.fastq.FastqPairedReaderFactory)2 FastqPairedWriter (com.github.lindenb.jvarkit.fastq.FastqPairedWriter)2 FastqRecordPair (com.github.lindenb.jvarkit.fastq.FastqRecordPair)2 FastqPairedWriterFactory (com.github.lindenb.jvarkit.fastq.FastqPairedWriterFactory)1 NullOuputStream (com.github.lindenb.jvarkit.io.NullOuputStream)1 FastqReader (htsjdk.samtools.fastq.FastqReader)1 FastqRecord (htsjdk.samtools.fastq.FastqRecord)1 FastqWriter (htsjdk.samtools.fastq.FastqWriter)1 File (java.io.File)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1