use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress in project jvarkit by lindenb.
the class VcfTail method doVcfToVcf.
@Override
protected int doVcfToVcf(final String inputName, final VcfIterator in, final VariantContextWriter delegate) {
try {
final VariantContextWriter out = this.component.open(delegate);
out.writeHeader(in.getHeader());
final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(in.getHeader()).logger(LOG);
while (in.hasNext()) {
out.add(progress.watch(in.next()));
}
progress.finish();
out.close();
return 0;
} catch (final Exception err) {
LOG.error(err);
return -1;
}
}
use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress in project jvarkit by lindenb.
the class Sam2Tsv method scan.
private void scan(final SamReader r) {
final Row row = new Row();
SAMRecordIterator iter = null;
try {
final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(r.getFileHeader());
iter = r.iterator();
while (iter.hasNext()) {
row.rec = progress.watch(iter.next());
printAln(row);
if (this.out.checkError())
break;
}
progress.finish();
} catch (final Exception err) {
LOG.error("scan error:", err);
throw new RuntimeException(String.valueOf(err.getMessage()), err);
} finally {
CloserUtil.close(iter);
}
}
use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress in project jvarkit by lindenb.
the class SamGrep method doWork.
@Override
public int doWork(List<String> args) {
readNames.clear();
if (namefile != null) {
BufferedReader in = null;
try {
in = IOUtils.openFileForBufferedReading(this.namefile);
String line;
while ((line = in.readLine()) != null) {
line = line.trim();
if (line.isEmpty())
continue;
readNames.put(line, 0);
}
} catch (Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(in);
}
}
for (final String line : this.nameStrings) {
readNames.put(line, 0);
}
if (readNames.isEmpty()) {
LOG.warn("no read found.");
}
SAMFileWriter sfw = null;
SAMFileWriter samStdout = null;
SamReader sfr = null;
try {
sfr = super.openSamReader(oneFileOrNull(args));
final SAMFileHeader header = sfr.getFileHeader().clone();
final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(header);
if (this.outputFile == null) {
sfw = this.writingBamArgs.openSAMFileWriter(null, header, true);
} else {
samStdout = this.writingBamArgs.openSAMFileWriter(null, header, true);
sfw = this.writingBamArgs.openSAMFileWriter(outputFile, header, true);
}
SAMRecordIterator iter = sfr.iterator();
while (iter.hasNext()) {
boolean keep = false;
final SAMRecord rec = progress.watch(iter.next());
if (samStdout != null)
samStdout.addAlignment(rec);
Integer count = readNames.get(rec.getReadName());
if (count != null) {
keep = true;
}
if (this.inverse)
keep = !keep;
if (keep) {
sfw.addAlignment(rec);
}
if (n_before_remove != -1 && !inverse && keep) {
count++;
if (count >= n_before_remove) {
readNames.remove(rec.getReadName());
if (samStdout == null && readNames.isEmpty())
break;
} else {
readNames.put(rec.getReadName(), count);
}
}
}
progress.finish();
return RETURN_OK;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(samStdout);
CloserUtil.close(sfw);
CloserUtil.close(sfr);
}
}
use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress in project jvarkit by lindenb.
the class SamCustomSortJdk method doWork.
@Override
public int doWork(final List<String> args) {
SAMRecordIterator iter = null;
SamReader samFileReader = null;
SAMFileWriter sw = null;
SortingCollection<SAMRecord> sorter = null;
CloseableIterator<SAMRecord> iter2 = null;
try {
final String code;
if (this.scriptFile != null) {
code = IOUtil.slurp(this.scriptFile);
} else if (!StringUtil.isBlank(this.scriptExpr)) {
code = this.scriptExpr;
} else {
LOG.error("Option -e or -f are required. The content of those empty mut be not empty");
return -1;
}
final Random rand = new Random(System.currentTimeMillis());
final String javaClassName = SamCustomSortJdk.class.getSimpleName() + "Custom" + Math.abs(rand.nextInt());
final StringWriter codeWriter = new StringWriter();
final PrintWriter pw = new PrintWriter(codeWriter);
pw.println("import java.util.*;");
pw.println("import java.util.stream.*;");
pw.println("import java.util.function.*;");
pw.println("import htsjdk.samtools.*;");
pw.println("import htsjdk.samtools.util.*;");
pw.println("import com.github.lindenb.jvarkit.tools.misc.IlluminaReadName;");
pw.println("import javax.annotation.Generated;");
pw.println("@Generated(value=\"" + SamCustomSortJdk.class.getSimpleName() + "\",date=\"" + new Iso8601Date(new Date()) + "\")");
pw.println("public class " + javaClassName + " extends " + AbstractSamComparator.class.getName().replace('$', '.') + " {");
pw.println(" public " + javaClassName + "(final SAMFileHeader header) {");
pw.println(" super(header);");
pw.println(" }");
if (user_code_is_body) {
pw.println(" //user's code starts here");
pw.println(code);
pw.println(" // user's code ends here");
} else {
pw.println(" @Override");
pw.println(" public int compare(final SAMRecord R1,final SAMRecord R2) {");
pw.println(" /** user's code starts here */");
pw.println(code);
pw.println("/** user's code ends here */");
pw.println(" }");
}
pw.println("}");
pw.flush();
if (!hideGeneratedCode) {
LOG.debug(" Compiling :\n" + InMemoryCompiler.beautifyCode(codeWriter.toString()));
}
if (this.saveCodeInDir != null) {
PrintWriter cw = null;
try {
IOUtil.assertDirectoryIsWritable(this.saveCodeInDir);
cw = new PrintWriter(new File(this.saveCodeInDir, javaClassName + ".java"));
cw.write(codeWriter.toString());
cw.flush();
cw.close();
cw = null;
LOG.info("saved " + javaClassName + ".java in " + this.saveCodeInDir);
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(cw);
}
}
final InMemoryCompiler inMemoryCompiler = new InMemoryCompiler();
final Class<?> compiledClass = inMemoryCompiler.compileClass(javaClassName, codeWriter.toString());
final Constructor<?> ctor = compiledClass.getDeclaredConstructor(SAMFileHeader.class);
samFileReader = openSamReader(oneFileOrNull(args));
final SAMFileHeader headerIn = samFileReader.getFileHeader();
final StableSort customComparator = new StableSort((Comparator<SAMRecord>) ctor.newInstance(headerIn));
final BAMRecordCodec bamRecordCodec = new BAMRecordCodec(headerIn);
sorter = SortingCollection.newInstance(SAMRecord.class, bamRecordCodec, customComparator, this.writingSortingCollection.getMaxRecordsInRam(), this.writingSortingCollection.getTmpPaths());
sorter.setDestructiveIteration(true);
SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(headerIn).logger(LOG);
iter = samFileReader.iterator();
while (iter.hasNext()) {
sorter.add(progress.watch(iter.next()));
}
samFileReader.close();
samFileReader = null;
sorter.doneAdding();
final SAMFileHeader headerOut = headerIn.clone();
headerOut.setSortOrder(SAMFileHeader.SortOrder.unsorted);
headerOut.addComment(getProgramName() + " " + getVersion() + " " + getProgramCommandLine());
sw = this.writingBamArgs.openSAMFileWriter(this.outputFile, headerOut, false);
progress = new SAMSequenceDictionaryProgress(headerIn).logger(LOG);
iter2 = sorter.iterator();
while (iter2.hasNext()) {
sw.addAlignment(progress.watch(iter2.next()));
}
iter2.close();
iter2 = null;
sw.close();
sw = null;
progress.finish();
return RETURN_OK;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
try {
if (sorter != null)
sorter.cleanup();
} catch (Exception e) {
}
CloserUtil.close(iter);
CloserUtil.close(iter2);
CloserUtil.close(samFileReader);
CloserUtil.close(sw);
}
}
use of com.github.lindenb.jvarkit.util.picard.SAMSequenceDictionaryProgress in project jvarkit by lindenb.
the class SamJavascript method doWork.
@Override
public int doWork(final List<String> args) {
SAMRecordIterator iter = null;
SamReader samFileReader = null;
SAMFileWriter sw = null;
try {
this.script = super.compileJavascript(this.jsExpression, this.jsFile);
samFileReader = openSamReader(oneFileOrNull(args));
final SAMFileHeader header = samFileReader.getFileHeader();
sw = writingBamArgs.openSAMFileWriter(outputFile, header, true);
long count = 0L;
final Bindings bindings = this.script.getEngine().createBindings();
bindings.put("header", samFileReader.getFileHeader());
final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(header).logger(LOG);
iter = samFileReader.iterator();
while (iter.hasNext()) {
final SAMRecord record = iter.next();
progress.watch(record);
bindings.put("record", record);
if (super.evalJavaScriptBoolean(this.script, bindings)) {
++count;
sw.addAlignment(record);
if (this.LIMIT > 0L && count >= this.LIMIT)
break;
} else {
failing(record, header);
}
}
sw.close();
/* create empty if never called */
openFailing(header);
return RETURN_OK;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(iter);
CloserUtil.close(samFileReader);
CloserUtil.close(sw);
CloserUtil.close(failingReadsWriter);
}
}
Aggregations