use of com.github.lindenb.jvarkit.fastq.FastqRecordPair 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);
}
}
use of com.github.lindenb.jvarkit.fastq.FastqRecordPair in project jvarkit by lindenb.
the class FastqJavascript method runPairedEnd.
@Override
protected int runPairedEnd(CloseableIterator<FastqRecordPair> iter, FastqPairedWriter fws) throws IOException {
try {
long count = 0L;
final Bindings bindings = this.script.getEngine().createBindings();
while (iter.hasNext()) {
final Pair p = new Pair(iter.next());
bindings.put("pair", p);
if (super.evalJavaScriptBoolean(this.script, bindings)) {
fws.write(p.rec1.toFastqRecord(), p.rec2.toFastqRecord());
}
++count;
if (this.LIMIT > 0L && count >= this.LIMIT)
break;
}
return 0;
} catch (final ScriptException err) {
getLogger().error(err);
return -1;
}
}
use of com.github.lindenb.jvarkit.fastq.FastqRecordPair 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);
}
}
use of com.github.lindenb.jvarkit.fastq.FastqRecordPair in project jvarkit by lindenb.
the class ConvertPhred64toFastq33 method runPairedEnd.
@Override
protected int runPairedEnd(CloseableIterator<FastqRecordPair> iter, FastqPairedWriter fws) throws IOException {
while (iter.hasNext()) {
final FastqRecordPair pair = iter.next();
fws.write(convert(pair.get(0)), convert(pair.get(1)));
}
return 0;
}
Aggregations